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