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