1 /*****************************************************************************
2 * playlist.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2002-2004 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 * connect delegates, actions and outlets in IB
28 * implement delete by backspace
29 * implement playlist item rightclick menu
31 * add 'icons' for different types of nodes? (http://www.cocoadev.com/index.pl?IconAndTextInTableCell)
32 * create a new 'playlist toggle' that hides the playlist and in effect give you the old controller
33 * create a new search field build with pictures from the 'regular' search field, so it can be emulated on 10.2
34 * create toggle buttons for the shuffle, repeat one, repeat all functions.
35 * implement drag and drop and item reordering.
36 * reimplement enable/disable item
37 * create a new 'tool' button (see the gear button in the Finder window) for 'actions'
38 (adding service discovery, other views, new node/playlist, save node/playlist) stuff like that
43 /*****************************************************************************
45 *****************************************************************************/
46 #include <stdlib.h> /* malloc(), free() */
47 #include <sys/param.h> /* for MAXPATHLEN */
50 #include <sys/mount.h>
58 /*****************************************************************************
59 * VLCPlaylistView implementation
60 *****************************************************************************/
61 @implementation VLCPlaylistView
63 - (NSMenu *)menuForEvent:(NSEvent *)o_event
65 return( [[self delegate] menuForEvent: o_event] );
68 - (void)keyDown:(NSEvent *)o_event
72 NSMutableArray *o_to_delete;
75 playlist_t * p_playlist;
76 intf_thread_t * p_intf = VLCIntf;
77 msg_Dbg( p_intf, "KEYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY");
78 if( [[o_event characters] length] )
80 key = [[o_event characters] characterAtIndex: 0];
83 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
86 if ( p_playlist == NULL )
93 case NSDeleteCharacter:
94 case NSDeleteFunctionKey:
95 case NSDeleteCharFunctionKey:
96 case NSBackspaceCharacter:
97 o_to_delete = [NSMutableArray arrayWithArray:[[self selectedRowEnumerator] allObjects]];
98 c = [o_to_delete count];
100 for( i = 0; i < c; i++ ) {
101 playlist_item_t * p_item;
102 o_number = [o_to_delete lastObject];
103 i_row = [o_number intValue];
105 if( p_playlist->status.p_item == [[self itemAtRow: i_row] pointerValue] && p_playlist->status.i_status )
107 playlist_Stop( p_playlist );
109 [o_to_delete removeObject: o_number];
110 [self deselectRow: i_row];
111 p_item = (playlist_item_t *)[[self itemAtRow: i_row]pointerValue];
112 playlist_Delete( p_playlist, p_item->input.i_id );
118 [super keyDown: o_event];
122 if( p_playlist != NULL )
124 vlc_object_release( p_playlist );
131 /*****************************************************************************
132 * VLCPlaylist implementation
133 *****************************************************************************/
134 @implementation VLCPlaylist
148 [o_outline_view setTarget: self];
149 [o_outline_view setDelegate: self];
150 [o_outline_view setDataSource: self];
152 [o_outline_view setDoubleAction: @selector(playItem:)];
154 [o_outline_view registerForDraggedTypes:
155 [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
156 [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
158 /* We need to check whether _defaultTableHeaderSortImage exists, since it
159 belongs to an Apple hidden private API, and then can "disapear" at any time*/
161 if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderSortImage)] )
163 o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
167 o_ascendingSortingImage = nil;
170 if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderReverseSortImage)] )
172 o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
176 o_descendingSortingImage = nil;
179 o_outline_dict = [[NSMutableDictionary alloc] init];
182 //[self playlistUpdated];
187 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
189 [o_mi_play setTitle: _NS("Play")];
190 [o_mi_delete setTitle: _NS("Delete")];
191 [o_mi_selectall setTitle: _NS("Select All")];
192 [o_mi_toggleItemsEnabled setTitle: _NS("Item Enabled")];
193 [o_mi_enableGroup setTitle: _NS("Enable all group items")];
194 [o_mi_disableGroup setTitle: _NS("Disable all group items")];
195 [o_mi_info setTitle: _NS("Properties")];
197 [[o_tc_name headerCell] setStringValue:_NS("Name")];
198 [[o_tc_author headerCell] setStringValue:_NS("Author")];
199 [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
200 [o_status_field setStringValue: [NSString stringWithFormat:
201 _NS("0 items in playlist")]];
203 [o_random_ckb setTitle: _NS("Random")];
205 [o_search_button setTitle: _NS("Search")];
207 [o_btn_playlist setToolTip: _NS("Playlist")];
208 [[o_loop_popup itemAtIndex:0] setTitle: _NS("Standard Play")];
209 [[o_loop_popup itemAtIndex:1] setTitle: _NS("Repeat One")];
210 [[o_loop_popup itemAtIndex:2] setTitle: _NS("Repeat All")];
213 - (void)playlistUpdated
215 [o_outline_view reloadData];
218 - (IBAction)playItem:(id)sender
220 intf_thread_t * p_intf = VLCIntf;
221 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
224 if( p_playlist != NULL )
226 playlist_item_t *p_item;
227 playlist_view_t *p_view;
228 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
229 p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
232 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VIEW_SIMPLE, p_view ? p_view->p_root : NULL, p_item );
233 vlc_object_release( p_playlist );
237 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
240 intf_thread_t * p_intf = VLCIntf;
241 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
244 if( p_playlist == NULL )
249 for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
252 NSDictionary *o_one_item;
253 int j, i_total_options = 0, i_new_id = -1;
254 int i_mode = PLAYLIST_INSERT;
255 BOOL b_rem = FALSE, b_dir = FALSE;
256 NSString *o_uri, *o_name;
259 char **ppsz_options = NULL;
262 o_one_item = [o_array objectAtIndex: i_item];
263 o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
264 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
265 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
267 /* If no name, then make a guess */
268 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
270 if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
271 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
272 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
274 /* All of this is to make sure CD's play when you D&D them on VLC */
275 /* Converts mountpoint to a /dev file */
278 buf = (struct statfs *) malloc (sizeof(struct statfs));
279 statfs( [o_uri fileSystemRepresentation], buf );
280 psz_dev = strdup(buf->f_mntfromname);
281 o_uri = [NSString stringWithCString: psz_dev ];
284 if( o_options && [o_options count] > 0 )
286 /* Count the input options */
287 i_total_options = [o_options count];
289 /* Allocate ppsz_options */
290 for( j = 0; j < i_total_options; j++ )
293 ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
295 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
300 i_new_id = playlist_AddExt( p_playlist, [o_uri fileSystemRepresentation],
301 [o_name UTF8String], i_mode,
302 i_position == -1 ? PLAYLIST_END : i_position + i_item,
303 0, (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options );
306 for( j = 0; j < i_total_options; j++ )
307 free( ppsz_options[j] );
308 if( ppsz_options ) free( ppsz_options ); */
310 /* Recent documents menu */
311 o_true_file = [NSURL fileURLWithPath: o_uri];
312 if( o_true_file != nil )
314 [[NSDocumentController sharedDocumentController]
315 noteNewRecentDocumentURL: o_true_file];
318 if( i_item == 0 && !b_enqueue )
320 playlist_Goto( p_playlist, playlist_GetPositionById( p_playlist, i_new_id ) );
321 playlist_Play( p_playlist );
325 vlc_object_release( p_playlist );
328 - (IBAction)handlePopUp:(id)sender
331 intf_thread_t * p_intf = VLCIntf;
332 vlc_value_t val1,val2;
333 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
335 if( p_playlist == NULL )
340 switch ([o_loop_popup indexOfSelectedItem])
345 var_Set( p_playlist, "loop", val1 );
347 var_Set( p_playlist, "repeat", val1 );
348 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat One" ) );
353 var_Set( p_playlist, "repeat", val1 );
355 var_Set( p_playlist, "loop", val1 );
356 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat All" ) );
360 var_Get( p_playlist, "repeat", &val1 );
361 var_Get( p_playlist, "loop", &val2 );
362 if (val1.b_bool || val2.b_bool)
365 var_Set( p_playlist, "repeat", val1 );
366 var_Set( p_playlist, "loop", val1 );
367 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
371 vlc_object_release( p_playlist );
372 [self playlistUpdated];
375 - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
377 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
379 playlist_item_t * p_selected_item;
380 int i_current, i_selected_row;
385 i_selected_row = [o_outline_view selectedRow];
386 if (i_selected_row < 0)
389 p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow:
390 i_selected_row] pointerValue];
392 for (i_current = 0; i_current < p_item->i_children ; i_current++)
395 NSString * o_current_name, * o_current_author;
397 vlc_mutex_lock( &p_playlist->object_lock );
398 o_current_name = [NSString stringWithUTF8String:
399 p_item->pp_children[i_current]->input.psz_name];
400 psz_temp = playlist_ItemGetInfo(p_item ,_("Meta-information"),_("Author") );
401 o_current_author = [NSString stringWithUTF8String: psz_temp];
403 vlc_mutex_unlock( &p_playlist->object_lock );
405 if (p_selected_item == p_item->pp_children[i_current] &&
406 b_selected_item_met == NO)
408 b_selected_item_met = YES;
410 else if (p_selected_item == p_item->pp_children[i_current] &&
411 b_selected_item_met == YES)
413 vlc_object_release(p_playlist);
416 else if (b_selected_item_met == YES &&
417 ([o_current_name rangeOfString:[o_search_field
418 stringValue] options:NSCaseInsensitiveSearch ].length ||
419 [o_current_author rangeOfString:[o_search_field
420 stringValue] options:NSCaseInsensitiveSearch ].length))
422 vlc_object_release(p_playlist);
423 /*Adds the parent items in the result array as well, so that we can
425 return [NSMutableArray arrayWithObject: [NSValue
426 valueWithPointer: p_item->pp_children[i_current]]];
428 if (p_item->pp_children[i_current]->i_children > 0)
430 id o_result = [self subSearchItem:
431 p_item->pp_children[i_current]];
432 if (o_result != NULL)
434 vlc_object_release(p_playlist);
435 [o_result insertObject: [NSValue valueWithPointer:
436 p_item->pp_children[i_current]] atIndex:0];
441 vlc_object_release(p_playlist);
445 - (IBAction)searchItem:(id)sender
447 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
449 playlist_view_t * p_view;
455 b_selected_item_met = NO;
457 if( p_playlist == NULL )
460 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
464 /*First, only search after the selected item:*
465 *(b_selected_item_met = NO) */
466 o_result = [self subSearchItem:p_view->p_root];
467 if (o_result == NULL)
469 /* If the first search failed, search again from the beginning */
470 o_result = [self subSearchItem:p_view->p_root];
472 if (o_result != NULL)
474 for (i = 0 ; i < [o_result count] - 1 ; i++)
476 [o_outline_view expandItem: [o_outline_dict objectForKey:
477 [NSString stringWithFormat: @"%p",
478 [[o_result objectAtIndex: i] pointerValue]]]];
480 i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
481 [NSString stringWithFormat: @"%p",
482 [[o_result objectAtIndex: [o_result count] - 1 ]
487 [o_outline_view selectRow:i_row byExtendingSelection: NO];
488 [o_outline_view scrollRowToVisible: i_row];
491 vlc_object_release(p_playlist);
497 @implementation VLCPlaylist (NSOutlineViewDataSource)
499 /* return the number of children for Obj-C pointer item */ /* DONE */
500 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
503 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
505 if( p_playlist == NULL )
511 playlist_view_t *p_view;
512 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
513 if( p_view && p_view->p_root )
514 i_return = p_view->p_root->i_children;
518 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
520 i_return = p_item->i_children;
522 vlc_object_release( p_playlist );
523 if( i_return == -1 ) i_return = 0;
524 msg_Dbg( p_playlist, "I have %d children", i_return );
528 /* return the child at index for the Obj-C pointer item */ /* DONE */
529 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
531 playlist_item_t *p_return = NULL;
532 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
536 if( p_playlist == NULL )
542 playlist_view_t *p_view;
543 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
544 if( p_view && index < p_view->p_root->i_children )
545 p_return = p_view->p_root->pp_children[index];
549 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
550 if( p_item && index < p_item->i_children )
552 p_return = p_item->pp_children[index];
556 [o_status_field setStringValue: [NSString stringWithFormat:
557 _NS("%i items in playlist"), p_playlist->i_size]];
559 vlc_object_release( p_playlist );
560 msg_Dbg( p_playlist, "childitem with index %d", index );
562 o_value = [NSValue valueWithPointer: p_return];
564 [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p", p_return]];
568 /* is the item expandable */
569 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
572 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
574 if( p_playlist == NULL )
580 playlist_view_t *p_view;
581 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
582 if( p_view && p_view->p_root )
583 i_return = p_view->p_root->i_children;
587 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
589 i_return = p_item->i_children;
591 vlc_object_release( p_playlist );
593 if( i_return == -1 || i_return == 0 )
599 /* retrieve the string values for the cells */
600 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
603 intf_thread_t * p_intf = VLCIntf;
604 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
606 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
608 if( p_playlist == NULL || p_item == NULL )
613 if( [[o_tc identifier] isEqualToString:@"1"] )
615 o_value = [NSString stringWithUTF8String:
616 p_item->input.psz_name];
617 if( o_value == NULL )
618 o_value = [NSString stringWithCString:
619 p_item->input.psz_name];
621 else if( [[o_tc identifier] isEqualToString:@"2"] )
624 psz_temp = playlist_ItemGetInfo( p_item ,_("Meta-information"),_("Artist") );
626 if( psz_temp == NULL )
630 o_value = [NSString stringWithUTF8String: psz_temp];
631 if( o_value == NULL )
633 o_value = [NSString stringWithCString: psz_temp];
638 else if( [[o_tc identifier] isEqualToString:@"3"] )
640 char psz_duration[MSTRTIME_MAX_SIZE];
641 mtime_t dur = p_item->input.i_duration;
644 secstotimestr( psz_duration, dur/1000000 );
645 o_value = [NSString stringWithUTF8String: psz_duration];
649 o_value = @"-:--:--";
653 vlc_object_release( p_playlist );
658 /* Required for drag & drop and reordering */
659 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
664 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
666 return NSDragOperationNone;
669 /* Delegate method of NSWindow */
670 - (void)windowWillClose:(NSNotification *)aNotification
672 [o_btn_playlist setState: NSOffState];