MacOSX/Framework/VLCMediaList.m: Cache the media list objects in a NSMutableArray...
[vlc.git] / extras / MacOSX / Framework / Sources / VLCMediaList.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 "VLCMediaList.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 /* Notification Messages */
33 NSString *VLCMediaListItemAdded        = @"VLCMediaListItemAdded";
34 NSString *VLCMediaListItemDeleted    = @"VLCMediaListItemDeleted";
35
36 // TODO: Documentation
37 @interface VLCMediaList (Private)
38 /* Initializers */
39 - (void)initInternalMediaList;
40
41 /* Libvlc event bridges */
42 - (void)mediaListItemAdded:(NSDictionary *)args;
43 - (void)mediaListItemRemoved:(NSNumber *)index;
44 @end
45
46 /* libvlc event callback */
47 static void HandleMediaListItemAdded(const libvlc_event_t *event, void *user_data)
48 {
49     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
50     id self = user_data;
51     [[VLCEventManager sharedManager] callOnMainThreadObject:self 
52                                                  withMethod:@selector(mediaListItemAdded:) 
53                                        withArgumentAsObject:[NSDictionary dictionaryWithObjectsAndKeys:
54                                                           [VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_list_item_added.item], @"media",
55                                                           [NSNumber numberWithInt:event->u.media_list_item_added.index], @"index",
56                                                           nil]];
57     [pool release];
58 }
59
60 static void HandleMediaListItemDeleted( const libvlc_event_t * event, void * user_data)
61 {
62     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
63     id self = user_data;
64     [[VLCEventManager sharedManager] callOnMainThreadObject:self 
65                                                  withMethod:@selector(mediaListItemRemoved:) 
66                                        withArgumentAsObject:[NSNumber numberWithInt:event->u.media_list_item_deleted.index]];
67     [pool release];
68 }
69
70 @implementation VLCMediaList (KeyValueCodingCompliance)
71 /* For the @"Media" key */
72 - (int) countOfMedia
73 {
74     return [self count];
75 }
76
77 - (id) objectInMediaAtIndex:(int)i
78 {
79     return [self mediaAtIndex:i];
80 }
81 @end
82
83 @implementation VLCMediaList
84 - (id)init
85 {
86     if (self = [super init])
87     {
88         // Create a new libvlc media list instance
89         libvlc_exception_t p_e;
90         libvlc_exception_init(&p_e);
91         p_mlist = libvlc_media_list_new([VLCLibrary sharedInstance], &p_e);
92         quit_on_exception(&p_e);
93         
94         // Initialize internals to defaults
95         cachedMedia = [[NSMutableArray alloc] init];
96         delegate = nil;
97         [self initInternalMediaList];
98     }
99     return self;
100 }
101
102 - (void)release
103 {
104     @synchronized(self)
105     {
106         if([self retainCount] <= 1)
107         {
108             /* We must make sure we won't receive new event after an upcoming dealloc
109              * We also may receive a -retain in some event callback that may occcur
110              * Before libvlc_event_detach. So this can't happen in dealloc */
111             libvlc_event_manager_t * p_em = libvlc_media_list_event_manager(p_mlist, NULL);
112             libvlc_event_detach(p_em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, self, NULL);
113             libvlc_event_detach(p_em, libvlc_MediaListItemAdded,   HandleMediaListItemAdded,   self, NULL);
114         }
115         [super release];
116     }
117 }
118
119 - (void)dealloc
120 {
121     // Release allocated memory
122     libvlc_media_list_release(p_mlist);
123     [cachedMedia release];
124     [super dealloc];
125 }
126
127 - (void)setDelegate:(id)value
128 {
129     delegate = value;
130 }
131
132 - (id)delegate
133 {
134     return delegate;
135 }
136
137 - (void)lock
138 {
139     libvlc_media_list_lock( p_mlist );
140 }
141
142 - (void)unlock
143 {
144     libvlc_media_list_unlock( p_mlist );
145 }
146
147 - (int)addMedia:(VLCMedia *)media
148 {
149     int index = [self count];
150     [self insertMedia:media atIndex:index];
151     return index;
152 }
153
154 - (void)insertMedia:(VLCMedia *)media atIndex: (int)index
155 {
156     [media retain];
157     
158     // Add it to the libvlc's medialist
159     libvlc_exception_t p_e;
160     libvlc_exception_init( &p_e );
161     libvlc_media_list_insert_media_descriptor( p_mlist, [media libVLCMediaDescriptor], index, &p_e );
162     quit_on_exception( &p_e );
163 }
164
165 - (void)removeMediaAtIndex:(int)index
166 {
167     [[self mediaAtIndex:index] release];
168
169     // Remove it from the libvlc's medialist
170     libvlc_exception_t p_e;
171     libvlc_exception_init( &p_e );
172     libvlc_media_list_remove_index( p_mlist, index, &p_e );
173     quit_on_exception( &p_e );
174 }
175
176 - (VLCMedia *)mediaAtIndex:(int)index
177 {
178     return [cachedMedia objectAtIndex:index];
179 }
180
181 - (int)count
182 {
183     return [cachedMedia count];
184 }
185
186 - (int)indexOfMedia:(VLCMedia *)media
187 {
188     libvlc_exception_t p_e;
189     libvlc_exception_init( &p_e );
190     int result = libvlc_media_list_index_of_item( p_mlist, [media libVLCMediaDescriptor], &p_e );
191     quit_on_exception( &p_e );
192     
193     return result;
194 }
195
196 /* Media list aspect */
197 - (VLCMediaListAspect *)hierarchicalAspect
198 {
199     VLCMediaListAspect * hierarchicalAspect;
200     libvlc_media_list_view_t * p_mlv = libvlc_media_list_hierarchical_view( p_mlist, NULL );
201     hierarchicalAspect = [VLCMediaListAspect mediaListAspectWithLibVLCMediaListView: p_mlv];
202     libvlc_media_list_view_release( p_mlv );
203     return hierarchicalAspect;
204 }
205
206 - (VLCMediaListAspect *)flatAspect
207 {
208     VLCMediaListAspect * flatAspect;
209     libvlc_media_list_view_t * p_mlv = libvlc_media_list_flat_view( p_mlist, NULL );
210     flatAspect = [VLCMediaListAspect mediaListAspectWithLibVLCMediaListView: p_mlv];
211     libvlc_media_list_view_release( p_mlv );
212     return flatAspect;
213 }
214
215 @end
216
217 @implementation VLCMediaList (LibVLCBridging)
218 + (id)mediaListWithLibVLCMediaList:(void *)p_new_mlist;
219 {
220     return [[[VLCMediaList alloc] initWithLibVLCMediaList:p_new_mlist] autorelease];
221 }
222
223 - (id)initWithLibVLCMediaList:(void *)p_new_mlist;
224 {
225     if( self = [super init] )
226     {
227         p_mlist = p_new_mlist;
228         libvlc_media_list_retain(p_mlist);
229         libvlc_media_list_lock(p_mlist);
230         cachedMedia = [[NSMutableArray alloc] initWithCapacity:libvlc_media_list_count(p_mlist, NULL)];
231         int i, count = libvlc_media_list_count(p_mlist, NULL);
232         for( i = 0; i < count; i++ )
233         {
234             libvlc_media_descriptor_t * p_md = libvlc_media_list_item_at_index(p_mlist, i, NULL);
235             [cachedMedia addObject:[VLCMedia mediaWithLibVLCMediaDescriptor: p_md]];
236             libvlc_media_descriptor_release(p_md);
237         }
238         [self initInternalMediaList];
239         libvlc_media_list_unlock(p_mlist);
240     }
241     return self;
242 }
243
244 - (void *)libVLCMediaList
245 {
246     return p_mlist;
247 }
248 @end
249
250 @implementation VLCMediaList (Private)
251 - (void)initInternalMediaList
252 {
253     // Add event callbacks
254     [self lock];
255     libvlc_exception_t p_e;
256     libvlc_exception_init(&p_e);
257
258     libvlc_event_manager_t *p_em = libvlc_media_list_event_manager( p_mlist, &p_e );
259     libvlc_event_attach( p_em, libvlc_MediaListItemAdded,   HandleMediaListItemAdded,   self, &p_e );
260     libvlc_event_attach( p_em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, self, &p_e );
261     [self unlock];
262     
263     quit_on_exception( &p_e );
264 }
265
266 - (void)mediaListItemAdded:(NSDictionary *)args
267 {
268     int index = [[args objectForKey:@"index"] intValue];
269     VLCMedia * media = [args objectForKey:@"media"];
270
271     [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"Media"];
272     [cachedMedia insertObject:media atIndex:index];
273     [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"Media"];
274
275     // Post the notification
276     [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemAdded
277                                                         object:self
278                                                       userInfo:args];
279
280     // Let the delegate know that the item was added
281     if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaAdded:atIndex:)])
282         [delegate mediaList:self mediaAdded:media atIndex:index];
283 }
284
285 - (void)mediaListItemRemoved:(NSNumber *)index
286 {
287     [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"Media"];
288     [cachedMedia removeObjectAtIndex:[index intValue]];
289     [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"Media"];
290
291     // Post the notification
292     [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemDeleted 
293                                                         object:self
294                                                       userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
295                                                           index, @"index",
296                                                           nil]];
297
298     // Let the delegate know that the item is being removed
299     if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaRemovedAtIndex:)])
300         [delegate mediaList:self mediaRemovedAtIndex:[index intValue]];
301 }
302 @end
303