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