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