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 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <stdlib.h> /* malloc(), free() */
30 #include <sys/param.h> /* for MAXPATHLEN */
33 #include <sys/mount.h>
41 /*****************************************************************************
42 * VLCPlaylistView implementation
43 *****************************************************************************/
44 @implementation VLCPlaylistView
46 - (NSMenu *)menuForEvent:(NSEvent *)o_event
48 return( [[self delegate] menuForEvent: o_event] );
51 - (void)keyDown:(NSEvent *)o_event
55 NSMutableArray *o_to_delete;
58 playlist_t * p_playlist;
59 intf_thread_t * p_intf = [NSApp getIntf];
61 if( [[o_event characters] length] )
63 key = [[o_event characters] characterAtIndex: 0];
66 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
69 if ( p_playlist == NULL )
76 case NSDeleteCharacter:
77 case NSDeleteFunctionKey:
78 case NSDeleteCharFunctionKey:
79 case NSBackspaceCharacter:
80 o_to_delete = [NSMutableArray arrayWithArray:[[self selectedRowEnumerator] allObjects]];
81 c = [o_to_delete count];
83 for( i = 0; i < c; i++ ) {
84 o_number = [o_to_delete lastObject];
85 i_row = [o_number intValue];
87 if( p_playlist->i_index == i_row && p_playlist->i_status )
89 playlist_Stop( p_playlist );
91 [o_to_delete removeObject: o_number];
92 [self deselectRow: i_row];
93 playlist_Delete( p_playlist, i_row );
99 [super keyDown: o_event];
103 if( p_playlist != NULL )
105 vlc_object_release( p_playlist );
112 /*****************************************************************************
113 * VLCPlaylist implementation
114 *****************************************************************************/
115 @implementation VLCPlaylist
129 [o_table_view setTarget: self];
130 [o_table_view setDelegate: self];
131 [o_table_view setDataSource: self];
133 [o_table_view setDoubleAction: @selector(playItem:)];
135 [o_table_view registerForDraggedTypes:
136 [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
137 [o_table_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
138 [o_window setExcludedFromWindowsMenu: TRUE];
140 // [o_tbv_info setDataSource: [VLCInfoDataSource init]];
142 /* We need to check whether _defaultTableHeaderSortImage exists, since it
143 belongs to an Apple hidden private API, and then can "disapear" at any time*/
145 if( [[NSTableView class] respondsToSelector:@selector(_defaultTableHeaderSortImage)] )
147 o_ascendingSortingImage = [[NSTableView class] _defaultTableHeaderSortImage];
151 o_ascendingSortingImage = nil;
154 if( [[NSTableView class] respondsToSelector:@selector(_defaultTableHeaderReverseSortImage)] )
156 o_descendingSortingImage = [[NSTableView class] _defaultTableHeaderReverseSortImage];
160 o_descendingSortingImage = nil;
164 [self playlistUpdated];
169 [o_window setTitle: _NS("Playlist")];
170 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
171 [o_mi_play setTitle: _NS("Play")];
172 [o_mi_delete setTitle: _NS("Delete")];
173 [o_mi_selectall setTitle: _NS("Select All")];
174 [o_mi_info setTitle: _NS("Proprieties")];
176 [[o_tc_name headerCell] setStringValue:_NS("Name")];
177 [[o_tc_author headerCell] setStringValue:_NS("Author")];
178 [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
179 [o_random_ckb setTitle: _NS("Random")];
180 [o_search_button setTitle: _NS("Search")];
181 [o_btn_playlist setToolTip: _NS("Playlist")];
182 [[o_loop_popup itemAtIndex:0] setTitle: _NS("Standard Play")];
183 [[o_loop_popup itemAtIndex:1] setTitle: _NS("Repeat One")];
184 [[o_loop_popup itemAtIndex:2] setTitle: _NS("Repeat All")];
187 - (void) tableView:(NSTableView*)o_tv
188 didClickTableColumn:(NSTableColumn *)o_tc
190 intf_thread_t * p_intf = [NSApp getIntf];
191 playlist_t *p_playlist =
192 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
195 int max = [[o_table_view tableColumns] count];
198 if( p_playlist == NULL )
203 if( o_tc_sortColumn == o_tc )
205 b_isSortDescending = !b_isSortDescending;
207 else if( o_tc == o_tc_name || o_tc == o_tc_author ||
210 b_isSortDescending = VLC_FALSE;
211 [o_table_view setHighlightedTableColumn:o_tc];
212 o_tc_sortColumn = o_tc;
213 for( i=0 ; i<max ; i++ )
215 [o_table_view setIndicatorImage:nil inTableColumn:[[o_table_view tableColumns] objectAtIndex:i]];
219 if( o_tc_id == o_tc && !b_isSortDescending )
221 playlist_SortID( p_playlist , ORDER_NORMAL );
222 [o_table_view setIndicatorImage:o_ascendingSortingImage inTableColumn:o_tc];
224 else if( o_tc_name == o_tc && !b_isSortDescending )
226 playlist_SortTitle( p_playlist , ORDER_NORMAL );
227 [o_table_view setIndicatorImage:o_ascendingSortingImage inTableColumn:o_tc];
229 else if( o_tc_author == o_tc && !b_isSortDescending )
231 playlist_SortAuthor( p_playlist , ORDER_NORMAL );
232 [o_table_view setIndicatorImage:o_ascendingSortingImage inTableColumn:o_tc];
234 else if( o_tc_id == o_tc && b_isSortDescending )
236 playlist_SortID( p_playlist , ORDER_REVERSE );
237 [o_table_view setIndicatorImage:o_ascendingSortingImage inTableColumn:o_tc];
239 else if( o_tc_name == o_tc && b_isSortDescending )
241 playlist_SortTitle( p_playlist , ORDER_REVERSE );
242 [o_table_view setIndicatorImage:o_descendingSortingImage inTableColumn:o_tc];
244 else if( o_tc_author == o_tc && b_isSortDescending )
246 playlist_SortAuthor( p_playlist , ORDER_REVERSE );
247 [o_table_view setIndicatorImage:o_descendingSortingImage inTableColumn:o_tc];
249 vlc_object_release( p_playlist );
250 [self playlistUpdated];
254 - (BOOL)tableView:(NSTableView *)o_tv
255 shouldEditTableColumn:(NSTableColumn *)o_tc
261 - (NSMenu *)menuForEvent:(NSEvent *)o_event
265 vlc_bool_t b_item_sel;
267 pt = [o_table_view convertPoint: [o_event locationInWindow]
269 b_item_sel = ( [o_table_view rowAtPoint: pt] != -1 &&
270 [o_table_view selectedRow] != -1 );
271 b_rows = [o_table_view numberOfRows] != 0;
273 [o_mi_play setEnabled: b_item_sel];
274 [o_mi_delete setEnabled: b_item_sel];
275 [o_mi_selectall setEnabled: b_rows];
276 [o_mi_info setEnabled: b_item_sel];
278 return( o_ctx_menu );
281 - (IBAction)toggleWindow:(id)sender
283 if( [o_window isVisible] )
285 [o_window orderOut:sender];
286 [o_btn_playlist setState:NSOffState];
290 [o_window makeKeyAndOrderFront:sender];
291 [o_btn_playlist setState:NSOnState];
295 - (IBAction)savePlaylist:(id)sender
297 intf_thread_t * p_intf = [NSApp getIntf];
298 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
301 NSSavePanel *o_save_panel = [NSSavePanel savePanel];
302 NSString * o_name = [NSString stringWithFormat: @"%@.m3u", _NS("Untitled")];
303 [o_save_panel setTitle: _NS("Save Playlist")];
304 [o_save_panel setPrompt: _NS("Save")];
306 if( [o_save_panel runModalForDirectory: nil
307 file: o_name] == NSOKButton )
309 playlist_Export( p_playlist, [[o_save_panel filename] fileSystemRepresentation], "export-m3u" );
314 - (IBAction)playItem:(id)sender
316 intf_thread_t * p_intf = [NSApp getIntf];
317 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
320 if( p_playlist != NULL )
322 playlist_Goto( p_playlist, [o_table_view selectedRow] );
323 vlc_object_release( p_playlist );
327 - (IBAction)deleteItems:(id)sender
330 NSMutableArray *o_to_delete;
333 intf_thread_t * p_intf = [NSApp getIntf];
334 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
337 if( p_playlist == NULL )
342 o_to_delete = [NSMutableArray arrayWithArray:[[o_table_view selectedRowEnumerator] allObjects]];
343 c = (int)[o_to_delete count];
345 for( i = 0; i < c; i++ ) {
346 o_number = [o_to_delete lastObject];
347 i_row = [o_number intValue];
349 if( p_playlist->i_index == i_row && p_playlist->i_status )
351 playlist_Stop( p_playlist );
353 [o_to_delete removeObject: o_number];
354 [o_table_view deselectRow: i_row];
355 playlist_Delete( p_playlist, i_row );
358 vlc_object_release( p_playlist );
360 /* this is actually duplicity, because the intf.m manage also updates the view
361 * when the playlist changes. we do this on purpose, because else there is a
362 * delay of .5 sec or so when we delete an item */
363 [self playlistUpdated];
364 [self updateRowSelection];
367 - (IBAction)selectAll:(id)sender
369 [o_table_view selectAll: nil];
373 - (IBAction)searchItem:(id)sender
376 NSString *o_current_name;
377 NSString *o_current_author;
379 intf_thread_t * p_intf = [NSApp getIntf];
380 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
383 if( p_playlist == NULL )
387 if( [o_table_view numberOfRows] < 1 )
392 if( [o_table_view selectedRow] == [o_table_view numberOfRows]-1 )
398 i_current = [o_table_view selectedRow];
405 vlc_mutex_lock( &p_playlist->object_lock );
406 o_current_name = [NSString stringWithUTF8String:
407 p_playlist->pp_items[i_current]->psz_name];
408 o_current_author = [NSString stringWithUTF8String:
409 playlist_GetInfo(p_playlist, i_current ,_("General"),_("Author") )];
410 vlc_mutex_unlock( &p_playlist->object_lock );
413 if( [o_current_name rangeOfString:[o_search_keyword stringValue] options:NSCaseInsensitiveSearch ].length ||
414 [o_current_author rangeOfString:[o_search_keyword stringValue] options:NSCaseInsensitiveSearch ].length )
416 [o_table_view selectRow: i_current byExtendingSelection: NO];
417 [o_table_view scrollRowToVisible: i_current];
420 if( i_current == [o_table_view numberOfRows] - 1 )
425 while (i_current != [o_table_view selectedRow]);
426 vlc_object_release( p_playlist );
430 - (IBAction)handlePopUp:(id)sender
433 intf_thread_t * p_intf = [NSApp getIntf];
434 vlc_value_t val1,val2;
435 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
437 if( p_playlist == NULL )
442 switch ([o_loop_popup indexOfSelectedItem])
447 var_Set( p_playlist, "loop", val1 );
449 var_Set( p_playlist, "repeat", val1 );
450 vout_OSDMessage( p_intf, _( "Repeat One" ) );
455 var_Set( p_playlist, "repeat", val1 );
457 var_Set( p_playlist, "loop", val1 );
458 vout_OSDMessage( p_intf, _( "Repeat All" ) );
462 var_Get( p_playlist, "repeat", &val1 );
463 var_Get( p_playlist, "loop", &val2 );
464 if (val1.b_bool || val2.b_bool)
467 var_Set( p_playlist, "repeat", val1 );
468 var_Set( p_playlist, "loop", val1 );
469 vout_OSDMessage( p_intf, _( "Repeat Off" ) );
473 vlc_object_release( p_playlist );
474 [self playlistUpdated];
478 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
481 intf_thread_t * p_intf = [NSApp getIntf];
482 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
485 if( p_playlist == NULL )
490 for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
493 NSDictionary *o_one_item;
494 int j, i_total_options = 0, i_new_id = -1;
495 int i_mode = PLAYLIST_INSERT;
496 BOOL b_rem = FALSE, b_dir = FALSE;
497 NSString *o_uri, *o_name;
500 char **ppsz_options = NULL;
503 o_one_item = [o_array objectAtIndex: i_item];
504 o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
505 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
506 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
508 /* If no name, then make a guess */
509 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
511 if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
512 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
513 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
515 /* All of this is to make sure CD's play when you D&D them on VLC */
516 /* Converts mountpoint to a /dev file */
519 buf = (struct statfs *) malloc (sizeof(struct statfs));
520 statfs( [o_uri fileSystemRepresentation], buf );
521 psz_dev = strdup(buf->f_mntfromname);
522 o_uri = [NSString stringWithCString: psz_dev ];
525 if( o_options && [o_options count] > 0 )
527 /* Count the input options */
528 i_total_options = [o_options count];
530 /* Allocate ppsz_options */
531 for( j = 0; j < i_total_options; j++ )
534 ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
536 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
541 i_new_id = playlist_AddExt( p_playlist, [o_uri fileSystemRepresentation],
542 [o_name UTF8String], i_mode,
543 i_position == -1 ? PLAYLIST_END : i_position + i_item,
544 0, (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options );
547 for( j = 0; j < i_total_options; j++ )
548 free( ppsz_options[j] );
549 if( ppsz_options ) free( ppsz_options ); */
551 /* Recent documents menu */
552 o_true_file = [NSURL fileURLWithPath: o_uri];
553 if( o_true_file != nil )
555 [[NSDocumentController sharedDocumentController]
556 noteNewRecentDocumentURL: o_true_file];
559 if( i_item == 0 && !b_enqueue )
561 playlist_Goto( p_playlist, playlist_GetPositionById( p_playlist, i_new_id ) );
562 playlist_Play( p_playlist );
566 vlc_object_release( p_playlist );
569 - (void)playlistUpdated
571 vlc_value_t val1, val2;
572 intf_thread_t * p_intf = [NSApp getIntf];
573 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
575 if( p_playlist != NULL )
577 var_Get( p_playlist, "random", &val1 );
578 [o_random_ckb setState: val1.b_bool];
580 var_Get( p_playlist, "repeat", &val1 );
581 var_Get( p_playlist, "loop", &val2 );
584 [o_loop_popup selectItemAtIndex:1];
588 [o_loop_popup selectItemAtIndex:2];
592 [o_loop_popup selectItemAtIndex:0];
594 vlc_object_release( p_playlist );
596 [o_table_view reloadData];
599 - (void)updateRowSelection
603 intf_thread_t * p_intf = [NSApp getIntf];
604 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
607 if( p_playlist == NULL )
612 i_row = p_playlist->i_index;
613 vlc_object_release( p_playlist );
615 [o_table_view selectRow: i_row byExtendingSelection: NO];
616 [o_table_view scrollRowToVisible: i_row];
619 - (int)selectedPlaylistItem
621 return [o_table_view selectedRow];
626 @implementation VLCPlaylist (NSTableDataSource)
628 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
631 intf_thread_t * p_intf = [NSApp getIntf];
632 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
635 if( p_playlist != NULL )
637 vlc_mutex_lock( &p_playlist->object_lock );
638 i_count = p_playlist->i_size;
639 vlc_mutex_unlock( &p_playlist->object_lock );
640 vlc_object_release( p_playlist );
642 [o_status_field setStringValue: [NSString stringWithFormat:_NS("%i items in playlist"), i_count]];
646 - (id)tableView:(NSTableView *)o_tv
647 objectValueForTableColumn:(NSTableColumn *)o_tc
651 intf_thread_t * p_intf = [NSApp getIntf];
652 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
655 if( p_playlist == NULL )
660 if( [[o_tc identifier] isEqualToString:@"0"] )
662 o_value = [NSString stringWithFormat:@"%i", i_row + 1];
664 else if( [[o_tc identifier] isEqualToString:@"1"] )
666 vlc_mutex_lock( &p_playlist->object_lock );
667 o_value = [NSString stringWithUTF8String:
668 p_playlist->pp_items[i_row]->psz_name];
669 vlc_mutex_unlock( &p_playlist->object_lock );
671 else if( [[o_tc identifier] isEqualToString:@"2"] )
673 vlc_mutex_lock( &p_playlist->object_lock );
674 o_value = [NSString stringWithUTF8String:
675 playlist_GetInfo(p_playlist, i_row ,_("General"),_("Author") )];
676 vlc_mutex_unlock( &p_playlist->object_lock );
678 else if( [[o_tc identifier] isEqualToString:@"3"] )
680 char psz_duration[MSTRTIME_MAX_SIZE];
681 mtime_t dur = p_playlist->pp_items[i_row]->i_duration;
684 secstotimestr( psz_duration, dur/1000000 );
685 o_value = [NSString stringWithUTF8String: psz_duration];
689 o_value = @"-:--:--";
693 vlc_object_release( p_playlist );
698 - (void)tableView:(NSTableView *)o_tv
699 willDisplayCell:(id)o_cell
700 forTableColumn:(NSTableColumn *)o_tc
703 intf_thread_t * p_intf = [NSApp getIntf];
704 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
706 if ((p_playlist->i_groups) > 1 )
708 [o_cell setDrawsBackground: VLC_TRUE];
709 switch ( p_playlist->pp_items[o_rows]->i_group % 8 )
713 [o_cell setBackgroundColor: [NSColor colorWithDeviceRed:1.0 green:1.0 blue:1.0 alpha:1.0]];
718 [o_cell setBackgroundColor: [NSColor colorWithDeviceRed:1.0 green:0.76471 blue:0.76471 alpha:1.0]];
723 [o_cell setBackgroundColor: [NSColor colorWithDeviceRed:0.76471 green:0.76471 blue:1.0 alpha:1.0]];
728 [o_cell setBackgroundColor: [NSColor colorWithDeviceRed:1.0 green:0.89804 blue:0.76471 alpha:1.0]];
733 [o_cell setBackgroundColor: [NSColor colorWithDeviceRed:1.0 green:0.76471 blue:1.0 alpha:1.0]];
738 [o_cell setBackgroundColor: [NSColor colorWithDeviceRed:0.76471 green:1.0 blue:0.76471 alpha:1.0]];
743 [o_cell setBackgroundColor: [NSColor colorWithDeviceRed:0.76471 green:1.0 blue:1.0 alpha:1.0]];
748 [o_cell setBackgroundColor: [NSColor colorWithDeviceRed:1.0 green:1.0 blue:0.76471 alpha:1.0]];
753 vlc_object_release( p_playlist );
756 - (BOOL)tableView:(NSTableView *)o_tv
757 writeRows:(NSArray*)o_rows
758 toPasteboard:(NSPasteboard*)o_pasteboard
760 int i_rows = [o_rows count];
761 NSArray *o_filenames = [NSArray array];
763 [o_pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
764 [o_pasteboard setPropertyList:o_filenames forType:NSFilenamesPboardType];
767 i_moveRow = [[o_rows objectAtIndex:0]intValue];
773 - (NSDragOperation)tableView:(NSTableView*)o_tv
774 validateDrop:(id <NSDraggingInfo>)o_info
775 proposedRow:(int)i_row
776 proposedDropOperation:(NSTableViewDropOperation)o_operation
778 if ( o_operation == NSTableViewDropAbove )
780 if ( i_moveRow >= 0 )
782 if ( i_row != i_moveRow )
784 return NSDragOperationMove;
786 /* what if in the previous run, the row wasn't actually moved?
787 then we can't drop new files on this location */
788 return NSDragOperationNone;
790 return NSDragOperationGeneric;
792 return NSDragOperationNone;
795 - (BOOL)tableView:(NSTableView*)o_tv
796 acceptDrop:(id <NSDraggingInfo>)o_info
797 row:(int)i_proposed_row
798 dropOperation:(NSTableViewDropOperation)o_operation
800 if ( i_moveRow >= 0 )
802 if (i_moveRow != -1 && i_proposed_row != -1)
804 intf_thread_t * p_intf = [NSApp getIntf];
805 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
808 if( p_playlist == NULL )
814 playlist_Move( p_playlist, i_moveRow, i_proposed_row );
816 vlc_object_release( p_playlist );
818 [self playlistUpdated];
824 NSPasteboard * o_pasteboard;
825 o_pasteboard = [o_info draggingPasteboard];
827 if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
830 NSArray *o_array = [NSArray array];
831 NSArray *o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
832 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
834 for( i = 0; i < (int)[o_values count]; i++)
837 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
838 o_array = [o_array arrayByAddingObject: o_dic];
840 [self appendArray: o_array atPos: i_proposed_row enqueue:YES];
845 [self updateRowSelection];
848 /* Delegate method of NSWindow */
849 - (void)windowWillClose:(NSNotification *)aNotification
851 [o_btn_playlist setState: NSOffState];