1 /*****************************************************************************
2 * playlist.m: MacOS X interface plugin
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: playlist.m,v 1.48 2003/12/11 19:34:46 hartman Exp $
7 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8 * Derk-Jan Hartman <thedj@users.sourceforge.net>
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 );
109 /*****************************************************************************
110 * VLCPlaylist implementation
111 *****************************************************************************/
112 @implementation VLCPlaylist
126 [o_table_view setTarget: self];
127 [o_table_view setDelegate: self];
128 [o_table_view setDataSource: self];
130 [o_table_view setDoubleAction: @selector(playItem:)];
132 [o_table_view registerForDraggedTypes:
133 [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
135 [o_window setExcludedFromWindowsMenu: TRUE];
141 [o_window setTitle: _NS("Playlist")];
142 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
143 [o_mi_play setTitle: _NS("Play")];
144 [o_mi_delete setTitle: _NS("Delete")];
145 [o_mi_selectall setTitle: _NS("Select All")];
146 [[o_tc_name headerCell] setStringValue:_NS("Name")];
147 [[o_tc_author headerCell] setStringValue:_NS("Author")];
148 [o_random_ckb setTitle: _NS("Shuffle")];
149 [o_loop_ckb setTitle: _NS("Repeat Playlist")];
150 [o_repeat_ckb setTitle: _NS("Repeat Item")];
151 [o_search_button setTitle: _NS("Search")];
152 [o_btn_playlist setToolTip: _NS("Playlist")];
155 - (BOOL)tableView:(NSTableView *)o_tv
156 shouldEditTableColumn:(NSTableColumn *)o_tc
162 - (NSMenu *)menuForEvent:(NSEvent *)o_event
166 vlc_bool_t b_item_sel;
168 pt = [o_table_view convertPoint: [o_event locationInWindow]
170 b_item_sel = ( [o_table_view rowAtPoint: pt] != -1 &&
171 [o_table_view selectedRow] != -1 );
172 b_rows = [o_table_view numberOfRows] != 0;
174 [o_mi_play setEnabled: b_item_sel];
175 [o_mi_delete setEnabled: b_item_sel];
176 [o_mi_selectall setEnabled: b_rows];
178 return( o_ctx_menu );
181 - (IBAction)toggleWindow:(id)sender
183 if( [o_window isVisible] )
185 [o_window orderOut:sender];
186 [o_btn_playlist setState:NSOffState];
190 [o_window makeKeyAndOrderFront:sender];
191 [o_btn_playlist setState:NSOnState];
195 - (IBAction)savePlaylist:(id)sender
197 intf_thread_t * p_intf = [NSApp getIntf];
198 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
201 NSSavePanel *o_save_panel = [NSSavePanel savePanel];
202 NSString * o_name = [NSString stringWithFormat: @"%@.m3u", _NS("Untitled")];
203 [o_save_panel setTitle: _NS("Save Playlist")];
204 [o_save_panel setPrompt: _NS("Save")];
206 if( [o_save_panel runModalForDirectory: nil
207 file: o_name] == NSOKButton )
209 playlist_SaveFile( p_playlist, [[o_save_panel filename] fileSystemRepresentation] );
214 - (IBAction)playItem:(id)sender
216 intf_thread_t * p_intf = [NSApp getIntf];
217 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
220 if( p_playlist != NULL )
222 playlist_Goto( p_playlist, [o_table_view selectedRow] );
223 vlc_object_release( p_playlist );
227 - (IBAction)deleteItems:(id)sender
230 NSMutableArray *o_to_delete;
233 intf_thread_t * p_intf = [NSApp getIntf];
234 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
237 if( p_playlist == NULL )
242 o_to_delete = [NSMutableArray arrayWithArray:[[o_table_view selectedRowEnumerator] allObjects]];
243 c = (int)[o_to_delete count];
245 for( i = 0; i < c; i++ ) {
246 o_number = [o_to_delete lastObject];
247 i_row = [o_number intValue];
249 if( p_playlist->i_index == i_row && p_playlist->i_status )
251 playlist_Stop( p_playlist );
253 [o_to_delete removeObject: o_number];
254 [o_table_view deselectRow: i_row];
255 playlist_Delete( p_playlist, i_row );
258 vlc_object_release( p_playlist );
260 /* this is actually duplicity, because the intf.m manage also updates the view
261 * when the playlist changes. we do this on purpose, because else there is a
262 * delay of .5 sec or so when we delete an item */
263 [self playlistUpdated];
264 [self updateRowSelection];
267 - (IBAction)selectAll:(id)sender
269 [o_table_view selectAll: nil];
273 - (IBAction)searchItem:(id)sender
276 NSString *o_current_name;
277 NSString *o_current_author;
279 intf_thread_t * p_intf = [NSApp getIntf];
280 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
283 if( p_playlist == NULL )
288 if ([o_table_view selectedRow] == [o_table_view numberOfRows]-1 )
294 i_current = [o_table_view selectedRow];
301 vlc_mutex_lock( &p_playlist->object_lock );
302 o_current_name = [NSString stringWithUTF8String:
303 p_playlist->pp_items[i_current]->psz_name];
304 o_current_author = [NSString stringWithUTF8String:
305 p_playlist->pp_items[i_current]->psz_author];
306 vlc_mutex_unlock( &p_playlist->object_lock );
309 if( [o_current_name rangeOfString:[o_search_keyword stringValue] options:NSCaseInsensitiveSearch ].length ||
310 [o_current_author rangeOfString:[o_search_keyword stringValue] options:NSCaseInsensitiveSearch ].length )
312 [o_table_view selectRow: i_current byExtendingSelection: NO];
313 [o_table_view scrollRowToVisible: i_current];
316 if ( i_current == [o_table_view numberOfRows] - 1 )
321 while (i_current != [o_table_view selectedRow]);
322 vlc_object_release( p_playlist );
325 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
328 intf_thread_t * p_intf = [NSApp getIntf];
329 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
332 if( p_playlist == NULL )
337 for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
340 NSDictionary *o_one_item;
342 int i_total_options = 0;
343 int i_mode = PLAYLIST_INSERT;
344 BOOL b_rem = FALSE, b_dir = FALSE;
345 NSString *o_url, *o_name;
347 char **ppsz_options = NULL;
350 o_one_item = [o_array objectAtIndex: i_item];
351 o_url = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
352 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
353 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
355 /* If no name, then make a guess */
356 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_url];
358 if( [[NSFileManager defaultManager] fileExistsAtPath:o_url isDirectory:&b_dir] && b_dir &&
359 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_url isRemovable: &b_rem
360 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
362 /* All of this is to make sure CD's play when you D&D them on VLC */
363 /* Converts mountpoint to a /dev file */
365 char *psz_dev, *temp;
366 buf = (struct statfs *) malloc (sizeof(struct statfs));
367 statfs( [o_url fileSystemRepresentation], buf );
368 psz_dev = strdup(buf->f_mntfromname);
370 temp = strrchr( psz_dev , 's' );
371 psz_dev[temp - psz_dev] = '\0';
372 o_url = [NSString stringWithCString: psz_dev ];
375 if (i_item == 0 && !b_enqueue)
376 i_mode |= PLAYLIST_GO;
378 if( o_options && [o_options count] > 0 )
380 /* Count the input options */
381 i_total_options = [o_options count];
383 /* Allocate ppsz_options */
384 for( j = 0; j < i_total_options; j++ )
387 ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
389 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
393 playlist_AddExt( p_playlist, [o_url fileSystemRepresentation], [o_name UTF8String], -1,
394 (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options,
395 i_mode, i_position == -1 ? PLAYLIST_END : i_position + i_item);
398 for( j = 0; j < i_total_options; j++ )
399 free( ppsz_options[j] );
400 if( ppsz_options ) free( ppsz_options );
402 /* Recent documents menu */
403 NSURL *o_true_url = [NSURL fileURLWithPath: o_url];
404 if( o_true_url != nil )
406 [[NSDocumentController sharedDocumentController]
407 noteNewRecentDocumentURL: o_true_url];
411 vlc_object_release( p_playlist );
414 - (void)playlistUpdated
417 intf_thread_t * p_intf = [NSApp getIntf];
418 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
420 if( p_playlist != NULL )
422 var_Get( p_playlist, "random", &val );
423 [o_random_ckb setState: val.b_bool];
425 var_Get( p_playlist, "loop", &val );
426 [o_loop_ckb setState: val.b_bool];
428 var_Get( p_playlist, "repeat", &val );
429 [o_repeat_ckb setState: val.b_bool];
431 vlc_object_release( p_playlist );
433 [o_table_view reloadData];
436 - (void)updateRowSelection
440 intf_thread_t * p_intf = [NSApp getIntf];
441 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
444 if( p_playlist == NULL )
449 i_row = p_playlist->i_index;
450 vlc_object_release( p_playlist );
452 [o_table_view selectRow: i_row byExtendingSelection: NO];
453 [o_table_view scrollRowToVisible: i_row];
459 @implementation VLCPlaylist (NSTableDataSource)
461 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
464 intf_thread_t * p_intf = [NSApp getIntf];
465 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
468 if( p_playlist != NULL )
470 vlc_mutex_lock( &p_playlist->object_lock );
471 i_count = p_playlist->i_size;
472 vlc_mutex_unlock( &p_playlist->object_lock );
473 vlc_object_release( p_playlist );
475 [o_status_field setStringValue: [NSString stringWithFormat:_NS("%i items in playlist"), i_count]];
479 - (id)tableView:(NSTableView *)o_tv
480 objectValueForTableColumn:(NSTableColumn *)o_tc
484 intf_thread_t * p_intf = [NSApp getIntf];
485 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
488 if( p_playlist == NULL )
493 if( [[o_tc identifier] isEqualToString:@"0"] )
495 o_value = [NSString stringWithFormat:@"%i", i_row + 1];
497 else if( [[o_tc identifier] isEqualToString:@"1"] )
499 vlc_mutex_lock( &p_playlist->object_lock );
500 o_value = [[NSString stringWithUTF8String:
501 p_playlist->pp_items[i_row]->psz_name] lastPathComponent];
502 vlc_mutex_unlock( &p_playlist->object_lock );
504 else if( [[o_tc identifier] isEqualToString:@"2"] )
506 vlc_mutex_lock( &p_playlist->object_lock );
507 o_value = [NSString stringWithUTF8String:
508 p_playlist->pp_items[i_row]->psz_author];
509 vlc_mutex_unlock( &p_playlist->object_lock );
512 vlc_object_release( p_playlist );
517 - (BOOL)tableView:(NSTableView *)o_tv
518 writeRows:(NSArray*)o_rows
519 toPasteboard:(NSPasteboard*)o_pasteboard
521 int i_rows = [o_rows count];
522 NSArray *o_filenames = [NSArray array];
524 [o_pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
525 [o_pasteboard setPropertyList:o_filenames forType:NSFilenamesPboardType];
528 i_moveRow = [[o_rows objectAtIndex:0]intValue];
534 - (NSDragOperation)tableView:(NSTableView*)o_tv
535 validateDrop:(id <NSDraggingInfo>)o_info
536 proposedRow:(int)i_row
537 proposedDropOperation:(NSTableViewDropOperation)o_operation
539 if ( o_operation == NSTableViewDropAbove )
541 if ( i_moveRow >= 0 )
543 if ( i_row != i_moveRow )
545 return NSDragOperationMove;
547 /* what if in the previous run, the row wasn't actually moved?
548 then we can't drop new files on this location */
549 return NSDragOperationNone;
551 return NSDragOperationGeneric;
553 return NSDragOperationNone;
556 - (BOOL)tableView:(NSTableView*)o_tv
557 acceptDrop:(id <NSDraggingInfo>)o_info
558 row:(int)i_proposed_row
559 dropOperation:(NSTableViewDropOperation)o_operation
561 if ( i_moveRow >= 0 )
563 if (i_moveRow != -1 && i_proposed_row != -1)
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 )
575 playlist_Move( p_playlist, i_moveRow, i_proposed_row );
577 vlc_object_release( p_playlist );
579 [self playlistUpdated];
585 NSPasteboard * o_pasteboard;
586 o_pasteboard = [o_info draggingPasteboard];
588 if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
591 NSArray *o_array = [NSArray array];
592 NSArray *o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
593 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
595 for( i = 0; i < (int)[o_values count]; i++)
598 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
599 o_array = [o_array arrayByAddingObject: o_dic];
601 [self appendArray: o_array atPos: i_proposed_row enqueue:YES];
606 [self updateRowSelection];
609 /* Delegate method of NSWindow */
610 - (void)windowWillClose:(NSNotification *)aNotification
612 [o_btn_playlist setState: NSOffState];