]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlist.m
* Still some work for playlist drag'n'drop
[vlc] / modules / gui / macosx / playlist.m
1 /*****************************************************************************
2  * playlist.m: MacOS X interface module
3  *****************************************************************************
4 * Copyright (C) 2002-2005 VideoLAN
5  * $Id$
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *          Derk-Jan Hartman <hartman at videolan dot org>
9  *          Benjamin Pracht <bigben at videolab dot org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /* TODO
27  * add 'icons' for different types of nodes? (http://www.cocoadev.com/index.pl?IconAndTextInTableCell)
28  * create a new search field build with pictures from the 'regular' search field, so it can be emulated on 10.2
29  * create toggle buttons for the shuffle, repeat one, repeat all functions.
30  * implement drag and drop and item reordering.
31  * reimplement enable/disable item
32  * create a new 'tool' button (see the gear button in the Finder window) for 'actions'
33    (adding service discovery, other views, new node/playlist, save node/playlist) stuff like that
34  */
35
36
37 /*****************************************************************************
38  * Preamble
39  *****************************************************************************/
40 #include <stdlib.h>                                      /* malloc(), free() */
41 #include <sys/param.h>                                    /* for MAXPATHLEN */
42 #include <string.h>
43 #include <math.h>
44 #include <sys/mount.h>
45 #include <vlc_keys.h>
46
47 #include "intf.h"
48 #include "playlist.h"
49 #include "controls.h"
50 #include "osd.h"
51 #include "misc.h"
52
53 /*****************************************************************************
54  * VLCPlaylistView implementation 
55  *****************************************************************************/
56 @implementation VLCPlaylistView
57
58 - (NSMenu *)menuForEvent:(NSEvent *)o_event
59 {
60     return( [[self delegate] menuForEvent: o_event] );
61 }
62
63 - (void)keyDown:(NSEvent *)o_event
64 {
65     unichar key = 0;
66
67     if( [[o_event characters] length] )
68     {
69         key = [[o_event characters] characterAtIndex: 0];
70     }
71
72     switch( key )
73     {
74         case NSDeleteCharacter:
75         case NSDeleteFunctionKey:
76         case NSDeleteCharFunctionKey:
77         case NSBackspaceCharacter:
78             [[self delegate] deleteItem:self];
79             break;
80
81         case NSEnterCharacter:
82         case NSCarriageReturnCharacter:
83             [(VLCPlaylist *)[[VLCMain sharedInstance] getPlaylist]
84                                                             playItem:self];
85             break;
86
87         default:
88             [super keyDown: o_event];
89             break;
90     }
91 }
92
93 @end
94
95 /*****************************************************************************
96  * VLCPlaylist implementation 
97  *****************************************************************************/
98 @implementation VLCPlaylist
99
100 - (id)init
101 {
102     self = [super init];
103     if ( self != nil )
104     {
105         o_outline_dict = [[NSMutableDictionary alloc] init];
106         //i_moveRow = -1;
107     }
108     return self;
109 }
110
111 - (void)awakeFromNib
112 {
113     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
114                                           FIND_ANYWHERE );
115     vlc_list_t *p_list = vlc_list_find( p_playlist, VLC_OBJECT_MODULE,
116                                         FIND_ANYWHERE );
117
118     int i_index;
119     i_current_view = VIEW_CATEGORY;
120     playlist_ViewUpdate( p_playlist, i_current_view );
121
122     [o_outline_view setTarget: self];
123     [o_outline_view setDelegate: self];
124     [o_outline_view setDataSource: self];
125
126     [o_outline_view setDoubleAction: @selector(playItem:)];
127
128     [o_outline_view registerForDraggedTypes:
129         [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
130     [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
131
132 /* We need to check whether _defaultTableHeaderSortImage exists, since it 
133 belongs to an Apple hidden private API, and then can "disapear" at any time*/
134
135     if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderSortImage)] )
136     {
137         o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
138     }
139     else
140     {
141         o_ascendingSortingImage = nil;
142     }
143
144     if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderReverseSortImage)] )
145     {
146         o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
147     }
148     else
149     {
150         o_descendingSortingImage = nil;
151     }
152
153     o_tc_sortColumn = nil;
154
155     for( i_index = 0; i_index < p_list->i_count; i_index++ )
156     {
157         NSMenuItem * o_lmi;
158         module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
159
160         if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
161         {
162             /* create the menu entries used in the playlist menu */
163             o_lmi = [[o_mi_services submenu] addItemWithTitle:
164                      [NSString stringWithUTF8String:
165                      p_parser->psz_longname ? p_parser->psz_longname :
166                      ( p_parser->psz_shortname ? p_parser->psz_shortname:
167                      p_parser->psz_object_name)]
168                                              action: @selector(servicesChange:)
169                                              keyEquivalent: @""];
170             [o_lmi setTarget: self];
171             [o_lmi setRepresentedObject:
172                    [NSString stringWithCString: p_parser->psz_object_name]];
173             if( playlist_IsServicesDiscoveryLoaded( p_playlist,
174                     p_parser->psz_object_name ) )
175                 [o_lmi setState: NSOnState];
176                 
177             /* create the menu entries for the main menu */
178             o_lmi = [[o_mm_mi_services submenu] addItemWithTitle:
179                      [NSString stringWithUTF8String:
180                      p_parser->psz_longname ? p_parser->psz_longname :
181                      ( p_parser->psz_shortname ? p_parser->psz_shortname:
182                      p_parser->psz_object_name)]
183                                              action: @selector(servicesChange:)
184                                              keyEquivalent: @""];
185             [o_lmi setTarget: self];
186             [o_lmi setRepresentedObject:
187                    [NSString stringWithCString: p_parser->psz_object_name]];
188             if( playlist_IsServicesDiscoveryLoaded( p_playlist,
189                     p_parser->psz_object_name ) )
190                 [o_lmi setState: NSOnState];
191         }
192     }
193     vlc_list_release( p_list );
194     vlc_object_release( p_playlist );
195
196     /* Change the simple textfield into a searchField if we can... */
197 #if 0
198     if( MACOS_VERSION >= 10.3 )
199     {
200         NSView *o_parentview = [o_status_field superview];
201         NSSearchField *o_better_search_field = [[NSSearchField alloc]initWithFrame:[o_search_field frame]];
202         [o_better_search_field setRecentsAutosaveName:@"VLC media player search"];
203         [o_better_search_field setDelegate:self];
204         [[NSNotificationCenter defaultCenter] addObserver: self
205             selector: @selector(searchfieldChanged:)
206             name: NSControlTextDidChangeNotification
207             object: o_better_search_field];
208
209         [o_better_search_field setTarget:self];
210         [o_better_search_field setAction:@selector(searchItem:)];
211
212         [o_better_search_field setAutoresizingMask:NSViewMinXMargin];
213         [o_parentview addSubview:o_better_search_field];
214         [o_search_field setHidden:YES];
215     }
216 #endif
217     [self initStrings];
218     //[self playlistUpdated];
219 }
220
221 - (void)searchfieldChanged:(NSNotification *)o_notification
222 {
223     [o_search_field setStringValue:[[o_notification object] stringValue]];
224 }
225
226 - (void)initStrings
227 {
228     [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
229     [o_mi_play setTitle: _NS("Play")];
230     [o_mi_delete setTitle: _NS("Delete")];
231     [o_mi_recursive_expand setTitle: _NS("Expand Node")];
232     [o_mi_selectall setTitle: _NS("Select All")];
233     [o_mi_info setTitle: _NS("Properties")];
234     [o_mi_sort_name setTitle: _NS("Sort Node by Name")];
235     [o_mi_sort_author setTitle: _NS("Sort Node by Author")];
236     [o_mi_services setTitle: _NS("Services discovery")];
237     [[o_tc_name headerCell] setStringValue:_NS("Name")];
238     [[o_tc_author headerCell] setStringValue:_NS("Author")];
239     [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
240     [o_status_field setStringValue: [NSString stringWithFormat:
241                         _NS("no items in playlist")]];
242
243     [o_random_ckb setTitle: _NS("Random")];
244 #if 0
245     [o_search_button setTitle: _NS("Search")];
246 #endif
247     [o_search_field setToolTip: _NS("Search in Playlist")];
248     [[o_loop_popup itemAtIndex:0] setTitle: _NS("Standard Play")];
249     [[o_loop_popup itemAtIndex:1] setTitle: _NS("Repeat One")];
250     [[o_loop_popup itemAtIndex:2] setTitle: _NS("Repeat All")];
251 }
252
253 - (NSOutlineView *)outlineView
254 {
255     return o_outline_view;
256 }
257
258 - (void)playlistUpdated
259 {
260     unsigned int i;
261
262     /* Clear indications of any existing column sorting*/
263     for( i = 0 ; i < [[o_outline_view tableColumns] count] ; i++ )
264     {
265         [o_outline_view setIndicatorImage:nil inTableColumn:
266                             [[o_outline_view tableColumns] objectAtIndex:i]];
267     }
268
269     [o_outline_view setHighlightedTableColumn:nil];
270     o_tc_sortColumn = nil;
271     // TODO Find a way to keep the dict size to a minimum
272     //[o_outline_dict removeAllObjects];
273     [o_outline_view reloadData];
274 }
275
276 - (void)playModeUpdated
277 {
278     playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
279                                           FIND_ANYWHERE );
280     vlc_value_t val, val2;
281
282     if( p_playlist == NULL )
283     {
284         return;
285     }
286
287     var_Get( p_playlist, "loop", &val2 );
288     var_Get( p_playlist, "repeat", &val );
289     if( val.b_bool == VLC_TRUE )
290     {
291         [o_loop_popup selectItemAtIndex: 1];
292    }
293     else if( val2.b_bool == VLC_TRUE )
294     {
295         [o_loop_popup selectItemAtIndex: 2];
296     }
297     else
298     {
299         [o_loop_popup selectItemAtIndex: 0];
300     }
301
302     var_Get( p_playlist, "random", &val );
303     [o_random_ckb setState: val.b_bool];
304
305     vlc_object_release( p_playlist );
306 }
307
308 - (void)updateRowSelection
309 {
310     int i,i_row;
311     unsigned int j;
312
313     playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
314                                           FIND_ANYWHERE );
315     playlist_item_t *p_item, *p_temp_item;
316     NSMutableArray *o_array = [NSMutableArray array];
317
318     if( p_playlist == NULL )
319         return;
320
321     p_item = p_playlist->status.p_item;
322     if( p_item == NULL ) return;
323
324     p_temp_item = p_item;
325     while( p_temp_item->i_parents > 0 )
326     {
327         [o_array insertObject: [NSValue valueWithPointer: p_temp_item] atIndex: 0];
328         for (i = 0 ; i < p_temp_item->i_parents ; i++)
329         {
330             if( p_temp_item->pp_parents[i]->i_view == i_current_view )
331             {
332                 p_temp_item = p_temp_item->pp_parents[i]->p_parent;
333                 break;
334             }
335         }
336     }
337
338     for (j = 0 ; j < [o_array count] - 1 ; j++)
339     {
340         id o_item;
341         if( ( o_item = [o_outline_dict objectForKey:
342                             [NSString stringWithFormat: @"%p",
343                             [[o_array objectAtIndex:j] pointerValue]]] ) != nil )
344             [o_outline_view expandItem: o_item];
345
346     }
347
348     i_row = [o_outline_view rowForItem:[o_outline_dict
349             objectForKey:[NSString stringWithFormat: @"%p", p_item]]];
350
351     [o_outline_view selectRow: i_row byExtendingSelection: NO];
352     [o_outline_view scrollRowToVisible: i_row];
353
354     vlc_object_release(p_playlist);
355 }
356
357 - (BOOL)isItem: (playlist_item_t *)p_item inNode: (playlist_item_t *)p_node
358 {
359     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
360                                           FIND_ANYWHERE );
361     playlist_item_t *p_temp_item = p_item;
362
363     if( p_playlist == NULL )
364     {
365         return NO;
366     }
367
368     if ( p_temp_item )
369     {
370         int i;
371         vlc_mutex_lock( &p_playlist->object_lock );
372
373 // Let's check if we can do without that
374 #if 0
375
376         /* Since outlineView: willDisplayCell:... may call this function with
377            p_items that don't exist anymore, first check if the item is still
378            in the playlist. Any cleaner solution welcomed. */
379
380         {
381             if( p_playlist->pp_all_items[i] == p_item ) break;
382             else if ( i == p_playlist->i_all_size - 1 )
383             {
384                 vlc_object_release( p_playlist );
385                 vlc_mutex_unlock( &p_playlist->object_lock );
386                 return NO;
387             }
388         }
389 #endif
390         while( p_temp_item->i_parents > 0 )
391         {
392             for( i = 0; i < p_temp_item->i_parents ; i++ )
393             {
394                 if( p_temp_item->pp_parents[i]->i_view == i_current_view )
395                 {
396                     if( p_temp_item->pp_parents[i]->p_parent == p_node )
397                     {
398                         vlc_mutex_unlock( &p_playlist->object_lock );
399                         vlc_object_release( p_playlist );
400                         return YES;
401                     }
402                     else
403                     {
404                         p_temp_item = p_temp_item->pp_parents[i]->p_parent;
405                         break;
406                     }
407                 }
408             }
409             vlc_mutex_unlock( &p_playlist->object_lock );
410         }
411     }
412
413     vlc_object_release( p_playlist );
414     return NO;
415 }
416
417 - (BOOL)isValueItem: (id)o_item inNode: (id)o_node
418 {
419     int i;
420     int i_total = [[o_outline_view dataSource] outlineView:o_outline_view
421                                         numberOfChildrenOfItem: o_node];
422     for( i = 0 ; i < i_total ; i++ )
423     {
424         id o_temp_item = [[o_outline_view dataSource] outlineView:
425                                     o_outline_view child:i ofItem: o_node];
426         if( [[o_outline_view dataSource] outlineView:o_outline_view
427                                     numberOfChildrenOfItem: o_temp_item] > 0 )
428         {
429             if( [self isValueItem: o_item inNode: o_temp_item] == YES )
430             return YES;
431         }
432         else if( [o_temp_item isEqual: o_item] )
433         {
434             return YES;
435         }
436     }
437     return NO;
438 }
439
440 - (IBAction)savePlaylist:(id)sender
441 {
442     intf_thread_t * p_intf = VLCIntf;
443     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
444                                                        FIND_ANYWHERE );
445
446     NSSavePanel *o_save_panel = [NSSavePanel savePanel];
447     NSString * o_name = [NSString stringWithFormat: @"%@.m3u", _NS("Untitled")];
448     [o_save_panel setTitle: _NS("Save Playlist")];
449     [o_save_panel setPrompt: _NS("Save")];
450
451     if( [o_save_panel runModalForDirectory: nil
452             file: o_name] == NSOKButton )
453     {
454         playlist_Export( p_playlist, [[o_save_panel filename] fileSystemRepresentation], "export-m3u" );
455     }
456 }
457
458
459 /* When called retrieves the selected outlineview row and plays that node or item */
460 - (IBAction)playItem:(id)sender
461 {
462     intf_thread_t * p_intf = VLCIntf;
463     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
464                                                        FIND_ANYWHERE );
465
466     if( p_playlist != NULL )
467     {
468         playlist_item_t *p_item;
469         playlist_item_t *p_node = NULL;
470         int i;
471
472         p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
473
474         if( p_item )
475         {
476             if( p_item->i_children == -1 )
477             {
478                 for( i = 0 ; i < p_item->i_parents ; i++ )
479                 {
480                     if( p_item->pp_parents[i]->i_view == i_current_view )
481                     {
482                         p_node = p_item->pp_parents[i]->p_parent;
483                     }
484                 }
485             }
486             else
487             {
488                 p_node = p_item;
489                 if( p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1 )
490                 {
491                     p_item = p_node->pp_children[0];
492                 }
493                 else
494                 {
495                     p_item = NULL;
496                 }
497             }
498             playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view, p_node, p_item );
499         }
500         vlc_object_release( p_playlist );
501     }
502 }
503
504 - (IBAction)servicesChange:(id)sender
505 {
506     NSMenuItem *o_mi = (NSMenuItem *)sender;
507     NSString *o_string = [o_mi representedObject];
508     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
509                                           FIND_ANYWHERE );
510     if( !playlist_IsServicesDiscoveryLoaded( p_playlist, [o_string cString] ) )
511         playlist_ServicesDiscoveryAdd( p_playlist, [o_string cString] );
512     else
513         playlist_ServicesDiscoveryRemove( p_playlist, [o_string cString] );
514
515     [o_mi setState: playlist_IsServicesDiscoveryLoaded( p_playlist,
516                                           [o_string cString] ) ? YES : NO];
517
518     i_current_view = VIEW_CATEGORY;
519     playlist_ViewUpdate( p_playlist, i_current_view );
520     vlc_object_release( p_playlist );
521     [self playlistUpdated];
522     return;
523 }
524
525 - (IBAction)selectAll:(id)sender
526 {
527     [o_outline_view selectAll: nil];
528 }
529
530 - (IBAction)deleteItem:(id)sender
531 {
532     int i, i_count, i_row;
533     NSMutableArray *o_to_delete;
534     NSNumber *o_number;
535
536     playlist_t * p_playlist;
537     intf_thread_t * p_intf = VLCIntf;
538
539     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
540                                           FIND_ANYWHERE );
541
542     if ( p_playlist == NULL )
543     {
544         return;
545     }
546     o_to_delete = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
547     i_count = [o_to_delete count];
548
549     for( i = 0; i < i_count; i++ )
550     {
551         o_number = [o_to_delete lastObject];
552         i_row = [o_number intValue];
553         id o_item = [o_outline_view itemAtRow: i_row];
554         playlist_item_t *p_item = [o_item pointerValue];
555         [o_to_delete removeObject: o_number];
556         [o_outline_view deselectRow: i_row];
557
558         if( [[o_outline_view dataSource] outlineView:o_outline_view
559                                         numberOfChildrenOfItem: o_item]  > 0 )
560         //is a node and not an item
561         {
562             if( p_playlist->status.i_status != PLAYLIST_STOPPED &&
563                 [self isItem: p_playlist->status.p_item inNode:
564                         ((playlist_item_t *)[o_item pointerValue])] == YES )
565             {
566                 // if current item is in selected node and is playing then stop playlist
567                 playlist_Stop( p_playlist );
568             }
569             vlc_mutex_lock( &p_playlist->object_lock );
570             playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
571             vlc_mutex_unlock( &p_playlist->object_lock );
572         }
573         else
574         {
575             if( p_playlist->status.i_status != PLAYLIST_STOPPED &&
576                 p_playlist->status.p_item == [[o_outline_view itemAtRow: i_row] pointerValue] )
577             {
578                 playlist_Stop( p_playlist );
579             }
580             vlc_mutex_lock( &p_playlist->object_lock );
581             playlist_Delete( p_playlist, p_item->input.i_id );
582             vlc_mutex_unlock( &p_playlist->object_lock );
583         }
584     }
585     [self playlistUpdated];
586     vlc_object_release( p_playlist );
587 }
588
589 - (IBAction)sortNodeByName:(id)sender
590 {
591     [self sortNode: SORT_TITLE];
592 }
593
594 - (IBAction)sortNodeByAuthor:(id)sender
595 {
596     [self sortNode: SORT_AUTHOR];
597 }
598
599 - (void)sortNode:(int)i_mode
600 {
601     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
602                                           FIND_ANYWHERE );
603     playlist_item_t * p_item;
604
605     if (p_playlist == NULL)
606     {
607         return;
608     }
609
610     if( [o_outline_view selectedRow] > -1 )
611     {
612         p_item = [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
613                                                                 pointerValue];
614     }
615     else
616     /*If no item is selected, sort the whole playlist*/
617     {
618         playlist_view_t * p_view = playlist_ViewFind( p_playlist, i_current_view );
619         p_item = p_view->p_root;
620     }
621
622     if( p_item->i_children > -1 ) // the item is a node
623     {
624         vlc_mutex_lock( &p_playlist->object_lock );
625         playlist_RecursiveNodeSort( p_playlist, p_item, i_mode, ORDER_NORMAL );
626         vlc_mutex_unlock( &p_playlist->object_lock );
627     }
628     else
629     {
630         int i;
631
632         for( i = 0 ; i < p_item->i_parents ; i++ )
633         {
634             if( p_item->pp_parents[i]->i_view == i_current_view )
635             {
636                 vlc_mutex_lock( &p_playlist->object_lock );
637                 playlist_RecursiveNodeSort( p_playlist,
638                         p_item->pp_parents[i]->p_parent, i_mode, ORDER_NORMAL );
639                 vlc_mutex_unlock( &p_playlist->object_lock );
640                 break;
641             }
642         }
643     }
644     vlc_object_release( p_playlist );
645     [self playlistUpdated];
646 }
647
648 - (playlist_item_t *)createItem:(NSDictionary *)o_one_item
649 {
650     intf_thread_t * p_intf = VLCIntf;
651     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
652                                                        FIND_ANYWHERE );
653
654     if( p_playlist == NULL )
655     {
656         return NULL;
657     }
658     playlist_item_t *p_item;
659     int i;
660     BOOL b_rem = FALSE, b_dir = FALSE;
661     NSString *o_uri, *o_name;
662     NSArray *o_options;
663     NSURL *o_true_file;
664
665     /* Get the item */
666     o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
667     o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
668     o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
669
670     /* Find the name for a disc entry ( i know, can you believe the trouble?) */
671     if( ( !o_name || [o_name isEqualToString:@""] ) && [o_uri rangeOfString: @"/dev/"].location != NSNotFound )
672     {
673         int i_count, i_index;
674         struct statfs *mounts = NULL;
675
676         i_count = getmntinfo (&mounts, MNT_NOWAIT);
677         /* getmntinfo returns a pointer to static data. Do not free. */
678         for( i_index = 0 ; i_index < i_count; i_index++ )
679         {
680             NSMutableString *o_temp, *o_temp2;
681             o_temp = [NSMutableString stringWithString: o_uri];
682             o_temp2 = [NSMutableString stringWithCString: mounts[i_index].f_mntfromname];
683             [o_temp replaceOccurrencesOfString: @"/dev/rdisk" withString: @"/dev/disk" options:NULL range:NSMakeRange(0, [o_temp length]) ];
684             [o_temp2 replaceOccurrencesOfString: @"s0" withString: @"" options:NULL range:NSMakeRange(0, [o_temp2 length]) ];
685             [o_temp2 replaceOccurrencesOfString: @"s1" withString: @"" options:NULL range:NSMakeRange(0, [o_temp2 length]) ];
686
687             if( strstr( [o_temp fileSystemRepresentation], [o_temp2 fileSystemRepresentation] ) != NULL )
688             {
689                 o_name = [[NSFileManager defaultManager] displayNameAtPath: [NSString stringWithCString:mounts[i_index].f_mntonname]];
690             }
691         }
692     }
693     /* If no name, then make a guess */
694     if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
695
696     if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
697         [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
698                 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem   )
699     {
700         /* All of this is to make sure CD's play when you D&D them on VLC */
701         /* Converts mountpoint to a /dev file */
702         struct statfs *buf;
703         char *psz_dev;
704         NSMutableString *o_temp;
705
706         buf = (struct statfs *) malloc (sizeof(struct statfs));
707         statfs( [o_uri fileSystemRepresentation], buf );
708         psz_dev = strdup(buf->f_mntfromname);
709         o_temp = [NSMutableString stringWithCString: psz_dev ];
710         [o_temp replaceOccurrencesOfString: @"/dev/disk" withString: @"/dev/rdisk" options:NULL range:NSMakeRange(0, [o_temp length]) ];
711         [o_temp replaceOccurrencesOfString: @"s0" withString: @"" options:NULL range:NSMakeRange(0, [o_temp length]) ];
712         [o_temp replaceOccurrencesOfString: @"s1" withString: @"" options:NULL range:NSMakeRange(0, [o_temp length]) ];
713         o_uri = o_temp;
714     }
715
716     p_item = playlist_ItemNew( p_intf, [o_uri fileSystemRepresentation], [o_name UTF8String] );
717     if( !p_item )
718        return NULL;
719
720     if( o_options )
721     {
722         for( i = 0; i < (int)[o_options count]; i++ )
723         {
724             playlist_ItemAddOption( p_item, strdup( [[o_options objectAtIndex:i] UTF8String] ) );
725         }
726     }
727
728     /* Recent documents menu */
729     o_true_file = [NSURL fileURLWithPath: o_uri];
730     if( o_true_file != nil )
731     {
732         [[NSDocumentController sharedDocumentController]
733             noteNewRecentDocumentURL: o_true_file];
734     }
735
736     vlc_object_release( p_playlist );
737     return p_item;
738 }
739
740 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
741 {
742     int i_item;
743     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
744                                             FIND_ANYWHERE );
745     if( p_playlist == NULL )
746     {
747         return;
748     }
749
750     for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
751     {
752         playlist_item_t *p_item;
753         NSDictionary *o_one_item;
754
755         /* Get the item */
756         o_one_item = [o_array objectAtIndex: i_item];
757         p_item = [self createItem: o_one_item];
758         if( !p_item )
759         {
760             continue;
761         }
762
763         /* Add the item */
764         playlist_AddItem( p_playlist, p_item, PLAYLIST_APPEND, i_position == -1 ? PLAYLIST_END : i_position + i_item );
765
766         if( i_item == 0 && !b_enqueue )
767         {
768             playlist_Control( p_playlist, PLAYLIST_ITEMPLAY, p_item );
769         }
770     }
771     vlc_object_release( p_playlist );
772 }
773
774 - (void)appendNodeArray:(NSArray*)o_array inNode:(playlist_item_t *)p_node atPos:(int)i_position inView:(int)i_view enqueue:(BOOL)b_enqueue
775 {
776     int i_item;
777     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
778                                             FIND_ANYWHERE );
779     if( p_playlist == NULL )
780     {
781         return;
782     }
783
784     for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
785     {
786         playlist_item_t *p_item;
787         NSDictionary *o_one_item;
788
789         /* Get the item */
790         o_one_item = [o_array objectAtIndex: i_item];
791         p_item = [self createItem: o_one_item];
792         if( !p_item )
793         {
794             continue;
795         }
796
797         /* Add the item */
798         playlist_NodeAddItem( p_playlist, p_item, i_view, p_node, PLAYLIST_APPEND, i_position + i_item );
799
800         if( i_item == 0 && !b_enqueue )
801         {
802             playlist_Control( p_playlist, PLAYLIST_ITEMPLAY, p_item );
803         }
804     }
805     vlc_object_release( p_playlist );
806
807 }
808
809 - (IBAction)handlePopUp:(id)sender
810
811 {
812     intf_thread_t * p_intf = VLCIntf;
813     vlc_value_t val1,val2;
814     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
815                                             FIND_ANYWHERE );
816     if( p_playlist == NULL )
817     {
818         return;
819     }
820
821     switch( [o_loop_popup indexOfSelectedItem] )
822     {
823         case 1:
824
825              val1.b_bool = 0;
826              var_Set( p_playlist, "loop", val1 );
827              val1.b_bool = 1;
828              var_Set( p_playlist, "repeat", val1 );
829              vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat One" ) );
830         break;
831
832         case 2:
833              val1.b_bool = 0;
834              var_Set( p_playlist, "repeat", val1 );
835              val1.b_bool = 1;
836              var_Set( p_playlist, "loop", val1 );
837              vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat All" ) );
838         break;
839
840         default:
841              var_Get( p_playlist, "repeat", &val1 );
842              var_Get( p_playlist, "loop", &val2 );
843              if( val1.b_bool || val2.b_bool )
844              {
845                   val1.b_bool = 0;
846                   var_Set( p_playlist, "repeat", val1 );
847                   var_Set( p_playlist, "loop", val1 );
848                   vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
849              }
850          break;
851      }
852      vlc_object_release( p_playlist );
853      [self playlistUpdated];
854 }
855
856 - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
857 {
858     playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
859                                                        FIND_ANYWHERE );
860     playlist_item_t *p_selected_item;
861     int i_current, i_selected_row;
862
863     if( !p_playlist )
864         return NULL;
865
866     i_selected_row = [o_outline_view selectedRow];
867     if (i_selected_row < 0)
868         i_selected_row = 0;
869
870     p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow:
871                                             i_selected_row] pointerValue];
872
873     for( i_current = 0; i_current < p_item->i_children ; i_current++ )
874     {
875         char *psz_temp;
876         NSString *o_current_name, *o_current_author;
877
878         vlc_mutex_lock( &p_playlist->object_lock );
879         o_current_name = [NSString stringWithUTF8String:
880             p_item->pp_children[i_current]->input.psz_name];
881         psz_temp = vlc_input_item_GetInfo( &p_item->input ,
882                    _("Meta-information"),_("Artist") );
883         o_current_author = [NSString stringWithUTF8String: psz_temp];
884         free( psz_temp);
885         vlc_mutex_unlock( &p_playlist->object_lock );
886
887         if( p_selected_item == p_item->pp_children[i_current] &&
888                     b_selected_item_met == NO )
889         {
890             b_selected_item_met = YES;
891         }
892         else if( p_selected_item == p_item->pp_children[i_current] &&
893                     b_selected_item_met == YES )
894         {
895             vlc_object_release( p_playlist );
896             return NULL;
897         }
898         else if( b_selected_item_met == YES &&
899                     ( [o_current_name rangeOfString:[o_search_field
900                         stringValue] options:NSCaseInsensitiveSearch ].length ||
901                       [o_current_author rangeOfString:[o_search_field
902                         stringValue] options:NSCaseInsensitiveSearch ].length ) )
903         {
904             vlc_object_release( p_playlist );
905             /*Adds the parent items in the result array as well, so that we can
906             expand the tree*/
907             return [NSMutableArray arrayWithObject: [NSValue
908                             valueWithPointer: p_item->pp_children[i_current]]];
909         }
910         if( p_item->pp_children[i_current]->i_children > 0 )
911         {
912             id o_result = [self subSearchItem:
913                                             p_item->pp_children[i_current]];
914             if( o_result != NULL )
915             {
916                 vlc_object_release( p_playlist );
917                 [o_result insertObject: [NSValue valueWithPointer:
918                                 p_item->pp_children[i_current]] atIndex:0];
919                 return o_result;
920             }
921         }
922     }
923     vlc_object_release( p_playlist );
924     return NULL;
925 }
926
927 - (IBAction)searchItem:(id)sender
928 {
929     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
930                                                        FIND_ANYWHERE );
931     playlist_view_t * p_view;
932     id o_result;
933
934     unsigned int i;
935     int i_row = -1;
936
937     b_selected_item_met = NO;
938
939     if( p_playlist == NULL )
940         return;
941     p_view = playlist_ViewFind( p_playlist, i_current_view );
942
943     if( p_view )
944     {
945         /*First, only search after the selected item:*
946          *(b_selected_item_met = NO)                 */
947         o_result = [self subSearchItem:p_view->p_root];
948         if( o_result == NULL )
949         {
950             /* If the first search failed, search again from the beginning */
951             o_result = [self subSearchItem:p_view->p_root];
952         }
953         if( o_result != NULL )
954         {
955             int i_start;
956             if( [[o_result objectAtIndex: 0] pointerValue] ==
957                                                     p_playlist->p_general )
958             i_start = 1;
959             else
960             i_start = 0;
961
962             for( i = i_start ; i < [o_result count] - 1 ; i++ )
963             {
964                 [o_outline_view expandItem: [o_outline_dict objectForKey:
965                             [NSString stringWithFormat: @"%p",
966                             [[o_result objectAtIndex: i] pointerValue]]]];
967             }
968             i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
969                             [NSString stringWithFormat: @"%p",
970                             [[o_result objectAtIndex: [o_result count] - 1 ]
971                             pointerValue]]]];
972         }
973         if( i_row > -1 )
974         {
975             [o_outline_view selectRow:i_row byExtendingSelection: NO];
976             [o_outline_view scrollRowToVisible: i_row];
977         }
978     }
979     vlc_object_release( p_playlist );
980 }
981
982 - (IBAction)recursiveExpandNode:(id)sender
983 {
984     int i;
985     id o_item = [o_outline_view itemAtRow: [o_outline_view selectedRow]];
986     playlist_item_t *p_item = (playlist_item_t *)[o_item pointerValue];
987
988     if( ![[o_outline_view dataSource] outlineView: o_outline_view
989                                                     isItemExpandable: o_item] )
990     {
991         for( i = 0 ; i < p_item->i_parents ; i++ )
992         {
993             if( p_item->pp_parents[i]->i_view == i_current_view )
994             {
995                 o_item = [o_outline_dict objectForKey: [NSString
996                     stringWithFormat: @"%p", p_item->pp_parents[i]->p_parent]];
997                 break;
998             }
999         }
1000     }
1001
1002     /* We need to collapse the node first, since OSX refuses to recursively
1003        expand an already expanded node, even if children nodes are collapsed. */
1004     [o_outline_view collapseItem: o_item collapseChildren: YES];
1005     [o_outline_view expandItem: o_item expandChildren: YES];
1006 }
1007
1008 - (NSMenu *)menuForEvent:(NSEvent *)o_event
1009 {
1010     NSPoint pt;
1011     vlc_bool_t b_rows;
1012     vlc_bool_t b_item_sel;
1013
1014     pt = [o_outline_view convertPoint: [o_event locationInWindow]
1015                                                  fromView: nil];
1016     b_item_sel = ( [o_outline_view rowAtPoint: pt] != -1 &&
1017                    [o_outline_view selectedRow] != -1 );
1018     b_rows = [o_outline_view numberOfRows] != 0;
1019
1020     [o_mi_play setEnabled: b_item_sel];
1021     [o_mi_delete setEnabled: b_item_sel];
1022     [o_mi_selectall setEnabled: b_rows];
1023     [o_mi_info setEnabled: b_item_sel];
1024     [o_mi_recursive_expand setEnabled: b_item_sel];
1025     [o_mi_sort_name setEnabled: b_item_sel];
1026     [o_mi_sort_author setEnabled: b_item_sel];
1027
1028     return( o_ctx_menu );
1029 }
1030
1031 - (playlist_item_t *)selectedPlaylistItem
1032 {
1033     return [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
1034                                                                 pointerValue];
1035 }
1036
1037 - (void)outlineView: (NSTableView*)o_tv
1038                   didClickTableColumn:(NSTableColumn *)o_tc
1039 {
1040     int i_mode = 0, i_type;
1041     intf_thread_t *p_intf = VLCIntf;
1042     playlist_view_t *p_view;
1043
1044     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1045                                        FIND_ANYWHERE );
1046     if( p_playlist == NULL )
1047     {
1048         return;
1049     }
1050
1051     /* Check whether the selected table column header corresponds to a
1052        sortable table column*/
1053     if( !( o_tc == o_tc_name || o_tc == o_tc_author ) )
1054     {
1055         vlc_object_release( p_playlist );
1056         return;
1057     }
1058
1059     p_view = playlist_ViewFind( p_playlist, i_current_view );
1060
1061     if( o_tc_sortColumn == o_tc )
1062     {
1063         b_isSortDescending = !b_isSortDescending;
1064     }
1065     else
1066     {
1067         b_isSortDescending = VLC_FALSE;
1068     }
1069
1070     if( o_tc == o_tc_name )
1071     {
1072         i_mode = SORT_TITLE;
1073     }
1074     else if( o_tc == o_tc_author )
1075     {
1076         i_mode = SORT_AUTHOR;
1077     }
1078
1079     if( b_isSortDescending )
1080     {
1081         i_type = ORDER_REVERSE;
1082     }
1083     else
1084     {
1085         i_type = ORDER_NORMAL;
1086     }
1087
1088     vlc_mutex_lock( &p_playlist->object_lock );
1089     playlist_RecursiveNodeSort( p_playlist, p_view->p_root, i_mode, i_type );
1090     vlc_mutex_unlock( &p_playlist->object_lock );
1091
1092     vlc_object_release( p_playlist );
1093     [self playlistUpdated];
1094
1095     o_tc_sortColumn = o_tc;
1096     [o_outline_view setHighlightedTableColumn:o_tc];
1097
1098     if( b_isSortDescending )
1099     {
1100         [o_outline_view setIndicatorImage:o_descendingSortingImage
1101                                                         inTableColumn:o_tc];
1102     }
1103     else
1104     {
1105         [o_outline_view setIndicatorImage:o_ascendingSortingImage
1106                                                         inTableColumn:o_tc];
1107     }
1108 }
1109
1110
1111 - (void)outlineView:(NSOutlineView *)outlineView
1112                                 willDisplayCell:(id)cell
1113                                 forTableColumn:(NSTableColumn *)tableColumn
1114                                 item:(id)item
1115 {
1116     playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1117                                           FIND_ANYWHERE );
1118
1119     id o_playing_item;
1120
1121     if( !p_playlist ) return;
1122
1123     o_playing_item = [o_outline_dict objectForKey:
1124                 [NSString stringWithFormat:@"%p",  p_playlist->status.p_item]];
1125
1126     if( [self isValueItem: o_playing_item inNode: item] ||
1127                                                 [o_playing_item isEqual: item] )
1128     {
1129         [cell setFont: [NSFont boldSystemFontOfSize: 0]];
1130     }
1131     else
1132     {
1133         [cell setFont: [NSFont systemFontOfSize: 0]];
1134     }
1135     vlc_object_release( p_playlist );
1136 }
1137
1138 @end
1139
1140 @implementation VLCPlaylist (NSOutlineViewDataSource)
1141
1142 /* return the number of children for Obj-C pointer item */ /* DONE */
1143 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
1144 {
1145     int i_return = 0;
1146     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1147                                                        FIND_ANYWHERE );
1148     if( p_playlist == NULL || outlineView != o_outline_view )
1149         return 0;
1150
1151     if( item == nil )
1152     {
1153         /* root object */
1154         playlist_view_t *p_view;
1155         p_view = playlist_ViewFind( p_playlist, i_current_view );
1156         if( p_view && p_view->p_root )
1157         {
1158             i_return = p_view->p_root->i_children;
1159             if( i_current_view == VIEW_CATEGORY )
1160             {
1161                 i_return--; /* remove the GENERAL item from the list */
1162                 i_return += p_playlist->p_general->i_children; /* add the items of the general node */
1163             }
1164         }
1165     }
1166     else
1167     {
1168         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
1169         if( p_item )
1170             i_return = p_item->i_children;
1171     }
1172     vlc_object_release( p_playlist );
1173     
1174     if( i_return <= 0 )
1175         i_return = 0;
1176     
1177     return i_return;
1178 }
1179
1180 /* return the child at index for the Obj-C pointer item */ /* DONE */
1181 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
1182 {
1183     playlist_item_t *p_return = NULL;
1184     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1185                                                        FIND_ANYWHERE );
1186     NSValue *o_value;
1187
1188     if( p_playlist == NULL )
1189         return nil;
1190
1191     if( item == nil )
1192     {
1193         /* root object */
1194         playlist_view_t *p_view;
1195         p_view = playlist_ViewFind( p_playlist, i_current_view );
1196         if( p_view && p_view->p_root ) p_return = p_view->p_root->pp_children[index];
1197         
1198         if( i_current_view == VIEW_CATEGORY )
1199         {
1200             if( p_playlist->p_general->i_children && index >= 0 && index < p_playlist->p_general->i_children )
1201             {
1202                 p_return = p_playlist->p_general->pp_children[index];
1203             }
1204             else if( p_view && p_view->p_root && index >= 0 && index - p_playlist->p_general->i_children < p_view->p_root->i_children )
1205             {
1206                 p_return = p_view->p_root->pp_children[index - p_playlist->p_general->i_children + 1];
1207                 
1208             }
1209         }
1210     }
1211     else
1212     {
1213         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
1214         if( p_item && index < p_item->i_children && index >= 0 )
1215             p_return = p_item->pp_children[index];
1216     }
1217     
1218     if( p_playlist->i_size >= 2 )
1219     {
1220         [o_status_field setStringValue: [NSString stringWithFormat:
1221                     _NS("%i items in playlist"), p_playlist->i_size]];
1222     }
1223     else
1224     {
1225         if( p_playlist->i_size == 0 )
1226         {
1227             [o_status_field setStringValue: [NSString stringWithFormat:
1228                     _NS("no items in playlist"), p_playlist->i_size]];
1229         }
1230         else
1231         {
1232             [o_status_field setStringValue: [NSString stringWithFormat:
1233                     _NS("1 item in playlist"), p_playlist->i_size]];
1234         }
1235     }
1236
1237     vlc_object_release( p_playlist );
1238
1239     o_value = [[NSValue valueWithPointer: p_return] retain];
1240
1241     [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p", p_return]];
1242     return o_value;
1243 }
1244
1245 /* is the item expandable */
1246 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
1247 {
1248     int i_return = 0;
1249     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1250                                                        FIND_ANYWHERE );
1251     if( p_playlist == NULL )
1252         return NO;
1253
1254     if( item == nil )
1255     {
1256         /* root object */
1257         playlist_view_t *p_view;
1258         p_view = playlist_ViewFind( p_playlist, i_current_view );
1259         if( p_view && p_view->p_root ) i_return = p_view->p_root->i_children;
1260         
1261         if( i_current_view == VIEW_CATEGORY )
1262         {
1263             i_return--;
1264             i_return += p_playlist->p_general->i_children;
1265         }
1266     }
1267     else
1268     {
1269         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
1270         if( p_item )
1271             i_return = p_item->i_children;
1272     }
1273     vlc_object_release( p_playlist );
1274
1275     if( i_return <= 0 )
1276         return NO;
1277     else
1278         return YES;
1279 }
1280
1281 /* retrieve the string values for the cells */
1282 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
1283 {
1284     id o_value = nil;
1285     intf_thread_t *p_intf = VLCIntf;
1286     playlist_t *p_playlist;
1287     playlist_item_t *p_item;
1288     
1289     if( item == nil || ![item isKindOfClass: [NSValue class]] ) return( @"error" );
1290     
1291     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1292                                                FIND_ANYWHERE );
1293     if( p_playlist == NULL )
1294     {
1295         return( @"error" );
1296     }
1297
1298     p_item = (playlist_item_t *)[item pointerValue];
1299
1300     if( p_item == NULL )
1301     {
1302         vlc_object_release( p_playlist );
1303         return( @"error");
1304     }
1305
1306     if( [[o_tc identifier] isEqualToString:@"1"] )
1307     {
1308         o_value = [NSString stringWithUTF8String:
1309             p_item->input.psz_name];
1310         if( o_value == NULL )
1311             o_value = [NSString stringWithCString:
1312                 p_item->input.psz_name];
1313     }
1314     else if( [[o_tc identifier] isEqualToString:@"2"] )
1315     {
1316         char *psz_temp;
1317         psz_temp = vlc_input_item_GetInfo( &p_item->input ,_("Meta-information"),_("Artist") );
1318
1319         if( psz_temp == NULL )
1320             o_value = @"";
1321         else
1322         {
1323             o_value = [NSString stringWithUTF8String: psz_temp];
1324             if( o_value == NULL )
1325             {
1326                 o_value = [NSString stringWithCString: psz_temp];
1327             }
1328             free( psz_temp );
1329         }
1330     }
1331     else if( [[o_tc identifier] isEqualToString:@"3"] )
1332     {
1333         char psz_duration[MSTRTIME_MAX_SIZE];
1334         mtime_t dur = p_item->input.i_duration;
1335         if( dur != -1 )
1336         {
1337             secstotimestr( psz_duration, dur/1000000 );
1338             o_value = [NSString stringWithUTF8String: psz_duration];
1339         }
1340         else
1341         {
1342             o_value = @"-:--:--";
1343         }
1344     }
1345     vlc_object_release( p_playlist );
1346
1347     return( o_value );
1348 }
1349
1350 /* Required for drag & drop and reordering */
1351 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
1352 {
1353     unsigned int i,j;
1354     playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1355                                                FIND_ANYWHERE );
1356     NSMutableArray *o_nodes_array = [NSMutableArray array];
1357     NSMutableArray *o_items_array = [NSMutableArray array];
1358     NSMutableArray *o_objects_array = [NSMutableArray array];
1359
1360     if( !p_playlist ) return NO;
1361
1362     [pboard declareTypes: [NSArray arrayWithObjects:
1363         @"VLCPlaylistItemPboardType", NSFilenamesPboardType, nil] owner: self];
1364
1365     for( i = 0 ; i < [items count] ; i++ )
1366     {
1367         id o_item = [items objectAtIndex: i];
1368
1369         if( ![self isItem: [o_item pointerValue] inNode:
1370                                         p_playlist->p_general] )
1371         {
1372             vlc_object_release(p_playlist);
1373             return NO;
1374         }
1375         if( ((playlist_item_t *)[o_item pointerValue])->i_children > 0 )
1376             [o_nodes_array addObject: o_item];
1377         else
1378             [o_items_array addObject: o_item];
1379     }
1380
1381     /* Now we need to check if there are selected items that are in already
1382        selected nodes. In that case, we only want to move the nodes */
1383     for( i = 0 ; i < [o_nodes_array count] ; i++ )
1384     {
1385         for ( j = 0 ; j < [o_nodes_array count] ; j++ )
1386         {
1387             if( j == i ) continue;
1388             if( [self isItem: [[o_nodes_array objectAtIndex:i] pointerValue]
1389                     inNode: [[o_nodes_array objectAtIndex:j] pointerValue]] )
1390             {
1391                 [o_nodes_array removeObjectAtIndex:i];
1392                 /* We need to execute the next iteration with the same index
1393                    since the current item has been deleted */
1394                 i--;
1395                 break;
1396             }
1397         }
1398     }
1399
1400     for( i = 0 ; i < [o_items_array count] ; i++ )
1401     {
1402         for ( j = 0 ; j < [o_nodes_array count] ; j++ )
1403         {
1404             if( [self isItem: [[o_items_array objectAtIndex:i] pointerValue]
1405                     inNode: [[o_nodes_array objectAtIndex:j] pointerValue]] )
1406             {
1407                 [o_items_array removeObjectAtIndex:i];
1408                 i--;
1409                 break;
1410             }
1411             if( j == [o_nodes_array count] ) i++;
1412         }
1413     }
1414
1415     [o_objects_array addObjectsFromArray: o_nodes_array];
1416     [o_objects_array addObjectsFromArray: o_items_array];
1417
1418     if( ![pboard setPropertyList: o_objects_array
1419             forType:@"VLCPlaylistItemPboardType"] ||
1420             ![pboard setPropertyList: [NSArray array]
1421             forType:@"VLCPlaylistItemPboardType"])
1422     {
1423         vlc_object_release(p_playlist);
1424         return NO;
1425     }
1426
1427     vlc_object_release(p_playlist);
1428     return YES;
1429 }
1430
1431 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
1432 {
1433     playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1434                                                FIND_ANYWHERE );
1435     NSPasteboard *o_pasteboard = [info draggingPasteboard];
1436
1437     if( !p_playlist ) return NSDragOperationNone;
1438
1439     /* Drop from the Playlist */
1440     if( [[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] &&
1441         [self isItem: [item pointerValue] inNode: p_playlist->p_general])
1442     {
1443         vlc_object_release(p_playlist);
1444         return NSDragOperationMove;
1445     }
1446
1447     /* Drop from the Finder */
1448     else if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] &&
1449         ![[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"])
1450     {
1451         vlc_object_release(p_playlist);
1452         return NSDragOperationGeneric;
1453     }
1454     vlc_object_release(p_playlist);
1455     return NSDragOperationNone;
1456 }
1457
1458 - (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(int)index
1459 {
1460     playlist_t * p_playlist =  vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1461                                                        FIND_ANYWHERE );
1462     NSPasteboard *o_pasteboard = [info draggingPasteboard];
1463
1464     if( !p_playlist ) return NO;
1465
1466     if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1467     {
1468         int i;
1469         playlist_item_t *p_node = [item pointerValue];
1470
1471         NSArray *o_array = [NSArray array];
1472         NSArray *o_values = [[o_pasteboard propertyListForType:
1473                                         NSFilenamesPboardType]
1474                                 sortedArrayUsingSelector:
1475                                         @selector(caseInsensitiveCompare:)];
1476
1477         for( i = 0; i < (int)[o_values count]; i++)
1478         {
1479             NSDictionary *o_dic;
1480             o_dic = [NSDictionary dictionaryWithObject:[o_values
1481                         objectAtIndex:i] forKey:@"ITEM_URL"];
1482             o_array = [o_array arrayByAddingObject: o_dic];
1483         }
1484
1485         if ( item == nil )
1486         {
1487             [self appendArray: o_array atPos: index enqueue: YES];
1488         }
1489         else if( p_node->i_children == -1 )
1490         {
1491             int i_counter;
1492             playlist_item_t *p_real_node = NULL;
1493
1494             for( i_counter = 0 ; i_counter < p_node->i_parents ; i_counter++ )
1495             {
1496                 if( p_node->pp_parents[i_counter]->i_view == i_current_view )
1497                 {
1498                     p_real_node = p_node->pp_parents[i_counter]->p_parent;
1499                     break;
1500                 }
1501                 if( i_counter == p_node->i_parents )
1502                 {
1503                     return NO;
1504                 }
1505             }
1506             [self appendNodeArray: o_array inNode: p_real_node
1507                 atPos: index inView: i_current_view enqueue: YES];
1508         }
1509         else
1510         {
1511             [self appendNodeArray: o_array inNode: p_node
1512                 atPos: index inView: i_current_view enqueue: YES];
1513         }
1514         vlc_object_release( p_playlist );
1515         return YES;
1516     }
1517     vlc_object_release( p_playlist );
1518     return NO;
1519 }
1520
1521 /* Delegate method of NSWindow */
1522 /*- (void)windowWillClose:(NSNotification *)aNotification
1523 {
1524     [o_btn_playlist setState: NSOffState];
1525 }
1526 */
1527 @end
1528
1529