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];
113 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
115 vlc_list_t *p_list = vlc_list_find( p_playlist, VLC_OBJECT_MODULE,
119 i_current_view = VIEW_CATEGORY;
120 playlist_ViewUpdate( p_playlist, i_current_view );
122 [o_outline_view setTarget: self];
123 [o_outline_view setDelegate: self];
124 [o_outline_view setDataSource: self];
126 [o_outline_view setDoubleAction: @selector(playItem:)];
128 [o_outline_view registerForDraggedTypes:
129 [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
130 [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
132 /* We need to check whether _defaultTableHeaderSortImage exists, since it
133 belongs to an Apple hidden private API, and then can "disapear" at any time*/
135 if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderSortImage)] )
137 o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
141 o_ascendingSortingImage = nil;
144 if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderReverseSortImage)] )
146 o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
150 o_descendingSortingImage = nil;
153 o_tc_sortColumn = nil;
155 for( i_index = 0; i_index < p_list->i_count; i_index++ )
158 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
160 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
162 /* create the menu entries used in the playlist menu */
163 o_lmi = [[o_mi_services submenu] addItemWithTitle:
164 [NSString stringWithUTF8String:
165 p_parser->psz_longname ? p_parser->psz_longname :
166 ( p_parser->psz_shortname ? p_parser->psz_shortname:
167 p_parser->psz_object_name)]
168 action: @selector(servicesChange:)
170 [o_lmi setTarget: self];
171 [o_lmi setRepresentedObject:
172 [NSString stringWithCString: p_parser->psz_object_name]];
173 if( playlist_IsServicesDiscoveryLoaded( p_playlist,
174 p_parser->psz_object_name ) )
175 [o_lmi setState: NSOnState];
177 /* create the menu entries for the main menu */
178 o_lmi = [[o_mm_mi_services submenu] addItemWithTitle:
179 [NSString stringWithUTF8String:
180 p_parser->psz_longname ? p_parser->psz_longname :
181 ( p_parser->psz_shortname ? p_parser->psz_shortname:
182 p_parser->psz_object_name)]
183 action: @selector(servicesChange:)
185 [o_lmi setTarget: self];
186 [o_lmi setRepresentedObject:
187 [NSString stringWithCString: p_parser->psz_object_name]];
188 if( playlist_IsServicesDiscoveryLoaded( p_playlist,
189 p_parser->psz_object_name ) )
190 [o_lmi setState: NSOnState];
193 vlc_list_release( p_list );
194 vlc_object_release( p_playlist );
196 /* Change the simple textfield into a searchField if we can... */
198 if( MACOS_VERSION >= 10.3 )
200 NSView *o_parentview = [o_status_field superview];
201 NSSearchField *o_better_search_field = [[NSSearchField alloc]initWithFrame:[o_search_field frame]];
202 [o_better_search_field setRecentsAutosaveName:@"VLC media player search"];
203 [o_better_search_field setDelegate:self];
204 [[NSNotificationCenter defaultCenter] addObserver: self
205 selector: @selector(searchfieldChanged:)
206 name: NSControlTextDidChangeNotification
207 object: o_better_search_field];
209 [o_better_search_field setTarget:self];
210 [o_better_search_field setAction:@selector(searchItem:)];
212 [o_better_search_field setAutoresizingMask:NSViewMinXMargin];
213 [o_parentview addSubview:o_better_search_field];
214 [o_search_field setHidden:YES];
218 //[self playlistUpdated];
221 - (void)searchfieldChanged:(NSNotification *)o_notification
223 [o_search_field setStringValue:[[o_notification object] stringValue]];
228 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
229 [o_mi_play setTitle: _NS("Play")];
230 [o_mi_delete setTitle: _NS("Delete")];
231 [o_mi_selectall setTitle: _NS("Select All")];
232 [o_mi_info setTitle: _NS("Properties")];
233 [o_mi_sort_name setTitle: _NS("Sort Node by Name")];
234 [o_mi_sort_author setTitle: _NS("Sort Node by Author")];
235 [o_mi_services setTitle: _NS("Services discovery")];
236 [[o_tc_name headerCell] setStringValue:_NS("Name")];
237 [[o_tc_author headerCell] setStringValue:_NS("Author")];
238 [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
239 [o_status_field setStringValue: [NSString stringWithFormat:
240 _NS("no items in playlist")]];
242 [o_random_ckb setTitle: _NS("Random")];
244 [o_search_button setTitle: _NS("Search")];
246 [[o_loop_popup itemAtIndex:0] setTitle: _NS("Standard Play")];
247 [[o_loop_popup itemAtIndex:1] setTitle: _NS("Repeat One")];
248 [[o_loop_popup itemAtIndex:2] setTitle: _NS("Repeat All")];
251 - (NSOutlineView *)outlineView
253 return o_outline_view;
256 - (void)playlistUpdated
260 /* Clear indications of any existing column sorting*/
261 for( i = 0 ; i < [[o_outline_view tableColumns] count] ; i++ )
263 [o_outline_view setIndicatorImage:nil inTableColumn:
264 [[o_outline_view tableColumns] objectAtIndex:i]];
267 [o_outline_view setHighlightedTableColumn:nil];
268 o_tc_sortColumn = nil;
269 // TODO Find a way to keep the dict size to a minimum
270 //[o_outline_dict removeAllObjects];
271 [o_outline_view reloadData];
274 - (void)playModeUpdated
276 playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
278 vlc_value_t val, val2;
280 if( p_playlist == NULL )
285 var_Get( p_playlist, "loop", &val2 );
286 var_Get( p_playlist, "repeat", &val );
287 if( val.b_bool == VLC_TRUE )
289 [o_loop_popup selectItemAtIndex: 1];
291 else if( val2.b_bool == VLC_TRUE )
293 [o_loop_popup selectItemAtIndex: 2];
297 [o_loop_popup selectItemAtIndex: 0];
300 var_Get( p_playlist, "random", &val );
301 [o_random_ckb setState: val.b_bool];
303 vlc_object_release( p_playlist );
306 - (void)updateRowSelection
311 playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
313 playlist_item_t *p_item, *p_temp_item;
314 NSMutableArray *o_array = [NSMutableArray array];
316 if( p_playlist == NULL )
319 p_item = p_playlist->status.p_item;
320 if( p_item == NULL ) return;
322 p_temp_item = p_item;
323 while( p_temp_item->i_parents > 0 )
325 [o_array insertObject: [NSValue valueWithPointer: p_temp_item] atIndex: 0];
326 for (i = 0 ; i < p_temp_item->i_parents ; i++)
328 if( p_temp_item->pp_parents[i]->i_view == i_current_view )
330 p_temp_item = p_temp_item->pp_parents[i]->p_parent;
336 for (j = 0 ; j < [o_array count] - 1 ; j++)
339 if( ( o_item = [o_outline_dict objectForKey:
340 [NSString stringWithFormat: @"%p",
341 [[o_array objectAtIndex:j] pointerValue]]] ) != nil )
342 [o_outline_view expandItem: o_item];
346 i_row = [o_outline_view rowForItem:[o_outline_dict
347 objectForKey:[NSString stringWithFormat: @"%p", p_item]]];
349 [o_outline_view selectRow: i_row byExtendingSelection: NO];
350 [o_outline_view scrollRowToVisible: i_row];
352 vlc_object_release(p_playlist);
356 - (BOOL)isItem: (playlist_item_t *)p_item inNode: (playlist_item_t *)p_node
358 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
360 playlist_item_t *p_temp_item = p_item;
362 if( p_playlist == NULL )
369 while( p_temp_item->i_parents > 0 )
372 for( i = 0; i < p_temp_item->i_parents ; i++ )
374 if( p_temp_item->pp_parents[i]->i_view == i_current_view )
376 if( p_temp_item->pp_parents[i]->p_parent == p_node )
378 vlc_object_release( p_playlist );
383 p_temp_item = p_temp_item->pp_parents[i]->p_parent;
391 vlc_object_release( p_playlist );
396 /* When called retrieves the selected outlineview row and plays that node or item */
397 - (IBAction)playItem:(id)sender
399 intf_thread_t * p_intf = VLCIntf;
400 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
403 if( p_playlist != NULL )
405 playlist_item_t *p_item;
406 playlist_item_t *p_node = NULL;
409 p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
413 if( p_item->i_children == -1 )
415 for( i = 0 ; i < p_item->i_parents ; i++ )
417 if( p_item->pp_parents[i]->i_view == i_current_view )
419 p_node = p_item->pp_parents[i]->p_parent;
426 if( p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1 )
428 p_item = p_node->pp_children[0];
435 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view, p_node, p_item );
437 vlc_object_release( p_playlist );
441 - (IBAction)servicesChange:(id)sender
443 NSMenuItem *o_mi = (NSMenuItem *)sender;
444 NSString *o_string = [o_mi representedObject];
445 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
447 if( !playlist_IsServicesDiscoveryLoaded( p_playlist, [o_string cString] ) )
448 playlist_ServicesDiscoveryAdd( p_playlist, [o_string cString] );
450 playlist_ServicesDiscoveryRemove( p_playlist, [o_string cString] );
452 [o_mi setState: playlist_IsServicesDiscoveryLoaded( p_playlist,
453 [o_string cString] ) ? YES : NO];
455 i_current_view = VIEW_CATEGORY;
456 playlist_ViewUpdate( p_playlist, i_current_view );
457 vlc_object_release( p_playlist );
458 [self playlistUpdated];
462 - (IBAction)selectAll:(id)sender
464 [o_outline_view selectAll: nil];
467 - (IBAction)deleteItem:(id)sender
469 int i, i_count, i_row;
470 NSMutableArray *o_to_delete;
473 playlist_t * p_playlist;
474 intf_thread_t * p_intf = VLCIntf;
476 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
479 if ( p_playlist == NULL )
483 o_to_delete = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
484 i_count = [o_to_delete count];
486 for( i = 0; i < i_count; i++ )
488 playlist_item_t * p_item;
489 o_number = [o_to_delete lastObject];
490 i_row = [o_number intValue];
492 [o_to_delete removeObject: o_number];
493 [o_outline_view deselectRow: i_row];
495 p_item = (playlist_item_t *)[[o_outline_view itemAtRow: i_row] pointerValue];
497 if( p_item->i_children > -1 ) //is a node and not an item
499 if( p_playlist->status.i_status != PLAYLIST_STOPPED &&
500 [self isItem: p_playlist->status.p_item inNode: p_item] == YES )
502 // if current item is in selected node and is playing then stop playlist
503 playlist_Stop( p_playlist );
505 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
509 if( p_playlist->status.i_status != PLAYLIST_STOPPED &&
510 p_playlist->status.p_item == [[o_outline_view itemAtRow: i_row] pointerValue] )
512 playlist_Stop( p_playlist );
514 playlist_LockDelete( p_playlist, p_item->input.i_id );
517 [self playlistUpdated];
518 vlc_object_release( p_playlist );
521 - (IBAction)sortNodeByName:(id)sender
523 [self sortNode: SORT_TITLE];
526 - (IBAction)sortNodeByAuthor:(id)sender
528 [self sortNode: SORT_AUTHOR];
531 - (void)sortNode:(int)i_mode
533 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
535 playlist_item_t * p_item;
537 if (p_playlist == NULL)
542 if( [o_outline_view selectedRow] > -1 )
544 p_item = [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
548 /*If no item is selected, sort the whole playlist*/
550 playlist_view_t * p_view = playlist_ViewFind( p_playlist, i_current_view );
551 p_item = p_view->p_root;
554 if( p_item->i_children > -1 ) // the item is a node
556 vlc_mutex_lock( &p_playlist->object_lock );
557 playlist_RecursiveNodeSort( p_playlist, p_item, i_mode, ORDER_NORMAL );
558 vlc_mutex_unlock( &p_playlist->object_lock );
564 for( i = 0 ; i < p_item->i_parents ; i++ )
566 if( p_item->pp_parents[i]->i_view == i_current_view )
568 vlc_mutex_lock( &p_playlist->object_lock );
569 playlist_RecursiveNodeSort( p_playlist,
570 p_item->pp_parents[i]->p_parent, i_mode, ORDER_NORMAL );
571 vlc_mutex_unlock( &p_playlist->object_lock );
576 vlc_object_release( p_playlist );
577 [self playlistUpdated];
580 - (playlist_item_t *)createItem:(NSDictionary *)o_one_item
582 intf_thread_t * p_intf = VLCIntf;
583 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
586 if( p_playlist == NULL )
590 playlist_item_t *p_item;
592 BOOL b_rem = FALSE, b_dir = FALSE;
593 NSString *o_uri, *o_name;
598 o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
599 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
600 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
602 /* Find the name for a disc entry ( i know, can you believe the trouble?) */
603 if( ( !o_name || [o_name isEqualToString:@""] ) && [o_uri rangeOfString: @"/dev/"].location != NSNotFound )
605 int i_count, i_index;
606 struct statfs *mounts = NULL;
608 i_count = getmntinfo (&mounts, MNT_NOWAIT);
609 /* getmntinfo returns a pointer to static data. Do not free. */
610 for( i_index = 0 ; i_index < i_count; i_index++ )
612 NSMutableString *o_temp, *o_temp2;
613 o_temp = [NSMutableString stringWithString: o_uri];
614 o_temp2 = [NSMutableString stringWithCString: mounts[i_index].f_mntfromname];
615 [o_temp replaceOccurrencesOfString: @"/dev/rdisk" withString: @"/dev/disk" options:NULL range:NSMakeRange(0, [o_temp length]) ];
616 [o_temp2 replaceOccurrencesOfString: @"s0" withString: @"" options:NULL range:NSMakeRange(0, [o_temp2 length]) ];
617 [o_temp2 replaceOccurrencesOfString: @"s1" withString: @"" options:NULL range:NSMakeRange(0, [o_temp2 length]) ];
619 if( strstr( [o_temp fileSystemRepresentation], [o_temp2 fileSystemRepresentation] ) != NULL )
621 o_name = [[NSFileManager defaultManager] displayNameAtPath: [NSString stringWithCString:mounts[i_index].f_mntonname]];
625 /* If no name, then make a guess */
626 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
628 if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
629 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
630 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
632 /* All of this is to make sure CD's play when you D&D them on VLC */
633 /* Converts mountpoint to a /dev file */
636 NSMutableString *o_temp;
638 buf = (struct statfs *) malloc (sizeof(struct statfs));
639 statfs( [o_uri fileSystemRepresentation], buf );
640 psz_dev = strdup(buf->f_mntfromname);
641 o_temp = [NSMutableString stringWithCString: psz_dev ];
642 [o_temp replaceOccurrencesOfString: @"/dev/disk" withString: @"/dev/rdisk" options:NULL range:NSMakeRange(0, [o_temp length]) ];
643 [o_temp replaceOccurrencesOfString: @"s0" withString: @"" options:NULL range:NSMakeRange(0, [o_temp length]) ];
644 [o_temp replaceOccurrencesOfString: @"s1" withString: @"" options:NULL range:NSMakeRange(0, [o_temp length]) ];
648 p_item = playlist_ItemNew( p_intf, [o_uri fileSystemRepresentation], [o_name UTF8String] );
654 for( i = 0; i < (int)[o_options count]; i++ )
656 playlist_ItemAddOption( p_item, strdup( [[o_options objectAtIndex:i] UTF8String] ) );
660 /* Recent documents menu */
661 o_true_file = [NSURL fileURLWithPath: o_uri];
662 if( o_true_file != nil )
664 [[NSDocumentController sharedDocumentController]
665 noteNewRecentDocumentURL: o_true_file];
668 vlc_object_release( p_playlist );
672 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
675 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
677 if( p_playlist == NULL )
682 for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
684 playlist_item_t *p_item;
685 NSDictionary *o_one_item;
688 o_one_item = [o_array objectAtIndex: i_item];
689 p_item = [self createItem: o_one_item];
696 playlist_AddItem( p_playlist, p_item, PLAYLIST_APPEND, i_position == -1 ? PLAYLIST_END : i_position + i_item );
698 if( i_item == 0 && !b_enqueue )
700 playlist_Control( p_playlist, PLAYLIST_ITEMPLAY, p_item );
703 vlc_object_release( p_playlist );
706 - (void)appendNodeArray:(NSArray*)o_array inNode:(playlist_item_t *)p_node atPos:(int)i_position inView:(int)i_view enqueue:(BOOL)b_enqueue
709 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
711 if( p_playlist == NULL )
716 for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
718 playlist_item_t *p_item;
719 NSDictionary *o_one_item;
722 o_one_item = [o_array objectAtIndex: i_item];
723 p_item = [self createItem: o_one_item];
730 playlist_NodeAddItem( p_playlist, p_item, i_view, p_node, PLAYLIST_APPEND, i_position + i_item );
732 if( i_item == 0 && !b_enqueue )
734 playlist_Control( p_playlist, PLAYLIST_ITEMPLAY, p_item );
737 vlc_object_release( p_playlist );
741 - (IBAction)handlePopUp:(id)sender
744 intf_thread_t * p_intf = VLCIntf;
745 vlc_value_t val1,val2;
746 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
748 if( p_playlist == NULL )
753 switch( [o_loop_popup indexOfSelectedItem] )
758 var_Set( p_playlist, "loop", val1 );
760 var_Set( p_playlist, "repeat", val1 );
761 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat One" ) );
766 var_Set( p_playlist, "repeat", val1 );
768 var_Set( p_playlist, "loop", val1 );
769 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat All" ) );
773 var_Get( p_playlist, "repeat", &val1 );
774 var_Get( p_playlist, "loop", &val2 );
775 if( val1.b_bool || val2.b_bool )
778 var_Set( p_playlist, "repeat", val1 );
779 var_Set( p_playlist, "loop", val1 );
780 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
784 vlc_object_release( p_playlist );
785 [self playlistUpdated];
788 - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
790 playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
792 playlist_item_t *p_selected_item;
793 int i_current, i_selected_row;
798 i_selected_row = [o_outline_view selectedRow];
799 if (i_selected_row < 0)
802 p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow:
803 i_selected_row] pointerValue];
805 for( i_current = 0; i_current < p_item->i_children ; i_current++ )
808 NSString *o_current_name, *o_current_author;
810 vlc_mutex_lock( &p_playlist->object_lock );
811 o_current_name = [NSString stringWithUTF8String:
812 p_item->pp_children[i_current]->input.psz_name];
813 psz_temp = vlc_input_item_GetInfo( &p_item->input ,
814 _("Meta-information"),_("Artist") );
815 o_current_author = [NSString stringWithUTF8String: psz_temp];
817 vlc_mutex_unlock( &p_playlist->object_lock );
819 if( p_selected_item == p_item->pp_children[i_current] &&
820 b_selected_item_met == NO )
822 b_selected_item_met = YES;
824 else if( p_selected_item == p_item->pp_children[i_current] &&
825 b_selected_item_met == YES )
827 vlc_object_release( p_playlist );
830 else if( b_selected_item_met == YES &&
831 ( [o_current_name rangeOfString:[o_search_field
832 stringValue] options:NSCaseInsensitiveSearch ].length ||
833 [o_current_author rangeOfString:[o_search_field
834 stringValue] options:NSCaseInsensitiveSearch ].length ) )
836 vlc_object_release( p_playlist );
837 /*Adds the parent items in the result array as well, so that we can
839 return [NSMutableArray arrayWithObject: [NSValue
840 valueWithPointer: p_item->pp_children[i_current]]];
842 if( p_item->pp_children[i_current]->i_children > 0 )
844 id o_result = [self subSearchItem:
845 p_item->pp_children[i_current]];
846 if( o_result != NULL )
848 vlc_object_release( p_playlist );
849 [o_result insertObject: [NSValue valueWithPointer:
850 p_item->pp_children[i_current]] atIndex:0];
855 vlc_object_release( p_playlist );
859 - (IBAction)searchItem:(id)sender
861 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
863 playlist_view_t * p_view;
869 b_selected_item_met = NO;
871 if( p_playlist == NULL )
873 p_view = playlist_ViewFind( p_playlist, i_current_view );
877 /*First, only search after the selected item:*
878 *(b_selected_item_met = NO) */
879 o_result = [self subSearchItem:p_view->p_root];
880 if( o_result == NULL )
882 /* If the first search failed, search again from the beginning */
883 o_result = [self subSearchItem:p_view->p_root];
885 if( o_result != NULL )
887 for( i = 0 ; i < [o_result count] - 1 ; i++ )
889 [o_outline_view expandItem: [o_outline_dict objectForKey:
890 [NSString stringWithFormat: @"%p",
891 [[o_result objectAtIndex: i] pointerValue]]]];
893 i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
894 [NSString stringWithFormat: @"%p",
895 [[o_result objectAtIndex: [o_result count] - 1 ]
900 [o_outline_view selectRow:i_row byExtendingSelection: NO];
901 [o_outline_view scrollRowToVisible: i_row];
904 vlc_object_release( p_playlist );
907 - (NSMenu *)menuForEvent:(NSEvent *)o_event
911 vlc_bool_t b_item_sel;
913 pt = [o_outline_view convertPoint: [o_event locationInWindow]
915 b_item_sel = ( [o_outline_view rowAtPoint: pt] != -1 &&
916 [o_outline_view selectedRow] != -1 );
917 b_rows = [o_outline_view numberOfRows] != 0;
919 [o_mi_play setEnabled: b_item_sel];
920 [o_mi_delete setEnabled: b_item_sel];
921 [o_mi_selectall setEnabled: b_rows];
922 [o_mi_info setEnabled: b_item_sel];
924 return( o_ctx_menu );
927 - (playlist_item_t *)selectedPlaylistItem
929 return [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
933 - (void)outlineView: (NSTableView*)o_tv
934 didClickTableColumn:(NSTableColumn *)o_tc
936 int i_mode = 0, i_type;
937 intf_thread_t *p_intf = VLCIntf;
938 playlist_view_t *p_view;
940 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
942 if( p_playlist == NULL )
947 /* Check whether the selected table column header corresponds to a
948 sortable table column*/
949 if( !( o_tc == o_tc_name || o_tc == o_tc_author ) )
951 vlc_object_release( p_playlist );
955 p_view = playlist_ViewFind( p_playlist, i_current_view );
957 if( o_tc_sortColumn == o_tc )
959 b_isSortDescending = !b_isSortDescending;
963 b_isSortDescending = VLC_FALSE;
966 if( o_tc == o_tc_name )
970 else if( o_tc == o_tc_author )
972 i_mode = SORT_AUTHOR;
975 if( b_isSortDescending )
977 i_type = ORDER_REVERSE;
981 i_type = ORDER_NORMAL;
984 vlc_mutex_lock( &p_playlist->object_lock );
985 playlist_RecursiveNodeSort( p_playlist, p_view->p_root, i_mode, i_type );
986 vlc_mutex_unlock( &p_playlist->object_lock );
988 vlc_object_release( p_playlist );
989 [self playlistUpdated];
991 o_tc_sortColumn = o_tc;
992 [o_outline_view setHighlightedTableColumn:o_tc];
994 if( b_isSortDescending )
996 [o_outline_view setIndicatorImage:o_descendingSortingImage
1001 [o_outline_view setIndicatorImage:o_ascendingSortingImage
1002 inTableColumn:o_tc];
1007 - (void)outlineView:(NSOutlineView *)outlineView
1008 willDisplayCell:(id)cell
1009 forTableColumn:(NSTableColumn *)tableColumn
1012 playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1014 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
1016 if( !p_playlist ) return;
1018 if( ( p_item == p_playlist->status.p_item ) ||
1019 ( p_item->i_children != 0 &&
1020 [self isItem: p_playlist->status.p_item inNode: p_item] ) )
1022 [cell setFont: [NSFont boldSystemFontOfSize: 0]];
1026 [cell setFont: [NSFont systemFontOfSize: 0]];
1028 vlc_object_release( p_playlist );
1033 @implementation VLCPlaylist (NSOutlineViewDataSource)
1035 /* return the number of children for Obj-C pointer item */ /* DONE */
1036 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
1039 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1041 if( p_playlist == NULL || outlineView != o_outline_view )
1047 playlist_view_t *p_view;
1048 p_view = playlist_ViewFind( p_playlist, i_current_view );
1049 if( p_view && p_view->p_root )
1050 i_return = p_view->p_root->i_children;
1054 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
1056 i_return = p_item->i_children;
1058 vlc_object_release( p_playlist );
1066 /* return the child at index for the Obj-C pointer item */ /* DONE */
1067 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
1069 playlist_item_t *p_return = NULL;
1070 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1074 if( p_playlist == NULL )
1080 playlist_view_t *p_view;
1081 p_view = playlist_ViewFind( p_playlist, i_current_view );
1082 if( p_view && index < p_view->p_root->i_children && index >= 0 )
1083 p_return = p_view->p_root->pp_children[index];
1087 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
1088 if( p_item && index < p_item->i_children && index >= 0 )
1089 p_return = p_item->pp_children[index];
1092 if( p_playlist->i_size >= 2 )
1094 [o_status_field setStringValue: [NSString stringWithFormat:
1095 _NS("%i items in playlist"), p_playlist->i_size]];
1099 if( p_playlist->i_size == 0 )
1101 [o_status_field setStringValue: [NSString stringWithFormat:
1102 _NS("no items in playlist"), p_playlist->i_size]];
1106 [o_status_field setStringValue: [NSString stringWithFormat:
1107 _NS("1 item in playlist"), p_playlist->i_size]];
1111 vlc_object_release( p_playlist );
1113 o_value = [[NSValue valueWithPointer: p_return] retain];
1115 [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p", p_return]];
1119 /* is the item expandable */
1120 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
1123 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1125 if( p_playlist == NULL )
1131 playlist_view_t *p_view;
1132 p_view = playlist_ViewFind( p_playlist, i_current_view );
1133 if( p_view && p_view->p_root )
1134 i_return = p_view->p_root->i_children;
1138 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
1140 i_return = p_item->i_children;
1142 vlc_object_release( p_playlist );
1150 /* retrieve the string values for the cells */
1151 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
1154 intf_thread_t *p_intf = VLCIntf;
1155 playlist_t *p_playlist;
1156 playlist_item_t *p_item;
1158 if( item == nil || ![item isKindOfClass: [NSValue class]] ) return( @"error" );
1160 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1162 if( p_playlist == NULL )
1167 p_item = (playlist_item_t *)[item pointerValue];
1169 if( p_item == NULL )
1171 vlc_object_release( p_playlist );
1175 if( [[o_tc identifier] isEqualToString:@"1"] )
1177 o_value = [NSString stringWithUTF8String:
1178 p_item->input.psz_name];
1179 if( o_value == NULL )
1180 o_value = [NSString stringWithCString:
1181 p_item->input.psz_name];
1183 else if( [[o_tc identifier] isEqualToString:@"2"] )
1186 psz_temp = vlc_input_item_GetInfo( &p_item->input ,_("Meta-information"),_("Artist") );
1188 if( psz_temp == NULL )
1192 o_value = [NSString stringWithUTF8String: psz_temp];
1193 if( o_value == NULL )
1195 o_value = [NSString stringWithCString: psz_temp];
1200 else if( [[o_tc identifier] isEqualToString:@"3"] )
1202 char psz_duration[MSTRTIME_MAX_SIZE];
1203 mtime_t dur = p_item->input.i_duration;
1206 secstotimestr( psz_duration, dur/1000000 );
1207 o_value = [NSString stringWithUTF8String: psz_duration];
1211 o_value = @"-:--:--";
1214 vlc_object_release( p_playlist );
1219 /* Required for drag & drop and reordering */
1220 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
1224 for( i = 0 ; i < [items count] ; i++ )
1226 if( [outlineView levelForItem: [items objectAtIndex: i]] == 0 )
1234 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
1236 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1238 if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1240 return NSDragOperationGeneric;
1242 return NSDragOperationNone;
1245 - (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(int)index
1247 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1249 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1251 if( !p_playlist ) return NO;
1253 if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1256 playlist_item_t *p_node = [item pointerValue];
1258 NSArray *o_array = [NSArray array];
1259 NSArray *o_values = [[o_pasteboard propertyListForType:
1260 NSFilenamesPboardType]
1261 sortedArrayUsingSelector:
1262 @selector(caseInsensitiveCompare:)];
1264 for( i = 0; i < (int)[o_values count]; i++)
1266 NSDictionary *o_dic;
1267 o_dic = [NSDictionary dictionaryWithObject:[o_values
1268 objectAtIndex:i] forKey:@"ITEM_URL"];
1269 o_array = [o_array arrayByAddingObject: o_dic];
1274 [self appendArray: o_array atPos: index enqueue: YES];
1276 else if( p_node->i_children == -1 )
1279 playlist_item_t *p_real_node = NULL;
1281 for( i_counter = 0 ; i_counter < p_node->i_parents ; i_counter++ )
1283 if( p_node->pp_parents[i_counter]->i_view == i_current_view )
1285 p_real_node = p_node->pp_parents[i_counter]->p_parent;
1288 if( i_counter == p_node->i_parents )
1293 [self appendNodeArray: o_array inNode: p_real_node
1294 atPos: index inView: i_current_view enqueue: YES];
1298 [self appendNodeArray: o_array inNode: p_node
1299 atPos: index inView: i_current_view enqueue: YES];
1301 vlc_object_release( p_playlist );
1304 vlc_object_release( p_playlist );
1308 /* Delegate method of NSWindow */
1309 /*- (void)windowWillClose:(NSNotification *)aNotification
1311 [o_btn_playlist setState: NSOffState];