]> git.sesse.net Git - vlc/blobdiff - modules/gui/macosx/playlist.m
macosx: dynamically create context and main sub menu for playlist table header select...
[vlc] / modules / gui / macosx / playlist.m
index 6c0fab8e1f9158308d372d5c870cc5161fe48910..2ec7746ae37bbb2c5ba2535aa5a8c27cd84435e9 100644 (file)
  *****************************************************************************/
 @implementation VLCPlaylistCommon
 
-+ (void)initialize{
-    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
-    NSMutableArray * o_columnArray = [[NSMutableArray alloc] init];
-    [o_columnArray addObject: [NSArray arrayWithObjects: TITLE_COLUMN, [NSNumber numberWithFloat: 190.], nil]];
-    [o_columnArray addObject: [NSArray arrayWithObjects: ARTIST_COLUMN, [NSNumber numberWithFloat: 95.], nil]];
-    [o_columnArray addObject: [NSArray arrayWithObjects: DURATION_COLUMN, [NSNumber numberWithFloat: 95.], nil]];
-    NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:[NSArray arrayWithArray:o_columnArray] forKey:@"PlaylistColumnSelection"];
-
-    [defaults registerDefaults:appDefaults];
-    [o_columnArray release];
-}
-
 - (id)init
 {
     playlist_t * p_playlist = pl_Get( VLCIntf );
     [[o_tc_name_other headerCell] setStringValue:_NS("Name")];
     [[o_tc_author_other headerCell] setStringValue:_NS("Author")];
     [[o_tc_duration_other headerCell] setStringValue:_NS("Duration")];
-
-    NSArray * o_columnArray = [[NSUserDefaults standardUserDefaults] arrayForKey:@"PlaylistColumnSelection"];
-    NSUInteger count = [o_columnArray count];
-
-    id o_menu = [[VLCMain sharedInstance] mainMenu];
-    NSString * o_column;
-    for (NSUInteger i = 0; i < count; i++)
-    {
-        o_column = [[o_columnArray objectAtIndex:i] objectAtIndex:0];
-        if ([o_column isEqualToString:@"status"])
-            continue;
-
-        [self setColumn: o_column state: NSOnState];
-        [o_menu setPlaylistColumnTableState: NSOnState forColumn: o_column];
-        [[o_outline_view tableColumnWithIdentifier: o_column] setWidth: [[[o_columnArray objectAtIndex:i] objectAtIndex:1] floatValue]];
-    }
-
-    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(applicationWillTerminate:) name: NSApplicationWillTerminateNotification object: nil];
-}
-
-- (void)applicationWillTerminate:(NSNotification *)notification
-{
-    /* let's make sure we save the correct widths and positions, since this likely changed since the last time the user played with the column selection */
-    [self saveTableColumns];
 }
 
 - (void)setPlaylistRoot: (playlist_item_t *)root_item
                                                                 pointerValue];
 }
 
