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;
38 @implementation VLCMediaListAspect (KeyValueCodingCompliance)
39 /* For the @"Media" key */
44 - (id) objectInMediaAtIndex:(int)i
46 return [self mediaAtIndex:i];
50 /* libvlc event callback */
51 static void HandleMediaListViewItemAdded(const libvlc_event_t *event, void *user_data)
53 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
55 int index = event->u.media_list_view_item_added.index;
56 [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"Media"];
59 static void HandleMediaListViewItemDeleted( const libvlc_event_t * event, void * user_data)
61 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
63 int index = event->u.media_list_view_item_deleted.index;
64 [self didChange:NSKeyValueChangeRemoval valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"Media"];
68 @implementation VLCMediaListAspect
71 // Release allocated memory
72 libvlc_media_list_view_release(p_mlv);
73 [cachedMedia release];
76 - (VLCMedia *)mediaAtIndex:(int)index
78 libvlc_exception_t p_e;
79 libvlc_exception_init( &p_e );
80 libvlc_media_descriptor_t *p_md = libvlc_media_list_view_item_at_index( p_mlv, index, &p_e );
81 quit_on_exception( &p_e );
83 // Returns local object for media descriptor, searchs for user data first. If not found it creates a
84 // new cocoa object representation of the media descriptor.
85 return [VLCMedia mediaWithLibVLCMediaDescriptor:p_md];
90 libvlc_exception_t p_e;
91 libvlc_exception_init( &p_e );
92 int result = libvlc_media_list_view_count( p_mlv, &p_e );
93 quit_on_exception( &p_e );
99 @implementation VLCMediaListAspect (LibVLCBridging)
100 + (id)mediaListAspectWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv;
102 return [[[VLCMediaListAspect alloc] initWithLibVLCMediaListView:p_new_mlv] autorelease];
105 - (id)initWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv;
107 if( self = [super init] )
110 libvlc_media_list_view_retain(p_mlv);
111 //libvlc_media_list_lock(p_mlist);
112 cachedMedia = [[NSMutableArray alloc] initWithCapacity:libvlc_media_list_view_count(p_mlv, NULL)];
113 int i, count = libvlc_media_list_view_count(p_mlv, NULL);
114 for( i = 0; i < count; i++ )
116 libvlc_media_descriptor_t * p_md = libvlc_media_list_view_item_at_index(p_mlv, i, NULL);
117 [cachedMedia addObject:[VLCMedia mediaWithLibVLCMediaDescriptor: p_md]];
118 libvlc_media_descriptor_release(p_md);
120 [self initInternalMediaListView];
121 //libvlc_media_list_unlock(p_mlist);
126 - (libvlc_media_list_view_t *)libVLCMediaListView
128 return (libvlc_media_list_view_t *)p_mlv;
132 @implementation VLCMediaListAspect (Private)
133 - (void)initInternalMediaListView
135 libvlc_exception_t e;
136 libvlc_exception_init( &e );
138 libvlc_event_manager_t *p_em = libvlc_media_list_event_manager( p_mlv, &e );
140 /* Add internal callback */
141 libvlc_event_attach( p_em, libvlc_MediaListViewItemAdded, HandleMediaListViewItemAdded, self, &e );
142 libvlc_event_attach( p_em, libvlc_MediaListViewItemDeleted, HandleMediaListViewItemDeleted, self, &e );
143 quit_on_exception( &e );