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 if( [[o_event characters] length] )
74 key = [[o_event characters] characterAtIndex: 0];
79 case NSDeleteCharacter:
80 case NSDeleteFunctionKey:
81 case NSDeleteCharFunctionKey:
82 case NSBackspaceCharacter:
83 [[self delegate] deleteItem:self];
87 [super keyDown: o_event];
94 /*****************************************************************************
95 * VLCPlaylist implementation
96 *****************************************************************************/
97 @implementation VLCPlaylist
111 [o_outline_view setTarget: self];
112 [o_outline_view setDelegate: self];
113 [o_outline_view setDataSource: self];
115 [o_outline_view setDoubleAction: @selector(playItem:)];
117 [o_outline_view registerForDraggedTypes:
118 [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
119 [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
121 /* We need to check whether _defaultTableHeaderSortImage exists, since it
122 belongs to an Apple hidden private API, and then can "disapear" at any time*/
124 if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderSortImage)] )
126 o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
130 o_ascendingSortingImage = nil;
133 if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderReverseSortImage)] )
135 o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
139 o_descendingSortingImage = nil;
142 o_outline_dict = [[NSMutableDictionary alloc] init];
145 //[self playlistUpdated];
150 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
151 [o_mi_play setTitle: _NS("Play")];
152 [o_mi_delete setTitle: _NS("Delete")];
153 [o_mi_selectall setTitle: _NS("Select All")];
154 [o_mi_info setTitle: _NS("Properties")];
155 [o_mi_sort_name setTitle: _NS("Sort Node by Name")];
156 [o_mi_sort_author setTitle: _NS("Sort Node by Author")];
157 [[o_tc_name headerCell] setStringValue:_NS("Name")];
158 [[o_tc_author headerCell] setStringValue:_NS("Author")];
159 [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
160 [o_status_field setStringValue: [NSString stringWithFormat:
161 _NS("0 items in playlist")]];
163 [o_random_ckb setTitle: _NS("Random")];
165 [o_search_button setTitle: _NS("Search")];
167 [o_btn_playlist setToolTip: _NS("Playlist")];
168 [[o_loop_popup itemAtIndex:0] setTitle: _NS("Standard Play")];
169 [[o_loop_popup itemAtIndex:1] setTitle: _NS("Repeat One")];
170 [[o_loop_popup itemAtIndex:2] setTitle: _NS("Repeat All")];
173 - (void)playlistUpdated
175 [o_outline_dict removeAllObjects];
176 [o_outline_view reloadData];
179 - (bool)isItem:(playlist_item_t *)p_item inNode:(playlist_item_t *)p_node
181 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
183 playlist_item_t * p_temp_item = p_item;
185 if ( p_playlist == NULL )
190 while ( p_temp_item->i_parents > 0 )
193 for (i = 0; i < p_temp_item->i_parents ; i++)
195 if (p_temp_item->pp_parents[i]->i_view == VIEW_SIMPLE)
197 if (p_temp_item->pp_parents[i]->p_parent == p_node)
199 vlc_object_release(p_playlist);
204 p_temp_item = p_temp_item->pp_parents[i]->p_parent;
211 vlc_object_release(p_playlist);
216 - (IBAction)playItem:(id)sender
218 intf_thread_t * p_intf = VLCIntf;
219 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
222 if( p_playlist != NULL )
224 playlist_item_t *p_item;
225 playlist_item_t *p_node = NULL;
228 p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
232 if (p_item->i_children == -1)
234 for (i = 0 ; i < p_item->i_parents ; i++)
236 if (p_item->pp_parents[i]->i_view == VIEW_SIMPLE)
238 p_node = p_item->pp_parents[i]->p_parent;
245 if (p_node->pp_children[0]->i_children == -1 &&
246 p_node->i_children > 0)
248 p_item = p_node->pp_children[0];
256 // p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
259 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VIEW_SIMPLE, p_node, p_item );
260 // playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VIEW_SIMPLE, p_view ? p_view->p_root : NULL, p_item );
262 vlc_object_release( p_playlist );
266 - (IBAction)selectAll:(id)sender
268 [o_outline_view selectAll: nil];
271 - (IBAction)deleteItem:(id)sender
274 NSMutableArray *o_to_delete;
277 playlist_t * p_playlist;
278 intf_thread_t * p_intf = VLCIntf;
280 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
283 if ( p_playlist == NULL )
287 o_to_delete = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
288 c = [o_to_delete count];
290 for( i = 0; i < c; i++ ) {
291 playlist_item_t * p_item;
292 o_number = [o_to_delete lastObject];
293 i_row = [o_number intValue];
295 [o_to_delete removeObject: o_number];
296 [o_outline_view deselectRow: i_row];
297 p_item = (playlist_item_t *)[[o_outline_view itemAtRow: i_row]pointerValue];
298 if (p_item->i_children > -1)
300 if (p_playlist->status.i_status)
302 if ([self isItem:p_playlist->status.p_item inNode: p_item]
303 == YES && p_playlist->status.i_status)
305 playlist_Stop( p_playlist );
308 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE);
312 if( p_playlist->status.p_item == [[o_outline_view itemAtRow: i_row]
313 pointerValue] && p_playlist->status.i_status )
315 playlist_Stop( p_playlist );
317 playlist_LockDelete( p_playlist, p_item->input.i_id );
319 [self playlistUpdated];
323 - (IBAction)sortNodeByName:(id)sender
325 [self sortNode: SORT_TITLE];
328 - (IBAction)sortNodeByAuthor:(id)sender
330 [self sortNode: SORT_AUTHOR];
333 - (void)sortNode:(int)i_mode
335 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
337 playlist_item_t * p_item;
339 if (p_playlist == NULL)
344 if ([o_outline_view selectedRow] > -1)
346 p_item = [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
350 /*If no item is selected, sort the whole playlist*/
352 playlist_view_t * p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
353 p_item = p_view->p_root;
356 if (p_item->i_children > -1)
358 vlc_mutex_lock(&p_playlist->object_lock );
359 playlist_RecursiveNodeSort( p_playlist, p_item, i_mode, ORDER_NORMAL );
360 vlc_mutex_unlock(&p_playlist->object_lock );
366 for (i = 0 ; i < p_item->i_parents ; i++)
368 if (p_item->pp_parents[i]->i_view == VIEW_SIMPLE)
370 vlc_mutex_lock(&p_playlist->object_lock );
371 playlist_RecursiveNodeSort( p_playlist,
372 p_item->pp_parents[i]->p_parent, i_mode, ORDER_NORMAL );
373 vlc_mutex_unlock(&p_playlist->object_lock );
378 vlc_object_release(p_playlist);
379 [self playlistUpdated];
382 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
385 intf_thread_t * p_intf = VLCIntf;
386 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
389 if( p_playlist == NULL )
394 for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
397 NSDictionary *o_one_item;
398 int j, i_total_options = 0, i_new_id = -1;
399 int i_mode = PLAYLIST_INSERT;
400 BOOL b_rem = FALSE, b_dir = FALSE;
401 NSString *o_uri, *o_name;
404 char **ppsz_options = NULL;
407 o_one_item = [o_array objectAtIndex: i_item];
408 o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
409 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
410 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
412 /* If no name, then make a guess */
413 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
415 if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
416 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
417 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
419 /* All of this is to make sure CD's play when you D&D them on VLC */
420 /* Converts mountpoint to a /dev file */
423 buf = (struct statfs *) malloc (sizeof(struct statfs));
424 statfs( [o_uri fileSystemRepresentation], buf );
425 psz_dev = strdup(buf->f_mntfromname);
426 o_uri = [NSString stringWithCString: psz_dev ];
429 if( o_options && [o_options count] > 0 )
431 /* Count the input options */
432 i_total_options = [o_options count];
434 /* Allocate ppsz_options */
435 for( j = 0; j < i_total_options; j++ )
438 ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
440 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
445 i_new_id = playlist_AddExt( p_playlist, [o_uri fileSystemRepresentation],
446 [o_name UTF8String], i_mode,
447 i_position == -1 ? PLAYLIST_END : i_position + i_item,
448 0, (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options );
451 for( j = 0; j < i_total_options; j++ )
452 free( ppsz_options[j] );
453 if( ppsz_options ) free( ppsz_options ); */
455 /* Recent documents menu */
456 o_true_file = [NSURL fileURLWithPath: o_uri];
457 if( o_true_file != nil )
459 [[NSDocumentController sharedDocumentController]
460 noteNewRecentDocumentURL: o_true_file];
463 if( i_item == 0 && !b_enqueue )
465 playlist_Goto( p_playlist, playlist_GetPositionById( p_playlist, i_new_id ) );
466 playlist_Play( p_playlist );
470 vlc_object_release( p_playlist );
473 - (IBAction)handlePopUp:(id)sender
476 intf_thread_t * p_intf = VLCIntf;
477 vlc_value_t val1,val2;
478 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
480 if( p_playlist == NULL )
485 switch ([o_loop_popup indexOfSelectedItem])
490 var_Set( p_playlist, "loop", val1 );
492 var_Set( p_playlist, "repeat", val1 );
493 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat One" ) );
498 var_Set( p_playlist, "repeat", val1 );
500 var_Set( p_playlist, "loop", val1 );
501 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat All" ) );
505 var_Get( p_playlist, "repeat", &val1 );
506 var_Get( p_playlist, "loop", &val2 );
507 if (val1.b_bool || val2.b_bool)
510 var_Set( p_playlist, "repeat", val1 );
511 var_Set( p_playlist, "loop", val1 );
512 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
516 vlc_object_release( p_playlist );
517 [self playlistUpdated];
520 - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
522 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
524 playlist_item_t * p_selected_item;
525 int i_current, i_selected_row;
530 i_selected_row = [o_outline_view selectedRow];
531 if (i_selected_row < 0)
534 p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow:
535 i_selected_row] pointerValue];
537 for (i_current = 0; i_current < p_item->i_children ; i_current++)
540 NSString * o_current_name, * o_current_author;
542 vlc_mutex_lock( &p_playlist->object_lock );
543 o_current_name = [NSString stringWithUTF8String:
544 p_item->pp_children[i_current]->input.psz_name];
545 psz_temp = playlist_ItemGetInfo(p_item ,_("Meta-information"),_("Author") );
546 o_current_author = [NSString stringWithUTF8String: psz_temp];
548 vlc_mutex_unlock( &p_playlist->object_lock );
550 if (p_selected_item == p_item->pp_children[i_current] &&
551 b_selected_item_met == NO)
553 b_selected_item_met = YES;
555 else if (p_selected_item == p_item->pp_children[i_current] &&
556 b_selected_item_met == YES)
558 vlc_object_release(p_playlist);
561 else if (b_selected_item_met == YES &&
562 ([o_current_name rangeOfString:[o_search_field
563 stringValue] options:NSCaseInsensitiveSearch ].length ||
564 [o_current_author rangeOfString:[o_search_field
565 stringValue] options:NSCaseInsensitiveSearch ].length))
567 vlc_object_release(p_playlist);
568 /*Adds the parent items in the result array as well, so that we can
570 return [NSMutableArray arrayWithObject: [NSValue
571 valueWithPointer: p_item->pp_children[i_current]]];
573 if (p_item->pp_children[i_current]->i_children > 0)
575 id o_result = [self subSearchItem:
576 p_item->pp_children[i_current]];
577 if (o_result != NULL)
579 vlc_object_release(p_playlist);
580 [o_result insertObject: [NSValue valueWithPointer:
581 p_item->pp_children[i_current]] atIndex:0];
586 vlc_object_release(p_playlist);
590 - (IBAction)searchItem:(id)sender
592 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
594 playlist_view_t * p_view;
600 b_selected_item_met = NO;
602 if( p_playlist == NULL )
605 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
609 /*First, only search after the selected item:*
610 *(b_selected_item_met = NO) */
611 o_result = [self subSearchItem:p_view->p_root];
612 if (o_result == NULL)
614 /* If the first search failed, search again from the beginning */
615 o_result = [self subSearchItem:p_view->p_root];
617 if (o_result != NULL)
619 for (i = 0 ; i < [o_result count] - 1 ; i++)
621 [o_outline_view expandItem: [o_outline_dict objectForKey:
622 [NSString stringWithFormat: @"%p",
623 [[o_result objectAtIndex: i] pointerValue]]]];
625 i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
626 [NSString stringWithFormat: @"%p",
627 [[o_result objectAtIndex: [o_result count] - 1 ]
632 [o_outline_view selectRow:i_row byExtendingSelection: NO];
633 [o_outline_view scrollRowToVisible: i_row];
636 vlc_object_release(p_playlist);
640 - (NSMenu *)menuForEvent:(NSEvent *)o_event
644 vlc_bool_t b_item_sel;
646 pt = [o_outline_view convertPoint: [o_event locationInWindow]
648 b_item_sel = ( [o_outline_view rowAtPoint: pt] != -1 &&
649 [o_outline_view selectedRow] != -1 );
650 b_rows = [o_outline_view numberOfRows] != 0;
652 [o_mi_play setEnabled: b_item_sel];
653 [o_mi_delete setEnabled: b_item_sel];
654 [o_mi_selectall setEnabled: b_rows];
655 [o_mi_info setEnabled: b_item_sel];
657 return( o_ctx_menu );
660 - (playlist_item_t *)selectedPlaylistItem
662 return [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
668 @implementation VLCPlaylist (NSOutlineViewDataSource)
670 /* return the number of children for Obj-C pointer item */ /* DONE */
671 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
674 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
676 if( p_playlist == NULL )
682 playlist_view_t *p_view;
683 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
684 if( p_view && p_view->p_root )
685 i_return = p_view->p_root->i_children;
689 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
691 i_return = p_item->i_children;
693 vlc_object_release( p_playlist );
694 if( i_return == -1 ) i_return = 0;
695 msg_Dbg( p_playlist, "I have %d children", i_return );
699 /* return the child at index for the Obj-C pointer item */ /* DONE */
700 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
702 playlist_item_t *p_return = NULL;
703 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
707 if( p_playlist == NULL )
713 playlist_view_t *p_view;
714 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
715 if( p_view && index < p_view->p_root->i_children )
716 p_return = p_view->p_root->pp_children[index];
720 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
721 if( p_item && index < p_item->i_children )
723 p_return = p_item->pp_children[index];
727 [o_status_field setStringValue: [NSString stringWithFormat:
728 _NS("%i items in playlist"), p_playlist->i_size]];
730 vlc_object_release( p_playlist );
731 msg_Dbg( p_playlist, "childitem with index %d", index );
733 o_value = [NSValue valueWithPointer: p_return];
735 [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p", p_return]];
739 /* is the item expandable */
740 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
743 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
745 if( p_playlist == NULL )
751 playlist_view_t *p_view;
752 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
753 if( p_view && p_view->p_root )
754 i_return = p_view->p_root->i_children;
758 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
760 i_return = p_item->i_children;
762 vlc_object_release( p_playlist );
764 if( i_return == -1 || i_return == 0 )
770 /* retrieve the string values for the cells */
771 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
774 intf_thread_t * p_intf = VLCIntf;
775 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
777 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
779 if( p_playlist == NULL || p_item == NULL )
784 if( [[o_tc identifier] isEqualToString:@"1"] )
786 o_value = [NSString stringWithUTF8String:
787 p_item->input.psz_name];
788 if( o_value == NULL )
789 o_value = [NSString stringWithCString:
790 p_item->input.psz_name];
792 else if( [[o_tc identifier] isEqualToString:@"2"] )
795 psz_temp = playlist_ItemGetInfo( p_item ,_("Meta-information"),_("Artist") );
797 if( psz_temp == NULL )
801 o_value = [NSString stringWithUTF8String: psz_temp];
802 if( o_value == NULL )
804 o_value = [NSString stringWithCString: psz_temp];
809 else if( [[o_tc identifier] isEqualToString:@"3"] )
811 char psz_duration[MSTRTIME_MAX_SIZE];
812 mtime_t dur = p_item->input.i_duration;
815 secstotimestr( psz_duration, dur/1000000 );
816 o_value = [NSString stringWithUTF8String: psz_duration];
820 o_value = @"-:--:--";
824 vlc_object_release( p_playlist );
829 /* Required for drag & drop and reordering */
830 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
835 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
837 return NSDragOperationNone;
840 /* Delegate method of NSWindow */
841 - (void)windowWillClose:(NSNotification *)aNotification
843 [o_btn_playlist setState: NSOffState];