1 /*****************************************************************************
2 * playlist.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2002-2009 the VideoLAN team
7 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8 * Derk-Jan Hartman <hartman at videola/n dot org>
9 * Benjamin Pracht <bigben at videolab dot org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
27 * add 'icons' for different types of nodes? (http://www.cocoadev.com/index.pl?IconAndTextInTableCell)
28 * reimplement enable/disable item
29 * create a new 'tool' button (see the gear button in the Finder window) for 'actions'
30 (adding service discovery, other views, new node/playlist, save node/playlist) stuff like that
34 /*****************************************************************************
36 *****************************************************************************/
37 #include <stdlib.h> /* malloc(), free() */
38 #include <sys/param.h> /* for MAXPATHLEN */
41 #include <sys/mount.h>
46 #import "playlistinfo.h"
50 #import "sidebarview.h"
53 #import <vlc_services_discovery.h>
55 #import <vlc_interface.h>
58 /*****************************************************************************
59 * VLCPlaylistView implementation
60 *****************************************************************************/
61 @implementation VLCPlaylistView
63 - (NSMenu *)menuForEvent:(NSEvent *)o_event
65 return( [[self delegate] menuForEvent: o_event] );
68 - (void)keyDown:(NSEvent *)o_event
72 if( [[o_event characters] length] )
74 key = [[o_event characters] characterAtIndex: 0];
79 case NSDeleteCharacter:
80 case NSDeleteFunctionKey:
81 case NSDeleteCharFunctionKey:
82 case NSBackspaceCharacter:
83 [[self delegate] deleteItem:self];
86 case NSEnterCharacter:
87 case NSCarriageReturnCharacter:
88 [(VLCPlaylist *)[[VLCMain sharedInstance] playlist] playItem:self];
92 [super keyDown: o_event];
99 /*****************************************************************************
100 * VLCPlaylistCommon implementation
102 * This class the superclass of the VLCPlaylist and VLCPlaylistWizard.
103 * It contains the common methods and elements of these 2 entities.
104 *****************************************************************************/
105 @implementation VLCPlaylistCommon
112 o_outline_dict = [[NSMutableDictionary alloc] init];
118 playlist_t * p_playlist = pl_Hold( VLCIntf );
119 [o_outline_view setTarget: self];
120 [o_outline_view setDelegate: self];
121 [o_outline_view setDataSource: self];
122 [o_outline_view setAllowsEmptySelection: NO];
123 [o_outline_view expandItem: [o_outline_view itemAtRow:0]];
125 [o_outline_view_other setTarget: self];
126 [o_outline_view_other setDelegate: self];
127 [o_outline_view_other setDataSource: self];
128 [o_outline_view_other setAllowsEmptySelection: NO];
130 pl_Release( VLCIntf );
136 [[o_tc_name headerCell] setStringValue:_NS("Name")];
137 [[o_tc_author headerCell] setStringValue:_NS("Author")];
138 [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
140 [[o_tc_name_other headerCell] setStringValue:_NS("Name")];
141 [[o_tc_author_other headerCell] setStringValue:_NS("Author")];
142 [[o_tc_duration_other headerCell] setStringValue:_NS("Duration")];
145 - (void)swapPlaylists:(id)newList
147 if(newList != o_outline_view)
149 id o_outline_view_temp = o_outline_view;
150 id o_tc_author_temp = o_tc_author;
151 id o_tc_duration_temp = o_tc_duration;
152 id o_tc_name_temp = o_tc_name;
153 o_outline_view = o_outline_view_other;
154 o_tc_author = o_tc_author_other;
155 o_tc_duration = o_tc_duration_other;
156 o_tc_name = o_tc_name_other;
157 o_outline_view_other = o_outline_view_temp;
158 o_tc_author_other = o_tc_author_temp;
159 o_tc_duration_other = o_tc_duration_temp;
160 o_tc_name_other = o_tc_name_temp;
164 - (NSOutlineView *)outlineView
166 return o_outline_view;
169 - (playlist_item_t *)selectedPlaylistItem
171 return [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
177 @implementation VLCPlaylistCommon (NSOutlineViewDataSource)
179 /* return the number of children for Obj-C pointer item */ /* DONE */
180 - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
183 playlist_item_t *p_item = NULL;
184 playlist_t * p_playlist = pl_Hold( VLCIntf );
185 //assert( outlineView == o_outline_view );
188 p_item = p_playlist->p_root_category;
190 p_item = (playlist_item_t *)[item pointerValue];
193 i_return = p_item->i_children;
195 pl_Release( VLCIntf );
197 return i_return > 0 ? i_return : 0;
200 /* return the child at index for the Obj-C pointer item */ /* DONE */
201 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
203 playlist_item_t *p_return = NULL, *p_item = NULL;
205 playlist_t * p_playlist = pl_Hold( VLCIntf );
211 p_item = p_playlist->p_root_category;
215 p_item = (playlist_item_t *)[item pointerValue];
217 if( p_item && index < p_item->i_children && index >= 0 )
218 p_return = p_item->pp_children[index];
221 pl_Release( VLCIntf );
223 o_value = [o_outline_dict objectForKey:[NSString stringWithFormat: @"%p", p_return]];
227 /* FIXME: Why is there a warning if that happens all the time and seems
228 * to be normal? Add an assert and fix it.
229 * msg_Warn( VLCIntf, "playlist item misses pointer value, adding one" ); */
230 o_value = [[NSValue valueWithPointer: p_return] retain];
235 /* is the item expandable */
236 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
239 playlist_t *p_playlist = pl_Hold( VLCIntf );
245 if( p_playlist->p_root_category )
247 i_return = p_playlist->p_root_category->i_children;
252 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
254 i_return = p_item->i_children;
257 pl_Release( VLCIntf );
259 return (i_return >= 0);
262 /* retrieve the string values for the cells */
263 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
266 playlist_item_t *p_item;
268 /* For error handling */
269 static BOOL attempted_reload = NO;
271 if( item == nil || ![item isKindOfClass: [NSValue class]] )
273 /* Attempt to fix the error by asking for a data redisplay
274 * This might cause infinite loop, so add a small check */
275 if( !attempted_reload )
277 attempted_reload = YES;
278 [outlineView reloadData];
283 p_item = (playlist_item_t *)[item pointerValue];
284 if( !p_item || !p_item->p_input )
286 /* Attempt to fix the error by asking for a data redisplay
287 * This might cause infinite loop, so add a small check */
288 if( !attempted_reload )
290 attempted_reload = YES;
291 [outlineView reloadData];
296 attempted_reload = NO;
298 if( [[o_tc identifier] isEqualToString:@"name"] )
300 /* sanity check to prevent the NSString class from crashing */
301 char *psz_title = input_item_GetTitleFbName( p_item->p_input );
304 o_value = [NSString stringWithUTF8String: psz_title];
308 else if( [[o_tc identifier] isEqualToString:@"artist"] )
310 char *psz_artist = input_item_GetArtist( p_item->p_input );
312 o_value = [NSString stringWithUTF8String: psz_artist];
315 else if( [[o_tc identifier] isEqualToString:@"duration"] )
317 char psz_duration[MSTRTIME_MAX_SIZE];
318 mtime_t dur = input_item_GetDuration( p_item->p_input );
321 secstotimestr( psz_duration, dur/1000000 );
322 o_value = [NSString stringWithUTF8String: psz_duration];
327 else if( [[o_tc identifier] isEqualToString:@"status"] )
329 if( input_item_HasErrorWhenReading( p_item->p_input ) )
331 o_value = [NSImage imageWithWarningIcon];
339 /*****************************************************************************
340 * VLCPlaylistWizard implementation
341 *****************************************************************************/
342 @implementation VLCPlaylistWizard
344 - (IBAction)reloadOutlineView
346 /* Only reload the outlineview if the wizard window is open since this can
347 be quite long on big playlists */
348 if( [[o_outline_view window] isVisible] )
350 [o_outline_view reloadData];
356 /*****************************************************************************
357 * extension to NSOutlineView's interface to fix compilation warnings
358 * and let us access these 2 functions properly
359 * this uses a private Apple-API, but works fine on all current OSX releases
360 * keep checking for compatiblity with future releases though
361 *****************************************************************************/
363 @interface NSOutlineView (UndocumentedSortImages)
364 + (NSImage *)_defaultTableHeaderSortImage;
365 + (NSImage *)_defaultTableHeaderReverseSortImage;
369 /*****************************************************************************
370 * VLCPlaylist implementation
371 *****************************************************************************/
372 @implementation VLCPlaylist
379 o_nodes_array = [[NSMutableArray alloc] init];
380 o_items_array = [[NSMutableArray alloc] init];
387 [o_nodes_array release];
388 [o_items_array release];
394 playlist_t * p_playlist = pl_Hold( VLCIntf );
398 [super awakeFromNib];
400 [o_outline_view setDoubleAction: @selector(playItem:)];
401 [o_outline_view_other setDoubleAction: @selector(playItem:)];
403 [o_outline_view registerForDraggedTypes:
404 [NSArray arrayWithObjects: NSFilenamesPboardType,
405 @"VLCPlaylistItemPboardType", nil]];
406 [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
408 [o_outline_view_other registerForDraggedTypes:
409 [NSArray arrayWithObjects: NSFilenamesPboardType,
410 @"VLCPlaylistItemPboardType", nil]];
411 [o_outline_view_other setIntercellSpacing: NSMakeSize (0.0, 1.0)];
413 /* This uses private Apple API which works fine until 10.5.
414 * We need to keep checking in the future!
415 * These methods are being added artificially to NSOutlineView's interface above */
416 o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
417 o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
419 o_tc_sortColumn = nil;
422 char ** ppsz_services = vlc_sd_GetNames( VLCIntf, &ppsz_name );
425 pl_Release( VLCIntf );
429 for( i = 0; ppsz_services[i]; i++ )
434 char * name = ppsz_name[i] ? ppsz_name[i] : ppsz_services[i];
435 /* Check whether to enable these menuitems */
436 b_enabled = playlist_IsServicesDiscoveryLoaded( p_playlist, ppsz_services[i] );
438 /* Create the menu entries used in the playlist menu */
439 o_lmi = [[o_mi_services submenu] addItemWithTitle:
440 [NSString stringWithUTF8String: name]
441 action: @selector(servicesChange:)
443 [o_lmi setTarget: self];
444 [o_lmi setRepresentedObject: [NSString stringWithUTF8String: ppsz_services[i]]];
445 if( b_enabled ) [o_lmi setState: NSOnState];
447 /* Create the menu entries for the main menu */
448 o_lmi = [[o_mm_mi_services submenu] addItemWithTitle:
449 [NSString stringWithUTF8String: name]
450 action: @selector(servicesChange:)
452 [o_lmi setTarget: self];
453 [o_lmi setRepresentedObject: [NSString stringWithUTF8String: ppsz_services[i]]];
454 if( b_enabled ) [o_lmi setState: NSOnState];
456 free( ppsz_services[i] );
457 free( ppsz_name[i] );
459 free( ppsz_services );
462 pl_Release( VLCIntf );
465 - (void)searchfieldChanged:(NSNotification *)o_notification
467 [o_search_field setStringValue:[[o_notification object] stringValue]];
474 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
475 [o_mi_play setTitle: _NS("Play")];
476 [o_mi_delete setTitle: _NS("Delete")];
477 [o_mi_recursive_expand setTitle: _NS("Expand Node")];
478 [o_mi_selectall setTitle: _NS("Select All")];
479 [o_mi_info setTitle: _NS("Media Information...")];
480 [o_mi_dl_cover_art setTitle: _NS("Download Cover Art")];
481 [o_mi_preparse setTitle: _NS("Fetch Meta Data")];
482 [o_mi_revealInFinder setTitle: _NS("Reveal in Finder")];
483 [o_mm_mi_revealInFinder setTitle: _NS("Reveal in Finder")];
484 [[o_mm_mi_revealInFinder menu] setAutoenablesItems: NO];
485 [o_mi_sort_name setTitle: _NS("Sort Node by Name")];
486 [o_mi_sort_author setTitle: _NS("Sort Node by Author")];
487 [o_mi_services setTitle: _NS("Services discovery")];
488 [o_mm_mi_services setTitle: _NS("Services discovery")];
489 [o_status_field setStringValue: _NS("No items in the playlist")];
490 [o_status_field_embed setStringValue: _NS("No items in the playlist")];
492 [o_search_field setToolTip: _NS("Search in Playlist")];
493 [o_search_field_other setToolTip: _NS("Search in Playlist")];
494 [o_mi_addNode setTitle: _NS("Add Folder to Playlist")];
496 [o_save_accessory_text setStringValue: _NS("File Format:")];
497 [[o_save_accessory_popup itemAtIndex:0] setTitle: _NS("Extended M3U")];
498 [[o_save_accessory_popup itemAtIndex:1] setTitle: _NS("XML Shareable Playlist Format (XSPF)")];
499 [[o_save_accessory_popup itemAtIndex:2] setTitle: _NS("HTML Playlist")];
502 - (void)swapPlaylists:(id)newList
504 if(newList != o_outline_view)
506 id o_search_field_temp = o_search_field;
507 o_search_field = o_search_field_other;
508 o_search_field_other = o_search_field_temp;
509 [super swapPlaylists:newList];
510 [self playlistUpdated];
514 - (void)playlistUpdated
516 /* Clear indications of any existing column sorting */
517 for( unsigned int i = 0 ; i < [[o_outline_view tableColumns] count] ; i++ )
519 [o_outline_view setIndicatorImage:nil inTableColumn:
520 [[o_outline_view tableColumns] objectAtIndex:i]];
523 [o_outline_view setHighlightedTableColumn:nil];
524 o_tc_sortColumn = nil;
525 // TODO Find a way to keep the dict size to a minimum
526 //[o_outline_dict removeAllObjects];
527 [o_outline_view reloadData];
528 [o_sidebar updateSidebar:[self playingItem]];
529 [[[[VLCMain sharedInstance] wizard] playlistWizard] reloadOutlineView];
530 [[[[VLCMain sharedInstance] bookmarks] dataTable] reloadData];
532 playlist_t *p_playlist = pl_Hold( VLCIntf );
535 if( playlist_CurrentSize( p_playlist ) >= 2 )
537 [o_status_field setStringValue: [NSString stringWithFormat:
539 playlist_CurrentSize( p_playlist )]];
540 [o_status_field_embed setStringValue: [NSString stringWithFormat:
542 playlist_CurrentSize( p_playlist )]];
546 if( playlist_IsEmpty( p_playlist ) )
548 [o_status_field setStringValue: _NS("No items in the playlist")];
549 [o_status_field_embed setStringValue: _NS("No items in the playlist")];
553 [o_status_field setStringValue: _NS("1 item")];
554 [o_status_field_embed setStringValue: _NS("1 item")];
558 pl_Release( VLCIntf );
560 [self outlineViewSelectionDidChange: nil];
563 - (void)playModeUpdated
565 playlist_t *p_playlist = pl_Hold( VLCIntf );
567 bool loop = var_GetBool( p_playlist, "loop" );
568 bool repeat = var_GetBool( p_playlist, "repeat" );
570 [[[VLCMain sharedInstance] controls] repeatOne];
572 [[[VLCMain sharedInstance] controls] repeatAll];
574 [[[VLCMain sharedInstance] controls] repeatOff];
576 [[[VLCMain sharedInstance] controls] shuffle];
578 pl_Release( VLCIntf );
581 - (void)outlineViewSelectionDidChange:(NSNotification *)notification
584 playlist_item_t * p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
588 /* update the state of our Reveal-in-Finder menu items */
589 NSMutableString *o_mrl;
590 char *psz_uri = input_item_GetURI( p_item->p_input );
592 [o_mi_revealInFinder setEnabled: NO];
593 [o_mm_mi_revealInFinder setEnabled: NO];
596 o_mrl = [NSMutableString stringWithUTF8String: psz_uri];
598 /* perform some checks whether it is a file and if it is local at all... */
599 NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
600 if( prefix_range.location != NSNotFound )
601 [o_mrl deleteCharactersInRange: prefix_range];
603 if( [o_mrl characterAtIndex:0] == '/' )
605 [o_mi_revealInFinder setEnabled: YES];
606 [o_mm_mi_revealInFinder setEnabled: YES];
611 if( [[VLCMain sharedInstance] isPlaylistCollapsed] == NO )
613 /* update our info-panel to reflect the new item, if we aren't collapsed */
614 [[[VLCMain sharedInstance] info] updatePanelWithItem:p_item->p_input];
619 - (BOOL)isSelectionEmpty
621 return [o_outline_view selectedRow] == -1;
624 - (void)updateRowSelection
630 playlist_t *p_playlist = pl_Hold( VLCIntf );
631 playlist_item_t *p_item, *p_temp_item;
632 NSMutableArray *o_array = [NSMutableArray array];
635 p_item = playlist_CurrentPlayingItem( p_playlist );
639 pl_Release( VLCIntf );
643 p_temp_item = p_item;
644 while( p_temp_item->p_parent )
646 [o_array insertObject: [NSValue valueWithPointer: p_temp_item] atIndex: 0];
647 p_temp_item = p_temp_item->p_parent;
651 for( j = 0; j < [o_array count] - 1; j++ )
654 if( ( o_item = [o_outline_dict objectForKey:
655 [NSString stringWithFormat: @"%p",
656 [[o_array objectAtIndex:j] pointerValue]]] ) != nil )
658 [o_outline_view expandItem: o_item];
662 pl_Release( VLCIntf );
665 /* Check if p_item is a child of p_node recursively. We need to check the item
666 existence first since OSX sometimes tries to redraw items that have been
667 deleted. We don't do it when not required since this verification takes
668 quite a long time on big playlists (yes, pretty hacky). */
670 - (BOOL)isItem: (playlist_item_t *)p_item
671 inNode: (playlist_item_t *)p_node
672 checkItemExistence:(BOOL)b_check
673 locked:(BOOL)b_locked
676 playlist_t * p_playlist = pl_Hold( VLCIntf );
677 playlist_item_t *p_temp_item = p_item;
679 if( p_node == p_item )
681 pl_Release( VLCIntf );
685 if( p_node->i_children < 1)
687 pl_Release( VLCIntf );
694 if(!b_locked) PL_LOCK;
698 /* Since outlineView: willDisplayCell:... may call this function with
699 p_items that don't exist anymore, first check if the item is still
700 in the playlist. Any cleaner solution welcomed. */
701 for( i = 0; i < p_playlist->all_items.i_size; i++ )
703 if( ARRAY_VAL( p_playlist->all_items, i) == p_item ) break;
704 else if ( i == p_playlist->all_items.i_size - 1 )
706 if(!b_locked) PL_UNLOCK;
707 pl_Release( VLCIntf );
715 p_temp_item = p_temp_item->p_parent;
716 if( p_temp_item == p_node )
718 if(!b_locked) PL_UNLOCK;
719 pl_Release( VLCIntf );
723 if(!b_locked) PL_UNLOCK;
726 pl_Release( VLCIntf );
730 - (BOOL)isItem: (playlist_item_t *)p_item
731 inNode: (playlist_item_t *)p_node
732 checkItemExistence:(BOOL)b_check
734 [self isItem:p_item inNode:p_node checkItemExistence:b_check locked:NO];
737 /* This method is usefull for instance to remove the selected children of an
738 already selected node */
739 - (void)removeItemsFrom:(id)o_items ifChildrenOf:(id)o_nodes
742 for( i = 0 ; i < [o_items count] ; i++ )
744 for ( j = 0 ; j < [o_nodes count] ; j++ )
746 if( o_items == o_nodes)
748 if( j == i ) continue;
750 if( [self isItem: [[o_items objectAtIndex:i] pointerValue]
751 inNode: [[o_nodes objectAtIndex:j] pointerValue]
752 checkItemExistence: NO locked:NO] )
754 [o_items removeObjectAtIndex:i];
755 /* We need to execute the next iteration with the same index
756 since the current item has been deleted */
764 - (IBAction)savePlaylist:(id)sender
766 playlist_t * p_playlist = pl_Hold( VLCIntf );
768 NSSavePanel *o_save_panel = [NSSavePanel savePanel];
769 NSString * o_name = [NSString stringWithFormat: @"%@", _NS("Untitled")];
771 [o_save_panel setTitle: _NS("Save Playlist")];
772 [o_save_panel setPrompt: _NS("Save")];
773 [o_save_panel setAccessoryView: o_save_accessory_view];
775 if( [o_save_panel runModalForDirectory: nil
776 file: o_name] == NSOKButton )
778 NSString *o_filename = [o_save_panel filename];
780 if( [o_save_accessory_popup indexOfSelectedItem] == 0 )
782 NSString * o_real_filename;
784 range.location = [o_filename length] - [@".m3u" length];
785 range.length = [@".m3u" length];
787 if( [o_filename compare:@".m3u" options: NSCaseInsensitiveSearch
788 range: range] != NSOrderedSame )
790 o_real_filename = [NSString stringWithFormat: @"%@.m3u", o_filename];
794 o_real_filename = o_filename;
796 playlist_Export( p_playlist,
797 [o_real_filename fileSystemRepresentation],
798 p_playlist->p_local_category, "export-m3u" );
800 else if( [o_save_accessory_popup indexOfSelectedItem] == 1 )
802 NSString * o_real_filename;
804 range.location = [o_filename length] - [@".xspf" length];
805 range.length = [@".xspf" length];
807 if( [o_filename compare:@".xspf" options: NSCaseInsensitiveSearch
808 range: range] != NSOrderedSame )
810 o_real_filename = [NSString stringWithFormat: @"%@.xspf", o_filename];
814 o_real_filename = o_filename;
816 playlist_Export( p_playlist,
817 [o_real_filename fileSystemRepresentation],
818 p_playlist->p_local_category, "export-xspf" );
822 NSString * o_real_filename;
824 range.location = [o_filename length] - [@".html" length];
825 range.length = [@".html" length];
827 if( [o_filename compare:@".html" options: NSCaseInsensitiveSearch
828 range: range] != NSOrderedSame )
830 o_real_filename = [NSString stringWithFormat: @"%@.html", o_filename];
834 o_real_filename = o_filename;
836 playlist_Export( p_playlist,
837 [o_real_filename fileSystemRepresentation],
838 p_playlist->p_local_category, "export-html" );
841 pl_Release( VLCIntf );
844 /* When called retrieves the selected outlineview row and plays that node or item */
845 - (IBAction)playItem:(id)sender
847 intf_thread_t * p_intf = VLCIntf;
848 playlist_t * p_playlist = pl_Hold( p_intf );
850 playlist_item_t *p_item;
851 playlist_item_t *p_node = NULL;
853 p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
858 if( p_item->i_children == -1 )
860 p_node = p_item->p_parent;
865 if( p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1 )
867 p_item = p_node->pp_children[0];
874 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, p_node, p_item );
877 pl_Release( p_intf );
880 - (void)playSidebarItem:(id)item
882 intf_thread_t * p_intf = VLCIntf;
883 playlist_t * p_playlist = pl_Hold( p_intf );
885 playlist_item_t *p_item;
886 playlist_item_t *p_node = NULL;
888 p_item = [item pointerValue];
892 if( p_item->i_children == -1 )
894 p_node = p_item->p_parent;
900 if( p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1 )
902 p_item = p_node->pp_children[0];
909 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Unlocked, p_node, p_item );
911 pl_Release( p_intf );
914 - (IBAction)revealItemInFinder:(id)sender
916 playlist_item_t * p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
917 NSMutableString * o_mrl = nil;
919 if(! p_item || !p_item->p_input )
922 char *psz_uri = input_item_GetURI( p_item->p_input );
924 o_mrl = [NSMutableString stringWithUTF8String: psz_uri];
926 /* perform some checks whether it is a file and if it is local at all... */
927 NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
928 if( prefix_range.location != NSNotFound )
929 [o_mrl deleteCharactersInRange: prefix_range];
931 if( [o_mrl characterAtIndex:0] == '/' )
932 [[NSWorkspace sharedWorkspace] selectFile: o_mrl inFileViewerRootedAtPath: o_mrl];
935 /* When called retrieves the selected outlineview row and plays that node or item */
936 - (IBAction)preparseItem:(id)sender
939 NSMutableArray *o_to_preparse;
940 intf_thread_t * p_intf = VLCIntf;
941 playlist_t * p_playlist = pl_Hold( p_intf );
943 o_to_preparse = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
944 i_count = [o_to_preparse count];
948 playlist_item_t *p_item = NULL;
950 for( i = 0; i < i_count; i++ )
952 o_number = [o_to_preparse lastObject];
953 i_row = [o_number intValue];
954 p_item = [[o_outline_view itemAtRow:i_row] pointerValue];
955 [o_to_preparse removeObject: o_number];
956 [o_outline_view deselectRow: i_row];
960 if( p_item->i_children == -1 )
962 playlist_PreparseEnqueue( p_playlist, p_item->p_input, pl_Unlocked );
966 msg_Dbg( p_intf, "preparsing nodes not implemented" );
970 pl_Release( p_intf );
971 [self playlistUpdated];
974 - (IBAction)downloadCoverArt:(id)sender
977 NSMutableArray *o_to_preparse;
978 intf_thread_t * p_intf = VLCIntf;
979 playlist_t * p_playlist = pl_Hold( p_intf );
981 o_to_preparse = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
982 i_count = [o_to_preparse count];
986 playlist_item_t *p_item = NULL;
988 for( i = 0; i < i_count; i++ )
990 o_number = [o_to_preparse lastObject];
991 i_row = [o_number intValue];
992 p_item = [[o_outline_view itemAtRow:i_row] pointerValue];
993 [o_to_preparse removeObject: o_number];
994 [o_outline_view deselectRow: i_row];
996 if( p_item && p_item->i_children == -1 )
998 playlist_AskForArtEnqueue( p_playlist, p_item->p_input, pl_Unlocked );
1001 pl_Release( p_intf );
1002 [self playlistUpdated];
1005 - (IBAction)servicesChange:(id)sender
1007 NSMenuItem *o_mi = (NSMenuItem *)sender;
1008 NSString *o_string = [o_mi representedObject];
1009 playlist_t * p_playlist = pl_Hold( VLCIntf );
1010 if( !playlist_IsServicesDiscoveryLoaded( p_playlist, [o_string UTF8String] ) )
1011 playlist_ServicesDiscoveryAdd( p_playlist, [o_string UTF8String] );
1013 playlist_ServicesDiscoveryRemove( p_playlist, [o_string UTF8String] );
1015 [o_mi setState: playlist_IsServicesDiscoveryLoaded( p_playlist,
1016 [o_string UTF8String] ) ? YES : NO];
1018 pl_Release( VLCIntf );
1019 [self playlistUpdated];
1023 - (IBAction)selectAll:(id)sender
1025 [o_outline_view selectAll: nil];
1028 - (IBAction)deleteItem:(id)sender
1031 NSMutableArray *o_to_delete;
1034 playlist_t * p_playlist;
1035 intf_thread_t * p_intf = VLCIntf;
1037 o_to_delete = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
1038 i_count = [o_to_delete count];
1040 p_playlist = pl_Hold( p_intf );
1042 for( int i = 0; i < i_count; i++ )
1044 o_number = [o_to_delete lastObject];
1045 i_row = [o_number intValue];
1046 id o_item = [o_outline_view itemAtRow: i_row];
1047 [o_outline_view deselectRow: i_row];
1050 playlist_item_t *p_item = [o_item pointerValue];
1052 msg_Dbg( p_intf, "deleting item %i (of %i) with id \"%i\", pointerValue \"%p\" and %i children", i+1, i_count,
1053 p_item->p_input->i_id, [o_item pointerValue], p_item->i_children +1 );
1055 [o_to_delete removeObject: o_number];
1057 if( p_item->i_children != -1 )
1058 //is a node and not an item
1060 if( playlist_Status( p_playlist ) != PLAYLIST_STOPPED &&
1061 [self isItem: playlist_CurrentPlayingItem( p_playlist ) inNode:
1062 ((playlist_item_t *)[o_item pointerValue])
1063 checkItemExistence: NO locked:YES] == YES )
1064 // if current item is in selected node and is playing then stop playlist
1065 playlist_Control(p_playlist, PLAYLIST_STOP, pl_Locked );
1067 playlist_NodeDelete( p_playlist, p_item, true, false );
1070 playlist_DeleteFromInput( p_playlist, p_item->p_input, pl_Locked );
1073 [o_outline_dict removeObjectForKey:[NSString stringWithFormat:@"%p",
1074 [o_item pointerValue]]];
1078 [self playlistUpdated];
1079 pl_Release( p_intf );
1082 - (IBAction)sortNodeByName:(id)sender
1084 [self sortNode: SORT_TITLE];
1087 - (IBAction)sortNodeByAuthor:(id)sender
1089 [self sortNode: SORT_ARTIST];
1092 - (void)sortNode:(int)i_mode
1094 playlist_t * p_playlist = pl_Hold( VLCIntf );
1095 playlist_item_t * p_item;
1097 if( [o_outline_view selectedRow] > -1 )
1099 p_item = [[o_outline_view itemAtRow: [o_outline_view selectedRow]] pointerValue];
1102 /*If no item is selected, sort the whole playlist*/
1104 p_item = p_playlist->p_root_category;
1108 if( p_item->i_children > -1 ) // the item is a node
1110 playlist_RecursiveNodeSort( p_playlist, p_item, i_mode, ORDER_NORMAL );
1114 playlist_RecursiveNodeSort( p_playlist,
1115 p_item->p_parent, i_mode, ORDER_NORMAL );
1118 pl_Release( VLCIntf );
1119 [self playlistUpdated];
1122 - (input_item_t *)createItem:(NSDictionary *)o_one_item
1124 intf_thread_t * p_intf = VLCIntf;
1125 playlist_t * p_playlist = pl_Hold( p_intf );
1127 input_item_t *p_input;
1129 BOOL b_rem = FALSE, b_dir = FALSE;
1130 NSString *o_uri, *o_name;
1135 o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
1136 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
1137 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
1139 /* Find the name for a disc entry (i know, can you believe the trouble?) */
1140 if( ( !o_name || [o_name isEqualToString:@""] ) && [o_uri rangeOfString: @"/dev/"].location != NSNotFound )
1142 int i_count, i_index;
1143 struct statfs *mounts = NULL;
1145 i_count = getmntinfo (&mounts, MNT_NOWAIT);
1146 /* getmntinfo returns a pointer to static data. Do not free. */
1147 for( i_index = 0 ; i_index < i_count; i_index++ )
1149 NSMutableString *o_temp, *o_temp2;
1150 o_temp = [NSMutableString stringWithString: o_uri];
1151 o_temp2 = [NSMutableString stringWithUTF8String: mounts[i_index].f_mntfromname];
1152 [o_temp replaceOccurrencesOfString: @"/dev/rdisk" withString: @"/dev/disk" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
1153 [o_temp2 replaceOccurrencesOfString: @"s0" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp2 length]) ];
1154 [o_temp2 replaceOccurrencesOfString: @"s1" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp2 length]) ];
1156 if( strstr( [o_temp fileSystemRepresentation], [o_temp2 fileSystemRepresentation] ) != NULL )
1158 o_name = [[NSFileManager defaultManager] displayNameAtPath: [NSString stringWithUTF8String:mounts[i_index].f_mntonname]];
1163 if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
1164 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
1165 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
1167 /* All of this is to make sure CD's play when you D&D them on VLC */
1168 /* Converts mountpoint to a /dev file */
1171 NSMutableString *o_temp;
1173 buf = (struct statfs *) malloc (sizeof(struct statfs));
1174 statfs( [o_uri fileSystemRepresentation], buf );
1175 psz_dev = strdup(buf->f_mntfromname);
1176 o_temp = [NSMutableString stringWithUTF8String: psz_dev ];
1177 [o_temp replaceOccurrencesOfString: @"/dev/disk" withString: @"/dev/rdisk" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
1178 [o_temp replaceOccurrencesOfString: @"s0" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
1179 [o_temp replaceOccurrencesOfString: @"s1" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
1183 p_input = input_item_New( p_playlist, [o_uri fileSystemRepresentation], o_name ? [o_name UTF8String] : NULL );
1186 pl_Release( p_intf );
1192 for( i = 0; i < (int)[o_options count]; i++ )
1194 input_item_AddOption( p_input, [[o_options objectAtIndex:i] UTF8String],
1195 VLC_INPUT_OPTION_TRUSTED );
1199 /* Recent documents menu */
1200 o_true_file = [NSURL fileURLWithPath: o_uri];
1201 if( o_true_file != nil && (BOOL)config_GetInt( p_playlist, "macosx-recentitems" ) == YES )
1203 [[NSDocumentController sharedDocumentController]
1204 noteNewRecentDocumentURL: o_true_file];
1207 pl_Release( p_intf );
1211 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
1214 playlist_t * p_playlist = pl_Hold( VLCIntf );
1217 for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
1219 input_item_t *p_input;
1220 NSDictionary *o_one_item;
1223 o_one_item = [o_array objectAtIndex: i_item];
1224 p_input = [self createItem: o_one_item];
1231 /* FIXME: playlist_AddInput() can fail */
1233 playlist_AddInput( p_playlist, p_input, PLAYLIST_INSERT,
1234 i_position == -1 ? PLAYLIST_END : i_position + i_item, true,
1237 if( i_item == 0 && !b_enqueue )
1239 playlist_item_t *p_item = NULL;
1240 playlist_item_t *p_node = NULL;
1241 p_item = playlist_ItemGetByInput( p_playlist, p_input );
1244 if( p_item->i_children == -1 )
1245 p_node = p_item->p_parent;
1249 if( p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1 )
1250 p_item = p_node->pp_children[0];
1254 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, p_node, p_item );
1257 vlc_gc_decref( p_input );
1261 [self playlistUpdated];
1262 pl_Release( VLCIntf );
1265 - (void)appendNodeArray:(NSArray*)o_array inNode:(playlist_item_t *)p_node atPos:(int)i_position enqueue:(BOOL)b_enqueue
1268 playlist_t * p_playlist = pl_Hold( VLCIntf );
1270 for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
1272 input_item_t *p_input;
1273 NSDictionary *o_one_item;
1276 o_one_item = [o_array objectAtIndex: i_item];
1277 p_input = [self createItem: o_one_item];
1279 if( !p_input ) continue;
1282 /* FIXME: playlist_BothAddInput() can fail */
1284 playlist_BothAddInput( p_playlist, p_input, p_node,
1287 PLAYLIST_END : i_position + i_item,
1288 NULL, NULL, pl_Locked );
1291 if( i_item == 0 && !b_enqueue )
1293 playlist_item_t *p_item;
1294 p_item = playlist_ItemGetByInput( p_playlist, p_input );
1295 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, p_node, p_item );
1298 vlc_gc_decref( p_input );
1300 [self playlistUpdated];
1301 pl_Release( VLCIntf );
1304 - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
1306 playlist_t *p_playlist = pl_Hold( VLCIntf );
1307 playlist_item_t *p_selected_item;
1308 int i_current, i_selected_row;
1310 i_selected_row = [o_outline_view selectedRow];
1311 if (i_selected_row < 0)
1314 p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow:
1315 i_selected_row] pointerValue];
1317 for( i_current = 0; i_current < p_item->i_children ; i_current++ )
1320 NSString *o_current_name, *o_current_author;
1323 o_current_name = [NSString stringWithUTF8String:
1324 p_item->pp_children[i_current]->p_input->psz_name];
1325 psz_temp = input_item_GetInfo( p_item->p_input ,
1326 _("Meta-information"),_("Artist") );
1327 o_current_author = [NSString stringWithUTF8String: psz_temp];
1331 if( p_selected_item == p_item->pp_children[i_current] &&
1332 b_selected_item_met == NO )
1334 b_selected_item_met = YES;
1336 else if( p_selected_item == p_item->pp_children[i_current] &&
1337 b_selected_item_met == YES )
1339 pl_Release( VLCIntf );
1342 else if( b_selected_item_met == YES &&
1343 ( [o_current_name rangeOfString:[o_search_field
1344 stringValue] options:NSCaseInsensitiveSearch].length ||
1345 [o_current_author rangeOfString:[o_search_field
1346 stringValue] options:NSCaseInsensitiveSearch].length ) )
1348 pl_Release( VLCIntf );
1349 /*Adds the parent items in the result array as well, so that we can
1351 return [NSMutableArray arrayWithObject: [NSValue
1352 valueWithPointer: p_item->pp_children[i_current]]];
1354 if( p_item->pp_children[i_current]->i_children > 0 )
1356 id o_result = [self subSearchItem:
1357 p_item->pp_children[i_current]];
1358 if( o_result != NULL )
1360 pl_Release( VLCIntf );
1361 [o_result insertObject: [NSValue valueWithPointer:
1362 p_item->pp_children[i_current]] atIndex:0];
1367 pl_Release( VLCIntf );
1371 - (IBAction)searchItem:(id)sender
1373 playlist_t * p_playlist = pl_Hold( VLCIntf );
1379 b_selected_item_met = NO;
1381 /*First, only search after the selected item:*
1382 *(b_selected_item_met = NO) */
1383 o_result = [self subSearchItem:p_playlist->p_root_category];
1384 if( o_result == NULL )
1386 /* If the first search failed, search again from the beginning */
1387 o_result = [self subSearchItem:p_playlist->p_root_category];
1389 if( o_result != NULL )
1392 if( [[o_result objectAtIndex: 0] pointerValue] ==
1393 p_playlist->p_local_category )
1398 for( i = i_start ; i < [o_result count] - 1 ; i++ )
1400 [o_outline_view expandItem: [o_outline_dict objectForKey:
1401 [NSString stringWithFormat: @"%p",
1402 [[o_result objectAtIndex: i] pointerValue]]]];
1404 i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
1405 [NSString stringWithFormat: @"%p",
1406 [[o_result objectAtIndex: [o_result count] - 1 ]
1411 [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:i_row] byExtendingSelection:NO];
1412 [o_outline_view scrollRowToVisible: i_row];
1414 pl_Release( VLCIntf );
1417 - (IBAction)recursiveExpandNode:(id)sender
1419 id o_item = [o_outline_view itemAtRow: [o_outline_view selectedRow]];
1420 playlist_item_t *p_item = (playlist_item_t *)[o_item pointerValue];
1422 if( ![[o_outline_view dataSource] outlineView: o_outline_view
1423 isItemExpandable: o_item] )
1425 o_item = [o_outline_dict objectForKey: [NSString
1426 stringWithFormat: @"%p", p_item->p_parent]];
1429 /* We need to collapse the node first, since OSX refuses to recursively
1430 expand an already expanded node, even if children nodes are collapsed. */
1431 [o_outline_view collapseItem: o_item collapseChildren: YES];
1432 [o_outline_view expandItem: o_item expandChildren: YES];
1435 - (NSMenu *)menuForEvent:(NSEvent *)o_event
1441 pt = [o_outline_view convertPoint: [o_event locationInWindow]
1443 int row = [o_outline_view rowAtPoint:pt];
1445 [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
1447 b_item_sel = ( row != -1 && [o_outline_view selectedRow] != -1 );
1448 b_rows = [o_outline_view numberOfRows] != 0;
1450 [o_mi_play setEnabled: b_item_sel];
1451 [o_mi_delete setEnabled: b_item_sel];
1452 [o_mi_selectall setEnabled: b_rows];
1453 [o_mi_info setEnabled: b_item_sel];
1454 [o_mi_preparse setEnabled: b_item_sel];
1455 [o_mi_recursive_expand setEnabled: b_item_sel];
1456 [o_mi_sort_name setEnabled: b_item_sel];
1457 [o_mi_sort_author setEnabled: b_item_sel];
1459 return( o_ctx_menu );
1462 - (void)outlineView: (NSOutlineView *)o_tv
1463 didClickTableColumn:(NSTableColumn *)o_tc
1465 int i_mode, i_type = 0;
1466 intf_thread_t *p_intf = VLCIntf;
1468 playlist_t *p_playlist = pl_Hold( p_intf );
1470 /* Check whether the selected table column header corresponds to a
1471 sortable table column*/
1472 if( !( o_tc == o_tc_name || o_tc == o_tc_author ) )
1474 pl_Release( p_intf );
1478 if( o_tc_sortColumn == o_tc )
1480 b_isSortDescending = !b_isSortDescending;
1484 b_isSortDescending = false;
1487 if( o_tc == o_tc_name )
1489 i_mode = SORT_TITLE;
1491 else if( o_tc == o_tc_author )
1493 i_mode = SORT_ARTIST;
1496 if( b_isSortDescending )
1498 i_type = ORDER_REVERSE;
1502 i_type = ORDER_NORMAL;
1506 playlist_RecursiveNodeSort( p_playlist, p_playlist->p_root_category, i_mode, i_type );
1509 pl_Release( p_intf );
1510 [self playlistUpdated];
1512 o_tc_sortColumn = o_tc;
1513 [o_outline_view setHighlightedTableColumn:o_tc];
1515 if( b_isSortDescending )
1517 [o_outline_view setIndicatorImage:o_descendingSortingImage
1518 inTableColumn:o_tc];
1522 [o_outline_view setIndicatorImage:o_ascendingSortingImage
1523 inTableColumn:o_tc];
1528 - (void)outlineView:(NSOutlineView *)outlineView
1529 willDisplayCell:(id)cell
1530 forTableColumn:(NSTableColumn *)tableColumn
1533 playlist_t *p_playlist = pl_Hold( VLCIntf );
1538 o_playing_item = [o_outline_dict objectForKey:
1539 [NSString stringWithFormat:@"%p", playlist_CurrentPlayingItem( p_playlist )]];
1542 if( [self isItem: [o_playing_item pointerValue] inNode:
1543 [item pointerValue] checkItemExistence: YES]
1544 || [o_playing_item isEqual: item] )
1546 [cell setFont: [[NSFontManager sharedFontManager] convertFont:[cell font] toHaveTrait:NSBoldFontMask]];
1550 [cell setFont: [[NSFontManager sharedFontManager] convertFont:[cell font] toNotHaveTrait:NSBoldFontMask]];
1552 pl_Release( VLCIntf );
1557 playlist_t *p_playlist = pl_Hold( VLCIntf );
1562 o_playing_item = [o_outline_dict objectForKey:
1563 [NSString stringWithFormat:@"%p", playlist_CurrentPlayingItem( p_playlist )]];
1566 pl_Release( VLCIntf );
1568 return o_playing_item;
1571 - (IBAction)addNode:(id)sender
1573 playlist_t * p_playlist = pl_Hold( VLCIntf );
1574 vlc_thread_set_priority( p_playlist, VLC_THREAD_PRIORITY_LOW );
1577 playlist_NodeCreate( p_playlist, _("Empty Folder"),
1578 p_playlist->p_local_category, 0, NULL );
1581 pl_Release( VLCIntf );
1583 [self playlistUpdated];
1587 @implementation VLCPlaylist (NSOutlineViewDataSource)
1589 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
1591 id o_value = [super outlineView: outlineView child: index ofItem: item];
1592 playlist_t *p_playlist = pl_Hold( VLCIntf );
1595 if( playlist_CurrentSize( p_playlist ) >= 2 )
1597 [o_status_field setStringValue: [NSString stringWithFormat:
1599 playlist_CurrentSize( p_playlist )]];
1600 [o_status_field_embed setStringValue: [NSString stringWithFormat:
1602 playlist_CurrentSize( p_playlist )]];
1606 if( playlist_IsEmpty( p_playlist ) )
1608 [o_status_field setStringValue: _NS("No items in the playlist")];
1609 [o_status_field_embed setStringValue: _NS("No items in the playlist")];
1613 [o_status_field setStringValue: _NS("1 item")];
1614 [o_status_field_embed setStringValue: _NS("1 item")];
1619 pl_Release( VLCIntf );
1621 [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p",
1622 [o_value pointerValue]]];
1627 /* Required for drag & drop and reordering */
1628 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
1631 playlist_t *p_playlist = pl_Hold( VLCIntf );
1633 /* First remove the items that were moved during the last drag & drop
1635 [o_items_array removeAllObjects];
1636 [o_nodes_array removeAllObjects];
1638 for( i = 0 ; i < [items count] ; i++ )
1640 id o_item = [items objectAtIndex: i];
1642 /* Refuse to move items that are not in the General Node
1643 (Service Discovery) */
1644 if( ![self isItem: [o_item pointerValue] inNode:
1645 p_playlist->p_local_category checkItemExistence: NO] &&
1646 var_CreateGetBool( p_playlist, "media-library" ) &&
1647 ![self isItem: [o_item pointerValue] inNode:
1648 p_playlist->p_ml_category checkItemExistence: NO] ||
1649 [o_item pointerValue] == p_playlist->p_local_category ||
1650 [o_item pointerValue] == p_playlist->p_ml_category )
1652 pl_Release( VLCIntf );
1655 /* Fill the items and nodes to move in 2 different arrays */
1656 if( ((playlist_item_t *)[o_item pointerValue])->i_children > 0 )
1657 [o_nodes_array addObject: o_item];
1659 [o_items_array addObject: o_item];
1662 /* Now we need to check if there are selected items that are in already
1663 selected nodes. In that case, we only want to move the nodes */
1664 [self removeItemsFrom: o_nodes_array ifChildrenOf: o_nodes_array];
1665 [self removeItemsFrom: o_items_array ifChildrenOf: o_nodes_array];
1667 /* We add the "VLCPlaylistItemPboardType" type to be able to recognize
1668 a Drop operation coming from the playlist. */
1670 [pboard declareTypes: [NSArray arrayWithObjects:
1671 @"VLCPlaylistItemPboardType", nil] owner: self];
1672 [pboard setData:[NSData data] forType:@"VLCPlaylistItemPboardType"];
1674 pl_Release( VLCIntf );
1678 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)index
1680 playlist_t *p_playlist = pl_Hold( VLCIntf );
1681 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1683 if( !p_playlist ) return NSDragOperationNone;
1685 /* Dropping ON items is not allowed if item is not a node */
1688 if( index == NSOutlineViewDropOnItemIndex &&
1689 ((playlist_item_t *)[item pointerValue])->i_children == -1 )
1691 pl_Release( VLCIntf );
1692 return NSDragOperationNone;
1696 /* Don't allow on drop on playlist root element's child */
1697 if( !item && index != NSOutlineViewDropOnItemIndex)
1699 pl_Release( VLCIntf );
1700 return NSDragOperationNone;
1703 /* We refuse to drop an item in anything else than a child of the General
1704 Node. We still accept items that would be root nodes of the outlineview
1705 however, to allow drop in an empty playlist. */
1706 if( !( ([self isItem: [item pointerValue] inNode: p_playlist->p_local_category checkItemExistence: NO] ||
1707 ( var_CreateGetBool( p_playlist, "media-library" ) && [self isItem: [item pointerValue] inNode: p_playlist->p_ml_category checkItemExistence: NO] ) ) || item == nil ) )
1709 pl_Release( VLCIntf );
1710 return NSDragOperationNone;
1713 /* Drop from the Playlist */
1714 if( [[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] )
1717 for( i = 0 ; i < [o_nodes_array count] ; i++ )
1719 /* We refuse to Drop in a child of an item we are moving */
1720 if( [self isItem: [item pointerValue] inNode:
1721 [[o_nodes_array objectAtIndex: i] pointerValue]
1722 checkItemExistence: NO] )
1724 pl_Release( VLCIntf );
1725 return NSDragOperationNone;
1728 pl_Release( VLCIntf );
1729 return NSDragOperationMove;
1732 /* Drop from the Finder */
1733 else if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1735 pl_Release( VLCIntf );
1736 return NSDragOperationGeneric;
1738 pl_Release( VLCIntf );
1739 return NSDragOperationNone;
1742 - (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(NSInteger)index
1744 playlist_t * p_playlist = pl_Hold( VLCIntf );
1745 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1747 /* Drag & Drop inside the playlist */
1748 if( [[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] )
1750 int i_row, i_removed_from_node = 0;
1752 playlist_item_t *p_new_parent, *p_item = NULL;
1753 NSArray *o_all_items = [o_nodes_array arrayByAddingObjectsFromArray:
1755 /* If the item is to be dropped as root item of the outline, make it a
1756 child of the General node.
1757 Else, choose the proposed parent as parent. */
1758 if( item == nil ) p_new_parent = p_playlist->p_local_category;
1759 else p_new_parent = [item pointerValue];
1761 /* Make sure the proposed parent is a node.
1762 (This should never be true) */
1763 if( p_new_parent->i_children < 0 )
1765 pl_Release( VLCIntf );
1769 for( i = 0; i < [o_all_items count]; i++ )
1771 playlist_item_t *p_old_parent = NULL;
1772 int i_old_index = 0;
1774 p_item = [[o_all_items objectAtIndex:i] pointerValue];
1775 p_old_parent = p_item->p_parent;
1778 /* We may need the old index later */
1779 if( p_new_parent == p_old_parent )
1782 for( j = 0; j < p_old_parent->i_children; j++ )
1784 if( p_old_parent->pp_children[j] == p_item )
1793 // Actually detach the item from the old position
1794 if( playlist_NodeRemoveItem( p_playlist, p_item, p_old_parent ) ==
1798 /* Calculate the new index */
1801 /* If we move the item in the same node, we need to take into
1802 account that one item will be deleted */
1805 if ((p_new_parent == p_old_parent &&
1806 i_old_index < index + (int)i) )
1808 i_removed_from_node++;
1810 i_new_index = index + i - i_removed_from_node;
1812 // Reattach the item to the new position
1813 playlist_NodeInsert( p_playlist, p_item, p_new_parent, i_new_index );
1817 [self playlistUpdated];
1818 i_row = [o_outline_view rowForItem:[o_outline_dict
1819 objectForKey:[NSString stringWithFormat: @"%p",
1820 [[o_all_items objectAtIndex: 0] pointerValue]]]];
1824 i_row = [o_outline_view rowForItem:[o_outline_dict
1825 objectForKey:[NSString stringWithFormat: @"%p", p_new_parent]]];
1828 [o_outline_view deselectAll: self];
1829 [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:i_row] byExtendingSelection:NO];
1830 [o_outline_view scrollRowToVisible: i_row];
1832 pl_Release( VLCIntf );
1836 else if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1839 playlist_item_t *p_node = [item pointerValue];
1841 NSArray *o_array = [NSArray array];
1842 NSArray *o_values = [[o_pasteboard propertyListForType:
1843 NSFilenamesPboardType]
1844 sortedArrayUsingSelector:
1845 @selector(caseInsensitiveCompare:)];
1847 for( i = 0; i < (int)[o_values count]; i++)
1849 NSDictionary *o_dic;
1850 o_dic = [NSDictionary dictionaryWithObject:[o_values
1851 objectAtIndex:i] forKey:@"ITEM_URL"];
1852 o_array = [o_array arrayByAddingObject: o_dic];
1857 [self appendArray:o_array atPos:index enqueue: YES];
1861 assert( p_node->i_children != -1 );
1862 [self appendNodeArray:o_array inNode: p_node
1863 atPos:index enqueue:YES];
1865 pl_Release( VLCIntf );
1868 pl_Release( VLCIntf );