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 NSAssert( 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];
295 o_value = @"-:--:--";
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++ )
389 char * name = ppsz_name[i] ? ppsz_name[i] : ppsz_services[i];
390 /* Check whether to enable these menuitems */
391 b_enabled = playlist_IsServicesDiscoveryLoaded( p_playlist, objectname );
393 /* Create the menu entries used in the playlist menu */
394 o_lmi = [[o_mi_services submenu] addItemWithTitle:
395 [NSString stringWithUTF8String: name]
396 action: @selector(servicesChange:)
398 [o_lmi setTarget: self];
399 [o_lmi setRepresentedObject: [NSString stringWithUTF8String: ppsz_services[i]]];
400 if( b_enabled ) [o_lmi setState: NSOnState];
402 /* Create the menu entries for the main menu */
403 o_lmi = [[o_mm_mi_services submenu] addItemWithTitle:
404 [NSString stringWithUTF8String: name]
405 action: @selector(servicesChange:)
407 [o_lmi setTarget: self];
408 [o_lmi setRepresentedObject: [NSString stringWithUTF8String: ppsz_services[i]]];
409 if( b_enabled ) [o_lmi setState: NSOnState];
411 free( ppsz_services[i] );
412 free( ppsz_name[i] );
414 free( ppsz_services );
417 vlc_object_release( p_playlist );
420 - (void)searchfieldChanged:(NSNotification *)o_notification
422 [o_search_field setStringValue:[[o_notification object] stringValue]];
429 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
430 [o_mi_play setTitle: _NS("Play")];
431 [o_mi_delete setTitle: _NS("Delete")];
432 [o_mi_recursive_expand setTitle: _NS("Expand Node")];
433 [o_mi_selectall setTitle: _NS("Select All")];
434 [o_mi_info setTitle: _NS("Information")];
435 [o_mi_preparse setTitle: _NS("Get Stream Information")];
436 [o_mi_sort_name setTitle: _NS("Sort Node by Name")];
437 [o_mi_sort_author setTitle: _NS("Sort Node by Author")];
438 [o_mi_services setTitle: _NS("Services discovery")];
439 [o_status_field setStringValue: _NS("No items in the playlist")];
441 [o_search_field setToolTip: _NS("Search in Playlist")];
442 [o_mi_addNode setTitle: _NS("Add Folder to Playlist")];
444 [o_save_accessory_text setStringValue: _NS("File Format:")];
445 [[o_save_accessory_popup itemAtIndex:0] setTitle: _NS("Extended M3U")];
446 [[o_save_accessory_popup itemAtIndex:1] setTitle: _NS("XML Shareable Playlist Format (XSPF)")];
449 - (void)playlistUpdated
451 /* Clear indications of any existing column sorting */
452 for( unsigned int i = 0 ; i < [[o_outline_view tableColumns] count] ; i++ )
454 [o_outline_view setIndicatorImage:nil inTableColumn:
455 [[o_outline_view tableColumns] objectAtIndex:i]];
458 [o_outline_view setHighlightedTableColumn:nil];
459 o_tc_sortColumn = nil;
460 // TODO Find a way to keep the dict size to a minimum
461 //[o_outline_dict removeAllObjects];
462 [o_outline_view reloadData];
463 [[[[VLCMain sharedInstance] getWizard] getPlaylistWizard] reloadOutlineView];
464 [[[[VLCMain sharedInstance] getBookmarks] getDataTable] reloadData];
466 playlist_t *p_playlist = pl_Yield( VLCIntf );
468 if( playlist_CurrentSize( p_playlist ) >= 2 )
470 [o_status_field setStringValue: [NSString stringWithFormat:
471 _NS("%i items in the playlist"),
472 playlist_CurrentSize( p_playlist )]];
476 if( playlist_IsEmpty( p_playlist ) )
477 [o_status_field setStringValue: _NS("No items in the playlist")];
479 [o_status_field setStringValue: _NS("1 item in the playlist")];
481 vlc_object_release( p_playlist );
484 - (void)playModeUpdated
486 playlist_t *p_playlist = pl_Yield( VLCIntf );
488 bool loop = var_GetBool( p_playlist, "loop" );
489 bool repeat = var_GetBool( p_playlist, "repeat" );
491 [[[VLCMain sharedInstance] getControls] repeatOne];
493 [[[VLCMain sharedInstance] getControls] repeatAll];
495 [[[VLCMain sharedInstance] getControls] repeatOff];
497 [[[VLCMain sharedInstance] getControls] shuffle];
499 vlc_object_release( p_playlist );
502 - (void)updateRowSelection
507 playlist_t *p_playlist = pl_Yield( VLCIntf );
508 playlist_item_t *p_item, *p_temp_item;
509 NSMutableArray *o_array = [NSMutableArray array];
511 p_item = p_playlist->status.p_item;
514 vlc_object_release(p_playlist);
518 p_temp_item = p_item;
519 while( p_temp_item->p_parent )
521 [o_array insertObject: [NSValue valueWithPointer: p_temp_item] atIndex: 0];
522 p_temp_item = p_temp_item->p_parent;
525 for( j = 0; j < [o_array count] - 1; j++ )
528 if( ( o_item = [o_outline_dict objectForKey:
529 [NSString stringWithFormat: @"%p",
530 [[o_array objectAtIndex:j] pointerValue]]] ) != nil )
532 [o_outline_view expandItem: o_item];
537 vlc_object_release( p_playlist );
539 /* update our info-panel to reflect the new item */
540 [[[VLCMain sharedInstance] getInfo] updatePanel];
543 /* Check if p_item is a child of p_node recursively. We need to check the item
544 existence first since OSX sometimes tries to redraw items that have been
545 deleted. We don't do it when not required since this verification takes
546 quite a long time on big playlists (yes, pretty hacky). */
547 - (BOOL)isItem: (playlist_item_t *)p_item
548 inNode: (playlist_item_t *)p_node
549 checkItemExistence:(BOOL)b_check
552 playlist_t * p_playlist = pl_Yield( VLCIntf );
553 playlist_item_t *p_temp_item = p_item;
555 if( p_node == p_item )
557 vlc_object_release(p_playlist);
561 if( p_node->i_children < 1)
563 vlc_object_release(p_playlist);
574 /* Since outlineView: willDisplayCell:... may call this function with
575 p_items that don't exist anymore, first check if the item is still
576 in the playlist. Any cleaner solution welcomed. */
577 for( i = 0; i < p_playlist->all_items.i_size; i++ )
579 if( ARRAY_VAL( p_playlist->all_items, i) == p_item ) break;
580 else if ( i == p_playlist->all_items.i_size - 1 )
582 vlc_object_release( p_playlist );
591 p_temp_item = p_temp_item->p_parent;
592 if( p_temp_item == p_node )
595 vlc_object_release( p_playlist );
602 vlc_object_release( p_playlist );
606 /* This method is usefull for instance to remove the selected children of an
607 already selected node */
608 - (void)removeItemsFrom:(id)o_items ifChildrenOf:(id)o_nodes
611 for( i = 0 ; i < [o_items count] ; i++ )
613 for ( j = 0 ; j < [o_nodes count] ; j++ )
615 if( o_items == o_nodes)
617 if( j == i ) continue;
619 if( [self isItem: [[o_items objectAtIndex:i] pointerValue]
620 inNode: [[o_nodes objectAtIndex:j] pointerValue]
621 checkItemExistence: NO] )
623 [o_items removeObjectAtIndex:i];
624 /* We need to execute the next iteration with the same index
625 since the current item has been deleted */
634 - (IBAction)savePlaylist:(id)sender
636 playlist_t * p_playlist = pl_Yield( VLCIntf );
638 NSSavePanel *o_save_panel = [NSSavePanel savePanel];
639 NSString * o_name = [NSString stringWithFormat: @"%@", _NS("Untitled")];
641 //[o_save_panel setAllowedFileTypes: [NSArray arrayWithObjects: @"m3u", @"xpf", nil] ];
642 [o_save_panel setTitle: _NS("Save Playlist")];
643 [o_save_panel setPrompt: _NS("Save")];
644 [o_save_panel setAccessoryView: o_save_accessory_view];
646 if( [o_save_panel runModalForDirectory: nil
647 file: o_name] == NSOKButton )
649 NSString *o_filename = [o_save_panel filename];
651 if( [o_save_accessory_popup indexOfSelectedItem] == 1 )
653 NSString * o_real_filename;
655 range.location = [o_filename length] - [@".xspf" length];
656 range.length = [@".xspf" length];
658 if( [o_filename compare:@".xspf" options: NSCaseInsensitiveSearch
659 range: range] != NSOrderedSame )
661 o_real_filename = [NSString stringWithFormat: @"%@.xspf", o_filename];
665 o_real_filename = o_filename;
667 playlist_Export( p_playlist,
668 [o_real_filename fileSystemRepresentation],
669 p_playlist->p_local_category, "export-xspf" );
673 NSString * o_real_filename;
675 range.location = [o_filename length] - [@".m3u" length];
676 range.length = [@".m3u" length];
678 if( [o_filename compare:@".m3u" options: NSCaseInsensitiveSearch
679 range: range] != NSOrderedSame )
681 o_real_filename = [NSString stringWithFormat: @"%@.m3u", o_filename];
685 o_real_filename = o_filename;
687 playlist_Export( p_playlist,
688 [o_real_filename fileSystemRepresentation],
689 p_playlist->p_local_category, "export-m3u" );
692 vlc_object_release( p_playlist );
695 /* When called retrieves the selected outlineview row and plays that node or item */
696 - (IBAction)playItem:(id)sender
698 intf_thread_t * p_intf = VLCIntf;
699 playlist_t * p_playlist = pl_Yield( p_intf );
701 playlist_item_t *p_item;
702 playlist_item_t *p_node = NULL;
704 p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
708 if( p_item->i_children == -1 )
710 p_node = p_item->p_parent;
716 if( p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1 )
718 p_item = p_node->pp_children[0];
725 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, true, p_node, p_item );
727 vlc_object_release( p_playlist );
730 /* When called retrieves the selected outlineview row and plays that node or item */
731 - (IBAction)preparseItem:(id)sender
734 NSMutableArray *o_to_preparse;
735 intf_thread_t * p_intf = VLCIntf;
736 playlist_t * p_playlist = pl_Yield( p_intf );
738 o_to_preparse = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
739 i_count = [o_to_preparse count];
743 playlist_item_t *p_item = NULL;
745 for( i = 0; i < i_count; i++ )
747 o_number = [o_to_preparse lastObject];
748 i_row = [o_number intValue];
749 p_item = [[o_outline_view itemAtRow:i_row] pointerValue];
750 [o_to_preparse removeObject: o_number];
751 [o_outline_view deselectRow: i_row];
755 if( p_item->i_children == -1 )
757 playlist_PreparseEnqueue( p_playlist, p_item->p_input );
761 msg_Dbg( p_intf, "preparsing nodes not implemented" );
765 vlc_object_release( p_playlist );
766 [self playlistUpdated];
769 - (IBAction)servicesChange:(id)sender
771 NSMenuItem *o_mi = (NSMenuItem *)sender;
772 NSString *o_string = [o_mi representedObject];
773 playlist_t * p_playlist = pl_Yield( VLCIntf );
774 if( !playlist_IsServicesDiscoveryLoaded( p_playlist, [o_string UTF8String] ) )
775 playlist_ServicesDiscoveryAdd( p_playlist, [o_string UTF8String] );
777 playlist_ServicesDiscoveryRemove( p_playlist, [o_string UTF8String] );
779 [o_mi setState: playlist_IsServicesDiscoveryLoaded( p_playlist,
780 [o_string UTF8String] ) ? YES : NO];
782 vlc_object_release( p_playlist );
783 [self playlistUpdated];
787 - (IBAction)selectAll:(id)sender
789 [o_outline_view selectAll: nil];
792 - (IBAction)deleteItem:(id)sender
795 NSMutableArray *o_to_delete;
798 playlist_t * p_playlist;
799 intf_thread_t * p_intf = VLCIntf;
801 o_to_delete = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
802 i_count = [o_to_delete count];
804 p_playlist = pl_Yield( p_intf );
807 for( int i = 0; i < i_count; i++ )
809 o_number = [o_to_delete lastObject];
810 i_row = [o_number intValue];
811 id o_item = [o_outline_view itemAtRow: i_row];
812 playlist_item_t *p_item = [o_item pointerValue];
814 msg_Dbg( p_intf, "deleting item %i (of %i) with id \"%i\", pointerValue \"%p\" and %i children", i+1, i_count,
815 p_item->p_input->i_id, [o_item pointerValue], p_item->i_children +1 );
817 [o_to_delete removeObject: o_number];
818 [o_outline_view deselectRow: i_row];
820 if( p_item->i_children != -1 )
821 //is a node and not an item
823 if( p_playlist->status.i_status != PLAYLIST_STOPPED &&
824 [self isItem: p_playlist->status.p_item inNode:
825 ((playlist_item_t *)[o_item pointerValue])
826 checkItemExistence: NO] == YES )
827 // if current item is in selected node and is playing then stop playlist
828 playlist_Stop( p_playlist );
830 playlist_NodeDelete( p_playlist, p_item, true, false );
833 playlist_DeleteFromInput( p_playlist, p_item->p_input->i_id, true );
837 [self playlistUpdated];
838 vlc_object_release( p_playlist );
841 - (IBAction)sortNodeByName:(id)sender
843 [self sortNode: SORT_TITLE];
846 - (IBAction)sortNodeByAuthor:(id)sender
848 [self sortNode: SORT_ARTIST];
851 - (void)sortNode:(int)i_mode
853 playlist_t * p_playlist = pl_Yield( VLCIntf );
854 playlist_item_t * p_item;
856 if( [o_outline_view selectedRow] > -1 )
858 p_item = [[o_outline_view itemAtRow: [o_outline_view selectedRow]] pointerValue];
861 /*If no item is selected, sort the whole playlist*/
863 p_item = p_playlist->p_root_category;
866 if( p_item->i_children > -1 ) // the item is a node
869 playlist_RecursiveNodeSort( p_playlist, p_item, i_mode, ORDER_NORMAL );
875 playlist_RecursiveNodeSort( p_playlist,
876 p_item->p_parent, i_mode, ORDER_NORMAL );
879 vlc_object_release( p_playlist );
880 [self playlistUpdated];
883 - (input_item_t *)createItem:(NSDictionary *)o_one_item
885 intf_thread_t * p_intf = VLCIntf;
886 playlist_t * p_playlist = pl_Yield( p_intf );
888 input_item_t *p_input;
890 BOOL b_rem = FALSE, b_dir = FALSE;
891 NSString *o_uri, *o_name;
896 o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
897 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
898 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
900 /* Find the name for a disc entry (i know, can you believe the trouble?) */
901 if( ( !o_name || [o_name isEqualToString:@""] ) && [o_uri rangeOfString: @"/dev/"].location != NSNotFound )
903 int i_count, i_index;
904 struct statfs *mounts = NULL;
906 i_count = getmntinfo (&mounts, MNT_NOWAIT);
907 /* getmntinfo returns a pointer to static data. Do not free. */
908 for( i_index = 0 ; i_index < i_count; i_index++ )
910 NSMutableString *o_temp, *o_temp2;
911 o_temp = [NSMutableString stringWithString: o_uri];
912 o_temp2 = [NSMutableString stringWithUTF8String: mounts[i_index].f_mntfromname];
913 [o_temp replaceOccurrencesOfString: @"/dev/rdisk" withString: @"/dev/disk" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
914 [o_temp2 replaceOccurrencesOfString: @"s0" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp2 length]) ];
915 [o_temp2 replaceOccurrencesOfString: @"s1" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp2 length]) ];
917 if( strstr( [o_temp fileSystemRepresentation], [o_temp2 fileSystemRepresentation] ) != NULL )
919 o_name = [[NSFileManager defaultManager] displayNameAtPath: [NSString stringWithUTF8String:mounts[i_index].f_mntonname]];
923 /* If no name, then make a guess */
924 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
926 if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
927 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
928 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
930 /* All of this is to make sure CD's play when you D&D them on VLC */
931 /* Converts mountpoint to a /dev file */
934 NSMutableString *o_temp;
936 buf = (struct statfs *) malloc (sizeof(struct statfs));
937 statfs( [o_uri fileSystemRepresentation], buf );
938 psz_dev = strdup(buf->f_mntfromname);
939 o_temp = [NSMutableString stringWithUTF8String: psz_dev ];
940 [o_temp replaceOccurrencesOfString: @"/dev/disk" withString: @"/dev/rdisk" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
941 [o_temp replaceOccurrencesOfString: @"s0" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
942 [o_temp replaceOccurrencesOfString: @"s1" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
946 p_input = input_ItemNew( p_playlist, [o_uri fileSystemRepresentation], [o_name UTF8String] );
952 for( i = 0; i < (int)[o_options count]; i++ )
954 input_ItemAddOption( p_input, strdup( [[o_options objectAtIndex:i] UTF8String] ) );
958 /* Recent documents menu */
959 o_true_file = [NSURL fileURLWithPath: o_uri];
960 if( o_true_file != nil && (BOOL)config_GetInt( p_playlist, "macosx-recentitems" ) == YES )
962 [[NSDocumentController sharedDocumentController]
963 noteNewRecentDocumentURL: o_true_file];
966 vlc_object_release( p_playlist );
970 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
973 playlist_t * p_playlist = pl_Yield( VLCIntf );
975 for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
977 input_item_t *p_input;
978 NSDictionary *o_one_item;
981 o_one_item = [o_array objectAtIndex: i_item];
982 p_input = [self createItem: o_one_item];
989 /* FIXME: playlist_AddInput() can fail */
990 playlist_AddInput( p_playlist, p_input, PLAYLIST_INSERT,
991 i_position == -1 ? PLAYLIST_END : i_position + i_item, true,
994 if( i_item == 0 && !b_enqueue )
996 playlist_item_t *p_item;
997 p_item = playlist_ItemGetByInput( p_playlist, p_input, true );
998 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, true, NULL, p_item );
1002 playlist_item_t *p_item;
1003 p_item = playlist_ItemGetByInput( p_playlist, p_input, true );
1004 playlist_Control( p_playlist, PLAYLIST_SKIP, true, p_item );
1006 vlc_gc_decref( p_input );
1008 [self playlistUpdated];
1009 vlc_object_release( p_playlist );
1012 - (void)appendNodeArray:(NSArray*)o_array inNode:(playlist_item_t *)p_node atPos:(int)i_position enqueue:(BOOL)b_enqueue
1015 playlist_t * p_playlist = pl_Yield( VLCIntf );
1017 for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
1019 input_item_t *p_input;
1020 NSDictionary *o_one_item;
1023 o_one_item = [o_array objectAtIndex: i_item];
1024 p_input = [self createItem: o_one_item];
1031 /* FIXME: playlist_NodeAddInput() can fail */
1032 playlist_NodeAddInput( p_playlist, p_input, p_node,
1035 PLAYLIST_END : i_position + i_item, false );
1038 if( i_item == 0 && !b_enqueue )
1040 playlist_item_t *p_item;
1041 p_item = playlist_ItemGetByInput( p_playlist, p_input, true );
1042 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, true, NULL, p_item );
1046 playlist_item_t *p_item;
1047 p_item = playlist_ItemGetByInput( p_playlist, p_input, true );
1048 playlist_Control( p_playlist, PLAYLIST_SKIP, true, p_item );
1050 vlc_gc_decref( p_input );
1052 [self playlistUpdated];
1053 vlc_object_release( p_playlist );
1056 - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
1058 playlist_t *p_playlist = pl_Yield( VLCIntf );
1059 playlist_item_t *p_selected_item;
1060 int i_current, i_selected_row;
1062 i_selected_row = [o_outline_view selectedRow];
1063 if (i_selected_row < 0)
1066 p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow:
1067 i_selected_row] pointerValue];
1069 for( i_current = 0; i_current < p_item->i_children ; i_current++ )
1072 NSString *o_current_name, *o_current_author;
1075 o_current_name = [NSString stringWithUTF8String:
1076 p_item->pp_children[i_current]->p_input->psz_name];
1077 psz_temp = input_ItemGetInfo( p_item->p_input ,
1078 _("Meta-information"),_("Artist") );
1079 o_current_author = [NSString stringWithUTF8String: psz_temp];
1083 if( p_selected_item == p_item->pp_children[i_current] &&
1084 b_selected_item_met == NO )
1086 b_selected_item_met = YES;
1088 else if( p_selected_item == p_item->pp_children[i_current] &&
1089 b_selected_item_met == YES )
1091 vlc_object_release( p_playlist );
1094 else if( b_selected_item_met == YES &&
1095 ( [o_current_name rangeOfString:[o_search_field
1096 stringValue] options:NSCaseInsensitiveSearch].length ||
1097 [o_current_author rangeOfString:[o_search_field
1098 stringValue] options:NSCaseInsensitiveSearch].length ) )
1100 vlc_object_release( p_playlist );
1101 /*Adds the parent items in the result array as well, so that we can
1103 return [NSMutableArray arrayWithObject: [NSValue
1104 valueWithPointer: p_item->pp_children[i_current]]];
1106 if( p_item->pp_children[i_current]->i_children > 0 )
1108 id o_result = [self subSearchItem:
1109 p_item->pp_children[i_current]];
1110 if( o_result != NULL )
1112 vlc_object_release( p_playlist );
1113 [o_result insertObject: [NSValue valueWithPointer:
1114 p_item->pp_children[i_current]] atIndex:0];
1119 vlc_object_release( p_playlist );
1123 - (IBAction)searchItem:(id)sender
1125 playlist_t * p_playlist = pl_Yield( VLCIntf );
1131 b_selected_item_met = NO;
1133 /*First, only search after the selected item:*
1134 *(b_selected_item_met = NO) */
1135 o_result = [self subSearchItem:p_playlist->p_root_category];
1136 if( o_result == NULL )
1138 /* If the first search failed, search again from the beginning */
1139 o_result = [self subSearchItem:p_playlist->p_root_category];
1141 if( o_result != NULL )
1144 if( [[o_result objectAtIndex: 0] pointerValue] ==
1145 p_playlist->p_local_category )
1150 for( i = i_start ; i < [o_result count] - 1 ; i++ )
1152 [o_outline_view expandItem: [o_outline_dict objectForKey:
1153 [NSString stringWithFormat: @"%p",
1154 [[o_result objectAtIndex: i] pointerValue]]]];
1156 i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
1157 [NSString stringWithFormat: @"%p",
1158 [[o_result objectAtIndex: [o_result count] - 1 ]
1163 [o_outline_view selectRow:i_row byExtendingSelection: NO];
1164 [o_outline_view scrollRowToVisible: i_row];
1166 vlc_object_release( p_playlist );
1169 - (IBAction)recursiveExpandNode:(id)sender
1171 id o_item = [o_outline_view itemAtRow: [o_outline_view selectedRow]];
1172 playlist_item_t *p_item = (playlist_item_t *)[o_item pointerValue];
1174 if( ![[o_outline_view dataSource] outlineView: o_outline_view
1175 isItemExpandable: o_item] )
1177 o_item = [o_outline_dict objectForKey: [NSString
1178 stringWithFormat: @"%p", p_item->p_parent]];
1181 /* We need to collapse the node first, since OSX refuses to recursively
1182 expand an already expanded node, even if children nodes are collapsed. */
1183 [o_outline_view collapseItem: o_item collapseChildren: YES];
1184 [o_outline_view expandItem: o_item expandChildren: YES];
1187 - (NSMenu *)menuForEvent:(NSEvent *)o_event
1193 pt = [o_outline_view convertPoint: [o_event locationInWindow]
1195 b_item_sel = ( [o_outline_view rowAtPoint: pt] != -1 &&
1196 [o_outline_view selectedRow] != -1 );
1197 b_rows = [o_outline_view numberOfRows] != 0;
1199 [o_mi_play setEnabled: b_item_sel];
1200 [o_mi_delete setEnabled: b_item_sel];
1201 [o_mi_selectall setEnabled: b_rows];
1202 [o_mi_info setEnabled: b_item_sel];
1203 [o_mi_preparse setEnabled: b_item_sel];
1204 [o_mi_recursive_expand setEnabled: b_item_sel];
1205 [o_mi_sort_name setEnabled: b_item_sel];
1206 [o_mi_sort_author setEnabled: b_item_sel];
1208 return( o_ctx_menu );
1211 - (void)outlineView: (NSTableView*)o_tv
1212 didClickTableColumn:(NSTableColumn *)o_tc
1214 int i_mode = 0, i_type;
1215 intf_thread_t *p_intf = VLCIntf;
1217 playlist_t *p_playlist = pl_Yield( p_intf );
1219 /* Check whether the selected table column header corresponds to a
1220 sortable table column*/
1221 if( !( o_tc == o_tc_name || o_tc == o_tc_author ) )
1223 vlc_object_release( p_playlist );
1227 if( o_tc_sortColumn == o_tc )
1229 b_isSortDescending = !b_isSortDescending;
1233 b_isSortDescending = false;
1236 if( o_tc == o_tc_name )
1238 i_mode = SORT_TITLE;
1240 else if( o_tc == o_tc_author )
1242 i_mode = SORT_ARTIST;
1245 if( b_isSortDescending )
1247 i_type = ORDER_REVERSE;
1251 i_type = ORDER_NORMAL;
1254 vlc_mutex_lock( &p_playlist->object_lock );
1255 playlist_RecursiveNodeSort( p_playlist, p_playlist->p_root_category, i_mode, i_type );
1256 vlc_mutex_unlock( &p_playlist->object_lock );
1258 vlc_object_release( p_playlist );
1259 [self playlistUpdated];
1261 o_tc_sortColumn = o_tc;
1262 [o_outline_view setHighlightedTableColumn:o_tc];
1264 if( b_isSortDescending )
1266 [o_outline_view setIndicatorImage:o_descendingSortingImage
1267 inTableColumn:o_tc];
1271 [o_outline_view setIndicatorImage:o_ascendingSortingImage
1272 inTableColumn:o_tc];
1277 - (void)outlineView:(NSOutlineView *)outlineView
1278 willDisplayCell:(id)cell
1279 forTableColumn:(NSTableColumn *)tableColumn
1282 playlist_t *p_playlist = pl_Yield( VLCIntf );
1286 o_playing_item = [o_outline_dict objectForKey:
1287 [NSString stringWithFormat:@"%p", p_playlist->status.p_item]];
1289 if( [self isItem: [o_playing_item pointerValue] inNode:
1290 [item pointerValue] checkItemExistence: YES]
1291 || [o_playing_item isEqual: item] )
1293 [cell setFont: [NSFont boldSystemFontOfSize: 0]];
1297 [cell setFont: [NSFont systemFontOfSize: 0]];
1299 vlc_object_release( p_playlist );
1302 - (IBAction)addNode:(id)sender
1304 /* we have to create a new thread here because otherwise we would block the
1305 * interface since the interaction-stuff and this code would run in the same
1307 [NSThread detachNewThreadSelector: @selector(addNodeThreadedly)
1308 toTarget: self withObject:nil];
1309 [self playlistUpdated];
1312 - (void)addNodeThreadedly
1314 NSAutoreleasePool * ourPool = [[NSAutoreleasePool alloc] init];
1316 /* simply adds a new node to the end of the playlist */
1317 playlist_t * p_playlist = pl_Yield( VLCIntf );
1318 vlc_thread_set_priority( p_playlist, VLC_THREAD_PRIORITY_LOW );
1321 char *psz_name = NULL;
1322 playlist_item_t * p_item;
1323 ret_v = intf_UserStringInput( p_playlist, _("New Node"),
1324 _("Please enter a name for the new node."), &psz_name );
1326 if( ret_v != DIALOG_CANCELLED && psz_name && *psz_name )
1327 p_item = playlist_NodeCreate( p_playlist, psz_name,
1328 p_playlist->p_local_category, 0, NULL );
1329 else if(! config_GetInt( p_playlist, "interact" ) )
1331 /* in case that the interaction is disabled, just give it a bogus name */
1332 p_item = playlist_NodeCreate( p_playlist, _("Empty Folder"),
1333 p_playlist->p_local_category, 0, NULL );
1337 msg_Warn( VLCIntf, "node creation failed or cancelled by user" );
1339 vlc_object_release( p_playlist );
1345 @implementation VLCPlaylist (NSOutlineViewDataSource)
1347 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
1349 id o_value = [super outlineView: outlineView child: index ofItem: item];
1350 playlist_t *p_playlist = pl_Yield( VLCIntf );
1352 if( playlist_CurrentSize( p_playlist ) >= 2 )
1354 [o_status_field setStringValue: [NSString stringWithFormat:
1355 _NS("%i items in the playlist"),
1356 playlist_CurrentSize( p_playlist )]];
1360 if( playlist_IsEmpty( p_playlist ) )
1362 [o_status_field setStringValue: _NS("No items in the playlist")];
1366 [o_status_field setStringValue: _NS("1 item in the playlist")];
1369 vlc_object_release( p_playlist );
1371 [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p",
1372 [o_value pointerValue]]];
1374 msg_Dbg( VLCIntf, "adding item %p", [o_value pointerValue] );
1380 /* Required for drag & drop and reordering */
1381 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
1384 playlist_t *p_playlist = pl_Yield( VLCIntf );
1386 /* First remove the items that were moved during the last drag & drop
1388 [o_items_array removeAllObjects];
1389 [o_nodes_array removeAllObjects];
1391 for( i = 0 ; i < [items count] ; i++ )
1393 id o_item = [items objectAtIndex: i];
1395 /* Refuse to move items that are not in the General Node
1396 (Service Discovery) */
1397 if( ![self isItem: [o_item pointerValue] inNode:
1398 p_playlist->p_local_category checkItemExistence: NO] &&
1399 ( var_CreateGetBool( p_playlist, "media-library" ) &&
1400 ![self isItem: [o_item pointerValue] inNode:
1401 p_playlist->p_ml_category checkItemExistence: NO]) )
1403 vlc_object_release(p_playlist);
1406 /* Fill the items and nodes to move in 2 different arrays */
1407 if( ((playlist_item_t *)[o_item pointerValue])->i_children > 0 )
1408 [o_nodes_array addObject: o_item];
1410 [o_items_array addObject: o_item];
1413 /* Now we need to check if there are selected items that are in already
1414 selected nodes. In that case, we only want to move the nodes */
1415 [self removeItemsFrom: o_nodes_array ifChildrenOf: o_nodes_array];
1416 [self removeItemsFrom: o_items_array ifChildrenOf: o_nodes_array];
1418 /* We add the "VLCPlaylistItemPboardType" type to be able to recognize
1419 a Drop operation coming from the playlist. */
1421 [pboard declareTypes: [NSArray arrayWithObjects:
1422 @"VLCPlaylistItemPboardType", nil] owner: self];
1423 [pboard setData:[NSData data] forType:@"VLCPlaylistItemPboardType"];
1425 vlc_object_release(p_playlist);
1429 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
1431 playlist_t *p_playlist = pl_Yield( VLCIntf );
1432 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1434 if( !p_playlist ) return NSDragOperationNone;
1436 /* Dropping ON items is not allowed if item is not a node */
1439 if( index == NSOutlineViewDropOnItemIndex &&
1440 ((playlist_item_t *)[item pointerValue])->i_children == -1 )
1442 vlc_object_release( p_playlist );
1443 return NSDragOperationNone;
1447 /* Don't allow on drop on playlist root element's child */
1448 if( !item && index != NSOutlineViewDropOnItemIndex)
1450 vlc_object_release( p_playlist );
1451 return NSDragOperationNone;
1454 /* We refuse to drop an item in anything else than a child of the General
1455 Node. We still accept items that would be root nodes of the outlineview
1456 however, to allow drop in an empty playlist. */
1457 if( !( ([self isItem: [item pointerValue] inNode: p_playlist->p_local_category checkItemExistence: NO] ||
1458 ( var_CreateGetBool( p_playlist, "media-library" ) && [self isItem: [item pointerValue] inNode: p_playlist->p_ml_category checkItemExistence: NO] ) ) || item == nil ) )
1460 vlc_object_release( p_playlist );
1461 return NSDragOperationNone;
1464 /* Drop from the Playlist */
1465 if( [[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] )
1468 for( i = 0 ; i < [o_nodes_array count] ; i++ )
1470 /* We refuse to Drop in a child of an item we are moving */
1471 if( [self isItem: [item pointerValue] inNode:
1472 [[o_nodes_array objectAtIndex: i] pointerValue]
1473 checkItemExistence: NO] )
1475 vlc_object_release( p_playlist );
1476 return NSDragOperationNone;
1479 vlc_object_release( p_playlist );
1480 return NSDragOperationMove;
1483 /* Drop from the Finder */
1484 else if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1486 vlc_object_release( p_playlist );
1487 return NSDragOperationGeneric;
1489 vlc_object_release( p_playlist );
1490 return NSDragOperationNone;
1493 - (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(int)index
1495 playlist_t * p_playlist = pl_Yield( VLCIntf );
1496 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1498 /* Drag & Drop inside the playlist */
1499 if( [[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] )
1501 int i_row, i_removed_from_node = 0;
1503 playlist_item_t *p_new_parent, *p_item = NULL;
1504 NSArray *o_all_items = [o_nodes_array arrayByAddingObjectsFromArray:
1506 /* If the item is to be dropped as root item of the outline, make it a
1507 child of the General node.
1508 Else, choose the proposed parent as parent. */
1509 if( item == nil ) p_new_parent = p_playlist->p_local_category;
1510 else p_new_parent = [item pointerValue];
1512 /* Make sure the proposed parent is a node.
1513 (This should never be true) */
1514 if( p_new_parent->i_children < 0 )
1516 vlc_object_release( p_playlist );
1520 for( i = 0; i < [o_all_items count]; i++ )
1522 playlist_item_t *p_old_parent = NULL;
1523 int i_old_index = 0;
1525 p_item = [[o_all_items objectAtIndex:i] pointerValue];
1526 p_old_parent = p_item->p_parent;
1529 /* We may need the old index later */
1530 if( p_new_parent == p_old_parent )
1533 for( j = 0; j < p_old_parent->i_children; j++ )
1535 if( p_old_parent->pp_children[j] == p_item )
1544 // Actually detach the item from the old position
1545 if( playlist_NodeRemoveItem( p_playlist, p_item, p_old_parent ) ==
1549 /* Calculate the new index */
1552 /* If we move the item in the same node, we need to take into
1553 account that one item will be deleted */
1556 if ((p_new_parent == p_old_parent &&
1557 i_old_index < index + (int)i) )
1559 i_removed_from_node++;
1561 i_new_index = index + i - i_removed_from_node;
1563 // Reattach the item to the new position
1564 playlist_NodeInsert( p_playlist, p_item, p_new_parent, i_new_index );
1568 [self playlistUpdated];
1569 i_row = [o_outline_view rowForItem:[o_outline_dict
1570 objectForKey:[NSString stringWithFormat: @"%p",
1571 [[o_all_items objectAtIndex: 0] pointerValue]]]];
1575 i_row = [o_outline_view rowForItem:[o_outline_dict
1576 objectForKey:[NSString stringWithFormat: @"%p", p_new_parent]]];
1579 [o_outline_view deselectAll: self];
1580 [o_outline_view selectRow: i_row byExtendingSelection: NO];
1581 [o_outline_view scrollRowToVisible: i_row];
1583 vlc_object_release( p_playlist );
1587 else if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1590 playlist_item_t *p_node = [item pointerValue];
1592 NSArray *o_array = [NSArray array];
1593 NSArray *o_values = [[o_pasteboard propertyListForType:
1594 NSFilenamesPboardType]
1595 sortedArrayUsingSelector:
1596 @selector(caseInsensitiveCompare:)];
1598 for( i = 0; i < (int)[o_values count]; i++)
1600 NSDictionary *o_dic;
1601 o_dic = [NSDictionary dictionaryWithObject:[o_values
1602 objectAtIndex:i] forKey:@"ITEM_URL"];
1603 o_array = [o_array arrayByAddingObject: o_dic];
1608 [self appendArray: o_array atPos: index enqueue: YES];
1610 /* This should never occur */
1611 else if( p_node->i_children == -1 )
1613 vlc_object_release( p_playlist );
1618 [self appendNodeArray: o_array inNode: p_node
1619 atPos: index enqueue: YES];
1621 vlc_object_release( p_playlist );
1624 vlc_object_release( p_playlist );