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];
82 [super keyDown: o_event];
89 /*****************************************************************************
90 * VLCPlaylist implementation
91 *****************************************************************************/
92 @implementation VLCPlaylist
99 o_outline_dict = [[NSMutableDictionary alloc] init];
107 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
109 vlc_list_t *p_list = vlc_list_find( p_playlist, VLC_OBJECT_MODULE,
113 i_current_view = VIEW_CATEGORY;
114 playlist_ViewUpdate( p_playlist, i_current_view );
116 [o_outline_view setTarget: self];
117 [o_outline_view setDelegate: self];
118 [o_outline_view setDataSource: self];
120 [o_outline_view setDoubleAction: @selector(playItem:)];
122 [o_outline_view registerForDraggedTypes:
123 [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
124 [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
126 /* We need to check whether _defaultTableHeaderSortImage exists, since it
127 belongs to an Apple hidden private API, and then can "disapear" at any time*/
129 if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderSortImage)] )
131 o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
135 o_ascendingSortingImage = nil;
138 if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderReverseSortImage)] )
140 o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
144 o_descendingSortingImage = nil;
147 o_tc_sortColumn = nil;
149 for( i_index = 0; i_index < p_list->i_count; i_index++ )
152 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
154 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
156 /* create the menu entries used in the playlist menu */
157 o_lmi = [[o_mi_services submenu] addItemWithTitle:
158 [NSString stringWithUTF8String:
159 p_parser->psz_longname ? p_parser->psz_longname :
160 ( p_parser->psz_shortname ? p_parser->psz_shortname:
161 p_parser->psz_object_name)]
162 action: @selector(servicesChange:)
164 [o_lmi setTarget: self];
165 [o_lmi setRepresentedObject:
166 [NSString stringWithCString: p_parser->psz_object_name]];
167 if( playlist_IsServicesDiscoveryLoaded( p_playlist,
168 p_parser->psz_object_name ) )
169 [o_lmi setState: NSOnState];
171 /* create the menu entries for the main menu */
172 o_lmi = [[o_mm_mi_services submenu] addItemWithTitle:
173 [NSString stringWithUTF8String:
174 p_parser->psz_longname ? p_parser->psz_longname :
175 ( p_parser->psz_shortname ? p_parser->psz_shortname:
176 p_parser->psz_object_name)]
177 action: @selector(servicesChange:)
179 [o_lmi setTarget: self];
180 [o_lmi setRepresentedObject:
181 [NSString stringWithCString: p_parser->psz_object_name]];
182 if( playlist_IsServicesDiscoveryLoaded( p_playlist,
183 p_parser->psz_object_name ) )
184 [o_lmi setState: NSOnState];
187 vlc_list_release( p_list );
188 vlc_object_release( p_playlist );
190 /* Change the simple textfield into a searchField if we can... */
191 if( MACOS_VERSION >= 10.3 )
193 NSView *o_parentview = [o_status_field superview];
194 NSSearchField *o_better_search_field = [[NSSearchField alloc]initWithFrame:[o_search_field frame]];
195 [o_better_search_field setRecentsAutosaveName:@"VLC media player search"];
196 [o_better_search_field setDelegate:self];
197 [[NSNotificationCenter defaultCenter] addObserver: self
198 selector: @selector(searchfieldChanged:)
199 name: NSControlTextDidChangeNotification
200 object: o_better_search_field];
202 [o_better_search_field setTarget:self];
203 [o_better_search_field setAction:@selector(searchItem:)];
205 [o_better_search_field setAutoresizingMask:NSViewMinXMargin];
206 [o_parentview addSubview:o_better_search_field];
207 [o_search_field setHidden:YES];
211 //[self playlistUpdated];
214 - (void)searchfieldChanged:(NSNotification *)o_notification
216 [o_search_field setStringValue:[[o_notification object] stringValue]];
221 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
222 [o_mi_play setTitle: _NS("Play")];
223 [o_mi_delete setTitle: _NS("Delete")];
224 [o_mi_selectall setTitle: _NS("Select All")];
225 [o_mi_info setTitle: _NS("Properties")];
226 [o_mi_sort_name setTitle: _NS("Sort Node by Name")];
227 [o_mi_sort_author setTitle: _NS("Sort Node by Author")];
228 [o_mi_services setTitle: _NS("Services discovery")];
229 [[o_tc_name headerCell] setStringValue:_NS("Name")];
230 [[o_tc_author headerCell] setStringValue:_NS("Author")];
231 [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
232 [o_status_field setStringValue: [NSString stringWithFormat:
233 _NS("no items in playlist")]];
235 [o_random_ckb setTitle: _NS("Random")];
237 [o_search_button setTitle: _NS("Search")];
239 [[o_loop_popup itemAtIndex:0] setTitle: _NS("Standard Play")];
240 [[o_loop_popup itemAtIndex:1] setTitle: _NS("Repeat One")];
241 [[o_loop_popup itemAtIndex:2] setTitle: _NS("Repeat All")];
244 - (NSOutlineView *)outlineView
246 return o_outline_view;
249 - (void)playlistUpdated
253 /* Clear indications of any existing column sorting*/
254 for( i = 0 ; i < [[o_outline_view tableColumns] count] ; i++ )
256 [o_outline_view setIndicatorImage:nil inTableColumn:
257 [[o_outline_view tableColumns] objectAtIndex:i]];
260 [o_outline_view setHighlightedTableColumn:nil];
261 o_tc_sortColumn = nil;
262 // TODO Find a way to keep the dict size to a minimum
263 //[o_outline_dict removeAllObjects];
264 [o_outline_view reloadData];
267 - (void)playModeUpdated
269 playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
271 vlc_value_t val, val2;
273 if( p_playlist == NULL )
278 var_Get( p_playlist, "loop", &val2 );
279 var_Get( p_playlist, "repeat", &val );
280 if( val.b_bool == VLC_TRUE )
282 [o_loop_popup selectItemAtIndex: 1];
284 else if( val2.b_bool == VLC_TRUE )
286 [o_loop_popup selectItemAtIndex: 2];
290 [o_loop_popup selectItemAtIndex: 0];
293 var_Get( p_playlist, "random", &val );
294 [o_random_ckb setState: val.b_bool];
296 vlc_object_release( p_playlist );
299 - (void)updateRowSelection
304 playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
306 playlist_item_t *p_item, *p_temp_item;
307 NSMutableArray *o_array = [NSMutableArray array];
309 if( p_playlist == NULL )
312 p_item = p_playlist->status.p_item;
313 if( p_item == NULL ) return;
315 p_temp_item = p_item;
316 while( p_temp_item->i_parents > 0 )
318 [o_array insertObject: [NSValue valueWithPointer: p_temp_item] atIndex: 0];
319 for (i = 0 ; i < p_temp_item->i_parents ; i++)
321 if( p_temp_item->pp_parents[i]->i_view == i_current_view )
323 p_temp_item = p_temp_item->pp_parents[i]->p_parent;
329 for (j = 0 ; j < [o_array count] - 1 ; j++)
332 if( ( o_item = [o_outline_dict objectForKey:
333 [NSString stringWithFormat: @"%p",
334 [[o_array objectAtIndex:j] pointerValue]]] ) != nil )
335 [o_outline_view expandItem: o_item];
339 i_row = [o_outline_view rowForItem:[o_outline_dict
340 objectForKey:[NSString stringWithFormat: @"%p", p_item]]];
342 [o_outline_view selectRow: i_row byExtendingSelection: NO];
343 [o_outline_view scrollRowToVisible: i_row];
345 vlc_object_release(p_playlist);
349 - (BOOL)isItem: (playlist_item_t *)p_item inNode: (playlist_item_t *)p_node
351 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
353 playlist_item_t *p_temp_item = p_item;
355 if( p_playlist == NULL )
362 while( p_temp_item->i_parents > 0 )
365 for( i = 0; i < p_temp_item->i_parents ; i++ )
367 if( p_temp_item->pp_parents[i]->i_view == i_current_view )
369 if( p_temp_item->pp_parents[i]->p_parent == p_node )
371 vlc_object_release( p_playlist );
376 p_temp_item = p_temp_item->pp_parents[i]->p_parent;
384 vlc_object_release( p_playlist );
389 /* When called retrieves the selected outlineview row and plays that node or item */
390 - (IBAction)playItem:(id)sender
392 intf_thread_t * p_intf = VLCIntf;
393 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
396 if( p_playlist != NULL )
398 playlist_item_t *p_item;
399 playlist_item_t *p_node = NULL;
402 p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
406 if( p_item->i_children == -1 )
408 for( i = 0 ; i < p_item->i_parents ; i++ )
410 if( p_item->pp_parents[i]->i_view == i_current_view )
412 p_node = p_item->pp_parents[i]->p_parent;
419 if( p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1 )
421 p_item = p_node->pp_children[0];
428 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view, p_node, p_item );
430 vlc_object_release( p_playlist );
434 - (IBAction)servicesChange:(id)sender
436 NSMenuItem *o_mi = (NSMenuItem *)sender;
437 NSString *o_string = [o_mi representedObject];
438 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
440 if( !playlist_IsServicesDiscoveryLoaded( p_playlist, [o_string cString] ) )
441 playlist_ServicesDiscoveryAdd( p_playlist, [o_string cString] );
443 playlist_ServicesDiscoveryRemove( p_playlist, [o_string cString] );
445 [o_mi setState: playlist_IsServicesDiscoveryLoaded( p_playlist,
446 [o_string cString] ) ? YES : NO];
448 i_current_view = VIEW_CATEGORY;
449 playlist_ViewUpdate( p_playlist, i_current_view );
450 vlc_object_release( p_playlist );
451 [self playlistUpdated];
455 - (IBAction)selectAll:(id)sender
457 [o_outline_view selectAll: nil];
460 - (IBAction)deleteItem:(id)sender
462 int i, i_count, i_row;
463 NSMutableArray *o_to_delete;
466 playlist_t * p_playlist;
467 intf_thread_t * p_intf = VLCIntf;
469 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
472 if ( p_playlist == NULL )
476 o_to_delete = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
477 i_count = [o_to_delete count];
479 for( i = 0; i < i_count; i++ )
481 playlist_item_t * p_item;
482 o_number = [o_to_delete lastObject];
483 i_row = [o_number intValue];
485 [o_to_delete removeObject: o_number];
486 [o_outline_view deselectRow: i_row];
488 p_item = (playlist_item_t *)[[o_outline_view itemAtRow: i_row] pointerValue];
490 if( p_item->i_children > -1 ) //is a node and not an item
492 if( p_playlist->status.i_status != PLAYLIST_STOPPED &&
493 [self isItem: p_playlist->status.p_item inNode: p_item] == YES )
495 // if current item is in selected node and is playing then stop playlist
496 playlist_Stop( p_playlist );
498 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
502 if( p_playlist->status.i_status != PLAYLIST_STOPPED &&
503 p_playlist->status.p_item == [[o_outline_view itemAtRow: i_row] pointerValue] )
505 playlist_Stop( p_playlist );
507 playlist_LockDelete( p_playlist, p_item->input.i_id );
510 [self playlistUpdated];
511 vlc_object_release( p_playlist );
514 - (IBAction)sortNodeByName:(id)sender
516 [self sortNode: SORT_TITLE];
519 - (IBAction)sortNodeByAuthor:(id)sender
521 [self sortNode: SORT_AUTHOR];
524 - (void)sortNode:(int)i_mode
526 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
528 playlist_item_t * p_item;
530 if (p_playlist == NULL)
535 if( [o_outline_view selectedRow] > -1 )
537 p_item = [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
541 /*If no item is selected, sort the whole playlist*/
543 playlist_view_t * p_view = playlist_ViewFind( p_playlist, i_current_view );
544 p_item = p_view->p_root;
547 if( p_item->i_children > -1 ) // the item is a node
549 vlc_mutex_lock( &p_playlist->object_lock );
550 playlist_RecursiveNodeSort( p_playlist, p_item, i_mode, ORDER_NORMAL );
551 vlc_mutex_unlock( &p_playlist->object_lock );
557 for( i = 0 ; i < p_item->i_parents ; i++ )
559 if( p_item->pp_parents[i]->i_view == i_current_view )
561 vlc_mutex_lock( &p_playlist->object_lock );
562 playlist_RecursiveNodeSort( p_playlist,
563 p_item->pp_parents[i]->p_parent, i_mode, ORDER_NORMAL );
564 vlc_mutex_unlock( &p_playlist->object_lock );
569 vlc_object_release( p_playlist );
570 [self playlistUpdated];
573 - (playlist_item_t *)createItem:(NSDictionary *)o_one_item
575 intf_thread_t * p_intf = VLCIntf;
576 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
579 if( p_playlist == NULL )
583 playlist_item_t *p_item;
585 BOOL b_rem = FALSE, b_dir = FALSE;
586 NSString *o_uri, *o_name;
591 o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
592 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
593 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
595 /* Find the name for a disc entry ( i know, can you believe the trouble?) */
596 if( ( !o_name || [o_name isEqualToString:@""] ) && [o_uri rangeOfString: @"/dev/"].location != NSNotFound )
598 int i_count, i_index;
599 struct statfs *mounts = NULL;
601 i_count = getmntinfo (&mounts, MNT_NOWAIT);
602 /* getmntinfo returns a pointer to static data. Do not free. */
603 for( i_index = 0 ; i_index < i_count; i_index++ )
605 NSMutableString *o_temp, *o_temp2;
606 o_temp = [NSMutableString stringWithString: o_uri];
607 o_temp2 = [NSMutableString stringWithCString: mounts[i_index].f_mntfromname];
608 [o_temp replaceOccurrencesOfString: @"/dev/rdisk" withString: @"/dev/disk" options:NULL range:NSMakeRange(0, [o_temp length]) ];
609 [o_temp2 replaceOccurrencesOfString: @"s0" withString: @"" options:NULL range:NSMakeRange(0, [o_temp2 length]) ];
610 [o_temp2 replaceOccurrencesOfString: @"s1" withString: @"" options:NULL range:NSMakeRange(0, [o_temp2 length]) ];
612 if( strstr( [o_temp fileSystemRepresentation], [o_temp2 fileSystemRepresentation] ) != NULL )
614 o_name = [[NSFileManager defaultManager] displayNameAtPath: [NSString stringWithCString:mounts[i_index].f_mntonname]];
618 /* If no name, then make a guess */
619 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
621 if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
622 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
623 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
625 /* All of this is to make sure CD's play when you D&D them on VLC */
626 /* Converts mountpoint to a /dev file */
629 NSMutableString *o_temp;
631 buf = (struct statfs *) malloc (sizeof(struct statfs));
632 statfs( [o_uri fileSystemRepresentation], buf );
633 psz_dev = strdup(buf->f_mntfromname);
634 o_temp = [NSMutableString stringWithCString: psz_dev ];
635 [o_temp replaceOccurrencesOfString: @"/dev/disk" withString: @"/dev/rdisk" options:NULL range:NSMakeRange(0, [o_temp length]) ];
636 [o_temp replaceOccurrencesOfString: @"s0" withString: @"" options:NULL range:NSMakeRange(0, [o_temp length]) ];
637 [o_temp replaceOccurrencesOfString: @"s1" withString: @"" options:NULL range:NSMakeRange(0, [o_temp length]) ];
641 p_item = playlist_ItemNew( p_intf, [o_uri fileSystemRepresentation], [o_name UTF8String] );
647 for( i = 0; i < (int)[o_options count]; i++ )
649 playlist_ItemAddOption( p_item, strdup( [[o_options objectAtIndex:i] UTF8String] ) );
653 /* Recent documents menu */
654 o_true_file = [NSURL fileURLWithPath: o_uri];
655 if( o_true_file != nil )
657 [[NSDocumentController sharedDocumentController]
658 noteNewRecentDocumentURL: o_true_file];
661 vlc_object_release( p_playlist );
665 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
668 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
670 if( p_playlist == NULL )
675 for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
677 playlist_item_t *p_item;
678 NSDictionary *o_one_item;
681 o_one_item = [o_array objectAtIndex: i_item];
682 p_item = [self createItem: o_one_item];
689 playlist_AddItem( p_playlist, p_item, PLAYLIST_APPEND, i_position == -1 ? PLAYLIST_END : i_position + i_item );
691 if( i_item == 0 && !b_enqueue )
693 playlist_Control( p_playlist, PLAYLIST_ITEMPLAY, p_item );
696 vlc_object_release( p_playlist );
699 - (void)appendNodeArray:(NSArray*)o_array inNode:(playlist_item_t *)p_node atPos:(int)i_position inView:(int)i_view enqueue:(BOOL)b_enqueue
702 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
704 if( p_playlist == NULL )
709 for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
711 playlist_item_t *p_item;
712 NSDictionary *o_one_item;
715 o_one_item = [o_array objectAtIndex: i_item];
716 p_item = [self createItem: o_one_item];
723 playlist_NodeAddItem( p_playlist, p_item, i_view, p_node, PLAYLIST_APPEND, i_position + i_item );
725 if( i_item == 0 && !b_enqueue )
727 playlist_Control( p_playlist, PLAYLIST_ITEMPLAY, p_item );
730 vlc_object_release( p_playlist );
734 - (IBAction)handlePopUp:(id)sender
737 intf_thread_t * p_intf = VLCIntf;
738 vlc_value_t val1,val2;
739 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
741 if( p_playlist == NULL )
746 switch( [o_loop_popup indexOfSelectedItem] )
751 var_Set( p_playlist, "loop", val1 );
753 var_Set( p_playlist, "repeat", val1 );
754 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat One" ) );
759 var_Set( p_playlist, "repeat", val1 );
761 var_Set( p_playlist, "loop", val1 );
762 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat All" ) );
766 var_Get( p_playlist, "repeat", &val1 );
767 var_Get( p_playlist, "loop", &val2 );
768 if( val1.b_bool || val2.b_bool )
771 var_Set( p_playlist, "repeat", val1 );
772 var_Set( p_playlist, "loop", val1 );
773 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
777 vlc_object_release( p_playlist );
778 [self playlistUpdated];
781 - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
783 playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
785 playlist_item_t *p_selected_item;
786 int i_current, i_selected_row;
791 i_selected_row = [o_outline_view selectedRow];
792 if (i_selected_row < 0)
795 p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow:
796 i_selected_row] pointerValue];
798 for( i_current = 0; i_current < p_item->i_children ; i_current++ )
801 NSString *o_current_name, *o_current_author;
803 vlc_mutex_lock( &p_playlist->object_lock );
804 o_current_name = [NSString stringWithUTF8String:
805 p_item->pp_children[i_current]->input.psz_name];
806 psz_temp = vlc_input_item_GetInfo( &p_item->input ,
807 _("Meta-information"),_("Artist") );
808 o_current_author = [NSString stringWithUTF8String: psz_temp];
810 vlc_mutex_unlock( &p_playlist->object_lock );
812 if( p_selected_item == p_item->pp_children[i_current] &&
813 b_selected_item_met == NO )
815 b_selected_item_met = YES;
817 else if( p_selected_item == p_item->pp_children[i_current] &&
818 b_selected_item_met == YES )
820 vlc_object_release( p_playlist );
823 else if( b_selected_item_met == YES &&
824 ( [o_current_name rangeOfString:[o_search_field
825 stringValue] options:NSCaseInsensitiveSearch ].length ||
826 [o_current_author rangeOfString:[o_search_field
827 stringValue] options:NSCaseInsensitiveSearch ].length ) )
829 vlc_object_release( p_playlist );
830 /*Adds the parent items in the result array as well, so that we can
832 return [NSMutableArray arrayWithObject: [NSValue
833 valueWithPointer: p_item->pp_children[i_current]]];
835 if( p_item->pp_children[i_current]->i_children > 0 )
837 id o_result = [self subSearchItem:
838 p_item->pp_children[i_current]];
839 if( o_result != NULL )
841 vlc_object_release( p_playlist );
842 [o_result insertObject: [NSValue valueWithPointer:
843 p_item->pp_children[i_current]] atIndex:0];
848 vlc_object_release( p_playlist );
852 - (IBAction)searchItem:(id)sender
854 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
856 playlist_view_t * p_view;
862 b_selected_item_met = NO;
864 if( p_playlist == NULL )
866 p_view = playlist_ViewFind( p_playlist, i_current_view );
870 /*First, only search after the selected item:*
871 *(b_selected_item_met = NO) */
872 o_result = [self subSearchItem:p_view->p_root];
873 if( o_result == NULL )
875 /* If the first search failed, search again from the beginning */
876 o_result = [self subSearchItem:p_view->p_root];
878 if( o_result != NULL )
880 for( i = 0 ; i < [o_result count] - 1 ; i++ )
882 [o_outline_view expandItem: [o_outline_dict objectForKey:
883 [NSString stringWithFormat: @"%p",
884 [[o_result objectAtIndex: i] pointerValue]]]];
886 i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
887 [NSString stringWithFormat: @"%p",
888 [[o_result objectAtIndex: [o_result count] - 1 ]
893 [o_outline_view selectRow:i_row byExtendingSelection: NO];
894 [o_outline_view scrollRowToVisible: i_row];
897 vlc_object_release( p_playlist );
900 - (NSMenu *)menuForEvent:(NSEvent *)o_event
904 vlc_bool_t b_item_sel;
906 pt = [o_outline_view convertPoint: [o_event locationInWindow]
908 b_item_sel = ( [o_outline_view rowAtPoint: pt] != -1 &&
909 [o_outline_view selectedRow] != -1 );
910 b_rows = [o_outline_view numberOfRows] != 0;
912 [o_mi_play setEnabled: b_item_sel];
913 [o_mi_delete setEnabled: b_item_sel];
914 [o_mi_selectall setEnabled: b_rows];
915 [o_mi_info setEnabled: b_item_sel];
917 return( o_ctx_menu );
920 - (playlist_item_t *)selectedPlaylistItem
922 return [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
926 - (void)outlineView: (NSTableView*)o_tv
927 didClickTableColumn:(NSTableColumn *)o_tc
929 int i_mode = 0, i_type;
930 intf_thread_t *p_intf = VLCIntf;
931 playlist_view_t *p_view;
933 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
935 if( p_playlist == NULL )
940 /* Check whether the selected table column header corresponds to a
941 sortable table column*/
942 if( !( o_tc == o_tc_name || o_tc == o_tc_author ) )
944 vlc_object_release( p_playlist );
948 p_view = playlist_ViewFind( p_playlist, i_current_view );
950 if( o_tc_sortColumn == o_tc )
952 b_isSortDescending = !b_isSortDescending;
956 b_isSortDescending = VLC_FALSE;
959 if( o_tc == o_tc_name )
963 else if( o_tc == o_tc_author )
965 i_mode = SORT_AUTHOR;
968 if( b_isSortDescending )
970 i_type = ORDER_REVERSE;
974 i_type = ORDER_NORMAL;
977 vlc_mutex_lock( &p_playlist->object_lock );
978 playlist_RecursiveNodeSort( p_playlist, p_view->p_root, i_mode, i_type );
979 vlc_mutex_unlock( &p_playlist->object_lock );
981 vlc_object_release( p_playlist );
982 [self playlistUpdated];
984 o_tc_sortColumn = o_tc;
985 [o_outline_view setHighlightedTableColumn:o_tc];
987 if( b_isSortDescending )
989 [o_outline_view setIndicatorImage:o_descendingSortingImage
994 [o_outline_view setIndicatorImage:o_ascendingSortingImage
1000 - (void)outlineView:(NSOutlineView *)outlineView
1001 willDisplayCell:(id)cell
1002 forTableColumn:(NSTableColumn *)tableColumn
1005 playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1007 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
1009 if( !p_playlist ) return;
1011 if( ( p_item == p_playlist->status.p_item ) ||
1012 ( p_item->i_children != 0 &&
1013 [self isItem: p_playlist->status.p_item inNode: p_item] ) )
1015 [cell setFont: [NSFont boldSystemFontOfSize: 0]];
1019 [cell setFont: [NSFont systemFontOfSize: 0]];
1021 vlc_object_release( p_playlist );
1026 @implementation VLCPlaylist (NSOutlineViewDataSource)
1028 /* return the number of children for Obj-C pointer item */ /* DONE */
1029 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
1032 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1034 if( p_playlist == NULL || outlineView != o_outline_view )
1040 playlist_view_t *p_view;
1041 p_view = playlist_ViewFind( p_playlist, i_current_view );
1042 if( p_view && p_view->p_root )
1043 i_return = p_view->p_root->i_children;
1047 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
1049 i_return = p_item->i_children;
1051 vlc_object_release( p_playlist );
1059 /* return the child at index for the Obj-C pointer item */ /* DONE */
1060 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
1062 playlist_item_t *p_return = NULL;
1063 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1067 if( p_playlist == NULL )
1073 playlist_view_t *p_view;
1074 p_view = playlist_ViewFind( p_playlist, i_current_view );
1075 if( p_view && index < p_view->p_root->i_children && index >= 0 )
1076 p_return = p_view->p_root->pp_children[index];
1080 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
1081 if( p_item && index < p_item->i_children && index >= 0 )
1082 p_return = p_item->pp_children[index];
1085 if( p_playlist->i_size >= 2 )
1087 [o_status_field setStringValue: [NSString stringWithFormat:
1088 _NS("%i items in playlist"), p_playlist->i_size]];
1092 if( p_playlist->i_size == 0 )
1094 [o_status_field setStringValue: [NSString stringWithFormat:
1095 _NS("no items in playlist"), p_playlist->i_size]];
1099 [o_status_field setStringValue: [NSString stringWithFormat:
1100 _NS("1 item in playlist"), p_playlist->i_size]];
1104 vlc_object_release( p_playlist );
1106 o_value = [[NSValue valueWithPointer: p_return] retain];
1108 [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p", p_return]];
1112 /* is the item expandable */
1113 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
1116 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1118 if( p_playlist == NULL )
1124 playlist_view_t *p_view;
1125 p_view = playlist_ViewFind( p_playlist, i_current_view );
1126 if( p_view && p_view->p_root )
1127 i_return = p_view->p_root->i_children;
1131 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
1133 i_return = p_item->i_children;
1135 vlc_object_release( p_playlist );
1143 /* retrieve the string values for the cells */
1144 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
1147 intf_thread_t *p_intf = VLCIntf;
1148 playlist_t *p_playlist;
1149 playlist_item_t *p_item;
1151 if( item == nil || ![item isKindOfClass: [NSValue class]] ) return( @"error" );
1153 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1155 if( p_playlist == NULL )
1160 p_item = (playlist_item_t *)[item pointerValue];
1162 if( p_item == NULL )
1164 vlc_object_release( p_playlist );
1168 if( [[o_tc identifier] isEqualToString:@"1"] )
1170 o_value = [NSString stringWithUTF8String:
1171 p_item->input.psz_name];
1172 if( o_value == NULL )
1173 o_value = [NSString stringWithCString:
1174 p_item->input.psz_name];
1176 else if( [[o_tc identifier] isEqualToString:@"2"] )
1179 psz_temp = vlc_input_item_GetInfo( &p_item->input ,_("Meta-information"),_("Artist") );
1181 if( psz_temp == NULL )
1185 o_value = [NSString stringWithUTF8String: psz_temp];
1186 if( o_value == NULL )
1188 o_value = [NSString stringWithCString: psz_temp];
1193 else if( [[o_tc identifier] isEqualToString:@"3"] )
1195 char psz_duration[MSTRTIME_MAX_SIZE];
1196 mtime_t dur = p_item->input.i_duration;
1199 secstotimestr( psz_duration, dur/1000000 );
1200 o_value = [NSString stringWithUTF8String: psz_duration];
1204 o_value = @"-:--:--";
1207 vlc_object_release( p_playlist );
1212 /* Required for drag & drop and reordering */
1213 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
1217 for( i = 0 ; i < [items count] ; i++ )
1219 if( [outlineView levelForItem: [items objectAtIndex: i]] == 0 )
1227 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
1229 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1231 if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1233 return NSDragOperationGeneric;
1235 return NSDragOperationNone;
1238 - (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(int)index
1240 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1242 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1244 if( !p_playlist ) return NO;
1246 if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1249 playlist_item_t *p_node = [item pointerValue];
1251 NSArray *o_array = [NSArray array];
1252 NSArray *o_values = [[o_pasteboard propertyListForType:
1253 NSFilenamesPboardType]
1254 sortedArrayUsingSelector:
1255 @selector(caseInsensitiveCompare:)];
1257 for( i = 0; i < (int)[o_values count]; i++)
1259 NSDictionary *o_dic;
1260 o_dic = [NSDictionary dictionaryWithObject:[o_values
1261 objectAtIndex:i] forKey:@"ITEM_URL"];
1262 o_array = [o_array arrayByAddingObject: o_dic];
1267 [self appendArray: o_array atPos: index enqueue: YES];
1269 else if( p_node->i_children == -1 )
1272 playlist_item_t *p_real_node = NULL;
1274 for( i_counter = 0 ; i_counter < p_node->i_parents ; i_counter++ )
1276 if( p_node->pp_parents[i_counter]->i_view == i_current_view )
1278 p_real_node = p_node->pp_parents[i_counter]->p_parent;
1281 if( i_counter == p_node->i_parents )
1286 [self appendNodeArray: o_array inNode: p_real_node
1287 atPos: index inView: i_current_view enqueue: YES];
1291 [self appendNodeArray: o_array inNode: p_node
1292 atPos: index inView: i_current_view enqueue: YES];
1294 vlc_object_release( p_playlist );
1297 vlc_object_release( p_playlist );
1301 /* Delegate method of NSWindow */
1302 /*- (void)windowWillClose:(NSNotification *)aNotification
1304 [o_btn_playlist setState: NSOffState];