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