1 /*****************************************************************************
2 * playlist.m: MacOS X interface plugin
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: playlist.m,v 1.35 2003/11/06 16:28:28 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 )
84 case NSDeleteCharacter:
85 case NSDeleteFunctionKey:
86 case NSDeleteCharFunctionKey:
87 case NSBackspaceCharacter:
88 o_to_delete = [NSMutableArray arrayWithArray:[[self selectedRowEnumerator] allObjects]];
89 c = [o_to_delete count];
91 for( i = 0; i < c; i++ ) {
92 o_number = [o_to_delete lastObject];
93 i_row = [o_number intValue];
95 if( p_playlist->i_index == i_row && p_playlist->i_status )
97 playlist_Stop( p_playlist );
99 [o_to_delete removeObject: o_number];
100 [self deselectRow: i_row];
101 playlist_Delete( p_playlist, i_row );
107 [super keyDown: o_event];
111 if( p_playlist != NULL )
113 vlc_object_release( p_playlist );
117 - (void)highlightSelectionInClipRect:(NSRect)o_rect {
119 float f_height = [self rowHeight] + [self intercellSpacing].height;
120 float f_origin_y = NSMaxY( o_rect );
121 int i_row = o_rect.origin.y / f_height;
123 if ( i_row % 2 == 0 )
128 o_new_rect.size.width = o_rect.size.width;
129 o_new_rect.size.height = f_height;
130 o_new_rect.origin.x = o_rect.origin.x;
131 o_new_rect.origin.y = i_row * f_height;
133 if( ( MacVersion102 < 0 ) && ( floor( NSAppKitVersionNumber ) > NSAppKitVersionNumber10_1 ) )
138 if ( MacVersion102 == 102 && o_striped_row_color == nil )
140 o_striped_row_color = [[[NSColor alternateSelectedControlColor]
141 highlightWithLevel: 0.90] retain];
144 else if ( o_striped_row_color == nil )
146 /* OSX 10.1 and before ain't that smart ;) */
147 o_striped_row_color = [[NSColor whiteColor] retain];
150 [o_striped_row_color set];
152 while ( o_new_rect.origin.y < f_origin_y ) {
153 NSRectFill( o_new_rect );
154 o_new_rect.origin.y += f_height * 2.0;
156 [super highlightSelectionInClipRect:o_rect];
161 /*****************************************************************************
162 * VLCPlaylist implementation
163 *****************************************************************************/
164 @implementation VLCPlaylist
178 [o_table_view setTarget: self];
179 [o_table_view setDelegate: self];
180 [o_table_view setDataSource: self];
182 [o_table_view setDoubleAction: @selector(playItem:)];
184 [o_table_view registerForDraggedTypes:
185 [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
187 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
188 [o_mi_play setTitle: _NS("Play")];
189 [o_mi_delete setTitle: _NS("Delete")];
190 [o_mi_selectall setTitle: _NS("Select All")];
192 [o_btn_add setToolTip: _NS("Add")];
193 [o_btn_remove setToolTip: _NS("Delete")];
196 - (BOOL)tableView:(NSTableView *)o_tv
197 shouldEditTableColumn:(NSTableColumn *)o_tc
203 - (NSMenu *)menuForEvent:(NSEvent *)o_event
207 vlc_bool_t b_item_sel;
209 pt = [o_table_view convertPoint: [o_event locationInWindow]
211 b_item_sel = ( [o_table_view rowAtPoint: pt] != -1 &&
212 [o_table_view selectedRow] != -1 );
213 b_rows = [o_table_view numberOfRows] != 0;
215 [o_mi_play setEnabled: b_item_sel];
216 [o_mi_delete setEnabled: b_item_sel];
217 [o_mi_selectall setEnabled: b_rows];
219 return( o_ctx_menu );
222 - (IBAction)savePlaylist:(id)sender
224 intf_thread_t * p_intf = [NSApp getIntf];
225 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
228 NSSavePanel *o_save_panel = [NSSavePanel savePanel];
229 NSString * o_name = [NSString stringWithFormat: @"%@.m3u", _NS("Untitled")];
230 [o_save_panel setTitle: _NS("Save Playlist")];
231 [o_save_panel setPrompt: _NS("Save")];
233 if( [o_save_panel runModalForDirectory: nil
234 file: o_name] == NSOKButton )
236 playlist_SaveFile( p_playlist, [[o_save_panel filename] fileSystemRepresentation] );
241 - (IBAction)playItem:(id)sender
243 intf_thread_t * p_intf = [NSApp getIntf];
244 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
247 if( p_playlist != NULL )
249 playlist_Goto( p_playlist, [o_table_view selectedRow] );
250 vlc_object_release( p_playlist );
254 - (IBAction)deleteItems:(id)sender
257 NSMutableArray *o_to_delete;
260 intf_thread_t * p_intf = [NSApp getIntf];
261 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
264 if( p_playlist == NULL )
269 o_to_delete = [NSMutableArray arrayWithArray:[[o_table_view selectedRowEnumerator] allObjects]];
270 c = (int)[o_to_delete count];
272 for( i = 0; i < c; i++ ) {
273 o_number = [o_to_delete lastObject];
274 i_row = [o_number intValue];
276 if( p_playlist->i_index == i_row && p_playlist->i_status )
278 playlist_Stop( p_playlist );
280 [o_to_delete removeObject: o_number];
281 [o_table_view deselectRow: i_row];
282 playlist_Delete( p_playlist, i_row );
285 vlc_object_release( p_playlist );
287 /* this is actually duplicity, because the intf.m manage also updates the view
288 * when the playlist changes. we do this on purpose, because else there is a
289 * delay of .5 sec or so when we delete an item */
290 [self playlistUpdated];
291 [self updateRowSelection];
294 - (IBAction)selectAll:(id)sender
296 [o_table_view selectAll: nil];
299 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
302 intf_thread_t * p_intf = [NSApp getIntf];
303 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
306 if( p_playlist == NULL )
311 for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
314 NSDictionary *o_one_item;
316 int i_total_options = 0;
317 int i_mode = PLAYLIST_INSERT;
318 BOOL b_rem = FALSE, b_dir = FALSE;
319 NSString *o_url, *o_name;
321 char **ppsz_options = NULL;
324 o_one_item = [o_array objectAtIndex: i_item];
325 o_url = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
326 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
327 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
329 /* If no name, then make a guess */
330 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_url];
332 if( [[NSFileManager defaultManager] fileExistsAtPath:o_url isDirectory:&b_dir] && b_dir &&
333 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_url isRemovable: &b_rem
334 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
336 /* All of this is to make sure CD's play when you D&D them on VLC */
337 /* Converts mountpoint to a /dev file */
339 char *psz_dev, *temp;
340 buf = (struct statfs *) malloc (sizeof(struct statfs));
341 statfs( [o_url fileSystemRepresentation], buf );
342 psz_dev = strdup(buf->f_mntfromname);
344 temp = strrchr( psz_dev , 's' );
345 psz_dev[temp - psz_dev] = '\0';
346 o_url = [NSString stringWithCString: psz_dev ];
350 if (i_item == 0 && !b_enqueue)
351 i_mode |= PLAYLIST_GO;
353 if( o_options && [o_options count] > 0 )
355 /* Count the input options */
356 i_total_options = [o_options count];
358 /* Allocate ppsz_options */
359 for( j = 0; j < i_total_options; j++ )
362 ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
364 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
368 playlist_AddExt( p_playlist, [o_url fileSystemRepresentation], [o_name UTF8String], -1,
369 (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options,
370 i_mode, i_position == -1 ? PLAYLIST_END : i_position + i_item);
373 for( j = 0; j < i_total_options; j++ )
374 free( ppsz_options[j] );
375 if( ppsz_options ) free( ppsz_options );
377 /* Recent documents menu */
378 NSURL *o_true_url = [NSURL fileURLWithPath: o_url];
379 if( o_true_url != nil )
381 [[NSDocumentController sharedDocumentController]
382 noteNewRecentDocumentURL: o_true_url];
386 vlc_object_release( p_playlist );
389 - (void)playlistUpdated
391 [o_table_view reloadData];
394 - (void)updateRowSelection
398 intf_thread_t * p_intf = [NSApp getIntf];
399 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
402 if( p_playlist == NULL )
407 i_row = p_playlist->i_index;
408 vlc_object_release( p_playlist );
410 [o_table_view selectRow: i_row byExtendingSelection: NO];
411 [o_table_view scrollRowToVisible: i_row];
416 @implementation VLCPlaylist (NSTableDataSource)
418 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
421 intf_thread_t * p_intf = [NSApp getIntf];
422 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
425 if( p_playlist != NULL )
427 vlc_mutex_lock( &p_playlist->object_lock );
428 i_count = p_playlist->i_size;
429 vlc_mutex_unlock( &p_playlist->object_lock );
430 vlc_object_release( p_playlist );
436 - (id)tableView:(NSTableView *)o_tv
437 objectValueForTableColumn:(NSTableColumn *)o_tc
441 intf_thread_t * p_intf = [NSApp getIntf];
442 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
445 if( p_playlist == NULL )
450 vlc_mutex_lock( &p_playlist->object_lock );
451 o_value = [[NSString stringWithUTF8String:
452 p_playlist->pp_items[i_row]->psz_name] lastPathComponent];
453 vlc_mutex_unlock( &p_playlist->object_lock );
455 vlc_object_release( p_playlist );
460 - (BOOL)tableView:(NSTableView *)o_tv
461 writeRows:(NSArray*)o_rows
462 toPasteboard:(NSPasteboard*)o_pasteboard
464 int i_rows = [o_rows count];
465 NSArray *o_filenames = [NSArray array];
467 [o_pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
468 [o_pasteboard setPropertyList:o_filenames forType:NSFilenamesPboardType];
471 i_moveRow = [[o_rows objectAtIndex:0]intValue];
477 - (NSDragOperation)tableView:(NSTableView*)o_tv
478 validateDrop:(id <NSDraggingInfo>)o_info
479 proposedRow:(int)i_row
480 proposedDropOperation:(NSTableViewDropOperation)o_operation
482 if ( o_operation == NSTableViewDropAbove )
484 if ( i_moveRow >= 0 )
486 if ( i_row != i_moveRow )
488 return NSDragOperationMove;
490 /* what if in the previous run, the row wasn't actually moved?
491 then we can't drop new files on this location */
492 return NSDragOperationNone;
494 return NSDragOperationGeneric;
496 return NSDragOperationNone;
499 - (BOOL)tableView:(NSTableView*)o_tv
500 acceptDrop:(id <NSDraggingInfo>)o_info
501 row:(int)i_proposed_row
502 dropOperation:(NSTableViewDropOperation)o_operation
504 if ( i_moveRow >= 0 )
506 if (i_moveRow != -1 && i_proposed_row != -1)
508 intf_thread_t * p_intf = [NSApp getIntf];
509 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
512 if( p_playlist == NULL )
518 playlist_Move( p_playlist, i_moveRow, i_proposed_row );
520 vlc_object_release( p_playlist );
522 [self playlistUpdated];
528 NSPasteboard * o_pasteboard;
529 o_pasteboard = [o_info draggingPasteboard];
531 if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
534 NSArray *o_array = [NSArray array];
535 NSArray *o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
536 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
538 for( i = 0; i < (int)[o_values count]; i++)
541 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
542 o_array = [o_array arrayByAddingObject: o_dic];
544 [self appendArray: o_array atPos: i_proposed_row enqueue:YES];
549 [self updateRowSelection];