MacOSX/Framework: s/PrivateAPI/Private/
[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
39 /* Initializers */
40 - (void)initInternalMediaList;
41
42 /* Libvlc event bridges */
43 - (void)mediaListItemAdded:(NSArray *)args;
44 - (void)mediaListItemRemoved:(NSNumber *)index;
45
46 @end
47
48 /* libvlc event callback */
49 static void HandleMediaListItemAdded(const libvlc_event_t *event, void *user_data)
50 {
51     id self = user_data;
52         
53         // Check to see if the last item added is this item we're trying to introduce
54         // If no, then add the item to the local list, otherwise, the item has already 
55         // been added
56         [[VLCEventManager sharedManager] callOnMainThreadObject:self 
57                                                                                                  withMethod:@selector(mediaListItemAdded:) 
58                                                                            withArgumentAsObject:[NSArray arrayWithObjects:[VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_list_item_added.item],
59                                                                                    [NSNumber numberWithInt:event->u.media_list_item_added.index], nil]];
60 }
61
62 static void HandleMediaListItemDeleted( const libvlc_event_t * event, void * user_data)
63 {
64     id self = user_data;
65         
66         // Check to see if the last item deleted is this item we're trying delete now.
67         // If no, then delete the item from the local list, otherwise, the item has already 
68         // been deleted
69         [[VLCEventManager sharedManager] callOnMainThreadObject:self 
70                                                                                                  withMethod:@selector(mediaListItemRemoved:) 
71                                                                            withArgumentAsObject:[NSNumber numberWithInt:event->u.media_list_item_deleted.index]];
72 }
73
74 @implementation VLCMediaList
75
76 - (id)init
77 {
78     if (self = [super init])
79     {
80                 // Create a new libvlc media list instance
81         libvlc_exception_t p_e;
82         libvlc_exception_init(&p_e);
83         p_mlist = libvlc_media_list_new([VLCLibrary sharedInstance], &p_e);
84         quit_on_exception(&p_e);
85                 
86                 // Initialize internals to defaults
87                 delegate = 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     libvlc_media_list_release(p_mlist);
114         
115     [super dealloc];
116 }
117
118 - (void)setDelegate:(id)value
119 {
120         delegate = value;
121 }
122
123 - (id)delegate
124 {
125         return delegate;
126 }
127
128 - (void)lock
129 {
130     libvlc_media_list_lock(p_mlist);
131 }
132
133 - (void)unlock
134 {
135     libvlc_media_list_unlock(p_mlist);
136 }
137
138 - (int)addMedia:(VLCMedia *)media
139 {
140         int index = [self count];
141         [self insertMedia:media atIndex:index];
142         return index;
143 }
144
145 - (void)insertMedia:(VLCMedia *)media atIndex: (int)index
146 {
147         [media retain];
148         
149         // Add it to the libvlc's medialist
150     libvlc_exception_t p_e;
151     libvlc_exception_init( &p_e );
152     libvlc_media_list_insert_media_descriptor(p_mlist, [media libVLCMediaDescriptor], index, &p_e);
153     quit_on_exception(&p_e);
154 }
155
156 - (void)removeMediaAtIndex:(int)index
157 {
158         [[self mediaAtIndex:index] release];
159
160         // Remove it from the libvlc's medialist
161     libvlc_exception_t p_e;
162     libvlc_exception_init(&p_e);
163     libvlc_media_list_remove_index(p_mlist, index, &p_e);
164     quit_on_exception(&p_e);
165 }
166
167 - (VLCMedia *)mediaAtIndex:(int)index
168 {
169         libvlc_exception_t p_e;
170         libvlc_exception_init(&p_e);
171         libvlc_media_descriptor_t *p_md = libvlc_media_list_item_at_index(p_mlist, index, &p_e);
172         quit_on_exception(&p_e);
173         
174         // Returns local object for media descriptor, searchs for user data first.  If not found it creates a 
175         // new cocoa object representation of the media descriptor.
176         return [VLCMedia mediaWithLibVLCMediaDescriptor:p_md];
177 }
178
179 - (int)count
180 {
181         libvlc_exception_t p_e;
182         libvlc_exception_init(&p_e);
183         int result = libvlc_media_list_count(p_mlist, &p_e);
184         quit_on_exception(&p_e);
185
186         return result;
187 }
188
189 - (int)indexOfMedia:(VLCMedia *)media
190 {
191         libvlc_exception_t p_e;
192         libvlc_exception_init(&p_e);
193     int result = libvlc_media_list_index_of_item(p_mlist, [media libVLCMediaDescriptor], &p_e);
194         quit_on_exception(&p_e);
195         
196         return result;
197 }
198
199 - (NSArray *)sublists
200 {
201     NSMutableArray *ret = [[NSMutableArray alloc] initWithCapacity: 0];
202     int i, count;
203
204     libvlc_exception_t p_e;
205     libvlc_exception_init(&p_e);
206     count = libvlc_media_list_count(p_mlist, &p_e);
207     quit_on_exception(&p_e);
208
209     for(i = 0; i < count; i++)
210     {
211         libvlc_media_descriptor_t *p_md;
212         libvlc_media_list_t *p_submlist;
213         p_md = libvlc_media_list_item_at_index(p_mlist, i, NULL);
214         p_submlist = libvlc_media_descriptor_subitems(p_md, NULL);
215         if(p_submlist)
216         {
217             [ret addObject:[VLCMediaList medialistWithLibVLCMediaList:p_submlist]];
218             libvlc_media_list_release(p_submlist);
219         }
220         libvlc_media_descriptor_release(p_md);
221     }
222     return [ret autorelease];
223 }
224
225 //- (VLCMediaList *)flatPlaylist
226 //{
227 //    VLCMediaList * flatPlaylist;
228 //    libvlc_exception_t p_e;
229 //    libvlc_exception_init( &p_e );
230 //    libvlc_media_list_t * p_flat_mlist = libvlc_media_list_flat_media_list( p_mlist, &p_e );
231 //    quit_on_exception( &p_e );
232 //    flatPlaylist = [VLCMediaList medialistWithLibVLCMediaList: p_flat_mlist];
233 //    libvlc_media_list_release( p_flat_mlist );
234 //    return flatPlaylist;
235 //}
236 //
237 //- (VLCMedia *)providerMedia
238 //{
239 //    VLCMedia * ret;
240 //    libvlc_exception_t p_e;
241 //    libvlc_exception_init( &p_e );
242 //    libvlc_media_descriptor_t * p_md = libvlc_media_list_media_descriptor( p_mlist, &p_e );
243 //    ret = [VLCMedia mediaWithLibVLCMediaDescriptor: p_md];
244 //    libvlc_media_descriptor_release( p_md );
245 //    quit_on_exception( &p_e );
246 //    return ret;
247 //}
248
249 @end
250
251 @implementation VLCMediaList (LibVLCBridging)
252
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         [self initInternalMediaList];
265     }
266     return self;
267 }
268
269 - (void *)libVLCMediaList
270 {
271     return p_mlist;
272 }
273
274 @end
275
276 @implementation VLCMediaList (Private)
277
278 - (void)initInternalMediaList
279 {
280         // Add event callbacks
281     [self lock];
282     libvlc_exception_t p_e;
283     libvlc_exception_init(&p_e);
284
285     libvlc_event_manager_t *p_em = libvlc_media_list_event_manager(p_mlist, &p_e);
286     libvlc_event_attach(p_em, libvlc_MediaListItemAdded,   HandleMediaListItemAdded,   self, &p_e);
287     libvlc_event_attach(p_em, libvlc_MediaListItemDeleted, HandleMediaListItemDeleted, self, &p_e);
288     [self unlock];
289         
290     quit_on_exception( &p_e );
291 }
292
293 - (void)mediaListItemAdded:(NSArray *)args
294 {
295         VLCMedia *media = [args objectAtIndex:0];
296         NSNumber *index = [args objectAtIndex:1];
297         
298         // Post the notification
299         [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemAdded
300                                                                                                                 object:self
301                                                                                                           userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
302                                                                                                                   media, @"media",
303                                                                                                                   index, @"index",
304                                                                                                                   nil]];
305         
306         // Let the delegate know that the item was added
307         if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaAdded:atIndex:)])
308                 [delegate mediaList:self mediaAdded:media atIndex:[index intValue]];
309 }
310
311 - (void)mediaListItemRemoved:(NSNumber *)index
312 {
313         // Post the notification
314         [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaListItemDeleted 
315                                                                                                                 object:self
316                                                                                                           userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
317                                                                                                                   index, @"index",
318                                                                                                                   nil]];
319         
320         // Let the delegate know that the item is being removed
321         if (delegate && [delegate respondsToSelector:@selector(mediaList:mediaRemovedAtIndex:)])
322                 [delegate mediaList:self mediaRemovedAtIndex:index];
323 }
324
325 @end
326