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