1 /*****************************************************************************
2 * playlist.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2002-2008 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 <vlc_interface.h>
54 /*****************************************************************************
55 * VLCPlaylistView implementation
56 *****************************************************************************/
57 @implementation VLCPlaylistView
59 - (NSMenu *)menuForEvent:(NSEvent *)o_event
61 return( [[self delegate] menuForEvent: o_event] );
64 - (void)keyDown:(NSEvent *)o_event
68 if( [[o_event characters] length] )
70 key = [[o_event characters] characterAtIndex: 0];
75 case NSDeleteCharacter:
76 case NSDeleteFunctionKey:
77 case NSDeleteCharFunctionKey:
78 case NSBackspaceCharacter:
79 [[self delegate] deleteItem:self];
82 case NSEnterCharacter:
83 case NSCarriageReturnCharacter:
84 [(VLCPlaylist *)[[VLCMain sharedInstance] getPlaylist] playItem:self];
88 [super keyDown: o_event];
95 /*****************************************************************************
96 * VLCPlaylistCommon implementation
98 * This class the superclass of the VLCPlaylist and VLCPlaylistWizard.
99 * It contains the common methods and elements of these 2 entities.
100 *****************************************************************************/
101 @implementation VLCPlaylistCommon
108 o_outline_dict = [[NSMutableDictionary alloc] init];
114 playlist_t * p_playlist = pl_Yield( VLCIntf );
115 [o_outline_view setTarget: self];
116 [o_outline_view setDelegate: self];
117 [o_outline_view setDataSource: self];
118 [o_outline_view setAllowsEmptySelection: NO];
120 vlc_object_release( p_playlist );
126 [[o_tc_name headerCell] setStringValue:_NS("Name")];
127 [[o_tc_author headerCell] setStringValue:_NS("Author")];
128 [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
131 - (NSOutlineView *)outlineView
133 return o_outline_view;
136 - (playlist_item_t *)selectedPlaylistItem
138 return [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
144 @implementation VLCPlaylistCommon (NSOutlineViewDataSource)
146 /* return the number of children for Obj-C pointer item */ /* DONE */
147 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
150 playlist_item_t *p_item = NULL;
151 playlist_t * p_playlist = pl_Yield( VLCIntf );
152 assert( outlineView == o_outline_view );
155 p_item = p_playlist->p_root_category;
157 p_item = (playlist_item_t *)[item pointerValue];
160 i_return = p_item->i_children;
162 pl_Release( VLCIntf );
164 return i_return > 0 ? i_return : 0;
167 /* return the child at index for the Obj-C pointer item */ /* DONE */
168 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
170 playlist_item_t *p_return = NULL, *p_item = NULL;
172 playlist_t * p_playlist = pl_Yield( VLCIntf );
177 p_item = p_playlist->p_root_category;
181 p_item = (playlist_item_t *)[item pointerValue];
183 if( p_item && index < p_item->i_children && index >= 0 )
184 p_return = p_item->pp_children[index];
186 vlc_object_release( p_playlist );
188 o_value = [o_outline_dict objectForKey:[NSString stringWithFormat: @"%p", p_return]];
192 msg_Warn( VLCIntf, "playlist item misses pointer value, adding one" );
193 o_value = [[NSValue valueWithPointer: p_return] retain];
198 /* is the item expandable */
199 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
202 playlist_t *p_playlist = pl_Yield( VLCIntf );
207 if( p_playlist->p_root_category )
209 i_return = p_playlist->p_root_category->i_children;
214 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
216 i_return = p_item->i_children;
218 vlc_object_release( p_playlist );
220 return (i_return > 0);
223 /* retrieve the string values for the cells */
224 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
227 playlist_item_t *p_item;
229 /* For error handling */
230 static BOOL attempted_reload = NO;
232 if( item == nil || ![item isKindOfClass: [NSValue class]] )
234 /* Attempt to fix the error by asking for a data redisplay
235 * This might cause infinite loop, so add a small check */
236 if( !attempted_reload )
238 attempted_reload = YES;
239 [outlineView reloadData];
244 p_item = (playlist_item_t *)[item pointerValue];
245 if( !p_item || !p_item->p_input )
247 /* Attempt to fix the error by asking for a data redisplay
248 * This might cause infinite loop, so add a small check */
249 if( !attempted_reload )
251 attempted_reload = YES;
252 [outlineView reloadData];
257 attempted_reload = NO;
259 if( [[o_tc identifier] isEqualToString:@"1"] )
261 /* sanity check to prevent the NSString class from crashing */
262 char *psz_title = input_item_GetTitle( p_item->p_input );
263 if( !EMPTY_STR( psz_title ) )
265 o_value = [NSString stringWithUTF8String: psz_title];
269 char *psz_name = input_item_GetName( p_item->p_input );
270 if( !EMPTY_STR( psz_name ) )
272 o_value = [NSString stringWithUTF8String: psz_name];
280 char *psz_artist = input_item_GetArtist( p_item->p_input );
281 if( [[o_tc identifier] isEqualToString:@"2"] && !EMPTY_STR( psz_artist ) )
283 o_value = [NSString stringWithUTF8String: psz_artist];
285 else if( [[o_tc identifier] isEqualToString:@"3"] )
287 char psz_duration[MSTRTIME_MAX_SIZE];
288 mtime_t dur = input_item_GetDuration( p_item->p_input );
291 secstotimestr( psz_duration, dur/1000000 );
292 o_value = [NSString stringWithUTF8String: psz_duration];
306 /*****************************************************************************
307 * VLCPlaylistWizard implementation
308 *****************************************************************************/
309 @implementation VLCPlaylistWizard
311 - (IBAction)reloadOutlineView
313 /* Only reload the outlineview if the wizard window is open since this can
314 be quite long on big playlists */
315 if( [[o_outline_view window] isVisible] )
317 [o_outline_view reloadData];
323 /*****************************************************************************
324 * extension to NSOutlineView's interface to fix compilation warnings
325 * and let us access these 2 functions properly
326 * this uses a private Apple-API, but works fine on all current OSX releases
327 * keep checking for compatiblity with future releases though
328 *****************************************************************************/
330 @interface NSOutlineView (UndocumentedSortImages)
331 + (NSImage *)_defaultTableHeaderSortImage;
332 + (NSImage *)_defaultTableHeaderReverseSortImage;
336 /*****************************************************************************
337 * VLCPlaylist implementation
338 *****************************************************************************/
339 @implementation VLCPlaylist
346 o_nodes_array = [[NSMutableArray alloc] init];
347 o_items_array = [[NSMutableArray alloc] init];
354 playlist_t * p_playlist = pl_Yield( VLCIntf );
358 [super awakeFromNib];
360 [o_outline_view setDoubleAction: @selector(playItem:)];
362 [o_outline_view registerForDraggedTypes:
363 [NSArray arrayWithObjects: NSFilenamesPboardType,
364 @"VLCPlaylistItemPboardType", nil]];
365 [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
367 /* This uses private Apple API which works fine until 10.5.
368 * We need to keep checking in the future!
369 * These methods are being added artificially to NSOutlineView's interface above */
370 o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
371 o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
373 o_tc_sortColumn = nil;
376 char ** ppsz_services = services_discovery_GetServicesNames( p_playlist, &ppsz_name );
379 vlc_object_release( p_playlist );
383 for( i = 0; ppsz_services[i]; i++ )
388 char * name = ppsz_name[i] ? ppsz_name[i] : ppsz_services[i];
389 /* Check whether to enable these menuitems */
390 b_enabled = playlist_IsServicesDiscoveryLoaded( p_playlist, ppsz_services[i] );
392 /* Create the menu entries used in the playlist menu */
393 o_lmi = [[o_mi_services submenu] addItemWithTitle:
394 [NSString stringWithUTF8String: name]
395 action: @selector(servicesChange:)
397 [o_lmi setTarget: self];
398 [o_lmi setRepresentedObject: [NSString stringWithUTF8String: ppsz_services[i]]];
399 if( b_enabled ) [o_lmi setState: NSOnState];
401 /* Create the menu entries for the main menu */
402 o_lmi = [[o_mm_mi_services submenu] addItemWithTitle:
403 [NSString stringWithUTF8String: name]
404 action: @selector(servicesChange:)
406 [o_lmi setTarget: self];
407 [o_lmi setRepresentedObject: [NSString stringWithUTF8String: ppsz_services[i]]];
408 if( b_enabled ) [o_lmi setState: NSOnState];
410 free( ppsz_services[i] );
411 free( ppsz_name[i] );
413 free( ppsz_services );
416 vlc_object_release( p_playlist );
419 - (void)searchfieldChanged:(NSNotification *)o_notification
421 [o_search_field setStringValue:[[o_notification object] stringValue]];
428 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
429 [o_mi_play setTitle: _NS("Play")];
430 [o_mi_delete setTitle: _NS("Delete")];
431 [o_mi_recursive_expand setTitle: _NS("Expand Node")];
432 [o_mi_selectall setTitle: _NS("Select All")];
433 [o_mi_info setTitle: _NS("Information...")];
434 [o_mi_preparse setTitle: _NS("Fetch Meta Data")];
435 [o_mi_sort_name setTitle: _NS("Sort Node by Name")];
436 [o_mi_sort_author setTitle: _NS("Sort Node by Author")];
437 [o_mi_services setTitle: _NS("Services discovery")];
438 [o_status_field setStringValue: _NS("No items in the playlist")];
440 [o_search_field setToolTip: _NS("Search in Playlist")];
441 [o_mi_addNode setTitle: _NS("Add Folder to Playlist")];
443 [o_save_accessory_text setStringValue: _NS("File Format:")];
444 [[o_save_accessory_popup itemAtIndex:0] setTitle: _NS("Extended M3U")];
445 [[o_save_accessory_popup itemAtIndex:1] setTitle: _NS("XML Shareable Playlist Format (XSPF)")];
448 - (void)playlistUpdated
450 /* Clear indications of any existing column sorting */
451 for( unsigned int i = 0 ; i < [[o_outline_view tableColumns] count] ; i++ )
453 [o_outline_view setIndicatorImage:nil inTableColumn:
454 [[o_outline_view tableColumns] objectAtIndex:i]];
457 [o_outline_view setHighlightedTableColumn:nil];
458 o_tc_sortColumn = nil;
459 // TODO Find a way to keep the dict size to a minimum
460 //[o_outline_dict removeAllObjects];
461 [o_outline_view reloadData];
462 [[[[VLCMain sharedInstance] getWizard] getPlaylistWizard] reloadOutlineView];
463 [[[[VLCMain sharedInstance] getBookmarks] getDataTable] reloadData];
465 playlist_t *p_playlist = pl_Yield( VLCIntf );
467 if( playlist_CurrentSize( p_playlist ) >= 2 )
469 [o_status_field setStringValue: [NSString stringWithFormat:
471 playlist_CurrentSize( p_playlist )]];
475 if( playlist_IsEmpty( p_playlist ) )
476 [o_status_field setStringValue: _NS("No items in the playlist")];
478 [o_status_field setStringValue: _NS("1 item")];
480 vlc_object_release( p_playlist );
483 - (void)playModeUpdated
485 playlist_t *p_playlist = pl_Yield( VLCIntf );
487 bool loop = var_GetBool( p_playlist, "loop" );
488 bool repeat = var_GetBool( p_playlist, "repeat" );
490 [[[VLCMain sharedInstance] getControls] repeatOne];
492 [[[VLCMain sharedInstance] getControls] repeatAll];
494 [[[VLCMain sharedInstance] getControls] repeatOff];
496 [[[VLCMain sharedInstance] getControls] shuffle];
498 vlc_object_release( p_playlist );
501 - (void)outlineViewSelectionDidChange:(NSNotification *)notification
504 playlist_item_t * p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
508 /* update our info-panel to reflect the new item */
509 [[[VLCMain sharedInstance] getInfo] updatePanelWithItem:p_item->p_input];
513 - (BOOL)isSelectionEmpty
515 return [o_outline_view selectedRow] == -1;
518 - (void)updateRowSelection
524 playlist_t *p_playlist = pl_Yield( VLCIntf );
525 playlist_item_t *p_item, *p_temp_item;
526 NSMutableArray *o_array = [NSMutableArray array];
528 p_item = p_playlist->status.p_item;
531 vlc_object_release(p_playlist);
535 p_temp_item = p_item;
536 while( p_temp_item->p_parent )
538 [o_array insertObject: [NSValue valueWithPointer: p_temp_item] atIndex: 0];
539 p_temp_item = p_temp_item->p_parent;
542 for( j = 0; j < [o_array count] - 1; j++ )
545 if( ( o_item = [o_outline_dict objectForKey:
546 [NSString stringWithFormat: @"%p",
547 [[o_array objectAtIndex:j] pointerValue]]] ) != nil )
549 [o_outline_view expandItem: o_item];
554 /* update our info-panel to reflect the new item */
555 [[[VLCMain sharedInstance] getInfo] updatePanelWithItem:p_item->p_input];
557 vlc_object_release( p_playlist );
561 /* Check if p_item is a child of p_node recursively. We need to check the item
562 existence first since OSX sometimes tries to redraw items that have been
563 deleted. We don't do it when not required since this verification takes
564 quite a long time on big playlists (yes, pretty hacky). */
565 - (BOOL)isItem: (playlist_item_t *)p_item
566 inNode: (playlist_item_t *)p_node
567 checkItemExistence:(BOOL)b_check
570 playlist_t * p_playlist = pl_Yield( VLCIntf );
571 playlist_item_t *p_temp_item = p_item;
573 if( p_node == p_item )
575 vlc_object_release(p_playlist);
579 if( p_node->i_children < 1)
581 vlc_object_release(p_playlist);
592 /* Since outlineView: willDisplayCell:... may call this function with
593 p_items that don't exist anymore, first check if the item is still
594 in the playlist. Any cleaner solution welcomed. */
595 for( i = 0; i < p_playlist->all_items.i_size; i++ )
597 if( ARRAY_VAL( p_playlist->all_items, i) == p_item ) break;
598 else if ( i == p_playlist->all_items.i_size - 1 )
600 vlc_object_release( p_playlist );
609 p_temp_item = p_temp_item->p_parent;
610 if( p_temp_item == p_node )
613 vlc_object_release( p_playlist );
620 vlc_object_release( p_playlist );
624 /* This method is usefull for instance to remove the selected children of an
625 already selected node */
626 - (void)removeItemsFrom:(id)o_items ifChildrenOf:(id)o_nodes
629 for( i = 0 ; i < [o_items count] ; i++ )
631 for ( j = 0 ; j < [o_nodes count] ; j++ )
633 if( o_items == o_nodes)
635 if( j == i ) continue;
637 if( [self isItem: [[o_items objectAtIndex:i] pointerValue]
638 inNode: [[o_nodes objectAtIndex:j] pointerValue]
639 checkItemExistence: NO] )
641 [o_items removeObjectAtIndex:i];
642 /* We need to execute the next iteration with the same index
643 since the current item has been deleted */
652 - (IBAction)savePlaylist:(id)sender
654 playlist_t * p_playlist = pl_Yield( VLCIntf );
656 NSSavePanel *o_save_panel = [NSSavePanel savePanel];
657 NSString * o_name = [NSString stringWithFormat: @"%@", _NS("Untitled")];
659 //[o_save_panel setAllowedFileTypes: [NSArray arrayWithObjects: @"m3u", @"xpf", nil] ];
660 [o_save_panel setTitle: _NS("Save Playlist")];
661 [o_save_panel setPrompt: _NS("Save")];
662 [o_save_panel setAccessoryView: o_save_accessory_view];
664 if( [o_save_panel runModalForDirectory: nil
665 file: o_name] == NSOKButton )
667 NSString *o_filename = [o_save_panel filename];
669 if( [o_save_accessory_popup indexOfSelectedItem] == 1 )
671 NSString * o_real_filename;
673 range.location = [o_filename length] - [@".xspf" length];
674 range.length = [@".xspf" length];
676 if( [o_filename compare:@".xspf" options: NSCaseInsensitiveSearch
677 range: range] != NSOrderedSame )
679 o_real_filename = [NSString stringWithFormat: @"%@.xspf", o_filename];
683 o_real_filename = o_filename;
685 playlist_Export( p_playlist,
686 [o_real_filename fileSystemRepresentation],
687 p_playlist->p_local_category, "export-xspf" );
691 NSString * o_real_filename;
693 range.location = [o_filename length] - [@".m3u" length];
694 range.length = [@".m3u" length];
696 if( [o_filename compare:@".m3u" options: NSCaseInsensitiveSearch
697 range: range] != NSOrderedSame )
699 o_real_filename = [NSString stringWithFormat: @"%@.m3u", o_filename];
703 o_real_filename = o_filename;
705 playlist_Export( p_playlist,
706 [o_real_filename fileSystemRepresentation],
707 p_playlist->p_local_category, "export-m3u" );
710 vlc_object_release( p_playlist );
713 /* When called retrieves the selected outlineview row and plays that node or item */
714 - (IBAction)playItem:(id)sender
716 intf_thread_t * p_intf = VLCIntf;
717 playlist_t * p_playlist = pl_Yield( p_intf );
719 playlist_item_t *p_item;
720 playlist_item_t *p_node = NULL;
722 p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
726 if( p_item->i_children == -1 )
728 p_node = p_item->p_parent;
734 if( p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1 )
736 p_item = p_node->pp_children[0];
743 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, false, p_node, p_item );
745 vlc_object_release( p_playlist );
748 /* When called retrieves the selected outlineview row and plays that node or item */
749 - (IBAction)preparseItem:(id)sender
752 NSMutableArray *o_to_preparse;
753 intf_thread_t * p_intf = VLCIntf;
754 playlist_t * p_playlist = pl_Yield( p_intf );
756 o_to_preparse = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
757 i_count = [o_to_preparse count];
761 playlist_item_t *p_item = NULL;
763 for( i = 0; i < i_count; i++ )
765 o_number = [o_to_preparse lastObject];
766 i_row = [o_number intValue];
767 p_item = [[o_outline_view itemAtRow:i_row] pointerValue];
768 [o_to_preparse removeObject: o_number];
769 [o_outline_view deselectRow: i_row];
773 if( p_item->i_children == -1 )
775 playlist_PreparseEnqueue( p_playlist, p_item->p_input );
779 msg_Dbg( p_intf, "preparsing nodes not implemented" );
783 vlc_object_release( p_playlist );
784 [self playlistUpdated];
787 - (IBAction)servicesChange:(id)sender
789 NSMenuItem *o_mi = (NSMenuItem *)sender;
790 NSString *o_string = [o_mi representedObject];
791 playlist_t * p_playlist = pl_Yield( VLCIntf );
792 if( !playlist_IsServicesDiscoveryLoaded( p_playlist, [o_string UTF8String] ) )
793 playlist_ServicesDiscoveryAdd( p_playlist, [o_string UTF8String] );
795 playlist_ServicesDiscoveryRemove( p_playlist, [o_string UTF8String] );
797 [o_mi setState: playlist_IsServicesDiscoveryLoaded( p_playlist,
798 [o_string UTF8String] ) ? YES : NO];
800 vlc_object_release( p_playlist );
801 [self playlistUpdated];
805 - (IBAction)selectAll:(id)sender
807 [o_outline_view selectAll: nil];
810 - (IBAction)deleteItem:(id)sender
813 NSMutableArray *o_to_delete;
816 playlist_t * p_playlist;
817 intf_thread_t * p_intf = VLCIntf;
819 o_to_delete = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
820 i_count = [o_to_delete count];
822 p_playlist = pl_Yield( p_intf );
825 for( int i = 0; i < i_count; i++ )
827 o_number = [o_to_delete lastObject];
828 i_row = [o_number intValue];
829 id o_item = [o_outline_view itemAtRow: i_row];
830 playlist_item_t *p_item = [o_item pointerValue];
832 msg_Dbg( p_intf, "deleting item %i (of %i) with id \"%i\", pointerValue \"%p\" and %i children", i+1, i_count,
833 p_item->p_input->i_id, [o_item pointerValue], p_item->i_children +1 );
835 [o_to_delete removeObject: o_number];
836 [o_outline_view deselectRow: i_row];
838 if( p_item->i_children != -1 )
839 //is a node and not an item
841 if( p_playlist->status.i_status != PLAYLIST_STOPPED &&
842 [self isItem: p_playlist->status.p_item inNode:
843 ((playlist_item_t *)[o_item pointerValue])
844 checkItemExistence: NO] == YES )
845 // if current item is in selected node and is playing then stop playlist
846 playlist_Stop( p_playlist );
848 playlist_NodeDelete( p_playlist, p_item, true, false );
851 playlist_DeleteFromInput( p_playlist, p_item->p_input->i_id, true );
855 [self playlistUpdated];
856 vlc_object_release( p_playlist );
859 - (IBAction)sortNodeByName:(id)sender
861 [self sortNode: SORT_TITLE];
864 - (IBAction)sortNodeByAuthor:(id)sender
866 [self sortNode: SORT_ARTIST];
869 - (void)sortNode:(int)i_mode
871 playlist_t * p_playlist = pl_Yield( VLCIntf );
872 playlist_item_t * p_item;
874 if( [o_outline_view selectedRow] > -1 )
876 p_item = [[o_outline_view itemAtRow: [o_outline_view selectedRow]] pointerValue];
879 /*If no item is selected, sort the whole playlist*/
881 p_item = p_playlist->p_root_category;
884 if( p_item->i_children > -1 ) // the item is a node
887 playlist_RecursiveNodeSort( p_playlist, p_item, i_mode, ORDER_NORMAL );
893 playlist_RecursiveNodeSort( p_playlist,
894 p_item->p_parent, i_mode, ORDER_NORMAL );
897 vlc_object_release( p_playlist );
898 [self playlistUpdated];
901 - (input_item_t *)createItem:(NSDictionary *)o_one_item
903 intf_thread_t * p_intf = VLCIntf;
904 playlist_t * p_playlist = pl_Yield( p_intf );
906 input_item_t *p_input;
908 BOOL b_rem = FALSE, b_dir = FALSE;
909 NSString *o_uri, *o_name;
914 o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
915 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
916 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
918 /* Find the name for a disc entry (i know, can you believe the trouble?) */
919 if( ( !o_name || [o_name isEqualToString:@""] ) && [o_uri rangeOfString: @"/dev/"].location != NSNotFound )
921 int i_count, i_index;
922 struct statfs *mounts = NULL;
924 i_count = getmntinfo (&mounts, MNT_NOWAIT);
925 /* getmntinfo returns a pointer to static data. Do not free. */
926 for( i_index = 0 ; i_index < i_count; i_index++ )
928 NSMutableString *o_temp, *o_temp2;
929 o_temp = [NSMutableString stringWithString: o_uri];
930 o_temp2 = [NSMutableString stringWithUTF8String: mounts[i_index].f_mntfromname];
931 [o_temp replaceOccurrencesOfString: @"/dev/rdisk" withString: @"/dev/disk" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
932 [o_temp2 replaceOccurrencesOfString: @"s0" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp2 length]) ];
933 [o_temp2 replaceOccurrencesOfString: @"s1" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp2 length]) ];
935 if( strstr( [o_temp fileSystemRepresentation], [o_temp2 fileSystemRepresentation] ) != NULL )
937 o_name = [[NSFileManager defaultManager] displayNameAtPath: [NSString stringWithUTF8String:mounts[i_index].f_mntonname]];
941 /* If no name, then make a guess */
942 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
944 if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
945 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
946 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
948 /* All of this is to make sure CD's play when you D&D them on VLC */
949 /* Converts mountpoint to a /dev file */
952 NSMutableString *o_temp;
954 buf = (struct statfs *) malloc (sizeof(struct statfs));
955 statfs( [o_uri fileSystemRepresentation], buf );
956 psz_dev = strdup(buf->f_mntfromname);
957 o_temp = [NSMutableString stringWithUTF8String: psz_dev ];
958 [o_temp replaceOccurrencesOfString: @"/dev/disk" withString: @"/dev/rdisk" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
959 [o_temp replaceOccurrencesOfString: @"s0" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
960 [o_temp replaceOccurrencesOfString: @"s1" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
964 p_input = input_ItemNew( p_playlist, [o_uri fileSystemRepresentation], [o_name UTF8String] );
970 for( i = 0; i < (int)[o_options count]; i++ )
972 input_ItemAddOption( p_input, strdup( [[o_options objectAtIndex:i] UTF8String] ) );
976 /* Recent documents menu */
977 o_true_file = [NSURL fileURLWithPath: o_uri];
978 if( o_true_file != nil && (BOOL)config_GetInt( p_playlist, "macosx-recentitems" ) == YES )
980 [[NSDocumentController sharedDocumentController]
981 noteNewRecentDocumentURL: o_true_file];
984 vlc_object_release( p_playlist );
988 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
991 playlist_t * p_playlist = pl_Yield( VLCIntf );
994 for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
996 input_item_t *p_input;
997 NSDictionary *o_one_item;
1000 o_one_item = [o_array objectAtIndex: i_item];
1001 p_input = [self createItem: o_one_item];
1008 /* FIXME: playlist_AddInput() can fail */
1010 playlist_AddInput( p_playlist, p_input, PLAYLIST_INSERT,
1011 i_position == -1 ? PLAYLIST_END : i_position + i_item, true,
1014 if( i_item == 0 && !b_enqueue )
1016 playlist_item_t *p_item;
1017 p_item = playlist_ItemGetByInput( p_playlist, p_input, true );
1018 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, true, NULL, p_item );
1020 vlc_gc_decref( p_input );
1024 [self playlistUpdated];
1025 vlc_object_release( p_playlist );
1028 - (void)appendNodeArray:(NSArray*)o_array inNode:(playlist_item_t *)p_node atPos:(int)i_position enqueue:(BOOL)b_enqueue
1031 playlist_t * p_playlist = pl_Yield( VLCIntf );
1033 for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
1035 input_item_t *p_input;
1036 NSDictionary *o_one_item;
1039 o_one_item = [o_array objectAtIndex: i_item];
1040 p_input = [self createItem: o_one_item];
1042 if( !p_input ) continue;
1045 /* FIXME: playlist_BothAddInput() can fail */
1046 playlist_BothAddInput( p_playlist, p_input, p_node,
1049 PLAYLIST_END : i_position + i_item,
1050 NULL, NULL, false );
1053 if( i_item == 0 && !b_enqueue )
1055 playlist_item_t *p_item;
1056 p_item = playlist_ItemGetByInput( p_playlist, p_input, true );
1057 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, true, NULL, p_item );
1059 vlc_gc_decref( p_input );
1061 [self playlistUpdated];
1062 vlc_object_release( p_playlist );
1065 - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
1067 playlist_t *p_playlist = pl_Yield( VLCIntf );
1068 playlist_item_t *p_selected_item;
1069 int i_current, i_selected_row;
1071 i_selected_row = [o_outline_view selectedRow];
1072 if (i_selected_row < 0)
1075 p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow:
1076 i_selected_row] pointerValue];
1078 for( i_current = 0; i_current < p_item->i_children ; i_current++ )
1081 NSString *o_current_name, *o_current_author;
1084 o_current_name = [NSString stringWithUTF8String:
1085 p_item->pp_children[i_current]->p_input->psz_name];
1086 psz_temp = input_ItemGetInfo( p_item->p_input ,
1087 _("Meta-information"),_("Artist") );
1088 o_current_author = [NSString stringWithUTF8String: psz_temp];
1092 if( p_selected_item == p_item->pp_children[i_current] &&
1093 b_selected_item_met == NO )
1095 b_selected_item_met = YES;
1097 else if( p_selected_item == p_item->pp_children[i_current] &&
1098 b_selected_item_met == YES )
1100 vlc_object_release( p_playlist );
1103 else if( b_selected_item_met == YES &&
1104 ( [o_current_name rangeOfString:[o_search_field
1105 stringValue] options:NSCaseInsensitiveSearch].length ||
1106 [o_current_author rangeOfString:[o_search_field
1107 stringValue] options:NSCaseInsensitiveSearch].length ) )
1109 vlc_object_release( p_playlist );
1110 /*Adds the parent items in the result array as well, so that we can
1112 return [NSMutableArray arrayWithObject: [NSValue
1113 valueWithPointer: p_item->pp_children[i_current]]];
1115 if( p_item->pp_children[i_current]->i_children > 0 )
1117 id o_result = [self subSearchItem:
1118 p_item->pp_children[i_current]];
1119 if( o_result != NULL )
1121 vlc_object_release( p_playlist );
1122 [o_result insertObject: [NSValue valueWithPointer:
1123 p_item->pp_children[i_current]] atIndex:0];
1128 vlc_object_release( p_playlist );
1132 - (IBAction)searchItem:(id)sender
1134 playlist_t * p_playlist = pl_Yield( VLCIntf );
1140 b_selected_item_met = NO;
1142 /*First, only search after the selected item:*
1143 *(b_selected_item_met = NO) */
1144 o_result = [self subSearchItem:p_playlist->p_root_category];
1145 if( o_result == NULL )
1147 /* If the first search failed, search again from the beginning */
1148 o_result = [self subSearchItem:p_playlist->p_root_category];
1150 if( o_result != NULL )
1153 if( [[o_result objectAtIndex: 0] pointerValue] ==
1154 p_playlist->p_local_category )
1159 for( i = i_start ; i < [o_result count] - 1 ; i++ )
1161 [o_outline_view expandItem: [o_outline_dict objectForKey:
1162 [NSString stringWithFormat: @"%p",
1163 [[o_result objectAtIndex: i] pointerValue]]]];
1165 i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
1166 [NSString stringWithFormat: @"%p",
1167 [[o_result objectAtIndex: [o_result count] - 1 ]
1172 [o_outline_view selectRow:i_row byExtendingSelection: NO];
1173 [o_outline_view scrollRowToVisible: i_row];
1175 vlc_object_release( p_playlist );
1178 - (IBAction)recursiveExpandNode:(id)sender
1180 id o_item = [o_outline_view itemAtRow: [o_outline_view selectedRow]];
1181 playlist_item_t *p_item = (playlist_item_t *)[o_item pointerValue];
1183 if( ![[o_outline_view dataSource] outlineView: o_outline_view
1184 isItemExpandable: o_item] )
1186 o_item = [o_outline_dict objectForKey: [NSString
1187 stringWithFormat: @"%p", p_item->p_parent]];
1190 /* We need to collapse the node first, since OSX refuses to recursively
1191 expand an already expanded node, even if children nodes are collapsed. */
1192 [o_outline_view collapseItem: o_item collapseChildren: YES];
1193 [o_outline_view expandItem: o_item expandChildren: YES];
1196 - (NSMenu *)menuForEvent:(NSEvent *)o_event
1202 pt = [o_outline_view convertPoint: [o_event locationInWindow]
1204 NSInteger row = [o_outline_view rowAtPoint:pt];
1206 [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
1208 b_item_sel = ( row != -1 && [o_outline_view selectedRow] != -1 );
1209 b_rows = [o_outline_view numberOfRows] != 0;
1211 [o_mi_play setEnabled: b_item_sel];
1212 [o_mi_delete setEnabled: b_item_sel];
1213 [o_mi_selectall setEnabled: b_rows];
1214 [o_mi_info setEnabled: b_item_sel];
1215 [o_mi_preparse setEnabled: b_item_sel];
1216 [o_mi_recursive_expand setEnabled: b_item_sel];
1217 [o_mi_sort_name setEnabled: b_item_sel];
1218 [o_mi_sort_author setEnabled: b_item_sel];
1220 return( o_ctx_menu );
1223 - (void)outlineView: (NSTableView*)o_tv
1224 didClickTableColumn:(NSTableColumn *)o_tc
1226 int i_mode = 0, i_type;
1227 intf_thread_t *p_intf = VLCIntf;
1229 playlist_t *p_playlist = pl_Yield( p_intf );
1231 /* Check whether the selected table column header corresponds to a
1232 sortable table column*/
1233 if( !( o_tc == o_tc_name || o_tc == o_tc_author ) )
1235 vlc_object_release( p_playlist );
1239 if( o_tc_sortColumn == o_tc )
1241 b_isSortDescending = !b_isSortDescending;
1245 b_isSortDescending = false;
1248 if( o_tc == o_tc_name )
1250 i_mode = SORT_TITLE;
1252 else if( o_tc == o_tc_author )
1254 i_mode = SORT_ARTIST;
1257 if( b_isSortDescending )
1259 i_type = ORDER_REVERSE;
1263 i_type = ORDER_NORMAL;
1266 vlc_object_lock( p_playlist );
1267 playlist_RecursiveNodeSort( p_playlist, p_playlist->p_root_category, i_mode, i_type );
1268 vlc_object_unlock( p_playlist );
1270 vlc_object_release( p_playlist );
1271 [self playlistUpdated];
1273 o_tc_sortColumn = o_tc;
1274 [o_outline_view setHighlightedTableColumn:o_tc];
1276 if( b_isSortDescending )
1278 [o_outline_view setIndicatorImage:o_descendingSortingImage
1279 inTableColumn:o_tc];
1283 [o_outline_view setIndicatorImage:o_ascendingSortingImage
1284 inTableColumn:o_tc];
1289 - (void)outlineView:(NSOutlineView *)outlineView
1290 willDisplayCell:(id)cell
1291 forTableColumn:(NSTableColumn *)tableColumn
1294 playlist_t *p_playlist = pl_Yield( VLCIntf );
1298 o_playing_item = [o_outline_dict objectForKey:
1299 [NSString stringWithFormat:@"%p", p_playlist->status.p_item]];
1301 if( [self isItem: [o_playing_item pointerValue] inNode:
1302 [item pointerValue] checkItemExistence: YES]
1303 || [o_playing_item isEqual: item] )
1305 [cell setFont: [[NSFontManager sharedFontManager] convertFont:[cell font] toHaveTrait:NSBoldFontMask]];
1309 [cell setFont: [[NSFontManager sharedFontManager] convertFont:[cell font] toNotHaveTrait:NSBoldFontMask]];
1311 vlc_object_release( p_playlist );
1314 - (IBAction)addNode:(id)sender
1316 /* we have to create a new thread here because otherwise we would block the
1317 * interface since the interaction-stuff and this code would run in the same
1319 [NSThread detachNewThreadSelector: @selector(addNodeThreadedly)
1320 toTarget: self withObject:nil];
1321 [self playlistUpdated];
1324 - (void)addNodeThreadedly
1326 NSAutoreleasePool * ourPool = [[NSAutoreleasePool alloc] init];
1328 /* simply adds a new node to the end of the playlist */
1329 playlist_t * p_playlist = pl_Yield( VLCIntf );
1330 vlc_thread_set_priority( p_playlist, VLC_THREAD_PRIORITY_LOW );
1333 char *psz_name = NULL;
1334 playlist_item_t * p_item;
1335 ret_v = intf_UserStringInput( p_playlist, _("New Node"),
1336 _("Please enter a name for the new node."), &psz_name );
1338 if( ret_v != DIALOG_CANCELLED && psz_name && *psz_name )
1339 p_item = playlist_NodeCreate( p_playlist, psz_name,
1340 p_playlist->p_local_category, 0, NULL );
1341 else if(! config_GetInt( p_playlist, "interact" ) )
1343 /* in case that the interaction is disabled, just give it a bogus name */
1344 p_item = playlist_NodeCreate( p_playlist, _("Empty Folder"),
1345 p_playlist->p_local_category, 0, NULL );
1349 msg_Warn( VLCIntf, "node creation failed or cancelled by user" );
1351 vlc_object_release( p_playlist );
1357 @implementation VLCPlaylist (NSOutlineViewDataSource)
1359 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
1361 id o_value = [super outlineView: outlineView child: index ofItem: item];
1362 playlist_t *p_playlist = pl_Yield( VLCIntf );
1364 if( playlist_CurrentSize( p_playlist ) >= 2 )
1366 [o_status_field setStringValue: [NSString stringWithFormat:
1368 playlist_CurrentSize( p_playlist )]];
1372 if( playlist_IsEmpty( p_playlist ) )
1374 [o_status_field setStringValue: _NS("No items in the playlist")];
1378 [o_status_field setStringValue: _NS("1 item")];
1381 vlc_object_release( p_playlist );
1383 [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p",
1384 [o_value pointerValue]]];
1389 /* Required for drag & drop and reordering */
1390 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
1393 playlist_t *p_playlist = pl_Yield( VLCIntf );
1395 /* First remove the items that were moved during the last drag & drop
1397 [o_items_array removeAllObjects];
1398 [o_nodes_array removeAllObjects];
1400 for( i = 0 ; i < [items count] ; i++ )
1402 id o_item = [items objectAtIndex: i];
1404 /* Refuse to move items that are not in the General Node
1405 (Service Discovery) */
1406 if( ![self isItem: [o_item pointerValue] inNode:
1407 p_playlist->p_local_category checkItemExistence: NO] &&
1408 ( var_CreateGetBool( p_playlist, "media-library" ) &&
1409 ![self isItem: [o_item pointerValue] inNode:
1410 p_playlist->p_ml_category checkItemExistence: NO]) )
1412 vlc_object_release(p_playlist);
1415 /* Fill the items and nodes to move in 2 different arrays */
1416 if( ((playlist_item_t *)[o_item pointerValue])->i_children > 0 )
1417 [o_nodes_array addObject: o_item];
1419 [o_items_array addObject: o_item];
1422 /* Now we need to check if there are selected items that are in already
1423 selected nodes. In that case, we only want to move the nodes */
1424 [self removeItemsFrom: o_nodes_array ifChildrenOf: o_nodes_array];
1425 [self removeItemsFrom: o_items_array ifChildrenOf: o_nodes_array];
1427 /* We add the "VLCPlaylistItemPboardType" type to be able to recognize
1428 a Drop operation coming from the playlist. */
1430 [pboard declareTypes: [NSArray arrayWithObjects:
1431 @"VLCPlaylistItemPboardType", nil] owner: self];
1432 [pboard setData:[NSData data] forType:@"VLCPlaylistItemPboardType"];
1434 vlc_object_release(p_playlist);
1438 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
1440 playlist_t *p_playlist = pl_Yield( VLCIntf );
1441 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1443 if( !p_playlist ) return NSDragOperationNone;
1445 /* Dropping ON items is not allowed if item is not a node */
1448 if( index == NSOutlineViewDropOnItemIndex &&
1449 ((playlist_item_t *)[item pointerValue])->i_children == -1 )
1451 vlc_object_release( p_playlist );
1452 return NSDragOperationNone;
1456 /* Don't allow on drop on playlist root element's child */
1457 if( !item && index != NSOutlineViewDropOnItemIndex)
1459 vlc_object_release( p_playlist );
1460 return NSDragOperationNone;
1463 /* We refuse to drop an item in anything else than a child of the General
1464 Node. We still accept items that would be root nodes of the outlineview
1465 however, to allow drop in an empty playlist. */
1466 if( !( ([self isItem: [item pointerValue] inNode: p_playlist->p_local_category checkItemExistence: NO] ||
1467 ( var_CreateGetBool( p_playlist, "media-library" ) && [self isItem: [item pointerValue] inNode: p_playlist->p_ml_category checkItemExistence: NO] ) ) || item == nil ) )
1469 vlc_object_release( p_playlist );
1470 return NSDragOperationNone;
1473 /* Drop from the Playlist */
1474 if( [[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] )
1477 for( i = 0 ; i < [o_nodes_array count] ; i++ )
1479 /* We refuse to Drop in a child of an item we are moving */
1480 if( [self isItem: [item pointerValue] inNode:
1481 [[o_nodes_array objectAtIndex: i] pointerValue]
1482 checkItemExistence: NO] )
1484 vlc_object_release( p_playlist );
1485 return NSDragOperationNone;
1488 vlc_object_release( p_playlist );
1489 return NSDragOperationMove;
1492 /* Drop from the Finder */
1493 else if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1495 vlc_object_release( p_playlist );
1496 return NSDragOperationGeneric;
1498 vlc_object_release( p_playlist );
1499 return NSDragOperationNone;
1502 - (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(int)index
1504 playlist_t * p_playlist = pl_Yield( VLCIntf );
1505 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1507 /* Drag & Drop inside the playlist */
1508 if( [[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] )
1510 int i_row, i_removed_from_node = 0;
1512 playlist_item_t *p_new_parent, *p_item = NULL;
1513 NSArray *o_all_items = [o_nodes_array arrayByAddingObjectsFromArray:
1515 /* If the item is to be dropped as root item of the outline, make it a
1516 child of the General node.
1517 Else, choose the proposed parent as parent. */
1518 if( item == nil ) p_new_parent = p_playlist->p_local_category;
1519 else p_new_parent = [item pointerValue];
1521 /* Make sure the proposed parent is a node.
1522 (This should never be true) */
1523 if( p_new_parent->i_children < 0 )
1525 vlc_object_release( p_playlist );
1529 for( i = 0; i < [o_all_items count]; i++ )
1531 playlist_item_t *p_old_parent = NULL;
1532 int i_old_index = 0;
1534 p_item = [[o_all_items objectAtIndex:i] pointerValue];
1535 p_old_parent = p_item->p_parent;
1538 /* We may need the old index later */
1539 if( p_new_parent == p_old_parent )
1542 for( j = 0; j < p_old_parent->i_children; j++ )
1544 if( p_old_parent->pp_children[j] == p_item )
1553 // Actually detach the item from the old position
1554 if( playlist_NodeRemoveItem( p_playlist, p_item, p_old_parent ) ==
1558 /* Calculate the new index */
1561 /* If we move the item in the same node, we need to take into
1562 account that one item will be deleted */
1565 if ((p_new_parent == p_old_parent &&
1566 i_old_index < index + (int)i) )
1568 i_removed_from_node++;
1570 i_new_index = index + i - i_removed_from_node;
1572 // Reattach the item to the new position
1573 playlist_NodeInsert( p_playlist, p_item, p_new_parent, i_new_index );
1577 [self playlistUpdated];
1578 i_row = [o_outline_view rowForItem:[o_outline_dict
1579 objectForKey:[NSString stringWithFormat: @"%p",
1580 [[o_all_items objectAtIndex: 0] pointerValue]]]];
1584 i_row = [o_outline_view rowForItem:[o_outline_dict
1585 objectForKey:[NSString stringWithFormat: @"%p", p_new_parent]]];
1588 [o_outline_view deselectAll: self];
1589 [o_outline_view selectRow: i_row byExtendingSelection: NO];
1590 [o_outline_view scrollRowToVisible: i_row];
1592 vlc_object_release( p_playlist );
1596 else if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1599 playlist_item_t *p_node = [item pointerValue];
1601 NSArray *o_array = [NSArray array];
1602 NSArray *o_values = [[o_pasteboard propertyListForType:
1603 NSFilenamesPboardType]
1604 sortedArrayUsingSelector:
1605 @selector(caseInsensitiveCompare:)];
1607 for( i = 0; i < (int)[o_values count]; i++)
1609 NSDictionary *o_dic;
1610 o_dic = [NSDictionary dictionaryWithObject:[o_values
1611 objectAtIndex:i] forKey:@"ITEM_URL"];
1612 o_array = [o_array arrayByAddingObject: o_dic];
1617 [self appendArray:o_array atPos:index enqueue: YES];
1621 assert( p_node->i_children != -1 );
1622 [self appendNodeArray:o_array inNode: p_node
1623 atPos:index enqueue:YES];
1625 vlc_object_release( p_playlist );
1628 vlc_object_release( p_playlist );