1 /*****************************************************************************
2 * playlist.m: MacOS X interface plugin
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: playlist.m,v 1.37 2003/11/12 02:45:26 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>
38 /*****************************************************************************
39 * VLCPlaylistView implementation
40 *****************************************************************************/
41 @implementation VLCPlaylistView
43 - (NSMenu *)menuForEvent:(NSEvent *)o_event
45 return( [[self delegate] menuForEvent: o_event] );
48 - (void)keyDown:(NSEvent *)o_event
52 NSMutableArray *o_to_delete;
55 playlist_t * p_playlist;
56 intf_thread_t * p_intf = [NSApp getIntf];
58 if( [[o_event characters] length] )
60 key = [[o_event characters] characterAtIndex: 0];
63 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
66 if ( p_playlist == NULL )
73 case NSDeleteCharacter:
74 case NSDeleteFunctionKey:
75 case NSDeleteCharFunctionKey:
76 case NSBackspaceCharacter:
77 o_to_delete = [NSMutableArray arrayWithArray:[[self selectedRowEnumerator] allObjects]];
78 c = [o_to_delete count];
80 for( i = 0; i < c; i++ ) {
81 o_number = [o_to_delete lastObject];
82 i_row = [o_number intValue];
84 if( p_playlist->i_index == i_row && p_playlist->i_status )
86 playlist_Stop( p_playlist );
88 [o_to_delete removeObject: o_number];
89 [self deselectRow: i_row];
90 playlist_Delete( p_playlist, i_row );
96 [super keyDown: o_event];
100 if( p_playlist != NULL )
102 vlc_object_release( p_playlist );
108 /*****************************************************************************
109 * VLCPlaylist implementation
110 *****************************************************************************/
111 @implementation VLCPlaylist
125 [o_table_view setTarget: self];
126 [o_table_view setDelegate: self];
127 [o_table_view setDataSource: self];
129 [o_table_view setDoubleAction: @selector(playItem:)];
131 [o_table_view registerForDraggedTypes:
132 [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
134 [o_window setExcludedFromWindowsMenu: TRUE];
136 [o_window setTitle: _NS("Playlist")];
137 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
138 [o_mi_play setTitle: _NS("Play")];
139 [o_mi_delete setTitle: _NS("Delete")];
140 [o_mi_selectall setTitle: _NS("Select All")];
141 [[o_tc_name headerCell] setStringValue:_NS("Name")];
142 [[o_tc_author headerCell] setStringValue:_NS("Author")];
145 - (BOOL)tableView:(NSTableView *)o_tv
146 shouldEditTableColumn:(NSTableColumn *)o_tc
152 - (NSMenu *)menuForEvent:(NSEvent *)o_event
156 vlc_bool_t b_item_sel;
158 pt = [o_table_view convertPoint: [o_event locationInWindow]
160 b_item_sel = ( [o_table_view rowAtPoint: pt] != -1 &&
161 [o_table_view selectedRow] != -1 );
162 b_rows = [o_table_view numberOfRows] != 0;
164 [o_mi_play setEnabled: b_item_sel];
165 [o_mi_delete setEnabled: b_item_sel];
166 [o_mi_selectall setEnabled: b_rows];
168 return( o_ctx_menu );
171 - (IBAction)toggleWindow:(id)sender
173 if( [o_window isVisible] )
175 [o_window orderOut:sender];
179 [o_window makeKeyAndOrderFront:sender];
183 - (IBAction)savePlaylist:(id)sender
185 intf_thread_t * p_intf = [NSApp getIntf];
186 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
189 NSSavePanel *o_save_panel = [NSSavePanel savePanel];
190 NSString * o_name = [NSString stringWithFormat: @"%@.m3u", _NS("Untitled")];
191 [o_save_panel setTitle: _NS("Save Playlist")];
192 [o_save_panel setPrompt: _NS("Save")];
194 if( [o_save_panel runModalForDirectory: nil
195 file: o_name] == NSOKButton )
197 playlist_SaveFile( p_playlist, [[o_save_panel filename] fileSystemRepresentation] );
202 - (IBAction)playItem:(id)sender
204 intf_thread_t * p_intf = [NSApp getIntf];
205 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
208 if( p_playlist != NULL )
210 playlist_Goto( p_playlist, [o_table_view selectedRow] );
211 vlc_object_release( p_playlist );
215 - (IBAction)deleteItems:(id)sender
218 NSMutableArray *o_to_delete;
221 intf_thread_t * p_intf = [NSApp getIntf];
222 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
225 if( p_playlist == NULL )
230 o_to_delete = [NSMutableArray arrayWithArray:[[o_table_view selectedRowEnumerator] allObjects]];
231 c = (int)[o_to_delete count];
233 for( i = 0; i < c; i++ ) {
234 o_number = [o_to_delete lastObject];
235 i_row = [o_number intValue];
237 if( p_playlist->i_index == i_row && p_playlist->i_status )
239 playlist_Stop( p_playlist );
241 [o_to_delete removeObject: o_number];
242 [o_table_view deselectRow: i_row];
243 playlist_Delete( p_playlist, i_row );
246 vlc_object_release( p_playlist );
248 /* this is actually duplicity, because the intf.m manage also updates the view
249 * when the playlist changes. we do this on purpose, because else there is a
250 * delay of .5 sec or so when we delete an item */
251 [self playlistUpdated];
252 [self updateRowSelection];
255 - (IBAction)selectAll:(id)sender
257 [o_table_view selectAll: nil];
260 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
263 intf_thread_t * p_intf = [NSApp getIntf];
264 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
267 if( p_playlist == NULL )
272 for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
275 NSDictionary *o_one_item;
277 int i_total_options = 0;
278 int i_mode = PLAYLIST_INSERT;
279 BOOL b_rem = FALSE, b_dir = FALSE;
280 NSString *o_url, *o_name;
282 char **ppsz_options = NULL;
285 o_one_item = [o_array objectAtIndex: i_item];
286 o_url = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
287 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
288 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
290 /* If no name, then make a guess */
291 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_url];
293 if( [[NSFileManager defaultManager] fileExistsAtPath:o_url isDirectory:&b_dir] && b_dir &&
294 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_url isRemovable: &b_rem
295 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
297 /* All of this is to make sure CD's play when you D&D them on VLC */
298 /* Converts mountpoint to a /dev file */
300 char *psz_dev, *temp;
301 buf = (struct statfs *) malloc (sizeof(struct statfs));
302 statfs( [o_url fileSystemRepresentation], buf );
303 psz_dev = strdup(buf->f_mntfromname);
305 temp = strrchr( psz_dev , 's' );
306 psz_dev[temp - psz_dev] = '\0';
307 o_url = [NSString stringWithCString: psz_dev ];
311 if (i_item == 0 && !b_enqueue)
312 i_mode |= PLAYLIST_GO;
314 if( o_options && [o_options count] > 0 )
316 /* Count the input options */
317 i_total_options = [o_options count];
319 /* Allocate ppsz_options */
320 for( j = 0; j < i_total_options; j++ )
323 ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
325 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
329 playlist_AddExt( p_playlist, [o_url fileSystemRepresentation], [o_name UTF8String], -1,
330 (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options,
331 i_mode, i_position == -1 ? PLAYLIST_END : i_position + i_item);
334 for( j = 0; j < i_total_options; j++ )
335 free( ppsz_options[j] );
336 if( ppsz_options ) free( ppsz_options );
338 /* Recent documents menu */
339 NSURL *o_true_url = [NSURL fileURLWithPath: o_url];
340 if( o_true_url != nil )
342 [[NSDocumentController sharedDocumentController]
343 noteNewRecentDocumentURL: o_true_url];
347 vlc_object_release( p_playlist );
350 - (void)playlistUpdated
352 [o_table_view reloadData];
355 - (void)updateRowSelection
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 )
368 i_row = p_playlist->i_index;
369 vlc_object_release( p_playlist );
371 [o_table_view selectRow: i_row byExtendingSelection: NO];
372 [o_table_view scrollRowToVisible: i_row];
377 @implementation VLCPlaylist (NSTableDataSource)
379 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
382 intf_thread_t * p_intf = [NSApp getIntf];
383 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
386 if( p_playlist != NULL )
388 vlc_mutex_lock( &p_playlist->object_lock );
389 i_count = p_playlist->i_size;
390 vlc_mutex_unlock( &p_playlist->object_lock );
391 vlc_object_release( p_playlist );
393 [o_status_field setStringValue: [NSString stringWithFormat:_NS("%i items in playlist"), i_count]];
397 - (id)tableView:(NSTableView *)o_tv
398 objectValueForTableColumn:(NSTableColumn *)o_tc
402 intf_thread_t * p_intf = [NSApp getIntf];
403 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
406 if( p_playlist == NULL )
411 if( [[o_tc identifier] isEqualToString:@"0"] )
413 o_value = [NSString stringWithFormat:@"%i", i_row + 1];
415 else if( [[o_tc identifier] isEqualToString:@"1"] )
417 vlc_mutex_lock( &p_playlist->object_lock );
418 o_value = [[NSString stringWithUTF8String:
419 p_playlist->pp_items[i_row]->psz_name] lastPathComponent];
420 vlc_mutex_unlock( &p_playlist->object_lock );
422 else if( [[o_tc identifier] isEqualToString:@"2"] )
424 vlc_mutex_lock( &p_playlist->object_lock );
425 o_value = [NSString stringWithUTF8String:
426 p_playlist->pp_items[i_row]->psz_author];
427 vlc_mutex_unlock( &p_playlist->object_lock );
430 vlc_object_release( p_playlist );
435 - (BOOL)tableView:(NSTableView *)o_tv
436 writeRows:(NSArray*)o_rows
437 toPasteboard:(NSPasteboard*)o_pasteboard
439 int i_rows = [o_rows count];
440 NSArray *o_filenames = [NSArray array];
442 [o_pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
443 [o_pasteboard setPropertyList:o_filenames forType:NSFilenamesPboardType];
446 i_moveRow = [[o_rows objectAtIndex:0]intValue];
452 - (NSDragOperation)tableView:(NSTableView*)o_tv
453 validateDrop:(id <NSDraggingInfo>)o_info
454 proposedRow:(int)i_row
455 proposedDropOperation:(NSTableViewDropOperation)o_operation
457 if ( o_operation == NSTableViewDropAbove )
459 if ( i_moveRow >= 0 )
461 if ( i_row != i_moveRow )
463 return NSDragOperationMove;
465 /* what if in the previous run, the row wasn't actually moved?
466 then we can't drop new files on this location */
467 return NSDragOperationNone;
469 return NSDragOperationGeneric;
471 return NSDragOperationNone;
474 - (BOOL)tableView:(NSTableView*)o_tv
475 acceptDrop:(id <NSDraggingInfo>)o_info
476 row:(int)i_proposed_row
477 dropOperation:(NSTableViewDropOperation)o_operation
479 if ( i_moveRow >= 0 )
481 if (i_moveRow != -1 && i_proposed_row != -1)
483 intf_thread_t * p_intf = [NSApp getIntf];
484 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
487 if( p_playlist == NULL )
493 playlist_Move( p_playlist, i_moveRow, i_proposed_row );
495 vlc_object_release( p_playlist );
497 [self playlistUpdated];
503 NSPasteboard * o_pasteboard;
504 o_pasteboard = [o_info draggingPasteboard];
506 if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
509 NSArray *o_array = [NSArray array];
510 NSArray *o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
511 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
513 for( i = 0; i < (int)[o_values count]; i++)
516 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
517 o_array = [o_array arrayByAddingObject: o_dic];
519 [self appendArray: o_array atPos: i_proposed_row enqueue:YES];
524 [self updateRowSelection];