-- (void)setColumn: (NSString *)o_column state: (NSInteger)i_state
-{
-    NSTableColumn * o_work_tc;
-
-    if (i_state == NSOnState)
-    {
-        o_work_tc = [[NSTableColumn alloc] initWithIdentifier: o_column];
-        [o_work_tc setEditable: NO];
-        [[o_work_tc dataCell] setFont: [NSFont controlContentFontOfSize:11.]];
-
-        /* we cannot use a makro here, because gettext isn't clever enough for that */
-        if ([o_column isEqualToString: TRACKNUM_COLUMN])
-        {
-            [o_work_tc setMaxWidth: 20.];
-            [[o_work_tc headerCell] setStringValue: @"#"];
-        }
-        else if ([o_column isEqualToString: TITLE_COLUMN])
-        {
-            [[o_work_tc headerCell] setStringValue: _NS("Name")];
-            [o_work_tc setResizingMask: NSTableColumnAutoresizingMask | NSTableColumnUserResizingMask];
-        }
-        else if ([o_column isEqualToString: ARTIST_COLUMN])
-            [[o_work_tc headerCell] setStringValue: _NS("Author")];
-        else if ([o_column isEqualToString: DURATION_COLUMN])
-            [[o_work_tc headerCell] setStringValue: _NS("Duration")];
-        else if ([o_column isEqualToString: GENRE_COLUMN])
-            [[o_work_tc headerCell] setStringValue: _NS("Genre")];
-        else if ([o_column isEqualToString: ALBUM_COLUMN])
-            [[o_work_tc headerCell] setStringValue: _NS("Album")];
-        else if ([o_column isEqualToString: DESCRIPTION_COLUMN])
-            [[o_work_tc headerCell] setStringValue: _NS("Description")];
-        else if ([o_column isEqualToString: DATE_COLUMN])
-            [[o_work_tc headerCell] setStringValue: _NS("Date")];
-        else if ([o_column isEqualToString: LANGUAGE_COLUMN])
-            [[o_work_tc headerCell] setStringValue: _NS("Language")];
-
-        [o_outline_view addTableColumn: o_work_tc];
-        [o_work_tc release];
-        [o_outline_view reloadData];
-        [o_outline_view setNeedsDisplay: YES];
-    }
-    else
-        [o_outline_view removeTableColumn: [o_outline_view tableColumnWithIdentifier: o_column]];
-
-    [self saveTableColumns];
-}
-
-- (void)saveTableColumns
-{
-    NSMutableArray * o_arrayToSave = [[NSMutableArray alloc] init];
-    NSArray * o_columns = [o_outline_view tableColumns];
-    NSUInteger count = [o_columns count];
-    NSTableColumn * o_currentColumn;
-    for (NSUInteger i = 0; i < count; i++)
-    {
-        o_currentColumn = [o_columns objectAtIndex: i];
-        [o_arrayToSave addObject: [NSArray arrayWithObjects: [o_currentColumn identifier], [NSNumber numberWithFloat: [o_currentColumn width]], nil]];
-    }
-    [[NSUserDefaults standardUserDefaults] setObject: o_arrayToSave forKey:@"PlaylistColumnSelection"];
-    [o_arrayToSave release];
-}
-
 @end
 
 @implementation VLCPlaylistCommon (NSOutlineViewDataSource)
             free( psz_value ); \
         }
     }
+    else if( [o_identifier isEqualToString:URI_COLUMN] )
+    {
+        psz_value = decode_URI(input_item_GetURI( p_item->p_input )); \
+        if (psz_value) { \
+            o_value = [NSString stringWithUTF8String: psz_value]; \
+            free( psz_value ); \
+        }
+    }
     else if( [o_identifier isEqualToString:@"status"] )
     {
         if( input_item_HasErrorWhenReading( p_item->p_input ) )
 @end
 
 /*****************************************************************************
- * extension to NSOutlineView's interface to fix compilation warnings
- * and let us access these 2 functions properly
- * this uses a private Apple-API, but works fine on all current OSX releases
- * keep checking for compatiblity with future releases though
+ * An extension to NSOutlineView's interface to fix compilation warnings
+ * and let us access these 2 functions properly.
+ * This uses a private API, but works fine on all current OSX releases.
+ * Radar ID 11739459 request a public API for this. However, it is probably
+ * easier and faster to recreate similar looking bitmaps ourselves.
  *****************************************************************************/
 
 @interface NSOutlineView (UndocumentedSortImages)
  *****************************************************************************/
 @implementation VLCPlaylist
 
++ (void)initialize{
+    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
+    NSMutableArray * o_columnArray = [[NSMutableArray alloc] init];
+    [o_columnArray addObject: [NSArray arrayWithObjects: TITLE_COLUMN, [NSNumber numberWithFloat: 190.], nil]];
+    [o_columnArray addObject: [NSArray arrayWithObjects: ARTIST_COLUMN, [NSNumber numberWithFloat: 95.], nil]];
+    [o_columnArray addObject: [NSArray arrayWithObjects: DURATION_COLUMN, [NSNumber numberWithFloat: 95.], nil]];
+    NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:[NSArray arrayWithArray:o_columnArray] forKey:@"PlaylistColumnSelection"];
+
+    [defaults registerDefaults:appDefaults];
+    [o_columnArray release];
+}
+
 - (id)init
 {
     self = [super init];
       @"VLCPlaylistItemPboardType", nil]];
     [o_outline_view_other setIntercellSpacing: NSMakeSize (0.0, 1.0)];
 
