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 * VLCPlaylistCommon implementation
130 * This class the superclass of the VLCPlaylist and VLCPlaylistWizard.
131 * It contains the common methods and elements of these 2 entities.
132 *****************************************************************************/
133 @interface VLCPlaylistCommon ()
135 playlist_item_t * p_current_root_item;
139 @implementation VLCPlaylistCommon
143 playlist_t * p_playlist = pl_Get(VLCIntf);
144 p_current_root_item = p_playlist->p_local_category;
148 o_outline_dict = [[NSMutableDictionary alloc] init];
155 playlist_t * p_playlist = pl_Get(VLCIntf);
156 [o_outline_view setTarget: self];
157 [o_outline_view setDelegate: self];
158 [o_outline_view setDataSource: self];
159 [o_outline_view setAllowsEmptySelection: NO];
160 [o_outline_view expandItem: [o_outline_view itemAtRow:0]];
165 - (void)setPlaylistRoot: (playlist_item_t *)root_item
167 p_current_root_item = root_item;
168 [o_outline_view reloadData];
171 - (playlist_item_t *)currentPlaylistRoot
173 return p_current_root_item;
176 - (NSOutlineView *)outlineView
178 return o_outline_view;
181 - (playlist_item_t *)selectedPlaylistItem
183 return [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
191 if (config_GetInt(VLCIntf, "macosx-large-text")) {
192 fontToUse = [NSFont systemFontOfSize:13.];
195 fontToUse = [NSFont systemFontOfSize:11.];
199 NSArray *columns = [o_outline_view tableColumns];
200 NSUInteger count = columns.count;
201 for (NSUInteger x = 0; x < count; x++)
202 [[[columns objectAtIndex:x] dataCell] setFont:fontToUse];
203 [o_outline_view setRowHeight:rowHeight];
207 [o_outline_dict release];
213 @implementation VLCPlaylistCommon (NSOutlineViewDataSource)
214 /* return the number of children for Obj-C pointer item */ /* DONE */
215 - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
218 playlist_item_t *p_item = NULL;
219 playlist_t * p_playlist = pl_Get(VLCIntf);
220 //assert(outlineView == o_outline_view);
224 p_item = p_current_root_item;
226 p_item = (playlist_item_t *)[item pointerValue];
229 i_return = p_item->i_children;
232 return i_return > 0 ? i_return : 0;
235 /* return the child at index for the Obj-C pointer item */ /* DONE */
236 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
238 playlist_item_t *p_return = NULL, *p_item = NULL;
240 playlist_t * p_playlist = pl_Get(VLCIntf);
244 p_item = p_current_root_item; /* root object */
246 p_item = (playlist_item_t *)[item pointerValue];
248 if (p_item && index < p_item->i_children && index >= 0)
249 p_return = p_item->pp_children[index];
252 o_value = [o_outline_dict objectForKey:[NSString stringWithFormat: @"%p", p_return]];
254 if (o_value == nil) {
255 /* FIXME: Why is there a warning if that happens all the time and seems
256 * to be normal? Add an assert and fix it.
257 * msg_Warn(VLCIntf, "playlist item misses pointer value, adding one"); */
258 o_value = [[NSValue valueWithPointer: p_return] retain];
263 /* is the item expandable */
264 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
267 playlist_t *p_playlist = pl_Get(VLCIntf);
272 if (p_current_root_item) {
273 i_return = p_current_root_item->i_children;
276 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
278 i_return = p_item->i_children;
282 return (i_return >= 0);
285 /* retrieve the string values for the cells */
286 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
290 playlist_item_t *p_item;
292 /* For error handling */
293 static BOOL attempted_reload = NO;
295 if (item == nil || ![item isKindOfClass: [NSValue class]]) {
296 /* Attempt to fix the error by asking for a data redisplay
297 * This might cause infinite loop, so add a small check */
298 if (!attempted_reload) {
299 attempted_reload = YES;
300 [outlineView reloadData];
305 p_item = (playlist_item_t *)[item pointerValue];
306 if (!p_item || !p_item->p_input) {
307 /* Attempt to fix the error by asking for a data redisplay
308 * This might cause infinite loop, so add a small check */
309 if (!attempted_reload) {
310 attempted_reload = YES;
311 [outlineView reloadData];
316 attempted_reload = NO;
317 NSString * o_identifier = [o_tc identifier];
319 if ([o_identifier isEqualToString:TRACKNUM_COLUMN]) {
320 psz_value = input_item_GetTrackNumber(p_item->p_input);
322 o_value = [NSString stringWithUTF8String:psz_value];
325 } else if ([o_identifier isEqualToString:TITLE_COLUMN]) {
326 /* sanity check to prevent the NSString class from crashing */
327 char *psz_title = input_item_GetTitleFbName(p_item->p_input);
329 o_value = [NSString stringWithUTF8String:psz_title];
332 } else if ([o_identifier isEqualToString:ARTIST_COLUMN]) {
333 psz_value = input_item_GetArtist(p_item->p_input);
335 o_value = [NSString stringWithUTF8String:psz_value];
338 } else if ([o_identifier isEqualToString:@"duration"]) {
339 char psz_duration[MSTRTIME_MAX_SIZE];
340 mtime_t dur = input_item_GetDuration(p_item->p_input);
342 secstotimestr(psz_duration, dur/1000000);
343 o_value = [NSString stringWithUTF8String:psz_duration];
347 } else if ([o_identifier isEqualToString:GENRE_COLUMN]) {
348 psz_value = input_item_GetGenre(p_item->p_input);
350 o_value = [NSString stringWithUTF8String:psz_value];
353 } else if ([o_identifier isEqualToString:ALBUM_COLUMN]) {
354 psz_value = input_item_GetAlbum(p_item->p_input);
356 o_value = [NSString stringWithUTF8String:psz_value];
359 } else if ([o_identifier isEqualToString:DESCRIPTION_COLUMN]) {
360 psz_value = input_item_GetDescription(p_item->p_input);
362 o_value = [NSString stringWithUTF8String:psz_value];
365 } else if ([o_identifier isEqualToString:DATE_COLUMN]) {
366 psz_value = input_item_GetDate(p_item->p_input);
368 o_value = [NSString stringWithUTF8String:psz_value];
371 } else if ([o_identifier isEqualToString:LANGUAGE_COLUMN]) {
372 psz_value = input_item_GetLanguage(p_item->p_input);
374 o_value = [NSString stringWithUTF8String:psz_value];
378 else if ([o_identifier isEqualToString:URI_COLUMN]) {
379 psz_value = decode_URI(input_item_GetURI(p_item->p_input));
381 o_value = [NSString stringWithUTF8String:psz_value];
385 else if ([o_identifier isEqualToString:FILESIZE_COLUMN]) {
386 psz_value = input_item_GetURI(p_item->p_input);
389 NSURL *url = [NSURL URLWithString:[NSString stringWithUTF8String:psz_value]];
390 if ([url isFileURL]) {
391 NSFileManager *fileManager = [NSFileManager defaultManager];
392 if ([fileManager fileExistsAtPath:[url path]]) {
394 NSDictionary *attributes = [fileManager attributesOfItemAtPath:[url path] error:&error];
395 o_value = [VLCByteCountFormatter stringFromByteCount:[attributes fileSize] countStyle:NSByteCountFormatterCountStyleDecimal];
401 else if ([o_identifier isEqualToString:@"status"]) {
402 if (input_item_HasErrorWhenReading(p_item->p_input)) {
403 o_value = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kAlertCautionIcon)];
404 [o_value setSize: NSMakeSize(16,16)];
413 /*****************************************************************************
414 * VLCPlaylistWizard implementation
415 *****************************************************************************/
416 @implementation VLCPlaylistWizard
418 - (IBAction)reloadOutlineView
420 /* Only reload the outlineview if the wizard window is open since this can
421 be quite long on big playlists */
422 if ([[o_outline_view window] isVisible])
423 [o_outline_view reloadData];
428 /*****************************************************************************
429 * An extension to NSOutlineView's interface to fix compilation warnings
430 * and let us access these 2 functions properly.
431 * This uses a private API, but works fine on all current OSX releases.
432 * Radar ID 11739459 request a public API for this. However, it is probably
433 * easier and faster to recreate similar looking bitmaps ourselves.
434 *****************************************************************************/
436 @interface NSOutlineView (UndocumentedSortImages)
437 + (NSImage *)_defaultTableHeaderSortImage;
438 + (NSImage *)_defaultTableHeaderReverseSortImage;
442 /*****************************************************************************
443 * VLCPlaylist implementation
444 *****************************************************************************/
445 @interface VLCPlaylist ()
447 NSImage *o_descendingSortingImage;
448 NSImage *o_ascendingSortingImage;
450 NSMutableArray *o_nodes_array;
451 NSMutableArray *o_items_array;
453 BOOL b_selected_item_met;
454 BOOL b_isSortDescending;
456 NSUInteger retainedRowSelection;
458 BOOL b_playlistmenu_nib_loaded;
462 - (void)saveTableColumns;
465 @implementation VLCPlaylist
469 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
470 NSMutableArray *o_columnArray = [[NSMutableArray alloc] init];
471 [o_columnArray addObject: [NSArray arrayWithObjects:TITLE_COLUMN, [NSNumber numberWithFloat:190.], nil]];
472 [o_columnArray addObject: [NSArray arrayWithObjects:ARTIST_COLUMN, [NSNumber numberWithFloat:95.], nil]];
473 [o_columnArray addObject: [NSArray arrayWithObjects:DURATION_COLUMN, [NSNumber numberWithFloat:95.], nil]];
475 NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
476 [NSArray arrayWithArray:o_columnArray], @"PlaylistColumnSelection",
477 [NSArray array], @"recentlyPlayedMediaList",
478 [NSDictionary dictionary], @"recentlyPlayedMedia", nil];
480 [defaults registerDefaults:appDefaults];
481 [o_columnArray release];
488 o_nodes_array = [[NSMutableArray alloc] init];
489 o_items_array = [[NSMutableArray alloc] init];
496 [o_nodes_array release];
497 [o_items_array release];
506 playlist_t * p_playlist = pl_Get(VLCIntf);
508 [super awakeFromNib];
511 [o_outline_view setDoubleAction: @selector(playItem:)];
513 [o_outline_view registerForDraggedTypes: [NSArray arrayWithObjects:NSFilenamesPboardType, @"VLCPlaylistItemPboardType", nil]];
514 [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
516 /* This uses a private API, but works fine on all current OSX releases.
517 * Radar ID 11739459 request a public API for this. However, it is probably
518 * easier and faster to recreate similar looking bitmaps ourselves. */
519 o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
520 o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
522 o_tc_sortColumn = nil;
524 NSArray * o_columnArray = [[NSUserDefaults standardUserDefaults] arrayForKey:@"PlaylistColumnSelection"];
525 NSUInteger count = [o_columnArray count];
527 id o_menu = [[VLCMain sharedInstance] mainMenu];
530 NSMenu *o_context_menu = [o_menu setupPlaylistTableColumnsMenu];
531 [o_playlist_header setMenu: o_context_menu];
533 for (NSUInteger i = 0; i < count; i++) {
534 o_column = [[o_columnArray objectAtIndex:i] objectAtIndex:0];
535 if ([o_column isEqualToString:@"status"])
538 if(![o_menu setPlaylistColumnTableState: NSOnState forColumn: o_column])
541 [[o_outline_view tableColumnWithIdentifier: o_column] setWidth: [[[o_columnArray objectAtIndex:i] objectAtIndex:1] floatValue]];
544 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(applicationWillTerminate:) name: NSApplicationWillTerminateNotification object: nil];
549 - (void)applicationWillTerminate:(NSNotification *)notification
551 /* 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 */
552 [self saveTableColumns];
555 - (void)searchfieldChanged:(NSNotification *)o_notification
557 [o_search_field setStringValue:[[o_notification object] stringValue]];
562 [o_mi_play setTitle: _NS("Play")];
563 [o_mi_delete setTitle: _NS("Delete")];
564 [o_mi_recursive_expand setTitle: _NS("Expand Node")];
565 [o_mi_selectall setTitle: _NS("Select All")];
566 [o_mi_info setTitle: _NS("Media Information...")];
567 [o_mi_dl_cover_art setTitle: _NS("Download Cover Art")];
568 [o_mi_preparse setTitle: _NS("Fetch Meta Data")];
569 [o_mi_revealInFinder setTitle: _NS("Reveal in Finder")];
570 [o_mm_mi_revealInFinder setTitle: _NS("Reveal in Finder")];
571 [[o_mm_mi_revealInFinder menu] setAutoenablesItems: NO];
572 [o_mi_sort_name setTitle: _NS("Sort Node by Name")];
573 [o_mi_sort_author setTitle: _NS("Sort Node by Author")];
575 [o_search_field setToolTip: _NS("Search in Playlist")];
578 - (void)playlistUpdated
580 /* Clear indications of any existing column sorting */
581 NSUInteger count = [[o_outline_view tableColumns] count];
582 for (NSUInteger i = 0 ; i < count ; i++)
583 [o_outline_view setIndicatorImage:nil inTableColumn: [[o_outline_view tableColumns] objectAtIndex:i]];
585 [o_outline_view setHighlightedTableColumn:nil];
586 o_tc_sortColumn = nil;
587 // TODO Find a way to keep the dict size to a minimum
588 //[o_outline_dict removeAllObjects];
589 [o_outline_view reloadData];
590 [[[[VLCMain sharedInstance] wizard] playlistWizard] reloadOutlineView];
592 [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:retainedRowSelection] byExtendingSelection:NO];
594 [self outlineViewSelectionDidChange: nil];
595 [[VLCMain sharedInstance] updateMainWindow];
598 - (void)outlineViewSelectionDidChange:(NSNotification *)notification
601 playlist_item_t * p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
604 /* update the state of our Reveal-in-Finder menu items */
605 NSMutableString *o_mrl;
606 char *psz_uri = input_item_GetURI(p_item->p_input);
608 [o_mi_revealInFinder setEnabled: NO];
609 [o_mm_mi_revealInFinder setEnabled: NO];
611 o_mrl = [NSMutableString stringWithUTF8String: psz_uri];
613 /* perform some checks whether it is a file and if it is local at all... */
614 NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
615 if (prefix_range.location != NSNotFound)
616 [o_mrl deleteCharactersInRange: prefix_range];
618 if ([o_mrl characterAtIndex:0] == '/') {
619 [o_mi_revealInFinder setEnabled: YES];
620 [o_mm_mi_revealInFinder setEnabled: YES];
625 /* update our info-panel to reflect the new item */
626 [[[VLCMain sharedInstance] info] updatePanelWithItem:p_item->p_input];
630 - (BOOL)isSelectionEmpty
632 return [o_outline_view selectedRow] == -1;
635 - (void)updateRowSelection
638 playlist_t *p_playlist = pl_Get(VLCIntf);
639 playlist_item_t *p_item, *p_temp_item;
640 NSMutableArray *o_array = [NSMutableArray array];
643 p_item = playlist_CurrentPlayingItem(p_playlist);
644 if (p_item == NULL) {
649 p_temp_item = p_item;
650 while(p_temp_item->p_parent) {
651 [o_array insertObject: [NSValue valueWithPointer: p_temp_item] atIndex: 0];
652 p_temp_item = p_temp_item->p_parent;
656 NSUInteger count = [o_array count];
657 for (NSUInteger j = 0; j < count - 1; j++) {
659 if ((o_item = [o_outline_dict objectForKey:
660 [NSString stringWithFormat: @"%p",
661 [[o_array objectAtIndex:j] pointerValue]]]) != nil) {
662 [o_outline_view expandItem: o_item];
666 id o_item = [o_outline_dict objectForKey:[NSString stringWithFormat: @"%p", p_item]];
667 NSInteger i_index = [o_outline_view rowForItem:o_item];
668 [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:i_index] byExtendingSelection:NO];
669 [o_outline_view setNeedsDisplay:YES];
672 /* Check if p_item is a child of p_node recursively. We need to check the item
673 existence first since OSX sometimes tries to redraw items that have been
674 deleted. We don't do it when not required since this verification takes
675 quite a long time on big playlists (yes, pretty hacky). */
677 - (BOOL)isItem: (playlist_item_t *)p_item inNode: (playlist_item_t *)p_node checkItemExistence:(BOOL)b_check locked:(BOOL)b_locked
679 playlist_t * p_playlist = pl_Get(VLCIntf);
680 playlist_item_t *p_temp_item = p_item;
685 if (p_node == p_item)
688 if (p_node->i_children < 1)
693 if (!b_locked) PL_LOCK;
696 /* Since outlineView: willDisplayCell:... may call this function with
697 p_items that don't exist anymore, first check if the item is still
698 in the playlist. Any cleaner solution welcomed. */
699 for (i = 0; i < p_playlist->all_items.i_size; i++) {
700 if (ARRAY_VAL(p_playlist->all_items, i) == p_item)
702 else if (i == p_playlist->all_items.i_size - 1)
704 if (!b_locked) PL_UNLOCK;
711 p_temp_item = p_temp_item->p_parent;
712 if (p_temp_item == p_node) {
713 if (!b_locked) PL_UNLOCK;
717 if (!b_locked) PL_UNLOCK;
722 /* This method is useful for instance to remove the selected children of an
723 already selected node */
724 - (void)removeItemsFrom:(id)o_items ifChildrenOf:(id)o_nodes
726 NSUInteger itemCount = [o_items count];
727 NSUInteger nodeCount = [o_nodes count];
728 for (NSUInteger i = 0 ; i < itemCount ; i++) {
729 for (NSUInteger j = 0 ; j < nodeCount ; j++) {
730 if (o_items == o_nodes) {
731 if (j == i) continue;
733 if ([self isItem: [[o_items objectAtIndex:i] pointerValue]
734 inNode: [[o_nodes objectAtIndex:j] pointerValue]
735 checkItemExistence: NO locked:NO]) {
736 [o_items removeObjectAtIndex:i];
737 /* We need to execute the next iteration with the same index
738 since the current item has been deleted */
746 - (IBAction)savePlaylist:(id)sender
748 playlist_t * p_playlist = pl_Get(VLCIntf);
750 NSSavePanel *o_save_panel = [NSSavePanel savePanel];
751 NSString * o_name = [NSString stringWithFormat: @"%@", _NS("Untitled")];
753 [NSBundle loadNibNamed:@"PlaylistAccessoryView" owner:self];
755 [o_save_accessory_text setStringValue: _NS("File Format:")];
756 [[o_save_accessory_popup itemAtIndex:0] setTitle: _NS("Extended M3U")];
757 [[o_save_accessory_popup itemAtIndex:1] setTitle: _NS("XML Shareable Playlist Format (XSPF)")];
758 [[o_save_accessory_popup itemAtIndex:2] setTitle: _NS("HTML playlist")];
760 [o_save_panel setTitle: _NS("Save Playlist")];
761 [o_save_panel setPrompt: _NS("Save")];
762 [o_save_panel setAccessoryView: o_save_accessory_view];
763 [o_save_panel setNameFieldStringValue: o_name];
765 if ([o_save_panel runModal] == NSFileHandlingPanelOKButton) {
766 NSString *o_filename = [[o_save_panel URL] path];
768 if ([o_save_accessory_popup indexOfSelectedItem] == 0) {
769 NSString * o_real_filename;
771 range.location = [o_filename length] - [@".m3u" length];
772 range.length = [@".m3u" length];
774 if ([o_filename compare:@".m3u" options: NSCaseInsensitiveSearch range: range] != NSOrderedSame)
775 o_real_filename = [NSString stringWithFormat: @"%@.m3u", o_filename];
777 o_real_filename = o_filename;
779 playlist_Export(p_playlist,
780 [o_real_filename fileSystemRepresentation],
781 p_playlist->p_local_category, "export-m3u");
782 } else if ([o_save_accessory_popup indexOfSelectedItem] == 1) {
783 NSString * o_real_filename;
785 range.location = [o_filename length] - [@".xspf" length];
786 range.length = [@".xspf" length];
788 if ([o_filename compare:@".xspf" options: NSCaseInsensitiveSearch range: range] != NSOrderedSame)
789 o_real_filename = [NSString stringWithFormat: @"%@.xspf", o_filename];
791 o_real_filename = o_filename;
793 playlist_Export(p_playlist,
794 [o_real_filename fileSystemRepresentation],
795 p_playlist->p_local_category, "export-xspf");
797 NSString * o_real_filename;
799 range.location = [o_filename length] - [@".html" length];
800 range.length = [@".html" length];
802 if ([o_filename compare:@".html" options: NSCaseInsensitiveSearch range: range] != NSOrderedSame)
803 o_real_filename = [NSString stringWithFormat: @"%@.html", o_filename];
805 o_real_filename = o_filename;
807 playlist_Export(p_playlist,
808 [o_real_filename fileSystemRepresentation],
809 p_playlist->p_local_category, "export-html");
814 /* When called retrieves the selected outlineview row and plays that node or item */
815 - (IBAction)playItem:(id)sender
817 intf_thread_t * p_intf = VLCIntf;
818 playlist_t * p_playlist = pl_Get(p_intf);
820 playlist_item_t *p_item;
821 playlist_item_t *p_node = NULL;
823 // ignore clicks on column header when handling double action
824 if (sender != nil && [o_outline_view clickedRow] == -1 && sender != o_mi_play)
827 p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
831 if (p_item->i_children == -1) {
832 p_node = p_item->p_parent;
835 if (p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1)
836 p_item = p_node->pp_children[0];
841 playlist_Control(p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, p_node, p_item);
846 - (IBAction)revealItemInFinder:(id)sender
848 NSIndexSet * selectedRows = [o_outline_view selectedRowIndexes];
849 NSUInteger count = [selectedRows count];
850 NSUInteger indexes[count];
851 [selectedRows getIndexes:indexes maxCount:count inIndexRange:nil];
853 NSMutableString * o_mrl;
854 playlist_item_t *p_item;
855 for (NSUInteger i = 0; i < count; i++) {
856 p_item = [[o_outline_view itemAtRow:indexes[i]] pointerValue];
858 if (! p_item || !p_item->p_input)
861 char * psz_url = decode_URI(input_item_GetURI(p_item->p_input));
862 o_mrl = [[NSMutableString alloc] initWithString: [NSString stringWithUTF8String:psz_url ? psz_url : ""]];
866 /* perform some checks whether it is a file and if it is local at all... */
867 if ([o_mrl length] > 0) {
868 NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
869 if (prefix_range.location != NSNotFound)
870 [o_mrl deleteCharactersInRange: prefix_range];
872 if ([o_mrl characterAtIndex:0] == '/')
873 [[NSWorkspace sharedWorkspace] selectFile: o_mrl inFileViewerRootedAtPath: o_mrl];
880 /* When called retrieves the selected outlineview row and plays that node or item */
881 - (IBAction)preparseItem:(id)sender
884 NSIndexSet *o_selected_indexes;
885 intf_thread_t * p_intf = VLCIntf;
886 playlist_t * p_playlist = pl_Get(p_intf);
887 playlist_item_t *p_item = NULL;
889 o_selected_indexes = [o_outline_view selectedRowIndexes];
890 i_count = [o_selected_indexes count];
892 NSUInteger indexes[i_count];
893 [o_selected_indexes getIndexes:indexes maxCount:i_count inIndexRange:nil];
894 for (int i = 0; i < i_count; i++) {
895 p_item = [[o_outline_view itemAtRow:indexes[i]] pointerValue];
896 [o_outline_view deselectRow: indexes[i]];
899 if (p_item->i_children == -1)
900 libvlc_MetaRequest(p_intf->p_libvlc, p_item->p_input, META_REQUEST_OPTION_NONE);
902 msg_Dbg(p_intf, "preparsing nodes not implemented");
905 [self playlistUpdated];
908 - (IBAction)downloadCoverArt:(id)sender
911 NSIndexSet *o_selected_indexes;
912 intf_thread_t * p_intf = VLCIntf;
913 playlist_t * p_playlist = pl_Get(p_intf);
914 playlist_item_t *p_item = NULL;
916 o_selected_indexes = [o_outline_view selectedRowIndexes];
917 i_count = [o_selected_indexes count];
919 NSUInteger indexes[i_count];
920 [o_selected_indexes getIndexes:indexes maxCount:i_count inIndexRange:nil];
921 for (int i = 0; i < i_count; i++) {
922 p_item = [[o_outline_view itemAtRow: indexes[i]] pointerValue];
924 if (p_item && p_item->i_children == -1)
925 libvlc_ArtRequest(p_intf->p_libvlc, p_item->p_input, META_REQUEST_OPTION_NONE);
927 [self playlistUpdated];
930 - (IBAction)selectAll:(id)sender
932 [o_outline_view selectAll: nil];
935 - (IBAction)showInfoPanel:(id)sender
937 [[[VLCMain sharedInstance] info] initPanel];
940 - (IBAction)deleteItem:(id)sender
943 NSIndexSet *o_selected_indexes;
944 intf_thread_t * p_intf = VLCIntf;
945 playlist_t * p_playlist = pl_Get(p_intf);
947 // check if deletion is allowed
948 if ([self currentPlaylistRoot] != p_playlist->p_local_category && [self currentPlaylistRoot] != p_playlist->p_ml_category)
951 o_selected_indexes = [o_outline_view selectedRowIndexes];
952 i_count = [o_selected_indexes count];
953 retainedRowSelection = [o_selected_indexes firstIndex];
954 if (retainedRowSelection == NSNotFound)
955 retainedRowSelection = 0;
958 NSUInteger indexes[i_count];
959 if (i_count == [o_outline_view numberOfRows]) {
961 playlist_NodeDelete(p_playlist, [self currentPlaylistRoot], true, false);
963 [self playlistUpdated];
966 [o_selected_indexes getIndexes:indexes maxCount:i_count inIndexRange:nil];
967 for (int i = 0; i < i_count; i++) {
968 id o_item = [o_outline_view itemAtRow: indexes[i]];
969 [o_outline_view deselectRow: indexes[i]];
972 playlist_item_t *p_item = [o_item pointerValue];
973 if (!p_item || !p_item->p_input) {
978 if (p_item->i_children != -1) {
979 //is a node and not an item
980 if (playlist_Status(p_playlist) != PLAYLIST_STOPPED &&
981 [self isItem: playlist_CurrentPlayingItem(p_playlist) inNode: ((playlist_item_t *)[o_item pointerValue])
982 checkItemExistence: NO locked:YES] == YES)
983 // if current item is in selected node and is playing then stop playlist
984 playlist_Control(p_playlist, PLAYLIST_STOP, pl_Locked);
986 playlist_NodeDelete(p_playlist, p_item, true, false);
988 playlist_DeleteFromInput(p_playlist, p_item->p_input, pl_Locked);
991 [o_outline_dict removeObjectForKey:[NSString stringWithFormat:@"%p", [o_item pointerValue]]];
995 [self playlistUpdated];
998 - (IBAction)sortNodeByName:(id)sender
1000 [self sortNode: SORT_TITLE];
1003 - (IBAction)sortNodeByAuthor:(id)sender
1005 [self sortNode: SORT_ARTIST];
1008 - (void)sortNode:(int)i_mode
1010 playlist_t * p_playlist = pl_Get(VLCIntf);
1011 playlist_item_t * p_item;
1013 if ([o_outline_view selectedRow] > -1) {
1014 p_item = [[o_outline_view itemAtRow: [o_outline_view selectedRow]] pointerValue];
1018 p_item = [self currentPlaylistRoot]; // If no item is selected, sort the whole playlist
1021 if (p_item->i_children > -1) // the item is a node
1022 playlist_RecursiveNodeSort(p_playlist, p_item, i_mode, ORDER_NORMAL);
1024 playlist_RecursiveNodeSort(p_playlist, p_item->p_parent, i_mode, ORDER_NORMAL);
1027 [self playlistUpdated];
1030 - (input_item_t *)createItem:(NSDictionary *)o_one_item
1032 intf_thread_t * p_intf = VLCIntf;
1033 playlist_t * p_playlist = pl_Get(p_intf);
1035 input_item_t *p_input;
1036 BOOL b_rem = FALSE, b_dir = FALSE, b_writable = FALSE;
1037 NSString *o_uri, *o_name, *o_path;
1043 o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
1044 o_nsurl = [NSURL URLWithString: o_uri];
1045 o_path = [o_nsurl path];
1046 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
1047 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
1049 if ([[NSFileManager defaultManager] fileExistsAtPath:o_path isDirectory:&b_dir] && b_dir &&
1050 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath:o_path isRemovable: &b_rem
1051 isWritable:&b_writable isUnmountable:NULL description:NULL type:NULL] && b_rem && !b_writable && [o_nsurl isFileURL]) {
1053 NSString *diskType = [VLCOpen getVolumeTypeFromMountPath: o_path];
1054 msg_Dbg(p_intf, "detected optical media of type %s in the file input", [diskType UTF8String]);
1056 if ([diskType isEqualToString: kVLCMediaDVD])
1057 o_uri = [NSString stringWithFormat: @"dvdnav://%@", [VLCOpen getBSDNodeFromMountPath: o_path]];
1058 else if ([diskType isEqualToString: kVLCMediaVideoTSFolder])
1059 o_uri = [NSString stringWithFormat: @"dvdnav://%@", o_path];
1060 else if ([diskType isEqualToString: kVLCMediaAudioCD])
1061 o_uri = [NSString stringWithFormat: @"cdda://%@", [VLCOpen getBSDNodeFromMountPath: o_path]];
1062 else if ([diskType isEqualToString: kVLCMediaVCD])
1063 o_uri = [NSString stringWithFormat: @"vcd://%@#0:0", [VLCOpen getBSDNodeFromMountPath: o_path]];
1064 else if ([diskType isEqualToString: kVLCMediaSVCD])
1065 o_uri = [NSString stringWithFormat: @"vcd://%@@0:0", [VLCOpen getBSDNodeFromMountPath: o_path]];
1066 else if ([diskType isEqualToString: kVLCMediaBD] || [diskType isEqualToString: kVLCMediaBDMVFolder])
1067 o_uri = [NSString stringWithFormat: @"bluray://%@", o_path];
1069 msg_Warn(VLCIntf, "unknown disk type, treating %s as regular input", [o_path UTF8String]);
1071 p_input = input_item_New([o_uri UTF8String], [[[NSFileManager defaultManager] displayNameAtPath: o_path] UTF8String]);
1074 p_input = input_item_New([o_uri fileSystemRepresentation], o_name ? [o_name UTF8String] : NULL);
1080 NSUInteger count = [o_options count];
1081 for (NSUInteger i = 0; i < count; i++)
1082 input_item_AddOption(p_input, [[o_options objectAtIndex:i] UTF8String], VLC_INPUT_OPTION_TRUSTED);
1085 /* Recent documents menu */
1086 if (o_nsurl != nil && (BOOL)config_GetInt(p_playlist, "macosx-recentitems") == YES)
1087 [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL: o_nsurl];
1092 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
1094 playlist_t * p_playlist = pl_Get(VLCIntf);
1095 NSUInteger count = [o_array count];
1096 BOOL b_usingPlaylist;
1097 if ([self currentPlaylistRoot] == p_playlist->p_ml_category)
1098 b_usingPlaylist = NO;
1100 b_usingPlaylist = YES;
1103 for (NSUInteger i_item = 0; i_item < count; i_item++) {
1104 input_item_t *p_input;
1105 NSDictionary *o_one_item;
1108 o_one_item = [o_array objectAtIndex:i_item];
1109 p_input = [self createItem: o_one_item];
1114 int returnValue = playlist_AddInput(p_playlist, p_input, PLAYLIST_INSERT, i_position == -1 ? PLAYLIST_END : i_position + i_item, b_usingPlaylist, pl_Locked);
1115 if (returnValue != VLC_SUCCESS) {
1116 vlc_gc_decref(p_input);
1120 if (i_item == 0 && !b_enqueue) {
1121 playlist_item_t *p_item = playlist_ItemGetByInput(p_playlist, p_input);
1122 playlist_Control(p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, p_item->p_parent, p_item);
1125 vlc_gc_decref(p_input);
1128 [self playlistUpdated];
1131 - (void)appendNodeArray:(NSArray*)o_array inNode:(playlist_item_t *)p_node atPos:(int)i_position enqueue:(BOOL)b_enqueue
1133 playlist_t * p_playlist = pl_Get(VLCIntf);
1134 NSUInteger count = [o_array count];
1136 for (NSUInteger i_item = 0; i_item < count; i_item++) {
1137 input_item_t *p_input;
1138 NSDictionary *o_one_item;
1142 o_one_item = [o_array objectAtIndex:i_item];
1143 p_input = [self createItem: o_one_item];
1149 playlist_NodeAddInput(p_playlist, p_input, p_node,
1152 PLAYLIST_END : i_position + i_item,
1156 if (i_item == 0 && !b_enqueue) {
1157 playlist_item_t *p_item;
1158 p_item = playlist_ItemGetByInput(p_playlist, p_input);
1159 playlist_Control(p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, p_node, p_item);
1162 vlc_gc_decref(p_input);
1164 [self playlistUpdated];
1167 - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
1169 playlist_t *p_playlist = pl_Get(VLCIntf);
1170 playlist_item_t *p_selected_item;
1173 i_selected_row = [o_outline_view selectedRow];
1174 if (i_selected_row < 0)
1177 p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow: i_selected_row] pointerValue];
1179 for (NSUInteger i_current = 0; i_current < p_item->i_children ; i_current++) {
1181 NSString *o_current_name, *o_current_author;
1184 o_current_name = [NSString stringWithUTF8String:p_item->pp_children[i_current]->p_input->psz_name];
1185 psz_temp = input_item_GetInfo(p_item->p_input, _("Meta-information"),_("Artist"));
1186 o_current_author = [NSString stringWithUTF8String:psz_temp];
1190 if (p_selected_item == p_item->pp_children[i_current] && b_selected_item_met == NO)
1191 b_selected_item_met = YES;
1192 else if (p_selected_item == p_item->pp_children[i_current] && b_selected_item_met == YES)
1194 else if (b_selected_item_met == YES &&
1195 ([o_current_name rangeOfString:[o_search_field
1196 stringValue] options:NSCaseInsensitiveSearch].length ||
1197 [o_current_author rangeOfString:[o_search_field
1198 stringValue] options:NSCaseInsensitiveSearch].length))
1199 /*Adds the parent items in the result array as well, so that we can
1201 return [NSMutableArray arrayWithObject: [NSValue valueWithPointer: p_item->pp_children[i_current]]];
1203 if (p_item->pp_children[i_current]->i_children > 0) {
1204 id o_result = [self subSearchItem:
1205 p_item->pp_children[i_current]];
1206 if (o_result != NULL) {
1207 [o_result insertObject: [NSValue valueWithPointer:
1208 p_item->pp_children[i_current]] atIndex:0];
1216 - (IBAction)searchItem:(id)sender
1218 playlist_t * p_playlist = pl_Get(VLCIntf);
1223 b_selected_item_met = NO;
1225 /* First, only search after the selected item:
1226 * (b_selected_item_met = NO) */
1227 o_result = [self subSearchItem:[self currentPlaylistRoot]];
1228 if (o_result == NULL)
1229 /* If the first search failed, search again from the beginning */
1230 o_result = [self subSearchItem:[self currentPlaylistRoot]];
1232 if (o_result != NULL) {
1234 if ([[o_result objectAtIndex:0] pointerValue] == p_playlist->p_local_category)
1238 NSUInteger count = [o_result count];
1240 for (NSUInteger i = i_start ; i < count - 1 ; i++) {
1241 [o_outline_view expandItem: [o_outline_dict objectForKey:
1242 [NSString stringWithFormat: @"%p",
1243 [[o_result objectAtIndex:i] pointerValue]]]];
1245 i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
1246 [NSString stringWithFormat: @"%p",
1247 [[o_result objectAtIndex:count - 1 ]
1251 [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:i_row] byExtendingSelection:NO];
1252 [o_outline_view scrollRowToVisible: i_row];
1256 - (IBAction)recursiveExpandNode:(id)sender
1258 NSIndexSet * selectedRows = [o_outline_view selectedRowIndexes];
1259 NSUInteger count = [selectedRows count];
1260 NSUInteger indexes[count];
1261 [selectedRows getIndexes:indexes maxCount:count inIndexRange:nil];
1264 playlist_item_t *p_item;
1265 for (NSUInteger i = 0; i < count; i++) {
1266 o_item = [o_outline_view itemAtRow: indexes[i]];
1267 p_item = (playlist_item_t *)[o_item pointerValue];
1269 if (![[o_outline_view dataSource] outlineView: o_outline_view isItemExpandable: o_item])
1270 o_item = [o_outline_dict objectForKey: [NSString stringWithFormat: @"%p", p_item->p_parent]];
1272 /* We need to collapse the node first, since OSX refuses to recursively
1273 expand an already expanded node, even if children nodes are collapsed. */
1274 [o_outline_view collapseItem: o_item collapseChildren: YES];
1275 [o_outline_view expandItem: o_item expandChildren: YES];
1277 selectedRows = [o_outline_view selectedRowIndexes];
1278 [selectedRows getIndexes:indexes maxCount:count inIndexRange:nil];
1282 - (NSMenu *)menuForEvent:(NSEvent *)o_event
1284 if (!b_playlistmenu_nib_loaded)
1285 b_playlistmenu_nib_loaded = [NSBundle loadNibNamed:@"PlaylistMenu" owner:self];
1291 pt = [o_outline_view convertPoint: [o_event locationInWindow] fromView: nil];
1292 int row = [o_outline_view rowAtPoint:pt];
1293 if (row != -1 && ![[o_outline_view selectedRowIndexes] containsIndex: row])
1294 [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
1296 b_item_sel = (row != -1 && [o_outline_view selectedRow] != -1);
1297 b_rows = [o_outline_view numberOfRows] != 0;
1299 playlist_t *p_playlist = pl_Get(VLCIntf);
1300 bool b_del_allowed = [self currentPlaylistRoot] == p_playlist->p_local_category || [self currentPlaylistRoot] == p_playlist->p_ml_category;
1302 [o_mi_play setEnabled: b_item_sel];
1303 [o_mi_delete setEnabled: b_item_sel && b_del_allowed];
1304 [o_mi_selectall setEnabled: b_rows];
1305 [o_mi_info setEnabled: b_item_sel];
1306 [o_mi_preparse setEnabled: b_item_sel];
1307 [o_mi_recursive_expand setEnabled: b_item_sel];
1308 [o_mi_sort_name setEnabled: b_item_sel];
1309 [o_mi_sort_author setEnabled: b_item_sel];
1314 - (void)outlineView: (NSOutlineView *)o_tv didClickTableColumn:(NSTableColumn *)o_tc
1316 int i_mode, i_type = 0;
1317 intf_thread_t *p_intf = VLCIntf;
1318 NSString * o_identifier = [o_tc identifier];
1320 playlist_t *p_playlist = pl_Get(p_intf);
1322 if ([o_identifier isEqualToString:TRACKNUM_COLUMN])
1323 i_mode = SORT_TRACK_NUMBER;
1324 else if ([o_identifier isEqualToString:TITLE_COLUMN])
1325 i_mode = SORT_TITLE;
1326 else if ([o_identifier isEqualToString:ARTIST_COLUMN])
1327 i_mode = SORT_ARTIST;
1328 else if ([o_identifier isEqualToString:GENRE_COLUMN])
1329 i_mode = SORT_GENRE;
1330 else if ([o_identifier isEqualToString:DURATION_COLUMN])
1331 i_mode = SORT_DURATION;
1332 else if ([o_identifier isEqualToString:ALBUM_COLUMN])
1333 i_mode = SORT_ALBUM;
1334 else if ([o_identifier isEqualToString:DESCRIPTION_COLUMN])
1335 i_mode = SORT_DESCRIPTION;
1336 else if ([o_identifier isEqualToString:URI_COLUMN])
1341 if (o_tc_sortColumn == o_tc)
1342 b_isSortDescending = !b_isSortDescending;
1344 b_isSortDescending = false;
1346 if (b_isSortDescending)
1347 i_type = ORDER_REVERSE;
1349 i_type = ORDER_NORMAL;
1352 playlist_RecursiveNodeSort(p_playlist, [self currentPlaylistRoot], i_mode, i_type);
1355 [self playlistUpdated];
1357 o_tc_sortColumn = o_tc;
1358 [o_outline_view setHighlightedTableColumn:o_tc];
1360 if (b_isSortDescending)
1361 [o_outline_view setIndicatorImage:o_descendingSortingImage inTableColumn:o_tc];
1363 [o_outline_view setIndicatorImage:o_ascendingSortingImage inTableColumn:o_tc];
1367 - (void)outlineView:(NSOutlineView *)outlineView
1368 willDisplayCell:(id)cell
1369 forTableColumn:(NSTableColumn *)tableColumn
1372 /* this method can be called when VLC is already dead, hence the extra checks */
1373 intf_thread_t * p_intf = VLCIntf;
1376 playlist_t *p_playlist = pl_Get(p_intf);
1383 o_playing_item = [o_outline_dict objectForKey: [NSString stringWithFormat:@"%p", playlist_CurrentPlayingItem(p_playlist)]];
1387 if (config_GetInt(VLCIntf, "macosx-large-text"))
1388 fontToUse = [NSFont systemFontOfSize:13.];
1390 fontToUse = [NSFont systemFontOfSize:11.];
1392 if ([self isItem: [o_playing_item pointerValue] inNode: [item pointerValue] checkItemExistence:YES locked:NO]
1393 || [o_playing_item isEqual: item])
1394 [cell setFont: [[NSFontManager sharedFontManager] convertFont:fontToUse toHaveTrait:NSBoldFontMask]];
1396 [cell setFont: [[NSFontManager sharedFontManager] convertFont:fontToUse toNotHaveTrait:NSBoldFontMask]];
1401 playlist_t *p_playlist = pl_Get(VLCIntf);
1406 o_playing_item = [o_outline_dict objectForKey: [NSString stringWithFormat:@"%p", playlist_CurrentPlayingItem(p_playlist)]];
1409 return o_playing_item;
1412 - (NSArray *)draggedItems
1414 return [[o_nodes_array arrayByAddingObjectsFromArray: o_items_array] retain];
1417 - (void)setColumn: (NSString *)o_column state: (NSInteger)i_state translationDict:(NSDictionary *)o_dict
1419 NSTableColumn * o_work_tc;
1421 if (i_state == NSOnState) {
1422 NSString *o_title = [o_dict objectForKey:o_column];
1426 o_work_tc = [[NSTableColumn alloc] initWithIdentifier: o_column];
1427 [o_work_tc setEditable: NO];
1428 [[o_work_tc dataCell] setFont: [NSFont controlContentFontOfSize:11.]];
1430 [[o_work_tc headerCell] setStringValue: [o_dict objectForKey:o_column]];
1432 if ([o_column isEqualToString: TRACKNUM_COLUMN]) {
1433 [o_work_tc setWidth: 20.];
1434 [o_work_tc setResizingMask: NSTableColumnNoResizing];
1435 [[o_work_tc headerCell] setStringValue: @"#"];
1438 [o_outline_view addTableColumn: o_work_tc];
1439 [o_work_tc release];
1440 [o_outline_view reloadData];
1441 [o_outline_view setNeedsDisplay: YES];
1444 [o_outline_view removeTableColumn: [o_outline_view tableColumnWithIdentifier: o_column]];
1446 [o_outline_view setOutlineTableColumn: [o_outline_view tableColumnWithIdentifier:TITLE_COLUMN]];
1449 - (void)saveTableColumns
1451 NSMutableArray * o_arrayToSave = [[NSMutableArray alloc] init];
1452 NSArray * o_columns = [[NSArray alloc] initWithArray:[o_outline_view tableColumns]];
1453 NSUInteger count = [o_columns count];
1454 NSTableColumn * o_currentColumn;
1455 for (NSUInteger i = 0; i < count; i++) {
1456 o_currentColumn = [o_columns objectAtIndex:i];
1457 [o_arrayToSave addObject:[NSArray arrayWithObjects:[o_currentColumn identifier], [NSNumber numberWithFloat:[o_currentColumn width]], nil]];
1459 [[NSUserDefaults standardUserDefaults] setObject: o_arrayToSave forKey:@"PlaylistColumnSelection"];
1460 [[NSUserDefaults standardUserDefaults] synchronize];
1461 [o_columns release];
1462 [o_arrayToSave release];
1465 - (BOOL)isValidResumeItem:(input_item_t *)p_item
1467 char *psz_url = input_item_GetURI(p_item);
1468 NSString *o_url_string = toNSStr(psz_url);
1471 if ([o_url_string isEqualToString:@""])
1474 NSURL *o_url = [NSURL URLWithString:o_url_string];
1476 if (![o_url isFileURL])
1480 if (![[NSFileManager defaultManager] fileExistsAtPath:[o_url path] isDirectory:&isDir])
1489 - (void)continuePlaybackWhereYouLeftOff:(input_thread_t *)p_input_thread
1491 NSDictionary *recentlyPlayedFiles = [[NSUserDefaults standardUserDefaults] objectForKey:@"recentlyPlayedMedia"];
1492 if (recentlyPlayedFiles) {
1493 input_item_t *p_item = input_GetItem(p_input_thread);
1497 /* allow the user to over-write the start/stop/run-time */
1498 if (var_GetFloat(p_input_thread, "run-time") > 0 ||
1499 var_GetFloat(p_input_thread, "start-time") > 0 ||
1500 var_GetFloat(p_input_thread, "stop-time") > 0) {
1504 /* check for file existance before resuming */
1505 if (![self isValidResumeItem:p_item])
1508 char *psz_url = decode_URI(input_item_GetURI(p_item));
1511 NSString *url = toNSStr(psz_url);
1514 NSNumber *lastPosition = [recentlyPlayedFiles objectForKey:url];
1515 if (lastPosition && lastPosition.intValue > 0) {
1517 int settingValue = config_GetInt(VLCIntf, "macosx-continue-playback");
1518 NSInteger returnValue = NSAlertErrorReturn;
1520 if (settingValue == 0) {
1521 NSAlert *theAlert = [NSAlert alertWithMessageText:_NS("Continue playback?") defaultButton:_NS("Continue") alternateButton:_NS("Restart playback") otherButton:_NS("Always continue") informativeTextWithFormat:_NS("Playback of \"%@\" will continue at %@"), [NSString stringWithUTF8String:input_item_GetTitleFbName(p_item)], [[VLCStringUtility sharedInstance] stringForTime:lastPosition.intValue]];
1523 [[VLCCoreInteraction sharedInstance] pause];
1524 returnValue = [theAlert runModal];
1525 [[VLCCoreInteraction sharedInstance] playOrPause];
1528 if (returnValue == NSAlertAlternateReturn || settingValue == 2)
1529 lastPosition = [NSNumber numberWithInt:0];
1531 mtime_t lastPos = (mtime_t)lastPosition.intValue * 1000000;
1532 msg_Dbg(VLCIntf, "continuing playback at %lld", lastPos);
1533 var_SetTime(p_input_thread, "time", lastPos);
1535 if (returnValue == NSAlertOtherReturn)
1536 config_PutInt(VLCIntf, "macosx-continue-playback", 1);
1541 - (void)storePlaybackPositionForItem:(input_thread_t *)p_input_thread
1543 if (!var_InheritBool(VLCIntf, "macosx-recentitems"))
1546 input_item_t *p_item = input_GetItem(p_input_thread);
1550 if (![self isValidResumeItem:p_item])
1553 char *psz_url = decode_URI(input_item_GetURI(p_item));
1556 NSString *url = toNSStr(psz_url);
1559 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
1560 NSMutableDictionary *mutDict = [[NSMutableDictionary alloc] initWithDictionary:[defaults objectForKey:@"recentlyPlayedMedia"]];
1562 float relativePos = var_GetFloat(p_input_thread, "position");
1563 mtime_t pos = var_GetTime(p_input_thread, "time") / 1000000;
1564 mtime_t dur = input_item_GetDuration(p_item) / 1000000;
1566 NSMutableArray *mediaList = [[defaults objectForKey:@"recentlyPlayedMediaList"] mutableCopy];
1568 if (relativePos > .05 && relativePos < .95 && dur > 180) {
1569 [mutDict setObject:[NSNumber numberWithInt:pos] forKey:url];
1571 [mediaList removeObject:url];
1572 [mediaList addObject:url];
1573 NSUInteger mediaListCount = mediaList.count;
1574 if (mediaListCount > 30) {
1575 for (NSUInteger x = 0; x < mediaListCount - 30; x++) {
1576 [mutDict removeObjectForKey:[mediaList objectAtIndex:0]];
1577 [mediaList removeObjectAtIndex:0];
1581 [mutDict removeObjectForKey:url];
1582 [mediaList removeObject:url];
1584 [defaults setObject:mutDict forKey:@"recentlyPlayedMedia"];
1585 [defaults setObject:mediaList forKey:@"recentlyPlayedMediaList"];
1586 [defaults synchronize];
1589 [mediaList release];
1594 @implementation VLCPlaylist (NSOutlineViewDataSource)
1596 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
1598 id o_value = [super outlineView: outlineView child: index ofItem: item];
1600 [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p", [o_value pointerValue]]];
1604 /* Required for drag & drop and reordering */
1605 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
1607 playlist_t *p_playlist = pl_Get(VLCIntf);
1609 /* First remove the items that were moved during the last drag & drop
1611 [o_items_array removeAllObjects];
1612 [o_nodes_array removeAllObjects];
1614 NSUInteger itemCount = [items count];
1616 for (NSUInteger i = 0 ; i < itemCount ; i++) {
1617 id o_item = [items objectAtIndex:i];
1619 /* Fill the items and nodes to move in 2 different arrays */
1620 if (((playlist_item_t *)[o_item pointerValue])->i_children > 0)
1621 [o_nodes_array addObject: o_item];
1623 [o_items_array addObject: o_item];
1626 /* Now we need to check if there are selected items that are in already
1627 selected nodes. In that case, we only want to move the nodes */
1628 [self removeItemsFrom: o_nodes_array ifChildrenOf: o_nodes_array];
1629 [self removeItemsFrom: o_items_array ifChildrenOf: o_nodes_array];
1631 /* We add the "VLCPlaylistItemPboardType" type to be able to recognize
1632 a Drop operation coming from the playlist. */
1634 [pboard declareTypes: [NSArray arrayWithObject:@"VLCPlaylistItemPboardType"] owner: self];
1635 [pboard setData:[NSData data] forType:@"VLCPlaylistItemPboardType"];
1640 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)index
1642 playlist_t *p_playlist = pl_Get(VLCIntf);
1643 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1645 if (!p_playlist) return NSDragOperationNone;
1647 /* Dropping ON items is not allowed if item is not a node */
1649 if (index == NSOutlineViewDropOnItemIndex &&
1650 ((playlist_item_t *)[item pointerValue])->i_children == -1) {
1651 return NSDragOperationNone;
1655 /* We refuse to drop an item in anything else than a child of the General
1656 Node. We still accept items that would be root nodes of the outlineview
1657 however, to allow drop in an empty playlist. */
1658 if (!(([self isItem: [item pointerValue] inNode: p_playlist->p_local_category checkItemExistence: NO locked: NO] ||
1659 (var_CreateGetBool(p_playlist, "media-library") && [self isItem: [item pointerValue] inNode: p_playlist->p_ml_category checkItemExistence: NO locked: NO])) || item == nil)) {
1660 return NSDragOperationNone;
1663 /* Drop from the Playlist */
1664 if ([[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"]) {
1665 NSUInteger count = [o_nodes_array count];
1666 for (NSUInteger i = 0 ; i < count ; i++) {
1667 /* We refuse to Drop in a child of an item we are moving */
1668 if ([self isItem: [item pointerValue] inNode: [[o_nodes_array objectAtIndex:i] pointerValue] checkItemExistence: NO locked:NO]) {
1669 return NSDragOperationNone;
1672 return NSDragOperationMove;
1674 /* Drop from the Finder */
1675 else if ([[o_pasteboard types] containsObject: NSFilenamesPboardType]) {
1676 return NSDragOperationGeneric;
1678 return NSDragOperationNone;
1681 - (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(NSInteger)index
1683 playlist_t * p_playlist = pl_Get(VLCIntf);
1684 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1686 /* Drag & Drop inside the playlist */
1687 if ([[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"]) {
1688 if (index == -1) // this is no valid target, sanitize to top of table
1692 playlist_item_t *p_new_parent, *p_item = NULL;
1693 NSArray *o_all_items = [o_nodes_array arrayByAddingObjectsFromArray: o_items_array];
1694 /* If the item is to be dropped as root item of the outline, make it a
1695 child of the respective general node, if is either the pl or the ml
1696 Else, choose the proposed parent as parent. */
1698 if ([self currentPlaylistRoot] == p_playlist->p_local_category || [self currentPlaylistRoot] == p_playlist->p_ml_category)
1699 p_new_parent = [self currentPlaylistRoot];
1704 p_new_parent = [item pointerValue];
1706 /* Make sure the proposed parent is a node.
1707 (This should never be true) */
1708 if (p_new_parent->i_children < 0)
1711 NSUInteger count = [o_all_items count];
1715 playlist_item_t **pp_items = (playlist_item_t **)calloc(count, sizeof(playlist_item_t*));
1721 for (NSUInteger i = 0; i < count; i++) {
1722 p_item = [[o_all_items objectAtIndex:i] pointerValue];
1724 pp_items[j++] = p_item;
1727 if (j == 0 || playlist_TreeMoveMany(p_playlist, j, pp_items, p_new_parent, index) != VLC_SUCCESS) {
1736 [self playlistUpdated];
1737 i_row = [o_outline_view rowForItem:[o_outline_dict objectForKey:[NSString stringWithFormat: @"%p", [[o_all_items objectAtIndex:0] pointerValue]]]];
1740 i_row = [o_outline_view rowForItem:[o_outline_dict objectForKey:[NSString stringWithFormat: @"%p", p_new_parent]]];
1742 [o_outline_view deselectAll: self];
1743 [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:i_row] byExtendingSelection:NO];
1744 [o_outline_view scrollRowToVisible: i_row];
1749 else if ([[o_pasteboard types] containsObject: NSFilenamesPboardType]) {
1750 if ([self currentPlaylistRoot] != p_playlist->p_local_category && [self currentPlaylistRoot] != p_playlist->p_ml_category)
1753 playlist_item_t *p_node = [item pointerValue];
1755 NSArray *o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
1756 sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)];
1757 NSUInteger count = [o_values count];
1758 NSMutableArray *o_array = [NSMutableArray arrayWithCapacity:count];
1759 input_thread_t * p_input = pl_CurrentInput(VLCIntf);
1761 if (count == 1 && p_input) {
1762 int i_result = input_AddSubtitleOSD(p_input, vlc_path2uri([[o_values objectAtIndex:0] UTF8String], NULL), true, true);
1763 vlc_object_release(p_input);
1764 if (i_result == VLC_SUCCESS)
1768 vlc_object_release(p_input);
1770 for (NSUInteger i = 0; i < count; i++) {
1771 NSDictionary *o_dic;
1772 char *psz_uri = vlc_path2uri([[o_values objectAtIndex:i] UTF8String], NULL);
1776 o_dic = [NSDictionary dictionaryWithObject:[NSString stringWithCString:psz_uri encoding:NSUTF8StringEncoding] forKey:@"ITEM_URL"];
1780 [o_array addObject: o_dic];
1784 [self appendArray:o_array atPos:index enqueue: YES];
1786 assert(p_node->i_children != -1);
1787 [self appendNodeArray:o_array inNode: p_node atPos:index enqueue:YES];