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