1 /*****************************************************************************
2 * playlist.m: MacOS X interface plugin
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: playlist.m,v 1.40 2003/11/17 13:05:17 bigben 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];
137 [o_window setTitle: _NS("Playlist")];
138 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
139 [o_mi_play setTitle: _NS("Play")];
140 [o_mi_delete setTitle: _NS("Delete")];
141 [o_mi_selectall setTitle: _NS("Select All")];
142 [[o_tc_name headerCell] setStringValue:_NS("Name")];
143 [[o_tc_author headerCell] setStringValue:_NS("Author")];
144 [o_random_ckb setTitle: _NS("Random")];
145 [o_loop_ckb setTitle: _NS("Repeat All")];
146 [o_repeat_ckb setTitle: _NS("Repeat One")];
150 - (BOOL)tableView:(NSTableView *)o_tv
151 shouldEditTableColumn:(NSTableColumn *)o_tc
157 - (NSMenu *)menuForEvent:(NSEvent *)o_event
161 vlc_bool_t b_item_sel;
163 pt = [o_table_view convertPoint: [o_event locationInWindow]
165 b_item_sel = ( [o_table_view rowAtPoint: pt] != -1 &&
166 [o_table_view selectedRow] != -1 );
167 b_rows = [o_table_view numberOfRows] != 0;
169 [o_mi_play setEnabled: b_item_sel];
170 [o_mi_delete setEnabled: b_item_sel];
171 [o_mi_selectall setEnabled: b_rows];
173 return( o_ctx_menu );
176 - (IBAction)toggleWindow:(id)sender
178 if( [o_window isVisible] )
180 [o_window orderOut:sender];
184 [o_window makeKeyAndOrderFront:sender];
188 - (IBAction)savePlaylist:(id)sender
190 intf_thread_t * p_intf = [NSApp getIntf];
191 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
194 NSSavePanel *o_save_panel = [NSSavePanel savePanel];
195 NSString * o_name = [NSString stringWithFormat: @"%@.m3u", _NS("Untitled")];
196 [o_save_panel setTitle: _NS("Save Playlist")];
197 [o_save_panel setPrompt: _NS("Save")];
199 if( [o_save_panel runModalForDirectory: nil
200 file: o_name] == NSOKButton )
202 playlist_SaveFile( p_playlist, [[o_save_panel filename] fileSystemRepresentation] );
207 - (IBAction)playItem:(id)sender
209 intf_thread_t * p_intf = [NSApp getIntf];
210 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
213 if( p_playlist != NULL )
215 playlist_Goto( p_playlist, [o_table_view selectedRow] );
216 vlc_object_release( p_playlist );
220 - (IBAction)deleteItems:(id)sender
223 NSMutableArray *o_to_delete;
226 intf_thread_t * p_intf = [NSApp getIntf];
227 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
230 if( p_playlist == NULL )
235 o_to_delete = [NSMutableArray arrayWithArray:[[o_table_view selectedRowEnumerator] allObjects]];
236 c = (int)[o_to_delete count];
238 for( i = 0; i < c; i++ ) {
239 o_number = [o_to_delete lastObject];
240 i_row = [o_number intValue];
242 if( p_playlist->i_index == i_row && p_playlist->i_status )
244 playlist_Stop( p_playlist );
246 [o_to_delete removeObject: o_number];
247 [o_table_view deselectRow: i_row];
248 playlist_Delete( p_playlist, i_row );
251 vlc_object_release( p_playlist );
253 /* this is actually duplicity, because the intf.m manage also updates the view
254 * when the playlist changes. we do this on purpose, because else there is a
255 * delay of .5 sec or so when we delete an item */
256 [self playlistUpdated];
257 [self updateRowSelection];
260 - (IBAction)selectAll:(id)sender
262 [o_table_view selectAll: nil];
266 - (IBAction)searchItem:(id)sender
272 intf_thread_t * p_intf = [NSApp getIntf];
273 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
275 if ( [o_table_view selectedRow] == [o_table_view numberOfRows]-1 )
281 i_start = [o_table_view selectedRow];
284 for ( i_current = i_start + 1 ; i_current < [o_table_view numberOfRows] ; i_current++ )
287 if( p_playlist == NULL )
289 o_current_name = nil;
295 vlc_mutex_lock( &p_playlist->object_lock );
296 o_current_name = [[NSString stringWithUTF8String:
297 p_playlist->pp_items[i_current]->psz_name] lastPathComponent];
298 vlc_mutex_unlock( &p_playlist->object_lock );
301 if( [o_current_name rangeOfString:[o_search_keyword stringValue] options:NSCaseInsensitiveSearch ].length )
303 [o_table_view selectRow: i_current byExtendingSelection: NO];
304 [o_table_view scrollRowToVisible: i_current];
307 [o_table_view selectRow: i_current byExtendingSelection: NO];
308 [o_table_view scrollRowToVisible: i_current];
310 vlc_object_release( p_playlist );
313 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
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 )
325 for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
328 NSDictionary *o_one_item;
330 int i_total_options = 0;
331 int i_mode = PLAYLIST_INSERT;
332 BOOL b_rem = FALSE, b_dir = FALSE;
333 NSString *o_url, *o_name;
335 char **ppsz_options = NULL;
338 o_one_item = [o_array objectAtIndex: i_item];
339 o_url = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
340 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
341 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
343 /* If no name, then make a guess */
344 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_url];
346 if( [[NSFileManager defaultManager] fileExistsAtPath:o_url isDirectory:&b_dir] && b_dir &&
347 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_url isRemovable: &b_rem
348 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
350 /* All of this is to make sure CD's play when you D&D them on VLC */
351 /* Converts mountpoint to a /dev file */
353 char *psz_dev, *temp;
354 buf = (struct statfs *) malloc (sizeof(struct statfs));
355 statfs( [o_url fileSystemRepresentation], buf );
356 psz_dev = strdup(buf->f_mntfromname);
358 temp = strrchr( psz_dev , 's' );
359 psz_dev[temp - psz_dev] = '\0';
360 o_url = [NSString stringWithCString: psz_dev ];
363 if (i_item == 0 && !b_enqueue)
364 i_mode |= PLAYLIST_GO;
366 if( o_options && [o_options count] > 0 )
368 /* Count the input options */
369 i_total_options = [o_options count];
371 /* Allocate ppsz_options */
372 for( j = 0; j < i_total_options; j++ )
375 ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
377 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
381 playlist_AddExt( p_playlist, [o_url fileSystemRepresentation], [o_name UTF8String], -1,
382 (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options,
383 i_mode, i_position == -1 ? PLAYLIST_END : i_position + i_item);
386 for( j = 0; j < i_total_options; j++ )
387 free( ppsz_options[j] );
388 if( ppsz_options ) free( ppsz_options );
390 /* Recent documents menu */
391 NSURL *o_true_url = [NSURL fileURLWithPath: o_url];
392 if( o_true_url != nil )
394 [[NSDocumentController sharedDocumentController]
395 noteNewRecentDocumentURL: o_true_url];
399 vlc_object_release( p_playlist );
402 - (void)playlistUpdated
404 [o_table_view reloadData];
407 - (void)updateRowSelection
411 intf_thread_t * p_intf = [NSApp getIntf];
412 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
415 if( p_playlist == NULL )
420 i_row = p_playlist->i_index;
421 vlc_object_release( p_playlist );
423 [o_table_view selectRow: i_row byExtendingSelection: NO];
424 [o_table_view scrollRowToVisible: i_row];
430 @implementation VLCPlaylist (NSTableDataSource)
432 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
435 intf_thread_t * p_intf = [NSApp getIntf];
436 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
439 if( p_playlist != NULL )
441 vlc_mutex_lock( &p_playlist->object_lock );
442 i_count = p_playlist->i_size;
443 vlc_mutex_unlock( &p_playlist->object_lock );
444 vlc_object_release( p_playlist );
446 [o_status_field setStringValue: [NSString stringWithFormat:_NS("%i items in playlist"), i_count]];
450 - (id)tableView:(NSTableView *)o_tv
451 objectValueForTableColumn:(NSTableColumn *)o_tc
455 intf_thread_t * p_intf = [NSApp getIntf];
456 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
459 if( p_playlist == NULL )
464 if( [[o_tc identifier] isEqualToString:@"0"] )
466 o_value = [NSString stringWithFormat:@"%i", i_row + 1];
468 else if( [[o_tc identifier] isEqualToString:@"1"] )
470 vlc_mutex_lock( &p_playlist->object_lock );
471 o_value = [[NSString stringWithUTF8String:
472 p_playlist->pp_items[i_row]->psz_name] lastPathComponent];
473 vlc_mutex_unlock( &p_playlist->object_lock );
475 else if( [[o_tc identifier] isEqualToString:@"2"] )
477 vlc_mutex_lock( &p_playlist->object_lock );
478 o_value = [NSString stringWithUTF8String:
479 p_playlist->pp_items[i_row]->psz_author];
480 vlc_mutex_unlock( &p_playlist->object_lock );
483 vlc_object_release( p_playlist );
488 - (BOOL)tableView:(NSTableView *)o_tv
489 writeRows:(NSArray*)o_rows
490 toPasteboard:(NSPasteboard*)o_pasteboard
492 int i_rows = [o_rows count];
493 NSArray *o_filenames = [NSArray array];
495 [o_pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
496 [o_pasteboard setPropertyList:o_filenames forType:NSFilenamesPboardType];
499 i_moveRow = [[o_rows objectAtIndex:0]intValue];
505 - (NSDragOperation)tableView:(NSTableView*)o_tv
506 validateDrop:(id <NSDraggingInfo>)o_info
507 proposedRow:(int)i_row
508 proposedDropOperation:(NSTableViewDropOperation)o_operation
510 if ( o_operation == NSTableViewDropAbove )
512 if ( i_moveRow >= 0 )
514 if ( i_row != i_moveRow )
516 return NSDragOperationMove;
518 /* what if in the previous run, the row wasn't actually moved?
519 then we can't drop new files on this location */
520 return NSDragOperationNone;
522 return NSDragOperationGeneric;
524 return NSDragOperationNone;
527 - (BOOL)tableView:(NSTableView*)o_tv
528 acceptDrop:(id <NSDraggingInfo>)o_info
529 row:(int)i_proposed_row
530 dropOperation:(NSTableViewDropOperation)o_operation
532 if ( i_moveRow >= 0 )
534 if (i_moveRow != -1 && i_proposed_row != -1)
536 intf_thread_t * p_intf = [NSApp getIntf];
537 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
540 if( p_playlist == NULL )
546 playlist_Move( p_playlist, i_moveRow, i_proposed_row );
548 vlc_object_release( p_playlist );
550 [self playlistUpdated];
556 NSPasteboard * o_pasteboard;
557 o_pasteboard = [o_info draggingPasteboard];
559 if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
562 NSArray *o_array = [NSArray array];
563 NSArray *o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
564 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
566 for( i = 0; i < (int)[o_values count]; i++)
569 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
570 o_array = [o_array arrayByAddingObject: o_dic];
572 [self appendArray: o_array atPos: i_proposed_row enqueue:YES];
577 [self updateRowSelection];