1 /*****************************************************************************
2 * VLCMediaList.m: VLC.framework VLCMediaList implementation
3 *****************************************************************************
4 * Copyright (C) 2007 Pierre d'Herbemont
5 * Copyright (C) 2007 the VideoLAN team
8 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
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.
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.
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 *****************************************************************************/
25 #import "VLCMediaListAspect.h"
26 #import "VLCLibrary.h"
27 #import "VLCEventManager.h"
28 #import "VLCLibVLCBridging.h"
30 #include <vlc/libvlc.h>
32 // TODO: Documentation
33 @interface VLCMediaListAspect (Private)
35 - (void)initInternalMediaListView;
37 - (void)mediaListViewItemAdded:(NSArray *)args;
38 - (void)mediaListViewItemRemoved:(NSNumber *)index;
41 @implementation VLCMediaListAspectNode
47 return self.children == NULL;
52 [self.children release];
57 @implementation VLCMediaListAspect (KeyValueCodingCompliance)
58 /* For the @"media" key */
61 return [cachedNode count];
63 - (id) objectInMediaAtIndex:(int)i
65 return [[cachedNode objectAtIndex:i] media];
67 /* For the @"node" key */
70 return [cachedNode count];
72 - (id) objectInNodeAtIndex:(int)i
74 return [cachedNode objectAtIndex:i];
78 /* libvlc event callback */
79 static void HandleMediaListViewItemAdded(const libvlc_event_t *event, void *user_data)
81 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
83 [[VLCEventManager sharedManager] callOnMainThreadObject:self
84 withMethod:@selector(mediaListViewItemAdded:)
85 withArgumentAsObject:[NSArray arrayWithObject:[NSDictionary dictionaryWithObjectsAndKeys:
86 [VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_list_item_added.item], @"media",
87 [NSNumber numberWithInt:event->u.media_list_item_added.index], @"index",
92 static void HandleMediaListViewItemDeleted( const libvlc_event_t * event, void * user_data)
94 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
96 [[VLCEventManager sharedManager] callOnMainThreadObject:self
97 withMethod:@selector(mediaListViewItemRemoved:)
98 withArgumentAsObject:[NSNumber numberWithInt:event->u.media_list_item_deleted.index]];
102 @implementation VLCMediaListAspect
105 // Release allocated memory
106 libvlc_media_list_view_release(p_mlv);
107 [cachedNode release];
108 if( ownHisMediaList )
109 [parentMediaList release];
112 - (NSString *)description
114 NSMutableString *content = [NSMutableString string];
116 for( i = 0; i < [self count]; i++)
118 [content appendFormat:@"%@\n", [self mediaAtIndex: i]];
120 return [NSString stringWithFormat:@"<%@ %p> {\n%@}", [self className], self, content];
123 - (VLCMedia *)mediaAtIndex:(int)index
125 libvlc_exception_t p_e;
126 libvlc_exception_init( &p_e );
127 libvlc_media_descriptor_t *p_md = libvlc_media_list_view_item_at_index( p_mlv, index, &p_e );
128 quit_on_exception( &p_e );
130 // Returns local object for media descriptor, searchs for user data first. If not found it creates a
131 // new cocoa object representation of the media descriptor.
132 return [VLCMedia mediaWithLibVLCMediaDescriptor:p_md];
135 - (VLCMediaListAspect *)childrenAtIndex:(int)index
137 libvlc_exception_t p_e;
138 libvlc_exception_init( &p_e );
139 libvlc_media_list_view_t *p_sub_mlv = libvlc_media_list_view_children_at_index( p_mlv, index, &p_e );
140 quit_on_exception( &p_e );
145 // Returns local object for media descriptor, searchs for user data first. If not found it creates a
146 // new cocoa object representation of the media descriptor.
147 return [VLCMediaListAspect mediaListAspectWithLibVLCMediaListView:p_sub_mlv];
150 - (VLCMediaListAspectNode *)nodeAtIndex:(int)index
152 VLCMediaListAspectNode * node = [[[VLCMediaListAspectNode alloc] init] autorelease];
153 [node setMedia:[self mediaAtIndex: index]];
159 libvlc_exception_t p_e;
160 libvlc_exception_init( &p_e );
161 int result = libvlc_media_list_view_count( p_mlv, &p_e );
162 quit_on_exception( &p_e );
167 - (VLCMediaList *)parentMediaList
169 return parentMediaList;
173 @implementation VLCMediaListAspect (LibVLCBridging)
174 + (id)mediaListAspectWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv
176 return [[[VLCMediaListAspect alloc] initWithLibVLCMediaListView:p_new_mlv andMediaList:nil] autorelease];
179 + (id)mediaListAspectWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv andMediaList:(VLCMediaList *)mediaList;
181 return [[[VLCMediaListAspect alloc] initWithLibVLCMediaListView:p_new_mlv andMediaList:mediaList] autorelease];
184 - (id)initWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv andMediaList:(VLCMediaList *)mediaList;
186 if( self = [super init] )
189 libvlc_media_list_view_retain(p_mlv);
191 /* parentMediaList isn't retained, because we need a mediaList to exists, and not the contrary */
192 parentMediaList = mediaList;
193 ownHisMediaList = NO;
194 if( !parentMediaList )
196 /* We have to create it then */
197 libvlc_media_list_view_retain(p_mlv);
198 libvlc_media_list_t * p_mlist = libvlc_media_list_view_parent_media_list(p_mlv, NULL);
199 parentMediaList = [[VLCMediaList mediaListWithLibVLCMediaList: p_mlist] retain];
200 libvlc_media_list_release( p_mlist );
201 /* This is an exception, and we owns it here */
202 ownHisMediaList = YES;
205 //libvlc_media_list_lock(p_mlv->p_mlist);
206 cachedNode = [[NSMutableArray alloc] initWithCapacity:libvlc_media_list_view_count(p_mlv, NULL)];
207 libvlc_media_list_t * p_mlist;
208 p_mlist = libvlc_media_list_view_parent_media_list( p_mlv, NULL );
209 libvlc_media_list_lock( p_mlist );
210 int i, count = libvlc_media_list_view_count(p_mlv, NULL);
211 for( i = 0; i < count; i++ )
213 libvlc_media_descriptor_t * p_md = libvlc_media_list_view_item_at_index(p_mlv, i, NULL);
214 libvlc_media_list_view_t * p_sub_mlv = libvlc_media_list_view_children_at_index(p_mlv, i, NULL);
215 VLCMediaListAspectNode * node = [[[VLCMediaListAspectNode alloc] init] autorelease];
216 [node setMedia:[VLCMedia mediaWithLibVLCMediaDescriptor: p_md]];
217 [node setChildren: p_sub_mlv ? [VLCMediaListAspect mediaListAspectWithLibVLCMediaListView: p_sub_mlv] : nil];
218 [cachedNode addObject:node];
219 libvlc_media_descriptor_release(p_md);
220 if( p_sub_mlv ) libvlc_media_list_view_release(p_sub_mlv);
222 [self initInternalMediaListView];
223 libvlc_media_list_unlock( p_mlist );
224 libvlc_media_list_release( p_mlist );
225 //libvlc_media_list_unlock(p_mlv->p_mlist);
230 - (libvlc_media_list_view_t *)libVLCMediaListView
232 return (libvlc_media_list_view_t *)p_mlv;
236 @implementation VLCMediaListAspect (Private)
237 - (void)initInternalMediaListView
239 libvlc_exception_t e;
240 libvlc_exception_init( &e );
242 libvlc_event_manager_t *p_em = libvlc_media_list_event_manager( p_mlv, &e );
244 /* Add internal callback */
245 libvlc_event_attach( p_em, libvlc_MediaListViewItemAdded, HandleMediaListViewItemAdded, self, &e );
246 libvlc_event_attach( p_em, libvlc_MediaListViewItemDeleted, HandleMediaListViewItemDeleted, self, &e );
247 quit_on_exception( &e );
250 - (void)mediaListViewItemAdded:(NSArray *)arrayOfArgs
252 NSAssert([NSThread isMainThread], @"We are not on main thread");
254 /* We hope to receive index in a nide range, that could change one day */
255 int start = [[[arrayOfArgs objectAtIndex: 0] objectForKey:@"index"] intValue];
256 int end = [[[arrayOfArgs objectAtIndex: [arrayOfArgs count]-1] objectForKey:@"index"] intValue];
257 NSRange range = NSMakeRange(start, end-start);
259 [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
260 [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"node"];
261 int i = [[[arrayOfArgs objectAtIndex: 0] objectForKey:@"index"] intValue];
262 for( NSDictionary * args in arrayOfArgs )
264 int index = [[args objectForKey:@"index"] intValue];
265 VLCMedia * media = [args objectForKey:@"media"];
266 VLCMediaListAspectNode * node = [[[VLCMediaListAspectNode alloc] init] autorelease];
267 [node setMedia:media];
268 [node setChildren:[self childrenAtIndex:index]];
270 NSAssert( i == index, @"Expects some troubles, inserted items are not in a range" ); i++;
271 if( index && index >= [cachedNode count] )
272 index = [cachedNode count] - 1;
273 [cachedNode insertObject:node atIndex:index];
275 [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"node"];
276 [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
279 - (void)mediaListViewItemRemoved:(NSNumber *)index
281 [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
282 [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"node"];
283 [cachedNode removeObjectAtIndex:[index intValue]];
284 [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"node"];
285 [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];