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