]> git.sesse.net Git - vlc/blob - extras/MacOSX/Framework/Sources/VLCMediaListAspect.m
dc7d2980cdde266d23e2b3830fa4c43b5bb6bce1
[vlc] / extras / MacOSX / Framework / Sources / VLCMediaListAspect.m
1 /*****************************************************************************
2  * VLCMediaList.m: VLC.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 "VLCMediaListAspect.h"
26 #import "VLCLibrary.h"
27 #import "VLCEventManager.h"
28 #import "VLCLibVLCBridging.h"
29 #include <vlc/vlc.h>
30 #include <vlc/libvlc.h>
31
32 // TODO: Documentation
33 @interface VLCMediaListAspect (Private)
34 /* Initializers */
35 - (void)initInternalMediaListView;
36 @end
37
38 @implementation VLCMediaListAspect (KeyValueCodingCompliance)
39 /* For the @"Media" key */
40 - (int) countOfMedia
41 {
42     return [self count];
43 }
44 - (id) objectInMediaAtIndex:(int)i
45 {
46     return [self mediaAtIndex:i];
47 }
48 @end
49
50 /* libvlc event callback */
51 static void HandleMediaListViewItemAdded(const libvlc_event_t *event, void *user_data)
52 {
53     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
54     id self = user_data;
55     int index = event->u.media_list_view_item_added.index;
56     [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"Media"];
57     [pool release];
58 }
59 static void HandleMediaListViewItemDeleted( const libvlc_event_t * event, void * user_data)
60 {
61     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
62     id self = user_data;
63     int index = event->u.media_list_view_item_deleted.index;
64     [self didChange:NSKeyValueChangeRemoval valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"Media"];
65     [pool release];
66 }
67
68 @implementation VLCMediaListAspect
69 - (void)dealloc
70 {
71     // Release allocated memory
72     libvlc_media_list_view_release(p_mlv);
73     [cachedMedia release];
74     [super dealloc];
75 }
76 - (VLCMedia *)mediaAtIndex:(int)index
77 {
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 );
82     
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];
86 }
87
88 - (int)count
89 {
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 );
94
95     return result;
96 }
97 @end
98
99 @implementation VLCMediaListAspect (LibVLCBridging)
100 + (id)mediaListAspectWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv;
101 {
102     return [[[VLCMediaListAspect alloc] initWithLibVLCMediaListView:p_new_mlv] autorelease];
103 }
104
105 - (id)initWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv;
106 {
107     if( self = [super init] )
108     {
109         p_mlv = p_new_mlv;
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++ )
115         {
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);
119         }
120         [self initInternalMediaListView];
121         //libvlc_media_list_unlock(p_mlist);
122     }
123     return self;
124 }
125
126 - (libvlc_media_list_view_t *)libVLCMediaListView
127 {
128     return (libvlc_media_list_view_t *)p_mlv;
129 }
130 @end
131
132 @implementation VLCMediaListAspect (Private)
133 - (void)initInternalMediaListView
134 {
135     libvlc_exception_t e;
136     libvlc_exception_init( &e );
137
138     libvlc_event_manager_t *p_em = libvlc_media_list_event_manager( p_mlv, &e );
139
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 );
144 }
145 @end
146