1 /*****************************************************************************
2 * playlist.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2002-2004 VideoLAN
5 * $Id: playlist.m,v 1.56 2004/02/06 04:51:02 hartman Exp $
7 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8 * Derk-Jan Hartman <hartman at videolan dot org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
29 #include <sys/param.h> /* for MAXPATHLEN */
32 #include <sys/mount.h>
39 /*****************************************************************************
40 * VLCPlaylistView implementation
41 *****************************************************************************/
42 @implementation VLCPlaylistView
44 - (NSMenu *)menuForEvent:(NSEvent *)o_event
46 return( [[self delegate] menuForEvent: o_event] );
49 - (void)keyDown:(NSEvent *)o_event
53 NSMutableArray *o_to_delete;
56 playlist_t * p_playlist;
57 intf_thread_t * p_intf = [NSApp getIntf];
59 if( [[o_event characters] length] )
61 key = [[o_event characters] characterAtIndex: 0];
64 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
67 if ( p_playlist == NULL )
74 case NSDeleteCharacter:
75 case NSDeleteFunctionKey:
76 case NSDeleteCharFunctionKey:
77 case NSBackspaceCharacter:
78 o_to_delete = [NSMutableArray arrayWithArray:[[self selectedRowEnumerator] allObjects]];
79 c = [o_to_delete count];
81 for( i = 0; i < c; i++ ) {
82 o_number = [o_to_delete lastObject];
83 i_row = [o_number intValue];
85 if( p_playlist->i_index == i_row && p_playlist->i_status )
87 playlist_Stop( p_playlist );
89 [o_to_delete removeObject: o_number];
90 [self deselectRow: i_row];
91 playlist_Delete( p_playlist, i_row );
97 [super keyDown: o_event];
101 if( p_playlist != NULL )
103 vlc_object_release( p_playlist );
110 /*****************************************************************************
111 * VLCPlaylist implementation
112 *****************************************************************************/
113 @implementation VLCPlaylist
127 [o_table_view setTarget: self];
128 [o_table_view setDelegate: self];
129 [o_table_view setDataSource: self];
131 [o_table_view setDoubleAction: @selector(playItem:)];
133 [o_table_view registerForDraggedTypes:
134 [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
136 [o_window setExcludedFromWindowsMenu: TRUE];
138 /* We need to check whether _defaultTableHeaderSortImage exists, since it
139 belongs to an Apple hidden private API, and then can "disapear" at any time*/
141 if( [[NSTableView class] respondsToSelector:@selector(_defaultTableHeaderSortImage)] )
143 o_ascendingSortingImage = [[NSTableView class] _defaultTableHeaderSortImage];
147 o_ascendingSortingImage = nil;
150 if( [[NSTableView class] respondsToSelector:@selector(_defaultTableHeaderReverseSortImage)] )
152 o_descendingSortingImage = [[NSTableView class] _defaultTableHeaderReverseSortImage];
156 o_descendingSortingImage = nil;
164 [o_window setTitle: _NS("Playlist")];
165 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
166 [o_mi_play setTitle: _NS("Play")];
167 [o_mi_delete setTitle: _NS("Delete")];
168 [o_mi_selectall setTitle: _NS("Select All")];
169 [[o_tc_name headerCell] setStringValue:_NS("Name")];
170 [[o_tc_author headerCell] setStringValue:_NS("Author")];
171 [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
172 [o_random_ckb setTitle: _NS("Random")];
173 [o_loop_ckb setTitle: _NS("Repeat All")];
174 [o_repeat_ckb setTitle: _NS("Repeat One")];
175 [o_search_button setTitle: _NS("Search")];
176 [o_btn_playlist setToolTip: _NS("Playlist")];
179 - (void) tableView:(NSTableView*)o_tv
180 didClickTableColumn:(NSTableColumn *)o_tc
182 intf_thread_t * p_intf = [NSApp getIntf];
183 playlist_t *p_playlist =
184 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
187 int max = [[o_table_view tableColumns] count];
190 if( p_playlist == NULL )
195 if( o_tc_sortColumn == o_tc )
197 b_isSortDescending = !b_isSortDescending;
199 else if( o_tc == o_tc_name || o_tc == o_tc_author ||
202 b_isSortDescending = VLC_FALSE;
203 [o_table_view setHighlightedTableColumn:o_tc];
204 o_tc_sortColumn = o_tc;
205 for( i=0 ; i<max ; i++ )
207 [o_table_view setIndicatorImage:nil inTableColumn:[[o_table_view tableColumns] objectAtIndex:i]];
211 if( o_tc_id == o_tc && !b_isSortDescending )
213 playlist_SortID( p_playlist , ORDER_NORMAL );
214 [o_table_view setIndicatorImage:o_ascendingSortingImage inTableColumn:o_tc];
216 else if( o_tc_name == o_tc && !b_isSortDescending )
218 playlist_SortTitle( p_playlist , ORDER_NORMAL );
219 [o_table_view setIndicatorImage:o_ascendingSortingImage inTableColumn:o_tc];
221 else if( o_tc_author == o_tc && !b_isSortDescending )
223 playlist_SortAuthor( p_playlist , ORDER_NORMAL );
224 [o_table_view setIndicatorImage:o_ascendingSortingImage inTableColumn:o_tc];
226 else if( o_tc_id == o_tc && b_isSortDescending )
228 playlist_SortID( p_playlist , ORDER_REVERSE );
229 [o_table_view setIndicatorImage:o_ascendingSortingImage inTableColumn:o_tc];
231 else if( o_tc_name == o_tc && b_isSortDescending )
233 playlist_SortTitle( p_playlist , ORDER_REVERSE );
234 [o_table_view setIndicatorImage:o_descendingSortingImage inTableColumn:o_tc];
236 else if( o_tc_author == o_tc && b_isSortDescending )
238 playlist_SortAuthor( p_playlist , ORDER_REVERSE );
239 [o_table_view setIndicatorImage:o_descendingSortingImage inTableColumn:o_tc];
241 vlc_object_release( p_playlist );
242 [self playlistUpdated];
246 - (BOOL)tableView:(NSTableView *)o_tv
247 shouldEditTableColumn:(NSTableColumn *)o_tc
253 - (NSMenu *)menuForEvent:(NSEvent *)o_event
257 vlc_bool_t b_item_sel;
259 pt = [o_table_view convertPoint: [o_event locationInWindow]
261 b_item_sel = ( [o_table_view rowAtPoint: pt] != -1 &&
262 [o_table_view selectedRow] != -1 );
263 b_rows = [o_table_view numberOfRows] != 0;
265 [o_mi_play setEnabled: b_item_sel];
266 [o_mi_delete setEnabled: b_item_sel];
267 [o_mi_selectall setEnabled: b_rows];
269 return( o_ctx_menu );
272 - (IBAction)toggleWindow:(id)sender
274 if( [o_window isVisible] )
276 [o_window orderOut:sender];
277 [o_btn_playlist setState:NSOffState];
281 [o_window makeKeyAndOrderFront:sender];
282 [o_btn_playlist setState:NSOnState];
286 - (IBAction)savePlaylist:(id)sender
288 intf_thread_t * p_intf = [NSApp getIntf];
289 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
292 NSSavePanel *o_save_panel = [NSSavePanel savePanel];
293 NSString * o_name = [NSString stringWithFormat: @"%@.m3u", _NS("Untitled")];
294 [o_save_panel setTitle: _NS("Save Playlist")];
295 [o_save_panel setPrompt: _NS("Save")];
297 if( [o_save_panel runModalForDirectory: nil
298 file: o_name] == NSOKButton )
300 playlist_Export( p_playlist, [[o_save_panel filename] fileSystemRepresentation], "m3u" );
305 - (IBAction)playItem:(id)sender
307 intf_thread_t * p_intf = [NSApp getIntf];
308 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
311 if( p_playlist != NULL )
313 playlist_Goto( p_playlist, [o_table_view selectedRow] );
314 vlc_object_release( p_playlist );
318 - (IBAction)deleteItems:(id)sender
321 NSMutableArray *o_to_delete;
324 intf_thread_t * p_intf = [NSApp getIntf];
325 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
328 if( p_playlist == NULL )
333 o_to_delete = [NSMutableArray arrayWithArray:[[o_table_view selectedRowEnumerator] allObjects]];
334 c = (int)[o_to_delete count];
336 for( i = 0; i < c; i++ ) {
337 o_number = [o_to_delete lastObject];
338 i_row = [o_number intValue];
340 if( p_playlist->i_index == i_row && p_playlist->i_status )
342 playlist_Stop( p_playlist );
344 [o_to_delete removeObject: o_number];
345 [o_table_view deselectRow: i_row];
346 playlist_Delete( p_playlist, i_row );
349 vlc_object_release( p_playlist );
351 /* this is actually duplicity, because the intf.m manage also updates the view
352 * when the playlist changes. we do this on purpose, because else there is a
353 * delay of .5 sec or so when we delete an item */
354 [self playlistUpdated];
355 [self updateRowSelection];
358 - (IBAction)selectAll:(id)sender
360 [o_table_view selectAll: nil];
364 - (IBAction)searchItem:(id)sender
367 NSString *o_current_name;
368 NSString *o_current_author;
370 intf_thread_t * p_intf = [NSApp getIntf];
371 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
374 if( p_playlist == NULL )
378 if( [o_table_view numberOfRows] < 1 )
383 if( [o_table_view selectedRow] == [o_table_view numberOfRows]-1 )
389 i_current = [o_table_view selectedRow];
396 vlc_mutex_lock( &p_playlist->object_lock );
397 o_current_name = [NSString stringWithUTF8String:
398 p_playlist->pp_items[i_current]->psz_name];
399 o_current_author = [NSString stringWithUTF8String:
400 playlist_GetInfo(p_playlist, i_current ,_("General"),_("Author") )];
401 vlc_mutex_unlock( &p_playlist->object_lock );
404 if( [o_current_name rangeOfString:[o_search_keyword stringValue] options:NSCaseInsensitiveSearch ].length ||
405 [o_current_author rangeOfString:[o_search_keyword stringValue] options:NSCaseInsensitiveSearch ].length )
407 [o_table_view selectRow: i_current byExtendingSelection: NO];
408 [o_table_view scrollRowToVisible: i_current];
411 if( i_current == [o_table_view numberOfRows] - 1 )
416 while (i_current != [o_table_view selectedRow]);
417 vlc_object_release( p_playlist );
421 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
424 intf_thread_t * p_intf = [NSApp getIntf];
425 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
428 if( p_playlist == NULL )
433 for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
436 NSDictionary *o_one_item;
437 int j, i_total_options = 0, i_new_id = -1;
438 int i_mode = PLAYLIST_INSERT;
439 BOOL b_rem = FALSE, b_dir = FALSE;
440 NSString *o_uri, *o_name;
443 char **ppsz_options = NULL;
446 o_one_item = [o_array objectAtIndex: i_item];
447 o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
448 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
449 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
451 /* If no name, then make a guess */
452 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
454 if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
455 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
456 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
458 /* All of this is to make sure CD's play when you D&D them on VLC */
459 /* Converts mountpoint to a /dev file */
461 char *psz_dev, *temp;
462 buf = (struct statfs *) malloc (sizeof(struct statfs));
463 statfs( [o_uri fileSystemRepresentation], buf );
464 psz_dev = strdup(buf->f_mntfromname);
466 temp = strrchr( psz_dev , 's' );
467 psz_dev[temp - psz_dev] = '\0';
468 o_uri = [NSString stringWithCString: psz_dev ];
471 if( o_options && [o_options count] > 0 )
473 /* Count the input options */
474 i_total_options = [o_options count];
476 /* Allocate ppsz_options */
477 for( j = 0; j < i_total_options; j++ )
480 ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
482 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
487 i_new_id = playlist_AddExt( p_playlist, [o_uri fileSystemRepresentation],
488 [o_name UTF8String], i_mode,
489 i_position == -1 ? PLAYLIST_END : i_position + i_item,
490 0, (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options );
493 for( j = 0; j < i_total_options; j++ )
494 free( ppsz_options[j] );
495 if( ppsz_options ) free( ppsz_options ); */
497 /* Recent documents menu */
498 o_true_file = [NSURL fileURLWithPath: o_uri];
499 if( o_true_file != nil )
501 [[NSDocumentController sharedDocumentController]
502 noteNewRecentDocumentURL: o_true_file];
505 if( i_item == 0 && !b_enqueue )
507 playlist_Goto( p_playlist, playlist_GetPositionById( p_playlist, i_new_id ) );
508 playlist_Play( p_playlist );
512 vlc_object_release( p_playlist );
515 - (void)playlistUpdated
518 intf_thread_t * p_intf = [NSApp getIntf];
519 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
521 if( p_playlist != NULL )
523 var_Get( p_playlist, "random", &val );
524 [o_random_ckb setState: val.b_bool];
526 var_Get( p_playlist, "loop", &val );
527 [o_loop_ckb setState: val.b_bool];
529 var_Get( p_playlist, "repeat", &val );
530 [o_repeat_ckb setState: val.b_bool];
532 vlc_object_release( p_playlist );
534 [o_table_view reloadData];
537 - (void)updateRowSelection
541 intf_thread_t * p_intf = [NSApp getIntf];
542 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
545 if( p_playlist == NULL )
550 i_row = p_playlist->i_index;
551 vlc_object_release( p_playlist );
553 [o_table_view selectRow: i_row byExtendingSelection: NO];
554 [o_table_view scrollRowToVisible: i_row];
560 @implementation VLCPlaylist (NSTableDataSource)
562 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
565 intf_thread_t * p_intf = [NSApp getIntf];
566 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
569 if( p_playlist != NULL )
571 vlc_mutex_lock( &p_playlist->object_lock );
572 i_count = p_playlist->i_size;
573 vlc_mutex_unlock( &p_playlist->object_lock );
574 vlc_object_release( p_playlist );
576 [o_status_field setStringValue: [NSString stringWithFormat:_NS("%i items in playlist"), i_count]];
580 - (id)tableView:(NSTableView *)o_tv
581 objectValueForTableColumn:(NSTableColumn *)o_tc
585 intf_thread_t * p_intf = [NSApp getIntf];
586 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
589 if( p_playlist == NULL )
594 if( [[o_tc identifier] isEqualToString:@"0"] )
596 o_value = [NSString stringWithFormat:@"%i", i_row + 1];
598 else if( [[o_tc identifier] isEqualToString:@"1"] )
600 vlc_mutex_lock( &p_playlist->object_lock );
601 o_value = [NSString stringWithUTF8String:
602 p_playlist->pp_items[i_row]->psz_name];
603 vlc_mutex_unlock( &p_playlist->object_lock );
605 else if( [[o_tc identifier] isEqualToString:@"2"] )
607 vlc_mutex_lock( &p_playlist->object_lock );
608 o_value = [NSString stringWithUTF8String:
609 playlist_GetInfo(p_playlist, i_row ,_("General"),_("Author") )];
610 vlc_mutex_unlock( &p_playlist->object_lock );
612 else if( [[o_tc identifier] isEqualToString:@"3"] )
614 char psz_duration[MSTRTIME_MAX_SIZE];
615 mtime_t dur = p_playlist->pp_items[i_row]->i_duration;
618 secstotimestr( psz_duration, dur/1000000 );
619 o_value = [NSString stringWithUTF8String: psz_duration];
623 o_value = @"-:--:--";
627 vlc_object_release( p_playlist );
632 - (BOOL)tableView:(NSTableView *)o_tv
633 writeRows:(NSArray*)o_rows
634 toPasteboard:(NSPasteboard*)o_pasteboard
636 int i_rows = [o_rows count];
637 NSArray *o_filenames = [NSArray array];
639 [o_pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
640 [o_pasteboard setPropertyList:o_filenames forType:NSFilenamesPboardType];
643 i_moveRow = [[o_rows objectAtIndex:0]intValue];
649 - (NSDragOperation)tableView:(NSTableView*)o_tv
650 validateDrop:(id <NSDraggingInfo>)o_info
651 proposedRow:(int)i_row
652 proposedDropOperation:(NSTableViewDropOperation)o_operation
654 if ( o_operation == NSTableViewDropAbove )
656 if ( i_moveRow >= 0 )
658 if ( i_row != i_moveRow )
660 return NSDragOperationMove;
662 /* what if in the previous run, the row wasn't actually moved?
663 then we can't drop new files on this location */
664 return NSDragOperationNone;
666 return NSDragOperationGeneric;
668 return NSDragOperationNone;
671 - (BOOL)tableView:(NSTableView*)o_tv
672 acceptDrop:(id <NSDraggingInfo>)o_info
673 row:(int)i_proposed_row
674 dropOperation:(NSTableViewDropOperation)o_operation
676 if ( i_moveRow >= 0 )
678 if (i_moveRow != -1 && i_proposed_row != -1)
680 intf_thread_t * p_intf = [NSApp getIntf];
681 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
684 if( p_playlist == NULL )
690 playlist_Move( p_playlist, i_moveRow, i_proposed_row );
692 vlc_object_release( p_playlist );
694 [self playlistUpdated];
700 NSPasteboard * o_pasteboard;
701 o_pasteboard = [o_info draggingPasteboard];
703 if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
706 NSArray *o_array = [NSArray array];
707 NSArray *o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
708 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
710 for( i = 0; i < (int)[o_values count]; i++)
713 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
714 o_array = [o_array arrayByAddingObject: o_dic];
716 [self appendArray: o_array atPos: i_proposed_row enqueue:YES];
721 [self updateRowSelection];
724 /* Delegate method of NSWindow */
725 - (void)windowWillClose:(NSNotification *)aNotification
727 [o_btn_playlist setState: NSOffState];