]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlist.m
* Finally fixed the issue with resizing the controller and the broken playlist view...
[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             o_lmi = [[o_mi_services submenu] addItemWithTitle:
157                      [NSString stringWithCString:
158                      p_parser->psz_longname ? p_parser->psz_longname :
159                      ( p_parser->psz_shortname ? p_parser->psz_shortname:
160                      p_parser->psz_object_name)]
161                                              action: @selector(servicesChange:)
162                                              keyEquivalent: @""];
163             [o_lmi setTarget: self];
164             [o_lmi setRepresentedObject:
165                    [NSString stringWithCString: p_parser->psz_object_name]];
166             if( playlist_IsServicesDiscoveryLoaded( p_playlist,
167                     p_parser->psz_object_name ) )
168                 [o_lmi setState: NSOnState];
169         }
170     }
171     vlc_list_release( p_list );
172     vlc_object_release( p_playlist );
173
174     [self initStrings];
175     //[self playlistUpdated];
176 }
177
178 - (void)initStrings
179 {
180     [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
181     [o_mi_play setTitle: _NS("Play")];
182     [o_mi_delete setTitle: _NS("Delete")];
183     [o_mi_selectall setTitle: _NS("Select All")];
184     [o_mi_info setTitle: _NS("Properties")];
185     [o_mi_sort_name setTitle: _NS("Sort Node by Name")];
186     [o_mi_sort_author setTitle: _NS("Sort Node by Author")];
187     [o_mi_services setTitle: _NS("Services discovery")];
188     [[o_tc_name headerCell] setStringValue:_NS("Name")];
189     [[o_tc_author headerCell] setStringValue:_NS("Author")];
190     [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
191     [o_status_field setStringValue: [NSString stringWithFormat:
192                         _NS("no items in playlist")]];
193
194     [o_random_ckb setTitle: _NS("Random")];
195 #if 0
196     [o_search_button setTitle: _NS("Search")];
197 #endif
198     [[o_loop_popup itemAtIndex:0] setTitle: _NS("Standard Play")];
199     [[o_loop_popup itemAtIndex:1] setTitle: _NS("Repeat One")];
200     [[o_loop_popup itemAtIndex:2] setTitle: _NS("Repeat All")];
201 }
202
203 - (NSOutlineView *)outlineView
204 {
205     return o_outline_view;
206 }
207
208 - (void)playlistUpdated
209 {
210     unsigned int i;
211
212     /* Clear indications of any existing column sorting*/
213     for( i = 0 ; i < [[o_outline_view tableColumns] count] ; i++ )
214     {
215         [o_outline_view setIndicatorImage:nil inTableColumn:
216                             [[o_outline_view tableColumns] objectAtIndex:i]];
217     }
218
219     [o_outline_view setHighlightedTableColumn:nil];
220     o_tc_sortColumn = nil;
221     // TODO Find a way to keep the dict size to a minimum
222     //[o_outline_dict removeAllObjects];
223     [o_outline_view reloadData];
224 }
225
226 - (void)updateRowSelection
227 {
228     int i,i_row;
229     unsigned int j;
230     
231     playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
232                                           FIND_ANYWHERE );
233     playlist_item_t *p_item, *p_temp_item;
234     NSMutableArray *o_array = [NSMutableArray array];
235
236     if( p_playlist == NULL )
237         return;
238
239     p_item = p_playlist->status.p_item;
240     p_temp_item = p_item;
241
242     while( p_temp_item->i_parents > 0 )
243     {
244         [o_array insertObject: [NSValue valueWithPointer: p_temp_item] atIndex: 0];
245         for (i = 0 ; i < p_temp_item->i_parents ; i++)
246         {
247             if( p_temp_item->pp_parents[i]->i_view == i_current_view )
248             {
249                 p_temp_item = p_temp_item->pp_parents[i]->p_parent;
250                 break;
251             }
252         }
253     }
254
255     for (j = 0 ; j < [o_array count] - 1 ; j++)
256     {
257         [o_outline_view expandItem: [o_outline_dict objectForKey:
258                             [NSString stringWithFormat: @"%p",
259                             [[o_array objectAtIndex:j] pointerValue]]]];
260
261     }
262
263     i_row = [o_outline_view rowForItem:[o_outline_dict
264             objectForKey:[NSString stringWithFormat: @"%p", p_item]]];
265
266     [o_outline_view selectRow: i_row byExtendingSelection: NO];
267     [o_outline_view scrollRowToVisible: i_row];
268
269     vlc_object_release(p_playlist);
270 }
271
272
273 - (BOOL)isItem: (playlist_item_t *)p_item inNode: (playlist_item_t *)p_node
274 {
275     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
276                                           FIND_ANYWHERE );
277     playlist_item_t *p_temp_item = p_item;
278
279     if( p_playlist == NULL )
280     {
281         return NO;
282     }
283
284     while( p_temp_item->i_parents > 0 )
285     {
286         int i;
287         for( i = 0; i < p_temp_item->i_parents ; i++ )
288         {
289             if( p_temp_item->pp_parents[i]->i_view == i_current_view )
290             {
291                 if( p_temp_item->pp_parents[i]->p_parent == p_node )
292                 {
293                     vlc_object_release( p_playlist );
294                     return YES;
295                 }
296                 else
297                 {
298                     p_temp_item = p_temp_item->pp_parents[i]->p_parent;
299                     break;
300                 }
301             }
302         }
303     }
304
305     vlc_object_release( p_playlist );
306     return NO;
307 }
308
309
310 /* When called retrieves the selected outlineview row and plays that node or item */
311 - (IBAction)playItem:(id)sender
312 {
313     intf_thread_t * p_intf = VLCIntf;
314     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
315                                                        FIND_ANYWHERE );
316
317     if( p_playlist != NULL )
318     {
319         playlist_item_t *p_item;
320         playlist_item_t *p_node = NULL;
321         int i;
322
323         p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
324
325         if( p_item )
326         {
327             if( p_item->i_children == -1 )
328             {
329                 for( i = 0 ; i < p_item->i_parents ; i++ )
330                 {
331                     if( p_item->pp_parents[i]->i_view == i_current_view )
332                     {
333                         p_node = p_item->pp_parents[i]->p_parent;
334                     }
335                 }
336             }
337             else
338             {
339                 p_node = p_item;
340                 if( p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1 )
341                 {
342                     p_item = p_node->pp_children[0];
343                 }
344                 else
345                 {
346                     p_item = NULL;
347                 }
348             }
349
350             playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view, p_node, p_item );
351         }
352         vlc_object_release( p_playlist );
353     }
354 }
355
356 - (IBAction)servicesChange:(id)sender
357 {
358     NSMenuItem *o_mi = (NSMenuItem *)sender;
359     NSString *o_string = [o_mi representedObject];
360     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
361                                           FIND_ANYWHERE );
362     if( !playlist_IsServicesDiscoveryLoaded( p_playlist, [o_string cString] ) )
363         playlist_ServicesDiscoveryAdd( p_playlist, [o_string cString] );
364     else
365         playlist_ServicesDiscoveryRemove( p_playlist, [o_string cString] );
366
367     [o_mi setState: playlist_IsServicesDiscoveryLoaded( p_playlist,
368                                           [o_string cString] ) ? YES : NO];
369     [self playlistUpdated];
370     return;
371 }
372
373 - (IBAction)selectAll:(id)sender
374 {
375     [o_outline_view selectAll: nil];
376 }
377
378 - (IBAction)deleteItem:(id)sender
379 {
380     int i, i_count, i_row;
381     NSMutableArray *o_to_delete;
382     NSNumber *o_number;
383
384     playlist_t * p_playlist;
385     intf_thread_t * p_intf = VLCIntf;
386
387     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
388                                           FIND_ANYWHERE );
389
390     if ( p_playlist == NULL )
391     {
392         return;
393     }
394     o_to_delete = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
395     i_count = [o_to_delete count];
396
397     for( i = 0; i < i_count; i++ )
398     {
399         playlist_item_t * p_item;
400         o_number = [o_to_delete lastObject];
401         i_row = [o_number intValue];
402
403         [o_to_delete removeObject: o_number];
404         [o_outline_view deselectRow: i_row];
405
406         p_item = (playlist_item_t *)[[o_outline_view itemAtRow: i_row] pointerValue];
407
408         if( p_item->i_children > -1 ) //is a node and not an item
409         {
410             if( p_playlist->status.i_status != PLAYLIST_STOPPED &&
411                 [self isItem: p_playlist->status.p_item inNode: p_item] == YES )
412             {
413                 // if current item is in selected node and is playing then stop playlist
414                 playlist_Stop( p_playlist );
415             }
416             playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
417         }
418         else
419         {
420             if( p_playlist->status.i_status != PLAYLIST_STOPPED &&
421                 p_playlist->status.p_item == [[o_outline_view itemAtRow: i_row] pointerValue] )
422             {
423                 playlist_Stop( p_playlist );
424             }
425             playlist_LockDelete( p_playlist, p_item->input.i_id );
426         }
427     }
428     [self playlistUpdated];
429     vlc_object_release( p_playlist );
430 }
431
432 - (IBAction)sortNodeByName:(id)sender
433 {
434     [self sortNode: SORT_TITLE];
435 }
436
437 - (IBAction)sortNodeByAuthor:(id)sender
438 {
439     [self sortNode: SORT_AUTHOR];
440 }
441
442 - (void)sortNode:(int)i_mode
443 {
444     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
445                                           FIND_ANYWHERE );
446     playlist_item_t * p_item;
447
448     if (p_playlist == NULL)
449     {
450         return;
451     }
452
453     if( [o_outline_view selectedRow] > -1 )
454     {
455         p_item = [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
456                                                                 pointerValue];
457     }
458     else
459     /*If no item is selected, sort the whole playlist*/
460     {
461         playlist_view_t * p_view = playlist_ViewFind( p_playlist, i_current_view );
462         p_item = p_view->p_root;
463     }
464
465     if( p_item->i_children > -1 ) // the item is a node
466     {
467         vlc_mutex_lock( &p_playlist->object_lock );
468         playlist_RecursiveNodeSort( p_playlist, p_item, i_mode, ORDER_NORMAL );
469         vlc_mutex_unlock( &p_playlist->object_lock );
470     }
471     else
472     {
473         int i;
474
475         for( i = 0 ; i < p_item->i_parents ; i++ )
476         {
477             if( p_item->pp_parents[i]->i_view == i_current_view )
478             {
479                 vlc_mutex_lock( &p_playlist->object_lock );
480                 playlist_RecursiveNodeSort( p_playlist,
481                         p_item->pp_parents[i]->p_parent, i_mode, ORDER_NORMAL );
482                 vlc_mutex_unlock( &p_playlist->object_lock );
483                 break;
484             }
485         }
486     }
487     vlc_object_release( p_playlist );
488     [self playlistUpdated];
489 }
490
491 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
492 {
493     int i_item;
494     intf_thread_t * p_intf = VLCIntf;
495     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
496                                                        FIND_ANYWHERE );
497
498     if( p_playlist == NULL )
499     {
500         return;
501     }
502
503     for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
504     {
505         /* One item */
506         NSDictionary *o_one_item;
507         int j, i_total_options = 0, i_new_id = -1;
508         int i_mode = PLAYLIST_INSERT;
509         BOOL b_rem = FALSE, b_dir = FALSE;
510         NSString *o_uri, *o_name;
511         NSArray *o_options;
512         NSURL *o_true_file;
513         char **ppsz_options = NULL;
514
515         /* Get the item */
516         o_one_item = [o_array objectAtIndex: i_item];
517         o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
518         o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
519         o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
520
521         /* If no name, then make a guess */
522         if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
523
524         if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
525             [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
526                     isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem   )
527         {
528             /* All of this is to make sure CD's play when you D&D them on VLC */
529             /* Converts mountpoint to a /dev file */
530             struct statfs *buf;
531             char *psz_dev;
532             buf = (struct statfs *) malloc (sizeof(struct statfs));
533             statfs( [o_uri fileSystemRepresentation], buf );
534             psz_dev = strdup(buf->f_mntfromname);
535             o_uri = [NSString stringWithCString: psz_dev ];
536         }
537
538         if( o_options && [o_options count] > 0 )
539         {
540             /* Count the input options */
541             i_total_options = [o_options count];
542
543             /* Allocate ppsz_options */
544             for( j = 0; j < i_total_options; j++ )
545             {
546                 if( !ppsz_options )
547                     ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
548
549                 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
550             }
551         }
552
553         /* Add the item */
554         i_new_id = playlist_AddExt( p_playlist, [o_uri fileSystemRepresentation],
555                       [o_name UTF8String], i_mode,
556                       i_position == -1 ? PLAYLIST_END : i_position + i_item,
557                       0, (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options );
558
559         /* Recent documents menu */
560         o_true_file = [NSURL fileURLWithPath: o_uri];
561         if( o_true_file != nil )
562         {
563             [[NSDocumentController sharedDocumentController]
564                 noteNewRecentDocumentURL: o_true_file];
565         }
566
567         if( i_item == 0 && !b_enqueue )
568         {
569             playlist_Goto( p_playlist, playlist_GetPositionById( p_playlist, i_new_id ) );
570             playlist_Play( p_playlist );
571         }
572     }
573
574     vlc_object_release( p_playlist );
575 }
576
577 - (IBAction)handlePopUp:(id)sender
578
579 {
580     intf_thread_t * p_intf = VLCIntf;
581     vlc_value_t val1,val2;
582     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
583                                             FIND_ANYWHERE );
584     if( p_playlist == NULL )
585     {
586         return;
587     }
588
589     switch( [o_loop_popup indexOfSelectedItem] )
590     {
591         case 1:
592
593              val1.b_bool = 0;
594              var_Set( p_playlist, "loop", val1 );
595              val1.b_bool = 1;
596              var_Set( p_playlist, "repeat", val1 );
597              vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat One" ) );
598         break;
599
600         case 2:
601              val1.b_bool = 0;
602              var_Set( p_playlist, "repeat", val1 );
603              val1.b_bool = 1;
604              var_Set( p_playlist, "loop", val1 );
605              vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat All" ) );
606         break;
607
608         default:
609              var_Get( p_playlist, "repeat", &val1 );
610              var_Get( p_playlist, "loop", &val2 );
611              if( val1.b_bool || val2.b_bool )
612              {
613                   val1.b_bool = 0;
614                   var_Set( p_playlist, "repeat", val1 );
615                   var_Set( p_playlist, "loop", val1 );
616                   vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
617              }
618          break;
619      }
620      vlc_object_release( p_playlist );
621      [self playlistUpdated];
622 }
623
624 - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
625 {
626     playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
627                                                        FIND_ANYWHERE );
628     playlist_item_t *p_selected_item;
629     int i_current, i_selected_row;
630
631     if( !p_playlist )
632         return NULL;
633
634     i_selected_row = [o_outline_view selectedRow];
635     if (i_selected_row < 0)
636         i_selected_row = 0;
637
638     p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow:
639                                             i_selected_row] pointerValue];
640
641     for( i_current = 0; i_current < p_item->i_children ; i_current++ )
642     {
643         char *psz_temp;
644         NSString *o_current_name, *o_current_author;
645
646         vlc_mutex_lock( &p_playlist->object_lock );
647         o_current_name = [NSString stringWithUTF8String:
648             p_item->pp_children[i_current]->input.psz_name];
649         psz_temp = vlc_input_item_GetInfo( &p_item->input ,
650                                    _("Meta-information"),_("Artist") );
651         o_current_author = [NSString stringWithUTF8String: psz_temp];
652         free( psz_temp);
653         vlc_mutex_unlock( &p_playlist->object_lock );
654
655         if( p_selected_item == p_item->pp_children[i_current] &&
656                     b_selected_item_met == NO )
657         {
658             b_selected_item_met = YES;
659         }
660         else if( p_selected_item == p_item->pp_children[i_current] &&
661                     b_selected_item_met == YES )
662         {
663             vlc_object_release( p_playlist );
664             return NULL;
665         }
666         else if( b_selected_item_met == YES &&
667                     ( [o_current_name rangeOfString:[o_search_field
668                         stringValue] options:NSCaseInsensitiveSearch ].length ||
669                       [o_current_author rangeOfString:[o_search_field
670                         stringValue] options:NSCaseInsensitiveSearch ].length ) )
671         {
672             vlc_object_release( p_playlist );
673             /*Adds the parent items in the result array as well, so that we can
674             expand the tree*/
675             return [NSMutableArray arrayWithObject: [NSValue
676                             valueWithPointer: p_item->pp_children[i_current]]];
677         }
678         if( p_item->pp_children[i_current]->i_children > 0 )
679         {
680             id o_result = [self subSearchItem:
681                                             p_item->pp_children[i_current]];
682             if( o_result != NULL )
683             {
684                 vlc_object_release( p_playlist );
685                 [o_result insertObject: [NSValue valueWithPointer:
686                                 p_item->pp_children[i_current]] atIndex:0];
687                 return o_result;
688             }
689         }
690     }
691     vlc_object_release( p_playlist );
692     return NULL;
693 }
694
695 - (IBAction)searchItem:(id)sender
696 {
697     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
698                                                        FIND_ANYWHERE );
699     playlist_view_t * p_view;
700     id o_result;
701
702     unsigned int i;
703     int i_row = -1;
704
705     b_selected_item_met = NO;
706
707     if( p_playlist == NULL )
708         return;
709
710     p_view = playlist_ViewFind( p_playlist, i_current_view );
711
712     if( p_view )
713     {
714         /*First, only search after the selected item:*
715          *(b_selected_item_met = NO)                 */
716         o_result = [self subSearchItem:p_view->p_root];
717         if( o_result == NULL )
718         {
719             /* If the first search failed, search again from the beginning */
720             o_result = [self subSearchItem:p_view->p_root];
721         }
722         if( o_result != NULL )
723         {
724             for( i = 0 ; i < [o_result count] - 1 ; i++ )
725             {
726                 [o_outline_view expandItem: [o_outline_dict objectForKey:
727                             [NSString stringWithFormat: @"%p",
728                             [[o_result objectAtIndex: i] pointerValue]]]];
729             }
730             i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
731                             [NSString stringWithFormat: @"%p",
732                             [[o_result objectAtIndex: [o_result count] - 1 ]
733                             pointerValue]]]];
734         }
735         if( i_row > -1 )
736         {
737             [o_outline_view selectRow:i_row byExtendingSelection: NO];
738             [o_outline_view scrollRowToVisible: i_row];
739         }
740     }
741     vlc_object_release( p_playlist );
742 }
743
744 - (NSMenu *)menuForEvent:(NSEvent *)o_event
745 {
746     NSPoint pt;
747     vlc_bool_t b_rows;
748     vlc_bool_t b_item_sel;
749
750     pt = [o_outline_view convertPoint: [o_event locationInWindow]
751                                                  fromView: nil];
752     b_item_sel = ( [o_outline_view rowAtPoint: pt] != -1 &&
753                    [o_outline_view selectedRow] != -1 );
754     b_rows = [o_outline_view numberOfRows] != 0;
755
756     [o_mi_play setEnabled: b_item_sel];
757     [o_mi_delete setEnabled: b_item_sel];
758     [o_mi_selectall setEnabled: b_rows];
759     [o_mi_info setEnabled: b_item_sel];
760
761     return( o_ctx_menu );
762 }
763
764 - (playlist_item_t *)selectedPlaylistItem
765 {
766     return [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
767                                                                 pointerValue];
768 }
769
770 - (void)outlineView: (NSTableView*)o_tv
771                   didClickTableColumn:(NSTableColumn *)o_tc
772 {
773     int i_mode = 0, i_type;
774     intf_thread_t *p_intf = VLCIntf;
775     playlist_view_t *p_view;
776
777     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
778                                        FIND_ANYWHERE );
779     if( p_playlist == NULL )
780     {
781         return;
782     }
783     
784     /* Check whether the selected table column header corresponds to a
785        sortable table column*/
786     if( !( o_tc == o_tc_name || o_tc == o_tc_author ) )
787     {
788         vlc_object_release( p_playlist );
789         return;
790     }
791
792     p_view = playlist_ViewFind( p_playlist, i_current_view );
793
794     if( o_tc_sortColumn == o_tc )
795     {
796         b_isSortDescending = !b_isSortDescending;
797     }
798     else
799     {
800         b_isSortDescending = VLC_FALSE;
801     }
802
803     if( o_tc == o_tc_name )
804     {
805         i_mode = SORT_TITLE;
806     }
807     else if( o_tc == o_tc_author )
808     {
809         i_mode = SORT_AUTHOR;
810     }
811
812     if( b_isSortDescending )
813     {
814         i_type = ORDER_REVERSE;
815     }
816     else
817     {
818         i_type = ORDER_NORMAL;
819     }
820
821     vlc_mutex_lock( &p_playlist->object_lock );
822     playlist_RecursiveNodeSort( p_playlist, p_view->p_root, i_mode, i_type );
823     vlc_mutex_unlock( &p_playlist->object_lock );
824
825     vlc_object_release( p_playlist );
826     [self playlistUpdated];
827
828     o_tc_sortColumn = o_tc;
829     [o_outline_view setHighlightedTableColumn:o_tc];
830
831     if( b_isSortDescending )
832     {
833         [o_outline_view setIndicatorImage:o_descendingSortingImage
834                                                         inTableColumn:o_tc];
835     }
836     else
837     {
838         [o_outline_view setIndicatorImage:o_ascendingSortingImage
839                                                         inTableColumn:o_tc];
840     }
841 }
842
843 @end
844
845 @implementation VLCPlaylist (NSOutlineViewDataSource)
846
847 /* return the number of children for Obj-C pointer item */ /* DONE */
848 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
849 {
850     int i_return = 0;
851     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
852                                                        FIND_ANYWHERE );
853     if( p_playlist == NULL || outlineView != o_outline_view )
854         return 0;
855
856     if( item == nil )
857     {
858         /* root object */
859         playlist_view_t *p_view;
860         p_view = playlist_ViewFind( p_playlist, i_current_view );
861         if( p_view && p_view->p_root )
862             i_return = p_view->p_root->i_children;
863     }
864     else
865     {
866         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
867         if( p_item )
868             i_return = p_item->i_children;
869     }
870     vlc_object_release( p_playlist );
871     
872     if( i_return <= 0 )
873         i_return = 0;
874     
875     return i_return;
876 }
877
878 /* return the child at index for the Obj-C pointer item */ /* DONE */
879 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
880 {
881     playlist_item_t *p_return = NULL;
882     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
883                                                        FIND_ANYWHERE );
884     NSValue *o_value;
885
886     if( p_playlist == NULL )
887         return nil;
888
889     if( item == nil )
890     {
891         /* root object */
892         playlist_view_t *p_view;
893         p_view = playlist_ViewFind( p_playlist, i_current_view );
894         if( p_view && index < p_view->p_root->i_children && index >= 0 )
895             p_return = p_view->p_root->pp_children[index];
896     }
897     else
898     {
899         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
900         if( p_item && index < p_item->i_children && index >= 0 )
901             p_return = p_item->pp_children[index];
902     }
903     
904     if( p_playlist->i_size >= 2 )
905     {
906         [o_status_field setStringValue: [NSString stringWithFormat:
907                     _NS("%i items in playlist"), p_playlist->i_size]];
908     }
909     else
910     {
911         if( p_playlist->i_size == 0 )
912         {
913             [o_status_field setStringValue: [NSString stringWithFormat:
914                     _NS("no items in playlist"), p_playlist->i_size]];
915         }
916         else
917         {
918             [o_status_field setStringValue: [NSString stringWithFormat:
919                     _NS("1 item in playlist"), p_playlist->i_size]];
920         }
921     }
922
923     vlc_object_release( p_playlist );
924     
925
926     o_value = [[NSValue valueWithPointer: p_return] retain];
927
928     if( [o_outline_dict objectForKey: [NSString stringWithFormat:@"%p", p_return]] == nil )
929     {
930         [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p", p_return]];
931     }
932     return o_value;
933 }
934
935 /* is the item expandable */
936 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
937 {
938     int i_return = 0;
939     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
940                                                        FIND_ANYWHERE );
941     if( p_playlist == NULL )
942         return NO;
943
944     if( item == nil )
945     {
946         /* root object */
947         playlist_view_t *p_view;
948         p_view = playlist_ViewFind( p_playlist, i_current_view );
949         if( p_view && p_view->p_root )
950             i_return = p_view->p_root->i_children;
951     }
952     else
953     {
954         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
955         if( p_item )
956             i_return = p_item->i_children;
957     }
958     vlc_object_release( p_playlist );
959
960     if( i_return <= 0 )
961         return NO;
962     else
963         return YES;
964 }
965
966 /* retrieve the string values for the cells */
967 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
968 {
969     id o_value = nil;
970     intf_thread_t *p_intf = VLCIntf;
971     playlist_t *p_playlist;
972     playlist_item_t *p_item;
973     
974     if( item == nil || ![item isKindOfClass: [NSValue class]] ) return( @"error" );
975     
976     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
977                                                FIND_ANYWHERE );
978     if( p_playlist == NULL )
979     {
980         return( @"error" );
981     }
982     
983     p_item = (playlist_item_t *)[item pointerValue];
984
985     if( p_item == NULL )
986     {
987         vlc_object_release( p_playlist );
988         return( @"error");
989     }
990
991     if( [[o_tc identifier] isEqualToString:@"1"] )
992     {
993         o_value = [NSString stringWithUTF8String:
994             p_item->input.psz_name];
995         if( o_value == NULL )
996             o_value = [NSString stringWithCString:
997                 p_item->input.psz_name];
998     }
999     else if( [[o_tc identifier] isEqualToString:@"2"] )
1000     {
1001         char *psz_temp;
1002         psz_temp = vlc_input_item_GetInfo( &p_item->input ,_("Meta-information"),_("Artist") );
1003
1004         if( psz_temp == NULL )
1005             o_value = @"";
1006         else
1007         {
1008             o_value = [NSString stringWithUTF8String: psz_temp];
1009             if( o_value == NULL )
1010             {
1011                 o_value = [NSString stringWithCString: psz_temp];
1012             }
1013             free( psz_temp );
1014         }
1015     }
1016     else if( [[o_tc identifier] isEqualToString:@"3"] )
1017     {
1018         char psz_duration[MSTRTIME_MAX_SIZE];
1019         mtime_t dur = p_item->input.i_duration;
1020         if( dur != -1 )
1021         {
1022             secstotimestr( psz_duration, dur/1000000 );
1023             o_value = [NSString stringWithUTF8String: psz_duration];
1024         }
1025         else
1026         {
1027             o_value = @"-:--:--";
1028         }
1029     }
1030     vlc_object_release( p_playlist );
1031
1032     return( o_value );
1033 }
1034
1035 /* Required for drag & drop and reordering */
1036 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
1037 {
1038     return NO;
1039 }
1040
1041 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
1042 {
1043     return NSDragOperationNone;
1044 }
1045
1046 /* Delegate method of NSWindow */
1047 /*- (void)windowWillClose:(NSNotification *)aNotification
1048 {
1049     [o_btn_playlist setState: NSOffState];
1050 }
1051 */
1052 @end
1053
1054