1 /*****************************************************************************
2 * playlist.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2002-2004 VideoLAN
5 * $Id: playlist.m,v 1.57 2004/02/08 20:08:28 hartman Exp $
7 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8 * Derk-Jan Hartman <hartman at videolan dot org>
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 );
110 /*****************************************************************************
111 * VLCPlaylist implementation
112 *****************************************************************************/
113 @implementation VLCPlaylist
127 [o_table_view setTarget: self];
128 [o_table_view setDelegate: self];
129 [o_table_view setDataSource: self];
131 [o_table_view setDoubleAction: @selector(playItem:)];
133 [o_table_view registerForDraggedTypes:
134 [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
136 [o_window setExcludedFromWindowsMenu: TRUE];
138 /* We need to check whether _defaultTableHeaderSortImage exists, since it
139 belongs to an Apple hidden private API, and then can "disapear" at any time*/
141 if( [[NSTableView class] respondsToSelector:@selector(_defaultTableHeaderSortImage)] )
143 o_ascendingSortingImage = [[NSTableView class] _defaultTableHeaderSortImage];
147 o_ascendingSortingImage = nil;
150 if( [[NSTableView class] respondsToSelector:@selector(_defaultTableHeaderReverseSortImage)] )
152 o_descendingSortingImage = [[NSTableView class] _defaultTableHeaderReverseSortImage];
156 o_descendingSortingImage = nil;
164 [o_window setTitle: _NS("Playlist")];
165 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
166 [o_mi_play setTitle: _NS("Play")];
167 [o_mi_delete setTitle: _NS("Delete")];
168 [o_mi_selectall setTitle: _NS("Select All")];
169 [[o_tc_name headerCell] setStringValue:_NS("Name")];
170 [[o_tc_author headerCell] setStringValue:_NS("Author")];
171 [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
172 [o_random_ckb setTitle: _NS("Random")];
173 [o_loop_ckb setTitle: _NS("Repeat All")];
174 [o_repeat_ckb setTitle: _NS("Repeat One")];
175 [o_search_button setTitle: _NS("Search")];
176 [o_btn_playlist setToolTip: _NS("Playlist")];
179 - (void) tableView:(NSTableView*)o_tv
180 didClickTableColumn:(NSTableColumn *)o_tc
182 intf_thread_t * p_intf = [NSApp getIntf];
183 playlist_t *p_playlist =
184 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
187 int max = [[o_table_view tableColumns] count];
190 if( p_playlist == NULL )
195 if( o_tc_sortColumn == o_tc )
197 b_isSortDescending = !b_isSortDescending;
199 else if( o_tc == o_tc_name || o_tc == o_tc_author ||
202 b_isSortDescending = VLC_FALSE;
203 [o_table_view setHighlightedTableColumn:o_tc];
204 o_tc_sortColumn = o_tc;
205 for( i=0 ; i<max ; i++ )
207 [o_table_view setIndicatorImage:nil inTableColumn:[[o_table_view tableColumns] objectAtIndex:i]];
211 if( o_tc_id == o_tc && !b_isSortDescending )
213 playlist_SortID( p_playlist , ORDER_NORMAL );
214 [o_table_view setIndicatorImage:o_ascendingSortingImage inTableColumn:o_tc];
216 else if( o_tc_name == o_tc && !b_isSortDescending )
218 playlist_SortTitle( p_playlist , ORDER_NORMAL );
219 [o_table_view setIndicatorImage:o_ascendingSortingImage inTableColumn:o_tc];
221 else if( o_tc_author == o_tc && !b_isSortDescending )
223 playlist_SortAuthor( p_playlist , ORDER_NORMAL );
224 [o_table_view setIndicatorImage:o_ascendingSortingImage inTableColumn:o_tc];
226 else if( o_tc_id == o_tc && b_isSortDescending )
228 playlist_SortID( p_playlist , ORDER_REVERSE );
229 [o_table_view setIndicatorImage:o_ascendingSortingImage inTableColumn:o_tc];
231 else if( o_tc_name == o_tc && b_isSortDescending )
233 playlist_SortTitle( p_playlist , ORDER_REVERSE );
234 [o_table_view setIndicatorImage:o_descendingSortingImage inTableColumn:o_tc];
236 else if( o_tc_author == o_tc && b_isSortDescending )
238 playlist_SortAuthor( p_playlist , ORDER_REVERSE );
239 [o_table_view setIndicatorImage:o_descendingSortingImage inTableColumn:o_tc];
241 vlc_object_release( p_playlist );
242 [self playlistUpdated];
246 - (BOOL)tableView:(NSTableView *)o_tv
247 shouldEditTableColumn:(NSTableColumn *)o_tc
253 - (NSMenu *)menuForEvent:(NSEvent *)o_event
257 vlc_bool_t b_item_sel;
259 pt = [o_table_view convertPoint: [o_event locationInWindow]
261 b_item_sel = ( [o_table_view rowAtPoint: pt] != -1 &&
262 [o_table_view selectedRow] != -1 );
263 b_rows = [o_table_view numberOfRows] != 0;
265 [o_mi_play setEnabled: b_item_sel];
266 [o_mi_delete setEnabled: b_item_sel];
267 [o_mi_selectall setEnabled: b_rows];
269 return( o_ctx_menu );
272 - (IBAction)toggleWindow:(id)sender
274 if( [o_window isVisible] )
276 [o_window orderOut:sender];
277 [o_btn_playlist setState:NSOffState];
281 [o_window makeKeyAndOrderFront:sender];
282 [o_btn_playlist setState:NSOnState];
286 - (IBAction)savePlaylist:(id)sender
288 intf_thread_t * p_intf = [NSApp getIntf];
289 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
292 NSSavePanel *o_save_panel = [NSSavePanel savePanel];
293 NSString * o_name = [NSString stringWithFormat: @"%@.m3u", _NS("Untitled")];
294 [o_save_panel setTitle: _NS("Save Playlist")];
295 [o_save_panel setPrompt: _NS("Save")];
297 if( [o_save_panel runModalForDirectory: nil
298 file: o_name] == NSOKButton )
300 playlist_Export( p_playlist, [[o_save_panel filename] fileSystemRepresentation], "m3u" );
305 - (IBAction)playItem:(id)sender
307 intf_thread_t * p_intf = [NSApp getIntf];
308 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
311 if( p_playlist != NULL )
313 playlist_Goto( p_playlist, [o_table_view selectedRow] );
314 vlc_object_release( p_playlist );
318 - (IBAction)deleteItems:(id)sender
321 NSMutableArray *o_to_delete;
324 intf_thread_t * p_intf = [NSApp getIntf];
325 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
328 if( p_playlist == NULL )
333 o_to_delete = [NSMutableArray arrayWithArray:[[o_table_view selectedRowEnumerator] allObjects]];
334 c = (int)[o_to_delete count];
336 for( i = 0; i < c; i++ ) {
337 o_number = [o_to_delete lastObject];
338 i_row = [o_number intValue];
340 if( p_playlist->i_index == i_row && p_playlist->i_status )
342 playlist_Stop( p_playlist );
344 [o_to_delete removeObject: o_number];
345 [o_table_view deselectRow: i_row];
346 playlist_Delete( p_playlist, i_row );
349 vlc_object_release( p_playlist );
351 /* this is actually duplicity, because the intf.m manage also updates the view
352 * when the playlist changes. we do this on purpose, because else there is a
353 * delay of .5 sec or so when we delete an item */
354 [self playlistUpdated];
355 [self updateRowSelection];
358 - (IBAction)selectAll:(id)sender
360 [o_table_view selectAll: nil];
364 - (IBAction)searchItem:(id)sender
367 NSString *o_current_name;
368 NSString *o_current_author;
370 intf_thread_t * p_intf = [NSApp getIntf];
371 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
374 if( p_playlist == NULL )
378 if( [o_table_view numberOfRows] < 1 )
383 if( [o_table_view selectedRow] == [o_table_view numberOfRows]-1 )
389 i_current = [o_table_view selectedRow];
396 vlc_mutex_lock( &p_playlist->object_lock );
397 o_current_name = [NSString stringWithUTF8String:
398 p_playlist->pp_items[i_current]->psz_name];
399 o_current_author = [NSString stringWithUTF8String:
400 playlist_GetInfo(p_playlist, i_current ,_("General"),_("Author") )];
401 vlc_mutex_unlock( &p_playlist->object_lock );
404 if( [o_current_name rangeOfString:[o_search_keyword stringValue] options:NSCaseInsensitiveSearch ].length ||
405 [o_current_author rangeOfString:[o_search_keyword stringValue] options:NSCaseInsensitiveSearch ].length )
407 [o_table_view selectRow: i_current byExtendingSelection: NO];
408 [o_table_view scrollRowToVisible: i_current];
411 if( i_current == [o_table_view numberOfRows] - 1 )
416 while (i_current != [o_table_view selectedRow]);
417 vlc_object_release( p_playlist );
421 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
424 intf_thread_t * p_intf = [NSApp getIntf];
425 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
428 if( p_playlist == NULL )
433 for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
436 NSDictionary *o_one_item;
437 int j, i_total_options = 0, i_new_id = -1;
438 int i_mode = PLAYLIST_INSERT;
439 BOOL b_rem = FALSE, b_dir = FALSE;
440 NSString *o_uri, *o_name;
443 char **ppsz_options = NULL;
446 o_one_item = [o_array objectAtIndex: i_item];
447 o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
448 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
449 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
451 /* If no name, then make a guess */
452 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
454 if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
455 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
456 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
458 /* All of this is to make sure CD's play when you D&D them on VLC */
459 /* Converts mountpoint to a /dev file */
462 buf = (struct statfs *) malloc (sizeof(struct statfs));
463 statfs( [o_uri fileSystemRepresentation], buf );
464 psz_dev = strdup(buf->f_mntfromname);
465 o_uri = [NSString stringWithCString: psz_dev ];
468 if( o_options && [o_options count] > 0 )
470 /* Count the input options */
471 i_total_options = [o_options count];
473 /* Allocate ppsz_options */
474 for( j = 0; j < i_total_options; j++ )
477 ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
479 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
484 i_new_id = playlist_AddExt( p_playlist, [o_uri fileSystemRepresentation],
485 [o_name UTF8String], i_mode,
486 i_position == -1 ? PLAYLIST_END : i_position + i_item,
487 0, (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options );
490 for( j = 0; j < i_total_options; j++ )
491 free( ppsz_options[j] );
492 if( ppsz_options ) free( ppsz_options ); */
494 /* Recent documents menu */
495 o_true_file = [NSURL fileURLWithPath: o_uri];
496 if( o_true_file != nil )
498 [[NSDocumentController sharedDocumentController]
499 noteNewRecentDocumentURL: o_true_file];
502 if( i_item == 0 && !b_enqueue )
504 playlist_Goto( p_playlist, playlist_GetPositionById( p_playlist, i_new_id ) );
505 playlist_Play( p_playlist );
509 vlc_object_release( p_playlist );
512 - (void)playlistUpdated
515 intf_thread_t * p_intf = [NSApp getIntf];
516 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
518 if( p_playlist != NULL )
520 var_Get( p_playlist, "random", &val );
521 [o_random_ckb setState: val.b_bool];
523 var_Get( p_playlist, "loop", &val );
524 [o_loop_ckb setState: val.b_bool];
526 var_Get( p_playlist, "repeat", &val );
527 [o_repeat_ckb setState: val.b_bool];
529 vlc_object_release( p_playlist );
531 [o_table_view reloadData];
534 - (void)updateRowSelection
538 intf_thread_t * p_intf = [NSApp getIntf];
539 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
542 if( p_playlist == NULL )
547 i_row = p_playlist->i_index;
548 vlc_object_release( p_playlist );
550 [o_table_view selectRow: i_row byExtendingSelection: NO];
551 [o_table_view scrollRowToVisible: i_row];
557 @implementation VLCPlaylist (NSTableDataSource)
559 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
562 intf_thread_t * p_intf = [NSApp getIntf];
563 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
566 if( p_playlist != NULL )
568 vlc_mutex_lock( &p_playlist->object_lock );
569 i_count = p_playlist->i_size;
570 vlc_mutex_unlock( &p_playlist->object_lock );
571 vlc_object_release( p_playlist );
573 [o_status_field setStringValue: [NSString stringWithFormat:_NS("%i items in playlist"), i_count]];
577 - (id)tableView:(NSTableView *)o_tv
578 objectValueForTableColumn:(NSTableColumn *)o_tc
582 intf_thread_t * p_intf = [NSApp getIntf];
583 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
586 if( p_playlist == NULL )
591 if( [[o_tc identifier] isEqualToString:@"0"] )
593 o_value = [NSString stringWithFormat:@"%i", i_row + 1];
595 else if( [[o_tc identifier] isEqualToString:@"1"] )
597 vlc_mutex_lock( &p_playlist->object_lock );
598 o_value = [NSString stringWithUTF8String:
599 p_playlist->pp_items[i_row]->psz_name];
600 vlc_mutex_unlock( &p_playlist->object_lock );
602 else if( [[o_tc identifier] isEqualToString:@"2"] )
604 vlc_mutex_lock( &p_playlist->object_lock );
605 o_value = [NSString stringWithUTF8String:
606 playlist_GetInfo(p_playlist, i_row ,_("General"),_("Author") )];
607 vlc_mutex_unlock( &p_playlist->object_lock );
609 else if( [[o_tc identifier] isEqualToString:@"3"] )
611 char psz_duration[MSTRTIME_MAX_SIZE];
612 mtime_t dur = p_playlist->pp_items[i_row]->i_duration;
615 secstotimestr( psz_duration, dur/1000000 );
616 o_value = [NSString stringWithUTF8String: psz_duration];
620 o_value = @"-:--:--";
624 vlc_object_release( p_playlist );
629 - (BOOL)tableView:(NSTableView *)o_tv
630 writeRows:(NSArray*)o_rows
631 toPasteboard:(NSPasteboard*)o_pasteboard
633 int i_rows = [o_rows count];
634 NSArray *o_filenames = [NSArray array];
636 [o_pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
637 [o_pasteboard setPropertyList:o_filenames forType:NSFilenamesPboardType];
640 i_moveRow = [[o_rows objectAtIndex:0]intValue];
646 - (NSDragOperation)tableView:(NSTableView*)o_tv
647 validateDrop:(id <NSDraggingInfo>)o_info
648 proposedRow:(int)i_row
649 proposedDropOperation:(NSTableViewDropOperation)o_operation
651 if ( o_operation == NSTableViewDropAbove )
653 if ( i_moveRow >= 0 )
655 if ( i_row != i_moveRow )
657 return NSDragOperationMove;
659 /* what if in the previous run, the row wasn't actually moved?
660 then we can't drop new files on this location */
661 return NSDragOperationNone;
663 return NSDragOperationGeneric;
665 return NSDragOperationNone;
668 - (BOOL)tableView:(NSTableView*)o_tv
669 acceptDrop:(id <NSDraggingInfo>)o_info
670 row:(int)i_proposed_row
671 dropOperation:(NSTableViewDropOperation)o_operation
673 if ( i_moveRow >= 0 )
675 if (i_moveRow != -1 && i_proposed_row != -1)
677 intf_thread_t * p_intf = [NSApp getIntf];
678 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
681 if( p_playlist == NULL )
687 playlist_Move( p_playlist, i_moveRow, i_proposed_row );
689 vlc_object_release( p_playlist );
691 [self playlistUpdated];
697 NSPasteboard * o_pasteboard;
698 o_pasteboard = [o_info draggingPasteboard];
700 if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
703 NSArray *o_array = [NSArray array];
704 NSArray *o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
705 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
707 for( i = 0; i < (int)[o_values count]; i++)
710 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
711 o_array = [o_array arrayByAddingObject: o_dic];
713 [self appendArray: o_array atPos: i_proposed_row enqueue:YES];
718 [self updateRowSelection];
721 /* Delegate method of NSWindow */
722 - (void)windowWillClose:(NSNotification *)aNotification
724 [o_btn_playlist setState: NSOffState];