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