-    /* This uses private Apple API which works fine until 10.5.
-     * We need to keep checking in the future!
-     * These methods are being added artificially to NSOutlineView's interface above */
+    /* This uses a private API, but works fine on all current OSX releases.
+     * Radar ID 11739459 request a public API for this. However, it is probably
+     * easier and faster to recreate similar looking bitmaps ourselves. */
     o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
     o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
 
     o_tc_sortColumn = nil;
+
+    NSArray * o_columnArray = [[NSUserDefaults standardUserDefaults] arrayForKey:@"PlaylistColumnSelection"];
+    NSUInteger count = [o_columnArray count];
+
+    id o_menu = [[VLCMain sharedInstance] mainMenu];
+    NSString * o_column;
+
+    NSMenu *o_context_menu = [o_menu setupPlaylistTableColumnsMenu];
+    [o_playlist_header setMenu: o_context_menu];
+
+    for (NSUInteger i = 0; i < count; i++)
+    {
+        o_column = [[o_columnArray objectAtIndex:i] objectAtIndex:0];
+        if ([o_column isEqualToString:@"status"])
+            continue;
+
+        [self setColumn: o_column state: NSOnState];
+        [o_menu setPlaylistColumnTableState: NSOnState forColumn: o_column];
+        [[o_outline_view tableColumnWithIdentifier: o_column] setWidth: [[[o_columnArray objectAtIndex:i] objectAtIndex:1] floatValue]];
+    }
+
+    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(applicationWillTerminate:) name: NSApplicationWillTerminateNotification object: nil];
+}
+
+- (void)applicationWillTerminate:(NSNotification *)notification
+{
+    /* let's make sure we save the correct widths and positions, since this likely changed since the last time the user played with the column selection */
+    [self saveTableColumns];
 }
 
 - (void)searchfieldChanged:(NSNotification *)o_notification
     NSUInteger count = [[o_outline_view tableColumns] count];
     for( NSUInteger i = 0 ; i < count ; i++ )
     {
-        [o_outline_view setIndicatorImage:nil inTableColumn:
-                            [[o_outline_view tableColumns] objectAtIndex:i]];
+        [o_outline_view setIndicatorImage:nil inTableColumn: [[o_outline_view tableColumns] objectAtIndex:i]];
     }
 
     [o_outline_view setHighlightedTableColumn:nil];
     playlist_item_t *p_node = NULL;
 
     // ignore clicks on column header when handling double action
