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"
55 #import <vlc_interface.h>
58 /*****************************************************************************
59 * VLCPlaylistView implementation
60 *****************************************************************************/
61 @implementation VLCPlaylistView
63 - (NSMenu *)menuForEvent:(NSEvent *)o_event
65 return([(VLCPlaylist *)[self delegate] menuForEvent: o_event]);
68 - (void)keyDown:(NSEvent *)o_event
72 if ([[o_event characters] length])
73 key = [[o_event characters] characterAtIndex: 0];
76 case NSDeleteCharacter:
77 case NSDeleteFunctionKey:
78 case NSDeleteCharFunctionKey:
79 case NSBackspaceCharacter:
80 [(VLCPlaylist *)[self delegate] deleteItem:self];
83 case NSEnterCharacter:
84 case NSCarriageReturnCharacter:
85 [(VLCPlaylist *)[[VLCMain sharedInstance] playlist] playItem:nil];
89 [super keyDown: o_event];
94 - (BOOL)validateMenuItem:(NSMenuItem *)item
96 if (([self numberOfSelectedRows] >= 1 && [item action] == @selector(delete:)) || [item action] == @selector(selectAll:))
102 - (BOOL)acceptsFirstResponder
107 - (BOOL)becomeFirstResponder
109 [self setNeedsDisplay:YES];
113 - (BOOL)resignFirstResponder
115 [self setNeedsDisplay:YES];
119 - (IBAction)delete:(id)sender
121 [[[VLCMain sharedInstance] playlist] deleteItem: sender];
126 /*****************************************************************************
127 * VLCPlaylistCommon implementation
129 * This class the superclass of the VLCPlaylist and VLCPlaylistWizard.
130 * It contains the common methods and elements of these 2 entities.
131 *****************************************************************************/
132 @interface VLCPlaylistCommon ()
134 playlist_item_t * p_current_root_item;
138 @implementation VLCPlaylistCommon
142 playlist_t * p_playlist = pl_Get(VLCIntf);
143 p_current_root_item = p_playlist->p_local_category;
147 o_outline_dict = [[NSMutableDictionary alloc] init];
154 playlist_t * p_playlist = pl_Get(VLCIntf);
155 [o_outline_view setTarget: self];
156 [o_outline_view setDelegate: self];
157 [o_outline_view setDataSource: self];
158 [o_outline_view setAllowsEmptySelection: NO];
159 [o_outline_view expandItem: [o_outline_view itemAtRow:0]];
161 [o_outline_view_other setTarget: self];
162 [o_outline_view_other setDelegate: self];
163 [o_outline_view_other setDataSource: self];
164 [o_outline_view_other setAllowsEmptySelection: NO];
166 [[o_tc_name_other headerCell] setStringValue:_NS("Name")];
167 [[o_tc_author_other headerCell] setStringValue:_NS("Author")];
168 [[o_tc_duration_other headerCell] setStringValue:_NS("Duration")];
171 - (void)setPlaylistRoot: (playlist_item_t *)root_item
173 p_current_root_item = root_item;
174 [o_outline_view reloadData];
175 [o_outline_view_other reloadData];
178 - (playlist_item_t *)currentPlaylistRoot
180 return p_current_root_item;
183 - (NSOutlineView *)outlineView
185 return o_outline_view;
188 - (playlist_item_t *)selectedPlaylistItem
190 return [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
196 @implementation VLCPlaylistCommon (NSOutlineViewDataSource)
197 /* return the number of children for Obj-C pointer item */ /* DONE */
198 - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
201 playlist_item_t *p_item = NULL;
202 playlist_t * p_playlist = pl_Get(VLCIntf);
203 //assert(outlineView == o_outline_view);
207 p_item = p_current_root_item;
209 p_item = (playlist_item_t *)[item pointerValue];
212 i_return = p_item->i_children;
215 return i_return > 0 ? i_return : 0;
218 /* return the child at index for the Obj-C pointer item */ /* DONE */
219 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
221 playlist_item_t *p_return = NULL, *p_item = NULL;
223 playlist_t * p_playlist = pl_Get(VLCIntf);
227 p_item = p_current_root_item; /* root object */
229 p_item = (playlist_item_t *)[item pointerValue];
231 if (p_item && index < p_item->i_children && index >= 0)
232 p_return = p_item->pp_children[index];
235 o_value = [o_outline_dict objectForKey:[NSString stringWithFormat: @"%p", p_return]];
237 if (o_value == nil) {
238 /* FIXME: Why is there a warning if that happens all the time and seems
239 * to be normal? Add an assert and fix it.
240 * msg_Warn(VLCIntf, "playlist item misses pointer value, adding one"); */
241 o_value = [[NSValue valueWithPointer: p_return] retain];
246 /* is the item expandable */
247 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
250 playlist_t *p_playlist = pl_Get(VLCIntf);
255 if (p_current_root_item) {
256 i_return = p_current_root_item->i_children;
259 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
261 i_return = p_item->i_children;
265 return (i_return >= 0);
268 /* retrieve the string values for the cells */
269 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
273 playlist_item_t *p_item;
275 /* For error handling */
276 static BOOL attempted_reload = NO;
278 if (item == nil || ![item isKindOfClass: [NSValue class]]) {
279 /* Attempt to fix the error by asking for a data redisplay
280 * This might cause infinite loop, so add a small check */
281 if (!attempted_reload) {
282 attempted_reload = YES;
283 [outlineView reloadData];
288 p_item = (playlist_item_t *)[item pointerValue];
289 if (!p_item || !p_item->p_input) {
290 /* Attempt to fix the error by asking for a data redisplay
291 * This might cause infinite loop, so add a small check */
292 if (!attempted_reload) {
293 attempted_reload = YES;
294 [outlineView reloadData];
299 attempted_reload = NO;
300 NSString * o_identifier = [o_tc identifier];
302 if ([o_identifier isEqualToString:TRACKNUM_COLUMN]) {
303 psz_value = input_item_GetTrackNumber(p_item->p_input);
305 o_value = [NSString stringWithUTF8String:psz_value];
308 } else if ([o_identifier isEqualToString:TITLE_COLUMN]) {
309 /* sanity check to prevent the NSString class from crashing */
310 char *psz_title = input_item_GetTitleFbName(p_item->p_input);
312 o_value = [NSString stringWithUTF8String:psz_title];
315 } else if ([o_identifier isEqualToString:ARTIST_COLUMN]) {
316 psz_value = input_item_GetArtist(p_item->p_input);
318 o_value = [NSString stringWithUTF8String:psz_value];
321 } else if ([o_identifier isEqualToString:@"duration"]) {
322 char psz_duration[MSTRTIME_MAX_SIZE];
323 mtime_t dur = input_item_GetDuration(p_item->p_input);
325 secstotimestr(psz_duration, dur/1000000);
326 o_value = [NSString stringWithUTF8String:psz_duration];
330 } else if ([o_identifier isEqualToString:GENRE_COLUMN]) {
331 psz_value = input_item_GetGenre(p_item->p_input);
333 o_value = [NSString stringWithUTF8String:psz_value];
336 } else if ([o_identifier isEqualToString:ALBUM_COLUMN]) {
337 psz_value = input_item_GetAlbum(p_item->p_input);
339 o_value = [NSString stringWithUTF8String:psz_value];
342 } else if ([o_identifier isEqualToString:DESCRIPTION_COLUMN]) {
343 psz_value = input_item_GetDescription(p_item->p_input);
345 o_value = [NSString stringWithUTF8String:psz_value];
348 } else if ([o_identifier isEqualToString:DATE_COLUMN]) {
349 psz_value = input_item_GetDate(p_item->p_input);
351 o_value = [NSString stringWithUTF8String:psz_value];
354 } else if ([o_identifier isEqualToString:LANGUAGE_COLUMN]) {
355 psz_value = input_item_GetLanguage(p_item->p_input);
357 o_value = [NSString stringWithUTF8String:psz_value];
361 else if ([o_identifier isEqualToString:URI_COLUMN]) {
362 psz_value = decode_URI(input_item_GetURI(p_item->p_input));
364 o_value = [NSString stringWithUTF8String:psz_value];
368 else if ([o_identifier isEqualToString:FILESIZE_COLUMN]) {
369 psz_value = input_item_GetURI(p_item->p_input);
372 NSURL *url = [NSURL URLWithString:[NSString stringWithUTF8String:psz_value]];
373 if ([url isFileURL]) {
374 NSFileManager *fileManager = [NSFileManager defaultManager];
375 if ([fileManager fileExistsAtPath:[url path]]) {
377 NSDictionary *attributes = [fileManager attributesOfItemAtPath:[url path] error:&error];
378 o_value = [VLCByteCountFormatter stringFromByteCount:[attributes fileSize] countStyle:NSByteCountFormatterCountStyleDecimal];
384 else if ([o_identifier isEqualToString:@"status"]) {
385 if (input_item_HasErrorWhenReading(p_item->p_input)) {
386 o_value = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kAlertCautionIcon)];
387 [o_value setSize: NSMakeSize(16,16)];
396 /*****************************************************************************
397 * VLCPlaylistWizard implementation
398 *****************************************************************************/
399 @implementation VLCPlaylistWizard
401 - (IBAction)reloadOutlineView
403 /* Only reload the outlineview if the wizard window is open since this can
404 be quite long on big playlists */
405 if ([[o_outline_view window] isVisible])
406 [o_outline_view reloadData];
411 /*****************************************************************************
412 * An extension to NSOutlineView's interface to fix compilation warnings
413 * and let us access these 2 functions properly.
414 * This uses a private API, but works fine on all current OSX releases.
415 * Radar ID 11739459 request a public API for this. However, it is probably
416 * easier and faster to recreate similar looking bitmaps ourselves.
417 *****************************************************************************/
419 @interface NSOutlineView (UndocumentedSortImages)
420 + (NSImage *)_defaultTableHeaderSortImage;
421 + (NSImage *)_defaultTableHeaderReverseSortImage;
425 /*****************************************************************************
426 * VLCPlaylist implementation
427 *****************************************************************************/
428 @interface VLCPlaylist ()
430 NSImage *o_descendingSortingImage;
431 NSImage *o_ascendingSortingImage;
433 NSMutableArray *o_nodes_array;
434 NSMutableArray *o_items_array;
436 BOOL b_selected_item_met;
437 BOOL b_isSortDescending;
439 NSInteger retainedRowSelection;
441 BOOL b_playlistmenu_nib_loaded;
445 - (void)saveTableColumns;
448 @implementation VLCPlaylist
451 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
452 NSMutableArray * o_columnArray = [[NSMutableArray alloc] init];
453 [o_columnArray addObject: [NSArray arrayWithObjects:TITLE_COLUMN, [NSNumber numberWithFloat:190.], nil]];
454 [o_columnArray addObject: [NSArray arrayWithObjects:ARTIST_COLUMN, [NSNumber numberWithFloat:95.], nil]];
455 [o_columnArray addObject: [NSArray arrayWithObjects:DURATION_COLUMN, [NSNumber numberWithFloat:95.], nil]];
456 NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:[NSArray arrayWithArray:o_columnArray] forKey: @"PlaylistColumnSelection"];
458 [defaults registerDefaults:appDefaults];
459 [o_columnArray release];
466 o_nodes_array = [[NSMutableArray alloc] init];
467 o_items_array = [[NSMutableArray alloc] init];
474 [o_nodes_array release];
475 [o_items_array release];
484 playlist_t * p_playlist = pl_Get(VLCIntf);
486 [super awakeFromNib];
489 [o_outline_view setDoubleAction: @selector(playItem:)];
490 [o_outline_view_other setDoubleAction: @selector(playItem:)];
492 [o_outline_view registerForDraggedTypes: [NSArray arrayWithObjects:NSFilenamesPboardType, @"VLCPlaylistItemPboardType", nil]];
493 [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
495 [o_outline_view_other registerForDraggedTypes: [NSArray arrayWithObjects:NSFilenamesPboardType, @"VLCPlaylistItemPboardType", nil]];
496 [o_outline_view_other setIntercellSpacing: NSMakeSize (0.0, 1.0)];
498 /* This uses a private API, but works fine on all current OSX releases.
499 * Radar ID 11739459 request a public API for this. However, it is probably
500 * easier and faster to recreate similar looking bitmaps ourselves. */
501 o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
502 o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
504 o_tc_sortColumn = nil;
506 NSArray * o_columnArray = [[NSUserDefaults standardUserDefaults] arrayForKey:@"PlaylistColumnSelection"];
507 NSUInteger count = [o_columnArray count];
509 id o_menu = [[VLCMain sharedInstance] mainMenu];
512 NSMenu *o_context_menu = [o_menu setupPlaylistTableColumnsMenu];
513 [o_playlist_header setMenu: o_context_menu];
515 for (NSUInteger i = 0; i < count; i++) {
516 o_column = [[o_columnArray objectAtIndex:i] objectAtIndex:0];
517 if ([o_column isEqualToString:@"status"])
520 [o_menu setPlaylistColumnTableState: NSOnState forColumn: o_column];
521 [[o_outline_view tableColumnWithIdentifier: o_column] setWidth: [[[o_columnArray objectAtIndex:i] objectAtIndex:1] floatValue]];
524 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(applicationWillTerminate:) name: NSApplicationWillTerminateNotification object: nil];
529 - (void)applicationWillTerminate:(NSNotification *)notification
531 /* 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 */
532 [self saveTableColumns];
535 - (void)searchfieldChanged:(NSNotification *)o_notification
537 [o_search_field setStringValue:[[o_notification object] stringValue]];
542 [o_mi_play setTitle: _NS("Play")];
543 [o_mi_delete setTitle: _NS("Delete")];
544 [o_mi_recursive_expand setTitle: _NS("Expand Node")];
545 [o_mi_selectall setTitle: _NS("Select All")];
546 [o_mi_info setTitle: _NS("Media Information...")];
547 [o_mi_dl_cover_art setTitle: _NS("Download Cover Art")];
548 [o_mi_preparse setTitle: _NS("Fetch Meta Data")];
549 [o_mi_revealInFinder setTitle: _NS("Reveal in Finder")];
550 [o_mm_mi_revealInFinder setTitle: _NS("Reveal in Finder")];
551 [[o_mm_mi_revealInFinder menu] setAutoenablesItems: NO];
552 [o_mi_sort_name setTitle: _NS("Sort Node by Name")];
553 [o_mi_sort_author setTitle: _NS("Sort Node by Author")];
555 [o_search_field setToolTip: _NS("Search in Playlist")];
556 [o_search_field_other setToolTip: _NS("Search in Playlist")];
559 - (void)playlistUpdated
561 /* Clear indications of any existing column sorting */
562 NSUInteger count = [[o_outline_view tableColumns] count];
563 for (NSUInteger i = 0 ; i < count ; i++)
564 [o_outline_view setIndicatorImage:nil inTableColumn: [[o_outline_view tableColumns] objectAtIndex:i]];
566 [o_outline_view setHighlightedTableColumn:nil];
567 o_tc_sortColumn = nil;
568 // TODO Find a way to keep the dict size to a minimum
569 //[o_outline_dict removeAllObjects];
570 [o_outline_view reloadData];
571 [[[[VLCMain sharedInstance] wizard] playlistWizard] reloadOutlineView];
572 [[[[VLCMain sharedInstance] bookmarks] dataTable] reloadData];
574 [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:retainedRowSelection] byExtendingSelection:NO];
576 [self outlineViewSelectionDidChange: nil];
577 [[VLCMain sharedInstance] updateMainWindow];
580 - (void)outlineViewSelectionDidChange:(NSNotification *)notification
583 playlist_item_t * p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
586 /* update the state of our Reveal-in-Finder menu items */
587 NSMutableString *o_mrl;
588 char *psz_uri = input_item_GetURI(p_item->p_input);
590 [o_mi_revealInFinder setEnabled: NO];
591 [o_mm_mi_revealInFinder setEnabled: NO];
593 o_mrl = [NSMutableString stringWithUTF8String: psz_uri];
595 /* perform some checks whether it is a file and if it is local at all... */
596 NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
597 if (prefix_range.location != NSNotFound)
598 [o_mrl deleteCharactersInRange: prefix_range];
600 if ([o_mrl characterAtIndex:0] == '/') {
601 [o_mi_revealInFinder setEnabled: YES];
602 [o_mm_mi_revealInFinder setEnabled: YES];
607 /* update our info-panel to reflect the new item */
608 [[[VLCMain sharedInstance] info] updatePanelWithItem:p_item->p_input];
612 - (BOOL)isSelectionEmpty
614 return [o_outline_view selectedRow] == -1;
617 - (void)updateRowSelection
620 playlist_t *p_playlist = pl_Get(VLCIntf);
621 playlist_item_t *p_item, *p_temp_item;
622 NSMutableArray *o_array = [NSMutableArray array];
625 p_item = playlist_CurrentPlayingItem(p_playlist);
626 if (p_item == NULL) {
631 p_temp_item = p_item;
632 while(p_temp_item->p_parent) {
633 [o_array insertObject: [NSValue valueWithPointer: p_temp_item] atIndex: 0];
634 p_temp_item = p_temp_item->p_parent;
638 NSUInteger count = [o_array count];
639 for (NSUInteger j = 0; j < count - 1; j++) {
641 if ((o_item = [o_outline_dict objectForKey:
642 [NSString stringWithFormat: @"%p",
643 [[o_array objectAtIndex:j] pointerValue]]]) != nil) {
644 [o_outline_view expandItem: o_item];
648 id o_item = [o_outline_dict objectForKey:[NSString stringWithFormat: @"%p", p_item]];
649 NSInteger i_index = [o_outline_view rowForItem:o_item];
650 [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:i_index] byExtendingSelection:NO];
651 [o_outline_view setNeedsDisplay:YES];
654 /* Check if p_item is a child of p_node recursively. We need to check the item
655 existence first since OSX sometimes tries to redraw items that have been
656 deleted. We don't do it when not required since this verification takes
657 quite a long time on big playlists (yes, pretty hacky). */
659 - (BOOL)isItem: (playlist_item_t *)p_item inNode: (playlist_item_t *)p_node checkItemExistence:(BOOL)b_check locked:(BOOL)b_locked
661 playlist_t * p_playlist = pl_Get(VLCIntf);
662 playlist_item_t *p_temp_item = p_item;
667 if (p_node == p_item)
670 if (p_node->i_children < 1)
675 if (!b_locked) PL_LOCK;
678 /* Since outlineView: willDisplayCell:... may call this function with
679 p_items that don't exist anymore, first check if the item is still
680 in the playlist. Any cleaner solution welcomed. */
681 for (i = 0; i < p_playlist->all_items.i_size; i++) {
682 if (ARRAY_VAL(p_playlist->all_items, i) == p_item)
684 else if (i == p_playlist->all_items.i_size - 1)
686 if (!b_locked) PL_UNLOCK;
693 p_temp_item = p_temp_item->p_parent;
694 if (p_temp_item == p_node) {
695 if (!b_locked) PL_UNLOCK;
699 if (!b_locked) PL_UNLOCK;
704 /* This method is useful for instance to remove the selected children of an
705 already selected node */
706 - (void)removeItemsFrom:(id)o_items ifChildrenOf:(id)o_nodes
708 NSUInteger itemCount = [o_items count];
709 NSUInteger nodeCount = [o_nodes count];
710 for (NSUInteger i = 0 ; i < itemCount ; i++) {
711 for (NSUInteger j = 0 ; j < nodeCount ; j++) {
712 if (o_items == o_nodes) {
713 if (j == i) continue;
715 if ([self isItem: [[o_items objectAtIndex:i] pointerValue]
716 inNode: [[o_nodes objectAtIndex:j] pointerValue]
717 checkItemExistence: NO locked:NO]) {
718 [o_items removeObjectAtIndex:i];
719 /* We need to execute the next iteration with the same index
720 since the current item has been deleted */
728 - (IBAction)savePlaylist:(id)sender
730 playlist_t * p_playlist = pl_Get(VLCIntf);
732 NSSavePanel *o_save_panel = [NSSavePanel savePanel];
733 NSString * o_name = [NSString stringWithFormat: @"%@", _NS("Untitled")];
735 [NSBundle loadNibNamed:@"PlaylistAccessoryView" owner:self];
737 [o_save_accessory_text setStringValue: _NS("File Format:")];
738 [[o_save_accessory_popup itemAtIndex:0] setTitle: _NS("Extended M3U")];
739 [[o_save_accessory_popup itemAtIndex:1] setTitle: _NS("XML Shareable Playlist Format (XSPF)")];
740 [[o_save_accessory_popup itemAtIndex:2] setTitle: _NS("HTML playlist")];
742 [o_save_panel setTitle: _NS("Save Playlist")];
743 [o_save_panel setPrompt: _NS("Save")];
744 [o_save_panel setAccessoryView: o_save_accessory_view];
745 [o_save_panel setNameFieldStringValue: o_name];
747 if ([o_save_panel runModal] == NSFileHandlingPanelOKButton) {
748 NSString *o_filename = [[o_save_panel URL] path];
750 if ([o_save_accessory_popup indexOfSelectedItem] == 0) {
751 NSString * o_real_filename;
753 range.location = [o_filename length] - [@".m3u" length];
754 range.length = [@".m3u" length];
756 if ([o_filename compare:@".m3u" options: NSCaseInsensitiveSearch range: range] != NSOrderedSame)
757 o_real_filename = [NSString stringWithFormat: @"%@.m3u", o_filename];
759 o_real_filename = o_filename;
761 playlist_Export(p_playlist,
762 [o_real_filename fileSystemRepresentation],
763 p_playlist->p_local_category, "export-m3u");
764 } else if ([o_save_accessory_popup indexOfSelectedItem] == 1) {
765 NSString * o_real_filename;
767 range.location = [o_filename length] - [@".xspf" length];
768 range.length = [@".xspf" length];
770 if ([o_filename compare:@".xspf" options: NSCaseInsensitiveSearch range: range] != NSOrderedSame)
771 o_real_filename = [NSString stringWithFormat: @"%@.xspf", o_filename];
773 o_real_filename = o_filename;
775 playlist_Export(p_playlist,
776 [o_real_filename fileSystemRepresentation],
777 p_playlist->p_local_category, "export-xspf");
779 NSString * o_real_filename;
781 range.location = [o_filename length] - [@".html" length];
782 range.length = [@".html" length];
784 if ([o_filename compare:@".html" options: NSCaseInsensitiveSearch range: range] != NSOrderedSame)
785 o_real_filename = [NSString stringWithFormat: @"%@.html", o_filename];
787 o_real_filename = o_filename;
789 playlist_Export(p_playlist,
790 [o_real_filename fileSystemRepresentation],
791 p_playlist->p_local_category, "export-html");
796 /* When called retrieves the selected outlineview row and plays that node or item */
797 - (IBAction)playItem:(id)sender
799 intf_thread_t * p_intf = VLCIntf;
800 playlist_t * p_playlist = pl_Get(p_intf);
802 playlist_item_t *p_item;
803 playlist_item_t *p_node = NULL;
805 // ignore clicks on column header when handling double action
806 if (sender != nil && [o_outline_view clickedRow] == -1 && sender != o_mi_play)
809 p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
813 if (p_item->i_children == -1) {
814 p_node = p_item->p_parent;
817 if (p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1)
818 p_item = p_node->pp_children[0];
822 playlist_Control(p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, p_node, p_item);
827 - (IBAction)revealItemInFinder:(id)sender
829 NSIndexSet * selectedRows = [o_outline_view selectedRowIndexes];
830 NSUInteger count = [selectedRows count];
831 NSUInteger indexes[count];
832 [selectedRows getIndexes:indexes maxCount:count inIndexRange:nil];
834 NSMutableString * o_mrl;
835 playlist_item_t *p_item;
836 for (NSUInteger i = 0; i < count; i++) {
837 p_item = [[o_outline_view itemAtRow:indexes[i]] pointerValue];
839 if (! p_item || !p_item->p_input)
842 char * psz_url = decode_URI(input_item_GetURI(p_item->p_input));
843 o_mrl = [[NSMutableString alloc] initWithString: [NSString stringWithUTF8String:psz_url ? psz_url : ""]];
847 /* perform some checks whether it is a file and if it is local at all... */
848 if ([o_mrl length] > 0) {
849 NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
850 if (prefix_range.location != NSNotFound)
851 [o_mrl deleteCharactersInRange: prefix_range];
853 if ([o_mrl characterAtIndex:0] == '/')
854 [[NSWorkspace sharedWorkspace] selectFile: o_mrl inFileViewerRootedAtPath: o_mrl];
861 /* When called retrieves the selected outlineview row and plays that node or item */
862 - (IBAction)preparseItem:(id)sender
865 NSIndexSet *o_selected_indexes;
866 intf_thread_t * p_intf = VLCIntf;
867 playlist_t * p_playlist = pl_Get(p_intf);
868 playlist_item_t *p_item = NULL;
870 o_selected_indexes = [o_outline_view selectedRowIndexes];
871 i_count = [o_selected_indexes count];
873 NSUInteger indexes[i_count];
874 [o_selected_indexes getIndexes:indexes maxCount:i_count inIndexRange:nil];
875 for (int i = 0; i < i_count; i++) {
876 p_item = [[o_outline_view itemAtRow:indexes[i]] pointerValue];
877 [o_outline_view deselectRow: indexes[i]];
880 if (p_item->i_children == -1)
881 libvlc_MetaRequest(p_intf->p_libvlc, p_item->p_input);
883 msg_Dbg(p_intf, "preparsing nodes not implemented");
886 [self playlistUpdated];
889 - (IBAction)downloadCoverArt:(id)sender
892 NSIndexSet *o_selected_indexes;
893 intf_thread_t * p_intf = VLCIntf;
894 playlist_t * p_playlist = pl_Get(p_intf);
895 playlist_item_t *p_item = NULL;
897 o_selected_indexes = [o_outline_view selectedRowIndexes];
898 i_count = [o_selected_indexes count];
900 NSUInteger indexes[i_count];
901 [o_selected_indexes getIndexes:indexes maxCount:i_count inIndexRange:nil];
902 for (int i = 0; i < i_count; i++) {
903 p_item = [[o_outline_view itemAtRow: indexes[i]] pointerValue];
905 if (p_item && p_item->i_children == -1)
906 libvlc_ArtRequest(p_intf->p_libvlc, p_item->p_input);
908 [self playlistUpdated];
911 - (IBAction)selectAll:(id)sender
913 [o_outline_view selectAll: nil];
916 - (IBAction)showInfoPanel:(id)sender
918 [[[VLCMain sharedInstance] info] initPanel];
921 - (IBAction)deleteItem:(id)sender
924 NSIndexSet *o_selected_indexes;
925 playlist_t * p_playlist;
926 intf_thread_t * p_intf = VLCIntf;
928 o_selected_indexes = [o_outline_view selectedRowIndexes];
929 i_count = [o_selected_indexes count];
930 retainedRowSelection = [o_selected_indexes firstIndex];
932 p_playlist = pl_Get(p_intf);
934 NSUInteger indexes[i_count];
935 if (i_count == [o_outline_view numberOfRows]) {
937 playlist_NodeDelete(p_playlist, [self currentPlaylistRoot], true, false);
939 [self playlistUpdated];
942 [o_selected_indexes getIndexes:indexes maxCount:i_count inIndexRange:nil];
943 for (int i = 0; i < i_count; i++) {
944 id o_item = [o_outline_view itemAtRow: indexes[i]];
945 [o_outline_view deselectRow: indexes[i]];
948 playlist_item_t *p_item = [o_item pointerValue];
949 if (!p_item || !p_item->p_input) {
954 if (p_item->i_children != -1) {
955 //is a node and not an item
956 if (playlist_Status(p_playlist) != PLAYLIST_STOPPED &&
957 [self isItem: playlist_CurrentPlayingItem(p_playlist) inNode: ((playlist_item_t *)[o_item pointerValue])
958 checkItemExistence: NO locked:YES] == YES)
959 // if current item is in selected node and is playing then stop playlist
960 playlist_Control(p_playlist, PLAYLIST_STOP, pl_Locked);
962 playlist_NodeDelete(p_playlist, p_item, true, false);
964 playlist_DeleteFromInput(p_playlist, p_item->p_input, pl_Locked);
967 [o_outline_dict removeObjectForKey:[NSString stringWithFormat:@"%p", [o_item pointerValue]]];
971 [self playlistUpdated];
974 - (IBAction)sortNodeByName:(id)sender
976 [self sortNode: SORT_TITLE];
979 - (IBAction)sortNodeByAuthor:(id)sender
981 [self sortNode: SORT_ARTIST];
984 - (void)sortNode:(int)i_mode
986 playlist_t * p_playlist = pl_Get(VLCIntf);
987 playlist_item_t * p_item;
989 if ([o_outline_view selectedRow] > -1) {
990 p_item = [[o_outline_view itemAtRow: [o_outline_view selectedRow]] pointerValue];
994 p_item = [self currentPlaylistRoot]; // If no item is selected, sort the whole playlist
997 if (p_item->i_children > -1) // the item is a node
998 playlist_RecursiveNodeSort(p_playlist, p_item, i_mode, ORDER_NORMAL);
1000 playlist_RecursiveNodeSort(p_playlist, p_item->p_parent, i_mode, ORDER_NORMAL);
1003 [self playlistUpdated];
1006 - (input_item_t *)createItem:(NSDictionary *)o_one_item
1008 intf_thread_t * p_intf = VLCIntf;
1009 playlist_t * p_playlist = pl_Get(p_intf);
1011 input_item_t *p_input;
1012 BOOL b_rem = FALSE, b_dir = FALSE, b_writable = FALSE;
1013 NSString *o_uri, *o_name, *o_path;
1019 o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
1020 o_nsurl = [NSURL URLWithString: o_uri];
1021 o_path = [o_nsurl path];
1022 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
1023 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
1025 if ([[NSFileManager defaultManager] fileExistsAtPath:o_path isDirectory:&b_dir] && b_dir &&
1026 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath:o_path isRemovable: &b_rem
1027 isWritable:&b_writable isUnmountable:NULL description:NULL type:NULL] && b_rem && !b_writable && [o_nsurl isFileURL]) {
1028 id o_vlc_open = [[VLCMain sharedInstance] open];
1030 NSString *diskType = [o_vlc_open getVolumeTypeFromMountPath: o_path];
1031 msg_Dbg(p_intf, "detected optical media of type %s in the file input", [diskType UTF8String]);
1033 if ([diskType isEqualToString: kVLCMediaDVD])
1034 o_uri = [NSString stringWithFormat: @"dvdnav://%@", [o_vlc_open getBSDNodeFromMountPath: o_path]];
1035 else if ([diskType isEqualToString: kVLCMediaVideoTSFolder])
1036 o_uri = [NSString stringWithFormat: @"dvdnav://%@", o_path];
1037 else if ([diskType isEqualToString: kVLCMediaAudioCD])
1038 o_uri = [NSString stringWithFormat: @"cdda://%@", [o_vlc_open getBSDNodeFromMountPath: o_path]];
1039 else if ([diskType isEqualToString: kVLCMediaVCD])
1040 o_uri = [NSString stringWithFormat: @"vcd://%@#0:0", [o_vlc_open getBSDNodeFromMountPath: o_path]];
1041 else if ([diskType isEqualToString: kVLCMediaSVCD])
1042 o_uri = [NSString stringWithFormat: @"vcd://%@@0:0", [o_vlc_open getBSDNodeFromMountPath: o_path]];
1043 else if ([diskType isEqualToString: kVLCMediaBD] || [diskType isEqualToString: kVLCMediaBDMVFolder])
1044 o_uri = [NSString stringWithFormat: @"bluray://%@", o_path];
1046 msg_Warn(VLCIntf, "unknown disk type, treating %s as regular input", [o_path UTF8String]);
1048 p_input = input_item_New([o_uri UTF8String], [[[NSFileManager defaultManager] displayNameAtPath: o_path] UTF8String]);
1051 p_input = input_item_New([o_uri fileSystemRepresentation], o_name ? [o_name UTF8String] : NULL);
1057 NSUInteger count = [o_options count];
1058 for (NSUInteger i = 0; i < count; i++)
1059 input_item_AddOption(p_input, [[o_options objectAtIndex:i] UTF8String], VLC_INPUT_OPTION_TRUSTED);
1062 /* Recent documents menu */
1063 if (o_nsurl != nil && (BOOL)config_GetInt(p_playlist, "macosx-recentitems") == YES)
1064 [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL: o_nsurl];
1069 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
1071 playlist_t * p_playlist = pl_Get(VLCIntf);
1072 NSUInteger count = [o_array count];
1073 BOOL b_usingPlaylist;
1074 if ([self currentPlaylistRoot] == p_playlist->p_ml_category)
1075 b_usingPlaylist = NO;
1077 b_usingPlaylist = YES;
1080 for (NSUInteger i_item = 0; i_item < count; i_item++) {
1081 input_item_t *p_input;
1082 NSDictionary *o_one_item;
1085 o_one_item = [o_array objectAtIndex:i_item];
1086 p_input = [self createItem: o_one_item];
1091 int returnValue = playlist_AddInput(p_playlist, p_input, PLAYLIST_INSERT, i_position == -1 ? PLAYLIST_END : i_position + i_item, b_usingPlaylist, pl_Locked);
1092 if (returnValue != VLC_SUCCESS) {
1093 vlc_gc_decref(p_input);
1097 if (i_item == 0 && !b_enqueue) {
1098 playlist_item_t *p_item = playlist_ItemGetByInput(p_playlist, p_input);
1099 playlist_Control(p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, p_item->p_parent, p_item);
1102 vlc_gc_decref(p_input);
1105 [self playlistUpdated];
1108 - (void)appendNodeArray:(NSArray*)o_array inNode:(playlist_item_t *)p_node atPos:(int)i_position enqueue:(BOOL)b_enqueue
1110 playlist_t * p_playlist = pl_Get(VLCIntf);
1111 NSUInteger count = [o_array count];
1113 for (NSUInteger i_item = 0; i_item < count; i_item++) {
1114 input_item_t *p_input;
1115 NSDictionary *o_one_item;
1118 o_one_item = [o_array objectAtIndex:i_item];
1119 p_input = [self createItem: o_one_item];
1126 playlist_NodeAddInput(p_playlist, p_input, p_node,
1129 PLAYLIST_END : i_position + i_item,
1133 if (i_item == 0 && !b_enqueue) {
1134 playlist_item_t *p_item;
1135 p_item = playlist_ItemGetByInput(p_playlist, p_input);
1136 playlist_Control(p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, p_node, p_item);
1139 vlc_gc_decref(p_input);
1141 [self playlistUpdated];
1144 - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
1146 playlist_t *p_playlist = pl_Get(VLCIntf);
1147 playlist_item_t *p_selected_item;
1150 i_selected_row = [o_outline_view selectedRow];
1151 if (i_selected_row < 0)
1154 p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow: i_selected_row] pointerValue];
1156 for (NSUInteger i_current = 0; i_current < p_item->i_children ; i_current++) {
1158 NSString *o_current_name, *o_current_author;
1161 o_current_name = [NSString stringWithUTF8String:p_item->pp_children[i_current]->p_input->psz_name];
1162 psz_temp = input_item_GetInfo(p_item->p_input, _("Meta-information"),_("Artist"));
1163 o_current_author = [NSString stringWithUTF8String:psz_temp];
1167 if (p_selected_item == p_item->pp_children[i_current] && b_selected_item_met == NO)
1168 b_selected_item_met = YES;
1169 else if (p_selected_item == p_item->pp_children[i_current] && b_selected_item_met == YES)
1171 else if (b_selected_item_met == YES &&
1172 ([o_current_name rangeOfString:[o_search_field
1173 stringValue] options:NSCaseInsensitiveSearch].length ||
1174 [o_current_author rangeOfString:[o_search_field
1175 stringValue] options:NSCaseInsensitiveSearch].length))
1176 /*Adds the parent items in the result array as well, so that we can
1178 return [NSMutableArray arrayWithObject: [NSValue valueWithPointer: p_item->pp_children[i_current]]];
1180 if (p_item->pp_children[i_current]->i_children > 0) {
1181 id o_result = [self subSearchItem:
1182 p_item->pp_children[i_current]];
1183 if (o_result != NULL) {
1184 [o_result insertObject: [NSValue valueWithPointer:
1185 p_item->pp_children[i_current]] atIndex:0];
1193 - (IBAction)searchItem:(id)sender
1195 playlist_t * p_playlist = pl_Get(VLCIntf);
1200 b_selected_item_met = NO;
1202 /* First, only search after the selected item:
1203 * (b_selected_item_met = NO) */
1204 o_result = [self subSearchItem:[self currentPlaylistRoot]];
1205 if (o_result == NULL)
1206 /* If the first search failed, search again from the beginning */
1207 o_result = [self subSearchItem:[self currentPlaylistRoot]];
1209 if (o_result != NULL) {
1211 if ([[o_result objectAtIndex:0] pointerValue] == p_playlist->p_local_category)
1215 NSUInteger count = [o_result count];
1217 for (NSUInteger i = i_start ; i < count - 1 ; i++) {
1218 [o_outline_view expandItem: [o_outline_dict objectForKey:
1219 [NSString stringWithFormat: @"%p",
1220 [[o_result objectAtIndex:i] pointerValue]]]];
1222 i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
1223 [NSString stringWithFormat: @"%p",
1224 [[o_result objectAtIndex:count - 1 ]
1228 [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:i_row] byExtendingSelection:NO];
1229 [o_outline_view scrollRowToVisible: i_row];
1233 - (IBAction)recursiveExpandNode:(id)sender
1235 NSIndexSet * selectedRows = [o_outline_view selectedRowIndexes];
1236 NSUInteger count = [selectedRows count];
1237 NSUInteger indexes[count];
1238 [selectedRows getIndexes:indexes maxCount:count inIndexRange:nil];
1241 playlist_item_t *p_item;
1242 for (NSUInteger i = 0; i < count; i++) {
1243 o_item = [o_outline_view itemAtRow: indexes[i]];
1244 p_item = (playlist_item_t *)[o_item pointerValue];
1246 if (![[o_outline_view dataSource] outlineView: o_outline_view isItemExpandable: o_item])
1247 o_item = [o_outline_dict objectForKey: [NSString stringWithFormat: @"%p", p_item->p_parent]];
1249 /* We need to collapse the node first, since OSX refuses to recursively
1250 expand an already expanded node, even if children nodes are collapsed. */
1251 [o_outline_view collapseItem: o_item collapseChildren: YES];
1252 [o_outline_view expandItem: o_item expandChildren: YES];
1254 selectedRows = [o_outline_view selectedRowIndexes];
1255 [selectedRows getIndexes:indexes maxCount:count inIndexRange:nil];
1259 - (NSMenu *)menuForEvent:(NSEvent *)o_event
1261 if (!b_playlistmenu_nib_loaded)
1262 b_playlistmenu_nib_loaded = [NSBundle loadNibNamed:@"PlaylistMenu" owner:self];
1268 pt = [o_outline_view convertPoint: [o_event locationInWindow] fromView: nil];
1269 int row = [o_outline_view rowAtPoint:pt];
1270 if (row != -1 && ![[o_outline_view selectedRowIndexes] containsIndex: row])
1271 [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
1273 b_item_sel = (row != -1 && [o_outline_view selectedRow] != -1);
1274 b_rows = [o_outline_view numberOfRows] != 0;
1276 [o_mi_play setEnabled: b_item_sel];
1277 [o_mi_delete setEnabled: b_item_sel];
1278 [o_mi_selectall setEnabled: b_rows];
1279 [o_mi_info setEnabled: b_item_sel];
1280 [o_mi_preparse setEnabled: b_item_sel];
1281 [o_mi_recursive_expand setEnabled: b_item_sel];
1282 [o_mi_sort_name setEnabled: b_item_sel];
1283 [o_mi_sort_author setEnabled: b_item_sel];
1288 - (void)outlineView: (NSOutlineView *)o_tv didClickTableColumn:(NSTableColumn *)o_tc
1290 int i_mode, i_type = 0;
1291 intf_thread_t *p_intf = VLCIntf;
1292 NSString * o_identifier = [o_tc identifier];
1294 playlist_t *p_playlist = pl_Get(p_intf);
1296 if ([o_identifier isEqualToString:TRACKNUM_COLUMN])
1297 i_mode = SORT_TRACK_NUMBER;
1298 else if ([o_identifier isEqualToString:TITLE_COLUMN])
1299 i_mode = SORT_TITLE;
1300 else if ([o_identifier isEqualToString:ARTIST_COLUMN])
1301 i_mode = SORT_ARTIST;
1302 else if ([o_identifier isEqualToString:GENRE_COLUMN])
1303 i_mode = SORT_GENRE;
1304 else if ([o_identifier isEqualToString:DURATION_COLUMN])
1305 i_mode = SORT_DURATION;
1306 else if ([o_identifier isEqualToString:ALBUM_COLUMN])
1307 i_mode = SORT_ALBUM;
1308 else if ([o_identifier isEqualToString:DESCRIPTION_COLUMN])
1309 i_mode = SORT_DESCRIPTION;
1310 else if ([o_identifier isEqualToString:URI_COLUMN])
1315 if (o_tc_sortColumn == o_tc)
1316 b_isSortDescending = !b_isSortDescending;
1318 b_isSortDescending = false;
1320 if (b_isSortDescending)
1321 i_type = ORDER_REVERSE;
1323 i_type = ORDER_NORMAL;
1326 playlist_RecursiveNodeSort(p_playlist, [self currentPlaylistRoot], i_mode, i_type);
1329 [self playlistUpdated];
1331 o_tc_sortColumn = o_tc;
1332 [o_outline_view setHighlightedTableColumn:o_tc];
1334 if (b_isSortDescending)
1335 [o_outline_view setIndicatorImage:o_descendingSortingImage inTableColumn:o_tc];
1337 [o_outline_view setIndicatorImage:o_ascendingSortingImage inTableColumn:o_tc];
1341 - (void)outlineView:(NSOutlineView *)outlineView
1342 willDisplayCell:(id)cell
1343 forTableColumn:(NSTableColumn *)tableColumn
1346 /* this method can be called when VLC is already dead, hence the extra checks */
1347 intf_thread_t * p_intf = VLCIntf;
1350 playlist_t *p_playlist = pl_Get(p_intf);
1357 o_playing_item = [o_outline_dict objectForKey: [NSString stringWithFormat:@"%p", playlist_CurrentPlayingItem(p_playlist)]];
1360 if ([self isItem: [o_playing_item pointerValue] inNode: [item pointerValue] checkItemExistence:YES locked:NO]
1361 || [o_playing_item isEqual: item])
1362 [cell setFont: [[NSFontManager sharedFontManager] convertFont:[cell font] toHaveTrait:NSBoldFontMask]];
1364 [cell setFont: [[NSFontManager sharedFontManager] convertFont:[cell font] toNotHaveTrait:NSBoldFontMask]];
1369 playlist_t *p_playlist = pl_Get(VLCIntf);
1374 o_playing_item = [o_outline_dict objectForKey: [NSString stringWithFormat:@"%p", playlist_CurrentPlayingItem(p_playlist)]];
1377 return o_playing_item;
1380 - (NSArray *)draggedItems
1382 return [[o_nodes_array arrayByAddingObjectsFromArray: o_items_array] retain];
1385 - (void)setColumn: (NSString *)o_column state: (NSInteger)i_state translationDict:(NSDictionary *)o_dict
1387 NSTableColumn * o_work_tc;
1389 if (i_state == NSOnState) {
1390 o_work_tc = [[NSTableColumn alloc] initWithIdentifier: o_column];
1391 [o_work_tc setEditable: NO];
1392 [[o_work_tc dataCell] setFont: [NSFont controlContentFontOfSize:11.]];
1394 [[o_work_tc headerCell] setStringValue: [o_dict objectForKey:o_column]];
1396 if ([o_column isEqualToString: TRACKNUM_COLUMN]) {
1397 [o_work_tc setWidth: 20.];
1398 [o_work_tc setResizingMask: NSTableColumnNoResizing];
1399 [[o_work_tc headerCell] setStringValue: @"#"];
1402 [o_outline_view addTableColumn: o_work_tc];
1403 [o_work_tc release];
1404 [o_outline_view reloadData];
1405 [o_outline_view setNeedsDisplay: YES];
1408 [o_outline_view removeTableColumn: [o_outline_view tableColumnWithIdentifier: o_column]];
1410 [o_outline_view setOutlineTableColumn: [o_outline_view tableColumnWithIdentifier:TITLE_COLUMN]];
1413 - (void)saveTableColumns
1415 NSMutableArray * o_arrayToSave = [[NSMutableArray alloc] init];
1416 NSArray * o_columns = [[NSArray alloc] initWithArray:[o_outline_view tableColumns]];
1417 NSUInteger count = [o_columns count];
1418 NSTableColumn * o_currentColumn;
1419 for (NSUInteger i = 0; i < count; i++) {
1420 o_currentColumn = [o_columns objectAtIndex:i];
1421 [o_arrayToSave addObject:[NSArray arrayWithObjects:[o_currentColumn identifier], [NSNumber numberWithFloat:[o_currentColumn width]], nil]];
1423 [[NSUserDefaults standardUserDefaults] setObject: o_arrayToSave forKey:@"PlaylistColumnSelection"];
1424 [[NSUserDefaults standardUserDefaults] synchronize];
1425 [o_columns release];
1426 [o_arrayToSave release];
1431 @implementation VLCPlaylist (NSOutlineViewDataSource)
1433 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
1435 id o_value = [super outlineView: outlineView child: index ofItem: item];
1437 [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p", [o_value pointerValue]]];
1441 /* Required for drag & drop and reordering */
1442 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
1444 playlist_t *p_playlist = pl_Get(VLCIntf);
1446 /* First remove the items that were moved during the last drag & drop
1448 [o_items_array removeAllObjects];
1449 [o_nodes_array removeAllObjects];
1451 NSUInteger itemCount = [items count];
1453 for (NSUInteger i = 0 ; i < itemCount ; i++) {
1454 id o_item = [items objectAtIndex:i];
1456 /* Fill the items and nodes to move in 2 different arrays */
1457 if (((playlist_item_t *)[o_item pointerValue])->i_children > 0)
1458 [o_nodes_array addObject: o_item];
1460 [o_items_array addObject: o_item];
1463 /* Now we need to check if there are selected items that are in already
1464 selected nodes. In that case, we only want to move the nodes */
1465 [self removeItemsFrom: o_nodes_array ifChildrenOf: o_nodes_array];
1466 [self removeItemsFrom: o_items_array ifChildrenOf: o_nodes_array];
1468 /* We add the "VLCPlaylistItemPboardType" type to be able to recognize
1469 a Drop operation coming from the playlist. */
1471 [pboard declareTypes: [NSArray arrayWithObject:@"VLCPlaylistItemPboardType"] owner: self];
1472 [pboard setData:[NSData data] forType:@"VLCPlaylistItemPboardType"];
1477 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)index
1479 playlist_t *p_playlist = pl_Get(VLCIntf);
1480 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1482 if (!p_playlist) return NSDragOperationNone;
1484 /* Dropping ON items is not allowed if item is not a node */
1486 if (index == NSOutlineViewDropOnItemIndex &&
1487 ((playlist_item_t *)[item pointerValue])->i_children == -1) {
1488 return NSDragOperationNone;
1492 /* We refuse to drop an item in anything else than a child of the General
1493 Node. We still accept items that would be root nodes of the outlineview
1494 however, to allow drop in an empty playlist. */
1495 if (!(([self isItem: [item pointerValue] inNode: p_playlist->p_local_category checkItemExistence: NO locked: NO] ||
1496 (var_CreateGetBool(p_playlist, "media-library") && [self isItem: [item pointerValue] inNode: p_playlist->p_ml_category checkItemExistence: NO locked: NO])) || item == nil)) {
1497 return NSDragOperationNone;
1500 /* Drop from the Playlist */
1501 if ([[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"]) {
1502 NSUInteger count = [o_nodes_array count];
1503 for (NSUInteger i = 0 ; i < count ; i++) {
1504 /* We refuse to Drop in a child of an item we are moving */
1505 if ([self isItem: [item pointerValue] inNode: [[o_nodes_array objectAtIndex:i] pointerValue] checkItemExistence: NO locked:NO]) {
1506 return NSDragOperationNone;
1509 return NSDragOperationMove;
1511 /* Drop from the Finder */
1512 else if ([[o_pasteboard types] containsObject: NSFilenamesPboardType]) {
1513 return NSDragOperationGeneric;
1515 return NSDragOperationNone;
1518 - (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(NSInteger)index
1520 playlist_t * p_playlist = pl_Get(VLCIntf);
1521 NSPasteboard *o_pasteboard = [info draggingPasteboard];
1523 /* Drag & Drop inside the playlist */
1524 if ([[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"]) {
1525 if (index == -1) // this is no valid target, sanitize to top of table
1529 playlist_item_t *p_new_parent, *p_item = NULL;
1530 NSArray *o_all_items = [o_nodes_array arrayByAddingObjectsFromArray: o_items_array];
1531 /* If the item is to be dropped as root item of the outline, make it a
1532 child of the respective general node, if is either the pl or the ml
1533 Else, choose the proposed parent as parent. */
1535 if ([self currentPlaylistRoot] == p_playlist->p_local_category || [self currentPlaylistRoot] == p_playlist->p_ml_category)
1536 p_new_parent = [self currentPlaylistRoot];
1541 p_new_parent = [item pointerValue];
1543 /* Make sure the proposed parent is a node.
1544 (This should never be true) */
1545 if (p_new_parent->i_children < 0)
1548 NSUInteger count = [o_all_items count];
1552 playlist_item_t **pp_items = (playlist_item_t **)calloc(count, sizeof(playlist_item_t*));
1558 for (NSUInteger i = 0; i < count; i++) {
1559 p_item = [[o_all_items objectAtIndex:i] pointerValue];
1561 pp_items[j++] = p_item;
1564 if (j == 0 || playlist_TreeMoveMany(p_playlist, j, pp_items, p_new_parent, index) != VLC_SUCCESS) {
1573 [self playlistUpdated];
1574 i_row = [o_outline_view rowForItem:[o_outline_dict objectForKey:[NSString stringWithFormat: @"%p", [[o_all_items objectAtIndex:0] pointerValue]]]];
1577 i_row = [o_outline_view rowForItem:[o_outline_dict objectForKey:[NSString stringWithFormat: @"%p", p_new_parent]]];
1579 [o_outline_view deselectAll: self];
1580 [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:i_row] byExtendingSelection:NO];
1581 [o_outline_view scrollRowToVisible: i_row];
1586 else if ([[o_pasteboard types] containsObject: NSFilenamesPboardType]) {
1587 if ([self currentPlaylistRoot] != p_playlist->p_local_category && [self currentPlaylistRoot] != p_playlist->p_ml_category)
1590 playlist_item_t *p_node = [item pointerValue];
1592 NSArray *o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
1593 sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)];
1594 NSUInteger count = [o_values count];
1595 NSMutableArray *o_array = [NSMutableArray arrayWithCapacity:count];
1596 input_thread_t * p_input = pl_CurrentInput(VLCIntf);
1597 BOOL b_returned = NO;
1599 if (count == 1 && p_input) {
1600 b_returned = input_AddSubtitle(p_input, vlc_path2uri([[o_values objectAtIndex:0] UTF8String], NULL), true);
1601 vlc_object_release(p_input);
1606 vlc_object_release(p_input);
1608 for (NSUInteger i = 0; i < count; i++) {
1609 NSDictionary *o_dic;
1610 char *psz_uri = vlc_path2uri([[o_values objectAtIndex:i] UTF8String], NULL);
1614 o_dic = [NSDictionary dictionaryWithObject:[NSString stringWithCString:psz_uri encoding:NSUTF8StringEncoding] forKey:@"ITEM_URL"];
1618 [o_array addObject: o_dic];
1622 [self appendArray:o_array atPos:index enqueue: YES];
1624 assert(p_node->i_children != -1);
1625 [self appendNodeArray:o_array inNode: p_node atPos:index enqueue:YES];