]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlist.m
* playlist.m: fix a locking issue
[vlc] / modules / gui / macosx / playlist.m
1 /*****************************************************************************
2  * playlist.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2002-2004 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 'playlist toggle' that hides the playlist and in effect give you the old controller
29  * create a new search field build with pictures from the 'regular' search field, so it can be emulated on 10.2
30  * create toggle buttons for the shuffle, repeat one, repeat all functions.
31  * implement drag and drop and item reordering.
32  * reimplement enable/disable item
33  * create a new 'tool' button (see the gear button in the Finder window) for 'actions'
34    (adding service discovery, other views, new node/playlist, save node/playlist) stuff like that
35  */
36
37
38 /*****************************************************************************
39  * Preamble
40  *****************************************************************************/
41 #include <stdlib.h>                                      /* malloc(), free() */
42 #include <sys/param.h>                                    /* for MAXPATHLEN */
43 #include <string.h>
44 #include <math.h>
45 #include <sys/mount.h>
46 #include <vlc_keys.h>
47
48 #include "intf.h"
49 #include "playlist.h"
50 #include "controls.h"
51 #include "osd.h"
52 #include "misc.h"
53
54 #define REF_HEIGHT 500
55 #define REF_WIDTH 500
56
57 /*****************************************************************************
58  * VLCPlaylistView implementation 
59  *****************************************************************************/
60 @implementation VLCPlaylistView
61
62 - (NSMenu *)menuForEvent:(NSEvent *)o_event
63 {
64     return( [[self delegate] menuForEvent: o_event] );
65 }
66
67 - (void)keyDown:(NSEvent *)o_event
68 {
69     unichar key = 0;
70
71     if( [[o_event characters] length] )
72     {
73         key = [[o_event characters] characterAtIndex: 0];
74     }
75
76     switch( key )
77     {
78         case NSDeleteCharacter:
79         case NSDeleteFunctionKey:
80         case NSDeleteCharFunctionKey:
81         case NSBackspaceCharacter:
82             [[self delegate] deleteItem:self];
83             break;
84
85         default:
86             [super keyDown: o_event];
87             break;
88     }
89 }
90
91 @end
92
93 /*****************************************************************************
94  * VLCPlaylist implementation 
95  *****************************************************************************/
96 @implementation VLCPlaylist
97
98 - (id)init
99 {
100     self = [super init];
101     if ( self != nil )
102     {
103         o_outline_dict = [[NSMutableDictionary alloc] init];
104         //i_moveRow = -1;
105     }
106     return self;
107 }
108
109 - (void)awakeFromNib
110 {
111     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
112                                           FIND_ANYWHERE );
113     i_current_view = VIEW_CATEGORY;
114     playlist_ViewUpdate( p_playlist, i_current_view );
115     vlc_object_release( p_playlist );
116
117     [o_outline_view setTarget: self];
118     [o_outline_view setDelegate: self];
119     [o_outline_view setDataSource: self];
120
121     [o_outline_view setDoubleAction: @selector(playItem:)];
122
123     [o_outline_view registerForDraggedTypes: 
124         [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
125     [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
126
127 /* We need to check whether _defaultTableHeaderSortImage exists, since it 
128 belongs to an Apple hidden private API, and then can "disapear" at any time*/
129
130     if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderSortImage)] )
131     {
132         o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
133     }
134     else
135     {
136         o_ascendingSortingImage = nil;
137     }
138
139     if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderReverseSortImage)] )
140     {
141         o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
142     }
143     else
144     {
145         o_descendingSortingImage = nil;
146     }
147
148     o_tc_sortColumn = nil;
149
150     [self initStrings];
151     //[self playlistUpdated];
152 }
153
154 - (void)initStrings
155 {
156     [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
157     [o_mi_play setTitle: _NS("Play")];
158     [o_mi_delete setTitle: _NS("Delete")];
159     [o_mi_selectall setTitle: _NS("Select All")];
160     [o_mi_info setTitle: _NS("Properties")];
161     [o_mi_sort_name setTitle: _NS("Sort Node by Name")];
162     [o_mi_sort_author setTitle: _NS("Sort Node by Author")];
163     [[o_tc_name headerCell] setStringValue:_NS("Name")];
164     [[o_tc_author headerCell] setStringValue:_NS("Author")];
165     [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
166     [o_status_field setStringValue: [NSString stringWithFormat:
167                         _NS("no items in playlist")]];
168
169     [o_random_ckb setTitle: _NS("Random")];
170 #if 0
171     [o_search_button setTitle: _NS("Search")];
172 #endif
173     [o_btn_playlist setToolTip: _NS("Playlist")];
174     [[o_loop_popup itemAtIndex:0] setTitle: _NS("Standard Play")];
175     [[o_loop_popup itemAtIndex:1] setTitle: _NS("Repeat One")];
176     [[o_loop_popup itemAtIndex:2] setTitle: _NS("Repeat All")];
177 }
178
179 - (NSOutlineView *)playlistView
180 {
181     return o_outline_view;
182 }
183
184 - (IBAction)toggleWindow:(id)sender
185 {
186     NSRect o_rect;
187     /*First, check if the playlist is visible*/
188     if ( [o_controller frame].size.height == [o_controller minSize].height )
189     {
190         /*Check if the stored heigth of the controller is usable (!= minSize)*/
191         if ([o_controller getSizeWithPlaylist].height !=
192                                         [o_controller minSize].height)
193         {
194             o_rect.size.height = [o_controller getSizeWithPlaylist].height;
195         }
196         else
197         {
198             /*If the stored height is not usable, use a reference one*/
199             o_rect.size.height = REF_HEIGHT;
200         }
201
202         /*Check if the controller width is the minimum one*/
203         if ( [o_controller frame].size.width == [o_controller minSize].width)
204         {
205             /*If the controller width is minimum, check if the stored height
206               of the playlist makes it visible*/
207             if ([o_controller getSizeWithPlaylist].height !=
208                                                [o_controller minSize].height)
209             {
210                 o_rect.size.width = [o_controller getSizeWithPlaylist].width;
211             }
212             else
213             {
214                 /*If not, use a reference width*/
215                 o_rect.size.width = REF_WIDTH;
216             }
217         }
218         else
219         {
220             o_rect.size.width = [o_controller frame].size.width;
221         }
222         o_rect.origin.x = [o_controller frame].origin.x;
223         o_rect.origin.y = [o_controller frame].origin.y - o_rect.size.height +
224                                                 [o_controller minSize].height;
225
226         [o_btn_playlist setState: YES];
227     }
228     else
229     {
230         o_rect.size = [o_controller minSize];
231         o_rect.origin.x = [o_controller frame].origin.x;
232         /*Calculate the position of the lower right corner after resize*/
233         o_rect.origin.y = [o_controller frame].origin.y +
234             [o_controller frame].size.height - [o_controller minSize].height;
235
236         [o_btn_playlist setState: NO];
237     }
238
239     [o_controller setFrame: o_rect display:YES animate: YES];
240 }
241
242 - (void)playlistUpdated
243 {
244     unsigned int i;
245
246     /* Clear indications of any existing column sorting*/
247     for( i = 0 ; i < [[o_outline_view tableColumns] count] ; i++ )
248     {
249         [o_outline_view setIndicatorImage:nil inTableColumn:
250                             [[o_outline_view tableColumns] objectAtIndex:i]];
251     }
252
253     [o_outline_view setHighlightedTableColumn:nil];
254     o_tc_sortColumn = nil;
255     // TODO Find a way to keep the dict size to a minimum
256     //[o_outline_dict removeAllObjects];
257     [o_outline_view reloadData];
258 }
259
260
261 - (void)updateTogglePlaylistState
262 {
263     if( [o_controller getSizeWithPlaylist].height ==
264                                     [o_controller minSize].height )
265     {
266         [o_btn_playlist setState: NO];
267     }
268     else
269     {
270         [o_btn_playlist setState: YES];
271     }
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     p_temp_item = p_item;
289
290     while( p_temp_item->i_parents > 0 )
291     {
292         [o_array insertObject: [NSValue valueWithPointer: p_temp_item] atIndex: 0];
293         for (i = 0 ; i < p_temp_item->i_parents ; i++)
294         {
295             if( p_temp_item->pp_parents[i]->i_view == i_current_view )
296             {
297                 p_temp_item = p_temp_item->pp_parents[i]->p_parent;
298                 break;
299             }
300         }
301     }
302
303     for (j = 0 ; j < [o_array count] - 1 ; j++)
304     {
305         [o_outline_view expandItem: [o_outline_dict objectForKey:
306                             [NSString stringWithFormat: @"%p",
307                             [[o_array objectAtIndex:j] pointerValue]]]];
308
309     }
310
311     i_row = [o_outline_view rowForItem:[o_outline_dict
312             objectForKey:[NSString stringWithFormat: @"%p", p_item]]];
313
314     [o_outline_view selectRow: i_row byExtendingSelection: NO];
315     [o_outline_view scrollRowToVisible: i_row];
316
317     vlc_object_release(p_playlist);
318 }
319
320
321 - (BOOL)isItem: (playlist_item_t *)p_item inNode: (playlist_item_t *)p_node
322 {
323     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
324                                           FIND_ANYWHERE );
325     playlist_item_t *p_temp_item = p_item;
326
327     if( p_playlist == NULL )
328     {
329         return NO;
330     }
331
332     while( p_temp_item->i_parents > 0 )
333     {
334         int i;
335         for( i = 0; i < p_temp_item->i_parents ; i++ )
336         {
337             if( p_temp_item->pp_parents[i]->i_view == i_current_view )
338             {
339                 if( p_temp_item->pp_parents[i]->p_parent == p_node )
340                 {
341                     vlc_object_release( p_playlist );
342                     return YES;
343                 }
344                 else
345                 {
346                     p_temp_item = p_temp_item->pp_parents[i]->p_parent;
347                     break;
348                 }
349             }
350         }
351     }
352
353     vlc_object_release( p_playlist );
354     return NO;
355 }
356
357
358 /* When called retrieves the selected outlineview row and plays that node or item */
359 - (IBAction)playItem:(id)sender
360 {
361     intf_thread_t * p_intf = VLCIntf;
362     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
363                                                        FIND_ANYWHERE );
364
365     if( p_playlist != NULL )
366     {
367         playlist_item_t *p_item;
368         playlist_item_t *p_node = NULL;
369         int i;
370
371         p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
372
373         if( p_item )
374         {
375             if( p_item->i_children == -1 )
376             {
377                 for( i = 0 ; i < p_item->i_parents ; i++ )
378                 {
379                     if( p_item->pp_parents[i]->i_view == i_current_view )
380                     {
381                         p_node = p_item->pp_parents[i]->p_parent;
382                     }
383                 }
384             }
385             else
386             {
387                 p_node = p_item;
388                 if( p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1 )
389                 {
390                     p_item = p_node->pp_children[0];
391                 }
392                 else
393                 {
394                     p_item = NULL;
395                 }
396             }
397
398             playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view, p_node, p_item );
399         }
400         vlc_object_release( p_playlist );
401     }
402 }
403
404 - (IBAction)selectAll:(id)sender
405 {
406     [o_outline_view selectAll: nil];
407 }
408
409 - (IBAction)deleteItem:(id)sender
410 {
411     int i, i_count, i_row;
412     NSMutableArray *o_to_delete;
413     NSNumber *o_number;
414
415     playlist_t * p_playlist;
416     intf_thread_t * p_intf = VLCIntf;
417
418     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
419                                           FIND_ANYWHERE );
420
421     if ( p_playlist == NULL )
422     {
423         return;
424     }
425     o_to_delete = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
426     i_count = [o_to_delete count];
427
428     for( i = 0; i < i_count; i++ )
429     {
430         playlist_item_t * p_item;
431         o_number = [o_to_delete lastObject];
432         i_row = [o_number intValue];
433
434         [o_to_delete removeObject: o_number];
435         [o_outline_view deselectRow: i_row];
436
437         p_item = (playlist_item_t *)[[o_outline_view itemAtRow: i_row] pointerValue];
438
439         if( p_item->i_children > -1 ) //is a node and not an item
440         {
441             if( p_playlist->status.i_status != PLAYLIST_STOPPED &&
442                 [self isItem: p_playlist->status.p_item inNode: p_item] == YES )
443             {
444                 // if current item is in selected node and is playing then stop playlist
445                 playlist_Stop( p_playlist );
446             }
447             playlist_NodeDelete( p_playlist, p_item, VLC_TRUE);
448         }
449         else
450         {
451             if( p_playlist->status.i_status != PLAYLIST_STOPPED &&
452                 p_playlist->status.p_item == [[o_outline_view itemAtRow: i_row] pointerValue] )
453             {
454                 playlist_Stop( p_playlist );
455             }
456             playlist_LockDelete( p_playlist, p_item->input.i_id );
457         }
458     }
459     [self playlistUpdated];
460     vlc_object_release( p_playlist );
461 }
462
463 - (IBAction)sortNodeByName:(id)sender
464 {
465     [self sortNode: SORT_TITLE];
466 }
467
468 - (IBAction)sortNodeByAuthor:(id)sender
469 {
470     [self sortNode: SORT_AUTHOR];
471 }
472
473 - (void)sortNode:(int)i_mode
474 {
475     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
476                                           FIND_ANYWHERE );
477     playlist_item_t * p_item;
478
479     if (p_playlist == NULL)
480     {
481         return;
482     }
483
484     if( [o_outline_view selectedRow] > -1 )
485     {
486         p_item = [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
487                                                                 pointerValue];
488     }
489     else
490     /*If no item is selected, sort the whole playlist*/
491     {
492         playlist_view_t * p_view = playlist_ViewFind( p_playlist, i_current_view );
493         p_item = p_view->p_root;
494     }
495
496     if( p_item->i_children > -1 ) // the item is a node
497     {
498         vlc_mutex_lock( &p_playlist->object_lock );
499         playlist_RecursiveNodeSort( p_playlist, p_item, i_mode, ORDER_NORMAL );
500         vlc_mutex_unlock( &p_playlist->object_lock );
501     }
502     else
503     {
504         int i;
505
506         for( i = 0 ; i < p_item->i_parents ; i++ )
507         {
508             if( p_item->pp_parents[i]->i_view == i_current_view )
509             {
510                 vlc_mutex_lock( &p_playlist->object_lock );
511                 playlist_RecursiveNodeSort( p_playlist,
512                         p_item->pp_parents[i]->p_parent, i_mode, ORDER_NORMAL );
513                 vlc_mutex_unlock( &p_playlist->object_lock );
514                 break;
515             }
516         }
517     }
518     vlc_object_release( p_playlist );
519     [self playlistUpdated];
520 }
521
522 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
523 {
524     int i_item;
525     intf_thread_t * p_intf = VLCIntf;
526     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
527                                                        FIND_ANYWHERE );
528
529     if( p_playlist == NULL )
530     {
531         return;
532     }
533
534     for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
535     {
536         /* One item */
537         NSDictionary *o_one_item;
538         int j, i_total_options = 0, i_new_id = -1;
539         int i_mode = PLAYLIST_INSERT;
540         BOOL b_rem = FALSE, b_dir = FALSE;
541         NSString *o_uri, *o_name;
542         NSArray *o_options;
543         NSURL *o_true_file;
544         char **ppsz_options = NULL;
545
546         /* Get the item */
547         o_one_item = [o_array objectAtIndex: i_item];
548         o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
549         o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
550         o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
551
552         /* If no name, then make a guess */
553         if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
554
555         if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
556             [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
557                     isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem   )
558         {
559             /* All of this is to make sure CD's play when you D&D them on VLC */
560             /* Converts mountpoint to a /dev file */
561             struct statfs *buf;
562             char *psz_dev;
563             buf = (struct statfs *) malloc (sizeof(struct statfs));
564             statfs( [o_uri fileSystemRepresentation], buf );
565             psz_dev = strdup(buf->f_mntfromname);
566             o_uri = [NSString stringWithCString: psz_dev ];
567         }
568
569         if( o_options && [o_options count] > 0 )
570         {
571             /* Count the input options */
572             i_total_options = [o_options count];
573
574             /* Allocate ppsz_options */
575             for( j = 0; j < i_total_options; j++ )
576             {
577                 if( !ppsz_options )
578                     ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
579
580                 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
581             }
582         }
583
584         /* Add the item */
585         i_new_id = playlist_AddExt( p_playlist, [o_uri fileSystemRepresentation],
586                       [o_name UTF8String], i_mode,
587                       i_position == -1 ? PLAYLIST_END : i_position + i_item,
588                       0, (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options );
589
590         /* Recent documents menu */
591         o_true_file = [NSURL fileURLWithPath: o_uri];
592         if( o_true_file != nil )
593         {
594             [[NSDocumentController sharedDocumentController]
595                 noteNewRecentDocumentURL: o_true_file];
596         }
597
598         if( i_item == 0 && !b_enqueue )
599         {
600             playlist_Goto( p_playlist, playlist_GetPositionById( p_playlist, i_new_id ) );
601             playlist_Play( p_playlist );
602         }
603     }
604
605     vlc_object_release( p_playlist );
606 }
607
608 - (IBAction)handlePopUp:(id)sender
609
610 {
611     intf_thread_t * p_intf = VLCIntf;
612     vlc_value_t val1,val2;
613     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
614                                             FIND_ANYWHERE );
615     if( p_playlist == NULL )
616     {
617         return;
618     }
619
620     switch( [o_loop_popup indexOfSelectedItem] )
621     {
622         case 1:
623
624              val1.b_bool = 0;
625              var_Set( p_playlist, "loop", val1 );
626              val1.b_bool = 1;
627              var_Set( p_playlist, "repeat", val1 );
628              vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat One" ) );
629         break;
630
631         case 2:
632              val1.b_bool = 0;
633              var_Set( p_playlist, "repeat", val1 );
634              val1.b_bool = 1;
635              var_Set( p_playlist, "loop", val1 );
636              vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat All" ) );
637         break;
638
639         default:
640              var_Get( p_playlist, "repeat", &val1 );
641              var_Get( p_playlist, "loop", &val2 );
642              if( val1.b_bool || val2.b_bool )
643              {
644                   val1.b_bool = 0;
645                   var_Set( p_playlist, "repeat", val1 );
646                   var_Set( p_playlist, "loop", val1 );
647                   vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
648              }
649          break;
650      }
651      vlc_object_release( p_playlist );
652      [self playlistUpdated];
653 }
654
655 - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
656 {
657     playlist_t *p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
658                                                        FIND_ANYWHERE );
659     playlist_item_t *p_selected_item;
660     int i_current, i_selected_row;
661
662     if( !p_playlist )
663         return NULL;
664
665     i_selected_row = [o_outline_view selectedRow];
666     if (i_selected_row < 0)
667         i_selected_row = 0;
668
669     p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow:
670                                             i_selected_row] pointerValue];
671
672     for( i_current = 0; i_current < p_item->i_children ; i_current++ )
673     {
674         char *psz_temp;
675         NSString *o_current_name, *o_current_author;
676
677         vlc_mutex_lock( &p_playlist->object_lock );
678         o_current_name = [NSString stringWithUTF8String:
679             p_item->pp_children[i_current]->input.psz_name];
680         psz_temp = vlc_input_item_GetInfo( &p_item->input ,
681                                    _("Meta-information"),_("Author") );
682         o_current_author = [NSString stringWithUTF8String: psz_temp];
683         free( psz_temp);
684         vlc_mutex_unlock( &p_playlist->object_lock );
685
686         if( p_selected_item == p_item->pp_children[i_current] &&
687                     b_selected_item_met == NO )
688         {
689             b_selected_item_met = YES;
690         }
691         else if( p_selected_item == p_item->pp_children[i_current] &&
692                     b_selected_item_met == YES )
693         {
694             vlc_object_release( p_playlist );
695             return NULL;
696         }
697         else if( b_selected_item_met == YES &&
698                     ( [o_current_name rangeOfString:[o_search_field
699                         stringValue] options:NSCaseInsensitiveSearch ].length ||
700                       [o_current_author rangeOfString:[o_search_field
701                         stringValue] options:NSCaseInsensitiveSearch ].length ) )
702         {
703             vlc_object_release( p_playlist );
704             /*Adds the parent items in the result array as well, so that we can
705             expand the tree*/
706             return [NSMutableArray arrayWithObject: [NSValue
707                             valueWithPointer: p_item->pp_children[i_current]]];
708         }
709         if( p_item->pp_children[i_current]->i_children > 0 )
710         {
711             id o_result = [self subSearchItem:
712                                             p_item->pp_children[i_current]];
713             if( o_result != NULL )
714             {
715                 vlc_object_release( p_playlist );
716                 [o_result insertObject: [NSValue valueWithPointer:
717                                 p_item->pp_children[i_current]] atIndex:0];
718                 return o_result;
719             }
720         }
721     }
722     vlc_object_release( p_playlist );
723     return NULL;
724 }
725
726 - (IBAction)searchItem:(id)sender
727 {
728     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
729                                                        FIND_ANYWHERE );
730     playlist_view_t * p_view;
731     id o_result;
732
733     unsigned int i;
734     int i_row = -1;
735
736     b_selected_item_met = NO;
737
738     if( p_playlist == NULL )
739         return;
740
741     p_view = playlist_ViewFind( p_playlist, i_current_view );
742
743     if( p_view )
744     {
745         /*First, only search after the selected item:*
746          *(b_selected_item_met = NO)                 */
747         o_result = [self subSearchItem:p_view->p_root];
748         if( o_result == NULL )
749         {
750             /* If the first search failed, search again from the beginning */
751             o_result = [self subSearchItem:p_view->p_root];
752         }
753         if( o_result != NULL )
754         {
755             for( i = 0 ; i < [o_result count] - 1 ; i++ )
756             {
757                 [o_outline_view expandItem: [o_outline_dict objectForKey:
758                             [NSString stringWithFormat: @"%p",
759                             [[o_result objectAtIndex: i] pointerValue]]]];
760             }
761             i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
762                             [NSString stringWithFormat: @"%p",
763                             [[o_result objectAtIndex: [o_result count] - 1 ]
764                             pointerValue]]]];
765         }
766         if( i_row > -1 )
767         {
768             [o_outline_view selectRow:i_row byExtendingSelection: NO];
769             [o_outline_view scrollRowToVisible: i_row];
770         }
771     }
772     vlc_object_release( p_playlist );
773 }
774
775 - (NSMenu *)menuForEvent:(NSEvent *)o_event
776 {
777     NSPoint pt;
778     vlc_bool_t b_rows;
779     vlc_bool_t b_item_sel;
780
781     pt = [o_outline_view convertPoint: [o_event locationInWindow]
782                                                  fromView: nil];
783     b_item_sel = ( [o_outline_view rowAtPoint: pt] != -1 &&
784                    [o_outline_view selectedRow] != -1 );
785     b_rows = [o_outline_view numberOfRows] != 0;
786
787     [o_mi_play setEnabled: b_item_sel];
788     [o_mi_delete setEnabled: b_item_sel];
789     [o_mi_selectall setEnabled: b_rows];
790     [o_mi_info setEnabled: b_item_sel];
791
792     return( o_ctx_menu );
793 }
794
795 - (playlist_item_t *)selectedPlaylistItem
796 {
797     return [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
798                                                                 pointerValue];
799 }
800
801 - (void)outlineView: (NSTableView*)o_tv
802                   didClickTableColumn:(NSTableColumn *)o_tc
803 {
804     int i_mode = 0, i_type;
805     intf_thread_t *p_intf = VLCIntf;
806     playlist_view_t *p_view;
807
808     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
809                                        FIND_ANYWHERE );
810     if( p_playlist == NULL )
811     {
812         return;
813     }
814     
815     /* Check whether the selected table column header corresponds to a
816        sortable table column*/
817     if( !( o_tc == o_tc_name || o_tc == o_tc_author ) )
818     {
819         vlc_object_release( p_playlist );
820         return;
821     }
822
823     p_view = playlist_ViewFind( p_playlist, i_current_view );
824
825     if( o_tc_sortColumn == o_tc )
826     {
827         b_isSortDescending = !b_isSortDescending;
828     }
829     else
830     {
831         b_isSortDescending = VLC_FALSE;
832     }
833
834     if( o_tc == o_tc_name )
835     {
836         i_mode = SORT_TITLE;
837     }
838     else if( o_tc == o_tc_author )
839     {
840         i_mode = SORT_AUTHOR;
841     }
842
843     if( b_isSortDescending )
844     {
845         i_type = ORDER_REVERSE;
846     }
847     else
848     {
849         i_type = ORDER_NORMAL;
850     }
851
852     vlc_mutex_lock( &p_playlist->object_lock );
853     playlist_RecursiveNodeSort( p_playlist, p_view->p_root, i_mode, i_type );
854     vlc_mutex_unlock( &p_playlist->object_lock );
855
856     vlc_object_release( p_playlist );
857     [self playlistUpdated];
858
859     o_tc_sortColumn = o_tc;
860     [o_outline_view setHighlightedTableColumn:o_tc];
861
862     if( b_isSortDescending )
863     {
864         [o_outline_view setIndicatorImage:o_descendingSortingImage
865                                                         inTableColumn:o_tc];
866     }
867     else
868     {
869         [o_outline_view setIndicatorImage:o_ascendingSortingImage
870                                                         inTableColumn:o_tc];
871     }
872 }
873
874 @end
875
876 @implementation VLCPlaylist (NSOutlineViewDataSource)
877
878 /* return the number of children for Obj-C pointer item */ /* DONE */
879 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
880 {
881     int i_return = 0;
882     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
883                                                        FIND_ANYWHERE );
884     if( p_playlist == NULL || outlineView != o_outline_view )
885         return 0;
886
887     if( item == nil )
888     {
889         /* root object */
890         playlist_view_t *p_view;
891         p_view = playlist_ViewFind( p_playlist, i_current_view );
892         if( p_view && p_view->p_root )
893             i_return = p_view->p_root->i_children;
894     }
895     else
896     {
897         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
898         if( p_item )
899             i_return = p_item->i_children;
900     }
901     vlc_object_release( p_playlist );
902     
903     if( i_return <= 0 )
904         i_return = 0;
905     
906     return i_return;
907 }
908
909 /* return the child at index for the Obj-C pointer item */ /* DONE */
910 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
911 {
912     playlist_item_t *p_return = NULL;
913     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
914                                                        FIND_ANYWHERE );
915     NSValue *o_value;
916
917     if( p_playlist == NULL )
918         return nil;
919
920     if( item == nil )
921     {
922         /* root object */
923         playlist_view_t *p_view;
924         p_view = playlist_ViewFind( p_playlist, i_current_view );
925         if( p_view && index < p_view->p_root->i_children && index >= 0 )
926             p_return = p_view->p_root->pp_children[index];
927     }
928     else
929     {
930         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
931         if( p_item && index < p_item->i_children && index >= 0 )
932             p_return = p_item->pp_children[index];
933     }
934     
935     if( p_playlist->i_size >= 2 )
936     {
937         [o_status_field setStringValue: [NSString stringWithFormat:
938                     _NS("%i items in playlist"), p_playlist->i_size]];
939     }
940     else
941     {
942         if( p_playlist->i_size == 0 )
943         {
944             [o_status_field setStringValue: [NSString stringWithFormat:
945                     _NS("no items in playlist"), p_playlist->i_size]];
946         }
947         else
948         {
949             [o_status_field setStringValue: [NSString stringWithFormat:
950                     _NS("1 item in playlist"), p_playlist->i_size]];
951         }
952     }
953
954     vlc_object_release( p_playlist );
955     
956
957     o_value = [[NSValue valueWithPointer: p_return] retain];
958
959     if( [o_outline_dict objectForKey: [NSString stringWithFormat:@"%p", p_return]] == nil )
960     {
961         [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p", p_return]];
962     }
963     return o_value;
964 }
965
966 /* is the item expandable */
967 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
968 {
969     int i_return = 0;
970     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
971                                                        FIND_ANYWHERE );
972     if( p_playlist == NULL )
973         return NO;
974
975     if( item == nil )
976     {
977         /* root object */
978         playlist_view_t *p_view;
979         p_view = playlist_ViewFind( p_playlist, i_current_view );
980         if( p_view && p_view->p_root )
981             i_return = p_view->p_root->i_children;
982     }
983     else
984     {
985         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
986         if( p_item )
987             i_return = p_item->i_children;
988     }
989     vlc_object_release( p_playlist );
990
991     if( i_return <= 0 )
992         return NO;
993     else
994         return YES;
995 }
996
997 /* retrieve the string values for the cells */
998 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
999 {
1000     id o_value = nil;
1001     intf_thread_t *p_intf = VLCIntf;
1002     playlist_t *p_playlist;
1003     playlist_item_t *p_item;
1004     
1005     if( item == nil || ![item isKindOfClass: [NSValue class]] ) return( @"error" );
1006     
1007     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1008                                                FIND_ANYWHERE );
1009     if( p_playlist == NULL )
1010     {
1011         return( @"error" );
1012     }
1013     
1014     p_item = (playlist_item_t *)[item pointerValue];
1015
1016     if( p_item == NULL )
1017     {
1018         vlc_object_release( p_playlist );
1019         return( @"error");
1020     }
1021
1022     if( [[o_tc identifier] isEqualToString:@"1"] )
1023     {
1024         o_value = [NSString stringWithUTF8String:
1025             p_item->input.psz_name];
1026         if( o_value == NULL )
1027             o_value = [NSString stringWithCString:
1028                 p_item->input.psz_name];
1029     }
1030     else if( [[o_tc identifier] isEqualToString:@"2"] )
1031     {
1032         char *psz_temp;
1033         psz_temp = vlc_input_item_GetInfo( &p_item->input ,_("Meta-information"),_("Artist") );
1034
1035         if( psz_temp == NULL )
1036             o_value = @"";
1037         else
1038         {
1039             o_value = [NSString stringWithUTF8String: psz_temp];
1040             if( o_value == NULL )
1041             {
1042                 o_value = [NSString stringWithCString: psz_temp];
1043             }
1044             free( psz_temp );
1045         }
1046     }
1047     else if( [[o_tc identifier] isEqualToString:@"3"] )
1048     {
1049         char psz_duration[MSTRTIME_MAX_SIZE];
1050         mtime_t dur = p_item->input.i_duration;
1051         if( dur != -1 )
1052         {
1053             secstotimestr( psz_duration, dur/1000000 );
1054             o_value = [NSString stringWithUTF8String: psz_duration];
1055         }
1056         else
1057         {
1058             o_value = @"-:--:--";
1059         }
1060     }
1061     vlc_object_release( p_playlist );
1062
1063     return( o_value );
1064 }
1065
1066 /* Required for drag & drop and reordering */
1067 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
1068 {
1069     return NO;
1070 }
1071
1072 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
1073 {
1074     return NSDragOperationNone;
1075 }
1076
1077 /* Delegate method of NSWindow */
1078 /*- (void)windowWillClose:(NSNotification *)aNotification
1079 {
1080     [o_btn_playlist setState: NSOffState];
1081 }
1082 */
1083 @end
1084
1085