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:(NSDictionary *)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:[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 [parentMediaList release];
111 - (VLCMedia *)mediaAtIndex:(int)index
113 libvlc_exception_t p_e;
114 libvlc_exception_init( &p_e );
115 libvlc_media_descriptor_t *p_md = libvlc_media_list_view_item_at_index( p_mlv, index, &p_e );
116 quit_on_exception( &p_e );
118 // Returns local object for media descriptor, searchs for user data first. If not found it creates a
119 // new cocoa object representation of the media descriptor.
120 return [VLCMedia mediaWithLibVLCMediaDescriptor:p_md];
123 - (VLCMediaListAspect *)childrenAtIndex:(int)index
125 libvlc_exception_t p_e;
126 libvlc_exception_init( &p_e );
127 libvlc_media_list_view_t *p_sub_mlv = libvlc_media_list_view_children_at_index( p_mlv, index, &p_e );
128 quit_on_exception( &p_e );
133 // Returns local object for media descriptor, searchs for user data first. If not found it creates a
134 // new cocoa object representation of the media descriptor.
135 return [VLCMediaListAspect mediaListAspectWithLibVLCMediaListView:p_sub_mlv];
138 - (VLCMediaListAspectNode *)nodeAtIndex:(int)index
140 VLCMediaListAspectNode * node = [[[VLCMediaListAspectNode alloc] init] autorelease];
141 [node setMedia:[self mediaAtIndex: index]];
147 libvlc_exception_t p_e;
148 libvlc_exception_init( &p_e );
149 int result = libvlc_media_list_view_count( p_mlv, &p_e );
150 quit_on_exception( &p_e );
155 - (VLCMediaList *)parentMediaList
157 return [[parentMediaList retain] autorelease];
161 @implementation VLCMediaListAspect (LibVLCBridging)
162 + (id)mediaListAspectWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv;
164 return [[[VLCMediaListAspect alloc] initWithLibVLCMediaListView:p_new_mlv] autorelease];
167 - (id)initWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv;
169 if( self = [super init] )
172 libvlc_media_list_view_retain(p_mlv);
173 libvlc_media_list_t * p_mlist = libvlc_media_list_view_parent_media_list(p_mlv, NULL);
174 parentMediaList = [[VLCMediaList mediaListWithLibVLCMediaList: p_mlist] retain];
175 libvlc_media_list_release( p_mlist );
177 //libvlc_media_list_lock(p_mlv->p_mlist);
178 cachedNode = [[NSMutableArray alloc] initWithCapacity:libvlc_media_list_view_count(p_mlv, NULL)];
179 int i, count = libvlc_media_list_view_count(p_mlv, NULL);
180 for( i = 0; i < count; i++ )
182 libvlc_media_descriptor_t * p_md = libvlc_media_list_view_item_at_index(p_mlv, i, NULL);
183 libvlc_media_list_view_t * p_sub_mlv = libvlc_media_list_view_children_at_index(p_mlv, i, NULL);
184 VLCMediaListAspectNode * node = [[[VLCMediaListAspectNode alloc] init] autorelease];
185 [node setMedia:[VLCMedia mediaWithLibVLCMediaDescriptor: p_md]];
186 [node setChildren: p_sub_mlv ? [VLCMediaListAspect mediaListAspectWithLibVLCMediaListView: p_sub_mlv] : nil];
187 [cachedNode addObject:node];
188 libvlc_media_descriptor_release(p_md);
189 if( p_sub_mlv ) libvlc_media_list_view_release(p_sub_mlv);
191 [self initInternalMediaListView];
192 //libvlc_media_list_unlock(p_mlv->p_mlist);
197 - (libvlc_media_list_view_t *)libVLCMediaListView
199 return (libvlc_media_list_view_t *)p_mlv;
203 @implementation VLCMediaListAspect (Private)
204 - (void)initInternalMediaListView
206 libvlc_exception_t e;
207 libvlc_exception_init( &e );
209 libvlc_event_manager_t *p_em = libvlc_media_list_event_manager( p_mlv, &e );
211 /* Add internal callback */
212 libvlc_event_attach( p_em, libvlc_MediaListViewItemAdded, HandleMediaListViewItemAdded, self, &e );
213 libvlc_event_attach( p_em, libvlc_MediaListViewItemDeleted, HandleMediaListViewItemDeleted, self, &e );
214 quit_on_exception( &e );
217 - (void)mediaListViewItemAdded:(NSDictionary *)args
219 int index = [[args objectForKey:@"index"] intValue];
220 VLCMedia * media = [args objectForKey:@"media"];
222 VLCMediaListAspectNode * node = [[[VLCMediaListAspectNode alloc] init] autorelease];
223 [node setMedia:media];
224 [node setChildren:[self childrenAtIndex:index]];
226 [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"media"];
227 [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"node"];
228 [cachedNode insertObject:node atIndex:index];
229 [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"node"];
230 [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"media"];
233 - (void)mediaListViewItemRemoved:(NSNumber *)index
235 [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
236 [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"node"];
237 [cachedNode removeObjectAtIndex:[index intValue]];
238 [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"node"];
239 [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];