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];
119 vlc_object_release( p_playlist );
125 [[o_tc_name headerCell] setStringValue:_NS("Name")];
126 [[o_tc_author headerCell] setStringValue:_NS("Author")];
127 [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
130 - (NSOutlineView *)outlineView
132 return o_outline_view;
135 - (playlist_item_t *)selectedPlaylistItem
137 return [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
143 @implementation VLCPlaylistCommon (NSOutlineViewDataSource)
145 /* return the number of children for Obj-C pointer item */ /* DONE */
146 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
149 playlist_item_t *p_item = NULL;
150 playlist_t * p_playlist = pl_Yield( VLCIntf );
151 assert( outlineView == o_outline_view );
154 p_item = p_playlist->p_root_category;
156 p_item = (playlist_item_t *)[item pointerValue];
159 i_return = p_item->i_children;
161 pl_Release( VLCIntf );
163 return i_return > 0 ? i_return : 0;
166 /* return the child at index for the Obj-C pointer item */ /* DONE */
167 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
169 playlist_item_t *p_return = NULL, *p_item = NULL;
171 playlist_t * p_playlist = pl_Yield( VLCIntf );
176 p_item = p_playlist->p_root_category;
180 p_item = (playlist_item_t *)[item pointerValue];
182 if( p_item && index < p_item->i_children && index >= 0 )
183 p_return = p_item->pp_children[index];
185 vlc_object_release( p_playlist );
187 o_value = [o_outline_dict objectForKey:[NSString stringWithFormat: @"%p", p_return]];
191 msg_Warn( VLCIntf, "playlist item misses pointer value, adding one" );
192 o_value = [[NSValue valueWithPointer: p_return] retain];
197 /* is the item expandable */
198 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
201 playlist_t *p_playlist = pl_Yield( VLCIntf );
206 if( p_playlist->p_root_category )
208 i_return = p_playlist->p_root_category->i_children;
213 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
215 i_return = p_item->i_children;
217 vlc_object_release( p_playlist );
219 return (i_return > 0);
222 /* retrieve the string values for the cells */
223 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
226 playlist_item_t *p_item;
228 /* For error handling */
229 static BOOL attempted_reload = NO;
231 if( item == nil || ![item isKindOfClass: [NSValue class]] )
233 /* Attempt to fix the error by asking for a data redisplay
234 * This might cause infinite loop, so add a small check */
235 if( !attempted_reload )
237 attempted_reload = YES;
238 [outlineView reloadData];
243 p_item = (playlist_item_t *)[item pointerValue];
244 if( !p_item || !p_item->p_input )
246 /* Attempt to fix the error by asking for a data redisplay
247 * This might cause infinite loop, so add a small check */
248 if( !attempted_reload )
250 attempted_reload = YES;
251 [outlineView reloadData];
256 attempted_reload = NO;
258 if( [[o_tc identifier] isEqualToString:@"1"] )
260 /* sanity check to prevent the NSString class from crashing */
261 char *psz_title = input_item_GetTitle( p_item->p_input );
262 if( !EMPTY_STR( psz_title ) )
264 o_value = [NSString stringWithUTF8String: psz_title];
268 char *psz_name = input_item_GetName( p_item->p_input );
269 if( !EMPTY_STR( psz_name ) )
271 o_value = [NSString stringWithUTF8String: psz_name];
279 char *psz_artist = input_item_GetArtist( p_item->p_input );
280 if( [[o_tc identifier] isEqualToString:@"2"] && !EMPTY_STR( psz_artist ) )
282 o_value = [NSString stringWithUTF8String: psz_artist];
284 else if( [[o_tc identifier] isEqualToString:@"3"] )
286 char psz_duration[MSTRTIME_MAX_SIZE];
287 mtime_t dur = input_item_GetDuration( p_item->p_input );
290 secstotimestr( psz_duration, dur/1000000 );
291 o_value = [NSString stringWithUTF8String: psz_duration];
305 /*****************************************************************************
306 * VLCPlaylistWizard implementation
307 *****************************************************************************/
308 @implementation VLCPlaylistWizard
310 - (IBAction)reloadOutlineView
312 /* Only reload the outlineview if the wizard window is open since this can
313 be quite long on big playlists */
314 if( [[o_outline_view window] isVisible] )
316 [o_outline_view reloadData];
322 /*****************************************************************************
323 * extension to NSOutlineView's interface to fix compilation warnings
324 * and let us access these 2 functions properly
325 * this uses a private Apple-API, but works fine on all current OSX releases
326 * keep checking for compatiblity with future releases though
327 *****************************************************************************/
329 @interface NSOutlineView (UndocumentedSortImages)
330 + (NSImage *)_defaultTableHeaderSortImage;
331 + (NSImage *)_defaultTableHeaderReverseSortImage;
335 /*****************************************************************************
336 * VLCPlaylist implementation
337 *****************************************************************************/
338 @implementation VLCPlaylist
345 o_nodes_array = [[NSMutableArray alloc] init];
346 o_items_array = [[NSMutableArray alloc] init];
353 playlist_t * p_playlist = pl_Yield( VLCIntf );
357 [super awakeFromNib];
359 [o_outline_view setDoubleAction: @selector(playItem:)];
361 [o_outline_view registerForDraggedTypes:
362 [NSArray arrayWithObjects: NSFilenamesPboardType,
363 @"VLCPlaylistItemPboardType", nil]];
364 [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
366 /* This uses private Apple API which works fine until 10.5.
367 * We need to keep checking in the future!
368 * These methods are being added artificially to NSOutlineView's interface above */
369 o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
370 o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
372 o_tc_sortColumn = nil;
375 char ** ppsz_services = services_discovery_GetServicesNames( p_playlist, &ppsz_name );
378 vlc_object_release( p_playlist );
382 for( i = 0; ppsz_services[i]; i++ )
387 char * name = ppsz_name[i] ? ppsz_name[i] : ppsz_services[i];
388 /* Check whether to enable these menuitems */
389 b_enabled = playlist_IsServicesDiscoveryLoaded( p_playlist, ppsz_services[i] );
391 /* Create the menu entries used in the playlist menu */
392 o_lmi = [[o_mi_services submenu] addItemWithTitle:
393 [NSString stringWithUTF8String: name]
394 action: @selector(servicesChange:)
396 [o_lmi setTarget: self];
397 [o_lmi setRepresentedObject: [NSString stringWithUTF8String: ppsz_services[i]]];
398 if( b_enabled ) [o_lmi setState: NSOnState];
400 /* Create the menu entries for the main menu */
401 o_lmi = [[o_mm_mi_services submenu] addItemWithTitle:
402 [NSString stringWithUTF8String: name]
403 action: @selector(servicesChange:)
405 [o_lmi setTarget: self];
406 [o_lmi setRepresentedObject: [NSString stringWithUTF8String: ppsz_services[i]]];
407 if( b_enabled ) [o_lmi setState: NSOnState];
409 free( ppsz_services[i] );
410 free( ppsz_name[i] );
412 free( ppsz_services );
415 vlc_object_release( p_playlist );
418 - (void)searchfieldChanged:(NSNotification *)o_notification
420 [o_search_field setStringValue:[[o_notification object] stringValue]];
427 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
428 [o_mi_play setTitle: _NS("Play")];
429 [o_mi_delete setTitle: _NS("Delete")];
430 [o_mi_recursive_expand setTitle: _NS("Expand Node")];
431 [o_mi_selectall setTitle: _NS("Select All")];
432 [o_mi_info setTitle: _NS("Information")];
433 [o_mi_preparse setTitle: _NS("Get Stream Information")];
434 [o_mi_sort_name setTitle: _NS("Sort Node by Name")];
435 [o_mi_sort_author setTitle: _NS("Sort Node by Author")];
436 [o_mi_services setTitle: _NS("Services discovery")];
437 [o_status_field setStringValue: _NS("No items in the playlist")];
439 [o_search_field setToolTip: _NS("Search in Playlist")];
440 [o_mi_addNode setTitle: _NS("Add Folder to Playlist")];
442 [o_save_accessory_text setStringValue: _NS("File Format:")];
443 [[o_save_accessory_popup itemAtIndex:0] setTitle: _NS("Extended M3U")];
444 [[o_save_accessory_popup itemAtIndex:1] setTitle: _NS("XML Shareable Playlist Format (XSPF)")];
447 - (void)playlistUpdated
449 /* Clear indications of any existing column sorting */
450 for( unsigned int i = 0 ; i < [[o_outline_view tableColumns] count] ; i++ )
452 [o_outline_view setIndicatorImage:nil inTableColumn:
453 [[o_outline_view tableColumns] objectAtIndex:i]];
456 [o_outline_view setHighlightedTableColumn:nil];
457 o_tc_sortColumn = nil;
458 // TODO Find a way to keep the dict size to a minimum
459 //[o_outline_dict removeAllObjects];
460 [o_outline_view reloadData];
461 [[[[VLCMain sharedInstance] getWizard] getPlaylistWizard] reloadOutlineView];
462 [[[[VLCMain sharedInstance] getBookmarks] getDataTable] reloadData];
464 playlist_t *p_playlist = pl_Yield( VLCIntf );
466 if( playlist_CurrentSize( p_playlist ) >= 2 )
468 [o_status_field setStringValue: [NSString stringWithFormat:
470 playlist_CurrentSize( p_playlist )]];
474 if( playlist_IsEmpty( p_playlist ) )
475 [o_status_field setStringValue: _NS("No items in the playlist")];
477 [o_status_field setStringValue: _NS("1 item")];
479 vlc_object_release( p_playlist );
482 - (void)playModeUpdated
484 playlist_t *p_playlist = pl_Yield( VLCIntf );
486 bool loop = var_GetBool( p_playlist, "loop" );
487 bool repeat = var_GetBool( p_playlist, "repeat" );
489 [[[VLCMain sharedInstance] getControls] repeatOne];
491 [[[VLCMain sharedInstance] getControls] repeatAll];
493 [[[VLCMain sharedInstance] getControls] repeatOff];
495 [[[VLCMain sharedInstance] getControls] shuffle];
497 vlc_object_release( p_playlist );
500 - (void)updateRowSelection
505 playlist_t *p_playlist = pl_Yield( VLCIntf );
506 playlist_item_t *p_item, *p_temp_item;
507 NSMutableArray *o_array = [NSMutableArray array];
509 p_item = p_playlist->status.p_item;
512 vlc_object_release(p_playlist);
516 p_temp_item = p_item;
517 while( p_temp_item->p_parent )
519 [o_array insertObject: [NSValue valueWithPointer: p_temp_item] atIndex: 0];
520 p_temp_item = p_temp_item->p_parent;
523 for( j = 0; j < [o_array count] - 1; j++ )
526 if( ( o_item = [o_outline_dict objectForKey:
527 [NSString stringWithFormat: @"%p",
528 [[o_array objectAtIndex:j] pointerValue]]] ) != nil )
530 [o_outline_view expandItem: o_item];
535 vlc_object_release( p_playlist );
537 /* update our info-panel to reflect the new item */
538 [[[VLCMain sharedInstance] getInfo] updatePanel];
541 /* Check if p_item is a child of p_node recursively. We need to check the item
542 existence first since OSX sometimes tries to redraw items that have been
543 deleted. We don't do it when not required since this verification takes
544 quite a long time on big playlists (yes, pretty hacky). */
545 - (BOOL)isItem: (playlist_item_t *)p_item
546 inNode: (playlist_item_t *)p_node
547 checkItemExistence:(BOOL)b_check
550 playlist_t * p_playlist = pl_Yield( VLCIntf );
551 playlist_item_t *p_temp_item = p_item;
553 if( p_node == p_item )
555 vlc_object_release(p_playlist);
559 if( p_node->i_children < 1)
561 vlc_object_release(p_playlist);
572 /* Since outlineView: willDisplayCell:... may call this function with
573 p_items that don't exist anymore, first check if the item is still
574 in the playlist. Any cleaner solution welcomed. */
575 for( i = 0; i < p_playlist->all_items.i_size; i++ )
577 if( ARRAY_VAL( p_playlist->all_items, i) == p_item ) break;
578 else if ( i == p_playlist->all_items.i_size - 1 )
580 vlc_object_release( p_playlist );
589 p_temp_item = p_temp_item->p_parent;
590 if( p_temp_item == p_node )
593 vlc_object_release( p_playlist );
600 vlc_object_release( p_playlist );
604 /* This method is usefull for instance to remove the selected children of an
605 already selected node */
606 - (void)removeItemsFrom:(id)o_items ifChildrenOf:(id)o_nodes
609 for( i = 0 ; i < [o_items count] ; i++ )
611 for ( j = 0 ; j < [o_nodes count] ; j++ )
613 if( o_items == o_nodes)
615 if( j == i ) continue;
617 if( [self isItem: [[o_items objectAtIndex:i] pointerValue]
618 inNode: [[o_nodes objectAtIndex:j] pointerValue]
619 checkItemExistence: NO] )
621 [o_items removeObjectAtIndex:i];
622 /* We need to execute the next iteration with the same index
623 since the current item has been deleted */
632 - (IBAction)savePlaylist:(id)sender
634 playlist_t * p_playlist = pl_Yield( VLCIntf );
636 NSSavePanel *o_save_panel = [NSSavePanel savePanel];
637 NSString * o_name = [NSString stringWithFormat: @"%@", _NS("Untitled")];
639 //[o_save_panel setAllowedFileTypes: [NSArray arrayWithObjects: @"m3u", @"xpf", nil] ];
640 [o_save_panel setTitle: _NS("Save Playlist")];
641 [o_save_panel setPrompt: _NS("Save")];
642 [o_save_panel setAccessoryView: o_save_accessory_view];
644 if( [o_save_panel runModalForDirectory: nil
645 file: o_name] == NSOKButton )
647 NSString *o_filename = [o_save_panel filename];
649 if( [o_save_accessory_popup indexOfSelectedItem] == 1 )
651 NSString * o_real_filename;
653 range.location = [o_filename length] - [@".xspf" length];
654 range.length = [@".xspf" length];
656 if( [o_filename compare:@".xspf" options: NSCaseInsensitiveSearch
657 range: range] != NSOrderedSame )
659 o_real_filename = [NSString stringWithFormat: @"%@.xspf", o_filename];
663 o_real_filename = o_filename;
665 playlist_Export( p_playlist,
666 [o_real_filename fileSystemRepresentation],
667 p_playlist->p_local_category, "export-xspf" );
671 NSString * o_real_filename;
673 range.location = [o_filename length] - [@".m3u" length];
674 range.length = [@".m3u" length];
676 if( [o_filename compare:@".m3u" options: NSCaseInsensitiveSearch
677 range: range] != NSOrderedSame )
679 o_real_filename = [NSString stringWithFormat: @"%@.m3u", 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-m3u" );
690 vlc_object_release( p_playlist );
693 /* When called retrieves the selected outlineview row and plays that node or item */
694 - (IBAction)playItem:(id)sender
696 intf_thread_t * p_intf = VLCIntf;
697 playlist_t * p_playlist = pl_Yield( p_intf );
699 playlist_item_t *p_item;
700 playlist_item_t *p_node = NULL;
702 p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
706 if( p_item->i_children == -1 )
708 p_node = p_item->p_parent;
714 if( p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1 )
716 p_item = p_node->pp_children[0];
723 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, false, p_node, p_item );
725 vlc_object_release( p_playlist );
728 /* When called retrieves the selected outlineview row and plays that node or item */
729 - (IBAction)preparseItem:(id)sender
732 NSMutableArray *o_to_preparse;
733 intf_thread_t * p_intf = VLCIntf;
734 playlist_t * p_playlist = pl_Yield( p_intf );
736 o_to_preparse = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
737 i_count = [o_to_preparse count];
741 playlist_item_t *p_item = NULL;
743 for( i = 0; i < i_count; i++ )
745 o_number = [o_to_preparse lastObject];
746 i_row = [o_number intValue];
747 p_item = [[o_outline_view itemAtRow:i_row] pointerValue];
748 [o_to_preparse removeObject: o_number];
749 [o_outline_view deselectRow: i_row];
753 if( p_item->i_children == -1 )
755 playlist_PreparseEnqueue( p_playlist, p_item->p_input );
759 msg_Dbg( p_intf, "preparsing nodes not implemented" );
763 vlc_object_release( p_playlist );
764 [self playlistUpdated];
767 - (IBAction)servicesChange:(id)sender
769 NSMenuItem *o_mi = (NSMenuItem *)sender;
770 NSString *o_string = [o_mi representedObject];
771 playlist_t * p_playlist = pl_Yield( VLCIntf );
772 if( !playlist_IsServicesDiscoveryLoaded( p_playlist, [o_string UTF8String] ) )
773 playlist_ServicesDiscoveryAdd( p_playlist, [o_string UTF8String] );
775 playlist_ServicesDiscoveryRemove( p_playlist, [o_string UTF8String] );
777 [o_mi setState: playlist_IsServicesDiscoveryLoaded( p_playlist,
778 [o_string UTF8String] ) ? YES : NO];
780 vlc_object_release( p_playlist );
781 [self playlistUpdated];
785 - (IBAction)selectAll:(id)sender
787 [o_outline_view selectAll: nil];
790 - (IBAction)deleteItem:(id)sender
793 NSMutableArray *o_to_delete;
796 playlist_t * p_playlist;
797 intf_thread_t * p_intf = VLCIntf;
799 o_to_delete = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
800 i_count = [o_to_delete count];
802 p_playlist = pl_Yield( p_intf );
805 for( int i = 0; i < i_count; i++ )
807 o_number = [o_to_delete lastObject];
808 i_row = [o_number intValue];
809 id o_item = [o_outline_view itemAtRow: i_row];
810 playlist_item_t *p_item = [o_item pointerValue];
812 msg_Dbg( p_intf, "deleting item %i (of %i) with id \"%i\", pointerValue \"%p\" and %i children", i+1, i_count,
813 p_item->p_input->i_id, [o_item pointerValue], p_item->i_children +1 );
815 [o_to_delete removeObject: o_number];
816 [o_outline_view deselectRow: i_row];
818 if( p_item->i_children != -1 )
819 //is a node and not an item
821 if( p_playlist->status.i_status != PLAYLIST_STOPPED &&
822 [self isItem: p_playlist->status.p_item inNode:
823 ((playlist_item_t *)[o_item pointerValue])
824 checkItemExistence: NO] == YES )
825 // if current item is in selected node and is playing then stop playlist
826 playlist_Stop( p_playlist );
828 playlist_NodeDelete( p_playlist, p_item, true, false );
831 playlist_DeleteFromInput( p_playlist, p_item->p_input->i_id, true );
835 [self playlistUpdated];
836 vlc_object_release( p_playlist );
839 - (IBAction)sortNodeByName:(id)sender
841 [self sortNode: SORT_TITLE];
844 - (IBAction)sortNodeByAuthor:(id)sender
846 [self sortNode: SORT_ARTIST];
849 - (void)sortNode:(int)i_mode
851 playlist_t * p_playlist = pl_Yield( VLCIntf );
852 playlist_item_t * p_item;
854 if( [o_outline_view selectedRow] > -1 )
856 p_item = [[o_outline_view itemAtRow: [o_outline_view selectedRow]] pointerValue];
859 /*If no item is selected, sort the whole playlist*/
861 p_item = p_playlist->p_root_category;
864 if( p_item->i_children > -1 ) // the item is a node
867 playlist_RecursiveNodeSort( p_playlist, p_item, i_mode, ORDER_NORMAL );
873 playlist_RecursiveNodeSort( p_playlist,
874 p_item->p_parent, i_mode, ORDER_NORMAL );
877 vlc_object_release( p_playlist );
878 [self playlistUpdated];
881 - (input_item_t *)createItem:(NSDictionary *)o_one_item
883 intf_thread_t * p_intf = VLCIntf;
884 playlist_t * p_playlist = pl_Yield( p_intf );
886 input_item_t *p_input;
888 BOOL b_rem = FALSE, b_dir = FALSE;
889 NSString *o_uri, *o_name;
894 o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
895 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
896 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
898 /* Find the name for a disc entry (i know, can you believe the trouble?) */
899 if( ( !o_name || [o_name isEqualToString:@""] ) && [o_uri rangeOfString: @"/dev/"].location != NSNotFound )
901 int i_count, i_index;
902 struct statfs *mounts = NULL;
904 i_count = getmntinfo (&mounts, MNT_NOWAIT);
905 /* getmntinfo returns a pointer to static data. Do not free. */
906 for( i_index = 0 ; i_index < i_count; i_index++ )
908 NSMutableString *o_temp, *o_temp2;
909 o_temp = [NSMutableString stringWithString: o_uri];
910 o_temp2 = [NSMutableString stringWithUTF8String: mounts[i_index].f_mntfromname];
911 [o_temp replaceOccurrencesOfString: @"/dev/rdisk" withString: @"/dev/disk" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
912 [o_temp2 replaceOccurrencesOfString: @"s0" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp2 length]) ];
913 [o_temp2 replaceOccurrencesOfString: @"s1" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp2 length]) ];
915 if( strstr( [o_temp fileSystemRepresentation], [o_temp2 fileSystemRepresentation] ) != NULL )
917 o_name = [[NSFileManager defaultManager] displayNameAtPath: [NSString stringWithUTF8String:mounts[i_index].f_mntonname]];
921 /* If no name, then make a guess */
922 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
924 if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
925 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
926 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
928 /* All of this is to make sure CD's play when you D&D them on VLC */
929 /* Converts mountpoint to a /dev file */
932 NSMutableString *o_temp;
934 buf = (struct statfs *) malloc (sizeof(struct statfs));
935 statfs( [o_uri fileSystemRepresentation], buf );
936 psz_dev = strdup(buf->f_mntfromname);
937 o_temp = [NSMutableString stringWithUTF8String: psz_dev ];
938 [o_temp replaceOccurrencesOfString: @"/dev/disk" withString: @"/dev/rdisk" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
939 [o_temp replaceOccurrencesOfString: @"s0" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
940 [o_temp replaceOccurrencesOfString: @"s1" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
944 p_input = input_ItemNew( p_playlist, [o_uri fileSystemRepresentation], [o_name UTF8String] );
950 for( i = 0; i < (int)[o_options count]; i++ )
952 input_ItemAddOption( p_input, strdup( [[o_options objectAtIndex:i] UTF8String] ) );
956 /* Recent documents menu */
957 o_true_file = [NSURL fileURLWithPath: o_uri];
958 if( o_true_file != nil && (BOOL)config_GetInt( p_playlist, "macosx-recentitems" ) == YES )
960 [[NSDocumentController sharedDocumentController]
961 noteNewRecentDocumentURL: o_true_file];
964 vlc_object_release( p_playlist );
968 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
971 playlist_t * p_playlist = pl_Yield( VLCIntf );
973 for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
975 input_item_t *p_input;
976 NSDictionary *o_one_item;
979 o_one_item = [o_array objectAtIndex: i_item];
980 p_input = [self createItem: o_one_item];
987 /* FIXME: playlist_AddInput() can fail */
988 playlist_AddInput( p_playlist, p_input, PLAYLIST_INSERT,
989 i_position == -1 ? PLAYLIST_END : i_position + i_item, true,
992 if( i_item == 0 && !b_enqueue )
994 playlist_item_t *p_item;
995 p_item = playlist_ItemGetByInput( p_playlist, p_input, true );
996 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, true, NULL, p_item );
998 vlc_gc_decref( p_input );
1000 [self playlistUpdated];
1001 vlc_object_release( p_playlist );
1004 - (void)appendNodeArray:(NSArray*)o_array inNode:(playlist_item_t *)p_node atPos:(int)i_position enqueue:(BOOL)b_enqueue
1007 playlist_t * p_playlist = pl_Yield( VLCIntf );
1009 for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
1011 input_item_t *p_input;
1012 NSDictionary *o_one_item;
1015 o_one_item = [o_array objectAtIndex: i_item];
1016 p_input = [self createItem: o_one_item];
1023 /* FIXME: playlist_NodeAddInput() can fail */
1024 playlist_NodeAddInput( p_playlist, p_input, p_node,
1027 PLAYLIST_END : i_position + i_item, false );
1030 if( i_item == 0 && !b_enqueue )
1032 playlist_item_t *p_item;
1033 p_item = playlist_ItemGetByInput( p_playlist, p_input, true );
1034 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, true, NULL, p_item );
1036 vlc_gc_decref( p_input );
1038 [self playlistUpdated];
1039 vlc_object_release( p_playlist );
1042 - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
1044 playlist_t *p_playlist = pl_Yield( VLCIntf );
1045 playlist_item_t *p_selected_item;
1046 int i_current, i_selected_row;
1048 i_selected_row = [o_outline_view selectedRow];
1049 if (i_selected_row < 0)
1052 p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow:
1053 i_selected_row] pointerValue];
1055 for( i_current = 0; i_current < p_item->i_children ; i_current++ )
1058 NSString *o_current_name, *o_current_author;
1061 o_current_name = [NSString stringWithUTF8String:
1062 p_item->pp_children[i_current]->p_input->psz_name];
1063 psz_temp = input_ItemGetInfo( p_item->p_input ,
1064 _("Meta-information"),_("Artist") );
1065 o_current_author = [NSString stringWithUTF8String: psz_temp];
1069 if( p_selected_item == p_item->pp_children[i_current] &&
1070 b_selected_item_met == NO )
1072 b_selected_item_met = YES;
1074 else if( p_selected_item == p_item->pp_children[i_current] &&
1075 b_selected_item_met == YES )
1077 vlc_object_release( p_playlist );
1080 else if( b_selected_item_met == YES &&
1081 ( [o_current_name rangeOfString:[o_search_field
1082 stringValue] options:NSCaseInsensitiveSearch].length ||
1083 [o_current_author rangeOfString:[o_search_field
1084 stringValue] options:NSCaseInsensitiveSearch].length ) )
1086 vlc_object_release( p_playlist );
1087 /*Adds the parent items in the result array as well, so that we can
1089 return [NSMutableArray arrayWithObject: [NSValue
1090 valueWithPointer: p_item->pp_children[i_current]]];
1092 if( p_item->pp_children[i_current]->i_children > 0 )
1094 id o_result = [self subSearchItem:
1095 p_item->pp_children[i_current]];
1096 if( o_result != NULL )
1098 vlc_object_release( p_playlist );
1099 [o_result insertObject: [NSValue valueWithPointer:
1100 p_item->pp_children[i_current]] atIndex:0];
1105 vlc_object_release( p_playlist );
1109 - (IBAction)searchItem:(id)sender
1111 playlist_t * p_playlist = pl_Yield( VLCIntf );
1117 b_selected_item_met = NO;
1119 /*First, only search after the selected item:*
1120 *(b_selected_item_met = NO) */
1121 o_result = [self subSearchItem:p_playlist->p_root_category];
1122 if( o_result == NULL )
1124 /* If the first search failed, search again from the beginning */
1125 o_result = [self subSearchItem:p_playlist->p_root_category];
1127 if( o_result != NULL )
1130 if( [[o_result objectAtIndex: 0] pointerValue] ==
1131 p_playlist->p_local_category )
1136 for( i = i_start ; i < [o_result count] - 1 ; i++ )
1138 [o_outline_view expandItem: [o_outline_dict objectForKey:
1139 [NSString stringWithFormat: @"%p",
1140 [[o_result objectAtIndex: i] pointerValue]]]];
1142 i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
1143 [NSString stringWithFormat: @"%p",
1144 [[o_result objectAtIndex: [o_result count] - 1 ]
1149 [o_outline_view selectRow:i_row byExtendingSelection: NO];
1150 [o_outline_view scrollRowToVisible: i_row];
1152 vlc_object_release( p_playlist );
1155 - (IBAction)recursiveExpandNode:(id)sender
1157 id o_item = [o_outline_view itemAtRow: [o_outline_view selectedRow]];
1158 playlist_item_t *p_item = (playlist_item_t *)[o_item pointerValue];
1160 if( ![[o_outline_view dataSource] outlineView: o_outline_view
1161 isItemExpandable: o_item] )
1163 o_item = [o_outline_dict objectForKey: [NSString
1164 stringWithFormat: @"%p", p_item->p_parent]];
1167 /* We need to collapse the node first, since OSX refuses to recursively
1168 expand an already expanded node, even if children nodes are collapsed. */
1169 [o_outline_view collapseItem: o_item collapseChildren: YES];
1170 [o_outline_view expandItem: o_item expandChildren: YES];
1173 - (NSMenu *)menuForEvent:(NSEvent *)o_event
1179 pt = [o_outline_view convertPoint: [o_event locationInWindow]
1181 b_item_sel = ( [o_outline_view rowAtPoint: pt] != -1 &&
1182 [o_outline_view selectedRow] != -1 );
1183 b_rows = [o_outline_view numberOfRows] != 0;
1185 [o_mi_play setEnabled: b_item_sel];
1186 [o_mi_delete setEnabled: b_item_sel];
1187 [o_mi_selectall setEnabled: b_rows];
1188 [o_mi_info setEnabled: b_item_sel];
1189 [o_mi_preparse setEnabled: b_item_sel];
1190 [o_mi_recursive_expand setEnabled: b_item_sel];
1191 [o_mi_sort_name setEnabled: b_item_sel];
1192 [o_mi_sort_author setEnabled: b_item_sel];
1194 return( o_ctx_menu );
1197 - (void)outlineView: (NSTableView*)o_tv
1198 didClickTableColumn:(NSTableColumn *)o_tc
1200 int i_mode = 0, i_type;
1201 intf_thread_t *p_intf = VLCIntf;
1203 playlist_t *p_playlist = pl_Yield( p_intf );
1205 /* Check whether the selected table column header corresponds to a
1206 sortable table column*/
1207 if( !( o_tc == o_tc_name || o_tc == o_tc_author ) )
1209 vlc_object_release( p_playlist );
1213 if( o_tc_sortColumn == o_tc )
1215 b_isSortDescending = !b_isSortDescending;
1219 b_isSortDescending = false;
1222 if( o_tc == o_tc_name )
1224 i_mode = SORT_TITLE;
1226 else if( o_tc == o_tc_author )
1228 i_mode = SORT_ARTIST;
1231 if( b_isSortDescending )
1233 i_type = ORDER_REVERSE;
1237 i_type = ORDER_NORMAL;
1240 vlc_object_lock( p_playlist );
1241 playlist_RecursiveNodeSort( p_playlist, p_playlist->p_root_category, i_mode, i_type );
1242 vlc_object_unlock( p_playlist );
1244 vlc_object_release( p_playlist );
1245 [self playlistUpdated];
1247 o_tc_sortColumn = o_tc;
1248 [o_outline_view setHighlightedTableColumn:o_tc];
1250 if( b_isSortDescending )
1252 [o_outline_view setIndicatorImage:o_descendingSortingImage
1253 inTableColumn:o_tc];
1257 [o_outline_view setIndicatorImage:o_ascendingSortingImage
1258 inTableColumn:o_tc];
1263 - (void)outlineView:(NSOutlineView *)outlineView
1264 willDisplayCell:(id)cell
1265 forTableColumn:(NSTableColumn *)tableColumn
1268 playlist_t *p_playlist = pl_Yield( VLCIntf );
1272 o_playing_item = [o_outline_dict objectForKey:
1273 [NSString stringWithFormat:@"%p", p_playlist->status.p_item]];
1275 if( [self isItem: [o_playing_item pointerValue] inNode:
1276 [item pointerValue] checkItemExistence: YES]
1277 || [o_playing_item isEqual: item] )
1279 [cell setFont: [[NSFontManager sharedFontManager] convertFont:[cell font] toHaveTrait:NSBoldFontMask]];
1283 [cell setFont: [[NSFontManager sharedFontManager] convertFont:[cell font] toNotHaveTrait:NSBoldFontMask]];
1285 vlc_object_release( p_playlist );
1288 - (IBAction)addNode:(id)sender
1290 /* we have to create a new thread here because otherwise we would block the
1291 * interface since the interaction-stuff and this code would run in the same
1293 [NSThread detachNewThreadSelector: @selector(addNodeThreadedly)
1294 toTarget: self withObject:nil];
1295 [self playlistUpdated];
1298 - (void)addNodeThreadedly
1300 NSAutoreleasePool * ourPool = [[NSAutoreleasePool alloc] init];
1302 /* simply adds a new node to the end of the playlist */
1303 playlist_t * p_playlist = pl_Yield( VLCIntf );
1304 vlc_thread_set_priority( p_playlist, VLC_THREAD_PRIORITY_LOW );
1307 char *psz_name = NULL;
1308 playlist_item_t * p_item;
1309 ret_v = intf_UserStringInput( p_playlist, _("New Node"),
1310 _("Please enter a name for the new node."), &psz_name );
1312 if( ret_v != DIALOG_CANCELLED && psz_name && *psz_name )
1313 p_item = playlist_NodeCreate( p_playlist, psz_name,
1314 p_playlist->p_local_category, 0, NULL );
1315 else if(! config_GetInt( p_playlist, "interact" ) )
1317 /* in case that the interaction is disabled, just give it a bogus name */
1318 p_item = playlist_NodeCreate( p_playlist, _("Empty Folder"),
1319 p_playlist->p_local_category, 0, NULL );
1323 msg_Warn( VLCIntf, "node creation failed or cancelled by user" );
1325 vlc_object_release( p_playlist );
1331 @implementation VLCPlaylist (NSOutlineViewDataSource)
1333 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
1335 id o_value = [super outlineView: outlineView child: index ofItem: item];
1336 playlist_t *p_playlist = pl_Yield( VLCIntf );
1338 if( playlist_CurrentSize( p_playlist ) >= 2 )
1340 [o_status_field setStringValue: [NSString stringWithFormat:
1342 playlist_CurrentSize( p_playlist )]];
1346 if( playlist_IsEmpty( p_playlist ) )
1348 [o_status_field setStringValue: _NS("No items in the playlist")];
1352 [o_status_field setStringValue: _NS("1 item")];
1355 vlc_object_release( p_playlist );
1357 [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p",
1358 [o_value pointerValue]]];
1363 /* Required for drag & drop and reordering */
1364 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
1367 playlist_t *p_playlist = pl_Yield( VLCIntf );
1369 /* First remove the items that were moved during the last drag & drop
1371 [o_items_array removeAllObjects];
1372 [o_nodes_array removeAllObjects];
1374 for( i = 0 ; i < [items count] ; i++ )
1376 id o_item = [items objectAtIndex: i];
1378 /* Refuse to move items that are not in the General Node
1379 (Service Discovery) */
1380 if( ![self isItem: [o_item pointerValue] inNode:
1381 p_playlist->p_local_category checkItemExistence: NO] &&
1382 ( var_CreateGetBool( p_playlist, "media-library" ) &&
1383 ![self isItem: [o_item pointerValue] inNode:
1384 p_playlist->p_ml_category checkItemExistence: NO]) )
1386 vlc_object_release(p_playlist);
1389 /* Fill the items and nodes to move in 2 different arrays */
1390 if( ((playlist_item_t *)[o_item pointerValue])->i_children > 0 )
1391 [o_nodes_array addObject: o_item];
1393 [o_items_array addObject: o_item];
1396 /* Now we need to check if there are selected items that are in already
1397 selected nodes. In that case, we only want to move the nodes */
1398 [self removeItemsFrom: o_nodes_array ifChildrenOf: o_nodes_array];
1399 [self removeItemsFrom: o_items_array ifChildrenOf: o_nodes_array];
1401 /* We add the "VLCPlaylistItemPboardType" type to be able to recognize
1402 a Drop operation coming from the playlist. */
1404 [pboard declareTypes: [NSArray arrayWithObjects:
1405 @"VLCPlaylistItemPboardType", nil] owner: self];
1406 [pboard setData:[NSData data] forType:@"VLCPlaylistItemPboardType"];
1408 vlc_object_release(p_playlist);
1412 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
1414 playlist_t *p_playlist = pl_Yield( VLCIntf );
1415 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1417 if( !p_playlist ) return NSDragOperationNone;
1419 /* Dropping ON items is not allowed if item is not a node */
1422 if( index == NSOutlineViewDropOnItemIndex &&
1423 ((playlist_item_t *)[item pointerValue])->i_children == -1 )
1425 vlc_object_release( p_playlist );
1426 return NSDragOperationNone;
1430 /* Don't allow on drop on playlist root element's child */
1431 if( !item && index != NSOutlineViewDropOnItemIndex)
1433 vlc_object_release( p_playlist );
1434 return NSDragOperationNone;
1437 /* We refuse to drop an item in anything else than a child of the General
1438 Node. We still accept items that would be root nodes of the outlineview
1439 however, to allow drop in an empty playlist. */
1440 if( !( ([self isItem: [item pointerValue] inNode: p_playlist->p_local_category checkItemExistence: NO] ||
1441 ( var_CreateGetBool( p_playlist, "media-library" ) && [self isItem: [item pointerValue] inNode: p_playlist->p_ml_category checkItemExistence: NO] ) ) || item == nil ) )
1443 vlc_object_release( p_playlist );
1444 return NSDragOperationNone;
1447 /* Drop from the Playlist */
1448 if( [[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] )
1451 for( i = 0 ; i < [o_nodes_array count] ; i++ )
1453 /* We refuse to Drop in a child of an item we are moving */
1454 if( [self isItem: [item pointerValue] inNode:
1455 [[o_nodes_array objectAtIndex: i] pointerValue]
1456 checkItemExistence: NO] )
1458 vlc_object_release( p_playlist );
1459 return NSDragOperationNone;
1462 vlc_object_release( p_playlist );
1463 return NSDragOperationMove;
1466 /* Drop from the Finder */
1467 else if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1469 vlc_object_release( p_playlist );
1470 return NSDragOperationGeneric;
1472 vlc_object_release( p_playlist );
1473 return NSDragOperationNone;
1476 - (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(int)index
1478 playlist_t * p_playlist = pl_Yield( VLCIntf );
1479 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1481 /* Drag & Drop inside the playlist */
1482 if( [[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] )
1484 int i_row, i_removed_from_node = 0;
1486 playlist_item_t *p_new_parent, *p_item = NULL;
1487 NSArray *o_all_items = [o_nodes_array arrayByAddingObjectsFromArray:
1489 /* If the item is to be dropped as root item of the outline, make it a
1490 child of the General node.
1491 Else, choose the proposed parent as parent. */
1492 if( item == nil ) p_new_parent = p_playlist->p_local_category;
1493 else p_new_parent = [item pointerValue];
1495 /* Make sure the proposed parent is a node.
1496 (This should never be true) */
1497 if( p_new_parent->i_children < 0 )
1499 vlc_object_release( p_playlist );
1503 for( i = 0; i < [o_all_items count]; i++ )
1505 playlist_item_t *p_old_parent = NULL;
1506 int i_old_index = 0;
1508 p_item = [[o_all_items objectAtIndex:i] pointerValue];
1509 p_old_parent = p_item->p_parent;
1512 /* We may need the old index later */
1513 if( p_new_parent == p_old_parent )
1516 for( j = 0; j < p_old_parent->i_children; j++ )
1518 if( p_old_parent->pp_children[j] == p_item )
1527 // Actually detach the item from the old position
1528 if( playlist_NodeRemoveItem( p_playlist, p_item, p_old_parent ) ==
1532 /* Calculate the new index */
1535 /* If we move the item in the same node, we need to take into
1536 account that one item will be deleted */
1539 if ((p_new_parent == p_old_parent &&
1540 i_old_index < index + (int)i) )
1542 i_removed_from_node++;
1544 i_new_index = index + i - i_removed_from_node;
1546 // Reattach the item to the new position
1547 playlist_NodeInsert( p_playlist, p_item, p_new_parent, i_new_index );
1551 [self playlistUpdated];
1552 i_row = [o_outline_view rowForItem:[o_outline_dict
1553 objectForKey:[NSString stringWithFormat: @"%p",
1554 [[o_all_items objectAtIndex: 0] pointerValue]]]];
1558 i_row = [o_outline_view rowForItem:[o_outline_dict
1559 objectForKey:[NSString stringWithFormat: @"%p", p_new_parent]]];
1562 [o_outline_view deselectAll: self];
1563 [o_outline_view selectRow: i_row byExtendingSelection: NO];
1564 [o_outline_view scrollRowToVisible: i_row];
1566 vlc_object_release( p_playlist );
1570 else if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1573 playlist_item_t *p_node = [item pointerValue];
1575 NSArray *o_array = [NSArray array];
1576 NSArray *o_values = [[o_pasteboard propertyListForType:
1577 NSFilenamesPboardType]
1578 sortedArrayUsingSelector:
1579 @selector(caseInsensitiveCompare:)];
1581 for( i = 0; i < (int)[o_values count]; i++)
1583 NSDictionary *o_dic;
1584 o_dic = [NSDictionary dictionaryWithObject:[o_values
1585 objectAtIndex:i] forKey:@"ITEM_URL"];
1586 o_array = [o_array arrayByAddingObject: o_dic];
1591 [self appendArray: o_array atPos: index enqueue: YES];
1593 /* This should never occur */
1594 else if( p_node->i_children == -1 )
1596 vlc_object_release( p_playlist );
1601 [self appendNodeArray: o_array inNode: p_node
1602 atPos: index enqueue: YES];
1604 vlc_object_release( p_playlist );
1607 vlc_object_release( p_playlist );