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;
1283 playlist_NodeAddInput( p_playlist, p_input, p_node,
1286 PLAYLIST_END : i_position + i_item,
1290 if( i_item == 0 && !b_enqueue )
1292 playlist_item_t *p_item;
1293 p_item = playlist_ItemGetByInput( p_playlist, p_input );
1294 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, p_node, p_item );
1297 vlc_gc_decref( p_input );
1299 [self playlistUpdated];
1300 pl_Release( VLCIntf );
1303 - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
1305 playlist_t *p_playlist = pl_Hold( VLCIntf );
1306 playlist_item_t *p_selected_item;
1307 int i_current, i_selected_row;
1309 i_selected_row = [o_outline_view selectedRow];
1310 if (i_selected_row < 0)
1313 p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow:
1314 i_selected_row] pointerValue];
1316 for( i_current = 0; i_current < p_item->i_children ; i_current++ )
1319 NSString *o_current_name, *o_current_author;
1322 o_current_name = [NSString stringWithUTF8String:
1323 p_item->pp_children[i_current]->p_input->psz_name];
1324 psz_temp = input_item_GetInfo( p_item->p_input ,
1325 _("Meta-information"),_("Artist") );
1326 o_current_author = [NSString stringWithUTF8String: psz_temp];
1330 if( p_selected_item == p_item->pp_children[i_current] &&
1331 b_selected_item_met == NO )
1333 b_selected_item_met = YES;
1335 else if( p_selected_item == p_item->pp_children[i_current] &&
1336 b_selected_item_met == YES )
1338 pl_Release( VLCIntf );
1341 else if( b_selected_item_met == YES &&
1342 ( [o_current_name rangeOfString:[o_search_field
1343 stringValue] options:NSCaseInsensitiveSearch].length ||
1344 [o_current_author rangeOfString:[o_search_field
1345 stringValue] options:NSCaseInsensitiveSearch].length ) )
1347 pl_Release( VLCIntf );
1348 /*Adds the parent items in the result array as well, so that we can
1350 return [NSMutableArray arrayWithObject: [NSValue
1351 valueWithPointer: p_item->pp_children[i_current]]];
1353 if( p_item->pp_children[i_current]->i_children > 0 )
1355 id o_result = [self subSearchItem:
1356 p_item->pp_children[i_current]];
1357 if( o_result != NULL )
1359 pl_Release( VLCIntf );
1360 [o_result insertObject: [NSValue valueWithPointer:
1361 p_item->pp_children[i_current]] atIndex:0];
1366 pl_Release( VLCIntf );
1370 - (IBAction)searchItem:(id)sender
1372 playlist_t * p_playlist = pl_Hold( VLCIntf );
1378 b_selected_item_met = NO;
1380 /*First, only search after the selected item:*
1381 *(b_selected_item_met = NO) */
1382 o_result = [self subSearchItem:p_playlist->p_root_category];
1383 if( o_result == NULL )
1385 /* If the first search failed, search again from the beginning */
1386 o_result = [self subSearchItem:p_playlist->p_root_category];
1388 if( o_result != NULL )
1391 if( [[o_result objectAtIndex: 0] pointerValue] ==
1392 p_playlist->p_local_category )
1397 for( i = i_start ; i < [o_result count] - 1 ; i++ )
1399 [o_outline_view expandItem: [o_outline_dict objectForKey:
1400 [NSString stringWithFormat: @"%p",
1401 [[o_result objectAtIndex: i] pointerValue]]]];
1403 i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
1404 [NSString stringWithFormat: @"%p",
1405 [[o_result objectAtIndex: [o_result count] - 1 ]
1410 [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:i_row] byExtendingSelection:NO];
1411 [o_outline_view scrollRowToVisible: i_row];
1413 pl_Release( VLCIntf );
1416 - (IBAction)recursiveExpandNode:(id)sender
1418 id o_item = [o_outline_view itemAtRow: [o_outline_view selectedRow]];
1419 playlist_item_t *p_item = (playlist_item_t *)[o_item pointerValue];
1421 if( ![[o_outline_view dataSource] outlineView: o_outline_view
1422 isItemExpandable: o_item] )
1424 o_item = [o_outline_dict objectForKey: [NSString
1425 stringWithFormat: @"%p", p_item->p_parent]];
1428 /* We need to collapse the node first, since OSX refuses to recursively
1429 expand an already expanded node, even if children nodes are collapsed. */
1430 [o_outline_view collapseItem: o_item collapseChildren: YES];
1431 [o_outline_view expandItem: o_item expandChildren: YES];
1434 - (NSMenu *)menuForEvent:(NSEvent *)o_event
1440 pt = [o_outline_view convertPoint: [o_event locationInWindow]
1442 int row = [o_outline_view rowAtPoint:pt];
1444 [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
1446 b_item_sel = ( row != -1 && [o_outline_view selectedRow] != -1 );
1447 b_rows = [o_outline_view numberOfRows] != 0;
1449 [o_mi_play setEnabled: b_item_sel];
1450 [o_mi_delete setEnabled: b_item_sel];
1451 [o_mi_selectall setEnabled: b_rows];
1452 [o_mi_info setEnabled: b_item_sel];
1453 [o_mi_preparse setEnabled: b_item_sel];
1454 [o_mi_recursive_expand setEnabled: b_item_sel];
1455 [o_mi_sort_name setEnabled: b_item_sel];
1456 [o_mi_sort_author setEnabled: b_item_sel];
1458 return( o_ctx_menu );
1461 - (void)outlineView: (NSOutlineView *)o_tv
1462 didClickTableColumn:(NSTableColumn *)o_tc
1464 int i_mode, i_type = 0;
1465 intf_thread_t *p_intf = VLCIntf;
1467 playlist_t *p_playlist = pl_Hold( p_intf );
1469 /* Check whether the selected table column header corresponds to a
1470 sortable table column*/
1471 if( !( o_tc == o_tc_name || o_tc == o_tc_author ) )
1473 pl_Release( p_intf );
1477 if( o_tc_sortColumn == o_tc )
1479 b_isSortDescending = !b_isSortDescending;
1483 b_isSortDescending = false;
1486 if( o_tc == o_tc_name )
1488 i_mode = SORT_TITLE;
1490 else if( o_tc == o_tc_author )
1492 i_mode = SORT_ARTIST;
1495 if( b_isSortDescending )
1497 i_type = ORDER_REVERSE;
1501 i_type = ORDER_NORMAL;
1505 playlist_RecursiveNodeSort( p_playlist, p_playlist->p_root_category, i_mode, i_type );
1508 pl_Release( p_intf );
1509 [self playlistUpdated];
1511 o_tc_sortColumn = o_tc;
1512 [o_outline_view setHighlightedTableColumn:o_tc];
1514 if( b_isSortDescending )
1516 [o_outline_view setIndicatorImage:o_descendingSortingImage
1517 inTableColumn:o_tc];
1521 [o_outline_view setIndicatorImage:o_ascendingSortingImage
1522 inTableColumn:o_tc];
1527 - (void)outlineView:(NSOutlineView *)outlineView
1528 willDisplayCell:(id)cell
1529 forTableColumn:(NSTableColumn *)tableColumn
1532 playlist_t *p_playlist = pl_Hold( VLCIntf );
1537 o_playing_item = [o_outline_dict objectForKey:
1538 [NSString stringWithFormat:@"%p", playlist_CurrentPlayingItem( p_playlist )]];
1541 if( [self isItem: [o_playing_item pointerValue] inNode:
1542 [item pointerValue] checkItemExistence: YES]
1543 || [o_playing_item isEqual: item] )
1545 [cell setFont: [[NSFontManager sharedFontManager] convertFont:[cell font] toHaveTrait:NSBoldFontMask]];
1549 [cell setFont: [[NSFontManager sharedFontManager] convertFont:[cell font] toNotHaveTrait:NSBoldFontMask]];
1551 pl_Release( VLCIntf );
1556 playlist_t *p_playlist = pl_Hold( VLCIntf );
1561 o_playing_item = [o_outline_dict objectForKey:
1562 [NSString stringWithFormat:@"%p", playlist_CurrentPlayingItem( p_playlist )]];
1565 pl_Release( VLCIntf );
1567 return o_playing_item;
1570 - (IBAction)addNode:(id)sender
1572 playlist_t * p_playlist = pl_Hold( VLCIntf );
1573 vlc_thread_set_priority( p_playlist, VLC_THREAD_PRIORITY_LOW );
1576 playlist_NodeCreate( p_playlist, _("Empty Folder"),
1577 p_playlist->p_local_category, 0, NULL );
1580 pl_Release( VLCIntf );
1582 [self playlistUpdated];
1586 @implementation VLCPlaylist (NSOutlineViewDataSource)
1588 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
1590 id o_value = [super outlineView: outlineView child: index ofItem: item];
1591 playlist_t *p_playlist = pl_Hold( VLCIntf );
1594 if( playlist_CurrentSize( p_playlist ) >= 2 )
1596 [o_status_field setStringValue: [NSString stringWithFormat:
1598 playlist_CurrentSize( p_playlist )]];
1599 [o_status_field_embed setStringValue: [NSString stringWithFormat:
1601 playlist_CurrentSize( p_playlist )]];
1605 if( playlist_IsEmpty( p_playlist ) )
1607 [o_status_field setStringValue: _NS("No items in the playlist")];
1608 [o_status_field_embed setStringValue: _NS("No items in the playlist")];
1612 [o_status_field setStringValue: _NS("1 item")];
1613 [o_status_field_embed setStringValue: _NS("1 item")];
1618 pl_Release( VLCIntf );
1620 [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p",
1621 [o_value pointerValue]]];
1626 /* Required for drag & drop and reordering */
1627 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
1630 playlist_t *p_playlist = pl_Hold( VLCIntf );
1632 /* First remove the items that were moved during the last drag & drop
1634 [o_items_array removeAllObjects];
1635 [o_nodes_array removeAllObjects];
1637 for( i = 0 ; i < [items count] ; i++ )
1639 id o_item = [items objectAtIndex: i];
1641 /* Refuse to move items that are not in the General Node
1642 (Service Discovery) */
1643 if( ![self isItem: [o_item pointerValue] inNode:
1644 p_playlist->p_local_category checkItemExistence: NO] &&
1645 var_CreateGetBool( p_playlist, "media-library" ) &&
1646 ![self isItem: [o_item pointerValue] inNode:
1647 p_playlist->p_ml_category checkItemExistence: NO] ||
1648 [o_item pointerValue] == p_playlist->p_local_category ||
1649 [o_item pointerValue] == p_playlist->p_ml_category )
1651 pl_Release( VLCIntf );
1654 /* Fill the items and nodes to move in 2 different arrays */
1655 if( ((playlist_item_t *)[o_item pointerValue])->i_children > 0 )
1656 [o_nodes_array addObject: o_item];
1658 [o_items_array addObject: o_item];
1661 /* Now we need to check if there are selected items that are in already
1662 selected nodes. In that case, we only want to move the nodes */
1663 [self removeItemsFrom: o_nodes_array ifChildrenOf: o_nodes_array];
1664 [self removeItemsFrom: o_items_array ifChildrenOf: o_nodes_array];
1666 /* We add the "VLCPlaylistItemPboardType" type to be able to recognize
1667 a Drop operation coming from the playlist. */
1669 [pboard declareTypes: [NSArray arrayWithObjects:
1670 @"VLCPlaylistItemPboardType", nil] owner: self];
1671 [pboard setData:[NSData data] forType:@"VLCPlaylistItemPboardType"];
1673 pl_Release( VLCIntf );
1677 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)index
1679 playlist_t *p_playlist = pl_Hold( VLCIntf );
1680 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1682 if( !p_playlist ) return NSDragOperationNone;
1684 /* Dropping ON items is not allowed if item is not a node */
1687 if( index == NSOutlineViewDropOnItemIndex &&
1688 ((playlist_item_t *)[item pointerValue])->i_children == -1 )
1690 pl_Release( VLCIntf );
1691 return NSDragOperationNone;
1695 /* Don't allow on drop on playlist root element's child */
1696 if( !item && index != NSOutlineViewDropOnItemIndex)
1698 pl_Release( VLCIntf );
1699 return NSDragOperationNone;
1702 /* We refuse to drop an item in anything else than a child of the General
1703 Node. We still accept items that would be root nodes of the outlineview
1704 however, to allow drop in an empty playlist. */
1705 if( !( ([self isItem: [item pointerValue] inNode: p_playlist->p_local_category checkItemExistence: NO] ||
1706 ( var_CreateGetBool( p_playlist, "media-library" ) && [self isItem: [item pointerValue] inNode: p_playlist->p_ml_category checkItemExistence: NO] ) ) || item == nil ) )
1708 pl_Release( VLCIntf );
1709 return NSDragOperationNone;
1712 /* Drop from the Playlist */
1713 if( [[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] )
1716 for( i = 0 ; i < [o_nodes_array count] ; i++ )
1718 /* We refuse to Drop in a child of an item we are moving */
1719 if( [self isItem: [item pointerValue] inNode:
1720 [[o_nodes_array objectAtIndex: i] pointerValue]
1721 checkItemExistence: NO] )
1723 pl_Release( VLCIntf );
1724 return NSDragOperationNone;
1727 pl_Release( VLCIntf );
1728 return NSDragOperationMove;
1731 /* Drop from the Finder */
1732 else if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1734 pl_Release( VLCIntf );
1735 return NSDragOperationGeneric;
1737 pl_Release( VLCIntf );
1738 return NSDragOperationNone;
1741 - (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(NSInteger)index
1743 playlist_t * p_playlist = pl_Hold( VLCIntf );
1744 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1746 /* Drag & Drop inside the playlist */
1747 if( [[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] )
1749 int i_row, i_removed_from_node = 0;
1751 playlist_item_t *p_new_parent, *p_item = NULL;
1752 NSArray *o_all_items = [o_nodes_array arrayByAddingObjectsFromArray:
1754 /* If the item is to be dropped as root item of the outline, make it a
1755 child of the General node.
1756 Else, choose the proposed parent as parent. */
1757 if( item == nil ) p_new_parent = p_playlist->p_local_category;
1758 else p_new_parent = [item pointerValue];
1760 /* Make sure the proposed parent is a node.
1761 (This should never be true) */
1762 if( p_new_parent->i_children < 0 )
1764 pl_Release( VLCIntf );
1768 for( i = 0; i < [o_all_items count]; i++ )
1770 playlist_item_t *p_old_parent = NULL;
1771 int i_old_index = 0;
1773 p_item = [[o_all_items objectAtIndex:i] pointerValue];
1774 p_old_parent = p_item->p_parent;
1777 /* We may need the old index later */
1778 if( p_new_parent == p_old_parent )
1781 for( j = 0; j < p_old_parent->i_children; j++ )
1783 if( p_old_parent->pp_children[j] == p_item )
1792 // Actually detach the item from the old position
1793 if( playlist_NodeRemoveItem( p_playlist, p_item, p_old_parent ) ==
1797 /* Calculate the new index */
1800 /* If we move the item in the same node, we need to take into
1801 account that one item will be deleted */
1804 if ((p_new_parent == p_old_parent &&
1805 i_old_index < index + (int)i) )
1807 i_removed_from_node++;
1809 i_new_index = index + i - i_removed_from_node;
1811 // Reattach the item to the new position
1812 playlist_NodeInsert( p_playlist, p_item, p_new_parent, i_new_index );
1816 [self playlistUpdated];
1817 i_row = [o_outline_view rowForItem:[o_outline_dict
1818 objectForKey:[NSString stringWithFormat: @"%p",
1819 [[o_all_items objectAtIndex: 0] pointerValue]]]];
1823 i_row = [o_outline_view rowForItem:[o_outline_dict
1824 objectForKey:[NSString stringWithFormat: @"%p", p_new_parent]]];
1827 [o_outline_view deselectAll: self];
1828 [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:i_row] byExtendingSelection:NO];
1829 [o_outline_view scrollRowToVisible: i_row];
1831 pl_Release( VLCIntf );
1835 else if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1838 playlist_item_t *p_node = [item pointerValue];
1840 NSArray *o_array = [NSArray array];
1841 NSArray *o_values = [[o_pasteboard propertyListForType:
1842 NSFilenamesPboardType]
1843 sortedArrayUsingSelector:
1844 @selector(caseInsensitiveCompare:)];
1846 for( i = 0; i < (int)[o_values count]; i++)
1848 NSDictionary *o_dic;
1849 o_dic = [NSDictionary dictionaryWithObject:[o_values
1850 objectAtIndex:i] forKey:@"ITEM_URL"];
1851 o_array = [o_array arrayByAddingObject: o_dic];
1856 [self appendArray:o_array atPos:index enqueue: YES];
1860 assert( p_node->i_children != -1 );
1861 [self appendNodeArray:o_array inNode: p_node
1862 atPos:index enqueue:YES];
1864 pl_Release( VLCIntf );
1867 pl_Release( VLCIntf );