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