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 );
243 if( p_playlist->p_root_category )
245 i_return = p_playlist->p_root_category->i_children;
250 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
252 i_return = p_item->i_children;
255 pl_Release( VLCIntf );
257 return (i_return >= 0);
260 /* retrieve the string values for the cells */
261 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
264 playlist_item_t *p_item;
266 /* For error handling */
267 static BOOL attempted_reload = NO;
269 if( item == nil || ![item isKindOfClass: [NSValue class]] )
271 /* Attempt to fix the error by asking for a data redisplay
272 * This might cause infinite loop, so add a small check */
273 if( !attempted_reload )
275 attempted_reload = YES;
276 [outlineView reloadData];
281 p_item = (playlist_item_t *)[item pointerValue];
282 if( !p_item || !p_item->p_input )
284 /* Attempt to fix the error by asking for a data redisplay
285 * This might cause infinite loop, so add a small check */
286 if( !attempted_reload )
288 attempted_reload = YES;
289 [outlineView reloadData];
294 attempted_reload = NO;
296 if( [[o_tc identifier] isEqualToString:@"name"] )
298 /* sanity check to prevent the NSString class from crashing */
299 char *psz_title = input_item_GetTitleFbName( p_item->p_input );
302 o_value = [NSString stringWithUTF8String: psz_title];
306 else if( [[o_tc identifier] isEqualToString:@"artist"] )
308 char *psz_artist = input_item_GetArtist( p_item->p_input );
310 o_value = [NSString stringWithUTF8String: psz_artist];
313 else if( [[o_tc identifier] isEqualToString:@"duration"] )
315 char psz_duration[MSTRTIME_MAX_SIZE];
316 mtime_t dur = input_item_GetDuration( p_item->p_input );
319 secstotimestr( psz_duration, dur/1000000 );
320 o_value = [NSString stringWithUTF8String: psz_duration];
325 else if( [[o_tc identifier] isEqualToString:@"status"] )
327 if( input_item_HasErrorWhenReading( p_item->p_input ) )
329 o_value = [NSImage imageWithWarningIcon];
337 /*****************************************************************************
338 * VLCPlaylistWizard implementation
339 *****************************************************************************/
340 @implementation VLCPlaylistWizard
342 - (IBAction)reloadOutlineView
344 /* Only reload the outlineview if the wizard window is open since this can
345 be quite long on big playlists */
346 if( [[o_outline_view window] isVisible] )
348 [o_outline_view reloadData];
354 /*****************************************************************************
355 * extension to NSOutlineView's interface to fix compilation warnings
356 * and let us access these 2 functions properly
357 * this uses a private Apple-API, but works fine on all current OSX releases
358 * keep checking for compatiblity with future releases though
359 *****************************************************************************/
361 @interface NSOutlineView (UndocumentedSortImages)
362 + (NSImage *)_defaultTableHeaderSortImage;
363 + (NSImage *)_defaultTableHeaderReverseSortImage;
367 /*****************************************************************************
368 * VLCPlaylist implementation
369 *****************************************************************************/
370 @implementation VLCPlaylist
377 o_nodes_array = [[NSMutableArray alloc] init];
378 o_items_array = [[NSMutableArray alloc] init];
385 [o_nodes_array release];
386 [o_items_array release];
392 playlist_t * p_playlist = pl_Hold( VLCIntf );
396 [super awakeFromNib];
398 [o_outline_view setDoubleAction: @selector(playItem:)];
399 [o_outline_view_other setDoubleAction: @selector(playItem:)];
401 [o_outline_view registerForDraggedTypes:
402 [NSArray arrayWithObjects: NSFilenamesPboardType,
403 @"VLCPlaylistItemPboardType", nil]];
404 [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
406 [o_outline_view_other registerForDraggedTypes:
407 [NSArray arrayWithObjects: NSFilenamesPboardType,
408 @"VLCPlaylistItemPboardType", nil]];
409 [o_outline_view_other setIntercellSpacing: NSMakeSize (0.0, 1.0)];
411 /* This uses private Apple API which works fine until 10.5.
412 * We need to keep checking in the future!
413 * These methods are being added artificially to NSOutlineView's interface above */
414 o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
415 o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
417 o_tc_sortColumn = nil;
420 char ** ppsz_services = vlc_sd_GetNames( &ppsz_name );
423 pl_Release( VLCIntf );
427 for( i = 0; ppsz_services[i]; i++ )
432 char * name = ppsz_name[i] ? ppsz_name[i] : ppsz_services[i];
433 /* Check whether to enable these menuitems */
434 b_enabled = playlist_IsServicesDiscoveryLoaded( p_playlist, ppsz_services[i] );
436 /* Create the menu entries used in the playlist menu */
437 o_lmi = [[o_mi_services submenu] addItemWithTitle:
438 [NSString stringWithUTF8String: name]
439 action: @selector(servicesChange:)
441 [o_lmi setTarget: self];
442 [o_lmi setRepresentedObject: [NSString stringWithUTF8String: ppsz_services[i]]];
443 if( b_enabled ) [o_lmi setState: NSOnState];
445 /* Create the menu entries for the main menu */
446 o_lmi = [[o_mm_mi_services submenu] addItemWithTitle:
447 [NSString stringWithUTF8String: name]
448 action: @selector(servicesChange:)
450 [o_lmi setTarget: self];
451 [o_lmi setRepresentedObject: [NSString stringWithUTF8String: ppsz_services[i]]];
452 if( b_enabled ) [o_lmi setState: NSOnState];
454 free( ppsz_services[i] );
455 free( ppsz_name[i] );
457 free( ppsz_services );
460 pl_Release( VLCIntf );
463 - (void)searchfieldChanged:(NSNotification *)o_notification
465 [o_search_field setStringValue:[[o_notification object] stringValue]];
472 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
473 [o_mi_play setTitle: _NS("Play")];
474 [o_mi_delete setTitle: _NS("Delete")];
475 [o_mi_recursive_expand setTitle: _NS("Expand Node")];
476 [o_mi_selectall setTitle: _NS("Select All")];
477 [o_mi_info setTitle: _NS("Media Information...")];
478 [o_mi_dl_cover_art setTitle: _NS("Download Cover Art")];
479 [o_mi_preparse setTitle: _NS("Fetch Meta Data")];
480 [o_mi_revealInFinder setTitle: _NS("Reveal in Finder")];
481 [o_mm_mi_revealInFinder setTitle: _NS("Reveal in Finder")];
482 [[o_mm_mi_revealInFinder menu] setAutoenablesItems: NO];
483 [o_mi_sort_name setTitle: _NS("Sort Node by Name")];
484 [o_mi_sort_author setTitle: _NS("Sort Node by Author")];
485 [o_mi_services setTitle: _NS("Services discovery")];
486 [o_mm_mi_services setTitle: _NS("Services discovery")];
487 [o_status_field setStringValue: _NS("No items in the playlist")];
488 [o_status_field_embed setStringValue: _NS("No items in the playlist")];
490 [o_search_field setToolTip: _NS("Search in Playlist")];
491 [o_search_field_other setToolTip: _NS("Search in Playlist")];
492 [o_mi_addNode setTitle: _NS("Add Folder to Playlist")];
494 [o_save_accessory_text setStringValue: _NS("File Format:")];
495 [[o_save_accessory_popup itemAtIndex:0] setTitle: _NS("Extended M3U")];
496 [[o_save_accessory_popup itemAtIndex:1] setTitle: _NS("XML Shareable Playlist Format (XSPF)")];
497 [[o_save_accessory_popup itemAtIndex:2] setTitle: _NS("HTML Playlist")];
500 - (void)swapPlaylists:(id)newList
502 if(newList != o_outline_view)
504 id o_search_field_temp = o_search_field;
505 o_search_field = o_search_field_other;
506 o_search_field_other = o_search_field_temp;
507 [super swapPlaylists:newList];
508 [self playlistUpdated];
512 - (void)playlistUpdated
514 /* Clear indications of any existing column sorting */
515 for( unsigned int i = 0 ; i < [[o_outline_view tableColumns] count] ; i++ )
517 [o_outline_view setIndicatorImage:nil inTableColumn:
518 [[o_outline_view tableColumns] objectAtIndex:i]];
521 [o_outline_view setHighlightedTableColumn:nil];
522 o_tc_sortColumn = nil;
523 // TODO Find a way to keep the dict size to a minimum
524 //[o_outline_dict removeAllObjects];
525 [o_outline_view reloadData];
526 [o_sidebar updateSidebar:[self playingItem]];
527 [[[[VLCMain sharedInstance] wizard] playlistWizard] reloadOutlineView];
528 [[[[VLCMain sharedInstance] bookmarks] dataTable] reloadData];
530 playlist_t *p_playlist = pl_Hold( VLCIntf );
533 if( playlist_CurrentSize( p_playlist ) >= 2 )
535 [o_status_field setStringValue: [NSString stringWithFormat:
537 playlist_CurrentSize( p_playlist )]];
538 [o_status_field_embed setStringValue: [NSString stringWithFormat:
540 playlist_CurrentSize( p_playlist )]];
544 if( playlist_IsEmpty( p_playlist ) )
546 [o_status_field setStringValue: _NS("No items in the playlist")];
547 [o_status_field_embed setStringValue: _NS("No items in the playlist")];
551 [o_status_field setStringValue: _NS("1 item")];
552 [o_status_field_embed setStringValue: _NS("1 item")];
556 pl_Release( VLCIntf );
558 [self outlineViewSelectionDidChange: nil];
561 - (void)playModeUpdated
563 playlist_t *p_playlist = pl_Hold( VLCIntf );
565 bool loop = var_GetBool( p_playlist, "loop" );
566 bool repeat = var_GetBool( p_playlist, "repeat" );
568 [[[VLCMain sharedInstance] controls] repeatOne];
570 [[[VLCMain sharedInstance] controls] repeatAll];
572 [[[VLCMain sharedInstance] controls] repeatOff];
574 [[[VLCMain sharedInstance] controls] shuffle];
576 pl_Release( VLCIntf );
579 - (void)outlineViewSelectionDidChange:(NSNotification *)notification
582 playlist_item_t * p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
586 /* update the state of our Reveal-in-Finder menu items */
587 NSMutableString *o_mrl;
588 char *psz_uri = input_item_GetURI( p_item->p_input );
590 [o_mi_revealInFinder setEnabled: NO];
591 [o_mm_mi_revealInFinder setEnabled: NO];
594 o_mrl = [NSMutableString stringWithUTF8String: psz_uri];
596 /* perform some checks whether it is a file and if it is local at all... */
597 NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
598 if( prefix_range.location != NSNotFound )
599 [o_mrl deleteCharactersInRange: prefix_range];
601 if( [o_mrl characterAtIndex:0] == '/' )
603 [o_mi_revealInFinder setEnabled: YES];
604 [o_mm_mi_revealInFinder setEnabled: YES];
609 if( [[VLCMain sharedInstance] isPlaylistCollapsed] == NO )
611 /* update our info-panel to reflect the new item, if we aren't collapsed */
612 [[[VLCMain sharedInstance] info] updatePanelWithItem:p_item->p_input];
617 - (BOOL)isSelectionEmpty
619 return [o_outline_view selectedRow] == -1;
622 - (void)updateRowSelection
628 playlist_t *p_playlist = pl_Hold( VLCIntf );
629 playlist_item_t *p_item, *p_temp_item;
630 NSMutableArray *o_array = [NSMutableArray array];
633 p_item = playlist_CurrentPlayingItem( p_playlist );
637 pl_Release( VLCIntf );
641 p_temp_item = p_item;
642 while( p_temp_item->p_parent )
644 [o_array insertObject: [NSValue valueWithPointer: p_temp_item] atIndex: 0];
645 p_temp_item = p_temp_item->p_parent;
649 for( j = 0; j < [o_array count] - 1; j++ )
652 if( ( o_item = [o_outline_dict objectForKey:
653 [NSString stringWithFormat: @"%p",
654 [[o_array objectAtIndex:j] pointerValue]]] ) != nil )
656 [o_outline_view expandItem: o_item];
660 pl_Release( VLCIntf );
663 /* Check if p_item is a child of p_node recursively. We need to check the item
664 existence first since OSX sometimes tries to redraw items that have been
665 deleted. We don't do it when not required since this verification takes
666 quite a long time on big playlists (yes, pretty hacky). */
668 - (BOOL)isItem: (playlist_item_t *)p_item
669 inNode: (playlist_item_t *)p_node
670 checkItemExistence:(BOOL)b_check
671 locked:(BOOL)b_locked
674 playlist_t * p_playlist = pl_Hold( VLCIntf );
675 playlist_item_t *p_temp_item = p_item;
677 if( p_node == p_item )
679 pl_Release( VLCIntf );
683 if( p_node->i_children < 1)
685 pl_Release( VLCIntf );
692 if(!b_locked) PL_LOCK;
696 /* Since outlineView: willDisplayCell:... may call this function with
697 p_items that don't exist anymore, first check if the item is still
698 in the playlist. Any cleaner solution welcomed. */
699 for( i = 0; i < p_playlist->all_items.i_size; i++ )
701 if( ARRAY_VAL( p_playlist->all_items, i) == p_item ) break;
702 else if ( i == p_playlist->all_items.i_size - 1 )
704 if(!b_locked) PL_UNLOCK;
705 pl_Release( VLCIntf );
713 p_temp_item = p_temp_item->p_parent;
714 if( p_temp_item == p_node )
716 if(!b_locked) PL_UNLOCK;
717 pl_Release( VLCIntf );
721 if(!b_locked) PL_UNLOCK;
724 pl_Release( VLCIntf );
728 - (BOOL)isItem: (playlist_item_t *)p_item
729 inNode: (playlist_item_t *)p_node
730 checkItemExistence:(BOOL)b_check
732 [self isItem:p_item inNode:p_node checkItemExistence:b_check locked:NO];
735 /* This method is usefull for instance to remove the selected children of an
736 already selected node */
737 - (void)removeItemsFrom:(id)o_items ifChildrenOf:(id)o_nodes
740 for( i = 0 ; i < [o_items count] ; i++ )
742 for ( j = 0 ; j < [o_nodes count] ; j++ )
744 if( o_items == o_nodes)
746 if( j == i ) continue;
748 if( [self isItem: [[o_items objectAtIndex:i] pointerValue]
749 inNode: [[o_nodes objectAtIndex:j] pointerValue]
750 checkItemExistence: NO locked:NO] )
752 [o_items removeObjectAtIndex:i];
753 /* We need to execute the next iteration with the same index
754 since the current item has been deleted */
762 - (IBAction)savePlaylist:(id)sender
764 playlist_t * p_playlist = pl_Hold( VLCIntf );
766 NSSavePanel *o_save_panel = [NSSavePanel savePanel];
767 NSString * o_name = [NSString stringWithFormat: @"%@", _NS("Untitled")];
769 [o_save_panel setTitle: _NS("Save Playlist")];
770 [o_save_panel setPrompt: _NS("Save")];
771 [o_save_panel setAccessoryView: o_save_accessory_view];
773 if( [o_save_panel runModalForDirectory: nil
774 file: o_name] == NSOKButton )
776 NSString *o_filename = [o_save_panel filename];
778 if( [o_save_accessory_popup indexOfSelectedItem] == 0 )
780 NSString * o_real_filename;
782 range.location = [o_filename length] - [@".m3u" length];
783 range.length = [@".m3u" length];
785 if( [o_filename compare:@".m3u" options: NSCaseInsensitiveSearch
786 range: range] != NSOrderedSame )
788 o_real_filename = [NSString stringWithFormat: @"%@.m3u", o_filename];
792 o_real_filename = o_filename;
794 playlist_Export( p_playlist,
795 [o_real_filename fileSystemRepresentation],
796 p_playlist->p_local_category, "export-m3u" );
798 else if( [o_save_accessory_popup indexOfSelectedItem] == 1 )
800 NSString * o_real_filename;
802 range.location = [o_filename length] - [@".xspf" length];
803 range.length = [@".xspf" length];
805 if( [o_filename compare:@".xspf" options: NSCaseInsensitiveSearch
806 range: range] != NSOrderedSame )
808 o_real_filename = [NSString stringWithFormat: @"%@.xspf", o_filename];
812 o_real_filename = o_filename;
814 playlist_Export( p_playlist,
815 [o_real_filename fileSystemRepresentation],
816 p_playlist->p_local_category, "export-xspf" );
820 NSString * o_real_filename;
822 range.location = [o_filename length] - [@".html" length];
823 range.length = [@".html" length];
825 if( [o_filename compare:@".html" options: NSCaseInsensitiveSearch
826 range: range] != NSOrderedSame )
828 o_real_filename = [NSString stringWithFormat: @"%@.html", o_filename];
832 o_real_filename = o_filename;
834 playlist_Export( p_playlist,
835 [o_real_filename fileSystemRepresentation],
836 p_playlist->p_local_category, "export-html" );
839 pl_Release( VLCIntf );
842 /* When called retrieves the selected outlineview row and plays that node or item */
843 - (IBAction)playItem:(id)sender
845 intf_thread_t * p_intf = VLCIntf;
846 playlist_t * p_playlist = pl_Hold( p_intf );
848 playlist_item_t *p_item;
849 playlist_item_t *p_node = NULL;
851 p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
856 if( p_item->i_children == -1 )
858 p_node = p_item->p_parent;
863 if( p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1 )
865 p_item = p_node->pp_children[0];
872 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, p_node, p_item );
875 pl_Release( p_intf );
878 - (void)playSidebarItem:(id)item
880 intf_thread_t * p_intf = VLCIntf;
881 playlist_t * p_playlist = pl_Hold( p_intf );
883 playlist_item_t *p_item;
884 playlist_item_t *p_node = NULL;
886 p_item = [item pointerValue];
890 if( p_item->i_children == -1 )
892 p_node = p_item->p_parent;
898 if( p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1 )
900 p_item = p_node->pp_children[0];
907 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Unlocked, p_node, p_item );
909 pl_Release( p_intf );
912 - (IBAction)revealItemInFinder:(id)sender
914 playlist_item_t * p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
915 NSMutableString * o_mrl = nil;
917 if(! p_item || !p_item->p_input )
920 char *psz_uri = input_item_GetURI( p_item->p_input );
922 o_mrl = [NSMutableString stringWithUTF8String: psz_uri];
924 /* perform some checks whether it is a file and if it is local at all... */
925 NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
926 if( prefix_range.location != NSNotFound )
927 [o_mrl deleteCharactersInRange: prefix_range];
929 if( [o_mrl characterAtIndex:0] == '/' )
930 [[NSWorkspace sharedWorkspace] selectFile: o_mrl inFileViewerRootedAtPath: o_mrl];
933 /* When called retrieves the selected outlineview row and plays that node or item */
934 - (IBAction)preparseItem:(id)sender
937 NSMutableArray *o_to_preparse;
938 intf_thread_t * p_intf = VLCIntf;
939 playlist_t * p_playlist = pl_Hold( p_intf );
941 o_to_preparse = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
942 i_count = [o_to_preparse count];
946 playlist_item_t *p_item = NULL;
948 for( i = 0; i < i_count; i++ )
950 o_number = [o_to_preparse lastObject];
951 i_row = [o_number intValue];
952 p_item = [[o_outline_view itemAtRow:i_row] pointerValue];
953 [o_to_preparse removeObject: o_number];
954 [o_outline_view deselectRow: i_row];
958 if( p_item->i_children == -1 )
960 playlist_PreparseEnqueue( p_playlist, p_item->p_input, pl_Unlocked );
964 msg_Dbg( p_intf, "preparsing nodes not implemented" );
968 pl_Release( p_intf );
969 [self playlistUpdated];
972 - (IBAction)downloadCoverArt:(id)sender
975 NSMutableArray *o_to_preparse;
976 intf_thread_t * p_intf = VLCIntf;
977 playlist_t * p_playlist = pl_Hold( p_intf );
979 o_to_preparse = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
980 i_count = [o_to_preparse count];
984 playlist_item_t *p_item = NULL;
986 for( i = 0; i < i_count; i++ )
988 o_number = [o_to_preparse lastObject];
989 i_row = [o_number intValue];
990 p_item = [[o_outline_view itemAtRow:i_row] pointerValue];
991 [o_to_preparse removeObject: o_number];
992 [o_outline_view deselectRow: i_row];
994 if( p_item && p_item->i_children == -1 )
996 playlist_AskForArtEnqueue( p_playlist, p_item->p_input, pl_Unlocked );
999 pl_Release( p_intf );
1000 [self playlistUpdated];
1003 - (IBAction)servicesChange:(id)sender
1005 NSMenuItem *o_mi = (NSMenuItem *)sender;
1006 NSString *o_string = [o_mi representedObject];
1007 playlist_t * p_playlist = pl_Hold( VLCIntf );
1008 if( !playlist_IsServicesDiscoveryLoaded( p_playlist, [o_string UTF8String] ) )
1009 playlist_ServicesDiscoveryAdd( p_playlist, [o_string UTF8String] );
1011 playlist_ServicesDiscoveryRemove( p_playlist, [o_string UTF8String] );
1013 [o_mi setState: playlist_IsServicesDiscoveryLoaded( p_playlist,
1014 [o_string UTF8String] ) ? YES : NO];
1016 pl_Release( VLCIntf );
1017 [self playlistUpdated];
1021 - (IBAction)selectAll:(id)sender
1023 [o_outline_view selectAll: nil];
1026 - (IBAction)deleteItem:(id)sender
1029 NSMutableArray *o_to_delete;
1032 playlist_t * p_playlist;
1033 intf_thread_t * p_intf = VLCIntf;
1035 o_to_delete = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
1036 i_count = [o_to_delete count];
1038 p_playlist = pl_Hold( p_intf );
1040 for( int i = 0; i < i_count; i++ )
1042 o_number = [o_to_delete lastObject];
1043 i_row = [o_number intValue];
1044 id o_item = [o_outline_view itemAtRow: i_row];
1045 [o_outline_view deselectRow: i_row];
1048 playlist_item_t *p_item = [o_item pointerValue];
1050 msg_Dbg( p_intf, "deleting item %i (of %i) with id \"%i\", pointerValue \"%p\" and %i children", i+1, i_count,
1051 p_item->p_input->i_id, [o_item pointerValue], p_item->i_children +1 );
1053 [o_to_delete removeObject: o_number];
1055 if( p_item->i_children != -1 )
1056 //is a node and not an item
1058 if( playlist_Status( p_playlist ) != PLAYLIST_STOPPED &&
1059 [self isItem: playlist_CurrentPlayingItem( p_playlist ) inNode:
1060 ((playlist_item_t *)[o_item pointerValue])
1061 checkItemExistence: NO locked:YES] == YES )
1062 // if current item is in selected node and is playing then stop playlist
1063 playlist_Control(p_playlist, PLAYLIST_STOP, pl_Locked );
1065 playlist_NodeDelete( p_playlist, p_item, true, false );
1068 playlist_DeleteFromInput( p_playlist, p_item->p_input, pl_Locked );
1071 [o_outline_dict removeObjectForKey:[NSString stringWithFormat:@"%p",
1072 [o_item pointerValue]]];
1076 [self playlistUpdated];
1077 pl_Release( p_intf );
1080 - (IBAction)sortNodeByName:(id)sender
1082 [self sortNode: SORT_TITLE];
1085 - (IBAction)sortNodeByAuthor:(id)sender
1087 [self sortNode: SORT_ARTIST];
1090 - (void)sortNode:(int)i_mode
1092 playlist_t * p_playlist = pl_Hold( VLCIntf );
1093 playlist_item_t * p_item;
1095 if( [o_outline_view selectedRow] > -1 )
1097 p_item = [[o_outline_view itemAtRow: [o_outline_view selectedRow]] pointerValue];
1100 /*If no item is selected, sort the whole playlist*/
1102 p_item = p_playlist->p_root_category;
1106 if( p_item->i_children > -1 ) // the item is a node
1108 playlist_RecursiveNodeSort( p_playlist, p_item, i_mode, ORDER_NORMAL );
1112 playlist_RecursiveNodeSort( p_playlist,
1113 p_item->p_parent, i_mode, ORDER_NORMAL );
1116 pl_Release( VLCIntf );
1117 [self playlistUpdated];
1120 - (input_item_t *)createItem:(NSDictionary *)o_one_item
1122 intf_thread_t * p_intf = VLCIntf;
1123 playlist_t * p_playlist = pl_Hold( p_intf );
1125 input_item_t *p_input;
1127 BOOL b_rem = FALSE, b_dir = FALSE;
1128 NSString *o_uri, *o_name;
1133 o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
1134 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
1135 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
1137 /* Find the name for a disc entry (i know, can you believe the trouble?) */
1138 if( ( !o_name || [o_name isEqualToString:@""] ) && [o_uri rangeOfString: @"/dev/"].location != NSNotFound )
1140 int i_count, i_index;
1141 struct statfs *mounts = NULL;
1143 i_count = getmntinfo (&mounts, MNT_NOWAIT);
1144 /* getmntinfo returns a pointer to static data. Do not free. */
1145 for( i_index = 0 ; i_index < i_count; i_index++ )
1147 NSMutableString *o_temp, *o_temp2;
1148 o_temp = [NSMutableString stringWithString: o_uri];
1149 o_temp2 = [NSMutableString stringWithUTF8String: mounts[i_index].f_mntfromname];
1150 [o_temp replaceOccurrencesOfString: @"/dev/rdisk" withString: @"/dev/disk" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
1151 [o_temp2 replaceOccurrencesOfString: @"s0" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp2 length]) ];
1152 [o_temp2 replaceOccurrencesOfString: @"s1" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp2 length]) ];
1154 if( strstr( [o_temp fileSystemRepresentation], [o_temp2 fileSystemRepresentation] ) != NULL )
1156 o_name = [[NSFileManager defaultManager] displayNameAtPath: [NSString stringWithUTF8String:mounts[i_index].f_mntonname]];
1161 if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
1162 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
1163 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
1165 /* All of this is to make sure CD's play when you D&D them on VLC */
1166 /* Converts mountpoint to a /dev file */
1169 NSMutableString *o_temp;
1171 buf = (struct statfs *) malloc (sizeof(struct statfs));
1172 statfs( [o_uri fileSystemRepresentation], buf );
1173 psz_dev = strdup(buf->f_mntfromname);
1174 o_temp = [NSMutableString stringWithUTF8String: psz_dev ];
1175 [o_temp replaceOccurrencesOfString: @"/dev/disk" withString: @"/dev/rdisk" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
1176 [o_temp replaceOccurrencesOfString: @"s0" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
1177 [o_temp replaceOccurrencesOfString: @"s1" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
1181 p_input = input_item_New( p_playlist, [o_uri fileSystemRepresentation], o_name ? [o_name UTF8String] : NULL );
1184 pl_Release( p_intf );
1190 for( i = 0; i < (int)[o_options count]; i++ )
1192 input_item_AddOption( p_input, [[o_options objectAtIndex:i] UTF8String],
1193 VLC_INPUT_OPTION_TRUSTED );
1197 /* Recent documents menu */
1198 o_true_file = [NSURL fileURLWithPath: o_uri];
1199 if( o_true_file != nil && (BOOL)config_GetInt( p_playlist, "macosx-recentitems" ) == YES )
1201 [[NSDocumentController sharedDocumentController]
1202 noteNewRecentDocumentURL: o_true_file];
1205 pl_Release( p_intf );
1209 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
1212 playlist_t * p_playlist = pl_Hold( VLCIntf );
1215 for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
1217 input_item_t *p_input;
1218 NSDictionary *o_one_item;
1221 o_one_item = [o_array objectAtIndex: i_item];
1222 p_input = [self createItem: o_one_item];
1229 /* FIXME: playlist_AddInput() can fail */
1231 playlist_AddInput( p_playlist, p_input, PLAYLIST_INSERT,
1232 i_position == -1 ? PLAYLIST_END : i_position + i_item, true,
1235 if( i_item == 0 && !b_enqueue )
1237 playlist_item_t *p_item = NULL;
1238 playlist_item_t *p_node = NULL;
1239 p_item = playlist_ItemGetByInput( p_playlist, p_input );
1242 if( p_item->i_children == -1 )
1243 p_node = p_item->p_parent;
1247 if( p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1 )
1248 p_item = p_node->pp_children[0];
1252 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, p_node, p_item );
1255 vlc_gc_decref( p_input );
1259 [self playlistUpdated];
1260 pl_Release( VLCIntf );
1263 - (void)appendNodeArray:(NSArray*)o_array inNode:(playlist_item_t *)p_node atPos:(int)i_position enqueue:(BOOL)b_enqueue
1266 playlist_t * p_playlist = pl_Hold( VLCIntf );
1268 for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
1270 input_item_t *p_input;
1271 NSDictionary *o_one_item;
1274 o_one_item = [o_array objectAtIndex: i_item];
1275 p_input = [self createItem: o_one_item];
1277 if( !p_input ) continue;
1280 /* FIXME: playlist_BothAddInput() can fail */
1282 playlist_BothAddInput( p_playlist, p_input, p_node,
1285 PLAYLIST_END : i_position + i_item,
1286 NULL, NULL, pl_Locked );
1289 if( i_item == 0 && !b_enqueue )
1291 playlist_item_t *p_item;
1292 p_item = playlist_ItemGetByInput( p_playlist, p_input );
1293 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, p_node, p_item );
1296 vlc_gc_decref( p_input );
1298 [self playlistUpdated];
1299 pl_Release( VLCIntf );
1302 - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
1304 playlist_t *p_playlist = pl_Hold( VLCIntf );
1305 playlist_item_t *p_selected_item;
1306 int i_current, i_selected_row;
1308 i_selected_row = [o_outline_view selectedRow];
1309 if (i_selected_row < 0)
1312 p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow:
1313 i_selected_row] pointerValue];
1315 for( i_current = 0; i_current < p_item->i_children ; i_current++ )
1318 NSString *o_current_name, *o_current_author;
1321 o_current_name = [NSString stringWithUTF8String:
1322 p_item->pp_children[i_current]->p_input->psz_name];
1323 psz_temp = input_item_GetInfo( p_item->p_input ,
1324 _("Meta-information"),_("Artist") );
1325 o_current_author = [NSString stringWithUTF8String: psz_temp];
1329 if( p_selected_item == p_item->pp_children[i_current] &&
1330 b_selected_item_met == NO )
1332 b_selected_item_met = YES;
1334 else if( p_selected_item == p_item->pp_children[i_current] &&
1335 b_selected_item_met == YES )
1337 pl_Release( VLCIntf );
1340 else if( b_selected_item_met == YES &&
1341 ( [o_current_name rangeOfString:[o_search_field
1342 stringValue] options:NSCaseInsensitiveSearch].length ||
1343 [o_current_author rangeOfString:[o_search_field
1344 stringValue] options:NSCaseInsensitiveSearch].length ) )
1346 pl_Release( VLCIntf );
1347 /*Adds the parent items in the result array as well, so that we can
1349 return [NSMutableArray arrayWithObject: [NSValue
1350 valueWithPointer: p_item->pp_children[i_current]]];
1352 if( p_item->pp_children[i_current]->i_children > 0 )
1354 id o_result = [self subSearchItem:
1355 p_item->pp_children[i_current]];
1356 if( o_result != NULL )
1358 pl_Release( VLCIntf );
1359 [o_result insertObject: [NSValue valueWithPointer:
1360 p_item->pp_children[i_current]] atIndex:0];
1365 pl_Release( VLCIntf );
1369 - (IBAction)searchItem:(id)sender
1371 playlist_t * p_playlist = pl_Hold( VLCIntf );
1377 b_selected_item_met = NO;
1379 /*First, only search after the selected item:*
1380 *(b_selected_item_met = NO) */
1381 o_result = [self subSearchItem:p_playlist->p_root_category];
1382 if( o_result == NULL )
1384 /* If the first search failed, search again from the beginning */
1385 o_result = [self subSearchItem:p_playlist->p_root_category];
1387 if( o_result != NULL )
1390 if( [[o_result objectAtIndex: 0] pointerValue] ==
1391 p_playlist->p_local_category )
1396 for( i = i_start ; i < [o_result count] - 1 ; i++ )
1398 [o_outline_view expandItem: [o_outline_dict objectForKey:
1399 [NSString stringWithFormat: @"%p",
1400 [[o_result objectAtIndex: i] pointerValue]]]];
1402 i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
1403 [NSString stringWithFormat: @"%p",
1404 [[o_result objectAtIndex: [o_result count] - 1 ]
1409 [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:i_row] byExtendingSelection:NO];
1410 [o_outline_view scrollRowToVisible: i_row];
1412 pl_Release( VLCIntf );
1415 - (IBAction)recursiveExpandNode:(id)sender
1417 id o_item = [o_outline_view itemAtRow: [o_outline_view selectedRow]];
1418 playlist_item_t *p_item = (playlist_item_t *)[o_item pointerValue];
1420 if( ![[o_outline_view dataSource] outlineView: o_outline_view
1421 isItemExpandable: o_item] )
1423 o_item = [o_outline_dict objectForKey: [NSString
1424 stringWithFormat: @"%p", p_item->p_parent]];
1427 /* We need to collapse the node first, since OSX refuses to recursively
1428 expand an already expanded node, even if children nodes are collapsed. */
1429 [o_outline_view collapseItem: o_item collapseChildren: YES];
1430 [o_outline_view expandItem: o_item expandChildren: YES];
1433 - (NSMenu *)menuForEvent:(NSEvent *)o_event
1439 pt = [o_outline_view convertPoint: [o_event locationInWindow]
1441 int row = [o_outline_view rowAtPoint:pt];
1443 [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
1445 b_item_sel = ( row != -1 && [o_outline_view selectedRow] != -1 );
1446 b_rows = [o_outline_view numberOfRows] != 0;
1448 [o_mi_play setEnabled: b_item_sel];
1449 [o_mi_delete setEnabled: b_item_sel];
1450 [o_mi_selectall setEnabled: b_rows];
1451 [o_mi_info setEnabled: b_item_sel];
1452 [o_mi_preparse setEnabled: b_item_sel];
1453 [o_mi_recursive_expand setEnabled: b_item_sel];
1454 [o_mi_sort_name setEnabled: b_item_sel];
1455 [o_mi_sort_author setEnabled: b_item_sel];
1457 return( o_ctx_menu );
1460 - (void)outlineView: (NSOutlineView *)o_tv
1461 didClickTableColumn:(NSTableColumn *)o_tc
1463 int i_mode, i_type = 0;
1464 intf_thread_t *p_intf = VLCIntf;
1466 playlist_t *p_playlist = pl_Hold( p_intf );
1468 /* Check whether the selected table column header corresponds to a
1469 sortable table column*/
1470 if( !( o_tc == o_tc_name || o_tc == o_tc_author ) )
1472 pl_Release( p_intf );
1476 if( o_tc_sortColumn == o_tc )
1478 b_isSortDescending = !b_isSortDescending;
1482 b_isSortDescending = false;
1485 if( o_tc == o_tc_name )
1487 i_mode = SORT_TITLE;
1489 else if( o_tc == o_tc_author )
1491 i_mode = SORT_ARTIST;
1494 if( b_isSortDescending )
1496 i_type = ORDER_REVERSE;
1500 i_type = ORDER_NORMAL;
1504 playlist_RecursiveNodeSort( p_playlist, p_playlist->p_root_category, i_mode, i_type );
1507 pl_Release( p_intf );
1508 [self playlistUpdated];
1510 o_tc_sortColumn = o_tc;
1511 [o_outline_view setHighlightedTableColumn:o_tc];
1513 if( b_isSortDescending )
1515 [o_outline_view setIndicatorImage:o_descendingSortingImage
1516 inTableColumn:o_tc];
1520 [o_outline_view setIndicatorImage:o_ascendingSortingImage
1521 inTableColumn:o_tc];
1526 - (void)outlineView:(NSOutlineView *)outlineView
1527 willDisplayCell:(id)cell
1528 forTableColumn:(NSTableColumn *)tableColumn
1531 playlist_t *p_playlist = pl_Hold( VLCIntf );
1536 o_playing_item = [o_outline_dict objectForKey:
1537 [NSString stringWithFormat:@"%p", playlist_CurrentPlayingItem( p_playlist )]];
1540 if( [self isItem: [o_playing_item pointerValue] inNode:
1541 [item pointerValue] checkItemExistence: YES]
1542 || [o_playing_item isEqual: item] )
1544 [cell setFont: [[NSFontManager sharedFontManager] convertFont:[cell font] toHaveTrait:NSBoldFontMask]];
1548 [cell setFont: [[NSFontManager sharedFontManager] convertFont:[cell font] toNotHaveTrait:NSBoldFontMask]];
1550 pl_Release( VLCIntf );
1555 playlist_t *p_playlist = pl_Hold( VLCIntf );
1560 o_playing_item = [o_outline_dict objectForKey:
1561 [NSString stringWithFormat:@"%p", playlist_CurrentPlayingItem( p_playlist )]];
1564 pl_Release( VLCIntf );
1566 return o_playing_item;
1569 - (IBAction)addNode:(id)sender
1571 playlist_t * p_playlist = pl_Hold( VLCIntf );
1572 vlc_thread_set_priority( p_playlist, VLC_THREAD_PRIORITY_LOW );
1575 playlist_NodeCreate( p_playlist, _("Empty Folder"),
1576 p_playlist->p_local_category, 0, NULL );
1579 pl_Release( VLCIntf );
1581 [self playlistUpdated];
1585 @implementation VLCPlaylist (NSOutlineViewDataSource)
1587 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
1589 id o_value = [super outlineView: outlineView child: index ofItem: item];
1590 playlist_t *p_playlist = pl_Hold( VLCIntf );
1593 if( playlist_CurrentSize( p_playlist ) >= 2 )
1595 [o_status_field setStringValue: [NSString stringWithFormat:
1597 playlist_CurrentSize( p_playlist )]];
1598 [o_status_field_embed setStringValue: [NSString stringWithFormat:
1600 playlist_CurrentSize( p_playlist )]];
1604 if( playlist_IsEmpty( p_playlist ) )
1606 [o_status_field setStringValue: _NS("No items in the playlist")];
1607 [o_status_field_embed setStringValue: _NS("No items in the playlist")];
1611 [o_status_field setStringValue: _NS("1 item")];
1612 [o_status_field_embed setStringValue: _NS("1 item")];
1617 pl_Release( VLCIntf );
1619 [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p",
1620 [o_value pointerValue]]];
1625 /* Required for drag & drop and reordering */
1626 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
1629 playlist_t *p_playlist = pl_Hold( VLCIntf );
1631 /* First remove the items that were moved during the last drag & drop
1633 [o_items_array removeAllObjects];
1634 [o_nodes_array removeAllObjects];
1636 for( i = 0 ; i < [items count] ; i++ )
1638 id o_item = [items objectAtIndex: i];
1640 /* Refuse to move items that are not in the General Node
1641 (Service Discovery) */
1642 if( ![self isItem: [o_item pointerValue] inNode:
1643 p_playlist->p_local_category checkItemExistence: NO] &&
1644 var_CreateGetBool( p_playlist, "media-library" ) &&
1645 ![self isItem: [o_item pointerValue] inNode:
1646 p_playlist->p_ml_category checkItemExistence: NO] ||
1647 [o_item pointerValue] == p_playlist->p_local_category ||
1648 [o_item pointerValue] == p_playlist->p_ml_category )
1650 pl_Release( VLCIntf );
1653 /* Fill the items and nodes to move in 2 different arrays */
1654 if( ((playlist_item_t *)[o_item pointerValue])->i_children > 0 )
1655 [o_nodes_array addObject: o_item];
1657 [o_items_array addObject: o_item];
1660 /* Now we need to check if there are selected items that are in already
1661 selected nodes. In that case, we only want to move the nodes */
1662 [self removeItemsFrom: o_nodes_array ifChildrenOf: o_nodes_array];
1663 [self removeItemsFrom: o_items_array ifChildrenOf: o_nodes_array];
1665 /* We add the "VLCPlaylistItemPboardType" type to be able to recognize
1666 a Drop operation coming from the playlist. */
1668 [pboard declareTypes: [NSArray arrayWithObjects:
1669 @"VLCPlaylistItemPboardType", nil] owner: self];
1670 [pboard setData:[NSData data] forType:@"VLCPlaylistItemPboardType"];
1672 pl_Release( VLCIntf );
1676 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)index
1678 playlist_t *p_playlist = pl_Hold( VLCIntf );
1679 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1681 if( !p_playlist ) return NSDragOperationNone;
1683 /* Dropping ON items is not allowed if item is not a node */
1686 if( index == NSOutlineViewDropOnItemIndex &&
1687 ((playlist_item_t *)[item pointerValue])->i_children == -1 )
1689 pl_Release( VLCIntf );
1690 return NSDragOperationNone;
1694 /* Don't allow on drop on playlist root element's child */
1695 if( !item && index != NSOutlineViewDropOnItemIndex)
1697 pl_Release( VLCIntf );
1698 return NSDragOperationNone;
1701 /* We refuse to drop an item in anything else than a child of the General
1702 Node. We still accept items that would be root nodes of the outlineview
1703 however, to allow drop in an empty playlist. */
1704 if( !( ([self isItem: [item pointerValue] inNode: p_playlist->p_local_category checkItemExistence: NO] ||
1705 ( var_CreateGetBool( p_playlist, "media-library" ) && [self isItem: [item pointerValue] inNode: p_playlist->p_ml_category checkItemExistence: NO] ) ) || item == nil ) )
1707 pl_Release( VLCIntf );
1708 return NSDragOperationNone;
1711 /* Drop from the Playlist */
1712 if( [[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] )
1715 for( i = 0 ; i < [o_nodes_array count] ; i++ )
1717 /* We refuse to Drop in a child of an item we are moving */
1718 if( [self isItem: [item pointerValue] inNode:
1719 [[o_nodes_array objectAtIndex: i] pointerValue]
1720 checkItemExistence: NO] )
1722 pl_Release( VLCIntf );
1723 return NSDragOperationNone;
1726 pl_Release( VLCIntf );
1727 return NSDragOperationMove;
1730 /* Drop from the Finder */
1731 else if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1733 pl_Release( VLCIntf );
1734 return NSDragOperationGeneric;
1736 pl_Release( VLCIntf );
1737 return NSDragOperationNone;
1740 - (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(NSInteger)index
1742 playlist_t * p_playlist = pl_Hold( VLCIntf );
1743 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1745 /* Drag & Drop inside the playlist */
1746 if( [[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] )
1748 int i_row, i_removed_from_node = 0;
1750 playlist_item_t *p_new_parent, *p_item = NULL;
1751 NSArray *o_all_items = [o_nodes_array arrayByAddingObjectsFromArray:
1753 /* If the item is to be dropped as root item of the outline, make it a
1754 child of the General node.
1755 Else, choose the proposed parent as parent. */
1756 if( item == nil ) p_new_parent = p_playlist->p_local_category;
1757 else p_new_parent = [item pointerValue];
1759 /* Make sure the proposed parent is a node.
1760 (This should never be true) */
1761 if( p_new_parent->i_children < 0 )
1763 pl_Release( VLCIntf );
1767 for( i = 0; i < [o_all_items count]; i++ )
1769 playlist_item_t *p_old_parent = NULL;
1770 int i_old_index = 0;
1772 p_item = [[o_all_items objectAtIndex:i] pointerValue];
1773 p_old_parent = p_item->p_parent;
1776 /* We may need the old index later */
1777 if( p_new_parent == p_old_parent )
1780 for( j = 0; j < p_old_parent->i_children; j++ )
1782 if( p_old_parent->pp_children[j] == p_item )
1791 // Actually detach the item from the old position
1792 if( playlist_NodeRemoveItem( p_playlist, p_item, p_old_parent ) ==
1796 /* Calculate the new index */
1799 /* If we move the item in the same node, we need to take into
1800 account that one item will be deleted */
1803 if ((p_new_parent == p_old_parent &&
1804 i_old_index < index + (int)i) )
1806 i_removed_from_node++;
1808 i_new_index = index + i - i_removed_from_node;
1810 // Reattach the item to the new position
1811 playlist_NodeInsert( p_playlist, p_item, p_new_parent, i_new_index );
1815 [self playlistUpdated];
1816 i_row = [o_outline_view rowForItem:[o_outline_dict
1817 objectForKey:[NSString stringWithFormat: @"%p",
1818 [[o_all_items objectAtIndex: 0] pointerValue]]]];
1822 i_row = [o_outline_view rowForItem:[o_outline_dict
1823 objectForKey:[NSString stringWithFormat: @"%p", p_new_parent]]];
1826 [o_outline_view deselectAll: self];
1827 [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:i_row] byExtendingSelection:NO];
1828 [o_outline_view scrollRowToVisible: i_row];
1830 pl_Release( VLCIntf );
1834 else if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1837 playlist_item_t *p_node = [item pointerValue];
1839 NSArray *o_array = [NSArray array];
1840 NSArray *o_values = [[o_pasteboard propertyListForType:
1841 NSFilenamesPboardType]
1842 sortedArrayUsingSelector:
1843 @selector(caseInsensitiveCompare:)];
1845 for( i = 0; i < (int)[o_values count]; i++)
1847 NSDictionary *o_dic;
1848 o_dic = [NSDictionary dictionaryWithObject:[o_values
1849 objectAtIndex:i] forKey:@"ITEM_URL"];
1850 o_array = [o_array arrayByAddingObject: o_dic];
1855 [self appendArray:o_array atPos:index enqueue: YES];
1859 assert( p_node->i_children != -1 );
1860 [self appendNodeArray:o_array inNode: p_node
1861 atPos:index enqueue:YES];
1863 pl_Release( VLCIntf );
1866 pl_Release( VLCIntf );