1 /*****************************************************************************
2 * playlist.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2002-2014 VLC authors and VideoLAN
7 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8 * Derk-Jan Hartman <hartman at videola/n dot org>
9 * Benjamin Pracht <bigben at videolab dot org>
10 * Felix Paul Kühne <fkuehne at videolan dot org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
28 * add 'icons' for different types of nodes? (http://www.cocoadev.com/index.pl?IconAndTextInTableCell)
29 * reimplement enable/disable item
33 /*****************************************************************************
35 *****************************************************************************/
36 #include <stdlib.h> /* malloc(), free() */
37 #include <sys/param.h> /* for MAXPATHLEN */
40 #include <sys/mount.h>
42 #import "CompatibilityFixes.h"
47 #import "playlistinfo.h"
53 #import "CoreInteraction.h"
56 #import <vlc_interface.h>
59 /*****************************************************************************
60 * VLCPlaylistView implementation
61 *****************************************************************************/
62 @implementation VLCPlaylistView
64 - (NSMenu *)menuForEvent:(NSEvent *)o_event
66 return([(VLCPlaylist *)[self delegate] menuForEvent: o_event]);
69 - (void)keyDown:(NSEvent *)o_event
73 if ([[o_event characters] length])
74 key = [[o_event characters] characterAtIndex: 0];
77 case NSDeleteCharacter:
78 case NSDeleteFunctionKey:
79 case NSDeleteCharFunctionKey:
80 case NSBackspaceCharacter:
81 [(VLCPlaylist *)[self delegate] deleteItem:self];
84 case NSEnterCharacter:
85 case NSCarriageReturnCharacter:
86 [(VLCPlaylist *)[[VLCMain sharedInstance] playlist] playItem:nil];
90 [super keyDown: o_event];
95 - (BOOL)validateMenuItem:(NSMenuItem *)item
97 if (([self numberOfSelectedRows] >= 1 && [item action] == @selector(delete:)) || [item action] == @selector(selectAll:))
103 - (BOOL)acceptsFirstResponder
108 - (BOOL)becomeFirstResponder
110 [self setNeedsDisplay:YES];
114 - (BOOL)resignFirstResponder
116 [self setNeedsDisplay:YES];
120 - (IBAction)delete:(id)sender
122 [[[VLCMain sharedInstance] playlist] deleteItem: sender];
127 /*****************************************************************************
128 * VLCPlaylistWizard implementation
129 *****************************************************************************/
130 @implementation VLCPlaylistWizard
132 - (IBAction)reloadOutlineView
134 /* Only reload the outlineview if the wizard window is open since this can
135 be quite long on big playlists */
143 /*****************************************************************************
144 * An extension to NSOutlineView's interface to fix compilation warnings
145 * and let us access these 2 functions properly.
146 * This uses a private API, but works fine on all current OSX releases.
147 * Radar ID 11739459 request a public API for this. However, it is probably
148 * easier and faster to recreate similar looking bitmaps ourselves.
149 *****************************************************************************/
151 @interface NSOutlineView (UndocumentedSortImages)
152 + (NSImage *)_defaultTableHeaderSortImage;
153 + (NSImage *)_defaultTableHeaderReverseSortImage;
157 /*****************************************************************************
158 * VLCPlaylist implementation
159 *****************************************************************************/
160 @interface VLCPlaylist ()
162 NSImage *o_descendingSortingImage;
163 NSImage *o_ascendingSortingImage;
165 BOOL b_selected_item_met;
166 BOOL b_isSortDescending;
168 NSUInteger retainedRowSelection;
170 BOOL b_playlistmenu_nib_loaded;
174 - (void)saveTableColumns;
177 @implementation VLCPlaylist
181 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
182 NSMutableArray *o_columnArray = [[NSMutableArray alloc] init];
183 [o_columnArray addObject: [NSArray arrayWithObjects:TITLE_COLUMN, [NSNumber numberWithFloat:190.], nil]];
184 [o_columnArray addObject: [NSArray arrayWithObjects:ARTIST_COLUMN, [NSNumber numberWithFloat:95.], nil]];
185 [o_columnArray addObject: [NSArray arrayWithObjects:DURATION_COLUMN, [NSNumber numberWithFloat:95.], nil]];
187 NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
188 [NSArray arrayWithArray:o_columnArray], @"PlaylistColumnSelection",
189 [NSArray array], @"recentlyPlayedMediaList",
190 [NSDictionary dictionary], @"recentlyPlayedMedia", nil];
192 [defaults registerDefaults:appDefaults];
193 [o_columnArray release];
205 if (config_GetInt(VLCIntf, "macosx-large-text")) {
206 fontToUse = [NSFont systemFontOfSize:13.];
209 fontToUse = [NSFont systemFontOfSize:11.];
213 NSArray *columns = [o_outline_view tableColumns];
214 NSUInteger count = columns.count;
215 for (NSUInteger x = 0; x < count; x++)
216 [[[columns objectAtIndex:x] dataCell] setFont:fontToUse];
217 [o_outline_view setRowHeight:rowHeight];
230 playlist_t * p_playlist = pl_Get(VLCIntf);
235 o_model = [[PLModel alloc] initWithOutlineView:o_outline_view playlist:p_playlist rootItem:p_playlist->p_playing playlistObject:self];
236 [o_outline_view setDataSource:o_model];
237 [o_outline_view reloadData];
239 [o_outline_view setTarget: self];
240 [o_outline_view setDoubleAction: @selector(playItem:)];
242 [o_outline_view setAllowsEmptySelection: NO];
243 [o_outline_view registerForDraggedTypes: [NSArray arrayWithObjects:NSFilenamesPboardType, @"VLCPlaylistItemPboardType", nil]];
244 [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
246 /* This uses a private API, but works fine on all current OSX releases.
247 * Radar ID 11739459 request a public API for this. However, it is probably
248 * easier and faster to recreate similar looking bitmaps ourselves. */
249 o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
250 o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
252 o_tc_sortColumn = nil;
254 NSArray * o_columnArray = [[NSUserDefaults standardUserDefaults] arrayForKey:@"PlaylistColumnSelection"];
255 NSUInteger count = [o_columnArray count];
257 id o_menu = [[VLCMain sharedInstance] mainMenu];
260 NSMenu *o_context_menu = [o_menu setupPlaylistTableColumnsMenu];
261 [o_playlist_header setMenu: o_context_menu];
263 for (NSUInteger i = 0; i < count; i++) {
264 o_column = [[o_columnArray objectAtIndex:i] objectAtIndex:0];
265 if ([o_column isEqualToString:@"status"])
268 if(![o_menu setPlaylistColumnTableState: NSOnState forColumn: o_column])
271 [[o_outline_view tableColumnWithIdentifier: o_column] setWidth: [[[o_columnArray objectAtIndex:i] objectAtIndex:1] floatValue]];
274 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(applicationWillTerminate:) name: NSApplicationWillTerminateNotification object: nil];
279 - (void)applicationWillTerminate:(NSNotification *)notification
281 /* let's make sure we save the correct widths and positions, since this likely changed since the last time the user played with the column selection */
282 [self saveTableColumns];
287 [o_mi_play setTitle: _NS("Play")];
288 [o_mi_delete setTitle: _NS("Delete")];
289 [o_mi_recursive_expand setTitle: _NS("Expand Node")];
290 [o_mi_selectall setTitle: _NS("Select All")];
291 [o_mi_info setTitle: _NS("Media Information...")];
292 [o_mi_dl_cover_art setTitle: _NS("Download Cover Art")];
293 [o_mi_preparse setTitle: _NS("Fetch Meta Data")];
294 [o_mi_revealInFinder setTitle: _NS("Reveal in Finder")];
295 [o_mi_sort_name setTitle: _NS("Sort Node by Name")];
296 [o_mi_sort_author setTitle: _NS("Sort Node by Author")];
298 [o_search_field setToolTip: _NS("Search in Playlist")];
301 - (void)playlistUpdated
303 /* Clear indications of any existing column sorting */
304 NSUInteger count = [[o_outline_view tableColumns] count];
305 for (NSUInteger i = 0 ; i < count ; i++)
306 [o_outline_view setIndicatorImage:nil inTableColumn: [[o_outline_view tableColumns] objectAtIndex:i]];
308 [o_outline_view setHighlightedTableColumn:nil];
309 o_tc_sortColumn = nil;
311 [o_outline_view reloadData];
312 [[[[VLCMain sharedInstance] wizard] playlistWizard] reloadOutlineView];
314 [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:retainedRowSelection] byExtendingSelection:NO];
316 [self outlineViewSelectionDidChange: nil];
317 [[VLCMain sharedInstance] updateMainWindow];
320 - (void)outlineViewSelectionDidChange:(NSNotification *)notification
323 // playlist_item_t * p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
326 // /* update the state of our Reveal-in-Finder menu items */
327 // NSMutableString *o_mrl;
328 // char *psz_uri = input_item_GetURI(p_item->p_input);
330 // [o_mi_revealInFinder setEnabled: NO];
331 // [o_mm_mi_revealInFinder setEnabled: NO];
333 // o_mrl = [NSMutableString stringWithUTF8String: psz_uri];
335 // /* perform some checks whether it is a file and if it is local at all... */
336 // NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
337 // if (prefix_range.location != NSNotFound)
338 // [o_mrl deleteCharactersInRange: prefix_range];
340 // if ([o_mrl characterAtIndex:0] == '/') {
341 // [o_mi_revealInFinder setEnabled: YES];
342 // [o_mm_mi_revealInFinder setEnabled: YES];
347 // /* update our info-panel to reflect the new item */
348 // [[[VLCMain sharedInstance] info] updatePanelWithItem:p_item->p_input];
352 - (BOOL)isSelectionEmpty
354 return [o_outline_view selectedRow] == -1;
357 - (void)updateRowSelection
360 playlist_t *p_playlist = pl_Get(VLCIntf);
361 playlist_item_t *p_item, *p_temp_item;
362 NSMutableArray *o_array = [NSMutableArray array];
366 // p_item = playlist_CurrentPlayingItem(p_playlist);
367 // if (p_item == NULL) {
372 // p_temp_item = p_item;
373 // while(p_temp_item->p_parent) {
374 // [o_array insertObject: [NSValue valueWithPointer: p_temp_item] atIndex: 0];
375 // p_temp_item = p_temp_item->p_parent;
379 // NSUInteger count = [o_array count];
380 // for (NSUInteger j = 0; j < count - 1; j++) {
382 // if ((o_item = [o_outline_dict objectForKey:
383 // [NSString stringWithFormat: @"%p",
384 // [[o_array objectAtIndex:j] pointerValue]]]) != nil) {
385 // [o_outline_view expandItem: o_item];
389 // id o_item = [o_outline_dict objectForKey:[NSString stringWithFormat: @"%p", p_item]];
390 // NSInteger i_index = [o_outline_view rowForItem:o_item];
391 // [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:i_index] byExtendingSelection:NO];
392 // [o_outline_view setNeedsDisplay:YES];
395 - (IBAction)savePlaylist:(id)sender
397 playlist_t * p_playlist = pl_Get(VLCIntf);
399 NSSavePanel *o_save_panel = [NSSavePanel savePanel];
400 NSString * o_name = [NSString stringWithFormat: @"%@", _NS("Untitled")];
402 [NSBundle loadNibNamed:@"PlaylistAccessoryView" owner:self];
404 [o_save_accessory_text setStringValue: _NS("File Format:")];
405 [[o_save_accessory_popup itemAtIndex:0] setTitle: _NS("Extended M3U")];
406 [[o_save_accessory_popup itemAtIndex:1] setTitle: _NS("XML Shareable Playlist Format (XSPF)")];
407 [[o_save_accessory_popup itemAtIndex:2] setTitle: _NS("HTML playlist")];
409 [o_save_panel setTitle: _NS("Save Playlist")];
410 [o_save_panel setPrompt: _NS("Save")];
411 [o_save_panel setAccessoryView: o_save_accessory_view];
412 [o_save_panel setNameFieldStringValue: o_name];
414 if ([o_save_panel runModal] == NSFileHandlingPanelOKButton) {
415 NSString *o_filename = [[o_save_panel URL] path];
417 if ([o_save_accessory_popup indexOfSelectedItem] == 0) {
418 NSString * o_real_filename;
420 range.location = [o_filename length] - [@".m3u" length];
421 range.length = [@".m3u" length];
423 if ([o_filename compare:@".m3u" options: NSCaseInsensitiveSearch range: range] != NSOrderedSame)
424 o_real_filename = [NSString stringWithFormat: @"%@.m3u", o_filename];
426 o_real_filename = o_filename;
428 playlist_Export(p_playlist,
429 [o_real_filename fileSystemRepresentation],
430 p_playlist->p_local_category, "export-m3u");
431 } else if ([o_save_accessory_popup indexOfSelectedItem] == 1) {
432 NSString * o_real_filename;
434 range.location = [o_filename length] - [@".xspf" length];
435 range.length = [@".xspf" length];
437 if ([o_filename compare:@".xspf" options: NSCaseInsensitiveSearch range: range] != NSOrderedSame)
438 o_real_filename = [NSString stringWithFormat: @"%@.xspf", o_filename];
440 o_real_filename = o_filename;
442 playlist_Export(p_playlist,
443 [o_real_filename fileSystemRepresentation],
444 p_playlist->p_local_category, "export-xspf");
446 NSString * o_real_filename;
448 range.location = [o_filename length] - [@".html" length];
449 range.length = [@".html" length];
451 if ([o_filename compare:@".html" options: NSCaseInsensitiveSearch range: range] != NSOrderedSame)
452 o_real_filename = [NSString stringWithFormat: @"%@.html", o_filename];
454 o_real_filename = o_filename;
456 playlist_Export(p_playlist,
457 [o_real_filename fileSystemRepresentation],
458 p_playlist->p_local_category, "export-html");
463 /* When called retrieves the selected outlineview row and plays that node or item */
464 - (IBAction)playItem:(id)sender
466 playlist_t *p_playlist = pl_Get(VLCIntf);
468 // ignore clicks on column header when handling double action
469 if (sender == o_outline_view && [o_outline_view clickedRow] == -1)
472 PLItem *o_item = [o_outline_view itemAtRow:[o_outline_view selectedRow]];
477 playlist_item_t *p_item = playlist_ItemGetById(p_playlist, [o_item plItemId]);
478 playlist_item_t *p_node = playlist_ItemGetById(p_playlist, [[[self model] rootItem] plItemId]);
480 if (p_item && p_node) {
481 playlist_Control(p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, p_node, p_item);
486 - (IBAction)revealItemInFinder:(id)sender
488 NSIndexSet *selectedRows = [o_outline_view selectedRowIndexes];
489 [selectedRows enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
491 PLItem *o_item = [o_outline_view itemAtRow:idx];
493 /* perform some checks whether it is a file and if it is local at all... */
494 char *psz_url = input_item_GetURI([o_item input]);
495 NSURL *url = [NSURL URLWithString:toNSStr(psz_url)];
497 if (![url isFileURL])
499 if (![[NSFileManager defaultManager] fileExistsAtPath:[url path]])
502 msg_Dbg(VLCIntf, "Reveal url %s in finder", [[url path] UTF8String]);
503 [[NSWorkspace sharedWorkspace] selectFile: [url path] inFileViewerRootedAtPath: [url path]];
508 /* When called retrieves the selected outlineview row and plays that node or item */
509 - (IBAction)preparseItem:(id)sender
512 NSIndexSet *o_selected_indexes;
513 intf_thread_t * p_intf = VLCIntf;
514 playlist_t * p_playlist = pl_Get(p_intf);
515 playlist_item_t *p_item = NULL;
517 o_selected_indexes = [o_outline_view selectedRowIndexes];
518 i_count = [o_selected_indexes count];
520 NSUInteger indexes[i_count];
521 [o_selected_indexes getIndexes:indexes maxCount:i_count inIndexRange:nil];
522 for (int i = 0; i < i_count; i++) {
523 PLItem *o_item = [o_outline_view itemAtRow:indexes[i]];
524 [o_outline_view deselectRow: indexes[i]];
526 if (![o_item isLeaf]) {
527 msg_Dbg(p_intf, "preparsing nodes not implemented");
531 libvlc_MetaRequest(p_intf->p_libvlc, [o_item input], META_REQUEST_OPTION_NONE);
534 [self playlistUpdated];
537 - (IBAction)downloadCoverArt:(id)sender
540 NSIndexSet *o_selected_indexes;
541 intf_thread_t * p_intf = VLCIntf;
542 playlist_t * p_playlist = pl_Get(p_intf);
543 playlist_item_t *p_item = NULL;
545 o_selected_indexes = [o_outline_view selectedRowIndexes];
546 i_count = [o_selected_indexes count];
548 NSUInteger indexes[i_count];
549 [o_selected_indexes getIndexes:indexes maxCount:i_count inIndexRange:nil];
550 for (int i = 0; i < i_count; i++) {
551 PLItem *o_item = [o_outline_view itemAtRow: indexes[i]];
553 if (![o_item isLeaf])
556 libvlc_ArtRequest(p_intf->p_libvlc, [o_item input], META_REQUEST_OPTION_NONE);
558 [self playlistUpdated];
561 - (IBAction)selectAll:(id)sender
563 [o_outline_view selectAll: nil];
566 - (IBAction)showInfoPanel:(id)sender
568 [[[VLCMain sharedInstance] info] initPanel];
571 - (IBAction)deleteItem:(id)sender
574 NSIndexSet *o_selected_indexes;
575 intf_thread_t * p_intf = VLCIntf;
576 playlist_t * p_playlist = pl_Get(p_intf);
578 // check if deletion is allowed
579 if (![[self model] editAllowed])
582 o_selected_indexes = [o_outline_view selectedRowIndexes];
583 i_count = [o_selected_indexes count];
584 retainedRowSelection = [o_selected_indexes firstIndex];
585 if (retainedRowSelection == NSNotFound)
586 retainedRowSelection = 0;
589 NSUInteger indexes[i_count];
590 // if (i_count == [o_outline_view numberOfRows]) {
592 // playlist_NodeDelete(p_playlist, [self currentPlaylistRoot], true, false);
594 // [self playlistUpdated];
597 [o_selected_indexes getIndexes:indexes maxCount:i_count inIndexRange:nil];
598 for (int i = 0; i < i_count; i++) {
599 PLItem *o_item = [o_outline_view itemAtRow: indexes[i]];
600 [o_outline_view deselectRow: indexes[i]];
603 // if (p_item->i_children != -1) {
604 // //is a node and not an item
605 // if (playlist_Status(p_playlist) != PLAYLIST_STOPPED &&
606 // [self isItem: playlist_CurrentPlayingItem(p_playlist) inNode: ((playlist_item_t *)[o_item pointerValue])
607 // checkItemExistence: NO locked:YES] == YES)
608 // // if current item is in selected node and is playing then stop playlist
609 // playlist_Control(p_playlist, PLAYLIST_STOP, pl_Locked);
611 // playlist_NodeDelete(p_playlist, p_item, true, false);
614 playlist_DeleteFromInput(p_playlist, [o_item input], pl_Unlocked);
615 // [[o_item parent] deleteChild:o_item];
617 // [o_outline_view reloadData];
620 // [self playlistUpdated];
623 - (IBAction)sortNodeByName:(id)sender
625 [self sortNode: SORT_TITLE];
628 - (IBAction)sortNodeByAuthor:(id)sender
630 [self sortNode: SORT_ARTIST];
633 - (void)sortNode:(int)i_mode
635 playlist_t * p_playlist = pl_Get(VLCIntf);
636 playlist_item_t * p_item;
638 // TODO why do we need this kind of sort? It looks crap and confusing...
640 // if ([o_outline_view selectedRow] > -1) {
641 // p_item = [[o_outline_view itemAtRow: [o_outline_view selectedRow]] pointerValue];
645 // p_item = [self currentPlaylistRoot]; // If no item is selected, sort the whole playlist
648 // if (p_item->i_children > -1) // the item is a node
649 // playlist_RecursiveNodeSort(p_playlist, p_item, i_mode, ORDER_NORMAL);
651 // playlist_RecursiveNodeSort(p_playlist, p_item->p_parent, i_mode, ORDER_NORMAL);
654 // [self playlistUpdated];
657 - (input_item_t *)createItem:(NSDictionary *)o_one_item
659 intf_thread_t * p_intf = VLCIntf;
660 playlist_t * p_playlist = pl_Get(p_intf);
662 input_item_t *p_input;
663 BOOL b_rem = FALSE, b_dir = FALSE, b_writable = FALSE;
664 NSString *o_uri, *o_name, *o_path;
670 o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
671 o_nsurl = [NSURL URLWithString: o_uri];
672 o_path = [o_nsurl path];
673 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
674 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
676 if ([[NSFileManager defaultManager] fileExistsAtPath:o_path isDirectory:&b_dir] && b_dir &&
677 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath:o_path isRemovable: &b_rem
678 isWritable:&b_writable isUnmountable:NULL description:NULL type:NULL] && b_rem && !b_writable && [o_nsurl isFileURL]) {
680 NSString *diskType = [VLCOpen getVolumeTypeFromMountPath: o_path];
681 msg_Dbg(p_intf, "detected optical media of type %s in the file input", [diskType UTF8String]);
683 if ([diskType isEqualToString: kVLCMediaDVD])
684 o_uri = [NSString stringWithFormat: @"dvdnav://%@", [VLCOpen getBSDNodeFromMountPath: o_path]];
685 else if ([diskType isEqualToString: kVLCMediaVideoTSFolder])
686 o_uri = [NSString stringWithFormat: @"dvdnav://%@", o_path];
687 else if ([diskType isEqualToString: kVLCMediaAudioCD])
688 o_uri = [NSString stringWithFormat: @"cdda://%@", [VLCOpen getBSDNodeFromMountPath: o_path]];
689 else if ([diskType isEqualToString: kVLCMediaVCD])
690 o_uri = [NSString stringWithFormat: @"vcd://%@#0:0", [VLCOpen getBSDNodeFromMountPath: o_path]];
691 else if ([diskType isEqualToString: kVLCMediaSVCD])
692 o_uri = [NSString stringWithFormat: @"vcd://%@@0:0", [VLCOpen getBSDNodeFromMountPath: o_path]];
693 else if ([diskType isEqualToString: kVLCMediaBD] || [diskType isEqualToString: kVLCMediaBDMVFolder])
694 o_uri = [NSString stringWithFormat: @"bluray://%@", o_path];
696 msg_Warn(VLCIntf, "unknown disk type, treating %s as regular input", [o_path UTF8String]);
698 p_input = input_item_New([o_uri UTF8String], [[[NSFileManager defaultManager] displayNameAtPath: o_path] UTF8String]);
701 p_input = input_item_New([o_uri fileSystemRepresentation], o_name ? [o_name UTF8String] : NULL);
707 NSUInteger count = [o_options count];
708 for (NSUInteger i = 0; i < count; i++)
709 input_item_AddOption(p_input, [[o_options objectAtIndex:i] UTF8String], VLC_INPUT_OPTION_TRUSTED);
712 /* Recent documents menu */
713 if (o_nsurl != nil && (BOOL)config_GetInt(p_playlist, "macosx-recentitems") == YES)
714 [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL: o_nsurl];
719 - (void)addPlaylistItems:(NSArray*)o_array
724 // add items directly to media library if this is the current root
725 if ([[self model] currentRootType] == ROOT_TYPE_MEDIALIBRARY)
726 i_plItemId = [[[self model] rootItem] plItemId];
728 BOOL b_autoplay = var_InheritBool(VLCIntf, "macosx-autoplay");
730 [self addPlaylistItems:o_array withParentItemId:i_plItemId atPos:-1 startPlayback:b_autoplay];
733 - (void)addPlaylistItems:(NSArray*)o_array withParentItemId:(int)i_plItemId atPos:(int)i_position startPlayback:(BOOL)b_start
735 playlist_t * p_playlist = pl_Get(VLCIntf);
738 playlist_item_t *p_parent = NULL;
740 p_parent = playlist_ItemGetById(p_playlist, i_plItemId);
742 p_parent = p_playlist->p_playing;
749 NSUInteger count = [o_array count];
750 int i_current_offset = 0;
751 for (NSUInteger i = 0; i < count; ++i) {
753 NSDictionary *o_current_item = [o_array objectAtIndex:i];
754 input_item_t *p_input = [self createItem: o_current_item];
758 int i_pos = (i_position == -1) ? PLAYLIST_END : i_position + i_current_offset++;
759 playlist_item_t *p_item = playlist_NodeAddInput(p_playlist, p_input, p_parent,
760 PLAYLIST_INSERT, i_pos, pl_Locked);
764 if (i == 0 && b_start) {
765 playlist_Control(p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, p_parent, p_item);
767 input_item_Release(p_input);
773 - (IBAction)searchItem:(id)sender
775 [[self model] searchUpdate:[o_search_field stringValue]];
778 - (IBAction)recursiveExpandNode:(id)sender
780 NSIndexSet * selectedRows = [o_outline_view selectedRowIndexes];
781 NSUInteger count = [selectedRows count];
782 NSUInteger indexes[count];
783 [selectedRows getIndexes:indexes maxCount:count inIndexRange:nil];
786 playlist_item_t *p_item;
787 for (NSUInteger i = 0; i < count; i++) {
788 o_item = [o_outline_view itemAtRow: indexes[i]];
790 /* We need to collapse the node first, since OSX refuses to recursively
791 expand an already expanded node, even if children nodes are collapsed. */
792 if ([o_outline_view isExpandable:o_item]) {
793 [o_outline_view collapseItem: o_item collapseChildren: YES];
794 [o_outline_view expandItem: o_item expandChildren: YES];
797 selectedRows = [o_outline_view selectedRowIndexes];
798 [selectedRows getIndexes:indexes maxCount:count inIndexRange:nil];
802 - (NSMenu *)menuForEvent:(NSEvent *)o_event
804 if (!b_playlistmenu_nib_loaded)
805 b_playlistmenu_nib_loaded = [NSBundle loadNibNamed:@"PlaylistMenu" owner:self];
811 pt = [o_outline_view convertPoint: [o_event locationInWindow] fromView: nil];
812 int row = [o_outline_view rowAtPoint:pt];
813 if (row != -1 && ![[o_outline_view selectedRowIndexes] containsIndex: row])
814 [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
816 b_item_sel = (row != -1 && [o_outline_view selectedRow] != -1);
817 b_rows = [o_outline_view numberOfRows] != 0;
819 playlist_t *p_playlist = pl_Get(VLCIntf);
820 bool b_del_allowed = [[self model] editAllowed];
822 [o_mi_play setEnabled: b_item_sel];
823 [o_mi_delete setEnabled: b_item_sel && b_del_allowed];
824 [o_mi_selectall setEnabled: b_rows];
825 [o_mi_info setEnabled: b_item_sel];
826 [o_mi_preparse setEnabled: b_item_sel];
827 [o_mi_recursive_expand setEnabled: b_item_sel];
828 [o_mi_sort_name setEnabled: b_item_sel];
829 [o_mi_sort_author setEnabled: b_item_sel];
830 [o_mi_dl_cover_art setEnabled: b_item_sel];
835 - (void)outlineView: (NSOutlineView *)o_tv didClickTableColumn:(NSTableColumn *)o_tc
837 int i_mode, i_type = 0;
838 intf_thread_t *p_intf = VLCIntf;
839 NSString * o_identifier = [o_tc identifier];
841 playlist_t *p_playlist = pl_Get(p_intf);
843 if (o_tc_sortColumn == o_tc)
844 b_isSortDescending = !b_isSortDescending;
846 b_isSortDescending = false;
848 if (b_isSortDescending)
849 i_type = ORDER_REVERSE;
851 i_type = ORDER_NORMAL;
853 [[self model] sortForColumn:o_identifier withMode:i_type];
855 // TODO rework, why do we need a full call here?
856 // [self playlistUpdated];
858 /* Clear indications of any existing column sorting */
859 NSUInteger count = [[o_outline_view tableColumns] count];
860 for (NSUInteger i = 0 ; i < count ; i++)
861 [o_outline_view setIndicatorImage:nil inTableColumn: [[o_outline_view tableColumns] objectAtIndex:i]];
863 [o_outline_view setHighlightedTableColumn:nil];
864 o_tc_sortColumn = nil;
867 o_tc_sortColumn = o_tc;
868 [o_outline_view setHighlightedTableColumn:o_tc];
870 if (b_isSortDescending)
871 [o_outline_view setIndicatorImage:o_descendingSortingImage inTableColumn:o_tc];
873 [o_outline_view setIndicatorImage:o_ascendingSortingImage inTableColumn:o_tc];
877 - (void)outlineView:(NSOutlineView *)outlineView
878 willDisplayCell:(id)cell
879 forTableColumn:(NSTableColumn *)tableColumn
882 /* this method can be called when VLC is already dead, hence the extra checks */
883 intf_thread_t * p_intf = VLCIntf;
886 playlist_t *p_playlist = pl_Get(p_intf);
889 if (config_GetInt(VLCIntf, "macosx-large-text"))
890 fontToUse = [NSFont systemFontOfSize:13.];
892 fontToUse = [NSFont systemFontOfSize:11.];
894 BOOL b_is_playing = NO;
896 playlist_item_t *p_current_item = playlist_CurrentPlayingItem(p_playlist);
897 if (p_current_item) {
898 b_is_playing = p_current_item->i_id == [item plItemId];
903 TODO: repaint all items bold:
904 [self isItem: [o_playing_item pointerValue] inNode: [item pointerValue] checkItemExistence:YES locked:NO]
905 || [o_playing_item isEqual: item]
909 [cell setFont: [[NSFontManager sharedFontManager] convertFont:fontToUse toHaveTrait:NSBoldFontMask]];
911 [cell setFont: [[NSFontManager sharedFontManager] convertFont:fontToUse toNotHaveTrait:NSBoldFontMask]];
914 // TODO remove method
915 - (NSArray *)draggedItems
917 return [[self model] draggedItems];
920 - (void)setColumn: (NSString *)o_column state: (NSInteger)i_state translationDict:(NSDictionary *)o_dict
922 NSTableColumn * o_work_tc;
924 if (i_state == NSOnState) {
925 NSString *o_title = [o_dict objectForKey:o_column];
929 o_work_tc = [[NSTableColumn alloc] initWithIdentifier: o_column];
930 [o_work_tc setEditable: NO];
931 [[o_work_tc dataCell] setFont: [NSFont controlContentFontOfSize:11.]];
933 [[o_work_tc headerCell] setStringValue: [o_dict objectForKey:o_column]];
935 if ([o_column isEqualToString: TRACKNUM_COLUMN]) {
936 [o_work_tc setWidth: 20.];
937 [o_work_tc setResizingMask: NSTableColumnNoResizing];
938 [[o_work_tc headerCell] setStringValue: @"#"];
941 [o_outline_view addTableColumn: o_work_tc];
943 [o_outline_view reloadData];
944 [o_outline_view setNeedsDisplay: YES];
947 [o_outline_view removeTableColumn: [o_outline_view tableColumnWithIdentifier: o_column]];
949 [o_outline_view setOutlineTableColumn: [o_outline_view tableColumnWithIdentifier:TITLE_COLUMN]];
952 - (void)saveTableColumns
954 NSMutableArray * o_arrayToSave = [[NSMutableArray alloc] init];
955 NSArray * o_columns = [[NSArray alloc] initWithArray:[o_outline_view tableColumns]];
956 NSUInteger count = [o_columns count];
957 NSTableColumn * o_currentColumn;
958 for (NSUInteger i = 0; i < count; i++) {
959 o_currentColumn = [o_columns objectAtIndex:i];
960 [o_arrayToSave addObject:[NSArray arrayWithObjects:[o_currentColumn identifier], [NSNumber numberWithFloat:[o_currentColumn width]], nil]];
962 [[NSUserDefaults standardUserDefaults] setObject: o_arrayToSave forKey:@"PlaylistColumnSelection"];
963 [[NSUserDefaults standardUserDefaults] synchronize];
965 [o_arrayToSave release];
968 - (BOOL)isValidResumeItem:(input_item_t *)p_item
970 char *psz_url = input_item_GetURI(p_item);
971 NSString *o_url_string = toNSStr(psz_url);
974 if ([o_url_string isEqualToString:@""])
977 NSURL *o_url = [NSURL URLWithString:o_url_string];
979 if (![o_url isFileURL])
983 if (![[NSFileManager defaultManager] fileExistsAtPath:[o_url path] isDirectory:&isDir])
992 - (void)updateAlertWindow:(NSTimer *)timer
994 NSAlert *alert = [timer userInfo];
996 --currentResumeTimeout;
997 if (currentResumeTimeout <= 0) {
998 [[alert window] close];
1002 NSString *buttonLabel = _NS("Restart playback");
1003 buttonLabel = [buttonLabel stringByAppendingFormat:@" (%d)", currentResumeTimeout];
1005 [[[alert buttons] objectAtIndex:2] setTitle:buttonLabel];
1008 - (void)continuePlaybackWhereYouLeftOff:(input_thread_t *)p_input_thread
1010 NSDictionary *recentlyPlayedFiles = [[NSUserDefaults standardUserDefaults] objectForKey:@"recentlyPlayedMedia"];
1011 if (!recentlyPlayedFiles)
1014 input_item_t *p_item = input_GetItem(p_input_thread);
1018 /* allow the user to over-write the start/stop/run-time */
1019 if (var_GetFloat(p_input_thread, "run-time") > 0 ||
1020 var_GetFloat(p_input_thread, "start-time") > 0 ||
1021 var_GetFloat(p_input_thread, "stop-time") > 0) {
1025 /* check for file existance before resuming */
1026 if (![self isValidResumeItem:p_item])
1029 char *psz_url = decode_URI(input_item_GetURI(p_item));
1032 NSString *url = toNSStr(psz_url);
1035 NSNumber *lastPosition = [recentlyPlayedFiles objectForKey:url];
1036 if (!lastPosition || lastPosition.intValue <= 0)
1039 int settingValue = config_GetInt(VLCIntf, "macosx-continue-playback");
1040 if (settingValue == 2) // never resume
1043 NSInteger returnValue = NSAlertErrorReturn;
1044 if (settingValue == 0) { // ask
1046 char *psz_title_name = input_item_GetTitleFbName(p_item);
1047 NSString *o_title = toNSStr(psz_title_name);
1048 free(psz_title_name);
1050 currentResumeTimeout = 6;
1051 NSString *o_restartButtonLabel = _NS("Restart playback");
1052 o_restartButtonLabel = [o_restartButtonLabel stringByAppendingFormat:@" (%d)", currentResumeTimeout];
1053 NSAlert *theAlert = [NSAlert alertWithMessageText:_NS("Continue playback?") defaultButton:_NS("Continue") alternateButton:o_restartButtonLabel otherButton:_NS("Always continue") informativeTextWithFormat:_NS("Playback of \"%@\" will continue at %@"), o_title, [[VLCStringUtility sharedInstance] stringForTime:lastPosition.intValue]];
1055 NSTimer *timer = [NSTimer timerWithTimeInterval:1
1057 selector:@selector(updateAlertWindow:)
1061 [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSModalPanelRunLoopMode];
1063 returnValue = [theAlert runModal];
1066 // restart button was pressed or timeout happened
1067 if (returnValue == NSAlertAlternateReturn ||
1068 returnValue == NSRunAbortedResponse)
1072 mtime_t lastPos = (mtime_t)lastPosition.intValue * 1000000;
1073 msg_Dbg(VLCIntf, "continuing playback at %lld", lastPos);
1074 var_SetTime(p_input_thread, "time", lastPos);
1076 if (returnValue == NSAlertOtherReturn)
1077 config_PutInt(VLCIntf, "macosx-continue-playback", 1);
1080 - (void)storePlaybackPositionForItem:(input_thread_t *)p_input_thread
1082 if (!var_InheritBool(VLCIntf, "macosx-recentitems"))
1085 input_item_t *p_item = input_GetItem(p_input_thread);
1089 if (![self isValidResumeItem:p_item])
1092 char *psz_url = decode_URI(input_item_GetURI(p_item));
1095 NSString *url = toNSStr(psz_url);
1098 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
1099 NSMutableDictionary *mutDict = [[NSMutableDictionary alloc] initWithDictionary:[defaults objectForKey:@"recentlyPlayedMedia"]];
1101 float relativePos = var_GetFloat(p_input_thread, "position");
1102 mtime_t pos = var_GetTime(p_input_thread, "time") / 1000000;
1103 mtime_t dur = input_item_GetDuration(p_item) / 1000000;
1105 NSMutableArray *mediaList = [[defaults objectForKey:@"recentlyPlayedMediaList"] mutableCopy];
1107 if (relativePos > .05 && relativePos < .95 && dur > 180) {
1108 [mutDict setObject:[NSNumber numberWithInt:pos] forKey:url];
1110 [mediaList removeObject:url];
1111 [mediaList addObject:url];
1112 NSUInteger mediaListCount = mediaList.count;
1113 if (mediaListCount > 30) {
1114 for (NSUInteger x = 0; x < mediaListCount - 30; x++) {
1115 [mutDict removeObjectForKey:[mediaList objectAtIndex:0]];
1116 [mediaList removeObjectAtIndex:0];
1120 [mutDict removeObjectForKey:url];
1121 [mediaList removeObject:url];
1123 [defaults setObject:mutDict forKey:@"recentlyPlayedMedia"];
1124 [defaults setObject:mediaList forKey:@"recentlyPlayedMediaList"];
1125 [defaults synchronize];
1128 [mediaList release];