1 /*****************************************************************************
2 * playlist.m: MacOS X interface plugin
3 *****************************************************************************
4 * Copyright (C) 2002-2004 VideoLAN
5 * $Id: playlist.m,v 1.54 2004/01/20 15:34:43 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("Shuffle")];
173 [o_loop_ckb setTitle: _NS("Repeat Playlist")];
174 [o_repeat_ckb setTitle: _NS("Repeat Item")];
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)
201 b_isSortDescending = VLC_FALSE;
202 [o_table_view setHighlightedTableColumn:o_tc];
203 o_tc_sortColumn = o_tc;
206 [o_table_view setIndicatorImage:nil inTableColumn:[[o_table_view tableColumns] objectAtIndex:i]];
210 if (o_tc_name == o_tc && !b_isSortDescending)
212 playlist_SortTitle( p_playlist , 0 );
213 [o_table_view setIndicatorImage:o_ascendingSortingImage inTableColumn:o_tc];
215 else if (o_tc_author == o_tc && !b_isSortDescending)
217 playlist_SortAuthor( p_playlist , 0 );
218 [o_table_view setIndicatorImage:o_ascendingSortingImage inTableColumn:o_tc];
220 else if (o_tc_name == o_tc && b_isSortDescending)
222 playlist_SortTitle( p_playlist , 1 );
223 [o_table_view setIndicatorImage:o_descendingSortingImage inTableColumn:o_tc];
225 else if (o_tc_author == o_tc && b_isSortDescending)
227 playlist_SortAuthor( p_playlist , 1 );
228 [o_table_view setIndicatorImage:o_descendingSortingImage inTableColumn:o_tc];
230 vlc_object_release( p_playlist );
231 [self playlistUpdated];
235 - (BOOL)tableView:(NSTableView *)o_tv
236 shouldEditTableColumn:(NSTableColumn *)o_tc
242 - (NSMenu *)menuForEvent:(NSEvent *)o_event
246 vlc_bool_t b_item_sel;
248 pt = [o_table_view convertPoint: [o_event locationInWindow]
250 b_item_sel = ( [o_table_view rowAtPoint: pt] != -1 &&
251 [o_table_view selectedRow] != -1 );
252 b_rows = [o_table_view numberOfRows] != 0;
254 [o_mi_play setEnabled: b_item_sel];
255 [o_mi_delete setEnabled: b_item_sel];
256 [o_mi_selectall setEnabled: b_rows];
258 return( o_ctx_menu );
261 - (IBAction)toggleWindow:(id)sender
263 if( [o_window isVisible] )
265 [o_window orderOut:sender];
266 [o_btn_playlist setState:NSOffState];
270 [o_window makeKeyAndOrderFront:sender];
271 [o_btn_playlist setState:NSOnState];
275 - (IBAction)savePlaylist:(id)sender
277 intf_thread_t * p_intf = [NSApp getIntf];
278 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
281 NSSavePanel *o_save_panel = [NSSavePanel savePanel];
282 NSString * o_name = [NSString stringWithFormat: @"%@.m3u", _NS("Untitled")];
283 [o_save_panel setTitle: _NS("Save Playlist")];
284 [o_save_panel setPrompt: _NS("Save")];
286 if( [o_save_panel runModalForDirectory: nil
287 file: o_name] == NSOKButton )
289 playlist_Export( p_playlist, [[o_save_panel filename] fileSystemRepresentation], "m3u" );
294 - (IBAction)playItem:(id)sender
296 intf_thread_t * p_intf = [NSApp getIntf];
297 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
300 if( p_playlist != NULL )
302 playlist_Goto( p_playlist, [o_table_view selectedRow] );
303 vlc_object_release( p_playlist );
307 - (IBAction)deleteItems:(id)sender
310 NSMutableArray *o_to_delete;
313 intf_thread_t * p_intf = [NSApp getIntf];
314 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
317 if( p_playlist == NULL )
322 o_to_delete = [NSMutableArray arrayWithArray:[[o_table_view selectedRowEnumerator] allObjects]];
323 c = (int)[o_to_delete count];
325 for( i = 0; i < c; i++ ) {
326 o_number = [o_to_delete lastObject];
327 i_row = [o_number intValue];
329 if( p_playlist->i_index == i_row && p_playlist->i_status )
331 playlist_Stop( p_playlist );
333 [o_to_delete removeObject: o_number];
334 [o_table_view deselectRow: i_row];
335 playlist_Delete( p_playlist, i_row );
338 vlc_object_release( p_playlist );
340 /* this is actually duplicity, because the intf.m manage also updates the view
341 * when the playlist changes. we do this on purpose, because else there is a
342 * delay of .5 sec or so when we delete an item */
343 [self playlistUpdated];
344 [self updateRowSelection];
347 - (IBAction)selectAll:(id)sender
349 [o_table_view selectAll: nil];
353 - (IBAction)searchItem:(id)sender
356 NSString *o_current_name;
357 NSString *o_current_author;
359 intf_thread_t * p_intf = [NSApp getIntf];
360 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
363 if( p_playlist == NULL )
367 if( [o_table_view numberOfRows] < 1 )
372 if( [o_table_view selectedRow] == [o_table_view numberOfRows]-1 )
378 i_current = [o_table_view selectedRow];
385 vlc_mutex_lock( &p_playlist->object_lock );
386 o_current_name = [NSString stringWithUTF8String:
387 p_playlist->pp_items[i_current]->psz_name];
388 o_current_author = [NSString stringWithUTF8String:
389 playlist_GetInfo(p_playlist, i_current ,_("General"),_("Author") )];
390 vlc_mutex_unlock( &p_playlist->object_lock );
393 if( [o_current_name rangeOfString:[o_search_keyword stringValue] options:NSCaseInsensitiveSearch ].length ||
394 [o_current_author rangeOfString:[o_search_keyword stringValue] options:NSCaseInsensitiveSearch ].length )
396 [o_table_view selectRow: i_current byExtendingSelection: NO];
397 [o_table_view scrollRowToVisible: i_current];
400 if( i_current == [o_table_view numberOfRows] - 1 )
405 while (i_current != [o_table_view selectedRow]);
406 vlc_object_release( p_playlist );
410 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
413 intf_thread_t * p_intf = [NSApp getIntf];
414 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
417 if( p_playlist == NULL )
422 for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
425 NSDictionary *o_one_item;
426 int j, i_new_id = -1;
427 int i_mode = PLAYLIST_INSERT;
428 BOOL b_rem = FALSE, b_dir = FALSE;
429 NSString *o_uri, *o_name;
434 o_one_item = [o_array objectAtIndex: i_item];
435 o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
436 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
437 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
439 /* If no name, then make a guess */
440 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
442 if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
443 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
444 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
446 /* All of this is to make sure CD's play when you D&D them on VLC */
447 /* Converts mountpoint to a /dev file */
449 char *psz_dev, *temp;
450 buf = (struct statfs *) malloc (sizeof(struct statfs));
451 statfs( [o_uri fileSystemRepresentation], buf );
452 psz_dev = strdup(buf->f_mntfromname);
454 temp = strrchr( psz_dev , 's' );
455 psz_dev[temp - psz_dev] = '\0';
456 o_uri = [NSString stringWithCString: psz_dev ];
460 i_new_id = playlist_Add( p_playlist, [o_uri fileSystemRepresentation],
461 [o_name UTF8String], i_mode,
462 i_position == -1 ? PLAYLIST_END : i_position + i_item);
464 /* Add the options, when there are any */
467 for( j = 0; j < [o_options count]; j++ )
469 playlist_AddOption( p_playlist, i_new_id,
470 strdup( [[o_options objectAtIndex:j] UTF8String] ) );
474 if( i_item == 0 && !b_enqueue )
476 playlist_Goto( p_playlist, playlist_GetPositionById( p_playlist, i_new_id ) );
477 playlist_Play( p_playlist );
480 /* Recent documents menu */
481 o_true_file = [NSURL fileURLWithPath: o_uri];
482 if( o_true_file != nil )
484 [[NSDocumentController sharedDocumentController]
485 noteNewRecentDocumentURL: o_true_file];
489 vlc_object_release( p_playlist );
492 - (void)playlistUpdated
495 intf_thread_t * p_intf = [NSApp getIntf];
496 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
498 if( p_playlist != NULL )
500 var_Get( p_playlist, "random", &val );
501 [o_random_ckb setState: val.b_bool];
503 var_Get( p_playlist, "loop", &val );
504 [o_loop_ckb setState: val.b_bool];
506 var_Get( p_playlist, "repeat", &val );
507 [o_repeat_ckb setState: val.b_bool];
509 vlc_object_release( p_playlist );
511 [o_table_view reloadData];
514 - (void)updateRowSelection
518 intf_thread_t * p_intf = [NSApp getIntf];
519 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
522 if( p_playlist == NULL )
527 i_row = p_playlist->i_index;
528 vlc_object_release( p_playlist );
530 [o_table_view selectRow: i_row byExtendingSelection: NO];
531 [o_table_view scrollRowToVisible: i_row];
537 @implementation VLCPlaylist (NSTableDataSource)
539 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
542 intf_thread_t * p_intf = [NSApp getIntf];
543 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
546 if( p_playlist != NULL )
548 vlc_mutex_lock( &p_playlist->object_lock );
549 i_count = p_playlist->i_size;
550 vlc_mutex_unlock( &p_playlist->object_lock );
551 vlc_object_release( p_playlist );
553 [o_status_field setStringValue: [NSString stringWithFormat:_NS("%i items in playlist"), i_count]];
557 - (id)tableView:(NSTableView *)o_tv
558 objectValueForTableColumn:(NSTableColumn *)o_tc
562 intf_thread_t * p_intf = [NSApp getIntf];
563 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
566 if( p_playlist == NULL )
571 if( [[o_tc identifier] isEqualToString:@"0"] )
573 o_value = [NSString stringWithFormat:@"%i", i_row + 1];
575 else if( [[o_tc identifier] isEqualToString:@"1"] )
577 vlc_mutex_lock( &p_playlist->object_lock );
578 o_value = [NSString stringWithUTF8String:
579 p_playlist->pp_items[i_row]->psz_name];
580 vlc_mutex_unlock( &p_playlist->object_lock );
582 else if( [[o_tc identifier] isEqualToString:@"2"] )
584 vlc_mutex_lock( &p_playlist->object_lock );
585 o_value = [NSString stringWithUTF8String:
586 playlist_GetInfo(p_playlist, i_row ,_("General"),_("Author") )];
587 vlc_mutex_unlock( &p_playlist->object_lock );
589 else if( [[o_tc identifier] isEqualToString:@"3"] )
591 char psz_duration[MSTRTIME_MAX_SIZE];
592 mtime_t dur = p_playlist->pp_items[i_row]->i_duration;
595 secstotimestr( psz_duration, dur/1000000 );
596 o_value = [NSString stringWithUTF8String: psz_duration];
600 o_value = @"-:--:--";
604 vlc_object_release( p_playlist );
609 - (BOOL)tableView:(NSTableView *)o_tv
610 writeRows:(NSArray*)o_rows
611 toPasteboard:(NSPasteboard*)o_pasteboard
613 int i_rows = [o_rows count];
614 NSArray *o_filenames = [NSArray array];
616 [o_pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
617 [o_pasteboard setPropertyList:o_filenames forType:NSFilenamesPboardType];
620 i_moveRow = [[o_rows objectAtIndex:0]intValue];
626 - (NSDragOperation)tableView:(NSTableView*)o_tv
627 validateDrop:(id <NSDraggingInfo>)o_info
628 proposedRow:(int)i_row
629 proposedDropOperation:(NSTableViewDropOperation)o_operation
631 if ( o_operation == NSTableViewDropAbove )
633 if ( i_moveRow >= 0 )
635 if ( i_row != i_moveRow )
637 return NSDragOperationMove;
639 /* what if in the previous run, the row wasn't actually moved?
640 then we can't drop new files on this location */
641 return NSDragOperationNone;
643 return NSDragOperationGeneric;
645 return NSDragOperationNone;
648 - (BOOL)tableView:(NSTableView*)o_tv
649 acceptDrop:(id <NSDraggingInfo>)o_info
650 row:(int)i_proposed_row
651 dropOperation:(NSTableViewDropOperation)o_operation
653 if ( i_moveRow >= 0 )
655 if (i_moveRow != -1 && i_proposed_row != -1)
657 intf_thread_t * p_intf = [NSApp getIntf];
658 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
661 if( p_playlist == NULL )
667 playlist_Move( p_playlist, i_moveRow, i_proposed_row );
669 vlc_object_release( p_playlist );
671 [self playlistUpdated];
677 NSPasteboard * o_pasteboard;
678 o_pasteboard = [o_info draggingPasteboard];
680 if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
683 NSArray *o_array = [NSArray array];
684 NSArray *o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
685 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
687 for( i = 0; i < (int)[o_values count]; i++)
690 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
691 o_array = [o_array arrayByAddingObject: o_dic];
693 [self appendArray: o_array atPos: i_proposed_row enqueue:YES];
698 [self updateRowSelection];
701 /* Delegate method of NSWindow */
702 - (void)windowWillClose:(NSNotification *)aNotification
704 [o_btn_playlist setState: NSOffState];