]> git.sesse.net Git - vlc/blobdiff - extras/MacOSX/Framework/Sources/VLCMediaListAspect.m
Don't include config.h from the headers - refs #297.
[vlc] / extras / MacOSX / Framework / Sources / VLCMediaListAspect.m
index d4add8c5a6876b69c67f1e7c24caabb363471450..aa7411b98ca13854a892be9405cfe515211e08cc 100644 (file)
 #import "VLCLibrary.h"
 #import "VLCEventManager.h"
 #import "VLCLibVLCBridging.h"
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include <vlc/vlc.h>
 #include <vlc/libvlc.h>
 
@@ -34,7 +38,7 @@
 /* Initializers */
 - (void)initInternalMediaListView;
 
-- (void)mediaListViewItemAdded:(NSDictionary *)args;
+- (void)mediaListViewItemAdded:(NSArray *)args;
 - (void)mediaListViewItemRemoved:(NSNumber *)index;
 @end
 
 @end
 
 /* libvlc event callback */
-static void HandleMediaListViewItemAdded(const libvlc_event_t *event, void *user_data)
+static void HandleMediaListViewItemAdded(const libvlc_event_t * event, void * user_data)
 {
     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
     id self = user_data;
     [[VLCEventManager sharedManager] callOnMainThreadObject:self 
                                                  withMethod:@selector(mediaListViewItemAdded:) 
-                                       withArgumentAsObject:[NSDictionary dictionaryWithObjectsAndKeys:
+                                       withArgumentAsObject:[NSArray arrayWithObject:[NSDictionary dictionaryWithObjectsAndKeys:
                                                           [VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_list_item_added.item], @"media",
                                                           [NSNumber numberWithInt:event->u.media_list_item_added.index], @"index",
-                                                          nil]];
+                                                          nil]]];
     [pool release];
 }
 
@@ -105,14 +109,27 @@ static void HandleMediaListViewItemDeleted( const libvlc_event_t * event, void *
     // Release allocated memory
     libvlc_media_list_view_release(p_mlv);
     [cachedNode release];
+    if( ownHisMediaList )
+        [parentMediaList release];
     [super dealloc];
 }
