1 /*****************************************************************************
2 * playlist.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2002-2005 VideoLAN
7 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8 * Derk-Jan Hartman <hartman at videolan 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
27 * add 'icons' for different types of nodes? (http://www.cocoadev.com/index.pl?IconAndTextInTableCell)
28 * create a new search field build with pictures from the 'regular' search field, so it can be emulated on 10.2
29 * create toggle buttons for the shuffle, repeat one, repeat all functions.
30 * implement drag and drop and item reordering.
31 * reimplement enable/disable item
32 * create a new 'tool' button (see the gear button in the Finder window) for 'actions'
33 (adding service discovery, other views, new node/playlist, save node/playlist) stuff like that
37 /*****************************************************************************
39 *****************************************************************************/
40 #include <stdlib.h> /* malloc(), free() */
41 #include <sys/param.h> /* for MAXPATHLEN */
44 #include <sys/mount.h>
53 /*****************************************************************************
54 * VLCPlaylistView implementation
55 *****************************************************************************/
56 @implementation VLCPlaylistView
58 - (NSMenu *)menuForEvent:(NSEvent *)o_event
60 return( [[self delegate] menuForEvent: o_event] );
63 - (void)keyDown:(NSEvent *)o_event
67 if( [[o_event characters] length] )
69 key = [[o_event characters] characterAtIndex: 0];
74 case NSDeleteCharacter:
75 case NSDeleteFunctionKey:
76 case NSDeleteCharFunctionKey:
77 case NSBackspaceCharacter:
78 [[self delegate] deleteItem:self];
81 case NSEnterCharacter:
82 case NSCarriageReturnCharacter:
83 [(VLCPlaylist *)[[VLCMain sharedInstance] getPlaylist]
88 [super keyDown: o_event];
95 /*****************************************************************************
96 * VLCPlaylist implementation
97 *****************************************************************************/
98 @implementation VLCPlaylist
105 o_outline_dict = [[NSMutableDictionary alloc] init];
106 o_nodes_array = [[NSMutableArray alloc] init];
107 o_items_array = [[NSMutableArray alloc] init];
117 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
119 vlc_list_t *p_list = vlc_list_find( p_playlist, VLC_OBJECT_MODULE,
123 i_current_view = VIEW_CATEGORY;
124 playlist_ViewUpdate( p_playlist, i_current_view );
126 [o_outline_view setTarget: self];
127 [o_outline_view setDelegate: self];
128 [o_outline_view setDataSource: self];
130 [o_outline_view setDoubleAction: @selector(playItem:)];
132 [o_outline_view registerForDraggedTypes:
133 [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
134 [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
136 /* We need to check whether _defaultTableHeaderSortImage exists, since it
137 belongs to an Apple hidden private API, and then can "disapear" at any time*/
139 if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderSortImage)] )
141 o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
145 o_ascendingSortingImage = nil;
148 if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderReverseSortImage)] )
150 o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
154 o_descendingSortingImage = nil;
157 o_tc_sortColumn = nil;
159 for( i_index = 0; i_index < p_list->i_count; i_index++ )
162 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
164 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
166 /* create the menu entries used in the playlist menu */
167 o_lmi = [[o_mi_services submenu] addItemWithTitle:
168 [NSString stringWithUTF8String:
169 p_parser->psz_longname ? p_parser->psz_longname :
170 ( p_parser->psz_shortname ? p_parser->psz_shortname:
171 p_parser->psz_object_name)]
172 action: @selector(servicesChange:)
174 [o_lmi setTarget: self];
175 [o_lmi setRepresentedObject:
176 [NSString stringWithCString: p_parser->psz_object_name]];
177 if( playlist_IsServicesDiscoveryLoaded( p_playlist,
178 p_parser->psz_object_name ) )
179 [o_lmi setState: NSOnState];
181 /* create the menu entries for the main menu */
182 o_lmi = [[o_mm_mi_services submenu] addItemWithTitle:
183 [NSString stringWithUTF8String:
184 p_parser->psz_longname ? p_parser->psz_longname :
185 ( p_parser->psz_shortname ? p_parser->psz_shortname:
186 p_parser->psz_object_name)]
187 action: @selector(servicesChange:)
189 [o_lmi setTarget: self];
190 [o_lmi setRepresentedObject:
191 [NSString stringWithCString: p_parser->psz_object_name]];
192 if( playlist_IsServicesDiscoveryLoaded( p_playlist,
193 p_parser->psz_object_name ) )
194 [o_lmi setState: NSOnState];
197 vlc_list_release( p_list );
198 vlc_object_release( p_playlist );
200 /* Change the simple textfield into a searchField if we can... */
202 if( MACOS_VERSION >= 10.3 )
204 NSView *o_parentview = [o_status_field superview];
205 NSSearchField *o_better_search_field = [[NSSearchField alloc]initWithFrame:[o_search_field frame]];
206 [o_better_search_field setRecentsAutosaveName:@"VLC media player search"];
207 [o_better_search_field setDelegate:self];
208 [[NSNotificationCenter defaultCenter] addObserver: self
209 selector: @selector(searchfieldChanged:)
210 name: NSControlTextDidChangeNotification
211 object: o_better_search_field];
213 [o_better_search_field setTarget:self];
214 [o_better_search_field setAction:@selector(searchItem:)];
216 [o_better_search_field setAutoresizingMask:NSViewMinXMargin];
217 [o_parentview addSubview:o_better_search_field];
218 [o_search_field setHidden:YES];
222 //[self playlistUpdated];
225 - (void)searchfieldChanged:(NSNotification *)o_notification
227 [o_search_field setStringValue:[[o_notification object] stringValue]];
232 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
233 [o_mi_play setTitle: _NS("Play")];
234 [o_mi_delete setTitle: _NS("Delete")];
235 [o_mi_recursive_expand setTitle: _NS("Expand Node")];
236 [o_mi_selectall setTitle: _NS("Select All")];
237 [o_mi_info setTitle: _NS("Properties")];
238 [o_mi_sort_name setTitle: _NS("Sort Node by Name")];
239 [o_mi_sort_author setTitle: _NS("Sort Node by Author")];
240 [o_mi_services setTitle: _NS("Services discovery")];
241 [[o_tc_name headerCell] setStringValue:_NS("Name")];
242 [[o_tc_author headerCell] setStringValue:_NS("Author")];
243 [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
244 [o_status_field setStringValue: [NSString stringWithFormat:
245 _NS("no items in playlist")]];
247 [o_random_ckb setTitle: _NS("Random")];
249 [o_search_button setTitle: _NS("Search")];
251 [o_search_field setToolTip: _NS("Search in Playlist")];
252 [[o_loop_popup itemAtIndex:0] setTitle: _NS("Standard Play")];
253 [[o_loop_popup itemAtIndex:1] setTitle: _NS("Repeat One")];
254 [[o_loop_popup itemAtIndex:2] setTitle: _NS("Repeat All")];
257 - (NSOutlineView *)outlineView
259 return o_outline_view;
262 - (void)playlistUpdated
266 /* Clear indications of any existing column sorting*/
267 for( i = 0 ; i < [[o_outline_view tableColumns] count] ; i++ )
269 [o_outline_view setIndicatorImage:nil inTableColumn:
270 [[o_outline_view tableColumns] objectAtIndex:i]];
273 [o_outline_view setHighlightedTableColumn:nil];
274 o_tc_sortColumn = nil;
275 // TODO Find a way to keep the dict size to a minimum
276 //[o_outline_dict removeAllObjects];
277 [o_outline_view reloadData];
280 - (void)playModeUpdated
282 playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
284 vlc_value_t val, val2;
286 if( p_playlist == NULL )
291 var_Get( p_playlist, "loop", &val2 );
292 var_Get( p_playlist, "repeat", &val );
293 if( val.b_bool == VLC_TRUE )
295 [o_loop_popup selectItemAtIndex: 1];
297 else if( val2.b_bool == VLC_TRUE )
299 [o_loop_popup selectItemAtIndex: 2];
303 [o_loop_popup selectItemAtIndex: 0];
306 var_Get( p_playlist, "random", &val );
307 [o_random_ckb setState: val.b_bool];
309 vlc_object_release( p_playlist );
312 - (void)updateRowSelection
317 playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
319 playlist_item_t *p_item, *p_temp_item;
320 NSMutableArray *o_array = [NSMutableArray array];
322 if( p_playlist == NULL )
325 p_item = p_playlist->status.p_item;
326 if( p_item == NULL ) return;
328 p_temp_item = p_item;
329 while( p_temp_item->i_parents > 0 )
331 [o_array insertObject: [NSValue valueWithPointer: p_temp_item] atIndex: 0];
332 for (i = 0 ; i < p_temp_item->i_parents ; i++)
334 if( p_temp_item->pp_parents[i]->i_view == i_current_view )
336 p_temp_item = p_temp_item->pp_parents[i]->p_parent;
342 for (j = 0 ; j < [o_array count] - 1 ; j++)
345 if( ( o_item = [o_outline_dict objectForKey:
346 [NSString stringWithFormat: @"%p",
347 [[o_array objectAtIndex:j] pointerValue]]] ) != nil )
348 [o_outline_view expandItem: o_item];
352 i_row = [o_outline_view rowForItem:[o_outline_dict
353 objectForKey:[NSString stringWithFormat: @"%p", p_item]]];
355 [o_outline_view selectRow: i_row byExtendingSelection: NO];
356 [o_outline_view scrollRowToVisible: i_row];
358 vlc_object_release(p_playlist);
361 - (BOOL)isItem: (playlist_item_t *)p_item inNode: (playlist_item_t *)p_node
363 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
365 playlist_item_t *p_temp_item = p_item;
367 if( p_playlist == NULL )
372 if( p_node == p_item ) return YES;
377 vlc_mutex_lock( &p_playlist->object_lock );
379 // Let's check if we can do without that
382 /* Since outlineView: willDisplayCell:... may call this function with
383 p_items that don't exist anymore, first check if the item is still
384 in the playlist. Any cleaner solution welcomed. */
387 if( p_playlist->pp_all_items[i] == p_item ) break;
388 else if ( i == p_playlist->i_all_size - 1 )
390 vlc_object_release( p_playlist );
391 vlc_mutex_unlock( &p_playlist->object_lock );
396 while( p_temp_item->i_parents > 0 )
398 for( i = 0; i < p_temp_item->i_parents ; i++ )
400 if( p_temp_item->pp_parents[i]->i_view == i_current_view )
402 if( p_temp_item->pp_parents[i]->p_parent == p_node )
404 vlc_mutex_unlock( &p_playlist->object_lock );
405 vlc_object_release( p_playlist );
410 p_temp_item = p_temp_item->pp_parents[i]->p_parent;
415 vlc_mutex_unlock( &p_playlist->object_lock );
419 vlc_object_release( p_playlist );
423 - (BOOL)isValueItem: (id)o_item inNode: (id)o_node
426 int i_total = [[o_outline_view dataSource] outlineView:o_outline_view
427 numberOfChildrenOfItem: o_node];
428 for( i = 0 ; i < i_total ; i++ )
430 id o_temp_item = [[o_outline_view dataSource] outlineView:
431 o_outline_view child:i ofItem: o_node];
432 if( [[o_outline_view dataSource] outlineView:o_outline_view
433 numberOfChildrenOfItem: o_temp_item] > 0 )
435 if( [self isValueItem: o_item inNode: o_temp_item] == YES )
438 else if( [o_temp_item isEqual: o_item] )
446 - (IBAction)savePlaylist:(id)sender
448 intf_thread_t * p_intf = VLCIntf;
449 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
452 NSSavePanel *o_save_panel = [NSSavePanel savePanel];
453 NSString * o_name = [NSString stringWithFormat: @"%@.m3u", _NS("Untitled")];
454 [o_save_panel setTitle: _NS("Save Playlist")];
455 [o_save_panel setPrompt: _NS("Save")];
457 if( [o_save_panel runModalForDirectory: nil
458 file: o_name] == NSOKButton )
460 playlist_Export( p_playlist, [[o_save_panel filename] fileSystemRepresentation], "export-m3u" );
465 /* When called retrieves the selected outlineview row and plays that node or item */
466 - (IBAction)playItem:(id)sender
468 intf_thread_t * p_intf = VLCIntf;
469 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
472 if( p_playlist != NULL )
474 playlist_item_t *p_item;
475 playlist_item_t *p_node = NULL;
478 p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
482 if( p_item->i_children == -1 )
484 for( i = 0 ; i < p_item->i_parents ; i++ )
486 if( p_item->pp_parents[i]->i_view == i_current_view )
488 p_node = p_item->pp_parents[i]->p_parent;
495 if( p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1 )
497 p_item = p_node->pp_children[0];
504 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view, p_node, p_item );
506 vlc_object_release( p_playlist );
510 - (IBAction)servicesChange:(id)sender
512 NSMenuItem *o_mi = (NSMenuItem *)sender;
513 NSString *o_string = [o_mi representedObject];
514 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
516 if( !playlist_IsServicesDiscoveryLoaded( p_playlist, [o_string cString] ) )
517 playlist_ServicesDiscoveryAdd( p_playlist, [o_string cString] );
519 playlist_ServicesDiscoveryRemove( p_playlist, [o_string cString] );
521 [o_mi setState: playlist_IsServicesDiscoveryLoaded( p_playlist,
522 [o_string cString] ) ? YES : NO];
524 i_current_view = VIEW_CATEGORY;
525 playlist_ViewUpdate( p_playlist, i_current_view );
526 vlc_object_release( p_playlist );
527 [self playlistUpdated];
531 - (IBAction)selectAll:(id)sender
533 [o_outline_view selectAll: nil];
536 - (IBAction)deleteItem:(id)sender
538 int i, i_count, i_row;
539 NSMutableArray *o_to_delete;
542 playlist_t * p_playlist;
543 intf_thread_t * p_intf = VLCIntf;
545 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
548 if ( p_playlist == NULL )
552 o_to_delete = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
553 i_count = [o_to_delete count];
555 for( i = 0; i < i_count; i++ )
557 o_number = [o_to_delete lastObject];
558 i_row = [o_number intValue];
559 id o_item = [o_outline_view itemAtRow: i_row];
560 playlist_item_t *p_item = [o_item pointerValue];
561 [o_to_delete removeObject: o_number];
562 [o_outline_view deselectRow: i_row];
564 if( [[o_outline_view dataSource] outlineView:o_outline_view
565 numberOfChildrenOfItem: o_item] > 0 )
566 //is a node and not an item
568 if( p_playlist->status.i_status != PLAYLIST_STOPPED &&
569 [self isItem: p_playlist->status.p_item inNode:
570 ((playlist_item_t *)[o_item pointerValue])] == YES )
572 // if current item is in selected node and is playing then stop playlist
573 playlist_Stop( p_playlist );
575 vlc_mutex_lock( &p_playlist->object_lock );
576 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
577 vlc_mutex_unlock( &p_playlist->object_lock );
581 if( p_playlist->status.i_status != PLAYLIST_STOPPED &&
582 p_playlist->status.p_item == [[o_outline_view itemAtRow: i_row] pointerValue] )
584 playlist_Stop( p_playlist );
586 vlc_mutex_lock( &p_playlist->object_lock );
587 playlist_Delete( p_playlist, p_item->input.i_id );
588 vlc_mutex_unlock( &p_playlist->object_lock );
591 [self playlistUpdated];
592 vlc_object_release( p_playlist );
595 - (IBAction)sortNodeByName:(id)sender
597 [self sortNode: SORT_TITLE];
600 - (IBAction)sortNodeByAuthor:(id)sender
602 [self sortNode: SORT_AUTHOR];
605 - (void)sortNode:(int)i_mode
607 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
609 playlist_item_t * p_item;
611 if (p_playlist == NULL)
616 if( [o_outline_view selectedRow] > -1 )
618 p_item = [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
622 /*If no item is selected, sort the whole playlist*/
624 playlist_view_t * p_view = playlist_ViewFind( p_playlist, i_current_view );
625 p_item = p_view->p_root;
628 if( p_item->i_children > -1 ) // the item is a node
630 vlc_mutex_lock( &p_playlist->object_lock );
631 playlist_RecursiveNodeSort( p_playlist, p_item, i_mode, ORDER_NORMAL );
632 vlc_mutex_unlock( &p_playlist->object_lock );
638 for( i = 0 ; i < p_item->i_parents ; i++ )
640 if( p_item->pp_parents[i]->i_view == i_current_view )
642 vlc_mutex_lock( &p_playlist->object_lock );
643 playlist_RecursiveNodeSort( p_playlist,
644 p_item->pp_parents[i]->p_parent, i_mode, ORDER_NORMAL );
645 vlc_mutex_unlock( &p_playlist->object_lock );
650 vlc_object_release( p_playlist );
651 [self playlistUpdated];
654 - (playlist_item_t *)createItem:(NSDictionary *)o_one_item
656 intf_thread_t * p_intf = VLCIntf;
657 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
660 if( p_playlist == NULL )
664 playlist_item_t *p_item;
666 BOOL b_rem = FALSE, b_dir = FALSE;
667 NSString *o_uri, *o_name;
672 o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
673 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
674 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
676 /* Find the name for a disc entry ( i know, can you believe the trouble?) */
677 if( ( !o_name || [o_name isEqualToString:@""] ) && [o_uri rangeOfString: @"/dev/"].location != NSNotFound )
679 int i_count, i_index;
680 struct statfs *mounts = NULL;
682 i_count = getmntinfo (&mounts, MNT_NOWAIT);
683 /* getmntinfo returns a pointer to static data. Do not free. */
684 for( i_index = 0 ; i_index < i_count; i_index++ )
686 NSMutableString *o_temp, *o_temp2;
687 o_temp = [NSMutableString stringWithString: o_uri];
688 o_temp2 = [NSMutableString stringWithCString: mounts[i_index].f_mntfromname];
689 [o_temp replaceOccurrencesOfString: @"/dev/rdisk" withString: @"/dev/disk" options:NULL range:NSMakeRange(0, [o_temp length]) ];
690 [o_temp2 replaceOccurrencesOfString: @"s0" withString: @"" options:NULL range:NSMakeRange(0, [o_temp2 length]) ];
691 [o_temp2 replaceOccurrencesOfString: @"s1" withString: @"" options:NULL range:NSMakeRange(0, [o_temp2 length]) ];
693 if( strstr( [o_temp fileSystemRepresentation], [o_temp2 fileSystemRepresentation] ) != NULL )
695 o_name = [[NSFileManager defaultManager] displayNameAtPath: [NSString stringWithCString:mounts[i_index].f_mntonname]];
699 /* If no name, then make a guess */
700 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
702 if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
703 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
704 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
706 /* All of this is to make sure CD's play when you D&D them on VLC */
707 /* Converts mountpoint to a /dev file */
710 NSMutableString *o_temp;
712 buf = (struct statfs *) malloc (sizeof(struct statfs));
713 statfs( [o_uri fileSystemRepresentation], buf );
714 psz_dev = strdup(buf->f_mntfromname);
715 o_temp = [NSMutableString stringWithCString: psz_dev ];
716 [o_temp replaceOccurrencesOfString: @"/dev/disk" withString: @"/dev/rdisk" options:NULL range:NSMakeRange(0, [o_temp length]) ];
717 [o_temp replaceOccurrencesOfString: @"s0" withString: @"" options:NULL range:NSMakeRange(0, [o_temp length]) ];
718 [o_temp replaceOccurrencesOfString: @"s1" withString: @"" options:NULL range:NSMakeRange(0, [o_temp length]) ];
722 p_item = playlist_ItemNew( p_intf, [o_uri fileSystemRepresentation], [o_name UTF8String] );
728 for( i = 0; i < (int)[o_options count]; i++ )
730 playlist_ItemAddOption( p_item, strdup( [[o_options objectAtIndex:i] UTF8String] ) );
734 /* Recent documents menu */
735 o_true_file = [NSURL fileURLWithPath: o_uri];
736 if( o_true_file != nil )
738 [[NSDocumentController sharedDocumentController]
739 noteNewRecentDocumentURL: o_true_file];
742 vlc_object_release( p_playlist );
746 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
749 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
751 if( p_playlist == NULL )
756 for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
758 playlist_item_t *p_item;
759 NSDictionary *o_one_item;
762 o_one_item = [o_array objectAtIndex: i_item];
763 p_item = [self createItem: o_one_item];
770 playlist_AddItem( p_playlist, p_item, PLAYLIST_APPEND, i_position == -1 ? PLAYLIST_END : i_position + i_item );
772 if( i_item == 0 && !b_enqueue )
774 playlist_Control( p_playlist, PLAYLIST_ITEMPLAY, p_item );
777 vlc_object_release( p_playlist );
780 - (void)appendNodeArray:(NSArray*)o_array inNode:(playlist_item_t *)p_node atPos:(int)i_position inView:(int)i_view enqueue:(BOOL)b_enqueue
783 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
785 if( p_playlist == NULL )
790 for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
792 playlist_item_t *p_item;
793 NSDictionary *o_one_item;
796 o_one_item = [o_array objectAtIndex: i_item];
797 p_item = [self createItem: o_one_item];
804 playlist_NodeAddItem( p_playlist, p_item, i_view, p_node, PLAYLIST_APPEND, i_position + i_item );
806 if( i_item == 0 && !b_enqueue )
808 playlist_Control( p_playlist, PLAYLIST_ITEMPLAY, p_item );
811 vlc_object_release( p_playlist );
815 - (IBAction)handlePopUp:(id)sender
818 intf_thread_t * p_intf = VLCIntf;
819 vlc_value_t val1,val2;
820 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
822 if( p_playlist == NULL )
827 switch( [o_loop_popup indexOfSelectedItem] )
832 var_Set( p_playlist, "loop", val1 );
834 var_Set( p_playlist, "repeat", val1 );
835 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat One" ) );
840 var_Set( p_playlist, "repeat", val1 );
842 var_Set( p_playlist, "loop", val1 );
843 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat All" ) );
847 var_Get( p_playlist, "repeat", &val1 );
848 var_Get( p_playlist, "loop", &val2 );
849 if( val1.b_bool || val2.b_bool )
852 var_Set( p_playlist, "repeat", val1 );
853 var_Set( p_playlist, "loop", val1 );
854 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
858 vlc_object_release( p_playlist );
859 [self playlistUpdated];
862 - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
864 playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
866 playlist_item_t *p_selected_item;
867 int i_current, i_selected_row;
872 i_selected_row = [o_outline_view selectedRow];
873 if (i_selected_row < 0)
876 p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow:
877 i_selected_row] pointerValue];
879 for( i_current = 0; i_current < p_item->i_children ; i_current++ )
882 NSString *o_current_name, *o_current_author;
884 vlc_mutex_lock( &p_playlist->object_lock );
885 o_current_name = [NSString stringWithUTF8String:
886 p_item->pp_children[i_current]->input.psz_name];
887 psz_temp = vlc_input_item_GetInfo( &p_item->input ,
888 _("Meta-information"),_("Artist") );
889 o_current_author = [NSString stringWithUTF8String: psz_temp];
891 vlc_mutex_unlock( &p_playlist->object_lock );
893 if( p_selected_item == p_item->pp_children[i_current] &&
894 b_selected_item_met == NO )
896 b_selected_item_met = YES;
898 else if( p_selected_item == p_item->pp_children[i_current] &&
899 b_selected_item_met == YES )
901 vlc_object_release( p_playlist );
904 else if( b_selected_item_met == YES &&
905 ( [o_current_name rangeOfString:[o_search_field
906 stringValue] options:NSCaseInsensitiveSearch ].length ||
907 [o_current_author rangeOfString:[o_search_field
908 stringValue] options:NSCaseInsensitiveSearch ].length ) )
910 vlc_object_release( p_playlist );
911 /*Adds the parent items in the result array as well, so that we can
913 return [NSMutableArray arrayWithObject: [NSValue
914 valueWithPointer: p_item->pp_children[i_current]]];
916 if( p_item->pp_children[i_current]->i_children > 0 )
918 id o_result = [self subSearchItem:
919 p_item->pp_children[i_current]];
920 if( o_result != NULL )
922 vlc_object_release( p_playlist );
923 [o_result insertObject: [NSValue valueWithPointer:
924 p_item->pp_children[i_current]] atIndex:0];
929 vlc_object_release( p_playlist );
933 - (IBAction)searchItem:(id)sender
935 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
937 playlist_view_t * p_view;
943 b_selected_item_met = NO;
945 if( p_playlist == NULL )
947 p_view = playlist_ViewFind( p_playlist, i_current_view );
951 /*First, only search after the selected item:*
952 *(b_selected_item_met = NO) */
953 o_result = [self subSearchItem:p_view->p_root];
954 if( o_result == NULL )
956 /* If the first search failed, search again from the beginning */
957 o_result = [self subSearchItem:p_view->p_root];
959 if( o_result != NULL )
962 if( [[o_result objectAtIndex: 0] pointerValue] ==
963 p_playlist->p_general )
968 for( i = i_start ; i < [o_result count] - 1 ; i++ )
970 [o_outline_view expandItem: [o_outline_dict objectForKey:
971 [NSString stringWithFormat: @"%p",
972 [[o_result objectAtIndex: i] pointerValue]]]];
974 i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
975 [NSString stringWithFormat: @"%p",
976 [[o_result objectAtIndex: [o_result count] - 1 ]
981 [o_outline_view selectRow:i_row byExtendingSelection: NO];
982 [o_outline_view scrollRowToVisible: i_row];
985 vlc_object_release( p_playlist );
988 - (IBAction)recursiveExpandNode:(id)sender
991 id o_item = [o_outline_view itemAtRow: [o_outline_view selectedRow]];
992 playlist_item_t *p_item = (playlist_item_t *)[o_item pointerValue];
994 if( ![[o_outline_view dataSource] outlineView: o_outline_view
995 isItemExpandable: o_item] )
997 for( i = 0 ; i < p_item->i_parents ; i++ )
999 if( p_item->pp_parents[i]->i_view == i_current_view )
1001 o_item = [o_outline_dict objectForKey: [NSString
1002 stringWithFormat: @"%p", p_item->pp_parents[i]->p_parent]];
1008 /* We need to collapse the node first, since OSX refuses to recursively
1009 expand an already expanded node, even if children nodes are collapsed. */
1010 [o_outline_view collapseItem: o_item collapseChildren: YES];
1011 [o_outline_view expandItem: o_item expandChildren: YES];
1014 - (NSMenu *)menuForEvent:(NSEvent *)o_event
1018 vlc_bool_t b_item_sel;
1020 pt = [o_outline_view convertPoint: [o_event locationInWindow]
1022 b_item_sel = ( [o_outline_view rowAtPoint: pt] != -1 &&
1023 [o_outline_view selectedRow] != -1 );
1024 b_rows = [o_outline_view numberOfRows] != 0;
1026 [o_mi_play setEnabled: b_item_sel];
1027 [o_mi_delete setEnabled: b_item_sel];
1028 [o_mi_selectall setEnabled: b_rows];
1029 [o_mi_info setEnabled: b_item_sel];
1030 [o_mi_recursive_expand setEnabled: b_item_sel];
1031 [o_mi_sort_name setEnabled: b_item_sel];
1032 [o_mi_sort_author setEnabled: b_item_sel];
1034 return( o_ctx_menu );
1037 - (playlist_item_t *)selectedPlaylistItem
1039 return [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
1043 - (void)outlineView: (NSTableView*)o_tv
1044 didClickTableColumn:(NSTableColumn *)o_tc
1046 int i_mode = 0, i_type;
1047 intf_thread_t *p_intf = VLCIntf;
1048 playlist_view_t *p_view;
1050 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1052 if( p_playlist == NULL )
1057 /* Check whether the selected table column header corresponds to a
1058 sortable table column*/
1059 if( !( o_tc == o_tc_name || o_tc == o_tc_author ) )
1061 vlc_object_release( p_playlist );
1065 p_view = playlist_ViewFind( p_playlist, i_current_view );
1067 if( o_tc_sortColumn == o_tc )
1069 b_isSortDescending = !b_isSortDescending;
1073 b_isSortDescending = VLC_FALSE;
1076 if( o_tc == o_tc_name )
1078 i_mode = SORT_TITLE;
1080 else if( o_tc == o_tc_author )
1082 i_mode = SORT_AUTHOR;
1085 if( b_isSortDescending )
1087 i_type = ORDER_REVERSE;
1091 i_type = ORDER_NORMAL;
1094 vlc_mutex_lock( &p_playlist->object_lock );
1095 playlist_RecursiveNodeSort( p_playlist, p_view->p_root, i_mode, i_type );
1096 vlc_mutex_unlock( &p_playlist->object_lock );
1098 vlc_object_release( p_playlist );
1099 [self playlistUpdated];
1101 o_tc_sortColumn = o_tc;
1102 [o_outline_view setHighlightedTableColumn:o_tc];
1104 if( b_isSortDescending )
1106 [o_outline_view setIndicatorImage:o_descendingSortingImage
1107 inTableColumn:o_tc];
1111 [o_outline_view setIndicatorImage:o_ascendingSortingImage
1112 inTableColumn:o_tc];
1117 - (void)outlineView:(NSOutlineView *)outlineView
1118 willDisplayCell:(id)cell
1119 forTableColumn:(NSTableColumn *)tableColumn
1122 playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1127 if( !p_playlist ) return;
1129 o_playing_item = [o_outline_dict objectForKey:
1130 [NSString stringWithFormat:@"%p", p_playlist->status.p_item]];
1132 if( [self isValueItem: o_playing_item inNode: item] ||
1133 [o_playing_item isEqual: item] )
1135 [cell setFont: [NSFont boldSystemFontOfSize: 0]];
1139 [cell setFont: [NSFont systemFontOfSize: 0]];
1141 vlc_object_release( p_playlist );
1146 @implementation VLCPlaylist (NSOutlineViewDataSource)
1148 /* return the number of children for Obj-C pointer item */ /* DONE */
1149 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
1152 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1154 if( p_playlist == NULL || outlineView != o_outline_view )
1160 playlist_view_t *p_view;
1161 p_view = playlist_ViewFind( p_playlist, i_current_view );
1162 if( p_view && p_view->p_root )
1164 i_return = p_view->p_root->i_children;
1165 if( i_current_view == VIEW_CATEGORY )
1167 i_return--; /* remove the GENERAL item from the list */
1168 i_return += p_playlist->p_general->i_children; /* add the items of the general node */
1174 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
1176 i_return = p_item->i_children;
1178 vlc_object_release( p_playlist );
1186 /* return the child at index for the Obj-C pointer item */ /* DONE */
1187 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
1189 playlist_item_t *p_return = NULL;
1190 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1194 if( p_playlist == NULL )
1200 playlist_view_t *p_view;
1201 p_view = playlist_ViewFind( p_playlist, i_current_view );
1202 if( p_view && p_view->p_root ) p_return = p_view->p_root->pp_children[index];
1204 if( i_current_view == VIEW_CATEGORY )
1206 if( p_playlist->p_general->i_children && index >= 0 && index < p_playlist->p_general->i_children )
1208 p_return = p_playlist->p_general->pp_children[index];
1210 else if( p_view && p_view->p_root && index >= 0 && index - p_playlist->p_general->i_children < p_view->p_root->i_children )
1212 p_return = p_view->p_root->pp_children[index - p_playlist->p_general->i_children + 1];
1219 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
1220 if( p_item && index < p_item->i_children && index >= 0 )
1221 p_return = p_item->pp_children[index];
1224 if( p_playlist->i_size >= 2 )
1226 [o_status_field setStringValue: [NSString stringWithFormat:
1227 _NS("%i items in playlist"), p_playlist->i_size]];
1231 if( p_playlist->i_size == 0 )
1233 [o_status_field setStringValue: [NSString stringWithFormat:
1234 _NS("no items in playlist"), p_playlist->i_size]];
1238 [o_status_field setStringValue: [NSString stringWithFormat:
1239 _NS("1 item in playlist"), p_playlist->i_size]];
1243 vlc_object_release( p_playlist );
1245 o_value = [[NSValue valueWithPointer: p_return] retain];
1247 [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p", p_return]];
1251 /* is the item expandable */
1252 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
1255 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1257 if( p_playlist == NULL )
1263 playlist_view_t *p_view;
1264 p_view = playlist_ViewFind( p_playlist, i_current_view );
1265 if( p_view && p_view->p_root ) i_return = p_view->p_root->i_children;
1267 if( i_current_view == VIEW_CATEGORY )
1270 i_return += p_playlist->p_general->i_children;
1275 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
1277 i_return = p_item->i_children;
1279 vlc_object_release( p_playlist );
1287 /* retrieve the string values for the cells */
1288 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
1291 intf_thread_t *p_intf = VLCIntf;
1292 playlist_t *p_playlist;
1293 playlist_item_t *p_item;
1295 if( item == nil || ![item isKindOfClass: [NSValue class]] ) return( @"error" );
1297 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1299 if( p_playlist == NULL )
1304 p_item = (playlist_item_t *)[item pointerValue];
1306 if( p_item == NULL )
1308 vlc_object_release( p_playlist );
1312 if( [[o_tc identifier] isEqualToString:@"1"] )
1314 o_value = [NSString stringWithUTF8String:
1315 p_item->input.psz_name];
1316 if( o_value == NULL )
1317 o_value = [NSString stringWithCString:
1318 p_item->input.psz_name];
1320 else if( [[o_tc identifier] isEqualToString:@"2"] )
1323 psz_temp = vlc_input_item_GetInfo( &p_item->input ,_("Meta-information"),_("Artist") );
1325 if( psz_temp == NULL )
1329 o_value = [NSString stringWithUTF8String: psz_temp];
1330 if( o_value == NULL )
1332 o_value = [NSString stringWithCString: psz_temp];
1337 else if( [[o_tc identifier] isEqualToString:@"3"] )
1339 char psz_duration[MSTRTIME_MAX_SIZE];
1340 mtime_t dur = p_item->input.i_duration;
1343 secstotimestr( psz_duration, dur/1000000 );
1344 o_value = [NSString stringWithUTF8String: psz_duration];
1348 o_value = @"-:--:--";
1351 vlc_object_release( p_playlist );
1356 /* Required for drag & drop and reordering */
1357 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
1360 playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1363 [o_items_array removeAllObjects];
1364 [o_nodes_array removeAllObjects];
1366 if( !p_playlist ) return NO;
1368 for( i = 0 ; i < [items count] ; i++ )
1370 id o_item = [items objectAtIndex: i];
1372 if( ![self isItem: [o_item pointerValue] inNode:
1373 p_playlist->p_general] )
1375 vlc_object_release(p_playlist);
1378 if( ((playlist_item_t *)[o_item pointerValue])->i_children > 0 )
1379 [o_nodes_array addObject: o_item];
1381 [o_items_array addObject: o_item];
1384 /* Now we need to check if there are selected items that are in already
1385 selected nodes. In that case, we only want to move the nodes */
1386 for( i = 0 ; i < [o_nodes_array count] ; i++ )
1388 for ( j = 0 ; j < [o_nodes_array count] ; j++ )
1390 if( j == i ) continue;
1391 if( [self isItem: [[o_nodes_array objectAtIndex:i] pointerValue]
1392 inNode: [[o_nodes_array objectAtIndex:j] pointerValue]] )
1394 [o_nodes_array removeObjectAtIndex:i];
1395 /* We need to execute the next iteration with the same index
1396 since the current item has been deleted */
1403 for( i = 0 ; i < [o_items_array count] ; i++ )
1405 for ( j = 0 ; j < [o_nodes_array count] ; j++ )
1407 if( [self isItem: [[o_items_array objectAtIndex:i] pointerValue]
1408 inNode: [[o_nodes_array objectAtIndex:j] pointerValue]] )
1410 [o_items_array removeObjectAtIndex:i];
1417 [pboard declareTypes: [NSArray arrayWithObjects:
1418 @"VLCPlaylistItemPboardType",NSFilenamesPboardType, nil] owner: self];
1419 [pboard setPropertyList:[NSArray array]
1420 forType:NSFilenamesPboardType];
1422 vlc_object_release(p_playlist);
1426 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
1428 playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1430 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1432 if( !p_playlist ) return NSDragOperationNone;
1434 if( ![self isItem: [item pointerValue] inNode: p_playlist->p_general] )
1435 return NSDragOperationNone;
1437 /* Drop from the Playlist */
1438 if( [[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] )
1441 for( i = 0 ; i < [o_nodes_array count] ; i++ )
1443 if( [self isItem: [item pointerValue] inNode:
1444 [[o_nodes_array objectAtIndex: i] pointerValue]] )
1446 vlc_object_release(p_playlist);
1447 return NSDragOperationNone;
1450 vlc_object_release(p_playlist);
1451 return NSDragOperationMove;
1454 /* Drop from the Finder */
1455 else if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1457 vlc_object_release(p_playlist);
1458 return NSDragOperationGeneric;
1460 vlc_object_release(p_playlist);
1461 return NSDragOperationNone;
1464 - (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(int)index
1466 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1468 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1470 if( !p_playlist ) return NO;
1472 if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1475 playlist_item_t *p_node = [item pointerValue];
1477 NSArray *o_array = [NSArray array];
1478 NSArray *o_values = [[o_pasteboard propertyListForType:
1479 NSFilenamesPboardType]
1480 sortedArrayUsingSelector:
1481 @selector(caseInsensitiveCompare:)];
1483 for( i = 0; i < (int)[o_values count]; i++)
1485 NSDictionary *o_dic;
1486 o_dic = [NSDictionary dictionaryWithObject:[o_values
1487 objectAtIndex:i] forKey:@"ITEM_URL"];
1488 o_array = [o_array arrayByAddingObject: o_dic];
1493 [self appendArray: o_array atPos: index enqueue: YES];
1495 else if( p_node->i_children == -1 )
1498 playlist_item_t *p_real_node = NULL;
1500 for( i_counter = 0 ; i_counter < p_node->i_parents ; i_counter++ )
1502 if( p_node->pp_parents[i_counter]->i_view == i_current_view )
1504 p_real_node = p_node->pp_parents[i_counter]->p_parent;
1507 if( i_counter == p_node->i_parents )
1512 [self appendNodeArray: o_array inNode: p_real_node
1513 atPos: index inView: i_current_view enqueue: YES];
1517 [self appendNodeArray: o_array inNode: p_node
1518 atPos: index inView: i_current_view enqueue: YES];
1520 vlc_object_release( p_playlist );
1523 vlc_object_release( p_playlist );
1527 /* Delegate method of NSWindow */
1528 /*- (void)windowWillClose:(NSNotification *)aNotification
1530 [o_btn_playlist setState: NSOffState];