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 - (bool)isItem:(playlist_item_t *)p_item inNode:(playlist_item_t *)p_node
70 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
72 playlist_item_t * p_temp_item = p_item;
74 if ( p_playlist == NULL )
79 while ( p_temp_item->i_parents > 0 )
82 for (i = 0; i < p_temp_item->i_parents ; i++)
84 if (p_temp_item->pp_parents[i]->i_view == VIEW_SIMPLE)
86 if (p_temp_item->pp_parents[i]->p_parent == p_node)
88 vlc_object_release(p_playlist);
93 p_temp_item = p_temp_item->pp_parents[i]->p_parent;
100 vlc_object_release(p_playlist);
104 - (void)keyDown:(NSEvent *)o_event
108 NSMutableArray *o_to_delete;
111 playlist_t * p_playlist;
112 intf_thread_t * p_intf = VLCIntf;
113 msg_Dbg( p_intf, "KEYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY");
114 if( [[o_event characters] length] )
116 key = [[o_event characters] characterAtIndex: 0];
119 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
122 if ( p_playlist == NULL )
129 case NSDeleteCharacter:
130 case NSDeleteFunctionKey:
131 case NSDeleteCharFunctionKey:
132 case NSBackspaceCharacter:
133 o_to_delete = [NSMutableArray arrayWithArray:[[self selectedRowEnumerator] allObjects]];
134 c = [o_to_delete count];
136 for( i = 0; i < c; i++ ) {
137 playlist_item_t * p_item;
138 o_number = [o_to_delete lastObject];
139 i_row = [o_number intValue];
141 [o_to_delete removeObject: o_number];
142 [self deselectRow: i_row];
143 p_item = (playlist_item_t *)[[self itemAtRow: i_row]pointerValue];
144 if (p_item->i_children > -1)
146 if ([self isItem:p_playlist->status.p_item inNode: p_item]
147 == YES && p_playlist->status.i_status)
149 playlist_Stop( p_playlist );
151 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE);
155 if( p_playlist->status.p_item == [[self itemAtRow: i_row]
156 pointerValue] && p_playlist->status.i_status )
158 playlist_Stop( p_playlist );
160 playlist_Delete( p_playlist, p_item->input.i_id );
162 [[self delegate] playlistUpdated];
167 [super keyDown: o_event];
171 if( p_playlist != NULL )
173 vlc_object_release( p_playlist );
179 /*****************************************************************************
180 * VLCPlaylist implementation
181 *****************************************************************************/
182 @implementation VLCPlaylist
196 [o_outline_view setTarget: self];
197 [o_outline_view setDelegate: self];
198 [o_outline_view setDataSource: self];
200 [o_outline_view setDoubleAction: @selector(playItem:)];
202 [o_outline_view registerForDraggedTypes:
203 [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
204 [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
206 /* We need to check whether _defaultTableHeaderSortImage exists, since it
207 belongs to an Apple hidden private API, and then can "disapear" at any time*/
209 if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderSortImage)] )
211 o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
215 o_ascendingSortingImage = nil;
218 if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderReverseSortImage)] )
220 o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
224 o_descendingSortingImage = nil;
227 o_outline_dict = [[NSMutableDictionary alloc] init];
230 //[self playlistUpdated];
235 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
236 [o_mi_play setTitle: _NS("Play")];
237 [o_mi_delete setTitle: _NS("Delete")];
238 [o_mi_selectall setTitle: _NS("Select All")];
239 [o_mi_info setTitle: _NS("Properties")];
240 [[o_tc_name headerCell] setStringValue:_NS("Name")];
241 [[o_tc_author headerCell] setStringValue:_NS("Author")];
242 [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
243 [o_status_field setStringValue: [NSString stringWithFormat:
244 _NS("0 items in playlist")]];
246 [o_random_ckb setTitle: _NS("Random")];
248 [o_search_button setTitle: _NS("Search")];
250 [o_btn_playlist setToolTip: _NS("Playlist")];
251 [[o_loop_popup itemAtIndex:0] setTitle: _NS("Standard Play")];
252 [[o_loop_popup itemAtIndex:1] setTitle: _NS("Repeat One")];
253 [[o_loop_popup itemAtIndex:2] setTitle: _NS("Repeat All")];
256 - (void)playlistUpdated
258 [o_outline_dict removeAllObjects];
259 [o_outline_view reloadData];
262 - (IBAction)playItem:(id)sender
264 intf_thread_t * p_intf = VLCIntf;
265 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
268 if( p_playlist != NULL )
270 playlist_item_t *p_item;
271 playlist_item_t *p_node = NULL;
274 p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
278 if (p_item->i_children == -1)
280 for (i = 0 ; i < p_item->i_parents ; i++)
282 if (p_item->pp_parents[i]->i_view == VIEW_SIMPLE)
284 p_node = p_item->pp_parents[i]->p_parent;
291 if (p_node->pp_children[0]->i_children == -1 &&
292 p_node->i_children > 0)
294 p_item = p_node->pp_children[0];
302 // p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
305 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VIEW_SIMPLE, p_node, p_item );
306 // playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VIEW_SIMPLE, p_view ? p_view->p_root : NULL, p_item );
308 vlc_object_release( p_playlist );
312 - (IBAction)selectAll:(id)sender
314 [o_outline_view selectAll: nil];
318 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
321 intf_thread_t * p_intf = VLCIntf;
322 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
325 if( p_playlist == NULL )
330 for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
333 NSDictionary *o_one_item;
334 int j, i_total_options = 0, i_new_id = -1;
335 int i_mode = PLAYLIST_INSERT;
336 BOOL b_rem = FALSE, b_dir = FALSE;
337 NSString *o_uri, *o_name;
340 char **ppsz_options = NULL;
343 o_one_item = [o_array objectAtIndex: i_item];
344 o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
345 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
346 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
348 /* If no name, then make a guess */
349 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
351 if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
352 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
353 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
355 /* All of this is to make sure CD's play when you D&D them on VLC */
356 /* Converts mountpoint to a /dev file */
359 buf = (struct statfs *) malloc (sizeof(struct statfs));
360 statfs( [o_uri fileSystemRepresentation], buf );
361 psz_dev = strdup(buf->f_mntfromname);
362 o_uri = [NSString stringWithCString: psz_dev ];
365 if( o_options && [o_options count] > 0 )
367 /* Count the input options */
368 i_total_options = [o_options count];
370 /* Allocate ppsz_options */
371 for( j = 0; j < i_total_options; j++ )
374 ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
376 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
381 i_new_id = playlist_AddExt( p_playlist, [o_uri fileSystemRepresentation],
382 [o_name UTF8String], i_mode,
383 i_position == -1 ? PLAYLIST_END : i_position + i_item,
384 0, (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options );
387 for( j = 0; j < i_total_options; j++ )
388 free( ppsz_options[j] );
389 if( ppsz_options ) free( ppsz_options ); */
391 /* Recent documents menu */
392 o_true_file = [NSURL fileURLWithPath: o_uri];
393 if( o_true_file != nil )
395 [[NSDocumentController sharedDocumentController]
396 noteNewRecentDocumentURL: o_true_file];
399 if( i_item == 0 && !b_enqueue )
401 playlist_Goto( p_playlist, playlist_GetPositionById( p_playlist, i_new_id ) );
402 playlist_Play( p_playlist );
406 vlc_object_release( p_playlist );
409 - (IBAction)handlePopUp:(id)sender
412 intf_thread_t * p_intf = VLCIntf;
413 vlc_value_t val1,val2;
414 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
416 if( p_playlist == NULL )
421 switch ([o_loop_popup indexOfSelectedItem])
426 var_Set( p_playlist, "loop", val1 );
428 var_Set( p_playlist, "repeat", val1 );
429 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat One" ) );
434 var_Set( p_playlist, "repeat", val1 );
436 var_Set( p_playlist, "loop", val1 );
437 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat All" ) );
441 var_Get( p_playlist, "repeat", &val1 );
442 var_Get( p_playlist, "loop", &val2 );
443 if (val1.b_bool || val2.b_bool)
446 var_Set( p_playlist, "repeat", val1 );
447 var_Set( p_playlist, "loop", val1 );
448 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
452 vlc_object_release( p_playlist );
453 [self playlistUpdated];
456 - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
458 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
460 playlist_item_t * p_selected_item;
461 int i_current, i_selected_row;
466 i_selected_row = [o_outline_view selectedRow];
467 if (i_selected_row < 0)
470 p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow:
471 i_selected_row] pointerValue];
473 for (i_current = 0; i_current < p_item->i_children ; i_current++)
476 NSString * o_current_name, * o_current_author;
478 vlc_mutex_lock( &p_playlist->object_lock );
479 o_current_name = [NSString stringWithUTF8String:
480 p_item->pp_children[i_current]->input.psz_name];
481 psz_temp = playlist_ItemGetInfo(p_item ,_("Meta-information"),_("Author") );
482 o_current_author = [NSString stringWithUTF8String: psz_temp];
484 vlc_mutex_unlock( &p_playlist->object_lock );
486 if (p_selected_item == p_item->pp_children[i_current] &&
487 b_selected_item_met == NO)
489 b_selected_item_met = YES;
491 else if (p_selected_item == p_item->pp_children[i_current] &&
492 b_selected_item_met == YES)
494 vlc_object_release(p_playlist);
497 else if (b_selected_item_met == YES &&
498 ([o_current_name rangeOfString:[o_search_field
499 stringValue] options:NSCaseInsensitiveSearch ].length ||
500 [o_current_author rangeOfString:[o_search_field
501 stringValue] options:NSCaseInsensitiveSearch ].length))
503 vlc_object_release(p_playlist);
504 /*Adds the parent items in the result array as well, so that we can
506 return [NSMutableArray arrayWithObject: [NSValue
507 valueWithPointer: p_item->pp_children[i_current]]];
509 if (p_item->pp_children[i_current]->i_children > 0)
511 id o_result = [self subSearchItem:
512 p_item->pp_children[i_current]];
513 if (o_result != NULL)
515 vlc_object_release(p_playlist);
516 [o_result insertObject: [NSValue valueWithPointer:
517 p_item->pp_children[i_current]] atIndex:0];
522 vlc_object_release(p_playlist);
526 - (IBAction)searchItem:(id)sender
528 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
530 playlist_view_t * p_view;
536 b_selected_item_met = NO;
538 if( p_playlist == NULL )
541 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
545 /*First, only search after the selected item:*
546 *(b_selected_item_met = NO) */
547 o_result = [self subSearchItem:p_view->p_root];
548 if (o_result == NULL)
550 /* If the first search failed, search again from the beginning */
551 o_result = [self subSearchItem:p_view->p_root];
553 if (o_result != NULL)
555 for (i = 0 ; i < [o_result count] - 1 ; i++)
557 [o_outline_view expandItem: [o_outline_dict objectForKey:
558 [NSString stringWithFormat: @"%p",
559 [[o_result objectAtIndex: i] pointerValue]]]];
561 i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
562 [NSString stringWithFormat: @"%p",
563 [[o_result objectAtIndex: [o_result count] - 1 ]
568 [o_outline_view selectRow:i_row byExtendingSelection: NO];
569 [o_outline_view scrollRowToVisible: i_row];
572 vlc_object_release(p_playlist);
576 - (NSMenu *)menuForEvent:(NSEvent *)o_event
580 vlc_bool_t b_item_sel;
582 pt = [o_outline_view convertPoint: [o_event locationInWindow]
584 b_item_sel = ( [o_outline_view rowAtPoint: pt] != -1 &&
585 [o_outline_view selectedRow] != -1 );
586 b_rows = [o_outline_view numberOfRows] != 0;
588 [o_mi_play setEnabled: b_item_sel];
589 [o_mi_delete setEnabled: b_item_sel];
590 [o_mi_selectall setEnabled: b_rows];
591 [o_mi_info setEnabled: b_item_sel];
593 return( o_ctx_menu );
596 - (playlist_item_t *)selectedPlaylistItem
598 return [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
604 @implementation VLCPlaylist (NSOutlineViewDataSource)
606 /* return the number of children for Obj-C pointer item */ /* DONE */
607 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
610 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
612 if( p_playlist == NULL )
618 playlist_view_t *p_view;
619 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
620 if( p_view && p_view->p_root )
621 i_return = p_view->p_root->i_children;
625 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
627 i_return = p_item->i_children;
629 vlc_object_release( p_playlist );
630 if( i_return == -1 ) i_return = 0;
631 msg_Dbg( p_playlist, "I have %d children", i_return );
635 /* return the child at index for the Obj-C pointer item */ /* DONE */
636 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
638 playlist_item_t *p_return = NULL;
639 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
643 if( p_playlist == NULL )
649 playlist_view_t *p_view;
650 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
651 if( p_view && index < p_view->p_root->i_children )
652 p_return = p_view->p_root->pp_children[index];
656 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
657 if( p_item && index < p_item->i_children )
659 p_return = p_item->pp_children[index];
663 [o_status_field setStringValue: [NSString stringWithFormat:
664 _NS("%i items in playlist"), p_playlist->i_size]];
666 vlc_object_release( p_playlist );
667 msg_Dbg( p_playlist, "childitem with index %d", index );
669 o_value = [NSValue valueWithPointer: p_return];
671 [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p", p_return]];
675 /* is the item expandable */
676 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
679 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
681 if( p_playlist == NULL )
687 playlist_view_t *p_view;
688 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
689 if( p_view && p_view->p_root )
690 i_return = p_view->p_root->i_children;
694 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
696 i_return = p_item->i_children;
698 vlc_object_release( p_playlist );
700 if( i_return == -1 || i_return == 0 )
706 /* retrieve the string values for the cells */
707 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
710 intf_thread_t * p_intf = VLCIntf;
711 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
713 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
715 if( p_playlist == NULL || p_item == NULL )
720 if( [[o_tc identifier] isEqualToString:@"1"] )
722 o_value = [NSString stringWithUTF8String:
723 p_item->input.psz_name];
724 if( o_value == NULL )
725 o_value = [NSString stringWithCString:
726 p_item->input.psz_name];
728 else if( [[o_tc identifier] isEqualToString:@"2"] )
731 psz_temp = playlist_ItemGetInfo( p_item ,_("Meta-information"),_("Artist") );
733 if( psz_temp == NULL )
737 o_value = [NSString stringWithUTF8String: psz_temp];
738 if( o_value == NULL )
740 o_value = [NSString stringWithCString: psz_temp];
745 else if( [[o_tc identifier] isEqualToString:@"3"] )
747 char psz_duration[MSTRTIME_MAX_SIZE];
748 mtime_t dur = p_item->input.i_duration;
751 secstotimestr( psz_duration, dur/1000000 );
752 o_value = [NSString stringWithUTF8String: psz_duration];
756 o_value = @"-:--:--";
760 vlc_object_release( p_playlist );
765 /* Required for drag & drop and reordering */
766 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
771 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
773 return NSDragOperationNone;
776 /* Delegate method of NSWindow */
777 - (void)windowWillClose:(NSNotification *)aNotification
779 [o_btn_playlist setState: NSOffState];