]> git.sesse.net Git - vlc/blob - extras/MacOSX/Framework/Sources/VLCMediaListAspect.m
MacOSX/Framework/VLCMediaListAspect.m: Implement -hierarchicalAspect and -flatAspect...
[vlc] / extras / MacOSX / Framework / Sources / VLCMediaListAspect.m
1 /*****************************************************************************
2  * VLCMediaList.m: VLC.framework VLCMediaList implementation
3  *****************************************************************************
4  * Copyright (C) 2007 Pierre d'Herbemont
5  * Copyright (C) 2007 the VideoLAN team
6  * $Id$
7  *
8  * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #import "VLCMediaListAspect.h"
26 #import "VLCLibrary.h"
27 #import "VLCEventManager.h"
28 #import "VLCLibVLCBridging.h"
29 #include <vlc/vlc.h>
30 #include <vlc/libvlc.h>
31
32 // TODO: Documentation
33 @interface VLCMediaListAspect (Private)
34 /* Initializers */
35 - (void)initInternalMediaListView;
36 @end
37
38 @implementation VLCMediaListAspect (KeyValueCodingCompliance)
39 /* For the @"Media" key */
40 - (int) countOfMedia
41 {
42     return [self count];
43 }
44 - (id) objectInMediaAtIndex:(int)i
45 {
46     return [self mediaAtIndex:i];
47 }
48 @end
49
50 /* libvlc event callback */
51 static void HandleMediaListViewItemAdded(const libvlc_event_t *event, void *user_data)
52 {
53     id self = user_data;
54     int index = event->u.media_list_view_item_added.index;
55     [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"Media"];
56 }
57 static void HandleMediaListViewWillAddItem(const libvlc_event_t *event, void *user_data)
58 {
59     id self = user_data;
60     int index = event->u.media_list_view_will_add_item.index;
61     [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"Media"];
62 }
63 static void HandleMediaListViewItemDeleted( const libvlc_event_t * event, void * user_data)
64 {
65     id self = user_data;
66     int index = event->u.media_list_view_will_add_item.index;
67     [self didChange:NSKeyValueChangeRemoval valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"Media"];
68 }
69 static void HandleMediaListViewWillDeleteItem(const libvlc_event_t *event, void *user_data)
70 {
71     id self = user_data;
72     int index = event->u.media_list_view_will_add_item.index;
73     [self willChange:NSKeyValueChangeRemoval valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"Media"];
74 }
75
76 @implementation VLCMediaListAspect
77 - (void)dealloc
78 {
79     // Release allocated memory
80     libvlc_media_list_release(p_mlv);
81     
82     [super dealloc];
83 }
84 - (VLCMedia *)mediaAtIndex:(int)index
85 {
86     libvlc_exception_t p_e;
87     libvlc_exception_init( &p_e );
88     libvlc_media_descriptor_t *p_md = libvlc_media_list_view_item_at_index( p_mlv, index, &p_e );
89     quit_on_exception( &p_e );
90     
91     // Returns local object for media descriptor, searchs for user data first.  If not found it creates a 
92     // new cocoa object representation of the media descriptor.
93     return [VLCMedia mediaWithLibVLCMediaDescriptor:p_md];
94 }
95
96 - (int)count
97 {
98     libvlc_exception_t p_e;
99     libvlc_exception_init( &p_e );
100     int result = libvlc_media_list_view_count( p_mlv, &p_e );
101     quit_on_exception( &p_e );
102
103     return result;
104 }
105 @end
106
107 @implementation VLCMediaListAspect (LibVLCBridging)
108 + (id)mediaListAspectWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv;
109 {
110     return [[[VLCMediaList alloc] initWithLibVLCMediaList:p_new_mlv] autorelease];
111 }
112
113 - (id)initWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv;
114 {
115     if( self = [super init] )
116     {
117         p_mlv = p_new_mlv;
118         libvlc_media_list_view_retain(p_mlv);
119         [self initInternalMediaListView];
120     }
121     return self;
122 }
123
124 - (libvlc_media_list_view_t *)libVLCMediaListView
125 {
126     return (libvlc_media_list_view_t *)p_mlv;
127 }
128 @end
129
130 @implementation VLCMediaListAspect (Private)
131 - (void)initInternalMediaListView
132 {
133     libvlc_exception_t e;
134     libvlc_exception_init( &e );
135
136     libvlc_event_manager_t *p_em = libvlc_media_list_event_manager( p_mlv, &e );
137
138     /* Add internal callback */
139     libvlc_event_attach( p_em, libvlc_MediaListViewItemAdded,   HandleMediaListViewItemAdded,   self, &e );
140     libvlc_event_attach( p_em, libvlc_MediaListViewWillAddItem, HandleMediaListViewWillAddItem, self, &e );
141     libvlc_event_attach( p_em, libvlc_MediaListViewItemDeleted, HandleMediaListViewItemDeleted, self, &e );
142     libvlc_event_attach( p_em, libvlc_MediaListViewWillDeleteItem, HandleMediaListViewWillDeleteItem, self, &e );
143
144     quit_on_exception( &e );
145 }
146 @end
147