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];
96 /*****************************************************************************
97 * VLCPlaylistCommon implementation
99 * This class the superclass of the VLCPlaylist and VLCPlaylistWizard.
100 * It contains the common methods and elements of these 2 entities.
101 *****************************************************************************/
102 @implementation VLCPlaylistCommon
109 o_outline_dict = [[NSMutableDictionary alloc] init];
115 playlist_t * p_playlist = pl_Yield( VLCIntf );
116 [o_outline_view setTarget: self];
117 [o_outline_view setDelegate: self];
118 [o_outline_view setDataSource: self];
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 if( outlineView != o_outline_view )
154 vlc_object_release( p_playlist );
161 p_item = p_playlist->p_root_category;
165 p_item = (playlist_item_t *)[item pointerValue];
168 i_return = p_item->i_children;
169 vlc_object_release( p_playlist );
177 /* return the child at index for the Obj-C pointer item */ /* DONE */
178 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
180 playlist_item_t *p_return = NULL, *p_item = NULL;
182 playlist_t * p_playlist = pl_Yield( VLCIntf );
187 p_item = p_playlist->p_root_category;
191 p_item = (playlist_item_t *)[item pointerValue];
193 if( p_item && index < p_item->i_children && index >= 0 )
194 p_return = p_item->pp_children[index];
196 vlc_object_release( p_playlist );
198 o_value = [o_outline_dict objectForKey:[NSString stringWithFormat: @"%p", p_return]];
202 msg_Warn( VLCIntf, "playlist item misses pointer value, adding one" );
203 o_value = [[NSValue valueWithPointer: p_return] retain];
208 /* is the item expandable */
209 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
212 playlist_t *p_playlist = pl_Yield( VLCIntf );
217 if( p_playlist->p_root_category )
219 i_return = p_playlist->p_root_category->i_children;
224 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
226 i_return = p_item->i_children;
228 vlc_object_release( p_playlist );
230 return (i_return > 0);
233 /* retrieve the string values for the cells */
234 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
237 playlist_item_t *p_item;
239 /* For error handling */
240 static BOOL attempted_reload = NO;
242 if( item == nil || ![item isKindOfClass: [NSValue class]] )
244 /* Attempt to fix the error by asking for a data redisplay
245 * This might cause infinite loop, so add a small check */
246 if( !attempted_reload )
248 attempted_reload = YES;
249 [outlineView reloadData];
254 p_item = (playlist_item_t *)[item pointerValue];
255 if( !p_item || !p_item->p_input )
257 /* Attempt to fix the error by asking for a data redisplay
258 * This might cause infinite loop, so add a small check */
259 if( !attempted_reload )
261 attempted_reload = YES;
262 [outlineView reloadData];
267 attempted_reload = NO;
269 if( [[o_tc identifier] isEqualToString:@"1"] )
271 /* sanity check to prevent the NSString class from crashing */
272 char *psz_title = input_item_GetTitle( p_item->p_input );
273 if( !EMPTY_STR( psz_title ) )
275 o_value = [NSString stringWithUTF8String: psz_title];
279 char *psz_name = input_item_GetName( p_item->p_input );
280 if( psz_name != NULL )
282 o_value = [NSString stringWithUTF8String: psz_name];
290 char *psz_artist = input_item_GetArtist( p_item->p_input );
291 if( [[o_tc identifier] isEqualToString:@"2"] && !EMPTY_STR( psz_artist ) )
293 o_value = [NSString stringWithUTF8String: psz_artist];
295 else if( [[o_tc identifier] isEqualToString:@"3"] )
297 char psz_duration[MSTRTIME_MAX_SIZE];
298 mtime_t dur = input_item_GetDuration( p_item->p_input );
301 secstotimestr( psz_duration, dur/1000000 );
302 o_value = [NSString stringWithUTF8String: psz_duration];
306 o_value = @"-:--:--";
317 /*****************************************************************************
318 * VLCPlaylistWizard implementation
319 *****************************************************************************/
320 @implementation VLCPlaylistWizard
322 - (IBAction)reloadOutlineView
324 /* Only reload the outlineview if the wizard window is open since this can
325 be quite long on big playlists */
326 if( [[o_outline_view window] isVisible] )
328 [o_outline_view reloadData];
334 /*****************************************************************************
335 * extension to NSOutlineView's interface to fix compilation warnings
336 * and let us access these 2 functions properly
337 * this uses a private Apple-API, but works fine on all current OSX releases
338 * keep checking for compatiblity with future releases though
339 *****************************************************************************/
341 @interface NSOutlineView (UndocumentedSortImages)
342 + (NSImage *)_defaultTableHeaderSortImage;
343 + (NSImage *)_defaultTableHeaderReverseSortImage;
347 /*****************************************************************************
348 * VLCPlaylist implementation
349 *****************************************************************************/
350 @implementation VLCPlaylist
357 o_nodes_array = [[NSMutableArray alloc] init];
358 o_items_array = [[NSMutableArray alloc] init];
365 playlist_t * p_playlist = pl_Yield( VLCIntf );
369 [super awakeFromNib];
371 [o_outline_view setDoubleAction: @selector(playItem:)];
373 [o_outline_view registerForDraggedTypes:
374 [NSArray arrayWithObjects: NSFilenamesPboardType,
375 @"VLCPlaylistItemPboardType", nil]];
376 [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
378 /* This uses private Apple API which works fine until 10.4.
379 * We need to keep checking in the future!
380 * These methods are being added artificially to NSOutlineView's interface above */
381 o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
382 o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
384 o_tc_sortColumn = nil;
387 char ** ppsz_services = services_discovery_GetServicesNames( p_playlist, &ppsz_name );
389 for( i = 0; ppsz_services[i]; i++ )
391 vlc_bool_t b_enabled;
395 char * name = ppsz_name[i] ? ppsz_name[i] : ppsz_services[i];
396 /* Check whether to enable these menuitems */
397 b_enabled = playlist_IsServicesDiscoveryLoaded( p_playlist, objectname );
399 /* Create the menu entries used in the playlist menu */
400 o_lmi = [[o_mi_services submenu] addItemWithTitle:
401 [NSString stringWithUTF8String: name]
402 action: @selector(servicesChange:)
404 [o_lmi setTarget: self];
405 [o_lmi setRepresentedObject: [NSString stringWithUTF8String: ppsz_services[i]]];
406 if( b_enabled ) [o_lmi setState: NSOnState];
408 /* Create the menu entries for the main menu */
409 o_lmi = [[o_mm_mi_services submenu] addItemWithTitle:
410 [NSString stringWithUTF8String: name]
411 action: @selector(servicesChange:)
413 [o_lmi setTarget: self];
414 [o_lmi setRepresentedObject: [NSString stringWithUTF8String: ppsz_services[i]]];
415 if( b_enabled ) [o_lmi setState: NSOnState];
417 free( ppsz_services[i] );
418 free( ppsz_name[i] );
420 free( ppsz_services );
423 vlc_object_release( p_playlist );
426 - (void)searchfieldChanged:(NSNotification *)o_notification
428 [o_search_field setStringValue:[[o_notification object] stringValue]];
435 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
436 [o_mi_play setTitle: _NS("Play")];
437 [o_mi_delete setTitle: _NS("Delete")];
438 [o_mi_recursive_expand setTitle: _NS("Expand Node")];
439 [o_mi_selectall setTitle: _NS("Select All")];
440 [o_mi_info setTitle: _NS("Information")];
441 [o_mi_preparse setTitle: _NS("Get Stream Information")];
442 [o_mi_sort_name setTitle: _NS("Sort Node by Name")];
443 [o_mi_sort_author setTitle: _NS("Sort Node by Author")];
444 [o_mi_services setTitle: _NS("Services discovery")];
445 [o_status_field setStringValue: _NS("No items in the playlist")];
447 [o_search_field setToolTip: _NS("Search in Playlist")];
448 [o_mi_addNode setTitle: _NS("Add Folder to Playlist")];
450 [o_save_accessory_text setStringValue: _NS("File Format:")];
451 [[o_save_accessory_popup itemAtIndex:0] setTitle: _NS("Extended M3U")];
452 [[o_save_accessory_popup itemAtIndex:1] setTitle: _NS("XML Shareable Playlist Format (XSPF)")];
455 - (void)playlistUpdated
459 /* Clear indications of any existing column sorting */
460 for( i = 0 ; i < [[o_outline_view tableColumns] count] ; i++ )
462 [o_outline_view setIndicatorImage:nil inTableColumn:
463 [[o_outline_view tableColumns] objectAtIndex:i]];
466 [o_outline_view setHighlightedTableColumn:nil];
467 o_tc_sortColumn = nil;
468 // TODO Find a way to keep the dict size to a minimum
469 //[o_outline_dict removeAllObjects];
470 [o_outline_view reloadData];
471 [[[[VLCMain sharedInstance] getWizard] getPlaylistWizard] reloadOutlineView];
472 [[[[VLCMain sharedInstance] getBookmarks] getDataTable] reloadData];
474 playlist_t *p_playlist = pl_Yield( VLCIntf );
476 if( playlist_CurrentSize( p_playlist ) >= 2 )
478 [o_status_field setStringValue: [NSString stringWithFormat:
479 _NS("%i items in the playlist"),
480 playlist_CurrentSize( p_playlist )]];
484 if( playlist_IsEmpty( p_playlist ) )
485 [o_status_field setStringValue: _NS("No items in the playlist")];
487 [o_status_field setStringValue: _NS("1 item in the playlist")];
489 vlc_object_release( p_playlist );
492 - (void)playModeUpdated
494 playlist_t *p_playlist = pl_Yield( VLCIntf );
495 vlc_value_t val, val2;
497 var_Get( p_playlist, "loop", &val2 );
498 var_Get( p_playlist, "repeat", &val );
499 if( val.b_bool == VLC_TRUE )
501 [[[VLCMain sharedInstance] getControls] repeatOne];
503 else if( val2.b_bool == VLC_TRUE )
505 [[[VLCMain sharedInstance] getControls] repeatAll];
509 [[[VLCMain sharedInstance] getControls] repeatOff];
512 [[[VLCMain sharedInstance] getControls] shuffle];
514 vlc_object_release( p_playlist );
517 - (void)updateRowSelection
522 playlist_t *p_playlist = pl_Yield( VLCIntf );
523 playlist_item_t *p_item, *p_temp_item;
524 NSMutableArray *o_array = [NSMutableArray array];
526 p_item = p_playlist->status.p_item;
529 vlc_object_release(p_playlist);
533 p_temp_item = p_item;
534 while( p_temp_item->p_parent )
536 [o_array insertObject: [NSValue valueWithPointer: p_temp_item] atIndex: 0];
537 p_temp_item = p_temp_item->p_parent;
540 for( j = 0; j < [o_array count] - 1; j++ )
543 if( ( o_item = [o_outline_dict objectForKey:
544 [NSString stringWithFormat: @"%p",
545 [[o_array objectAtIndex:j] pointerValue]]] ) != nil )
547 [o_outline_view expandItem: o_item];
552 vlc_object_release( p_playlist );
554 /* update our info-panel to reflect the new item */
555 [[[VLCMain sharedInstance] getInfo] updatePanel];
558 /* Check if p_item is a child of p_node recursively. We need to check the item
559 existence first since OSX sometimes tries to redraw items that have been
560 deleted. We don't do it when not required since this verification takes
561 quite a long time on big playlists (yes, pretty hacky). */
562 - (BOOL)isItem: (playlist_item_t *)p_item
563 inNode: (playlist_item_t *)p_node
564 checkItemExistence:(BOOL)b_check
567 playlist_t * p_playlist = pl_Yield( VLCIntf );
568 playlist_item_t *p_temp_item = p_item;
570 if( p_node == p_item )
572 vlc_object_release(p_playlist);
576 if( p_node->i_children < 1)
578 vlc_object_release(p_playlist);
585 vlc_mutex_lock( &p_playlist->object_lock );
589 /* Since outlineView: willDisplayCell:... may call this function with
590 p_items that don't exist anymore, first check if the item is still
591 in the playlist. Any cleaner solution welcomed. */
592 for( i = 0; i < p_playlist->all_items.i_size; i++ )
594 if( ARRAY_VAL( p_playlist->all_items, i) == p_item ) break;
595 else if ( i == p_playlist->all_items.i_size - 1 )
597 vlc_object_release( p_playlist );
598 vlc_mutex_unlock( &p_playlist->object_lock );
606 p_temp_item = p_temp_item->p_parent;
607 if( p_temp_item == p_node )
609 vlc_mutex_unlock( &p_playlist->object_lock );
610 vlc_object_release( p_playlist );
614 vlc_mutex_unlock( &p_playlist->object_lock );
617 vlc_object_release( p_playlist );
621 /* This method is usefull for instance to remove the selected children of an
622 already selected node */
623 - (void)removeItemsFrom:(id)o_items ifChildrenOf:(id)o_nodes
626 for( i = 0 ; i < [o_items count] ; i++ )
628 for ( j = 0 ; j < [o_nodes count] ; j++ )
630 if( o_items == o_nodes)
632 if( j == i ) continue;
634 if( [self isItem: [[o_items objectAtIndex:i] pointerValue]
635 inNode: [[o_nodes objectAtIndex:j] pointerValue]
636 checkItemExistence: NO] )
638 [o_items removeObjectAtIndex:i];
639 /* We need to execute the next iteration with the same index
640 since the current item has been deleted */
649 - (IBAction)savePlaylist:(id)sender
651 playlist_t * p_playlist = pl_Yield( VLCIntf );
653 NSSavePanel *o_save_panel = [NSSavePanel savePanel];
654 NSString * o_name = [NSString stringWithFormat: @"%@", _NS("Untitled")];
656 //[o_save_panel setAllowedFileTypes: [NSArray arrayWithObjects: @"m3u", @"xpf", nil] ];
657 [o_save_panel setTitle: _NS("Save Playlist")];
658 [o_save_panel setPrompt: _NS("Save")];
659 [o_save_panel setAccessoryView: o_save_accessory_view];
661 if( [o_save_panel runModalForDirectory: nil
662 file: o_name] == NSOKButton )
664 NSString *o_filename = [o_save_panel filename];
666 if( [o_save_accessory_popup indexOfSelectedItem] == 1 )
668 NSString * o_real_filename;
670 range.location = [o_filename length] - [@".xspf" length];
671 range.length = [@".xspf" length];
673 if( [o_filename compare:@".xspf" options: NSCaseInsensitiveSearch
674 range: range] != NSOrderedSame )
676 o_real_filename = [NSString stringWithFormat: @"%@.xspf", o_filename];
680 o_real_filename = o_filename;
682 playlist_Export( p_playlist,
683 [o_real_filename fileSystemRepresentation],
684 p_playlist->p_local_category, "export-xspf" );
688 NSString * o_real_filename;
690 range.location = [o_filename length] - [@".m3u" length];
691 range.length = [@".m3u" length];
693 if( [o_filename compare:@".m3u" options: NSCaseInsensitiveSearch
694 range: range] != NSOrderedSame )
696 o_real_filename = [NSString stringWithFormat: @"%@.m3u", o_filename];
700 o_real_filename = o_filename;
702 playlist_Export( p_playlist,
703 [o_real_filename fileSystemRepresentation],
704 p_playlist->p_local_category, "export-m3u" );
707 vlc_object_release( p_playlist );
710 /* When called retrieves the selected outlineview row and plays that node or item */
711 - (IBAction)playItem:(id)sender
713 intf_thread_t * p_intf = VLCIntf;
714 playlist_t * p_playlist = pl_Yield( p_intf );
716 playlist_item_t *p_item;
717 playlist_item_t *p_node = NULL;
719 p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
723 if( p_item->i_children == -1 )
725 p_node = p_item->p_parent;
731 if( p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1 )
733 p_item = p_node->pp_children[0];
740 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VLC_TRUE, p_node, p_item );
742 vlc_object_release( p_playlist );
745 /* When called retrieves the selected outlineview row and plays that node or item */
746 - (IBAction)preparseItem:(id)sender
749 NSMutableArray *o_to_preparse;
750 intf_thread_t * p_intf = VLCIntf;
751 playlist_t * p_playlist = pl_Yield( p_intf );
753 o_to_preparse = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
754 i_count = [o_to_preparse count];
758 playlist_item_t *p_item = NULL;
760 for( i = 0; i < i_count; i++ )
762 o_number = [o_to_preparse lastObject];
763 i_row = [o_number intValue];
764 p_item = [[o_outline_view itemAtRow:i_row] pointerValue];
765 [o_to_preparse removeObject: o_number];
766 [o_outline_view deselectRow: i_row];
770 if( p_item->i_children == -1 )
772 playlist_PreparseEnqueue( p_playlist, p_item->p_input );
776 msg_Dbg( p_intf, "preparsing nodes not implemented" );
780 vlc_object_release( p_playlist );
781 [self playlistUpdated];
784 - (IBAction)servicesChange:(id)sender
786 NSMenuItem *o_mi = (NSMenuItem *)sender;
787 NSString *o_string = [o_mi representedObject];
788 playlist_t * p_playlist = pl_Yield( VLCIntf );
789 if( !playlist_IsServicesDiscoveryLoaded( p_playlist, [o_string UTF8String] ) )
790 playlist_ServicesDiscoveryAdd( p_playlist, [o_string UTF8String] );
792 playlist_ServicesDiscoveryRemove( p_playlist, [o_string UTF8String] );
794 [o_mi setState: playlist_IsServicesDiscoveryLoaded( p_playlist,
795 [o_string UTF8String] ) ? YES : NO];
797 vlc_object_release( p_playlist );
798 [self playlistUpdated];
802 - (IBAction)selectAll:(id)sender
804 [o_outline_view selectAll: nil];
807 - (IBAction)deleteItem:(id)sender
809 int i, i_count, i_row;
810 NSMutableArray *o_to_delete;
813 playlist_t * p_playlist;
814 intf_thread_t * p_intf = VLCIntf;
816 p_playlist = pl_Yield( p_intf );
818 o_to_delete = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
819 i_count = [o_to_delete count];
821 for( i = 0; i < i_count; i++ )
823 o_number = [o_to_delete lastObject];
824 i_row = [o_number intValue];
825 id o_item = [o_outline_view itemAtRow: i_row];
826 playlist_item_t *p_item = [o_item pointerValue];
827 [o_to_delete removeObject: o_number];
828 [o_outline_view deselectRow: i_row];
830 if( [[o_outline_view dataSource] outlineView:o_outline_view
831 numberOfChildrenOfItem: o_item] > 0 )
832 //is a node and not an item
834 if( p_playlist->status.i_status != PLAYLIST_STOPPED &&
835 [self isItem: p_playlist->status.p_item inNode:
836 ((playlist_item_t *)[o_item pointerValue])
837 checkItemExistence: NO] == YES )
839 // if current item is in selected node and is playing then stop playlist
840 playlist_Stop( p_playlist );
842 vlc_mutex_lock( &p_playlist->object_lock );
843 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
844 vlc_mutex_unlock( &p_playlist->object_lock );
848 playlist_DeleteFromInput( p_playlist, p_item->p_input->i_id, VLC_FALSE );
851 [self playlistUpdated];
852 vlc_object_release( p_playlist );
855 - (IBAction)sortNodeByName:(id)sender
857 [self sortNode: SORT_TITLE];
860 - (IBAction)sortNodeByAuthor:(id)sender
862 [self sortNode: SORT_ARTIST];
865 - (void)sortNode:(int)i_mode
867 playlist_t * p_playlist = pl_Yield( VLCIntf );
868 playlist_item_t * p_item;
870 if( [o_outline_view selectedRow] > -1 )
872 p_item = [[o_outline_view itemAtRow: [o_outline_view selectedRow]] pointerValue];
875 /*If no item is selected, sort the whole playlist*/
877 p_item = p_playlist->p_root_category;
880 if( p_item->i_children > -1 ) // the item is a node
882 vlc_mutex_lock( &p_playlist->object_lock );
883 playlist_RecursiveNodeSort( p_playlist, p_item, i_mode, ORDER_NORMAL );
884 vlc_mutex_unlock( &p_playlist->object_lock );
888 vlc_mutex_lock( &p_playlist->object_lock );
889 playlist_RecursiveNodeSort( p_playlist,
890 p_item->p_parent, i_mode, ORDER_NORMAL );
891 vlc_mutex_unlock( &p_playlist->object_lock );
893 vlc_object_release( p_playlist );
894 [self playlistUpdated];
897 - (input_item_t *)createItem:(NSDictionary *)o_one_item
899 intf_thread_t * p_intf = VLCIntf;
900 playlist_t * p_playlist = pl_Yield( p_intf );
902 input_item_t *p_input;
904 BOOL b_rem = FALSE, b_dir = FALSE;
905 NSString *o_uri, *o_name;
910 o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
911 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
912 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
914 /* Find the name for a disc entry ( i know, can you believe the trouble?) */
915 if( ( !o_name || [o_name isEqualToString:@""] ) && [o_uri rangeOfString: @"/dev/"].location != NSNotFound )
917 int i_count, i_index;
918 struct statfs *mounts = NULL;
920 i_count = getmntinfo (&mounts, MNT_NOWAIT);
921 /* getmntinfo returns a pointer to static data. Do not free. */
922 for( i_index = 0 ; i_index < i_count; i_index++ )
924 NSMutableString *o_temp, *o_temp2;
925 o_temp = [NSMutableString stringWithString: o_uri];
926 o_temp2 = [NSMutableString stringWithUTF8String: mounts[i_index].f_mntfromname];
927 [o_temp replaceOccurrencesOfString: @"/dev/rdisk" withString: @"/dev/disk" options:nil range:NSMakeRange(0, [o_temp length]) ];
928 [o_temp2 replaceOccurrencesOfString: @"s0" withString: @"" options:nil range:NSMakeRange(0, [o_temp2 length]) ];
929 [o_temp2 replaceOccurrencesOfString: @"s1" withString: @"" options:nil range:NSMakeRange(0, [o_temp2 length]) ];
931 if( strstr( [o_temp fileSystemRepresentation], [o_temp2 fileSystemRepresentation] ) != NULL )
933 o_name = [[NSFileManager defaultManager] displayNameAtPath: [NSString stringWithUTF8String:mounts[i_index].f_mntonname]];
937 /* If no name, then make a guess */
938 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
940 if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
941 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
942 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
944 /* All of this is to make sure CD's play when you D&D them on VLC */
945 /* Converts mountpoint to a /dev file */
948 NSMutableString *o_temp;
950 buf = (struct statfs *) malloc (sizeof(struct statfs));
951 statfs( [o_uri fileSystemRepresentation], buf );
952 psz_dev = strdup(buf->f_mntfromname);
953 o_temp = [NSMutableString stringWithUTF8String: psz_dev ];
954 [o_temp replaceOccurrencesOfString: @"/dev/disk" withString: @"/dev/rdisk" options:nil range:NSMakeRange(0, [o_temp length]) ];
955 [o_temp replaceOccurrencesOfString: @"s0" withString: @"" options:nil range:NSMakeRange(0, [o_temp length]) ];
956 [o_temp replaceOccurrencesOfString: @"s1" withString: @"" options:nil range:NSMakeRange(0, [o_temp length]) ];
960 p_input = input_ItemNew( p_playlist, [o_uri fileSystemRepresentation], [o_name UTF8String] );
966 for( i = 0; i < (int)[o_options count]; i++ )
968 input_ItemAddOption( p_input, strdup( [[o_options objectAtIndex:i] UTF8String] ) );
972 /* Recent documents menu */
973 o_true_file = [NSURL fileURLWithPath: o_uri];
974 if( o_true_file != nil && (BOOL)config_GetInt( p_playlist, "macosx-recentitems" ) == YES )
976 [[NSDocumentController sharedDocumentController]
977 noteNewRecentDocumentURL: o_true_file];
980 vlc_object_release( p_playlist );
984 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
987 playlist_t * p_playlist = pl_Yield( VLCIntf );
989 for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
991 input_item_t *p_input;
992 NSDictionary *o_one_item;
995 o_one_item = [o_array objectAtIndex: i_item];
996 p_input = [self createItem: o_one_item];
1003 playlist_AddInput( p_playlist, p_input, PLAYLIST_INSERT,
1004 i_position == -1 ? PLAYLIST_END : i_position + i_item, VLC_TRUE,
1007 if( i_item == 0 && !b_enqueue )
1009 playlist_item_t *p_item;
1010 p_item = playlist_ItemGetByInput( p_playlist, p_input, VLC_TRUE );
1011 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VLC_TRUE, NULL, p_item );
1015 playlist_item_t *p_item;
1016 p_item = playlist_ItemGetByInput( p_playlist, p_input, VLC_TRUE );
1017 playlist_Control( p_playlist, PLAYLIST_SKIP, VLC_TRUE, p_item );
1020 [self playlistUpdated];
1021 vlc_object_release( p_playlist );
1024 - (void)appendNodeArray:(NSArray*)o_array inNode:(playlist_item_t *)p_node atPos:(int)i_position enqueue:(BOOL)b_enqueue
1027 playlist_t * p_playlist = pl_Yield( VLCIntf );
1029 for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
1031 input_item_t *p_input;
1032 NSDictionary *o_one_item;
1035 o_one_item = [o_array objectAtIndex: i_item];
1036 p_input = [self createItem: o_one_item];
1043 playlist_NodeAddInput( p_playlist, p_input, p_node,
1046 PLAYLIST_END : i_position + i_item, VLC_FALSE );
1049 if( i_item == 0 && !b_enqueue )
1051 playlist_item_t *p_item;
1052 p_item = playlist_ItemGetByInput( p_playlist, p_input, VLC_TRUE );
1053 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VLC_TRUE, NULL, p_item );
1057 playlist_item_t *p_item;
1058 p_item = playlist_ItemGetByInput( p_playlist, p_input, VLC_TRUE );
1059 playlist_Control( p_playlist, PLAYLIST_SKIP, VLC_TRUE, p_item );
1062 [self playlistUpdated];
1063 vlc_object_release( p_playlist );
1066 - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
1068 playlist_t *p_playlist = pl_Yield( VLCIntf );
1069 playlist_item_t *p_selected_item;
1070 int i_current, i_selected_row;
1072 i_selected_row = [o_outline_view selectedRow];
1073 if (i_selected_row < 0)
1076 p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow:
1077 i_selected_row] pointerValue];
1079 for( i_current = 0; i_current < p_item->i_children ; i_current++ )
1082 NSString *o_current_name, *o_current_author;
1084 vlc_mutex_lock( &p_playlist->object_lock );
1085 o_current_name = [NSString stringWithUTF8String:
1086 p_item->pp_children[i_current]->p_input->psz_name];
1087 psz_temp = input_ItemGetInfo( p_item->p_input ,
1088 _("Meta-information"),_("Artist") );
1089 o_current_author = [NSString stringWithUTF8String: psz_temp];
1091 vlc_mutex_unlock( &p_playlist->object_lock );
1093 if( p_selected_item == p_item->pp_children[i_current] &&
1094 b_selected_item_met == NO )
1096 b_selected_item_met = YES;
1098 else if( p_selected_item == p_item->pp_children[i_current] &&
1099 b_selected_item_met == YES )
1101 vlc_object_release( p_playlist );
1104 else if( b_selected_item_met == YES &&
1105 ( [o_current_name rangeOfString:[o_search_field
1106 stringValue] options:NSCaseInsensitiveSearch ].length ||
1107 [o_current_author rangeOfString:[o_search_field
1108 stringValue] options:NSCaseInsensitiveSearch ].length ) )
1110 vlc_object_release( p_playlist );
1111 /*Adds the parent items in the result array as well, so that we can
1113 return [NSMutableArray arrayWithObject: [NSValue
1114 valueWithPointer: p_item->pp_children[i_current]]];
1116 if( p_item->pp_children[i_current]->i_children > 0 )
1118 id o_result = [self subSearchItem:
1119 p_item->pp_children[i_current]];
1120 if( o_result != NULL )
1122 vlc_object_release( p_playlist );
1123 [o_result insertObject: [NSValue valueWithPointer:
1124 p_item->pp_children[i_current]] atIndex:0];
1129 vlc_object_release( p_playlist );
1133 - (IBAction)searchItem:(id)sender
1135 playlist_t * p_playlist = pl_Yield( VLCIntf );
1141 b_selected_item_met = NO;
1143 /*First, only search after the selected item:*
1144 *(b_selected_item_met = NO) */
1145 o_result = [self subSearchItem:p_playlist->p_root_category];
1146 if( o_result == NULL )
1148 /* If the first search failed, search again from the beginning */
1149 o_result = [self subSearchItem:p_playlist->p_root_category];
1151 if( o_result != NULL )
1154 if( [[o_result objectAtIndex: 0] pointerValue] ==
1155 p_playlist->p_local_category )
1160 for( i = i_start ; i < [o_result count] - 1 ; i++ )
1162 [o_outline_view expandItem: [o_outline_dict objectForKey:
1163 [NSString stringWithFormat: @"%p",
1164 [[o_result objectAtIndex: i] pointerValue]]]];
1166 i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
1167 [NSString stringWithFormat: @"%p",
1168 [[o_result objectAtIndex: [o_result count] - 1 ]
1173 [o_outline_view selectRow:i_row byExtendingSelection: NO];
1174 [o_outline_view scrollRowToVisible: i_row];
1176 vlc_object_release( p_playlist );
1179 - (IBAction)recursiveExpandNode:(id)sender
1181 id o_item = [o_outline_view itemAtRow: [o_outline_view selectedRow]];
1182 playlist_item_t *p_item = (playlist_item_t *)[o_item pointerValue];
1184 if( ![[o_outline_view dataSource] outlineView: o_outline_view
1185 isItemExpandable: o_item] )
1187 o_item = [o_outline_dict objectForKey: [NSString
1188 stringWithFormat: @"%p", p_item->p_parent]];
1191 /* We need to collapse the node first, since OSX refuses to recursively
1192 expand an already expanded node, even if children nodes are collapsed. */
1193 [o_outline_view collapseItem: o_item collapseChildren: YES];
1194 [o_outline_view expandItem: o_item expandChildren: YES];
1197 - (NSMenu *)menuForEvent:(NSEvent *)o_event
1201 vlc_bool_t b_item_sel;
1203 pt = [o_outline_view convertPoint: [o_event locationInWindow]
1205 b_item_sel = ( [o_outline_view rowAtPoint: pt] != -1 &&
1206 [o_outline_view selectedRow] != -1 );
1207 b_rows = [o_outline_view numberOfRows] != 0;
1209 [o_mi_play setEnabled: b_item_sel];
1210 [o_mi_delete setEnabled: b_item_sel];
1211 [o_mi_selectall setEnabled: b_rows];
1212 [o_mi_info setEnabled: b_item_sel];
1213 [o_mi_preparse setEnabled: b_item_sel];
1214 [o_mi_recursive_expand setEnabled: b_item_sel];
1215 [o_mi_sort_name setEnabled: b_item_sel];
1216 [o_mi_sort_author setEnabled: b_item_sel];
1218 return( o_ctx_menu );
1221 - (void)outlineView: (NSTableView*)o_tv
1222 didClickTableColumn:(NSTableColumn *)o_tc
1224 int i_mode = 0, i_type;
1225 intf_thread_t *p_intf = VLCIntf;
1227 playlist_t *p_playlist = pl_Yield( p_intf );
1229 /* Check whether the selected table column header corresponds to a
1230 sortable table column*/
1231 if( !( o_tc == o_tc_name || o_tc == o_tc_author ) )
1233 vlc_object_release( p_playlist );
1237 if( o_tc_sortColumn == o_tc )
1239 b_isSortDescending = !b_isSortDescending;
1243 b_isSortDescending = VLC_FALSE;
1246 if( o_tc == o_tc_name )
1248 i_mode = SORT_TITLE;
1250 else if( o_tc == o_tc_author )
1252 i_mode = SORT_ARTIST;
1255 if( b_isSortDescending )
1257 i_type = ORDER_REVERSE;
1261 i_type = ORDER_NORMAL;
1264 vlc_mutex_lock( &p_playlist->object_lock );
1265 playlist_RecursiveNodeSort( p_playlist, p_playlist->p_root_category, i_mode, i_type );
1266 vlc_mutex_unlock( &p_playlist->object_lock );
1268 vlc_object_release( p_playlist );
1269 [self playlistUpdated];
1271 o_tc_sortColumn = o_tc;
1272 [o_outline_view setHighlightedTableColumn:o_tc];
1274 if( b_isSortDescending )
1276 [o_outline_view setIndicatorImage:o_descendingSortingImage
1277 inTableColumn:o_tc];
1281 [o_outline_view setIndicatorImage:o_ascendingSortingImage
1282 inTableColumn:o_tc];
1287 - (void)outlineView:(NSOutlineView *)outlineView
1288 willDisplayCell:(id)cell
1289 forTableColumn:(NSTableColumn *)tableColumn
1292 playlist_t *p_playlist = pl_Yield( VLCIntf );
1296 o_playing_item = [o_outline_dict objectForKey:
1297 [NSString stringWithFormat:@"%p", p_playlist->status.p_item]];
1299 if( [self isItem: [o_playing_item pointerValue] inNode:
1300 [item pointerValue] checkItemExistence: YES]
1301 || [o_playing_item isEqual: item] )
1303 [cell setFont: [NSFont boldSystemFontOfSize: 0]];
1307 [cell setFont: [NSFont systemFontOfSize: 0]];
1309 vlc_object_release( p_playlist );
1312 - (IBAction)addNode:(id)sender
1314 /* we have to create a new thread here because otherwise we would block the
1315 * interface since the interaction-stuff and this code would run in the same
1317 [NSThread detachNewThreadSelector: @selector(addNodeThreadedly)
1318 toTarget: self withObject:nil];
1319 [self playlistUpdated];
1322 - (void)addNodeThreadedly
1324 NSAutoreleasePool * ourPool = [[NSAutoreleasePool alloc] init];
1326 /* simply adds a new node to the end of the playlist */
1327 playlist_t * p_playlist = pl_Yield( VLCIntf );
1328 vlc_thread_set_priority( p_playlist, VLC_THREAD_PRIORITY_LOW );
1331 char *psz_name = NULL;
1332 playlist_item_t * p_item;
1333 ret_v = intf_UserStringInput( p_playlist, _("New Node"),
1334 _("Please enter a name for the new node."), &psz_name );
1336 if( psz_name != NULL && psz_name != "" )
1337 p_item = playlist_NodeCreate( p_playlist, psz_name,
1338 p_playlist->p_local_category, 0, NULL );
1339 else if(! config_GetInt( p_playlist, "interact" ) )
1341 /* in case that the interaction is disabled, just give it a bogus name */
1342 p_item = playlist_NodeCreate( p_playlist, _("Empty Folder"),
1343 p_playlist->p_local_category, 0, NULL );
1347 msg_Warn( VLCIntf, "node creation failed or cancelled by user" );
1349 vlc_object_release( p_playlist );
1355 @implementation VLCPlaylist (NSOutlineViewDataSource)
1357 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
1359 id o_value = [super outlineView: outlineView child: index ofItem: item];
1360 playlist_t *p_playlist = pl_Yield( VLCIntf );
1362 if( playlist_CurrentSize( p_playlist ) >= 2 )
1364 [o_status_field setStringValue: [NSString stringWithFormat:
1365 _NS("%i items in the playlist"),
1366 playlist_CurrentSize( p_playlist )]];
1370 if( playlist_IsEmpty( p_playlist ) )
1372 [o_status_field setStringValue: _NS("No items in the playlist")];
1376 [o_status_field setStringValue: _NS("1 item in the playlist")];
1379 vlc_object_release( p_playlist );
1381 [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p",
1382 [o_value pointerValue]]];
1384 msg_Dbg( VLCIntf, "adding item %p", [o_value pointerValue] );
1390 /* Required for drag & drop and reordering */
1391 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
1394 playlist_t *p_playlist = pl_Yield( VLCIntf );
1396 /* First remove the items that were moved during the last drag & drop
1398 [o_items_array removeAllObjects];
1399 [o_nodes_array removeAllObjects];
1401 for( i = 0 ; i < [items count] ; i++ )
1403 id o_item = [items objectAtIndex: i];
1405 /* Refuse to move items that are not in the General Node
1406 (Service Discovery) */
1407 if( ![self isItem: [o_item pointerValue] inNode:
1408 p_playlist->p_local_category checkItemExistence: NO])
1410 vlc_object_release(p_playlist);
1413 /* Fill the items and nodes to move in 2 different arrays */
1414 if( ((playlist_item_t *)[o_item pointerValue])->i_children > 0 )
1415 [o_nodes_array addObject: o_item];
1417 [o_items_array addObject: o_item];
1420 /* Now we need to check if there are selected items that are in already
1421 selected nodes. In that case, we only want to move the nodes */
1422 [self removeItemsFrom: o_nodes_array ifChildrenOf: o_nodes_array];
1423 [self removeItemsFrom: o_items_array ifChildrenOf: o_nodes_array];
1425 /* We add the "VLCPlaylistItemPboardType" type to be able to recognize
1426 a Drop operation coming from the playlist. */
1428 [pboard declareTypes: [NSArray arrayWithObjects:
1429 @"VLCPlaylistItemPboardType", nil] owner: self];
1430 [pboard setData:[NSData data] forType:@"VLCPlaylistItemPboardType"];
1432 vlc_object_release(p_playlist);
1436 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
1438 playlist_t *p_playlist = pl_Yield( VLCIntf );
1439 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1441 if( !p_playlist ) return NSDragOperationNone;
1443 /* Dropping ON items is not allowed if item is not a node */
1446 if( index == NSOutlineViewDropOnItemIndex &&
1447 ((playlist_item_t *)[item pointerValue])->i_children == -1 )
1449 vlc_object_release( p_playlist );
1450 return NSDragOperationNone;
1454 /* Don't allow on drop on playlist root element's child */
1455 if( !item && index != NSOutlineViewDropOnItemIndex)
1457 vlc_object_release( p_playlist );
1458 return NSDragOperationNone;
1461 /* We refuse to drop an item in anything else than a child of the General
1462 Node. We still accept items that would be root nodes of the outlineview
1463 however, to allow drop in an empty playlist. */
1464 if( !([self isItem: [item pointerValue] inNode: p_playlist->p_local_category
1465 checkItemExistence: NO] || item == nil) )
1467 vlc_object_release( p_playlist );
1468 return NSDragOperationNone;
1471 /* Drop from the Playlist */
1472 if( [[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] )
1475 for( i = 0 ; i < [o_nodes_array count] ; i++ )
1477 /* We refuse to Drop in a child of an item we are moving */
1478 if( [self isItem: [item pointerValue] inNode:
1479 [[o_nodes_array objectAtIndex: i] pointerValue]
1480 checkItemExistence: NO] )
1482 vlc_object_release( p_playlist );
1483 return NSDragOperationNone;
1486 vlc_object_release( p_playlist );
1487 return NSDragOperationMove;
1490 /* Drop from the Finder */
1491 else if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1493 vlc_object_release( p_playlist );
1494 return NSDragOperationGeneric;
1496 vlc_object_release( p_playlist );
1497 return NSDragOperationNone;
1500 - (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(int)index
1502 playlist_t * p_playlist = pl_Yield( VLCIntf );
1503 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1505 /* Drag & Drop inside the playlist */
1506 if( [[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] )
1508 int i_row, i_removed_from_node = 0;
1510 playlist_item_t *p_new_parent, *p_item = NULL;
1511 NSArray *o_all_items = [o_nodes_array arrayByAddingObjectsFromArray:
1513 /* If the item is to be dropped as root item of the outline, make it a
1514 child of the General node.
1515 Else, choose the proposed parent as parent. */
1516 if( item == nil ) p_new_parent = p_playlist->p_local_category;
1517 else p_new_parent = [item pointerValue];
1519 /* Make sure the proposed parent is a node.
1520 (This should never be true) */
1521 if( p_new_parent->i_children < 0 )
1523 vlc_object_release( p_playlist );
1527 for( i = 0; i < [o_all_items count]; i++ )
1529 playlist_item_t *p_old_parent = NULL;
1530 int i_old_index = 0;
1532 p_item = [[o_all_items objectAtIndex:i] pointerValue];
1533 p_old_parent = p_item->p_parent;
1536 /* We may need the old index later */
1537 if( p_new_parent == p_old_parent )
1540 for( j = 0; j < p_old_parent->i_children; j++ )
1542 if( p_old_parent->pp_children[j] == p_item )
1550 vlc_mutex_lock( &p_playlist->object_lock );
1551 // Acually detach the item from the old position
1552 if( playlist_NodeRemoveItem( p_playlist, p_item, p_old_parent ) ==
1556 /* Calculate the new index */
1559 /* If we move the item in the same node, we need to take into
1560 account that one item will be deleted */
1563 if ((p_new_parent == p_old_parent &&
1564 i_old_index < index + (int)i) )
1566 i_removed_from_node++;
1568 i_new_index = index + i - i_removed_from_node;
1570 // Reattach the item to the new position
1571 playlist_NodeInsert( p_playlist, p_item, p_new_parent, i_new_index );
1573 vlc_mutex_unlock( &p_playlist->object_lock );
1575 [self playlistUpdated];
1576 i_row = [o_outline_view rowForItem:[o_outline_dict
1577 objectForKey:[NSString stringWithFormat: @"%p",
1578 [[o_all_items objectAtIndex: 0] pointerValue]]]];
1582 i_row = [o_outline_view rowForItem:[o_outline_dict
1583 objectForKey:[NSString stringWithFormat: @"%p", p_new_parent]]];
1586 [o_outline_view deselectAll: self];
1587 [o_outline_view selectRow: i_row byExtendingSelection: NO];
1588 [o_outline_view scrollRowToVisible: i_row];
1590 vlc_object_release( p_playlist );
1594 else if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1597 playlist_item_t *p_node = [item pointerValue];
1599 NSArray *o_array = [NSArray array];
1600 NSArray *o_values = [[o_pasteboard propertyListForType:
1601 NSFilenamesPboardType]
1602 sortedArrayUsingSelector:
1603 @selector(caseInsensitiveCompare:)];
1605 for( i = 0; i < (int)[o_values count]; i++)
1607 NSDictionary *o_dic;
1608 o_dic = [NSDictionary dictionaryWithObject:[o_values
1609 objectAtIndex:i] forKey:@"ITEM_URL"];
1610 o_array = [o_array arrayByAddingObject: o_dic];
1615 [self appendArray: o_array atPos: index enqueue: YES];
1617 /* This should never occur */
1618 else if( p_node->i_children == -1 )
1620 vlc_object_release( p_playlist );
1625 [self appendNodeArray: o_array inNode: p_node
1626 atPos: index enqueue: YES];
1628 vlc_object_release( p_playlist );
1631 vlc_object_release( p_playlist );