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