+- (NSString *)description
+{
+    NSMutableString * content = [NSMutableString string];
+    int i;
+    for( i = 0; i < [self count]; i++)
+    {
+        [content appendFormat:@"%@\n", [self mediaAtIndex: i]];
+    }
+    return [NSString stringWithFormat:@"<%@ %p> {\n%@}", [self className], self, content];
+}
+
 - (VLCMedia *)mediaAtIndex:(int)index
 {
     libvlc_exception_t p_e;
     libvlc_exception_init( &p_e );
-    libvlc_media_descriptor_t *p_md = libvlc_media_list_view_item_at_index( p_mlv, index, &p_e );
-    quit_on_exception( &p_e );
+    libvlc_media_descriptor_t * p_md = libvlc_media_list_view_item_at_index( p_mlv, index, &p_e );
+    catch_exception( &p_e );
     
     // Returns local object for media descriptor, searchs for user data first.  If not found it creates a 
     // new cocoa object representation of the media descriptor.
@@ -123,8 +140,8 @@ static void HandleMediaListViewItemDeleted( const libvlc_event_t * event, void *
 {
     libvlc_exception_t p_e;
     libvlc_exception_init( &p_e );
-    libvlc_media_list_view_t *p_sub_mlv = libvlc_media_list_view_children_at_index( p_mlv, index, &p_e );
-    quit_on_exception( &p_e );
+    libvlc_media_list_view_t * p_sub_mlv = libvlc_media_list_view_children_at_index( p_mlv, index, &p_e );
+    catch_exception( &p_e );
 
     if( !p_sub_mlv )
         return nil;
@@ -146,26 +163,53 @@ static void HandleMediaListViewItemDeleted( const libvlc_event_t * event, void *
     libvlc_exception_t p_e;
     libvlc_exception_init( &p_e );
     int result = libvlc_media_list_view_count( p_mlv, &p_e );
-    quit_on_exception( &p_e );
+    catch_exception( &p_e );
 
     return result;
 }
+
+- (VLCMediaList *)parentMediaList
+{
+    return parentMediaList;
+}
 @end
 
 @implementation VLCMediaListAspect (LibVLCBridging)
-+ (id)mediaListAspectWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv;
++ (id)mediaListAspectWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv
+{
+    return [[[VLCMediaListAspect alloc] initWithLibVLCMediaListView:p_new_mlv andMediaList:nil] autorelease];
+}
+
++ (id)mediaListAspectWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv andMediaList:(VLCMediaList *)mediaList;
 {
-    return [[[VLCMediaListAspect alloc] initWithLibVLCMediaListView:p_new_mlv] autorelease];
+    return [[[VLCMediaListAspect alloc] initWithLibVLCMediaListView:p_new_mlv andMediaList:mediaList] autorelease];
 }
 
-- (id)initWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv;
+- (id)initWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv andMediaList:(VLCMediaList *)mediaList;
 {
     if( self = [super init] )
     {
         p_mlv = p_new_mlv;
         libvlc_media_list_view_retain(p_mlv);
-        //libvlc_media_list_lock(p_mlv->p_mlist);
+
+        /* parentMediaList isn't retained, because we need a mediaList to exists, and not the contrary */
+        parentMediaList = mediaList;
+        ownHisMediaList = NO;
+        if( !parentMediaList )
+        {
+            /* We have to create it then */
+            libvlc_media_list_view_retain(p_mlv);
+            libvlc_media_list_t * p_mlist = libvlc_media_list_view_parent_media_list(p_mlv, NULL);
+            parentMediaList = [[VLCMediaList mediaListWithLibVLCMediaList: p_mlist] retain];
+            libvlc_media_list_release( p_mlist );
+            /* This is an exception, and we owns it here */
+            ownHisMediaList = YES;
+        }
+
         cachedNode = [[NSMutableArray alloc] initWithCapacity:libvlc_media_list_view_count(p_mlv, NULL)];
+        libvlc_media_list_t * p_mlist;
+        p_mlist = libvlc_media_list_view_parent_media_list( p_mlv, NULL );
+        libvlc_media_list_lock( p_mlist );
         int i, count = libvlc_media_list_view_count(p_mlv, NULL);
         for( i = 0; i < count; i++ )
         {
@@ -179,7 +223,8 @@ static void HandleMediaListViewItemDeleted( const libvlc_event_t * event, void *
             if( p_sub_mlv ) libvlc_media_list_view_release(p_sub_mlv);
         }
         [self initInternalMediaListView];
-        //libvlc_media_list_unlock(p_mlv->p_mlist);
+        libvlc_media_list_unlock( p_mlist );
+        libvlc_media_list_release( p_mlist );
     }
     return self;
 }
@@ -196,28 +241,39 @@ static void HandleMediaListViewItemDeleted( const libvlc_event_t * event, void *
     libvlc_exception_t e;
     libvlc_exception_init( &e );
 
-    libvlc_event_manager_t *p_em = libvlc_media_list_event_manager( p_mlv, &e );
+    libvlc_event_manager_t * p_em = libvlc_media_list_event_manager( p_mlv, &e );
 
     /* Add internal callback */
     libvlc_event_attach( p_em, libvlc_MediaListViewItemAdded,   HandleMediaListViewItemAdded,   self, &e );
     libvlc_event_attach( p_em, libvlc_MediaListViewItemDeleted, HandleMediaListViewItemDeleted, self, &e );
-    quit_on_exception( &e );
+    catch_exception( &e );
 }
 
-- (void)mediaListViewItemAdded:(NSDictionary *)args
+- (void)mediaListViewItemAdded:(NSArray *)arrayOfArgs
 {
-    int index = [[args objectForKey:@"index"] intValue];
-    VLCMedia * media = [args objectForKey:@"media"];
+    NSAssert([NSThread isMainThread], @"We are not on main thread");
 
-    VLCMediaListAspectNode * node = [[[VLCMediaListAspectNode alloc] init] autorelease];
-    [node setMedia:media];
-    [node setChildren:[self childrenAtIndex:index]];
+    /* We hope to receive index in a nide range, that could change one day */
+    int start = [[[arrayOfArgs objectAtIndex: 0] objectForKey:@"index"] intValue];
+    int end = [[[arrayOfArgs objectAtIndex: [arrayOfArgs count]-1] objectForKey:@"index"] intValue];
+    NSRange range = NSMakeRange(start, end-start);
 
-    [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"media"];
-    [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"node"];
-    [cachedNode insertObject:node atIndex:index];
-    [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"node"];
-    [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"media"];
+    [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
+    [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"node"];
+    for( NSDictionary * args in arrayOfArgs )
+    {
+        int index = [[args objectForKey:@"index"] intValue];
+        VLCMedia * media = [args objectForKey:@"media"];
+        VLCMediaListAspectNode * node = [[[VLCMediaListAspectNode alloc] init] autorelease];
+        [node setMedia:media];
+        [node setChildren:[self childrenAtIndex:index]];
+        /* Sanity check */
+        if( index && index > [cachedNode count] )
+            index = [cachedNode count];
+        [cachedNode insertObject:node atIndex:index];
+    }
+    [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"node"];
+    [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
 }
 
 - (void)mediaListViewItemRemoved:(NSNumber *)index
@@ -229,4 +285,3 @@ static void HandleMediaListViewItemDeleted( const libvlc_event_t * event, void *
     [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
 }
 @end
-