1 /*****************************************************************************
2 * playlist.m: MacOS X interface plugin
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: playlist.m,v 1.34 2003/10/19 23:12:16 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 int MacVersion102 = -1;
40 /*****************************************************************************
41 * VLCPlaylistView implementation
42 *****************************************************************************/
43 @implementation VLCPlaylistView
47 if( o_striped_row_color != nil )
49 [o_striped_row_color release];
54 - (NSMenu *)menuForEvent:(NSEvent *)o_event
56 return( [[self delegate] menuForEvent: o_event] );
59 - (void)keyDown:(NSEvent *)o_event
63 NSMutableArray *o_to_delete;
66 playlist_t * p_playlist;
67 intf_thread_t * p_intf = [NSApp getIntf];
69 if( [[o_event characters] length] )
71 key = [[o_event characters] characterAtIndex: 0];
74 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
77 if ( p_playlist == NULL )
85 [(VLCControls *)[[NSApp delegate] getControls] play: nil];
88 case NSDeleteCharacter:
89 case NSDeleteFunctionKey:
90 case NSDeleteCharFunctionKey:
91 case NSBackspaceCharacter:
92 o_to_delete = [NSMutableArray arrayWithArray:[[self selectedRowEnumerator] allObjects]];
93 c = [o_to_delete count];
95 for( i = 0; i < c; i++ ) {
96 o_number = [o_to_delete lastObject];
97 i_row = [o_number intValue];
99 if( p_playlist->i_index == i_row && p_playlist->i_status )
101 playlist_Stop( p_playlist );
103 [o_to_delete removeObject: o_number];
104 [self deselectRow: i_row];
105 playlist_Delete( p_playlist, i_row );
111 [super keyDown: o_event];
115 if( p_playlist != NULL )
117 vlc_object_release( p_playlist );
121 - (void)highlightSelectionInClipRect:(NSRect)o_rect {
123 float f_height = [self rowHeight] + [self intercellSpacing].height;
124 float f_origin_y = NSMaxY( o_rect );
125 int i_row = o_rect.origin.y / f_height;
127 if ( i_row % 2 == 0 )
132 o_new_rect.size.width = o_rect.size.width;
133 o_new_rect.size.height = f_height;
134 o_new_rect.origin.x = o_rect.origin.x;
135 o_new_rect.origin.y = i_row * f_height;
137 if( ( MacVersion102 < 0 ) && ( floor( NSAppKitVersionNumber ) > NSAppKitVersionNumber10_1 ) )
142 if ( MacVersion102 == 102 && o_striped_row_color == nil )
144 o_striped_row_color = [[[NSColor alternateSelectedControlColor]
145 highlightWithLevel: 0.90] retain];
148 else if ( o_striped_row_color == nil )
150 /* OSX 10.1 and before ain't that smart ;) */
151 o_striped_row_color = [[NSColor whiteColor] retain];
154 [o_striped_row_color set];
156 while ( o_new_rect.origin.y < f_origin_y ) {
157 NSRectFill( o_new_rect );
158 o_new_rect.origin.y += f_height * 2.0;
160 [super highlightSelectionInClipRect:o_rect];
165 /*****************************************************************************
166 * VLCPlaylist implementation
167 *****************************************************************************/
168 @implementation VLCPlaylist
182 [o_table_view setTarget: self];
183 [o_table_view setDelegate: self];
184 [o_table_view setDataSource: self];
186 [o_table_view setDoubleAction: @selector(playItem:)];
188 [o_table_view registerForDraggedTypes:
189 [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
191 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
192 [o_mi_play setTitle: _NS("Play")];
193 [o_mi_delete setTitle: _NS("Delete")];
194 [o_mi_selectall setTitle: _NS("Select All")];
196 [o_btn_add setToolTip: _NS("Add")];
197 [o_btn_remove setToolTip: _NS("Delete")];
200 - (BOOL)tableView:(NSTableView *)o_tv
201 shouldEditTableColumn:(NSTableColumn *)o_tc
207 - (NSMenu *)menuForEvent:(NSEvent *)o_event
211 vlc_bool_t b_item_sel;
213 pt = [o_table_view convertPoint: [o_event locationInWindow]
215 b_item_sel = ( [o_table_view rowAtPoint: pt] != -1 &&
216 [o_table_view selectedRow] != -1 );
217 b_rows = [o_table_view numberOfRows] != 0;
219 [o_mi_play setEnabled: b_item_sel];
220 [o_mi_delete setEnabled: b_item_sel];
221 [o_mi_selectall setEnabled: b_rows];
223 return( o_ctx_menu );
226 - (IBAction)savePlaylist:(id)sender
228 intf_thread_t * p_intf = [NSApp getIntf];
229 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
232 NSSavePanel *o_save_panel = [NSSavePanel savePanel];
233 NSString * o_name = [NSString stringWithFormat: @"%@.m3u", _NS("Untitled")];
234 [o_save_panel setTitle: _NS("Save Playlist")];
235 [o_save_panel setPrompt: _NS("Save")];
237 if( [o_save_panel runModalForDirectory: nil
238 file: o_name] == NSOKButton )
240 playlist_SaveFile( p_playlist, [[o_save_panel filename] fileSystemRepresentation] );
245 - (IBAction)playItem:(id)sender
247 intf_thread_t * p_intf = [NSApp getIntf];
248 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
251 if( p_playlist != NULL )
253 playlist_Goto( p_playlist, [o_table_view selectedRow] );
254 vlc_object_release( p_playlist );
258 - (IBAction)deleteItems:(id)sender
261 NSMutableArray *o_to_delete;
264 intf_thread_t * p_intf = [NSApp getIntf];
265 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
268 if( p_playlist == NULL )
273 o_to_delete = [NSMutableArray arrayWithArray:[[o_table_view selectedRowEnumerator] allObjects]];
274 c = (int)[o_to_delete count];
276 for( i = 0; i < c; i++ ) {
277 o_number = [o_to_delete lastObject];
278 i_row = [o_number intValue];
280 if( p_playlist->i_index == i_row && p_playlist->i_status )
282 playlist_Stop( p_playlist );
284 [o_to_delete removeObject: o_number];
285 [o_table_view deselectRow: i_row];
286 playlist_Delete( p_playlist, i_row );
289 vlc_object_release( p_playlist );
291 /* this is actually duplicity, because the intf.m manage also updates the view
292 * when the playlist changes. we do this on purpose, because else there is a
293 * delay of .5 sec or so when we delete an item */
294 [self playlistUpdated];
295 [self updateRowSelection];
298 - (IBAction)selectAll:(id)sender
300 [o_table_view selectAll: nil];
303 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
306 intf_thread_t * p_intf = [NSApp getIntf];
307 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
310 if( p_playlist == NULL )
315 for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
318 NSDictionary *o_one_item;
320 int i_total_options = 0;
321 int i_mode = PLAYLIST_INSERT;
322 BOOL b_rem = FALSE, b_dir = FALSE;
323 NSString *o_url, *o_name;
325 char **ppsz_options = NULL;
328 o_one_item = [o_array objectAtIndex: i_item];
329 o_url = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
330 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
331 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
333 /* If no name, then make a guess */
334 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_url];
336 if( [[NSFileManager defaultManager] fileExistsAtPath:o_url isDirectory:&b_dir] && b_dir &&
337 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_url isRemovable: &b_rem
338 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
340 /* All of this is to make sure CD's play when you D&D them on VLC */
341 /* Converts mountpoint to a /dev file */
343 char *psz_dev, *temp;
344 buf = (struct statfs *) malloc (sizeof(struct statfs));
345 statfs( [o_url fileSystemRepresentation], buf );
346 psz_dev = strdup(buf->f_mntfromname);
348 temp = strrchr( psz_dev , 's' );
349 psz_dev[temp - psz_dev] = '\0';
350 o_url = [NSString stringWithCString: psz_dev ];
354 if (i_item == 0 && !b_enqueue)
355 i_mode |= PLAYLIST_GO;
357 if( o_options && [o_options count] > 0 )
359 /* Count the input options */
360 i_total_options = [o_options count];
362 /* Allocate ppsz_options */
363 for( j = 0; j < i_total_options; j++ )
366 ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
368 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
372 playlist_AddExt( p_playlist, [o_url fileSystemRepresentation], [o_name UTF8String], -1,
373 (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options,
374 i_mode, i_position == -1 ? PLAYLIST_END : i_position + i_item);
377 for( j = 0; j < i_total_options; j++ )
378 free( ppsz_options[j] );
379 if( ppsz_options ) free( ppsz_options );
381 /* Recent documents menu */
382 NSURL *o_true_url = [NSURL fileURLWithPath: o_url];
383 if( o_true_url != nil )
385 [[NSDocumentController sharedDocumentController]
386 noteNewRecentDocumentURL: o_true_url];
390 vlc_object_release( p_playlist );
393 - (void)playlistUpdated
395 [o_table_view reloadData];
398 - (void)updateRowSelection
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 i_row = p_playlist->i_index;
412 vlc_object_release( p_playlist );
414 [o_table_view selectRow: i_row byExtendingSelection: NO];
415 [o_table_view scrollRowToVisible: i_row];
420 @implementation VLCPlaylist (NSTableDataSource)
422 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
425 intf_thread_t * p_intf = [NSApp getIntf];
426 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
429 if( p_playlist != NULL )
431 vlc_mutex_lock( &p_playlist->object_lock );
432 i_count = p_playlist->i_size;
433 vlc_mutex_unlock( &p_playlist->object_lock );
434 vlc_object_release( p_playlist );
440 - (id)tableView:(NSTableView *)o_tv
441 objectValueForTableColumn:(NSTableColumn *)o_tc
445 intf_thread_t * p_intf = [NSApp getIntf];
446 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
449 if( p_playlist == NULL )
454 vlc_mutex_lock( &p_playlist->object_lock );
455 o_value = [[NSString stringWithUTF8String:
456 p_playlist->pp_items[i_row]->psz_name] lastPathComponent];
457 vlc_mutex_unlock( &p_playlist->object_lock );
459 vlc_object_release( p_playlist );
464 - (BOOL)tableView:(NSTableView *)o_tv
465 writeRows:(NSArray*)o_rows
466 toPasteboard:(NSPasteboard*)o_pasteboard
468 int i_rows = [o_rows count];
469 NSArray *o_filenames = [NSArray array];
471 [o_pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
472 [o_pasteboard setPropertyList:o_filenames forType:NSFilenamesPboardType];
475 i_moveRow = [[o_rows objectAtIndex:0]intValue];
481 - (NSDragOperation)tableView:(NSTableView*)o_tv
482 validateDrop:(id <NSDraggingInfo>)o_info
483 proposedRow:(int)i_row
484 proposedDropOperation:(NSTableViewDropOperation)o_operation
486 if ( o_operation == NSTableViewDropAbove )
488 if ( i_moveRow >= 0 )
490 if ( i_row != i_moveRow )
492 return NSDragOperationMove;
494 /* what if in the previous run, the row wasn't actually moved?
495 then we can't drop new files on this location */
496 return NSDragOperationNone;
498 return NSDragOperationGeneric;
500 return NSDragOperationNone;
503 - (BOOL)tableView:(NSTableView*)o_tv
504 acceptDrop:(id <NSDraggingInfo>)o_info
505 row:(int)i_proposed_row
506 dropOperation:(NSTableViewDropOperation)o_operation
508 if ( i_moveRow >= 0 )
510 if (i_moveRow != -1 && i_proposed_row != -1)
512 intf_thread_t * p_intf = [NSApp getIntf];
513 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
516 if( p_playlist == NULL )
522 playlist_Move( p_playlist, i_moveRow, i_proposed_row );
524 vlc_object_release( p_playlist );
526 [self playlistUpdated];
532 NSPasteboard * o_pasteboard;
533 o_pasteboard = [o_info draggingPasteboard];
535 if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
538 NSArray *o_array = [NSArray array];
539 NSArray *o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
540 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
542 for( i = 0; i < (int)[o_values count]; i++)
545 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
546 o_array = [o_array arrayByAddingObject: o_dic];
548 [self appendArray: o_array atPos: i_proposed_row enqueue:YES];
553 [self updateRowSelection];