]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlist.m
* backport of [11397] and [11396]
[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
357 - (BOOL)isItem: (playlist_item_t *)p_item inNode: (playlist_item_t *)p_node
358 {
359     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
360                                           FIND_ANYWHERE );
361     playlist_item_t *p_temp_item = p_item;
362
363     if( p_playlist == NULL )
364     {
365         return NO;
366     }
367
368     if ( p_temp_item )
369     {
370         while( p_temp_item->i_parents > 0 )
371         {
372             int i;
373             for( i = 0; i < p_temp_item->i_parents ; i++ )
374             {
375                 if( p_temp_item->pp_parents[i]->i_view == i_current_view )
376                 {
377                     if( p_temp_item->pp_parents[i]->p_parent == p_node )
378                     {
379                         vlc_object_release( p_playlist );
380                         return YES;
381                     }
382                     else
383                     {
384                         p_temp_item = p_temp_item->pp_parents[i]->p_parent;
385                         break;
386                     }
387                 }
388             }
389         }
390     }
391
392     vlc_object_release( p_playlist );
393     return NO;
394 }
395
396 - (IBAction)savePlaylist:(id)sender
397 {
398     intf_thread_t * p_intf = VLCIntf;
399     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
400                                                        FIND_ANYWHERE );
401
402     NSSavePanel *o_save_panel = [NSSavePanel savePanel];
403     NSString * o_name = [NSString stringWithFormat: @"%@.m3u", _NS("Untitled")];
404     [o_save_panel setTitle: _NS("Save Playlist")];
405     [o_save_panel setPrompt: _NS("Save")];
406
407     if( [o_save_panel runModalForDirectory: nil
408             file: o_name] == NSOKButton )
409     {
410         playlist_Export( p_playlist, [[o_save_panel filename] fileSystemRepresentation], "export-m3u" );
411     }
412 }
413
414
415 /* When called retrieves the selected outlineview row and plays that node or item */
416 - (IBAction)playItem:(id)sender
417 {
418     intf_thread_t * p_intf = VLCIntf;
419     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
420                                                        FIND_ANYWHERE );
421
422     if( p_playlist != NULL )
423     {
424         playlist_item_t *p_item;
425         playlist_item_t *p_node = NULL;
426         int i;
427
428         p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
429
430         if( p_item )
431         {
432             if( p_item->i_children == -1 )
433             {
434                 for( i = 0 ; i < p_item->i_parents ; i++ )
435                 {
436                     if( p_item->pp_parents[i]->i_view == i_current_view )
437                     {
438                         p_node = p_item->pp_parents[i]->p_parent;
439                     }
440                 }
441             }
442             else
443             {
444                 p_node = p_item;
445                 if( p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1 )
446                 {
447                     p_item = p_node->pp_children[0];
448                 }
449                 else
450                 {
451                     p_item = NULL;
452                 }
453             }
454             playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view, p_node, p_item );
455         }
456         vlc_object_release( p_playlist );
457     }
458 }
459
460 - (IBAction)servicesChange:(id)sender
461 {
462     NSMenuItem *o_mi = (NSMenuItem *)sender;
463     NSString *o_string = [o_mi representedObject];
464     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
465                                           FIND_ANYWHERE );
466     if( !playlist_IsServicesDiscoveryLoaded( p_playlist, [o_string cString] ) )
467         playlist_ServicesDiscoveryAdd( p_playlist, [o_string cString] );
468     else
469         playlist_ServicesDiscoveryRemove( p_playlist, [o_string cString] );
470
471     [o_mi setState: playlist_IsServicesDiscoveryLoaded( p_playlist,
472                                           [o_string cString] ) ? YES : NO];
473
474     i_current_view = VIEW_CATEGORY;
475     playlist_ViewUpdate( p_playlist, i_current_view );
476     vlc_object_release( p_playlist );
477     [self playlistUpdated];
478     return;
479 }
480
481 - (IBAction)selectAll:(id)sender
482 {
483     [o_outline_view selectAll: nil];
484 }
485
486 - (IBAction)deleteItem:(id)sender
487 {
488     int i, i_count, i_row;
489     NSMutableArray *o_to_delete;
490     NSNumber *o_number;
491
492     playlist_t * p_playlist;
493     intf_thread_t * p_intf = VLCIntf;
494
495     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
496                                           FIND_ANYWHERE );
497
498     if ( p_playlist == NULL )
499     {
500         return;
501     }
502     o_to_delete = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
503     i_count = [o_to_delete count];
504
505     for( i = 0; i < i_count; i++ )
506     {
507         playlist_item_t * p_item;
508         o_number = [o_to_delete lastObject];
509         i_row = [o_number intValue];
510
511         [o_to_delete removeObject: o_number];
512         [o_outline_view deselectRow: i_row];
513
514         p_item = (playlist_item_t *)[[o_outline_view itemAtRow: i_row] pointerValue];
515
516         if( p_item->i_children > -1 ) //is a node and not an item
517         {
518             if( p_playlist->status.i_status != PLAYLIST_STOPPED &&
519                 [self isItem: p_playlist->status.p_item inNode: p_item] == YES )
520             {
521                 // if current item is in selected node and is playing then stop playlist
522                 playlist_Stop( p_playlist );
523             }
524             playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
525         }
526         else
527         {
528             if( p_playlist->status.i_status != PLAYLIST_STOPPED &&
529                 p_playlist->status.p_item == [[o_outline_view itemAtRow: i_row] pointerValue] )
530             {
531                 playlist_Stop( p_playlist );
532             }
533             playlist_LockDelete( p_playlist, p_item->input.i_id );
534         }
535     }
536     [self playlistUpdated];
537     vlc_object_release( p_playlist );
538 }
539
540 - (IBAction)sortNodeByName:(id)sender
541 {
542     [self sortNode: SORT_TITLE];
543 }
544
545 - (IBAction)sortNodeByAuthor:(id)sender
546 {
547     [self sortNode: SORT_AUTHOR];
548 }
549
550 - (void)sortNode:(int)i_mode
551 {
552     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
553                                           FIND_ANYWHERE );
554     playlist_item_t * p_item;
555
556     if (p_playlist == NULL)
557     {
558         return;
559     }
560
561     if( [o_outline_view selectedRow] > -1 )
562     {
563         p_item = [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
564                                                                 pointerValue];
565     }
566     else
567     /*If no item is selected, sort the whole playlist*/
568     {
569         playlist_view_t * p_view = playlist_ViewFind( p_playlist, i_current_view );
570         p_item = p_view->p_root;
571     }
572
573     if( p_item->i_children > -1 ) // the item is a node
574     {
575         vlc_mutex_lock( &p_playlist->object_lock );
576         playlist_RecursiveNodeSort( p_playlist, p_item, i_mode, ORDER_NORMAL );
577         vlc_mutex_unlock( &p_playlist->object_lock );
578     }
579     else
580     {
581         int i;
582
583         for( i = 0 ; i < p_item->i_parents ; i++ )
584         {
585             if( p_item->pp_parents[i]->i_view == i_current_view )
586             {
587                 vlc_mutex_lock( &p_playlist->object_lock );
588                 playlist_RecursiveNodeSort( p_playlist,
589                         p_item->pp_parents[i]->p_parent, i_mode, ORDER_NORMAL );
590                 vlc_mutex_unlock( &p_playlist->object_lock );
591                 break;
592             }
593         }
594     }
595     vlc_object_release( p_playlist );
596     [self playlistUpdated];
597 }
598
599 - (playlist_item_t *)createItem:(NSDictionary *)o_one_item
600 {
601     intf_thread_t * p_intf = VLCIntf;
602     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
603                                                        FIND_ANYWHERE );
604
605     if( p_playlist == NULL )
606     {
607         return NULL;
608     }
609     playlist_item_t *p_item;
610     int i;
611     BOOL b_rem = FALSE, b_dir = FALSE;
612     NSString *o_uri, *o_name;
613     NSArray *o_options;
614     NSURL *o_true_file;
615
616     /* Get the item */
617     o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
618     o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
619     o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
620
621     /* Find the name for a disc entry ( i know, can you believe the trouble?) */
622     if( ( !o_name || [o_name isEqualToString:@""] ) && [o_uri rangeOfString: @"/dev/"].location != NSNotFound )
623     {
624         int i_count, i_index;
625         struct statfs *mounts = NULL;
626
627         i_count = getmntinfo (&mounts, MNT_NOWAIT);
628         /* getmntinfo returns a pointer to static data. Do not free. */
629         for( i_index = 0 ; i_index < i_count; i_index++ )
630         {
631             NSMutableString *o_temp, *o_temp2;
632             o_temp = [NSMutableString stringWithString: o_uri];
633             o_temp2 = [NSMutableString stringWithCString: mounts[i_index].f_mntfromname];
634             [o_temp replaceOccurrencesOfString: @"/dev/rdisk" withString: @"/dev/disk" options:NULL range:NSMakeRange(0, [o_temp length]) ];
635             [o_temp2 replaceOccurrencesOfString: @"s0" withString: @"" options:NULL range:NSMakeRange(0, [o_temp2 length]) ];
636             [o_temp2 replaceOccurrencesOfString: @"s1" withString: @"" options:NULL range:NSMakeRange(0, [o_temp2 length]) ];
637
638             if( strstr( [o_temp fileSystemRepresentation], [o_temp2 fileSystemRepresentation] ) != NULL )
639             {
640                 o_name = [[NSFileManager defaultManager] displayNameAtPath: [NSString stringWithCString:mounts[i_index].f_mntonname]];
641             }
642         }
643     }
644     /* If no name, then make a guess */
645     if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
646
647     if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
648         [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
649                 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem   )
650     {
651         /* All of this is to make sure CD's play when you D&D them on VLC */
652         /* Converts mountpoint to a /dev file */
653         struct statfs *buf;
654         char *psz_dev;
655         NSMutableString *o_temp;
656
657         buf = (struct statfs *) malloc (sizeof(struct statfs));
658         statfs( [o_uri fileSystemRepresentation], buf );
659         psz_dev = strdup(buf->f_mntfromname);
660         o_temp = [NSMutableString stringWithCString: psz_dev ];
661         [o_temp replaceOccurrencesOfString: @"/dev/disk" withString: @"/dev/rdisk" options:NULL range:NSMakeRange(0, [o_temp length]) ];
662         [o_temp replaceOccurrencesOfString: @"s0" withString: @"" options:NULL range:NSMakeRange(0, [o_temp length]) ];
663         [o_temp replaceOccurrencesOfString: @"s1" withString: @"" options:NULL range:NSMakeRange(0, [o_temp length]) ];
664         o_uri = o_temp;
665     }
666
667     p_item = playlist_ItemNew( p_intf, [o_uri fileSystemRepresentation], [o_name UTF8String] );
668     if( !p_item )
669        return NULL;
670
671     if( o_options )
672     {
673         for( i = 0; i < (int)[o_options count]; i++ )
674         {
675             playlist_ItemAddOption( p_item, strdup( [[o_options objectAtIndex:i] UTF8String] ) );
676         }
677     }
678
679     /* Recent documents menu */
680     o_true_file = [NSURL fileURLWithPath: o_uri];
681     if( o_true_file != nil )
682     {
683         [[NSDocumentController sharedDocumentController]
684             noteNewRecentDocumentURL: o_true_file];
685     }
686
687     vlc_object_release( p_playlist );
688     return p_item;
689 }
690
691 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
692 {
693     int i_item;
694     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
695                                             FIND_ANYWHERE );
696     if( p_playlist == NULL )
697     {
698         return;
699     }
700
701     for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
702     {
703         playlist_item_t *p_item;
704         NSDictionary *o_one_item;
705
706         /* Get the item */
707         o_one_item = [o_array objectAtIndex: i_item];
708         p_item = [self createItem: o_one_item];
709         if( !p_item )
710         {
711             continue;
712         }
713
714         /* Add the item */
715         playlist_AddItem( p_playlist, p_item, PLAYLIST_APPEND, i_position == -1 ? PLAYLIST_END : i_position + i_item );
716
717         if( i_item == 0 && !b_enqueue )
718         {
719             playlist_Control( p_playlist, PLAYLIST_ITEMPLAY, p_item );
720         }
721     }
722     vlc_object_release( p_playlist );
723 }
724
725 - (void)appendNodeArray:(NSArray*)o_array inNode:(playlist_item_t *)p_node atPos:(int)i_position inView:(int)i_view enqueue:(BOOL)b_enqueue
726 {
727     int i_item;
728     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
729                                             FIND_ANYWHERE );
730     if( p_playlist == NULL )
731     {
732         return;
733     }
734
735     for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
736     {
737         playlist_item_t *p_item;
738         NSDictionary *o_one_item;
739
740         /* Get the item */
741         o_one_item = [o_array objectAtIndex: i_item];
742         p_item = [self createItem: o_one_item];
743         if( !p_item )
744         {
745             continue;
746         }
747
748         /* Add the item */
749         playlist_NodeAddItem( p_playlist, p_item, i_view, p_node, PLAYLIST_APPEND, i_position + i_item );
750
751         if( i_item == 0 && !b_enqueue )
752         {
753             playlist_Control( p_playlist, PLAYLIST_ITEMPLAY, p_item );
754         }
755     }
756     vlc_object_release( p_playlist );
757
758 }
759
760 - (IBAction)handlePopUp:(id)sender
761
762 {
763     intf_thread_t * p_intf = VLCIntf;
764     vlc_value_t val1,val2;
765     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
766                                             FIND_ANYWHERE );
767     if( p_playlist == NULL )
768     {
769         return;
770     }
771
772     switch( [o_loop_popup indexOfSelectedItem] )
773     {
774         case 1:
775
776              val1.b_bool = 0;
777              var_Set( p_playlist, "loop", val1 );
778              val1.b_bool = 1;
779              var_Set( p_playlist, "repeat", val1 );
780              vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat One" ) );
781         break;
782
783         case 2:
784              val1.b_bool = 0;
785              var_Set( p_playlist, "repeat", val1 );
786              val1.b_bool = 1;
787              var_Set( p_playlist, "loop", val1 );
788              vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat All" ) );
789         break;
790
791         default:
792              var_Get( p_playlist, "repeat", &val1 );
793              var_Get( p_playlist, "loop", &val2 );
794              if( val1.b_bool || val2.b_bool )
795              {
796                   val1.b_bool = 0;
797                   var_Set( p_playlist, "repeat", val1 );
798                   var_Set( p_playlist, "loop", val1 );
799                   vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
800              }
801          break;
802      }
803      vlc_object_release( p_playlist );
804      [self playlistUpdated];
805 }
806
807 - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
808 {
809     playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
810                                                        FIND_ANYWHERE );
811     playlist_item_t *p_selected_item;
812     int i_current, i_selected_row;
813
814     if( !p_playlist )
815         return NULL;
816
817     i_selected_row = [o_outline_view selectedRow];
818     if (i_selected_row < 0)
819         i_selected_row = 0;
820
821     p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow:
822                                             i_selected_row] pointerValue];
823
824     for( i_current = 0; i_current < p_item->i_children ; i_current++ )
825     {
826         char *psz_temp;
827         NSString *o_current_name, *o_current_author;
828
829         vlc_mutex_lock( &p_playlist->object_lock );
830         o_current_name = [NSString stringWithUTF8String:
831             p_item->pp_children[i_current]->input.psz_name];
832         psz_temp = vlc_input_item_GetInfo( &p_item->input ,
833                                    _("Meta-information"),_("Artist") );
834         o_current_author = [NSString stringWithUTF8String: psz_temp];
835         free( psz_temp);
836         vlc_mutex_unlock( &p_playlist->object_lock );
837
838         if( p_selected_item == p_item->pp_children[i_current] &&
839                     b_selected_item_met == NO )
840         {
841             b_selected_item_met = YES;
842         }
843         else if( p_selected_item == p_item->pp_children[i_current] &&
844                     b_selected_item_met == YES )
845         {
846             vlc_object_release( p_playlist );
847             return NULL;
848         }
849         else if( b_selected_item_met == YES &&
850                     ( [o_current_name rangeOfString:[o_search_field
851                         stringValue] options:NSCaseInsensitiveSearch ].length ||
852                       [o_current_author rangeOfString:[o_search_field
853                         stringValue] options:NSCaseInsensitiveSearch ].length ) )
854         {
855             vlc_object_release( p_playlist );
856             /*Adds the parent items in the result array as well, so that we can
857             expand the tree*/
858             return [NSMutableArray arrayWithObject: [NSValue
859                             valueWithPointer: p_item->pp_children[i_current]]];
860         }
861         if( p_item->pp_children[i_current]->i_children > 0 )
862         {
863             id o_result = [self subSearchItem:
864                                             p_item->pp_children[i_current]];
865             if( o_result != NULL )
866             {
867                 vlc_object_release( p_playlist );
868                 [o_result insertObject: [NSValue valueWithPointer:
869                                 p_item->pp_children[i_current]] atIndex:0];
870                 return o_result;
871             }
872         }
873     }
874     vlc_object_release( p_playlist );
875     return NULL;
876 }
877
878 - (IBAction)searchItem:(id)sender
879 {
880     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
881                                                        FIND_ANYWHERE );
882     playlist_view_t * p_view;
883     id o_result;
884
885     unsigned int i;
886     int i_row = -1;
887
888     b_selected_item_met = NO;
889
890     if( p_playlist == NULL )
891         return;
892     p_view = playlist_ViewFind( p_playlist, i_current_view );
893
894     if( p_view )
895     {
896         /*First, only search after the selected item:*
897          *(b_selected_item_met = NO)                 */
898         o_result = [self subSearchItem:p_view->p_root];
899         if( o_result == NULL )
900         {
901             /* If the first search failed, search again from the beginning */
902             o_result = [self subSearchItem:p_view->p_root];
903         }
904         if( o_result != NULL )
905         {
906             for( i = 0 ; i < [o_result count] - 1 ; i++ )
907             {
908                 [o_outline_view expandItem: [o_outline_dict objectForKey:
909                             [NSString stringWithFormat: @"%p",
910                             [[o_result objectAtIndex: i] pointerValue]]]];
911             }
912             i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
913                             [NSString stringWithFormat: @"%p",
914                             [[o_result objectAtIndex: [o_result count] - 1 ]
915                             pointerValue]]]];
916         }
917         if( i_row > -1 )
918         {
919             [o_outline_view selectRow:i_row byExtendingSelection: NO];
920             [o_outline_view scrollRowToVisible: i_row];
921         }
922     }
923     vlc_object_release( p_playlist );
924 }
925
926 - (NSMenu *)menuForEvent:(NSEvent *)o_event
927 {
928     NSPoint pt;
929     vlc_bool_t b_rows;
930     vlc_bool_t b_item_sel;
931
932     pt = [o_outline_view convertPoint: [o_event locationInWindow]
933                                                  fromView: nil];
934     b_item_sel = ( [o_outline_view rowAtPoint: pt] != -1 &&
935                    [o_outline_view selectedRow] != -1 );
936     b_rows = [o_outline_view numberOfRows] != 0;
937
938     [o_mi_play setEnabled: b_item_sel];
939     [o_mi_delete setEnabled: b_item_sel];
940     [o_mi_selectall setEnabled: b_rows];
941     [o_mi_info setEnabled: b_item_sel];
942
943     return( o_ctx_menu );
944 }
945
946 - (playlist_item_t *)selectedPlaylistItem
947 {
948     return [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
949                                                                 pointerValue];
950 }
951
952 - (void)outlineView: (NSTableView*)o_tv
953                   didClickTableColumn:(NSTableColumn *)o_tc
954 {
955     int i_mode = 0, i_type;
956     intf_thread_t *p_intf = VLCIntf;
957     playlist_view_t *p_view;
958
959     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
960                                        FIND_ANYWHERE );
961     if( p_playlist == NULL )
962     {
963         return;
964     }
965     
966     /* Check whether the selected table column header corresponds to a
967        sortable table column*/
968     if( !( o_tc == o_tc_name || o_tc == o_tc_author ) )
969     {
970         vlc_object_release( p_playlist );
971         return;
972     }
973
974     p_view = playlist_ViewFind( p_playlist, i_current_view );
975
976     if( o_tc_sortColumn == o_tc )
977     {
978         b_isSortDescending = !b_isSortDescending;
979     }
980     else
981     {
982         b_isSortDescending = VLC_FALSE;
983     }
984
985     if( o_tc == o_tc_name )
986     {
987         i_mode = SORT_TITLE;
988     }
989     else if( o_tc == o_tc_author )
990     {
991         i_mode = SORT_AUTHOR;
992     }
993
994     if( b_isSortDescending )
995     {
996         i_type = ORDER_REVERSE;
997     }
998     else
999     {
1000         i_type = ORDER_NORMAL;
1001     }
1002
1003     vlc_mutex_lock( &p_playlist->object_lock );
1004     playlist_RecursiveNodeSort( p_playlist, p_view->p_root, i_mode, i_type );
1005     vlc_mutex_unlock( &p_playlist->object_lock );
1006
1007     vlc_object_release( p_playlist );
1008     [self playlistUpdated];
1009
1010     o_tc_sortColumn = o_tc;
1011     [o_outline_view setHighlightedTableColumn:o_tc];
1012
1013     if( b_isSortDescending )
1014     {
1015         [o_outline_view setIndicatorImage:o_descendingSortingImage
1016                                                         inTableColumn:o_tc];
1017     }
1018     else
1019     {
1020         [o_outline_view setIndicatorImage:o_ascendingSortingImage
1021                                                         inTableColumn:o_tc];
1022     }
1023 }
1024
1025
1026 - (void)outlineView:(NSOutlineView *)outlineView
1027                                 willDisplayCell:(id)cell
1028                                 forTableColumn:(NSTableColumn *)tableColumn
1029                                 item:(id)item
1030 {
1031     playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1032                                           FIND_ANYWHERE );
1033     playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
1034
1035     if( !p_playlist ) return;
1036
1037     if( ( p_item == p_playlist->status.p_item ) ||
1038             ( p_item->i_children != 0 &&
1039             [self isItem: p_playlist->status.p_item inNode: p_item] ) )
1040     {
1041         [cell setFont: [NSFont boldSystemFontOfSize: 0]];
1042     }
1043     else
1044     {
1045         [cell setFont: [NSFont systemFontOfSize: 0]];
1046     }
1047     vlc_object_release( p_playlist );
1048 }
1049
1050 @end
1051
1052 @implementation VLCPlaylist (NSOutlineViewDataSource)
1053
1054 /* return the number of children for Obj-C pointer item */ /* DONE */
1055 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
1056 {
1057     int i_return = 0;
1058     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1059                                                        FIND_ANYWHERE );
1060     if( p_playlist == NULL || outlineView != o_outline_view )
1061         return 0;
1062
1063     if( item == nil )
1064     {
1065         /* root object */
1066         playlist_view_t *p_view;
1067         p_view = playlist_ViewFind( p_playlist, i_current_view );
1068         if( p_view && p_view->p_root )
1069             i_return = p_view->p_root->i_children;
1070     }
1071     else
1072     {
1073         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
1074         if( p_item )
1075             i_return = p_item->i_children;
1076     }
1077     vlc_object_release( p_playlist );
1078     
1079     if( i_return <= 0 )
1080         i_return = 0;
1081     
1082     return i_return;
1083 }
1084
1085 /* return the child at index for the Obj-C pointer item */ /* DONE */
1086 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
1087 {
1088     playlist_item_t *p_return = NULL;
1089     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1090                                                        FIND_ANYWHERE );
1091     NSValue *o_value;
1092
1093     if( p_playlist == NULL )
1094         return nil;
1095
1096     if( item == nil )
1097     {
1098         /* root object */
1099         playlist_view_t *p_view;
1100         p_view = playlist_ViewFind( p_playlist, i_current_view );
1101         if( p_view && index < p_view->p_root->i_children && index >= 0 )
1102             p_return = p_view->p_root->pp_children[index];
1103     }
1104     else
1105     {
1106         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
1107         if( p_item && index < p_item->i_children && index >= 0 )
1108             p_return = p_item->pp_children[index];
1109     }
1110     
1111     if( p_playlist->i_size >= 2 )
1112     {
1113         [o_status_field setStringValue: [NSString stringWithFormat:
1114                     _NS("%i items in playlist"), p_playlist->i_size]];
1115     }
1116     else
1117     {
1118         if( p_playlist->i_size == 0 )
1119         {
1120             [o_status_field setStringValue: [NSString stringWithFormat:
1121                     _NS("no items in playlist"), p_playlist->i_size]];
1122         }
1123         else
1124         {
1125             [o_status_field setStringValue: [NSString stringWithFormat:
1126                     _NS("1 item in playlist"), p_playlist->i_size]];
1127         }
1128     }
1129
1130     vlc_object_release( p_playlist );
1131
1132     o_value = [[NSValue valueWithPointer: p_return] retain];
1133
1134     [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p", p_return]];
1135     return o_value;
1136 }
1137
1138 /* is the item expandable */
1139 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
1140 {
1141     int i_return = 0;
1142     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1143                                                        FIND_ANYWHERE );
1144     if( p_playlist == NULL )
1145         return NO;
1146
1147     if( item == nil )
1148     {
1149         /* root object */
1150         playlist_view_t *p_view;
1151         p_view = playlist_ViewFind( p_playlist, i_current_view );
1152         if( p_view && p_view->p_root )
1153             i_return = p_view->p_root->i_children;
1154     }
1155     else
1156     {
1157         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
1158         if( p_item )
1159             i_return = p_item->i_children;
1160     }
1161     vlc_object_release( p_playlist );
1162
1163     if( i_return <= 0 )
1164         return NO;
1165     else
1166         return YES;
1167 }
1168
1169 /* retrieve the string values for the cells */
1170 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
1171 {
1172     id o_value = nil;
1173     intf_thread_t *p_intf = VLCIntf;
1174     playlist_t *p_playlist;
1175     playlist_item_t *p_item;
1176     
1177     if( item == nil || ![item isKindOfClass: [NSValue class]] ) return( @"error" );
1178     
1179     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1180                                                FIND_ANYWHERE );
1181     if( p_playlist == NULL )
1182     {
1183         return( @"error" );
1184     }
1185
1186     p_item = (playlist_item_t *)[item pointerValue];
1187
1188     if( p_item == NULL )
1189     {
1190         vlc_object_release( p_playlist );
1191         return( @"error");
1192     }
1193
1194     if( [[o_tc identifier] isEqualToString:@"1"] )
1195     {
1196         o_value = [NSString stringWithUTF8String:
1197             p_item->input.psz_name];
1198         if( o_value == NULL )
1199             o_value = [NSString stringWithCString:
1200                 p_item->input.psz_name];
1201     }
1202     else if( [[o_tc identifier] isEqualToString:@"2"] )
1203     {
1204         char *psz_temp;
1205         psz_temp = vlc_input_item_GetInfo( &p_item->input ,_("Meta-information"),_("Artist") );
1206
1207         if( psz_temp == NULL )
1208             o_value = @"";
1209         else
1210         {
1211             o_value = [NSString stringWithUTF8String: psz_temp];
1212             if( o_value == NULL )
1213             {
1214                 o_value = [NSString stringWithCString: psz_temp];
1215             }
1216             free( psz_temp );
1217         }
1218     }
1219     else if( [[o_tc identifier] isEqualToString:@"3"] )
1220     {
1221         char psz_duration[MSTRTIME_MAX_SIZE];
1222         mtime_t dur = p_item->input.i_duration;
1223         if( dur != -1 )
1224         {
1225             secstotimestr( psz_duration, dur/1000000 );
1226             o_value = [NSString stringWithUTF8String: psz_duration];
1227         }
1228         else
1229         {
1230             o_value = @"-:--:--";
1231         }
1232     }
1233     vlc_object_release( p_playlist );
1234
1235     return( o_value );
1236 }
1237
1238 /* Required for drag & drop and reordering */
1239 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
1240 {
1241 /*    unsigned int i;
1242
1243     for( i = 0 ; i < [items count] ; i++ )
1244     {
1245         if( [outlineView levelForItem: [items objectAtIndex: i]] == 0 )
1246         {
1247             return NO;
1248         }
1249     }*/
1250     return NO;
1251 }
1252
1253 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
1254 {
1255     NSPasteboard *o_pasteboard = [info draggingPasteboard];
1256
1257     if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1258     {
1259         return NSDragOperationGeneric;
1260     }
1261     return NSDragOperationNone;
1262 }
1263
1264 - (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(int)index
1265 {
1266     playlist_t * p_playlist =  vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
1267                                                        FIND_ANYWHERE );
1268     NSPasteboard *o_pasteboard = [info draggingPasteboard];
1269
1270     if( !p_playlist ) return NO;
1271
1272     if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1273     {
1274         int i;
1275         playlist_item_t *p_node = [item pointerValue];
1276
1277         NSArray *o_array = [NSArray array];
1278         NSArray *o_values = [[o_pasteboard propertyListForType:
1279                                         NSFilenamesPboardType]
1280                                 sortedArrayUsingSelector:
1281                                         @selector(caseInsensitiveCompare:)];
1282
1283         for( i = 0; i < (int)[o_values count]; i++)
1284         {
1285             NSDictionary *o_dic;
1286             o_dic = [NSDictionary dictionaryWithObject:[o_values
1287                         objectAtIndex:i] forKey:@"ITEM_URL"];
1288             o_array = [o_array arrayByAddingObject: o_dic];
1289         }
1290
1291         if ( item == nil )
1292         {
1293             [self appendArray: o_array atPos: index enqueue: YES];
1294         }
1295         else if( p_node->i_children == -1 )
1296         {
1297             int i_counter;
1298             playlist_item_t *p_real_node = NULL;
1299
1300             for( i_counter = 0 ; i_counter < p_node->i_parents ; i_counter++ )
1301             {
1302                 if( p_node->pp_parents[i_counter]->i_view == i_current_view )
1303                 {
1304                     p_real_node = p_node->pp_parents[i_counter]->p_parent;
1305                     break;
1306                 }
1307                 if( i_counter == p_node->i_parents )
1308                 {
1309                     return NO;
1310                 }
1311             }
1312             [self appendNodeArray: o_array inNode: p_real_node
1313                 atPos: index inView: i_current_view enqueue: YES];
1314         }
1315         else
1316         {
1317             [self appendNodeArray: o_array inNode: p_node
1318                 atPos: index inView: i_current_view enqueue: YES];
1319         }
1320         vlc_object_release( p_playlist );
1321         return YES;
1322     }
1323     vlc_object_release( p_playlist );
1324     return NO;
1325 }
1326
1327 /* Delegate method of NSWindow */
1328 /*- (void)windowWillClose:(NSNotification *)aNotification
1329 {
1330     [o_btn_playlist setState: NSOffState];
1331 }
1332 */
1333 @end
1334
1335