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