-    if( sender != nil && [o_outline_view clickedRow] == -1 )
+    if( sender != nil && [o_outline_view clickedRow] == -1 && sender != o_mi_play)
         return;
 
     p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
 
 - (IBAction)revealItemInFinder:(id)sender
 {
-    playlist_item_t * p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
-    NSMutableString * o_mrl = nil;
+    NSIndexSet * selectedRows = [o_outline_view selectedRowIndexes];
+    NSUInteger count = [selectedRows count];
+    NSUInteger indexes[count];
+    [selectedRows getIndexes:indexes maxCount:count inIndexRange:nil];
 
-    if(! p_item || !p_item->p_input )
-        return;
+    NSMutableString * o_mrl;
+    playlist_item_t *p_item;
+    for (NSUInteger i = 0; i < count; i++) {
+        p_item = [[o_outline_view itemAtRow:indexes[i]] pointerValue];
 
-    char *psz_uri = decode_URI( input_item_GetURI( p_item->p_input ) );
-    if( psz_uri )
-        o_mrl = [NSMutableString stringWithUTF8String: psz_uri];
+        if(! p_item || !p_item->p_input )
+            continue;
 
-    /* perform some checks whether it is a file and if it is local at all... */
-    NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
-    if( prefix_range.location != NSNotFound )
-        [o_mrl deleteCharactersInRange: prefix_range];
+        o_mrl = [[NSMutableString alloc] initWithFormat: @"%s", decode_URI( input_item_GetURI( p_item->p_input ))];
 
-    if( [o_mrl characterAtIndex:0] == '/' )
-        [[NSWorkspace sharedWorkspace] selectFile: o_mrl inFileViewerRootedAtPath: o_mrl];
+        /* perform some checks whether it is a file and if it is local at all... */
+        if ([o_mrl length] > 0)
+        {
+            NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
+            if( prefix_range.location != NSNotFound )
+                [o_mrl deleteCharactersInRange: prefix_range];
+
+            if( [o_mrl characterAtIndex:0] == '/' )
+                [[NSWorkspace sharedWorkspace] selectFile: o_mrl inFileViewerRootedAtPath: o_mrl];
+        }
+
+        [o_mrl release];
+    }
 }
 
 /* When called retrieves the selected outlineview row and plays that node or item */
     [o_selected_indexes getIndexes:indexes maxCount:i_count inIndexRange:nil];
     for (int i = 0; i < i_count; i++)
     {
-        p_item = [[o_outline_view itemAtRow: indexes[i]] pointerValue];   
-        [o_outline_view deselectRow: indexes[i]];
+        p_item = [[o_outline_view itemAtRow: indexes[i]] pointerValue];
 
         if( p_item && p_item->i_children == -1 )
             playlist_AskForArtEnqueue( p_playlist, p_item->p_input );
 
 - (IBAction)recursiveExpandNode:(id)sender
 {
-    id o_item = [o_outline_view itemAtRow: [o_outline_view selectedRow]];
-    playlist_item_t *p_item = (playlist_item_t *)[o_item pointerValue];
+    NSIndexSet * selectedRows = [o_outline_view selectedRowIndexes];
+    NSUInteger count = [selectedRows count];
+    NSUInteger indexes[count];
+    [selectedRows getIndexes:indexes maxCount:count inIndexRange:nil];
 
-    if( ![[o_outline_view dataSource] outlineView: o_outline_view
-                                                    isItemExpandable: o_item] )
-    {
-        o_item = [o_outline_dict objectForKey: [NSString stringWithFormat: @"%p", p_item->p_parent]];
-    }
+    id o_item;
+    playlist_item_t *p_item;
+    for (NSUInteger i = 0; i < count; i++) {
+        o_item = [o_outline_view itemAtRow: indexes[i]];
+        p_item = (playlist_item_t *)[o_item pointerValue];
 
-    /* We need to collapse the node first, since OSX refuses to recursively
-       expand an already expanded node, even if children nodes are collapsed. */
-    [o_outline_view collapseItem: o_item collapseChildren: YES];
-    [o_outline_view expandItem: o_item expandChildren: YES];
+        if( ![[o_outline_view dataSource] outlineView: o_outline_view isItemExpandable: o_item] )
+            o_item = [o_outline_dict objectForKey: [NSString stringWithFormat: @"%p", p_item->p_parent]];
+
+        /* We need to collapse the node first, since OSX refuses to recursively
+         expand an already expanded node, even if children nodes are collapsed. */
+        [o_outline_view collapseItem: o_item collapseChildren: YES];
+        [o_outline_view expandItem: o_item expandChildren: YES];
+
+        selectedRows = [o_outline_view selectedRowIndexes];
+        [selectedRows getIndexes:indexes maxCount:count inIndexRange:nil];
+    }
 }
 
 - (NSMenu *)menuForEvent:(NSEvent *)o_event
     bool b_rows;
     bool b_item_sel;
 
-    pt = [o_outline_view convertPoint: [o_event locationInWindow]
-                                                 fromView: nil];
+    pt = [o_outline_view convertPoint: [o_event locationInWindow] fromView: nil];
     int row = [o_outline_view rowAtPoint:pt];
-    if( row != -1 )
+    if( row != -1 && ![[o_outline_view selectedRowIndexes] containsIndex: row] )
         [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
 
     b_item_sel = ( row != -1 && [o_outline_view selectedRow] != -1 );
     return( o_ctx_menu );
 }
 
-- (void)outlineView: (NSOutlineView *)o_tv
-                  didClickTableColumn:(NSTableColumn *)o_tc
+- (void)outlineView: (NSOutlineView *)o_tv didClickTableColumn:(NSTableColumn *)o_tc
 {
     int i_mode, i_type = 0;
     intf_thread_t *p_intf = VLCIntf;
 
     playlist_t *p_playlist = pl_Get( p_intf );
 
-    /* Check whether the selected table column header corresponds to a
-       sortable table column*/
-    if( !( [o_identifier isEqualToString:TITLE_COLUMN] || [o_identifier isEqualToString:ARTIST_COLUMN] || [o_identifier isEqualToString:DURATION_COLUMN] ) )
+    if( [o_identifier isEqualToString:TRACKNUM_COLUMN] )
+        i_mode = SORT_TRACK_NUMBER;
+    else if( [o_identifier isEqualToString:TITLE_COLUMN] )
+        i_mode = SORT_TITLE;
+    else if( [o_identifier isEqualToString:ARTIST_COLUMN] )
+        i_mode = SORT_ARTIST;
+    else if( [o_identifier isEqualToString:GENRE_COLUMN] )
+        i_mode = SORT_GENRE;
+    else if( [o_identifier isEqualToString:DURATION_COLUMN] )
+        i_mode = SORT_DURATION;
+    else if( [o_identifier isEqualToString:ALBUM_COLUMN] )
+        i_mode = SORT_ALBUM;
+    else if( [o_identifier isEqualToString:DESCRIPTION_COLUMN] )
+        i_mode = SORT_DESCRIPTION;
+    else if( [o_identifier isEqualToString:URI_COLUMN] )
+        i_mode = SORT_URI;
+    else
         return;
 
     if( o_tc_sortColumn == o_tc )
     else
         b_isSortDescending = false;
 
-    if( [o_identifier isEqualToString:TITLE_COLUMN] )
-        i_mode = SORT_TITLE;
-    else if( [o_identifier isEqualToString:ARTIST_COLUMN] )
-        i_mode = SORT_ARTIST;
-    else if( [o_identifier isEqualToString:DURATION_COLUMN] )
-        i_mode = SORT_DURATION;
-
     if( b_isSortDescending )
         i_type = ORDER_REVERSE;
     else
 {
     return [[o_nodes_array arrayByAddingObjectsFromArray: o_items_array] retain];
 }
+
+- (void)setColumn: (NSString *)o_column state: (NSInteger)i_state
+{
+    NSTableColumn * o_work_tc;
+
+    if (i_state == NSOnState)
+    {
+        o_work_tc = [[NSTableColumn alloc] initWithIdentifier: o_column];
+        [o_work_tc setEditable: NO];
+        [[o_work_tc dataCell] setFont: [NSFont controlContentFontOfSize:11.]];
+
+        /* we cannot use a makro here, because gettext isn't clever enough for that */
+        if ([o_column isEqualToString: TRACKNUM_COLUMN])
+        {
+            [o_work_tc setMaxWidth: 20.];
+            [[o_work_tc headerCell] setStringValue: @"#"];
+        }
+        else if ([o_column isEqualToString: TITLE_COLUMN])
+        {
+            [[o_work_tc headerCell] setStringValue: _NS("Name")];
+            [o_work_tc setResizingMask: NSTableColumnAutoresizingMask | NSTableColumnUserResizingMask];
+        }
+        else if ([o_column isEqualToString: ARTIST_COLUMN])
+            [[o_work_tc headerCell] setStringValue: _NS("Author")];
+        else if ([o_column isEqualToString: DURATION_COLUMN])
+            [[o_work_tc headerCell] setStringValue: _NS("Duration")];
+        else if ([o_column isEqualToString: GENRE_COLUMN])
+            [[o_work_tc headerCell] setStringValue: _NS("Genre")];
+        else if ([o_column isEqualToString: ALBUM_COLUMN])
+            [[o_work_tc headerCell] setStringValue: _NS("Album")];
+        else if ([o_column isEqualToString: DESCRIPTION_COLUMN])
+            [[o_work_tc headerCell] setStringValue: _NS("Description")];
+        else if ([o_column isEqualToString: DATE_COLUMN])
+            [[o_work_tc headerCell] setStringValue: _NS("Date")];
+        else if ([o_column isEqualToString: LANGUAGE_COLUMN])
+            [[o_work_tc headerCell] setStringValue: _NS("Language")];
+        else if ([o_column isEqualToString: URI_COLUMN])
+            [[o_work_tc headerCell] setStringValue: _NS("URI")];
+
+        [o_outline_view addTableColumn: o_work_tc];
+        [o_work_tc release];
+        [o_outline_view reloadData];
+        [o_outline_view setNeedsDisplay: YES];
+    }
+    else
+        [o_outline_view removeTableColumn: [o_outline_view tableColumnWithIdentifier: o_column]];
+}
+
+- (void)saveTableColumns
+{
+    NSMutableArray * o_arrayToSave = [[NSMutableArray alloc] init];
+    NSArray * o_columns = [[NSArray alloc] initWithArray:[o_outline_view tableColumns]];
+    NSUInteger count = [o_columns count];
+    NSTableColumn * o_currentColumn;
+    for (NSUInteger i = 0; i < count; i++)
+    {
+        o_currentColumn = [o_columns objectAtIndex: i];
+        [o_arrayToSave addObject: [NSArray arrayWithObjects: [o_currentColumn identifier], [NSNumber numberWithFloat: [o_currentColumn width]], nil]];
+    }
+    [[NSUserDefaults standardUserDefaults] setObject: o_arrayToSave forKey:@"PlaylistColumnSelection"];
+    [[NSUserDefaults standardUserDefaults] synchronize];
+    [o_columns release];
+    [o_arrayToSave release];
+}
+
 @end
 
 @implementation VLCPlaylist (NSOutlineViewDataSource)
     /* Drag & Drop inside the playlist */
     if( [[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] )
     {
-        int i_row, i_removed_from_node = 0;
+        int i_row = 0;
         playlist_item_t *p_new_parent, *p_item = NULL;
         NSArray *o_all_items = [o_nodes_array arrayByAddingObjectsFromArray: o_items_array];
         /* If the item is to be dropped as root item of the outline, make it a
            Else, choose the proposed parent as parent. */
         if( item == nil )
         {
-            if ([self currentPlaylistRoot] == p_playlist->p_local_category || [self currentPlaylistRoot] == p_playlist->p_ml_category) 
+            if ([self currentPlaylistRoot] == p_playlist->p_local_category || [self currentPlaylistRoot] == p_playlist->p_ml_category)
                 p_new_parent = [self currentPlaylistRoot];
             else
                 return NO;
         }
 
         NSUInteger count = [o_all_items count];
+        if( count == 0 )
+            return NO;
+
+        playlist_item_t **pp_items = (playlist_item_t **)calloc( count, sizeof( playlist_item_t* ) );
+        if ( !pp_items )
+            return NO;
+
+        PL_LOCK;
+        NSUInteger j = 0;
         for( NSUInteger i = 0; i < count; i++ )
         {
-            playlist_item_t *p_old_parent = NULL;
-            int i_old_index = 0;
-
             p_item = [[o_all_items objectAtIndex:i] pointerValue];
-            p_old_parent = p_item->p_parent;
-            if( !p_old_parent )
-            continue;
-            /* We may need the old index later */
-            if( p_new_parent == p_old_parent )
-            {
-                for( NSInteger j = 0; j < p_old_parent->i_children; j++ )
-                {
-                    if( p_old_parent->pp_children[j] == p_item )
-                    {
-                        i_old_index = j;
-                        break;
-                    }
-                }
-            }
+            if( p_item )
+                pp_items[j++] = p_item;
+        }
 
-            PL_LOCK;
-            // Actually detach the item from the old position
-            if( playlist_NodeRemoveItem( p_playlist, p_item, p_old_parent ) ==
-                VLC_SUCCESS )
-            {
-                int i_new_index;
-                /* Calculate the new index */
-                if( index == -1 )
-                i_new_index = -1;
-                /* If we move the item in the same node, we need to take into
-                   account that one item will be deleted */
-                else
-                {
-                    if ((p_new_parent == p_old_parent && i_old_index < index + (int)i) )
-                    {
-                        i_removed_from_node++;
-                    }
-                    i_new_index = index + i - i_removed_from_node;
-                }
-                // Reattach the item to the new position
-                playlist_NodeInsert( p_playlist, p_item, p_new_parent, i_new_index );
-            }
+        if( j == 0 || playlist_TreeMoveMany( p_playlist, j, pp_items, p_new_parent, index ) != VLC_SUCCESS )
+        {
             PL_UNLOCK;
+            free( pp_items );
+            return NO;
         }
+
+        PL_UNLOCK;
+        free( pp_items );
+
         [self playlistUpdated];
         i_row = [o_outline_view rowForItem:[o_outline_dict objectForKey:[NSString stringWithFormat: @"%p", [[o_all_items objectAtIndex: 0] pointerValue]]]];
 
 
     else if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
     {
-        if ([self currentPlaylistRoot] != p_playlist->p_local_category && [self currentPlaylistRoot] != p_playlist->p_ml_category) 
+        if ([self currentPlaylistRoot] != p_playlist->p_local_category && [self currentPlaylistRoot] != p_playlist->p_ml_category)
             return NO;
 
         playlist_item_t *p_node = [item pointerValue];