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