]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlist.m
63069e0f85eacd2e84bc14db7582c6e453b247db
[vlc] / modules / gui / macosx / playlist.m
1 /*****************************************************************************
2  * playlist.m: MacOS X interface module
3  *****************************************************************************
4 * Copyright (C) 2002-2014 VLC authors and VideoLAN
5  * $Id$
6  *
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>
11  *
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.
16  *
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.
21  *
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  *****************************************************************************/
26
27 /* TODO
28  * add 'icons' for different types of nodes? (http://www.cocoadev.com/index.pl?IconAndTextInTableCell)
29  * reimplement enable/disable item
30  */
31
32
33 /*****************************************************************************
34  * Preamble
35  *****************************************************************************/
36 #include <stdlib.h>                                      /* malloc(), free() */
37 #include <sys/param.h>                                    /* for MAXPATHLEN */
38 #include <string.h>
39 #include <math.h>
40 #include <sys/mount.h>
41
42 #import "CompatibilityFixes.h"
43
44 #import "intf.h"
45 #import "wizard.h"
46 #import "bookmarks.h"
47 #import "playlistinfo.h"
48 #import "playlist.h"
49 #import "controls.h"
50 #import "misc.h"
51 #import "open.h"
52 #import "MainMenu.h"
53 #import "CoreInteraction.h"
54
55 #include <vlc_keys.h>
56 #import <vlc_interface.h>
57 #include <vlc_url.h>
58
59 /*****************************************************************************
60  * VLCPlaylistView implementation
61  *****************************************************************************/
62 @implementation VLCPlaylistView
63
64 - (NSMenu *)menuForEvent:(NSEvent *)o_event
65 {
66     return([(VLCPlaylist *)[self delegate] menuForEvent: o_event]);
67 }
68
69 - (void)keyDown:(NSEvent *)o_event
70 {
71     unichar key = 0;
72
73     if ([[o_event characters] length])
74         key = [[o_event characters] characterAtIndex: 0];
75
76     switch(key) {
77         case NSDeleteCharacter:
78         case NSDeleteFunctionKey:
79         case NSDeleteCharFunctionKey:
80         case NSBackspaceCharacter:
81             [(VLCPlaylist *)[self delegate] deleteItem:self];
82             break;
83
84         case NSEnterCharacter:
85         case NSCarriageReturnCharacter:
86             [(VLCPlaylist *)[[VLCMain sharedInstance] playlist] playItem:nil];
87             break;
88
89         default:
90             [super keyDown: o_event];
91             break;
92     }
93 }
94
95 - (BOOL)validateMenuItem:(NSMenuItem *)item
96 {
97     if (([self numberOfSelectedRows] >= 1 && [item action] == @selector(delete:)) || [item action] == @selector(selectAll:))
98         return YES;
99
100     return NO;
101 }
102
103 - (BOOL)acceptsFirstResponder
104 {
105     return YES;
106 }
107
108 - (BOOL)becomeFirstResponder
109 {
110     [self setNeedsDisplay:YES];
111     return YES;
112 }
113
114 - (BOOL)resignFirstResponder
115 {
116     [self setNeedsDisplay:YES];
117     return YES;
118 }
119
120 - (IBAction)delete:(id)sender
121 {
122     [[[VLCMain sharedInstance] playlist] deleteItem: sender];
123 }
124
125 @end
126
127 /*****************************************************************************
128  * VLCPlaylistWizard implementation
129  *****************************************************************************/
130 @implementation VLCPlaylistWizard
131
132 - (IBAction)reloadOutlineView
133 {
134     /* Only reload the outlineview if the wizard window is open since this can
135        be quite long on big playlists */
136
137
138     // to be removed
139 }
140
141 @end
142
143 /*****************************************************************************
144  * An extension to NSOutlineView's interface to fix compilation warnings
145  * and let us access these 2 functions properly.
146  * This uses a private API, but works fine on all current OSX releases.
147  * Radar ID 11739459 request a public API for this. However, it is probably
148  * easier and faster to recreate similar looking bitmaps ourselves.
149  *****************************************************************************/
150
151 @interface NSOutlineView (UndocumentedSortImages)
152 + (NSImage *)_defaultTableHeaderSortImage;
153 + (NSImage *)_defaultTableHeaderReverseSortImage;
154 @end
155
156
157 /*****************************************************************************
158  * VLCPlaylist implementation
159  *****************************************************************************/
160 @interface VLCPlaylist ()
161 {
162     NSImage *o_descendingSortingImage;
163     NSImage *o_ascendingSortingImage;
164
165     BOOL b_selected_item_met;
166     BOOL b_isSortDescending;
167     id o_tc_sortColumn;
168     NSUInteger retainedRowSelection;
169
170     BOOL b_playlistmenu_nib_loaded;
171     BOOL b_view_setup;
172 }
173
174 - (void)saveTableColumns;
175 @end
176
177 @implementation VLCPlaylist
178
179 + (void)initialize
180 {
181     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
182     NSMutableArray *o_columnArray = [[NSMutableArray alloc] init];
183     [o_columnArray addObject: [NSArray arrayWithObjects:TITLE_COLUMN, [NSNumber numberWithFloat:190.], nil]];
184     [o_columnArray addObject: [NSArray arrayWithObjects:ARTIST_COLUMN, [NSNumber numberWithFloat:95.], nil]];
185     [o_columnArray addObject: [NSArray arrayWithObjects:DURATION_COLUMN, [NSNumber numberWithFloat:95.], nil]];
186
187     NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
188                                  [NSArray arrayWithArray:o_columnArray], @"PlaylistColumnSelection",
189                                  [NSArray array], @"recentlyPlayedMediaList",
190                                  [NSDictionary dictionary], @"recentlyPlayedMedia", nil];
191
192     [defaults registerDefaults:appDefaults];
193     [o_columnArray release];
194 }
195
196 - (PLModel *)model
197 {
198     return o_model;
199 }
200
201 - (void)reloadStyles
202 {
203     NSFont *fontToUse;
204     CGFloat rowHeight;
205     if (config_GetInt(VLCIntf, "macosx-large-text")) {
206         fontToUse = [NSFont systemFontOfSize:13.];
207         rowHeight = 21.;
208     } else {
209         fontToUse = [NSFont systemFontOfSize:11.];
210         rowHeight = 16.;
211     }
212
213     NSArray *columns = [o_outline_view tableColumns];
214     NSUInteger count = columns.count;
215     for (NSUInteger x = 0; x < count; x++)
216         [[[columns objectAtIndex:x] dataCell] setFont:fontToUse];
217     [o_outline_view setRowHeight:rowHeight];
218 }
219
220 - (void)dealloc
221 {
222     [super dealloc];
223 }
224
225 - (void)awakeFromNib
226 {
227     if (b_view_setup)
228         return;
229
230     playlist_t * p_playlist = pl_Get(VLCIntf);
231
232     [self reloadStyles];
233     [self initStrings];
234
235     o_model = [[PLModel alloc] initWithOutlineView:o_outline_view playlist:p_playlist rootItem:p_playlist->p_playing playlistObject:self];
236     [o_outline_view setDataSource:o_model];
237     [o_outline_view reloadData];
238
239     [o_outline_view setTarget: self];
240     [o_outline_view setDoubleAction: @selector(playItem:)];
241
242     [o_outline_view setAllowsEmptySelection: NO];
243     [o_outline_view registerForDraggedTypes: [NSArray arrayWithObjects:NSFilenamesPboardType, @"VLCPlaylistItemPboardType", nil]];
244     [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
245
246     /* This uses a private API, but works fine on all current OSX releases.
247      * Radar ID 11739459 request a public API for this. However, it is probably
248      * easier and faster to recreate similar looking bitmaps ourselves. */
249     o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
250     o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
251
252     o_tc_sortColumn = nil;
253
254     NSArray * o_columnArray = [[NSUserDefaults standardUserDefaults] arrayForKey:@"PlaylistColumnSelection"];
255     NSUInteger count = [o_columnArray count];
256
257     id o_menu = [[VLCMain sharedInstance] mainMenu];
258     NSString * o_column;
259
260     NSMenu *o_context_menu = [o_menu setupPlaylistTableColumnsMenu];
261     [o_playlist_header setMenu: o_context_menu];
262
263     for (NSUInteger i = 0; i < count; i++) {
264         o_column = [[o_columnArray objectAtIndex:i] objectAtIndex:0];
265         if ([o_column isEqualToString:@"status"])
266             continue;
267
268         if(![o_menu setPlaylistColumnTableState: NSOnState forColumn: o_column])
269             continue;
270
271         [[o_outline_view tableColumnWithIdentifier: o_column] setWidth: [[[o_columnArray objectAtIndex:i] objectAtIndex:1] floatValue]];
272     }
273
274     [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(applicationWillTerminate:) name: NSApplicationWillTerminateNotification object: nil];
275
276     b_view_setup = YES;
277 }
278
279 - (void)applicationWillTerminate:(NSNotification *)notification
280 {
281     /* let's make sure we save the correct widths and positions, since this likely changed since the last time the user played with the column selection */
282     [self saveTableColumns];
283 }
284
285 - (void)initStrings
286 {
287     [o_mi_play setTitle: _NS("Play")];
288     [o_mi_delete setTitle: _NS("Delete")];
289     [o_mi_recursive_expand setTitle: _NS("Expand Node")];
290     [o_mi_selectall setTitle: _NS("Select All")];
291     [o_mi_info setTitle: _NS("Media Information...")];
292     [o_mi_dl_cover_art setTitle: _NS("Download Cover Art")];
293     [o_mi_preparse setTitle: _NS("Fetch Meta Data")];
294     [o_mi_revealInFinder setTitle: _NS("Reveal in Finder")];
295     [o_mi_sort_name setTitle: _NS("Sort Node by Name")];
296     [o_mi_sort_author setTitle: _NS("Sort Node by Author")];
297
298     [o_search_field setToolTip: _NS("Search in Playlist")];
299 }
300
301 - (void)playlistUpdated
302 {
303     /* Clear indications of any existing column sorting */
304     NSUInteger count = [[o_outline_view tableColumns] count];
305     for (NSUInteger i = 0 ; i < count ; i++)
306         [o_outline_view setIndicatorImage:nil inTableColumn: [[o_outline_view tableColumns] objectAtIndex:i]];
307
308     [o_outline_view setHighlightedTableColumn:nil];
309     o_tc_sortColumn = nil;
310
311     [o_outline_view reloadData];
312     [[[[VLCMain sharedInstance] wizard] playlistWizard] reloadOutlineView];
313
314     [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:retainedRowSelection] byExtendingSelection:NO];
315
316     [self outlineViewSelectionDidChange: nil];
317     [[VLCMain sharedInstance] updateMainWindow];
318 }
319
320 - (void)outlineViewSelectionDidChange:(NSNotification *)notification
321 {
322 //    // FIXME: unsafe
323 //    playlist_item_t * p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
324 //
325 //    if (p_item) {
326 //        /* update the state of our Reveal-in-Finder menu items */
327 //        NSMutableString *o_mrl;
328 //        char *psz_uri = input_item_GetURI(p_item->p_input);
329 //
330 //        [o_mi_revealInFinder setEnabled: NO];
331 //        [o_mm_mi_revealInFinder setEnabled: NO];
332 //        if (psz_uri) {
333 //            o_mrl = [NSMutableString stringWithUTF8String: psz_uri];
334 //
335 //            /* perform some checks whether it is a file and if it is local at all... */
336 //            NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
337 //            if (prefix_range.location != NSNotFound)
338 //                [o_mrl deleteCharactersInRange: prefix_range];
339 //
340 //            if ([o_mrl characterAtIndex:0] == '/') {
341 //                [o_mi_revealInFinder setEnabled: YES];
342 //                [o_mm_mi_revealInFinder setEnabled: YES];
343 //            }
344 //            free(psz_uri);
345 //        }
346 //
347 //        /* update our info-panel to reflect the new item */
348 //        [[[VLCMain sharedInstance] info] updatePanelWithItem:p_item->p_input];
349 //    }
350 }
351
352 - (BOOL)isSelectionEmpty
353 {
354     return [o_outline_view selectedRow] == -1;
355 }
356
357 - (void)updateRowSelection
358 {
359     // FIXME: unsafe
360     playlist_t *p_playlist = pl_Get(VLCIntf);
361     playlist_item_t *p_item, *p_temp_item;
362     NSMutableArray *o_array = [NSMutableArray array];
363
364     // TODO Rework
365 //    PL_LOCK;
366 //    p_item = playlist_CurrentPlayingItem(p_playlist);
367 //    if (p_item == NULL) {
368 //        PL_UNLOCK;
369 //        return;
370 //    }
371 //
372 //    p_temp_item = p_item;
373 //    while(p_temp_item->p_parent) {
374 //        [o_array insertObject: [NSValue valueWithPointer: p_temp_item] atIndex: 0];
375 //        p_temp_item = p_temp_item->p_parent;
376 //    }
377 //    PL_UNLOCK;
378 //
379 //    NSUInteger count = [o_array count];
380 //    for (NSUInteger j = 0; j < count - 1; j++) {
381 //        id o_item;
382 //        if ((o_item = [o_outline_dict objectForKey:
383 //                            [NSString stringWithFormat: @"%p",
384 //                            [[o_array objectAtIndex:j] pointerValue]]]) != nil) {
385 //            [o_outline_view expandItem: o_item];
386 //        }
387 //    }
388 //
389 //    id o_item = [o_outline_dict objectForKey:[NSString stringWithFormat: @"%p", p_item]];
390 //    NSInteger i_index = [o_outline_view rowForItem:o_item];
391 //    [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:i_index] byExtendingSelection:NO];
392 //    [o_outline_view setNeedsDisplay:YES];
393 }
394
395 - (IBAction)savePlaylist:(id)sender
396 {
397     playlist_t * p_playlist = pl_Get(VLCIntf);
398
399     NSSavePanel *o_save_panel = [NSSavePanel savePanel];
400     NSString * o_name = [NSString stringWithFormat: @"%@", _NS("Untitled")];
401
402     [NSBundle loadNibNamed:@"PlaylistAccessoryView" owner:self];
403
404     [o_save_accessory_text setStringValue: _NS("File Format:")];
405     [[o_save_accessory_popup itemAtIndex:0] setTitle: _NS("Extended M3U")];
406     [[o_save_accessory_popup itemAtIndex:1] setTitle: _NS("XML Shareable Playlist Format (XSPF)")];
407     [[o_save_accessory_popup itemAtIndex:2] setTitle: _NS("HTML playlist")];
408
409     [o_save_panel setTitle: _NS("Save Playlist")];
410     [o_save_panel setPrompt: _NS("Save")];
411     [o_save_panel setAccessoryView: o_save_accessory_view];
412     [o_save_panel setNameFieldStringValue: o_name];
413
414     if ([o_save_panel runModal] == NSFileHandlingPanelOKButton) {
415         NSString *o_filename = [[o_save_panel URL] path];
416
417         if ([o_save_accessory_popup indexOfSelectedItem] == 0) {
418             NSString * o_real_filename;
419             NSRange range;
420             range.location = [o_filename length] - [@".m3u" length];
421             range.length = [@".m3u" length];
422
423             if ([o_filename compare:@".m3u" options: NSCaseInsensitiveSearch range: range] != NSOrderedSame)
424                 o_real_filename = [NSString stringWithFormat: @"%@.m3u", o_filename];
425             else
426                 o_real_filename = o_filename;
427
428             playlist_Export(p_playlist,
429                 [o_real_filename fileSystemRepresentation],
430                 p_playlist->p_local_category, "export-m3u");
431         } else if ([o_save_accessory_popup indexOfSelectedItem] == 1) {
432             NSString * o_real_filename;
433             NSRange range;
434             range.location = [o_filename length] - [@".xspf" length];
435             range.length = [@".xspf" length];
436
437             if ([o_filename compare:@".xspf" options: NSCaseInsensitiveSearch range: range] != NSOrderedSame)
438                 o_real_filename = [NSString stringWithFormat: @"%@.xspf", o_filename];
439             else
440                 o_real_filename = o_filename;
441
442             playlist_Export(p_playlist,
443                 [o_real_filename fileSystemRepresentation],
444                 p_playlist->p_local_category, "export-xspf");
445         } else {
446             NSString * o_real_filename;
447             NSRange range;
448             range.location = [o_filename length] - [@".html" length];
449             range.length = [@".html" length];
450
451             if ([o_filename compare:@".html" options: NSCaseInsensitiveSearch range: range] != NSOrderedSame)
452                 o_real_filename = [NSString stringWithFormat: @"%@.html", o_filename];
453             else
454                 o_real_filename = o_filename;
455
456             playlist_Export(p_playlist,
457                 [o_real_filename fileSystemRepresentation],
458                 p_playlist->p_local_category, "export-html");
459         }
460     }
461 }
462
463 /* When called retrieves the selected outlineview row and plays that node or item */
464 - (IBAction)playItem:(id)sender
465 {
466     playlist_t *p_playlist = pl_Get(VLCIntf);
467
468     // ignore clicks on column header when handling double action
469     if (sender == o_outline_view && [o_outline_view clickedRow] == -1)
470         return;
471
472     PLItem *o_item = [o_outline_view itemAtRow:[o_outline_view selectedRow]];
473     if (!o_item)
474         return;
475
476     PL_LOCK;
477     playlist_item_t *p_item = playlist_ItemGetById(p_playlist, [o_item plItemId]);
478     playlist_item_t *p_node = playlist_ItemGetById(p_playlist, [[[self model] rootItem] plItemId]);
479
480     if (p_item && p_node) {
481         playlist_Control(p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, p_node, p_item);
482     }
483     PL_UNLOCK;
484 }
485
486 - (IBAction)revealItemInFinder:(id)sender
487 {
488     NSIndexSet *selectedRows = [o_outline_view selectedRowIndexes];
489     [selectedRows enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
490
491         PLItem *o_item = [o_outline_view itemAtRow:idx];
492
493         /* perform some checks whether it is a file and if it is local at all... */
494         char *psz_url = input_item_GetURI([o_item input]);
495         NSURL *url = [NSURL URLWithString:toNSStr(psz_url)];
496         free(psz_url);
497         if (![url isFileURL])
498             return;
499         if (![[NSFileManager defaultManager] fileExistsAtPath:[url path]])
500             return;
501
502         msg_Dbg(VLCIntf, "Reveal url %s in finder", [[url path] UTF8String]);
503         [[NSWorkspace sharedWorkspace] selectFile: [url path] inFileViewerRootedAtPath: [url path]];
504     }];
505
506 }
507
508 /* When called retrieves the selected outlineview row and plays that node or item */
509 - (IBAction)preparseItem:(id)sender
510 {
511     int i_count;
512     NSIndexSet *o_selected_indexes;
513     intf_thread_t * p_intf = VLCIntf;
514     playlist_t * p_playlist = pl_Get(p_intf);
515     playlist_item_t *p_item = NULL;
516
517     o_selected_indexes = [o_outline_view selectedRowIndexes];
518     i_count = [o_selected_indexes count];
519
520     NSUInteger indexes[i_count];
521     [o_selected_indexes getIndexes:indexes maxCount:i_count inIndexRange:nil];
522     for (int i = 0; i < i_count; i++) {
523         PLItem *o_item = [o_outline_view itemAtRow:indexes[i]];
524         [o_outline_view deselectRow: indexes[i]];
525
526         if (![o_item isLeaf]) {
527             msg_Dbg(p_intf, "preparsing nodes not implemented");
528             continue;
529         }
530
531         libvlc_MetaRequest(p_intf->p_libvlc, [o_item input], META_REQUEST_OPTION_NONE);
532
533     }
534     [self playlistUpdated];
535 }
536
537 - (IBAction)downloadCoverArt:(id)sender
538 {
539     int i_count;
540     NSIndexSet *o_selected_indexes;
541     intf_thread_t * p_intf = VLCIntf;
542     playlist_t * p_playlist = pl_Get(p_intf);
543     playlist_item_t *p_item = NULL;
544
545     o_selected_indexes = [o_outline_view selectedRowIndexes];
546     i_count = [o_selected_indexes count];
547
548     NSUInteger indexes[i_count];
549     [o_selected_indexes getIndexes:indexes maxCount:i_count inIndexRange:nil];
550     for (int i = 0; i < i_count; i++) {
551         PLItem *o_item = [o_outline_view itemAtRow: indexes[i]];
552
553         if (![o_item isLeaf])
554             continue;
555
556         libvlc_ArtRequest(p_intf->p_libvlc, [o_item input], META_REQUEST_OPTION_NONE);
557     }
558     [self playlistUpdated];
559 }
560
561 - (IBAction)selectAll:(id)sender
562 {
563     [o_outline_view selectAll: nil];
564 }
565
566 - (IBAction)showInfoPanel:(id)sender
567 {
568     [[[VLCMain sharedInstance] info] initPanel];
569 }
570
571 - (IBAction)deleteItem:(id)sender
572 {
573     int i_count;
574     NSIndexSet *o_selected_indexes;
575     intf_thread_t * p_intf = VLCIntf;
576     playlist_t * p_playlist = pl_Get(p_intf);
577
578     // check if deletion is allowed
579     if (![[self model] editAllowed])
580         return;
581
582     o_selected_indexes = [o_outline_view selectedRowIndexes];
583     i_count = [o_selected_indexes count];
584     retainedRowSelection = [o_selected_indexes firstIndex];
585     if (retainedRowSelection == NSNotFound)
586         retainedRowSelection = 0;
587
588
589     NSUInteger indexes[i_count];
590 //    if (i_count == [o_outline_view numberOfRows]) {
591 //        PL_LOCK;
592 //        playlist_NodeDelete(p_playlist, [self currentPlaylistRoot], true, false);
593 //        PL_UNLOCK;
594 //        [self playlistUpdated];
595 //        return;
596 //    }
597     [o_selected_indexes getIndexes:indexes maxCount:i_count inIndexRange:nil];
598     for (int i = 0; i < i_count; i++) {
599         PLItem *o_item = [o_outline_view itemAtRow: indexes[i]];
600         [o_outline_view deselectRow: indexes[i]];
601
602         /// TODO
603 //        if (p_item->i_children != -1) {
604 //        //is a node and not an item
605 //            if (playlist_Status(p_playlist) != PLAYLIST_STOPPED &&
606 //                [self isItem: playlist_CurrentPlayingItem(p_playlist) inNode: ((playlist_item_t *)[o_item pointerValue])
607 //                        checkItemExistence: NO locked:YES] == YES)
608 //                // if current item is in selected node and is playing then stop playlist
609 //                playlist_Control(p_playlist, PLAYLIST_STOP, pl_Locked);
610 //
611 //                playlist_NodeDelete(p_playlist, p_item, true, false);
612 //        } else
613
614             playlist_DeleteFromInput(p_playlist, [o_item input], pl_Unlocked);
615 //        [[o_item parent] deleteChild:o_item];
616 //
617 //        [o_outline_view reloadData];
618     }
619
620 //    [self playlistUpdated];
621 }
622
623 - (IBAction)sortNodeByName:(id)sender
624 {
625     [self sortNode: SORT_TITLE];
626 }
627
628 - (IBAction)sortNodeByAuthor:(id)sender
629 {
630     [self sortNode: SORT_ARTIST];
631 }
632
633 - (void)sortNode:(int)i_mode
634 {
635     playlist_t * p_playlist = pl_Get(VLCIntf);
636     playlist_item_t * p_item;
637
638     // TODO why do we need this kind of sort? It looks crap and confusing...
639
640 //    if ([o_outline_view selectedRow] > -1) {
641 //        p_item = [[o_outline_view itemAtRow: [o_outline_view selectedRow]] pointerValue];
642 //        if (!p_item)
643 //            return;
644 //    } else
645 //        p_item = [self currentPlaylistRoot]; // If no item is selected, sort the whole playlist
646 //
647 //    PL_LOCK;
648 //    if (p_item->i_children > -1) // the item is a node
649 //        playlist_RecursiveNodeSort(p_playlist, p_item, i_mode, ORDER_NORMAL);
650 //    else
651 //        playlist_RecursiveNodeSort(p_playlist, p_item->p_parent, i_mode, ORDER_NORMAL);
652 //
653 //    PL_UNLOCK;
654 //    [self playlistUpdated];
655 }
656
657 - (input_item_t *)createItem:(NSDictionary *)o_one_item
658 {
659     intf_thread_t * p_intf = VLCIntf;
660     playlist_t * p_playlist = pl_Get(p_intf);
661
662     input_item_t *p_input;
663     BOOL b_rem = FALSE, b_dir = FALSE, b_writable = FALSE;
664     NSString *o_uri, *o_name, *o_path;
665     NSURL * o_nsurl;
666     NSArray *o_options;
667     NSURL *o_true_file;
668
669     /* Get the item */
670     o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
671     o_nsurl = [NSURL URLWithString: o_uri];
672     o_path = [o_nsurl path];
673     o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
674     o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
675
676     if ([[NSFileManager defaultManager] fileExistsAtPath:o_path isDirectory:&b_dir] && b_dir &&
677         [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath:o_path isRemovable: &b_rem
678                                                      isWritable:&b_writable isUnmountable:NULL description:NULL type:NULL] && b_rem && !b_writable && [o_nsurl isFileURL]) {
679
680         NSString *diskType = [VLCOpen getVolumeTypeFromMountPath: o_path];
681         msg_Dbg(p_intf, "detected optical media of type %s in the file input", [diskType UTF8String]);
682
683         if ([diskType isEqualToString: kVLCMediaDVD])
684             o_uri = [NSString stringWithFormat: @"dvdnav://%@", [VLCOpen getBSDNodeFromMountPath: o_path]];
685         else if ([diskType isEqualToString: kVLCMediaVideoTSFolder])
686             o_uri = [NSString stringWithFormat: @"dvdnav://%@", o_path];
687         else if ([diskType isEqualToString: kVLCMediaAudioCD])
688             o_uri = [NSString stringWithFormat: @"cdda://%@", [VLCOpen getBSDNodeFromMountPath: o_path]];
689         else if ([diskType isEqualToString: kVLCMediaVCD])
690             o_uri = [NSString stringWithFormat: @"vcd://%@#0:0", [VLCOpen getBSDNodeFromMountPath: o_path]];
691         else if ([diskType isEqualToString: kVLCMediaSVCD])
692             o_uri = [NSString stringWithFormat: @"vcd://%@@0:0", [VLCOpen getBSDNodeFromMountPath: o_path]];
693         else if ([diskType isEqualToString: kVLCMediaBD] || [diskType isEqualToString: kVLCMediaBDMVFolder])
694             o_uri = [NSString stringWithFormat: @"bluray://%@", o_path];
695         else
696             msg_Warn(VLCIntf, "unknown disk type, treating %s as regular input", [o_path UTF8String]);
697
698         p_input = input_item_New([o_uri UTF8String], [[[NSFileManager defaultManager] displayNameAtPath: o_path] UTF8String]);
699     }
700     else
701         p_input = input_item_New([o_uri fileSystemRepresentation], o_name ? [o_name UTF8String] : NULL);
702
703     if (!p_input)
704         return NULL;
705
706     if (o_options) {
707         NSUInteger count = [o_options count];
708         for (NSUInteger i = 0; i < count; i++)
709             input_item_AddOption(p_input, [[o_options objectAtIndex:i] UTF8String], VLC_INPUT_OPTION_TRUSTED);
710     }
711
712     /* Recent documents menu */
713     if (o_nsurl != nil && (BOOL)config_GetInt(p_playlist, "macosx-recentitems") == YES)
714         [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL: o_nsurl];
715
716     return p_input;
717 }
718
719 - (void)addPlaylistItems:(NSArray*)o_array
720 {
721
722     int i_plItemId = -1;
723
724     // add items directly to media library if this is the current root
725     if ([[self model] currentRootType] == ROOT_TYPE_MEDIALIBRARY)
726         i_plItemId = [[[self model] rootItem] plItemId];
727
728     BOOL b_autoplay = var_InheritBool(VLCIntf, "macosx-autoplay");
729
730     [self addPlaylistItems:o_array withParentItemId:i_plItemId atPos:-1 startPlayback:b_autoplay];
731 }
732
733 - (void)addPlaylistItems:(NSArray*)o_array withParentItemId:(int)i_plItemId atPos:(int)i_position startPlayback:(BOOL)b_start
734 {
735     playlist_t * p_playlist = pl_Get(VLCIntf);
736     PL_LOCK;
737
738     playlist_item_t *p_parent = NULL;
739     if (i_plItemId >= 0)
740         p_parent = playlist_ItemGetById(p_playlist, i_plItemId);
741     else
742         p_parent = p_playlist->p_playing;
743
744     if (!p_parent) {
745         PL_UNLOCK;
746         return;
747     }
748
749     NSUInteger count = [o_array count];
750     int i_current_offset = 0;
751     for (NSUInteger i = 0; i < count; ++i) {
752
753         NSDictionary *o_current_item = [o_array objectAtIndex:i];
754         input_item_t *p_input = [self createItem: o_current_item];
755         if (!p_input)
756             continue;
757
758         int i_pos = (i_position == -1) ? PLAYLIST_END : i_position + i_current_offset++;
759         playlist_item_t *p_item = playlist_NodeAddInput(p_playlist, p_input, p_parent,
760                                                         PLAYLIST_INSERT, i_pos, pl_Locked);
761         if (!p_item)
762             continue;
763
764         if (i == 0 && b_start) {
765             playlist_Control(p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, p_parent, p_item);
766         }
767         input_item_Release(p_input);
768     }
769     PL_UNLOCK;
770 }
771
772
773 - (IBAction)searchItem:(id)sender
774 {
775     [[self model] searchUpdate:[o_search_field stringValue]];
776 }
777
778 - (IBAction)recursiveExpandNode:(id)sender
779 {
780     NSIndexSet * selectedRows = [o_outline_view selectedRowIndexes];
781     NSUInteger count = [selectedRows count];
782     NSUInteger indexes[count];
783     [selectedRows getIndexes:indexes maxCount:count inIndexRange:nil];
784
785     id o_item;
786     playlist_item_t *p_item;
787     for (NSUInteger i = 0; i < count; i++) {
788         o_item = [o_outline_view itemAtRow: indexes[i]];
789
790         /* We need to collapse the node first, since OSX refuses to recursively
791          expand an already expanded node, even if children nodes are collapsed. */
792         if ([o_outline_view isExpandable:o_item]) {
793             [o_outline_view collapseItem: o_item collapseChildren: YES];
794             [o_outline_view expandItem: o_item expandChildren: YES];
795         }
796
797         selectedRows = [o_outline_view selectedRowIndexes];
798         [selectedRows getIndexes:indexes maxCount:count inIndexRange:nil];
799     }
800 }
801
802 - (NSMenu *)menuForEvent:(NSEvent *)o_event
803 {
804     if (!b_playlistmenu_nib_loaded)
805         b_playlistmenu_nib_loaded = [NSBundle loadNibNamed:@"PlaylistMenu" owner:self];
806
807     NSPoint pt;
808     bool b_rows;
809     bool b_item_sel;
810
811     pt = [o_outline_view convertPoint: [o_event locationInWindow] fromView: nil];
812     int row = [o_outline_view rowAtPoint:pt];
813     if (row != -1 && ![[o_outline_view selectedRowIndexes] containsIndex: row])
814         [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
815
816     b_item_sel = (row != -1 && [o_outline_view selectedRow] != -1);
817     b_rows = [o_outline_view numberOfRows] != 0;
818
819     playlist_t *p_playlist = pl_Get(VLCIntf);
820     bool b_del_allowed = [[self model] editAllowed];
821
822     [o_mi_play setEnabled: b_item_sel];
823     [o_mi_delete setEnabled: b_item_sel && b_del_allowed];
824     [o_mi_selectall setEnabled: b_rows];
825     [o_mi_info setEnabled: b_item_sel];
826     [o_mi_preparse setEnabled: b_item_sel];
827     [o_mi_recursive_expand setEnabled: b_item_sel];
828     [o_mi_sort_name setEnabled: b_item_sel];
829     [o_mi_sort_author setEnabled: b_item_sel];
830     [o_mi_dl_cover_art setEnabled: b_item_sel];
831
832     return o_ctx_menu;
833 }
834
835 - (void)outlineView: (NSOutlineView *)o_tv didClickTableColumn:(NSTableColumn *)o_tc
836 {
837     int i_mode, i_type = 0;
838     intf_thread_t *p_intf = VLCIntf;
839     NSString * o_identifier = [o_tc identifier];
840
841     playlist_t *p_playlist = pl_Get(p_intf);
842
843     if (o_tc_sortColumn == o_tc)
844         b_isSortDescending = !b_isSortDescending;
845     else
846         b_isSortDescending = false;
847
848     if (b_isSortDescending)
849         i_type = ORDER_REVERSE;
850     else
851         i_type = ORDER_NORMAL;
852
853     [[self model] sortForColumn:o_identifier withMode:i_type];
854
855     // TODO rework, why do we need a full call here?
856 //    [self playlistUpdated];
857
858     /* Clear indications of any existing column sorting */
859     NSUInteger count = [[o_outline_view tableColumns] count];
860     for (NSUInteger i = 0 ; i < count ; i++)
861         [o_outline_view setIndicatorImage:nil inTableColumn: [[o_outline_view tableColumns] objectAtIndex:i]];
862
863     [o_outline_view setHighlightedTableColumn:nil];
864     o_tc_sortColumn = nil;
865
866
867     o_tc_sortColumn = o_tc;
868     [o_outline_view setHighlightedTableColumn:o_tc];
869
870     if (b_isSortDescending)
871         [o_outline_view setIndicatorImage:o_descendingSortingImage inTableColumn:o_tc];
872     else
873         [o_outline_view setIndicatorImage:o_ascendingSortingImage inTableColumn:o_tc];
874 }
875
876
877 - (void)outlineView:(NSOutlineView *)outlineView
878     willDisplayCell:(id)cell
879      forTableColumn:(NSTableColumn *)tableColumn
880                item:(id)item
881 {
882     /* this method can be called when VLC is already dead, hence the extra checks */
883     intf_thread_t * p_intf = VLCIntf;
884     if (!p_intf)
885         return;
886     playlist_t *p_playlist = pl_Get(p_intf);
887
888     NSFont *fontToUse;
889     if (config_GetInt(VLCIntf, "macosx-large-text"))
890         fontToUse = [NSFont systemFontOfSize:13.];
891     else
892         fontToUse = [NSFont systemFontOfSize:11.];
893
894     BOOL b_is_playing = NO;
895     PL_LOCK;
896     playlist_item_t *p_current_item = playlist_CurrentPlayingItem(p_playlist);
897     if (p_current_item) {
898         b_is_playing = p_current_item->i_id == [item plItemId];
899     }
900     PL_UNLOCK;
901
902     /*
903      TODO: repaint all items bold:
904      [self isItem: [o_playing_item pointerValue] inNode: [item pointerValue] checkItemExistence:YES locked:NO]
905      || [o_playing_item isEqual: item]
906      */
907
908     if (b_is_playing)
909         [cell setFont: [[NSFontManager sharedFontManager] convertFont:fontToUse toHaveTrait:NSBoldFontMask]];
910     else
911         [cell setFont: [[NSFontManager sharedFontManager] convertFont:fontToUse toNotHaveTrait:NSBoldFontMask]];
912 }
913
914 // TODO remove method
915 - (NSArray *)draggedItems
916 {
917     return [[self model] draggedItems];
918 }
919
920 - (void)setColumn: (NSString *)o_column state: (NSInteger)i_state translationDict:(NSDictionary *)o_dict
921 {
922     NSTableColumn * o_work_tc;
923
924     if (i_state == NSOnState) {
925         NSString *o_title = [o_dict objectForKey:o_column];
926         if (!o_title)
927             return;
928
929         o_work_tc = [[NSTableColumn alloc] initWithIdentifier: o_column];
930         [o_work_tc setEditable: NO];
931         [[o_work_tc dataCell] setFont: [NSFont controlContentFontOfSize:11.]];
932
933         [[o_work_tc headerCell] setStringValue: [o_dict objectForKey:o_column]];
934
935         if ([o_column isEqualToString: TRACKNUM_COLUMN]) {
936             [o_work_tc setWidth: 20.];
937             [o_work_tc setResizingMask: NSTableColumnNoResizing];
938             [[o_work_tc headerCell] setStringValue: @"#"];
939         }
940
941         [o_outline_view addTableColumn: o_work_tc];
942         [o_work_tc release];
943         [o_outline_view reloadData];
944         [o_outline_view setNeedsDisplay: YES];
945     }
946     else
947         [o_outline_view removeTableColumn: [o_outline_view tableColumnWithIdentifier: o_column]];
948
949     [o_outline_view setOutlineTableColumn: [o_outline_view tableColumnWithIdentifier:TITLE_COLUMN]];
950 }
951
952 - (void)saveTableColumns
953 {
954     NSMutableArray * o_arrayToSave = [[NSMutableArray alloc] init];
955     NSArray * o_columns = [[NSArray alloc] initWithArray:[o_outline_view tableColumns]];
956     NSUInteger count = [o_columns count];
957     NSTableColumn * o_currentColumn;
958     for (NSUInteger i = 0; i < count; i++) {
959         o_currentColumn = [o_columns objectAtIndex:i];
960         [o_arrayToSave addObject:[NSArray arrayWithObjects:[o_currentColumn identifier], [NSNumber numberWithFloat:[o_currentColumn width]], nil]];
961     }
962     [[NSUserDefaults standardUserDefaults] setObject: o_arrayToSave forKey:@"PlaylistColumnSelection"];
963     [[NSUserDefaults standardUserDefaults] synchronize];
964     [o_columns release];
965     [o_arrayToSave release];
966 }
967
968 - (BOOL)isValidResumeItem:(input_item_t *)p_item
969 {
970     char *psz_url = input_item_GetURI(p_item);
971     NSString *o_url_string = toNSStr(psz_url);
972     free(psz_url);
973
974     if ([o_url_string isEqualToString:@""])
975         return NO;
976
977     NSURL *o_url = [NSURL URLWithString:o_url_string];
978
979     if (![o_url isFileURL])
980         return NO;
981
982     BOOL isDir = false;
983     if (![[NSFileManager defaultManager] fileExistsAtPath:[o_url path] isDirectory:&isDir])
984         return NO;
985
986     if (isDir)
987         return NO;
988
989     return YES;
990 }
991
992 - (void)updateAlertWindow:(NSTimer *)timer
993 {
994     NSAlert *alert = [timer userInfo];
995
996     --currentResumeTimeout;
997     if (currentResumeTimeout <= 0) {
998         [[alert window] close];
999         [NSApp abortModal];
1000     }
1001
1002     NSString *buttonLabel = _NS("Restart playback");
1003     buttonLabel = [buttonLabel stringByAppendingFormat:@" (%d)", currentResumeTimeout];
1004
1005     [[[alert buttons] objectAtIndex:2] setTitle:buttonLabel];
1006 }
1007
1008 - (void)continuePlaybackWhereYouLeftOff:(input_thread_t *)p_input_thread
1009 {
1010     NSDictionary *recentlyPlayedFiles = [[NSUserDefaults standardUserDefaults] objectForKey:@"recentlyPlayedMedia"];
1011     if (!recentlyPlayedFiles)
1012         return;
1013
1014     input_item_t *p_item = input_GetItem(p_input_thread);
1015     if (!p_item)
1016         return;
1017
1018     /* allow the user to over-write the start/stop/run-time */
1019     if (var_GetFloat(p_input_thread, "run-time") > 0 ||
1020         var_GetFloat(p_input_thread, "start-time") > 0 ||
1021         var_GetFloat(p_input_thread, "stop-time") > 0) {
1022         return;
1023     }
1024
1025     /* check for file existance before resuming */
1026     if (![self isValidResumeItem:p_item])
1027         return;
1028
1029     char *psz_url = decode_URI(input_item_GetURI(p_item));
1030     if (!psz_url)
1031         return;
1032     NSString *url = toNSStr(psz_url);
1033     free(psz_url);
1034
1035     NSNumber *lastPosition = [recentlyPlayedFiles objectForKey:url];
1036     if (!lastPosition || lastPosition.intValue <= 0)
1037         return;
1038
1039     int settingValue = config_GetInt(VLCIntf, "macosx-continue-playback");
1040     if (settingValue == 2) // never resume
1041         return;
1042
1043     NSInteger returnValue = NSAlertErrorReturn;
1044     if (settingValue == 0) { // ask
1045
1046         char *psz_title_name = input_item_GetTitleFbName(p_item);
1047         NSString *o_title = toNSStr(psz_title_name);
1048         free(psz_title_name);
1049
1050         currentResumeTimeout = 6;
1051         NSString *o_restartButtonLabel = _NS("Restart playback");
1052         o_restartButtonLabel = [o_restartButtonLabel stringByAppendingFormat:@" (%d)", currentResumeTimeout];
1053         NSAlert *theAlert = [NSAlert alertWithMessageText:_NS("Continue playback?") defaultButton:_NS("Continue") alternateButton:o_restartButtonLabel otherButton:_NS("Always continue") informativeTextWithFormat:_NS("Playback of \"%@\" will continue at %@"), o_title, [[VLCStringUtility sharedInstance] stringForTime:lastPosition.intValue]];
1054
1055         NSTimer *timer = [NSTimer timerWithTimeInterval:1
1056                                                  target:self
1057                                                selector:@selector(updateAlertWindow:)
1058                                                userInfo:theAlert
1059                                                 repeats:YES];
1060
1061         [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSModalPanelRunLoopMode];
1062
1063         returnValue = [theAlert runModal];
1064         [timer invalidate];
1065
1066         // restart button was pressed or timeout happened
1067         if (returnValue == NSAlertAlternateReturn ||
1068             returnValue == NSRunAbortedResponse)
1069             return;
1070     }
1071
1072     mtime_t lastPos = (mtime_t)lastPosition.intValue * 1000000;
1073     msg_Dbg(VLCIntf, "continuing playback at %lld", lastPos);
1074     var_SetTime(p_input_thread, "time", lastPos);
1075
1076     if (returnValue == NSAlertOtherReturn)
1077         config_PutInt(VLCIntf, "macosx-continue-playback", 1);
1078 }
1079
1080 - (void)storePlaybackPositionForItem:(input_thread_t *)p_input_thread
1081 {
1082     if (!var_InheritBool(VLCIntf, "macosx-recentitems"))
1083         return;
1084
1085     input_item_t *p_item = input_GetItem(p_input_thread);
1086     if (!p_item)
1087         return;
1088
1089     if (![self isValidResumeItem:p_item])
1090         return;
1091
1092     char *psz_url = decode_URI(input_item_GetURI(p_item));
1093     if (!psz_url)
1094         return;
1095     NSString *url = toNSStr(psz_url);
1096     free(psz_url);
1097
1098     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
1099     NSMutableDictionary *mutDict = [[NSMutableDictionary alloc] initWithDictionary:[defaults objectForKey:@"recentlyPlayedMedia"]];
1100
1101     float relativePos = var_GetFloat(p_input_thread, "position");
1102     mtime_t pos = var_GetTime(p_input_thread, "time") / 1000000;
1103     mtime_t dur = input_item_GetDuration(p_item) / 1000000;
1104
1105     NSMutableArray *mediaList = [[defaults objectForKey:@"recentlyPlayedMediaList"] mutableCopy];
1106
1107     if (relativePos > .05 && relativePos < .95 && dur > 180) {
1108         [mutDict setObject:[NSNumber numberWithInt:pos] forKey:url];
1109
1110         [mediaList removeObject:url];
1111         [mediaList addObject:url];
1112         NSUInteger mediaListCount = mediaList.count;
1113         if (mediaListCount > 30) {
1114             for (NSUInteger x = 0; x < mediaListCount - 30; x++) {
1115                 [mutDict removeObjectForKey:[mediaList objectAtIndex:0]];
1116                 [mediaList removeObjectAtIndex:0];
1117             }
1118         }
1119     } else {
1120         [mutDict removeObjectForKey:url];
1121         [mediaList removeObject:url];
1122     }
1123     [defaults setObject:mutDict forKey:@"recentlyPlayedMedia"];
1124     [defaults setObject:mediaList forKey:@"recentlyPlayedMediaList"];
1125     [defaults synchronize];
1126
1127     [mutDict release];
1128     [mediaList release];
1129 }
1130
1131 @end