]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlist.m
* Don't select items that are in a selected node for 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_selectall setTitle: _NS("Select All")];
232     [o_mi_info setTitle: _NS("Properties")];
233     [o_mi_sort_name setTitle: _NS("Sort Node by Name")];
234     [o_mi_sort_author setTitle: _NS("Sort Node by Author")];
235     [o_mi_services setTitle: _NS("Services discovery")];
236     [[o_tc_name headerCell] setStringValue:_NS("Name")];
237     [[o_tc_author headerCell] setStringValue:_NS("Author")];
238     [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
239     [o_status_field setStringValue: [NSString stringWithFormat:
240                         _NS("no items in playlist")]];
241
242     [o_random_ckb setTitle: _NS("Random")];
243 #if 0
244     [o_search_button setTitle: _NS("Search")];
245 #endif
246     [o_search_field setToolTip: _NS("Search in Playlist")];
247     [[o_loop_popup itemAtIndex:0] setTitle: _NS("Standard Play")];
248     [[o_loop_popup itemAtIndex:1] setTitle: _NS("Repeat One")];
249     [[o_loop_popup itemAtIndex:2] setTitle: _NS("Repeat All")];
250 }
251
252 - (NSOutlineView *)outlineView
253 {
254     return o_outline_view;
255 }
256
257 - (void)playlistUpdated
258 {
259     unsigned int i;
260
261     /* Clear indications of any existing column sorting*/
262     for( i = 0 ; i < [[o_outline_view tableColumns] count] ; i++ )
263     {
264         [o_outline_view setIndicatorImage:nil inTableColumn:
265                             [[o_outline_view tableColumns] objectAtIndex:i]];
266     }
267
268     [o_outline_view setHighlightedTableColumn:nil];
269     o_tc_sortColumn = nil;
270     // TODO Find a way to keep the dict size to a minimum
271     //[o_outline_dict removeAllObjects];
272     [o_outline_view reloadData];
273 }
274
275 - (void)playModeUpdated
276 {
277     playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
278                                           FIND_ANYWHERE );
279     vlc_value_t val, val2;
280
281     if( p_playlist == NULL )
282     {
283         return;
284     }
285
286     var_Get( p_playlist, "loop", &val2 );
287     var_Get( p_playlist, "repeat", &val );
288     if( val.b_bool == VLC_TRUE )
289     {
290         [o_loop_popup selectItemAtIndex: 1];
291    }
292     else if( val2.b_bool == VLC_TRUE )
293     {
294         [o_loop_popup selectItemAtIndex: 2];
295     }
296     else
297     {
298         [o_loop_popup selectItemAtIndex: 0];
299     }
300
301     var_Get( p_playlist, "random", &val );
302     [o_random_ckb setState: val.b_bool];
303
304     vlc_object_release( p_playlist );
305 }
306
307 - (void)updateRowSelection
308 {
309     int i,i_row;
310     unsigned int j;
311
312     playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
313                                           FIND_ANYWHERE );
314     playlist_item_t *p_item, *p_temp_item;
315     NSMutableArray *o_array = [NSMutableArray array];
316
317     if( p_playlist == NULL )
318         return;
319
320     p_item = p_playlist->status.p_item;
321     if( p_item == NULL ) return;
322
323     p_temp_item = p_item;
324     while( p_temp_item->i_parents > 0 )
325     {
326         [o_array insertObject: [NSValue valueWithPointer: p_temp_item] atIndex: 0];
327         for (i = 0 ; i < p_temp_item->i_parents ; i++)
328         {
329             if( p_temp_item->pp_parents[i]->i_view == i_current_view )
330             {
331                 p_temp_item = p_temp_item->pp_parents[i]->p_parent;
332                 break;
333             }
334         }
335     }
336
337     for (j = 0 ; j < [o_array count] - 1 ; j++)
338     {
339         id o_item;
340         if( ( o_item = [o_outline_dict objectForKey:
341                             [NSString stringWithFormat: @"%p",
342                             [[o_array objectAtIndex:j] pointerValue]]] ) != nil )
343             [o_outline_view expandItem: o_item];
344
345     }
346
347     i_row = [o_outline_view rowForItem:[o_outline_dict
348             objectForKey:[NSString stringWithFormat: @"%p", p_item]]];
349
350     [o_outline_view selectRow: i_row byExtendingSelection: NO];
351     [o_outline_view scrollRowToVisible: i_row];
352
353     vlc_object_release(p_playlist);
354 }
355
356 - (BOOL)isItem: (playlist_item_t *)p_item inNode: (playlist_item_t *)p_node
357 {
358     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
359                                           FIND_ANYWHERE );
360     playlist_item_t *p_temp_item = p_item;
361
362     if( p_playlist == NULL )
363     {
364         return NO;
365     }
366
367     if ( p_temp_item )
368     {
369         int i;
370         vlc_mutex_lock( &p_playlist->object_lock );
371
372 // Let's check if we can do without that
373 #if 0
374
375         /* Since outlineView: willDisplayCell:... may call this function with
376            p_items that don't exist anymore, first check if the item is still
377            in the playlist. Any cleaner solution welcomed. */
378
379         {
380             if( p_playlist->pp_all_items[i] == p_item ) break;
381             else if ( i == p_playlist->i_all_size - 1 )
382             {
383                 vlc_object_release( p_playlist );
384                 vlc_mutex_unlock( &p_playlist->object_lock );
385                 return NO;
386             }
387         }
388 #endif
389         while( p_temp_item->i_parents > 0 )
390         {
391             for( i = 0; i < p_temp_item->i_parents ; i++ )
392             {
393                 if( p_temp_item->pp_parents[i]->i_view == i_current_view )
394                 {
395                     if( p_temp_item->pp_parents[i]->p_parent == p_node )
396                     {
397                         vlc_mutex_unlock( &p_playlist->object_lock );
398                         vlc_object_release( p_playlist );
399                         return YES;
400                     }
401                     else
402                     {
403                         p_temp_item = p_temp_item->pp_parents[i]->p_parent;
404                         break;
405                     }
406                 }
407             }
408             vlc_mutex_unlock( &p_playlist->object_lock );
409         }
410     }
411
412     vlc_object_release( p_playlist );
413     return NO;
414 }
415
416 - (BOOL)isValueItem: (id)o_item inNode: (id)o_node
417 {
418     int i;
419     int i_total = [[o_outline_view dataSource] outlineView:o_outline_view
420                                         numberOfChildrenOfItem: o_node];
421     for( i = 0 ; i < i_total ; i++ )
422     {
423         id o_temp_item = [[o_outline_view dataSource] outlineView:
424                                     o_outline_view child:i ofItem: o_node];
425         if( [[o_outline_view dataSource] outlineView:o_outline_view
426                                     numberOfChildrenOfItem: o_temp_item] > 0 )
427         {
428             if( [self isValueItem: o_item inNode: o_temp_item] == YES )
429             return YES;
430         }
431         else if( [o_temp_item isEqual: o_item] )
432         {
433             return YES;
434         }
435     }
436     return NO;
437 }
438
439 - (IBAction)savePlaylist:(id)sender
440 {
441     intf_thread_t * p_intf = VLCIntf;
442     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
443                                                        FIND_ANYWHERE );
444
445     NSSavePanel *o_save_panel = [NSSavePanel savePanel];
446     NSString * o_name = [NSString stringWithFormat: @"%@.m3u", _NS("Untitled")];
447     [o_save_panel setTitle: _NS("Save Playlist")];
448     [o_save_panel setPrompt: _NS("Save")];
449
450     if( [o_save_panel runModalForDirectory: nil
451             file: o_name] == NSOKButton )
452     {
453         playlist_Export( p_playlist, [[o_save_panel filename] fileSystemRepresentation], "export-m3u" );
454     }
455 }
456
457
458 /* When called retrieves the selected outlineview row and plays that node or item */
459 - (IBAction)playItem:(id)sender
460 {
461     intf_thread_t * p_intf = VLCIntf;
462     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
463                                                        FIND_ANYWHERE );
464
465     if( p_playlist != NULL )
466     {
467         playlist_item_t *p_item;
468         playlist_item_t *p_node = NULL;
469         int i;
470
471         p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
472
473         if( p_item )
474         {
475             if( p_item->i_children == -1 )
476             {
477                 for( i = 0 ; i < p_item->i_parents ; i++ )
478                 {
479                     if( p_item->pp_parents[i]->i_view == i_current_view )
480                     {
481                         p_node = p_item->pp_parents[i]->p_parent;
482                     }
483                 }
484             }
485             else
486             {
487                 p_node = p_item;
488                 if( p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1 )
489                 {
490                     p_item = p_node->pp_children[0];
491                 }
492                 else
493                 {
494                     p_item = NULL;
495                 }
496             }
497             playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view, p_node, p_item );
498         }
499         vlc_object_release( p_playlist );
500     }
501 }
502
503 - (IBAction)servicesChange:(id)sender
504 {
505     NSMenuItem *o_mi = (NSMenuItem *)sender;
506     NSString *o_string = [o_mi representedObject];
507     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
508                                           FIND_ANYWHERE );
509     if( !playlist_IsServicesDiscoveryLoaded( p_playlist, [o_string cString] ) )
510         playlist_ServicesDiscoveryAdd( p_playlist, [o_string cString] );
511     else
512         playlist_ServicesDiscoveryRemove( p_playlist, [o_string cString] );
513
514     [o_mi setState: playlist_IsServicesDiscoveryLoaded( p_playlist,
515                                           [o_string cString] ) ? YES : NO];
516
517     i_current_view = VIEW_CATEGORY;
518     playlist_ViewUpdate( p_playlist, i_current_view );
519     vlc_object_release( p_playlist );
520     [self playlistUpdated];
521     return;
522 }
523
524 - (IBAction)selectAll:(id)sender
525 {
526     [o_outline_view selectAll: nil];
527 }
528
529 - (IBAction)deleteItem:(id)sender
530 {
531     int i, i_count, i_row;
532     NSMutableArray *o_to_delete;
533     NSNumber *o_number;
534
535     playlist_t * p_playlist;
536     intf_thread_t * p_intf = VLCIntf;
537
538     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
539                                           FIND_ANYWHERE );
540
541     if ( p_playlist == NULL )
542     {
543         return;
544     }
545     o_to_delete = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
546     i_count = [o_to_delete count];
547
548     for( i = 0; i < i_count; i++ )
549     {
550         o_number = [o_to_delete lastObject];
551         i_row = [o_number intValue];
552         id o_item = [o_outline_view itemAtRow: i_row];
553         playlist_item_t *p_item = [o_item pointerValue];
554         [o_to_delete removeObject: o_number];
555         [o_outline_view deselectRow: i_row];
556
557         if( [[o_outline_view dataSource] outlineView:o_outline_view
558                                         numberOfChildrenOfItem: o_item]  > 0 )
559         //is a node and not an item
560         {
561             id o_playing_item = [o_outline_dict objectForKey:
562                 [NSString stringWithFormat: @"%p", p_playlist->status.p_item]];
563             if( p_playlist->status.i_status != PLAYLIST_STOPPED &&
564                 [self isValueItem: o_playing_item inNode: o_item] == 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 - (NSMenu *)menuForEvent:(NSEvent *)o_event
983 {
984     NSPoint pt;
985     vlc_bool_t b_rows;
986     vlc_bool_t b_item_sel;
987
988     pt = [o_outline_view convertPoint: [o_event locationInWindow]
989                                                  fromView: nil];
990     b_item_sel = ( [o_outline_view rowAtPoint: pt] != -1 &&
991                    [o_outline_view selectedRow] != -1 );
992     b_rows = [o_outline_view numberOfRows] != 0;
993
994     [o_mi_play setEnabled: b_item_sel];
995     [o_mi_delete setEnabled: b_item_sel];
996     [o_mi_selectall setEnabled: b_rows];
997     [o_mi_info setEnabled: b_item_sel];
998
999     return( o_ctx_menu );
1000 }
1001
1002 - (playlist_item_t *)selectedPlaylistItem
1003 {
1004     return [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
1005                                                                 pointerValue];
1006 }
1007
1008 - (void)outlineView: (NSTableView*)o_tv
1009                   didClickTableColumn:(NSTableColumn *)o_tc
1010 {
1011     int i_mode = 0, i_type;
1012     intf_thread_t *p_intf = VLCIntf;
1013     playlist_view_t *p_view;
1014
1015     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1016                                        FIND_ANYWHERE );
1017     if( p_playlist == NULL )
1018     {
1019         return;
1020     }
1021
1022     /* Check whether the selected table column header corresponds to a
1023        sortable table column*/
1024     if( !( o_tc == o_tc_name || o_tc == o_tc_author ) )
1025     {
1026         vlc_object_release( p_playlist );
1027         return;
1028     }
1029
1030     p_view = playlist_ViewFind( p_playlist, i_current_view );
1031
1032     if( o_tc_sortColumn == o_tc )
1033     {
1034         b_isSortDescending = !b_isSortDescending;
1035     }
1036     else
1037     {
1038         b_isSortDescending = VLC_FALSE;
1039     }
1040
1041     if( o_tc == o_tc_name )
1042     {
1043         i_mode = SORT_TITLE;
1044     }
1045     else if( o_tc == o_tc_author )
1046     {
1047         i_mode = SORT_AUTHOR;
1048     }
1049
1050     if( b_isSortDescending )
1051     {
1052         i_type = ORDER_REVERSE;
1053     }
1054     else
1055     {
1056         i_type = ORDER_NORMAL;
1057     }
1058
1059     vlc_mutex_lock( &p_playlist->object_lock );
1060     playlist_RecursiveNodeSort( p_playlist, p_view->p_root, i_mode, i_type );
1061     vlc_mutex_unlock( &p_playlist->object_lock );
1062
1063     vlc_object_release( p_playlist );
1064     [self playlistUpdated];
1065
1066     o_tc_sortColumn = o_tc;
1067     [o_outline_view setHighlightedTableColumn:o_tc];
1068
1069     if( b_isSortDescending )
1070     {
1071         [o_outline_view setIndicatorImage:o_descendingSortingImage
1072                                                         inTableColumn:o_tc];
1073     }
1074     else
1075     {
1076         [o_outline_view setIndicatorImage:o_ascendingSortingImage
1077                                                         inTableColumn:o_tc];
1078     }
1079 }
1080
1081
1082 - (void)outlineView:(NSOutlineView *)outlineView
1083                                 willDisplayCell:(id)cell
1084                                 forTableColumn:(NSTableColumn *)tableColumn
1085                                 item:(id)item
1086 {
1087     playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1088                                           FIND_ANYWHERE );
1089
1090     id o_playing_item;
1091
1092     if( !p_playlist ) return;
1093
1094     o_playing_item = [o_outline_dict objectForKey:
1095                 [NSString stringWithFormat:@"%p",  p_playlist->status.p_item]];
1096
1097     if( [self isValueItem: o_playing_item inNode: item] ||
1098                                                 [o_playing_item isEqual: item] )
1099     {
1100         [cell setFont: [NSFont boldSystemFontOfSize: 0]];
1101     }
1102     else
1103     {
1104         [cell setFont: [NSFont systemFontOfSize: 0]];
1105     }
1106     vlc_object_release( p_playlist );
1107 }
1108
1109 @end
1110
1111 @implementation VLCPlaylist (NSOutlineViewDataSource)
1112
1113 /* return the number of children for Obj-C pointer item */ /* DONE */
1114 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
1115 {
1116     int i_return = 0;
1117     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1118                                                        FIND_ANYWHERE );
1119     if( p_playlist == NULL || outlineView != o_outline_view )
1120         return 0;
1121
1122     if( item == nil )
1123     {
1124         /* root object */
1125         playlist_view_t *p_view;
1126         p_view = playlist_ViewFind( p_playlist, i_current_view );
1127         if( p_view && p_view->p_root )
1128         {
1129             i_return = p_view->p_root->i_children;
1130             if( i_current_view == VIEW_CATEGORY )
1131             {
1132                 i_return--; /* remove the GENERAL item from the list */
1133                 i_return += p_playlist->p_general->i_children; /* add the items of the general node */
1134             }
1135         }
1136     }
1137     else
1138     {
1139         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
1140         if( p_item )
1141             i_return = p_item->i_children;
1142     }
1143     vlc_object_release( p_playlist );
1144     
1145     if( i_return <= 0 )
1146         i_return = 0;
1147     
1148     return i_return;
1149 }
1150
1151 /* return the child at index for the Obj-C pointer item */ /* DONE */
1152 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
1153 {
1154     playlist_item_t *p_return = NULL;
1155     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1156                                                        FIND_ANYWHERE );
1157     NSValue *o_value;
1158
1159     if( p_playlist == NULL )
1160         return nil;
1161
1162     if( item == nil )
1163     {
1164         /* root object */
1165         playlist_view_t *p_view;
1166         p_view = playlist_ViewFind( p_playlist, i_current_view );
1167         if( p_view && p_view->p_root ) p_return = p_view->p_root->pp_children[index];
1168         
1169         if( i_current_view == VIEW_CATEGORY )
1170         {
1171             if( p_playlist->p_general->i_children && index >= 0 && index < p_playlist->p_general->i_children )
1172             {
1173                 p_return = p_playlist->p_general->pp_children[index];
1174             }
1175             else if( p_view && p_view->p_root && index >= 0 && index - p_playlist->p_general->i_children < p_view->p_root->i_children )
1176             {
1177                 p_return = p_view->p_root->pp_children[index - p_playlist->p_general->i_children + 1];
1178                 
1179             }
1180         }
1181     }
1182     else
1183     {
1184         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
1185         if( p_item && index < p_item->i_children && index >= 0 )
1186             p_return = p_item->pp_children[index];
1187     }
1188     
1189     if( p_playlist->i_size >= 2 )
1190     {
1191         [o_status_field setStringValue: [NSString stringWithFormat:
1192                     _NS("%i items in playlist"), p_playlist->i_size]];
1193     }
1194     else
1195     {
1196         if( p_playlist->i_size == 0 )
1197         {
1198             [o_status_field setStringValue: [NSString stringWithFormat:
1199                     _NS("no items in playlist"), p_playlist->i_size]];
1200         }
1201         else
1202         {
1203             [o_status_field setStringValue: [NSString stringWithFormat:
1204                     _NS("1 item in playlist"), p_playlist->i_size]];
1205         }
1206     }
1207
1208     vlc_object_release( p_playlist );
1209
1210     o_value = [[NSValue valueWithPointer: p_return] retain];
1211
1212     [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p", p_return]];
1213     return o_value;
1214 }
1215
1216 /* is the item expandable */
1217 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
1218 {
1219     int i_return = 0;
1220     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1221                                                        FIND_ANYWHERE );
1222     if( p_playlist == NULL )
1223         return NO;
1224
1225     if( item == nil )
1226     {
1227         /* root object */
1228         playlist_view_t *p_view;
1229         p_view = playlist_ViewFind( p_playlist, i_current_view );
1230         if( p_view && p_view->p_root ) i_return = p_view->p_root->i_children;
1231         
1232         if( i_current_view == VIEW_CATEGORY )
1233         {
1234             i_return--;
1235             i_return += p_playlist->p_general->i_children;
1236         }
1237     }
1238     else
1239     {
1240         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
1241         if( p_item )
1242             i_return = p_item->i_children;
1243     }
1244     vlc_object_release( p_playlist );
1245
1246     if( i_return <= 0 )
1247         return NO;
1248     else
1249         return YES;
1250 }
1251
1252 /* retrieve the string values for the cells */
1253 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
1254 {
1255     id o_value = nil;
1256     intf_thread_t *p_intf = VLCIntf;
1257     playlist_t *p_playlist;
1258     playlist_item_t *p_item;
1259     
1260     if( item == nil || ![item isKindOfClass: [NSValue class]] ) return( @"error" );
1261     
1262     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1263                                                FIND_ANYWHERE );
1264     if( p_playlist == NULL )
1265     {
1266         return( @"error" );
1267     }
1268
1269     p_item = (playlist_item_t *)[item pointerValue];
1270
1271     if( p_item == NULL )
1272     {
1273         vlc_object_release( p_playlist );
1274         return( @"error");
1275     }
1276
1277     if( [[o_tc identifier] isEqualToString:@"1"] )
1278     {
1279         o_value = [NSString stringWithUTF8String:
1280             p_item->input.psz_name];
1281         if( o_value == NULL )
1282             o_value = [NSString stringWithCString:
1283                 p_item->input.psz_name];
1284     }
1285     else if( [[o_tc identifier] isEqualToString:@"2"] )
1286     {
1287         char *psz_temp;
1288         psz_temp = vlc_input_item_GetInfo( &p_item->input ,_("Meta-information"),_("Artist") );
1289
1290         if( psz_temp == NULL )
1291             o_value = @"";
1292         else
1293         {
1294             o_value = [NSString stringWithUTF8String: psz_temp];
1295             if( o_value == NULL )
1296             {
1297                 o_value = [NSString stringWithCString: psz_temp];
1298             }
1299             free( psz_temp );
1300         }
1301     }
1302     else if( [[o_tc identifier] isEqualToString:@"3"] )
1303     {
1304         char psz_duration[MSTRTIME_MAX_SIZE];
1305         mtime_t dur = p_item->input.i_duration;
1306         if( dur != -1 )
1307         {
1308             secstotimestr( psz_duration, dur/1000000 );
1309             o_value = [NSString stringWithUTF8String: psz_duration];
1310         }
1311         else
1312         {
1313             o_value = @"-:--:--";
1314         }
1315     }
1316     vlc_object_release( p_playlist );
1317
1318     return( o_value );
1319 }
1320
1321 /* Required for drag & drop and reordering */
1322 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
1323 {
1324     unsigned int i,j;
1325     playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1326                                                FIND_ANYWHERE );
1327     NSMutableArray *o_nodes_array = [NSMutableArray array];
1328     NSMutableArray *o_items_array = [NSMutableArray array];
1329     NSMutableArray *o_objects_array = [NSMutableArray array];
1330
1331     if( !p_playlist ) return NO;
1332
1333     [pboard declareTypes: [NSArray arrayWithObject:
1334                                     @"VLCPlaylistItemPboardType"] owner: nil];
1335
1336     for( i = 0 ; i < [items count] ; i++ )
1337     {
1338         id o_item = [items objectAtIndex: i];
1339
1340         if( ![self isItem: [o_item pointerValue] inNode:
1341                                         p_playlist->p_general] )
1342         {
1343             vlc_object_release(p_playlist);
1344             return NO;
1345         }
1346         if( ((playlist_item_t *)[o_item pointerValue])->i_children > 0 )
1347             [o_nodes_array addObject: o_item];
1348         else
1349             [o_items_array addObject: o_item];
1350     }
1351
1352     /* Now we need to check if there are selected items that are in already
1353        selected nodes. In that case, we only want to move the nodes */
1354     for( i = 0 ; i < [o_nodes_array count] ; i++ )
1355     {
1356         for ( j = 0 ; j < [o_nodes_array count] ; j++ )
1357         {
1358             if( j == i ) continue;
1359             if( [self isItem: [[o_nodes_array objectAtIndex:i] pointerValue]
1360                     inNode: [[o_nodes_array objectAtIndex:j] pointerValue]] )
1361             {
1362                 [o_nodes_array removeObjectAtIndex:i];
1363                 /* We need to execute the next iteration with the same index
1364                    since the current item has been deleted */
1365                 i--;
1366                 break;
1367             }
1368         }
1369     }
1370
1371     for( i = 0 ; i < [o_items_array count] ; i++ )
1372     {
1373         for ( j = 0 ; j < [o_nodes_array count] ; j++ )
1374         {
1375             if( [self isItem: [[o_items_array objectAtIndex:i] pointerValue]
1376                     inNode: [[o_nodes_array objectAtIndex:j] pointerValue]] )
1377             {
1378                 [o_items_array removeObjectAtIndex:i];
1379                 i--;
1380                 break;
1381             }
1382             if( j == [o_nodes_array count] ) i++;
1383         }
1384     }
1385
1386     [o_objects_array addObjectsFromArray: o_nodes_array];
1387     [o_objects_array addObjectsFromArray: o_items_array];
1388
1389     if( ![pboard setPropertyList:(id)o_objects_array
1390                                         forType:@"VLCPlaylistItemPboardType"] )
1391     {
1392         vlc_object_release(p_playlist);
1393         return NO;
1394     }
1395
1396     vlc_object_release(p_playlist);
1397     return YES;
1398 }
1399
1400 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
1401 {
1402     NSPasteboard *o_pasteboard = [info draggingPasteboard];
1403
1404     if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1405     {
1406         return NSDragOperationGeneric;
1407     }
1408     return NSDragOperationNone;
1409 }
1410
1411 - (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(int)index
1412 {
1413     playlist_t * p_playlist =  vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1414                                                        FIND_ANYWHERE );
1415     NSPasteboard *o_pasteboard = [info draggingPasteboard];
1416
1417     if( !p_playlist ) return NO;
1418
1419     if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1420     {
1421         int i;
1422         playlist_item_t *p_node = [item pointerValue];
1423
1424         NSArray *o_array = [NSArray array];
1425         NSArray *o_values = [[o_pasteboard propertyListForType:
1426                                         NSFilenamesPboardType]
1427                                 sortedArrayUsingSelector:
1428                                         @selector(caseInsensitiveCompare:)];
1429
1430         for( i = 0; i < (int)[o_values count]; i++)
1431         {
1432             NSDictionary *o_dic;
1433             o_dic = [NSDictionary dictionaryWithObject:[o_values
1434                         objectAtIndex:i] forKey:@"ITEM_URL"];
1435             o_array = [o_array arrayByAddingObject: o_dic];
1436         }
1437
1438         if ( item == nil )
1439         {
1440             [self appendArray: o_array atPos: index enqueue: YES];
1441         }
1442         else if( p_node->i_children == -1 )
1443         {
1444             int i_counter;
1445             playlist_item_t *p_real_node = NULL;
1446
1447             for( i_counter = 0 ; i_counter < p_node->i_parents ; i_counter++ )
1448             {
1449                 if( p_node->pp_parents[i_counter]->i_view == i_current_view )
1450                 {
1451                     p_real_node = p_node->pp_parents[i_counter]->p_parent;
1452                     break;
1453                 }
1454                 if( i_counter == p_node->i_parents )
1455                 {
1456                     return NO;
1457                 }
1458             }
1459             [self appendNodeArray: o_array inNode: p_real_node
1460                 atPos: index inView: i_current_view enqueue: YES];
1461         }
1462         else
1463         {
1464             [self appendNodeArray: o_array inNode: p_node
1465                 atPos: index inView: i_current_view enqueue: YES];
1466         }
1467         vlc_object_release( p_playlist );
1468         return YES;
1469     }
1470     vlc_object_release( p_playlist );
1471     return NO;
1472 }
1473
1474 /* Delegate method of NSWindow */
1475 /*- (void)windowWillClose:(NSNotification *)aNotification
1476 {
1477     [o_btn_playlist setState: NSOffState];
1478 }
1479 */
1480 @end
1481
1482