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>
47 #import "playlistinfo.h"
52 #import "sidebarview.h"
53 #import <vlc_interface.h>
54 #import <vlc_services_discovery.h>
56 /*****************************************************************************
57 * VLCPlaylistView implementation
58 *****************************************************************************/
59 @implementation VLCPlaylistView
61 - (NSMenu *)menuForEvent:(NSEvent *)o_event
63 return( [[self delegate] menuForEvent: o_event] );
66 - (void)keyDown:(NSEvent *)o_event
70 if( [[o_event characters] length] )
72 key = [[o_event characters] characterAtIndex: 0];
77 case NSDeleteCharacter:
78 case NSDeleteFunctionKey:
79 case NSDeleteCharFunctionKey:
80 case NSBackspaceCharacter:
81 [[self delegate] deleteItem:self];
84 case NSEnterCharacter:
85 case NSCarriageReturnCharacter:
86 [(VLCPlaylist *)[[VLCMain sharedInstance] playlist] playItem:self];
90 [super keyDown: o_event];
97 /*****************************************************************************
98 * VLCPlaylistCommon implementation
100 * This class the superclass of the VLCPlaylist and VLCPlaylistWizard.
101 * It contains the common methods and elements of these 2 entities.
102 *****************************************************************************/
103 @implementation VLCPlaylistCommon
110 o_outline_dict = [[NSMutableDictionary alloc] init];
116 playlist_t * p_playlist = pl_Hold( VLCIntf );
117 [o_outline_view setTarget: self];
118 [o_outline_view setDelegate: self];
119 [o_outline_view setDataSource: self];
120 [o_outline_view setAllowsEmptySelection: NO];
121 [o_outline_view expandItem: [o_outline_view itemAtRow:0]];
123 [o_outline_view_other setTarget: self];
124 [o_outline_view_other setDelegate: self];
125 [o_outline_view_other setDataSource: self];
126 [o_outline_view_other setAllowsEmptySelection: NO];
128 pl_Release( VLCIntf );
134 [[o_tc_name headerCell] setStringValue:_NS("Name")];
135 [[o_tc_author headerCell] setStringValue:_NS("Author")];
136 [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
138 [[o_tc_name_other headerCell] setStringValue:_NS("Name")];
139 [[o_tc_author_other headerCell] setStringValue:_NS("Author")];
140 [[o_tc_duration_other headerCell] setStringValue:_NS("Duration")];
143 - (void)swapPlaylists:(id)newList
145 if(newList != o_outline_view)
147 id o_outline_view_temp = o_outline_view;
148 id o_tc_author_temp = o_tc_author;
149 id o_tc_duration_temp = o_tc_duration;
150 id o_tc_name_temp = o_tc_name;
151 o_outline_view = o_outline_view_other;
152 o_tc_author = o_tc_author_other;
153 o_tc_duration = o_tc_duration_other;
154 o_tc_name = o_tc_name_other;
155 o_outline_view_other = o_outline_view_temp;
156 o_tc_author_other = o_tc_author_temp;
157 o_tc_duration_other = o_tc_duration_temp;
158 o_tc_name_other = o_tc_name_temp;
162 - (NSOutlineView *)outlineView
164 return o_outline_view;
167 - (playlist_item_t *)selectedPlaylistItem
169 return [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
175 @implementation VLCPlaylistCommon (NSOutlineViewDataSource)
177 /* return the number of children for Obj-C pointer item */ /* DONE */
178 - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
181 playlist_item_t *p_item = NULL;
182 playlist_t * p_playlist = pl_Hold( VLCIntf );
183 assert( outlineView == o_outline_view );
186 p_item = p_playlist->p_root_category;
188 p_item = (playlist_item_t *)[item pointerValue];
191 i_return = p_item->i_children;
193 pl_Release( VLCIntf );
195 return i_return > 0 ? i_return : 0;
198 /* return the child at index for the Obj-C pointer item */ /* DONE */
199 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
201 playlist_item_t *p_return = NULL, *p_item = NULL;
203 playlist_t * p_playlist = pl_Hold( VLCIntf );
209 p_item = p_playlist->p_root_category;
213 p_item = (playlist_item_t *)[item pointerValue];
215 if( p_item && index < p_item->i_children && index >= 0 )
216 p_return = p_item->pp_children[index];
219 pl_Release( VLCIntf );
221 o_value = [o_outline_dict objectForKey:[NSString stringWithFormat: @"%p", p_return]];
225 /* FIXME: Why is there a warning if that happens all the time and seems
226 * to be normal? Add an assert and fix it.
227 * msg_Warn( VLCIntf, "playlist item misses pointer value, adding one" ); */
228 o_value = [[NSValue valueWithPointer: p_return] retain];
233 /* is the item expandable */
234 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
237 playlist_t *p_playlist = pl_Hold( VLCIntf );
242 if( p_playlist->p_root_category )
244 i_return = p_playlist->p_root_category->i_children;
249 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
251 i_return = p_item->i_children;
253 pl_Release( VLCIntf );
255 return (i_return >= 0);
258 /* retrieve the string values for the cells */
259 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
262 playlist_item_t *p_item;
264 /* For error handling */
265 static BOOL attempted_reload = NO;
267 if( item == nil || ![item isKindOfClass: [NSValue class]] )
269 /* Attempt to fix the error by asking for a data redisplay
270 * This might cause infinite loop, so add a small check */
271 if( !attempted_reload )
273 attempted_reload = YES;
274 [outlineView reloadData];
279 p_item = (playlist_item_t *)[item pointerValue];
280 if( !p_item || !p_item->p_input )
282 /* Attempt to fix the error by asking for a data redisplay
283 * This might cause infinite loop, so add a small check */
284 if( !attempted_reload )
286 attempted_reload = YES;
287 [outlineView reloadData];
292 attempted_reload = NO;
294 if( [[o_tc identifier] isEqualToString:@"name"] )
296 /* sanity check to prevent the NSString class from crashing */
297 char *psz_title = input_item_GetTitleFbName( p_item->p_input );
300 o_value = [NSString stringWithUTF8String: psz_title];
304 else if( [[o_tc identifier] isEqualToString:@"artist"] )
306 char *psz_artist = input_item_GetArtist( p_item->p_input );
308 o_value = [NSString stringWithUTF8String: psz_artist];
311 else if( [[o_tc identifier] isEqualToString:@"duration"] )
313 char psz_duration[MSTRTIME_MAX_SIZE];
314 mtime_t dur = input_item_GetDuration( p_item->p_input );
317 secstotimestr( psz_duration, dur/1000000 );
318 o_value = [NSString stringWithUTF8String: psz_duration];
323 else if( [[o_tc identifier] isEqualToString:@"status"] )
325 if( input_item_HasErrorWhenReading( p_item->p_input ) )
327 o_value = [NSImage imageWithWarningIcon];
335 /*****************************************************************************
336 * VLCPlaylistWizard implementation
337 *****************************************************************************/
338 @implementation VLCPlaylistWizard
340 - (IBAction)reloadOutlineView
342 /* Only reload the outlineview if the wizard window is open since this can
343 be quite long on big playlists */
344 if( [[o_outline_view window] isVisible] )
346 [o_outline_view reloadData];
352 /*****************************************************************************
353 * extension to NSOutlineView's interface to fix compilation warnings
354 * and let us access these 2 functions properly
355 * this uses a private Apple-API, but works fine on all current OSX releases
356 * keep checking for compatiblity with future releases though
357 *****************************************************************************/
359 @interface NSOutlineView (UndocumentedSortImages)
360 + (NSImage *)_defaultTableHeaderSortImage;
361 + (NSImage *)_defaultTableHeaderReverseSortImage;
365 /*****************************************************************************
366 * VLCPlaylist implementation
367 *****************************************************************************/
368 @implementation VLCPlaylist
375 o_nodes_array = [[NSMutableArray alloc] init];
376 o_items_array = [[NSMutableArray alloc] init];
383 [o_nodes_array release];
384 [o_items_array release];
390 playlist_t * p_playlist = pl_Hold( VLCIntf );
394 [super awakeFromNib];
396 [o_outline_view setDoubleAction: @selector(playItem:)];
397 [o_outline_view_other setDoubleAction: @selector(playItem:)];
399 [o_outline_view registerForDraggedTypes:
400 [NSArray arrayWithObjects: NSFilenamesPboardType,
401 @"VLCPlaylistItemPboardType", nil]];
402 [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
404 [o_outline_view_other registerForDraggedTypes:
405 [NSArray arrayWithObjects: NSFilenamesPboardType,
406 @"VLCPlaylistItemPboardType", nil]];
407 [o_outline_view_other setIntercellSpacing: NSMakeSize (0.0, 1.0)];
409 /* This uses private Apple API which works fine until 10.5.
410 * We need to keep checking in the future!
411 * These methods are being added artificially to NSOutlineView's interface above */
412 o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
413 o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
415 o_tc_sortColumn = nil;
418 char ** ppsz_services = vlc_sd_GetNames( &ppsz_name );
421 pl_Release( VLCIntf );
425 for( i = 0; ppsz_services[i]; i++ )
430 char * name = ppsz_name[i] ? ppsz_name[i] : ppsz_services[i];
431 /* Check whether to enable these menuitems */
432 b_enabled = playlist_IsServicesDiscoveryLoaded( p_playlist, ppsz_services[i] );
434 /* Create the menu entries used in the playlist menu */
435 o_lmi = [[o_mi_services submenu] addItemWithTitle:
436 [NSString stringWithUTF8String: name]
437 action: @selector(servicesChange:)
439 [o_lmi setTarget: self];
440 [o_lmi setRepresentedObject: [NSString stringWithUTF8String: ppsz_services[i]]];
441 if( b_enabled ) [o_lmi setState: NSOnState];
443 /* Create the menu entries for the main menu */
444 o_lmi = [[o_mm_mi_services submenu] addItemWithTitle:
445 [NSString stringWithUTF8String: name]
446 action: @selector(servicesChange:)
448 [o_lmi setTarget: self];
449 [o_lmi setRepresentedObject: [NSString stringWithUTF8String: ppsz_services[i]]];
450 if( b_enabled ) [o_lmi setState: NSOnState];
452 free( ppsz_services[i] );
453 free( ppsz_name[i] );
455 free( ppsz_services );
458 pl_Release( VLCIntf );
461 - (void)searchfieldChanged:(NSNotification *)o_notification
463 [o_search_field setStringValue:[[o_notification object] stringValue]];
470 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
471 [o_mi_play setTitle: _NS("Play")];
472 [o_mi_delete setTitle: _NS("Delete")];
473 [o_mi_recursive_expand setTitle: _NS("Expand Node")];
474 [o_mi_selectall setTitle: _NS("Select All")];
475 [o_mi_info setTitle: _NS("Media Information...")];
476 [o_mi_dl_cover_art setTitle: _NS("Download Cover Art")];
477 [o_mi_preparse setTitle: _NS("Fetch Meta Data")];
478 [o_mi_revealInFinder setTitle: _NS("Reveal in Finder")];
479 [o_mm_mi_revealInFinder setTitle: _NS("Reveal in Finder")];
480 [[o_mm_mi_revealInFinder menu] setAutoenablesItems: NO];
481 [o_mi_sort_name setTitle: _NS("Sort Node by Name")];
482 [o_mi_sort_author setTitle: _NS("Sort Node by Author")];
483 [o_mi_services setTitle: _NS("Services discovery")];
484 [o_mm_mi_services setTitle: _NS("Services discovery")];
485 [o_status_field setStringValue: _NS("No items in the playlist")];
486 [o_status_field_embed setStringValue: _NS("No items in the playlist")];
488 [o_search_field setToolTip: _NS("Search in Playlist")];
489 [o_search_field_other setToolTip: _NS("Search in Playlist")];
490 [o_mi_addNode setTitle: _NS("Add Folder to Playlist")];
492 [o_save_accessory_text setStringValue: _NS("File Format:")];
493 [[o_save_accessory_popup itemAtIndex:0] setTitle: _NS("Extended M3U")];
494 [[o_save_accessory_popup itemAtIndex:1] setTitle: _NS("XML Shareable Playlist Format (XSPF)")];
495 [[o_save_accessory_popup itemAtIndex:2] setTitle: _NS("HTML Playlist")];
498 - (void)swapPlaylists:(id)newList
500 if(newList != o_outline_view)
502 id o_search_field_temp = o_search_field;
503 o_search_field = o_search_field_other;
504 o_search_field_other = o_search_field_temp;
505 [super swapPlaylists:newList];
506 [self playlistUpdated];
510 - (void)playlistUpdated
512 /* Clear indications of any existing column sorting */
513 for( unsigned int i = 0 ; i < [[o_outline_view tableColumns] count] ; i++ )
515 [o_outline_view setIndicatorImage:nil inTableColumn:
516 [[o_outline_view tableColumns] objectAtIndex:i]];
519 [o_outline_view setHighlightedTableColumn:nil];
520 o_tc_sortColumn = nil;
521 // TODO Find a way to keep the dict size to a minimum
522 //[o_outline_dict removeAllObjects];
523 [o_outline_view reloadData];
524 [o_sidebar updateSidebar:[self playingItem]];
525 [[[[VLCMain sharedInstance] wizard] playlistWizard] reloadOutlineView];
526 [[[[VLCMain sharedInstance] bookmarks] dataTable] reloadData];
528 playlist_t *p_playlist = pl_Hold( VLCIntf );
531 if( playlist_CurrentSize( p_playlist ) >= 2 )
533 [o_status_field setStringValue: [NSString stringWithFormat:
535 playlist_CurrentSize( p_playlist )]];
536 [o_status_field_embed setStringValue: [NSString stringWithFormat:
538 playlist_CurrentSize( p_playlist )]];
542 if( playlist_IsEmpty( p_playlist ) )
544 [o_status_field setStringValue: _NS("No items in the playlist")];
545 [o_status_field_embed setStringValue: _NS("No items in the playlist")];
549 [o_status_field setStringValue: _NS("1 item")];
550 [o_status_field_embed setStringValue: _NS("1 item")];
554 pl_Release( VLCIntf );
556 [self outlineViewSelectionDidChange: nil];
559 - (void)playModeUpdated
561 playlist_t *p_playlist = pl_Hold( VLCIntf );
563 bool loop = var_GetBool( p_playlist, "loop" );
564 bool repeat = var_GetBool( p_playlist, "repeat" );
566 [[[VLCMain sharedInstance] controls] repeatOne];
568 [[[VLCMain sharedInstance] controls] repeatAll];
570 [[[VLCMain sharedInstance] controls] repeatOff];
572 [[[VLCMain sharedInstance] controls] shuffle];
574 pl_Release( VLCIntf );
577 - (void)outlineViewSelectionDidChange:(NSNotification *)notification
580 playlist_item_t * p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
584 /* update the state of our Reveal-in-Finder menu items */
585 NSMutableString *o_mrl;
586 char *psz_uri = input_item_GetURI( p_item->p_input );
589 o_mrl = [NSMutableString stringWithUTF8String: psz_uri];
591 /* perform some checks whether it is a file and if it is local at all... */
592 NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
593 if( prefix_range.location != NSNotFound )
594 [o_mrl deleteCharactersInRange: prefix_range];
596 if( [o_mrl characterAtIndex:0] == '/' )
598 [o_mi_revealInFinder setEnabled: YES];
599 [o_mm_mi_revealInFinder setEnabled: YES];
603 [o_mi_revealInFinder setEnabled: NO];
604 [o_mm_mi_revealInFinder setEnabled: NO];
606 if( [[VLCMain sharedInstance] isPlaylistCollapsed] == NO )
608 /* update our info-panel to reflect the new item, if we aren't collapsed */
609 [[[VLCMain sharedInstance] info] updatePanelWithItem:p_item->p_input];
614 - (BOOL)isSelectionEmpty
616 return [o_outline_view selectedRow] == -1;
619 - (void)updateRowSelection
625 playlist_t *p_playlist = pl_Hold( VLCIntf );
626 playlist_item_t *p_item, *p_temp_item;
627 NSMutableArray *o_array = [NSMutableArray array];
630 p_item = playlist_CurrentPlayingItem( p_playlist );
634 pl_Release( VLCIntf );
638 p_temp_item = p_item;
639 while( p_temp_item->p_parent )
641 [o_array insertObject: [NSValue valueWithPointer: p_temp_item] atIndex: 0];
642 p_temp_item = p_temp_item->p_parent;
645 for( j = 0; j < [o_array count] - 1; j++ )
648 if( ( o_item = [o_outline_dict objectForKey:
649 [NSString stringWithFormat: @"%p",
650 [[o_array objectAtIndex:j] pointerValue]]] ) != nil )
652 [o_outline_view expandItem: o_item];
658 pl_Release( VLCIntf );
661 /* Check if p_item is a child of p_node recursively. We need to check the item
662 existence first since OSX sometimes tries to redraw items that have been
663 deleted. We don't do it when not required since this verification takes
664 quite a long time on big playlists (yes, pretty hacky). */
666 - (BOOL)isItem: (playlist_item_t *)p_item
667 inNode: (playlist_item_t *)p_node
668 checkItemExistence:(BOOL)b_check
669 locked:(BOOL)b_locked
672 playlist_t * p_playlist = pl_Hold( VLCIntf );
673 playlist_item_t *p_temp_item = p_item;
675 if( p_node == p_item )
677 pl_Release( VLCIntf );
681 if( p_node->i_children < 1)
683 pl_Release( VLCIntf );
690 if(!b_locked) PL_LOCK;
694 /* Since outlineView: willDisplayCell:... may call this function with
695 p_items that don't exist anymore, first check if the item is still
696 in the playlist. Any cleaner solution welcomed. */
697 for( i = 0; i < p_playlist->all_items.i_size; i++ )
699 if( ARRAY_VAL( p_playlist->all_items, i) == p_item ) break;
700 else if ( i == p_playlist->all_items.i_size - 1 )
702 if(!b_locked) PL_UNLOCK;
703 pl_Release( VLCIntf );
711 p_temp_item = p_temp_item->p_parent;
712 if( p_temp_item == p_node )
714 if(!b_locked) PL_UNLOCK;
715 pl_Release( VLCIntf );
719 if(!b_locked) PL_UNLOCK;
722 pl_Release( VLCIntf );
726 - (BOOL)isItem: (playlist_item_t *)p_item
727 inNode: (playlist_item_t *)p_node
728 checkItemExistence:(BOOL)b_check
730 [self isItem:p_item inNode:p_node checkItemExistence:b_check locked:NO];
733 /* This method is usefull for instance to remove the selected children of an
734 already selected node */
735 - (void)removeItemsFrom:(id)o_items ifChildrenOf:(id)o_nodes
738 for( i = 0 ; i < [o_items count] ; i++ )
740 for ( j = 0 ; j < [o_nodes count] ; j++ )
742 if( o_items == o_nodes)
744 if( j == i ) continue;
746 if( [self isItem: [[o_items objectAtIndex:i] pointerValue]
747 inNode: [[o_nodes objectAtIndex:j] pointerValue]
748 checkItemExistence: NO locked:NO] )
750 [o_items removeObjectAtIndex:i];
751 /* We need to execute the next iteration with the same index
752 since the current item has been deleted */
760 - (IBAction)savePlaylist:(id)sender
762 playlist_t * p_playlist = pl_Hold( VLCIntf );
764 NSSavePanel *o_save_panel = [NSSavePanel savePanel];
765 NSString * o_name = [NSString stringWithFormat: @"%@", _NS("Untitled")];
767 [o_save_panel setTitle: _NS("Save Playlist")];
768 [o_save_panel setPrompt: _NS("Save")];
769 [o_save_panel setAccessoryView: o_save_accessory_view];
771 if( [o_save_panel runModalForDirectory: nil
772 file: o_name] == NSOKButton )
774 NSString *o_filename = [o_save_panel filename];
776 if( [o_save_accessory_popup indexOfSelectedItem] == 0 )
778 NSString * o_real_filename;
780 range.location = [o_filename length] - [@".m3u" length];
781 range.length = [@".m3u" length];
783 if( [o_filename compare:@".m3u" options: NSCaseInsensitiveSearch
784 range: range] != NSOrderedSame )
786 o_real_filename = [NSString stringWithFormat: @"%@.m3u", o_filename];
790 o_real_filename = o_filename;
792 playlist_Export( p_playlist,
793 [o_real_filename fileSystemRepresentation],
794 p_playlist->p_local_category, "export-m3u" );
796 else if( [o_save_accessory_popup indexOfSelectedItem] == 1 )
798 NSString * o_real_filename;
800 range.location = [o_filename length] - [@".xspf" length];
801 range.length = [@".xspf" length];
803 if( [o_filename compare:@".xspf" options: NSCaseInsensitiveSearch
804 range: range] != NSOrderedSame )
806 o_real_filename = [NSString stringWithFormat: @"%@.xspf", o_filename];
810 o_real_filename = o_filename;
812 playlist_Export( p_playlist,
813 [o_real_filename fileSystemRepresentation],
814 p_playlist->p_local_category, "export-xspf" );
818 NSString * o_real_filename;
820 range.location = [o_filename length] - [@".html" length];
821 range.length = [@".html" length];
823 if( [o_filename compare:@".html" options: NSCaseInsensitiveSearch
824 range: range] != NSOrderedSame )
826 o_real_filename = [NSString stringWithFormat: @"%@.html", o_filename];
830 o_real_filename = o_filename;
832 playlist_Export( p_playlist,
833 [o_real_filename fileSystemRepresentation],
834 p_playlist->p_local_category, "export-html" );
837 pl_Release( VLCIntf );
840 /* When called retrieves the selected outlineview row and plays that node or item */
841 - (IBAction)playItem:(id)sender
843 intf_thread_t * p_intf = VLCIntf;
844 playlist_t * p_playlist = pl_Hold( p_intf );
846 playlist_item_t *p_item;
847 playlist_item_t *p_node = NULL;
849 p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
853 if( p_item->i_children == -1 )
855 p_node = p_item->p_parent;
861 if( p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1 )
863 p_item = p_node->pp_children[0];
870 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Unlocked, p_node, p_item );
872 pl_Release( p_intf );
875 - (void)playSidebarItem:(id)item
877 intf_thread_t * p_intf = VLCIntf;
878 playlist_t * p_playlist = pl_Hold( p_intf );
880 playlist_item_t *p_item;
881 playlist_item_t *p_node = NULL;
883 p_item = [item pointerValue];
887 if( p_item->i_children == -1 )
889 p_node = p_item->p_parent;
895 if( p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1 )
897 p_item = p_node->pp_children[0];
904 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Unlocked, p_node, p_item );
906 pl_Release( p_intf );
909 - (IBAction)revealItemInFinder:(id)sender
911 playlist_item_t * p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
912 NSMutableString * o_mrl = nil;
914 if(! p_item || !p_item->p_input )
917 char *psz_uri = input_item_GetURI( p_item->p_input );
919 o_mrl = [NSMutableString stringWithUTF8String: psz_uri];
921 /* perform some checks whether it is a file and if it is local at all... */
922 NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
923 if( prefix_range.location != NSNotFound )
924 [o_mrl deleteCharactersInRange: prefix_range];
926 if( [o_mrl characterAtIndex:0] == '/' )
927 [[NSWorkspace sharedWorkspace] selectFile: o_mrl inFileViewerRootedAtPath: o_mrl];
930 /* When called retrieves the selected outlineview row and plays that node or item */
931 - (IBAction)preparseItem:(id)sender
934 NSMutableArray *o_to_preparse;
935 intf_thread_t * p_intf = VLCIntf;
936 playlist_t * p_playlist = pl_Hold( p_intf );
938 o_to_preparse = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
939 i_count = [o_to_preparse count];
943 playlist_item_t *p_item = NULL;
945 for( i = 0; i < i_count; i++ )
947 o_number = [o_to_preparse lastObject];
948 i_row = [o_number intValue];
949 p_item = [[o_outline_view itemAtRow:i_row] pointerValue];
950 [o_to_preparse removeObject: o_number];
951 [o_outline_view deselectRow: i_row];
955 if( p_item->i_children == -1 )
957 playlist_PreparseEnqueue( p_playlist, p_item->p_input, pl_Unlocked );
961 msg_Dbg( p_intf, "preparsing nodes not implemented" );
965 pl_Release( p_intf );
966 [self playlistUpdated];
969 - (IBAction)downloadCoverArt:(id)sender
972 NSMutableArray *o_to_preparse;
973 intf_thread_t * p_intf = VLCIntf;
974 playlist_t * p_playlist = pl_Hold( p_intf );
976 o_to_preparse = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
977 i_count = [o_to_preparse count];
981 playlist_item_t *p_item = NULL;
983 for( i = 0; i < i_count; i++ )
985 o_number = [o_to_preparse lastObject];
986 i_row = [o_number intValue];
987 p_item = [[o_outline_view itemAtRow:i_row] pointerValue];
988 [o_to_preparse removeObject: o_number];
989 [o_outline_view deselectRow: i_row];
991 if( p_item && p_item->i_children == -1 )
993 playlist_AskForArtEnqueue( p_playlist, p_item->p_input, pl_Unlocked );
996 pl_Release( p_intf );
997 [self playlistUpdated];
1000 - (IBAction)servicesChange:(id)sender
1002 NSMenuItem *o_mi = (NSMenuItem *)sender;
1003 NSString *o_string = [o_mi representedObject];
1004 playlist_t * p_playlist = pl_Hold( VLCIntf );
1005 if( !playlist_IsServicesDiscoveryLoaded( p_playlist, [o_string UTF8String] ) )
1006 playlist_ServicesDiscoveryAdd( p_playlist, [o_string UTF8String] );
1008 playlist_ServicesDiscoveryRemove( p_playlist, [o_string UTF8String] );
1010 [o_mi setState: playlist_IsServicesDiscoveryLoaded( p_playlist,
1011 [o_string UTF8String] ) ? YES : NO];
1013 pl_Release( VLCIntf );
1014 [self playlistUpdated];
1018 - (IBAction)selectAll:(id)sender
1020 [o_outline_view selectAll: nil];
1023 - (IBAction)deleteItem:(id)sender
1026 NSMutableArray *o_to_delete;
1029 playlist_t * p_playlist;
1030 intf_thread_t * p_intf = VLCIntf;
1032 o_to_delete = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
1033 i_count = [o_to_delete count];
1035 p_playlist = pl_Hold( p_intf );
1038 for( int i = 0; i < i_count; i++ )
1040 o_number = [o_to_delete lastObject];
1041 i_row = [o_number intValue];
1042 id o_item = [o_outline_view itemAtRow: i_row];
1043 playlist_item_t *p_item = [o_item pointerValue];
1045 msg_Dbg( p_intf, "deleting item %i (of %i) with id \"%i\", pointerValue \"%p\" and %i children", i+1, i_count,
1046 p_item->p_input->i_id, [o_item pointerValue], p_item->i_children +1 );
1048 [o_to_delete removeObject: o_number];
1049 [o_outline_view deselectRow: i_row];
1051 if( p_item->i_children != -1 )
1052 //is a node and not an item
1054 if( playlist_Status( p_playlist ) != PLAYLIST_STOPPED &&
1055 [self isItem: playlist_CurrentPlayingItem( p_playlist ) inNode:
1056 ((playlist_item_t *)[o_item pointerValue])
1057 checkItemExistence: NO locked:YES] == YES )
1058 // if current item is in selected node and is playing then stop playlist
1059 playlist_Control(p_playlist, PLAYLIST_STOP, pl_Locked );
1061 playlist_NodeDelete( p_playlist, p_item, true, false );
1064 playlist_DeleteFromInput( p_playlist, p_item->p_input, pl_Locked );
1068 [self playlistUpdated];
1069 pl_Release( p_intf );
1072 - (IBAction)sortNodeByName:(id)sender
1074 [self sortNode: SORT_TITLE];
1077 - (IBAction)sortNodeByAuthor:(id)sender
1079 [self sortNode: SORT_ARTIST];
1082 - (void)sortNode:(int)i_mode
1084 playlist_t * p_playlist = pl_Hold( VLCIntf );
1085 playlist_item_t * p_item;
1087 if( [o_outline_view selectedRow] > -1 )
1089 p_item = [[o_outline_view itemAtRow: [o_outline_view selectedRow]] pointerValue];
1092 /*If no item is selected, sort the whole playlist*/
1094 p_item = p_playlist->p_root_category;
1097 if( p_item->i_children > -1 ) // the item is a node
1100 playlist_RecursiveNodeSort( p_playlist, p_item, i_mode, ORDER_NORMAL );
1106 playlist_RecursiveNodeSort( p_playlist,
1107 p_item->p_parent, i_mode, ORDER_NORMAL );
1110 pl_Release( VLCIntf );
1111 [self playlistUpdated];
1114 - (input_item_t *)createItem:(NSDictionary *)o_one_item
1116 intf_thread_t * p_intf = VLCIntf;
1117 playlist_t * p_playlist = pl_Hold( p_intf );
1119 input_item_t *p_input;
1121 BOOL b_rem = FALSE, b_dir = FALSE;
1122 NSString *o_uri, *o_name;
1127 o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
1128 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
1129 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
1131 /* Find the name for a disc entry (i know, can you believe the trouble?) */
1132 if( ( !o_name || [o_name isEqualToString:@""] ) && [o_uri rangeOfString: @"/dev/"].location != NSNotFound )
1134 int i_count, i_index;
1135 struct statfs *mounts = NULL;
1137 i_count = getmntinfo (&mounts, MNT_NOWAIT);
1138 /* getmntinfo returns a pointer to static data. Do not free. */
1139 for( i_index = 0 ; i_index < i_count; i_index++ )
1141 NSMutableString *o_temp, *o_temp2;
1142 o_temp = [NSMutableString stringWithString: o_uri];
1143 o_temp2 = [NSMutableString stringWithUTF8String: mounts[i_index].f_mntfromname];
1144 [o_temp replaceOccurrencesOfString: @"/dev/rdisk" withString: @"/dev/disk" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
1145 [o_temp2 replaceOccurrencesOfString: @"s0" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp2 length]) ];
1146 [o_temp2 replaceOccurrencesOfString: @"s1" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp2 length]) ];
1148 if( strstr( [o_temp fileSystemRepresentation], [o_temp2 fileSystemRepresentation] ) != NULL )
1150 o_name = [[NSFileManager defaultManager] displayNameAtPath: [NSString stringWithUTF8String:mounts[i_index].f_mntonname]];
1154 /* If no name, then make a guess */
1155 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
1157 if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
1158 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
1159 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
1161 /* All of this is to make sure CD's play when you D&D them on VLC */
1162 /* Converts mountpoint to a /dev file */
1165 NSMutableString *o_temp;
1167 buf = (struct statfs *) malloc (sizeof(struct statfs));
1168 statfs( [o_uri fileSystemRepresentation], buf );
1169 psz_dev = strdup(buf->f_mntfromname);
1170 o_temp = [NSMutableString stringWithUTF8String: psz_dev ];
1171 [o_temp replaceOccurrencesOfString: @"/dev/disk" withString: @"/dev/rdisk" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
1172 [o_temp replaceOccurrencesOfString: @"s0" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
1173 [o_temp replaceOccurrencesOfString: @"s1" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
1177 p_input = input_item_New( p_playlist, [o_uri fileSystemRepresentation], [o_name UTF8String] );
1180 pl_Release( p_intf );
1186 for( i = 0; i < (int)[o_options count]; i++ )
1188 input_item_AddOption( p_input, strdup( [[o_options objectAtIndex:i] UTF8String] ),
1189 VLC_INPUT_OPTION_TRUSTED );
1193 /* Recent documents menu */
1194 o_true_file = [NSURL fileURLWithPath: o_uri];
1195 if( o_true_file != nil && (BOOL)config_GetInt( p_playlist, "macosx-recentitems" ) == YES )
1197 [[NSDocumentController sharedDocumentController]
1198 noteNewRecentDocumentURL: o_true_file];
1201 pl_Release( p_intf );
1205 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
1208 playlist_t * p_playlist = pl_Hold( VLCIntf );
1211 for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
1213 input_item_t *p_input;
1214 NSDictionary *o_one_item;
1217 o_one_item = [o_array objectAtIndex: i_item];
1218 p_input = [self createItem: o_one_item];
1225 /* FIXME: playlist_AddInput() can fail */
1227 playlist_AddInput( p_playlist, p_input, PLAYLIST_INSERT,
1228 i_position == -1 ? PLAYLIST_END : i_position + i_item, true,
1231 if( i_item == 0 && !b_enqueue )
1233 playlist_item_t *p_item = NULL;
1234 playlist_item_t *p_node = NULL;
1235 p_item = playlist_ItemGetByInput( p_playlist, p_input );
1238 if( p_item->i_children == -1 )
1239 p_node = p_item->p_parent;
1243 if( p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1 )
1244 p_item = p_node->pp_children[0];
1248 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, p_node, p_item );
1251 vlc_gc_decref( p_input );
1255 [self playlistUpdated];
1256 pl_Release( VLCIntf );
1259 - (void)appendNodeArray:(NSArray*)o_array inNode:(playlist_item_t *)p_node atPos:(int)i_position enqueue:(BOOL)b_enqueue
1262 playlist_t * p_playlist = pl_Hold( VLCIntf );
1264 for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
1266 input_item_t *p_input;
1267 NSDictionary *o_one_item;
1270 o_one_item = [o_array objectAtIndex: i_item];
1271 p_input = [self createItem: o_one_item];
1273 if( !p_input ) continue;
1276 /* FIXME: playlist_BothAddInput() can fail */
1278 playlist_BothAddInput( p_playlist, p_input, p_node,
1281 PLAYLIST_END : i_position + i_item,
1282 NULL, NULL, pl_Locked );
1285 if( i_item == 0 && !b_enqueue )
1287 playlist_item_t *p_item;
1288 p_item = playlist_ItemGetByInput( p_playlist, p_input );
1289 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, p_node, p_item );
1292 vlc_gc_decref( p_input );
1294 [self playlistUpdated];
1295 pl_Release( VLCIntf );
1298 - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
1300 playlist_t *p_playlist = pl_Hold( VLCIntf );
1301 playlist_item_t *p_selected_item;
1302 int i_current, i_selected_row;
1304 i_selected_row = [o_outline_view selectedRow];
1305 if (i_selected_row < 0)
1308 p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow:
1309 i_selected_row] pointerValue];
1311 for( i_current = 0; i_current < p_item->i_children ; i_current++ )
1314 NSString *o_current_name, *o_current_author;
1317 o_current_name = [NSString stringWithUTF8String:
1318 p_item->pp_children[i_current]->p_input->psz_name];
1319 psz_temp = input_item_GetInfo( p_item->p_input ,
1320 _("Meta-information"),_("Artist") );
1321 o_current_author = [NSString stringWithUTF8String: psz_temp];
1325 if( p_selected_item == p_item->pp_children[i_current] &&
1326 b_selected_item_met == NO )
1328 b_selected_item_met = YES;
1330 else if( p_selected_item == p_item->pp_children[i_current] &&
1331 b_selected_item_met == YES )
1333 pl_Release( VLCIntf );
1336 else if( b_selected_item_met == YES &&
1337 ( [o_current_name rangeOfString:[o_search_field
1338 stringValue] options:NSCaseInsensitiveSearch].length ||
1339 [o_current_author rangeOfString:[o_search_field
1340 stringValue] options:NSCaseInsensitiveSearch].length ) )
1342 pl_Release( VLCIntf );
1343 /*Adds the parent items in the result array as well, so that we can
1345 return [NSMutableArray arrayWithObject: [NSValue
1346 valueWithPointer: p_item->pp_children[i_current]]];
1348 if( p_item->pp_children[i_current]->i_children > 0 )
1350 id o_result = [self subSearchItem:
1351 p_item->pp_children[i_current]];
1352 if( o_result != NULL )
1354 pl_Release( VLCIntf );
1355 [o_result insertObject: [NSValue valueWithPointer:
1356 p_item->pp_children[i_current]] atIndex:0];
1361 pl_Release( VLCIntf );
1365 - (IBAction)searchItem:(id)sender
1367 playlist_t * p_playlist = pl_Hold( VLCIntf );
1373 b_selected_item_met = NO;
1375 /*First, only search after the selected item:*
1376 *(b_selected_item_met = NO) */
1377 o_result = [self subSearchItem:p_playlist->p_root_category];
1378 if( o_result == NULL )
1380 /* If the first search failed, search again from the beginning */
1381 o_result = [self subSearchItem:p_playlist->p_root_category];
1383 if( o_result != NULL )
1386 if( [[o_result objectAtIndex: 0] pointerValue] ==
1387 p_playlist->p_local_category )
1392 for( i = i_start ; i < [o_result count] - 1 ; i++ )
1394 [o_outline_view expandItem: [o_outline_dict objectForKey:
1395 [NSString stringWithFormat: @"%p",
1396 [[o_result objectAtIndex: i] pointerValue]]]];
1398 i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
1399 [NSString stringWithFormat: @"%p",
1400 [[o_result objectAtIndex: [o_result count] - 1 ]
1405 [o_outline_view selectRow:i_row byExtendingSelection: NO];
1406 [o_outline_view scrollRowToVisible: i_row];
1408 pl_Release( VLCIntf );
1411 - (IBAction)recursiveExpandNode:(id)sender
1413 id o_item = [o_outline_view itemAtRow: [o_outline_view selectedRow]];
1414 playlist_item_t *p_item = (playlist_item_t *)[o_item pointerValue];
1416 if( ![[o_outline_view dataSource] outlineView: o_outline_view
1417 isItemExpandable: o_item] )
1419 o_item = [o_outline_dict objectForKey: [NSString
1420 stringWithFormat: @"%p", p_item->p_parent]];
1423 /* We need to collapse the node first, since OSX refuses to recursively
1424 expand an already expanded node, even if children nodes are collapsed. */
1425 [o_outline_view collapseItem: o_item collapseChildren: YES];
1426 [o_outline_view expandItem: o_item expandChildren: YES];
1429 - (NSMenu *)menuForEvent:(NSEvent *)o_event
1435 pt = [o_outline_view convertPoint: [o_event locationInWindow]
1437 int row = [o_outline_view rowAtPoint:pt];
1439 [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
1441 b_item_sel = ( row != -1 && [o_outline_view selectedRow] != -1 );
1442 b_rows = [o_outline_view numberOfRows] != 0;
1444 [o_mi_play setEnabled: b_item_sel];
1445 [o_mi_delete setEnabled: b_item_sel];
1446 [o_mi_selectall setEnabled: b_rows];
1447 [o_mi_info setEnabled: b_item_sel];
1448 [o_mi_preparse setEnabled: b_item_sel];
1449 [o_mi_recursive_expand setEnabled: b_item_sel];
1450 [o_mi_sort_name setEnabled: b_item_sel];
1451 [o_mi_sort_author setEnabled: b_item_sel];
1453 return( o_ctx_menu );
1456 - (void)outlineView: (NSOutlineView *)o_tv
1457 didClickTableColumn:(NSTableColumn *)o_tc
1459 int i_mode, i_type = 0;
1460 intf_thread_t *p_intf = VLCIntf;
1462 playlist_t *p_playlist = pl_Hold( p_intf );
1464 /* Check whether the selected table column header corresponds to a
1465 sortable table column*/
1466 if( !( o_tc == o_tc_name || o_tc == o_tc_author ) )
1468 pl_Release( p_intf );
1472 if( o_tc_sortColumn == o_tc )
1474 b_isSortDescending = !b_isSortDescending;
1478 b_isSortDescending = false;
1481 if( o_tc == o_tc_name )
1483 i_mode = SORT_TITLE;
1485 else if( o_tc == o_tc_author )
1487 i_mode = SORT_ARTIST;
1490 if( b_isSortDescending )
1492 i_type = ORDER_REVERSE;
1496 i_type = ORDER_NORMAL;
1500 playlist_RecursiveNodeSort( p_playlist, p_playlist->p_root_category, i_mode, i_type );
1503 pl_Release( p_intf );
1504 [self playlistUpdated];
1506 o_tc_sortColumn = o_tc;
1507 [o_outline_view setHighlightedTableColumn:o_tc];
1509 if( b_isSortDescending )
1511 [o_outline_view setIndicatorImage:o_descendingSortingImage
1512 inTableColumn:o_tc];
1516 [o_outline_view setIndicatorImage:o_ascendingSortingImage
1517 inTableColumn:o_tc];
1522 - (void)outlineView:(NSOutlineView *)outlineView
1523 willDisplayCell:(id)cell
1524 forTableColumn:(NSTableColumn *)tableColumn
1527 playlist_t *p_playlist = pl_Hold( VLCIntf );
1532 o_playing_item = [o_outline_dict objectForKey:
1533 [NSString stringWithFormat:@"%p", playlist_CurrentPlayingItem( p_playlist )]];
1536 if( [self isItem: [o_playing_item pointerValue] inNode:
1537 [item pointerValue] checkItemExistence: YES]
1538 || [o_playing_item isEqual: item] )
1540 [cell setFont: [[NSFontManager sharedFontManager] convertFont:[cell font] toHaveTrait:NSBoldFontMask]];
1544 [cell setFont: [[NSFontManager sharedFontManager] convertFont:[cell font] toNotHaveTrait:NSBoldFontMask]];
1546 pl_Release( VLCIntf );
1551 playlist_t *p_playlist = pl_Hold( VLCIntf );
1556 o_playing_item = [o_outline_dict objectForKey:
1557 [NSString stringWithFormat:@"%p", playlist_CurrentPlayingItem( p_playlist )]];
1560 pl_Release( VLCIntf );
1562 return o_playing_item;
1565 - (IBAction)addNode:(id)sender
1567 playlist_t * p_playlist = pl_Hold( VLCIntf );
1568 vlc_thread_set_priority( p_playlist, VLC_THREAD_PRIORITY_LOW );
1571 playlist_NodeCreate( p_playlist, _("Empty Folder"),
1572 p_playlist->p_local_category, 0, NULL );
1575 pl_Release( VLCIntf );
1577 [self playlistUpdated];
1581 @implementation VLCPlaylist (NSOutlineViewDataSource)
1583 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
1585 id o_value = [super outlineView: outlineView child: index ofItem: item];
1586 playlist_t *p_playlist = pl_Hold( VLCIntf );
1589 if( playlist_CurrentSize( p_playlist ) >= 2 )
1591 [o_status_field setStringValue: [NSString stringWithFormat:
1593 playlist_CurrentSize( p_playlist )]];
1594 [o_status_field_embed setStringValue: [NSString stringWithFormat:
1596 playlist_CurrentSize( p_playlist )]];
1600 if( playlist_IsEmpty( p_playlist ) )
1602 [o_status_field setStringValue: _NS("No items in the playlist")];
1603 [o_status_field_embed setStringValue: _NS("No items in the playlist")];
1607 [o_status_field setStringValue: _NS("1 item")];
1608 [o_status_field_embed setStringValue: _NS("1 item")];
1613 pl_Release( VLCIntf );
1615 [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p",
1616 [o_value pointerValue]]];
1621 /* Required for drag & drop and reordering */
1622 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
1625 playlist_t *p_playlist = pl_Hold( VLCIntf );
1627 /* First remove the items that were moved during the last drag & drop
1629 [o_items_array removeAllObjects];
1630 [o_nodes_array removeAllObjects];
1632 for( i = 0 ; i < [items count] ; i++ )
1634 id o_item = [items objectAtIndex: i];
1636 /* Refuse to move items that are not in the General Node
1637 (Service Discovery) */
1638 if( ![self isItem: [o_item pointerValue] inNode:
1639 p_playlist->p_local_category checkItemExistence: NO] &&
1640 var_CreateGetBool( p_playlist, "media-library" ) &&
1641 ![self isItem: [o_item pointerValue] inNode:
1642 p_playlist->p_ml_category checkItemExistence: NO] ||
1643 [o_item pointerValue] == p_playlist->p_local_category ||
1644 [o_item pointerValue] == p_playlist->p_ml_category )
1646 pl_Release( VLCIntf );
1649 /* Fill the items and nodes to move in 2 different arrays */
1650 if( ((playlist_item_t *)[o_item pointerValue])->i_children > 0 )
1651 [o_nodes_array addObject: o_item];
1653 [o_items_array addObject: o_item];
1656 /* Now we need to check if there are selected items that are in already
1657 selected nodes. In that case, we only want to move the nodes */
1658 [self removeItemsFrom: o_nodes_array ifChildrenOf: o_nodes_array];
1659 [self removeItemsFrom: o_items_array ifChildrenOf: o_nodes_array];
1661 /* We add the "VLCPlaylistItemPboardType" type to be able to recognize
1662 a Drop operation coming from the playlist. */
1664 [pboard declareTypes: [NSArray arrayWithObjects:
1665 @"VLCPlaylistItemPboardType", nil] owner: self];
1666 [pboard setData:[NSData data] forType:@"VLCPlaylistItemPboardType"];
1668 pl_Release( VLCIntf );
1672 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)index
1674 playlist_t *p_playlist = pl_Hold( VLCIntf );
1675 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1677 if( !p_playlist ) return NSDragOperationNone;
1679 /* Dropping ON items is not allowed if item is not a node */
1682 if( index == NSOutlineViewDropOnItemIndex &&
1683 ((playlist_item_t *)[item pointerValue])->i_children == -1 )
1685 pl_Release( VLCIntf );
1686 return NSDragOperationNone;
1690 /* Don't allow on drop on playlist root element's child */
1691 if( !item && index != NSOutlineViewDropOnItemIndex)
1693 pl_Release( VLCIntf );
1694 return NSDragOperationNone;
1697 /* We refuse to drop an item in anything else than a child of the General
1698 Node. We still accept items that would be root nodes of the outlineview
1699 however, to allow drop in an empty playlist. */
1700 if( !( ([self isItem: [item pointerValue] inNode: p_playlist->p_local_category checkItemExistence: NO] ||
1701 ( var_CreateGetBool( p_playlist, "media-library" ) && [self isItem: [item pointerValue] inNode: p_playlist->p_ml_category checkItemExistence: NO] ) ) || item == nil ) )
1703 pl_Release( VLCIntf );
1704 return NSDragOperationNone;
1707 /* Drop from the Playlist */
1708 if( [[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] )
1711 for( i = 0 ; i < [o_nodes_array count] ; i++ )
1713 /* We refuse to Drop in a child of an item we are moving */
1714 if( [self isItem: [item pointerValue] inNode:
1715 [[o_nodes_array objectAtIndex: i] pointerValue]
1716 checkItemExistence: NO] )
1718 pl_Release( VLCIntf );
1719 return NSDragOperationNone;
1722 pl_Release( VLCIntf );
1723 return NSDragOperationMove;
1726 /* Drop from the Finder */
1727 else if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1729 pl_Release( VLCIntf );
1730 return NSDragOperationGeneric;
1732 pl_Release( VLCIntf );
1733 return NSDragOperationNone;
1736 - (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(NSInteger)index
1738 playlist_t * p_playlist = pl_Hold( VLCIntf );
1739 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1741 /* Drag & Drop inside the playlist */
1742 if( [[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] )
1744 int i_row, i_removed_from_node = 0;
1746 playlist_item_t *p_new_parent, *p_item = NULL;
1747 NSArray *o_all_items = [o_nodes_array arrayByAddingObjectsFromArray:
1749 /* If the item is to be dropped as root item of the outline, make it a
1750 child of the General node.
1751 Else, choose the proposed parent as parent. */
1752 if( item == nil ) p_new_parent = p_playlist->p_local_category;
1753 else p_new_parent = [item pointerValue];
1755 /* Make sure the proposed parent is a node.
1756 (This should never be true) */
1757 if( p_new_parent->i_children < 0 )
1759 pl_Release( VLCIntf );
1763 for( i = 0; i < [o_all_items count]; i++ )
1765 playlist_item_t *p_old_parent = NULL;
1766 int i_old_index = 0;
1768 p_item = [[o_all_items objectAtIndex:i] pointerValue];
1769 p_old_parent = p_item->p_parent;
1772 /* We may need the old index later */
1773 if( p_new_parent == p_old_parent )
1776 for( j = 0; j < p_old_parent->i_children; j++ )
1778 if( p_old_parent->pp_children[j] == p_item )
1787 // Actually detach the item from the old position
1788 if( playlist_NodeRemoveItem( p_playlist, p_item, p_old_parent ) ==
1792 /* Calculate the new index */
1795 /* If we move the item in the same node, we need to take into
1796 account that one item will be deleted */
1799 if ((p_new_parent == p_old_parent &&
1800 i_old_index < index + (int)i) )
1802 i_removed_from_node++;
1804 i_new_index = index + i - i_removed_from_node;
1806 // Reattach the item to the new position
1807 playlist_NodeInsert( p_playlist, p_item, p_new_parent, i_new_index );
1811 [self playlistUpdated];
1812 i_row = [o_outline_view rowForItem:[o_outline_dict
1813 objectForKey:[NSString stringWithFormat: @"%p",
1814 [[o_all_items objectAtIndex: 0] pointerValue]]]];
1818 i_row = [o_outline_view rowForItem:[o_outline_dict
1819 objectForKey:[NSString stringWithFormat: @"%p", p_new_parent]]];
1822 [o_outline_view deselectAll: self];
1823 [o_outline_view selectRow: i_row byExtendingSelection: NO];
1824 [o_outline_view scrollRowToVisible: i_row];
1826 pl_Release( VLCIntf );
1830 else if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1833 playlist_item_t *p_node = [item pointerValue];
1835 NSArray *o_array = [NSArray array];
1836 NSArray *o_values = [[o_pasteboard propertyListForType:
1837 NSFilenamesPboardType]
1838 sortedArrayUsingSelector:
1839 @selector(caseInsensitiveCompare:)];
1841 for( i = 0; i < (int)[o_values count]; i++)
1843 NSDictionary *o_dic;
1844 o_dic = [NSDictionary dictionaryWithObject:[o_values
1845 objectAtIndex:i] forKey:@"ITEM_URL"];
1846 o_array = [o_array arrayByAddingObject: o_dic];
1851 [self appendArray:o_array atPos:index enqueue: YES];
1855 assert( p_node->i_children != -1 );
1856 [self appendNodeArray:o_array inNode: p_node
1857 atPos:index enqueue:YES];
1859 pl_Release( VLCIntf );
1862 pl_Release( VLCIntf );