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