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)
54 int index = event->u.media_list_view_item_added.index;
55 [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"Media"];
57 static void HandleMediaListViewWillAddItem(const libvlc_event_t *event, void *user_data)
60 int index = event->u.media_list_view_will_add_item.index;
61 [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"Media"];
63 static void HandleMediaListViewItemDeleted( const libvlc_event_t * event, void * user_data)
66 int index = event->u.media_list_view_will_add_item.index;
67 [self didChange:NSKeyValueChangeRemoval valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"Media"];
69 static void HandleMediaListViewWillDeleteItem(const libvlc_event_t *event, void *user_data)
72 int index = event->u.media_list_view_will_add_item.index;
73 [self willChange:NSKeyValueChangeRemoval valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"Media"];
76 @implementation VLCMediaListAspect
79 // Release allocated memory
80 libvlc_media_list_release(p_mlv);
84 - (VLCMedia *)mediaAtIndex:(int)index
86 libvlc_exception_t p_e;
87 libvlc_exception_init( &p_e );
88 libvlc_media_descriptor_t *p_md = libvlc_media_list_view_item_at_index( p_mlv, index, &p_e );
89 quit_on_exception( &p_e );
91 // Returns local object for media descriptor, searchs for user data first. If not found it creates a
92 // new cocoa object representation of the media descriptor.
93 return [VLCMedia mediaWithLibVLCMediaDescriptor:p_md];
98 libvlc_exception_t p_e;
99 libvlc_exception_init( &p_e );
100 int result = libvlc_media_list_view_count( p_mlv, &p_e );
101 quit_on_exception( &p_e );
107 @implementation VLCMediaListAspect (LibVLCBridging)
108 + (id)mediaListAspectWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv;
110 return [[[VLCMediaList alloc] initWithLibVLCMediaList:p_new_mlv] autorelease];
113 - (id)initWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv;
115 if( self = [super init] )
118 libvlc_media_list_view_retain(p_mlv);
119 [self initInternalMediaListView];
124 - (libvlc_media_list_view_t *)libVLCMediaListView
126 return (libvlc_media_list_view_t *)p_mlv;
130 @implementation VLCMediaListAspect (Private)
131 - (void)initInternalMediaListView
133 libvlc_exception_t e;
134 libvlc_exception_init( &e );
136 libvlc_event_manager_t *p_em = libvlc_media_list_event_manager( p_mlv, &e );
138 /* Add internal callback */
139 libvlc_event_attach( p_em, libvlc_MediaListViewItemAdded, HandleMediaListViewItemAdded, self, &e );
140 libvlc_event_attach( p_em, libvlc_MediaListViewWillAddItem, HandleMediaListViewWillAddItem, self, &e );
141 libvlc_event_attach( p_em, libvlc_MediaListViewItemDeleted, HandleMediaListViewItemDeleted, self, &e );
142 libvlc_event_attach( p_em, libvlc_MediaListViewWillDeleteItem, HandleMediaListViewWillDeleteItem, self, &e );
144 quit_on_exception( &e );