]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlist.m
Fix the search field in OSX playlist
[vlc] / modules / gui / macosx / playlist.m
1 /*****************************************************************************
2  * playlist.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2002-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *          Derk-Jan Hartman <hartman at videolan dot org>
9  *          Benjamin Pracht <bigben at videolab dot org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /* TODO
27  * connect delegates, actions and outlets in IB
28  * implement delete by backspace
29  * implement playlist item rightclick menu
30  * implement sorting
31  * add 'icons' for different types of nodes? (http://www.cocoadev.com/index.pl?IconAndTextInTableCell)
32  * create a new 'playlist toggle' that hides the playlist and in effect give you the old controller
33  * create a new search field build with pictures from the 'regular' search field, so it can be emulated on 10.2
34  * create toggle buttons for the shuffle, repeat one, repeat all functions.
35  * implement drag and drop and item reordering.
36  * reimplement enable/disable item
37  * create a new 'tool' button (see the gear button in the Finder window) for 'actions'
38    (adding service discovery, other views, new node/playlist, save node/playlist) stuff like that
39  */
40
41
42
43 /*****************************************************************************
44  * Preamble
45  *****************************************************************************/
46 #include <stdlib.h>                                      /* malloc(), free() */
47 #include <sys/param.h>                                    /* for MAXPATHLEN */
48 #include <string.h>
49 #include <math.h>
50 #include <sys/mount.h>
51 #include <vlc_keys.h>
52
53 #include "intf.h"
54 #include "playlist.h"
55 #include "controls.h"
56 #include <OSD.h>
57
58 /*****************************************************************************
59  * VLCPlaylistView implementation 
60  *****************************************************************************/
61 @implementation VLCPlaylistView
62
63 - (NSMenu *)menuForEvent:(NSEvent *)o_event
64 {
65     return( [[self delegate] menuForEvent: o_event] );
66 }
67
68 - (void)keyDown:(NSEvent *)o_event
69 {
70     unichar key = 0;
71     int i, c, i_row;
72     NSMutableArray *o_to_delete;
73     NSNumber *o_number;
74
75     playlist_t * p_playlist;
76     intf_thread_t * p_intf = VLCIntf;
77 msg_Dbg( p_intf, "KEYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY");
78     if( [[o_event characters] length] )
79     {
80         key = [[o_event characters] characterAtIndex: 0];
81     }
82
83     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
84                                           FIND_ANYWHERE );
85
86     if ( p_playlist == NULL )
87     {
88         return;
89     }
90
91     switch( key )
92     {
93         case NSDeleteCharacter:
94         case NSDeleteFunctionKey:
95         case NSDeleteCharFunctionKey:
96         case NSBackspaceCharacter:
97             o_to_delete = [NSMutableArray arrayWithArray:[[self selectedRowEnumerator] allObjects]];
98             c = [o_to_delete count];
99
100             for( i = 0; i < c; i++ ) {
101                 o_number = [o_to_delete lastObject];
102                 i_row = [o_number intValue];
103
104                 if( p_playlist->status.p_item == [[self itemAtRow: i_row] pointerValue] && p_playlist->status.i_status )
105                 {
106                     playlist_Stop( p_playlist );
107                 }
108                 [o_to_delete removeObject: o_number];
109                 [self deselectRow: i_row];
110                 playlist_ItemDelete( [[self itemAtRow: i_row] pointerValue] );
111             }
112             break;
113
114         default:
115             [super keyDown: o_event];
116             break;
117     }
118
119     if( p_playlist != NULL )
120     {
121         vlc_object_release( p_playlist );
122     }
123 }
124
125
126 @end
127
128 /*****************************************************************************
129  * VLCPlaylist implementation 
130  *****************************************************************************/
131 @implementation VLCPlaylist
132
133 - (id)init
134 {
135     self = [super init];
136     if ( self !=nil )
137     {
138         //i_moveRow = -1;
139     }
140     return self;
141 }
142
143 - (void)awakeFromNib
144 {
145     [o_outline_view setTarget: self];
146     [o_outline_view setDelegate: self];
147     [o_outline_view setDataSource: self];
148
149     [o_outline_view setDoubleAction: @selector(playItem:)];
150
151     [o_outline_view registerForDraggedTypes: 
152         [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
153     [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
154
155 /* We need to check whether _defaultTableHeaderSortImage exists, since it 
156 belongs to an Apple hidden private API, and then can "disapear" at any time*/
157
158     if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderSortImage)] )
159     {
160         o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
161     }
162     else
163     {
164         o_ascendingSortingImage = nil;
165     }
166
167     if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderReverseSortImage)] )
168     {
169         o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
170     }
171     else
172     {
173         o_descendingSortingImage = nil;
174     }
175
176     o_outline_dict = [[NSMutableDictionary alloc] init];
177
178     [self initStrings];
179     //[self playlistUpdated];
180 }
181
182 - (void)initStrings
183 {
184     [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
185 #if 0
186     [o_mi_play setTitle: _NS("Play")];
187     [o_mi_delete setTitle: _NS("Delete")];
188     [o_mi_selectall setTitle: _NS("Select All")];
189     [o_mi_toggleItemsEnabled setTitle: _NS("Item Enabled")];
190     [o_mi_enableGroup setTitle: _NS("Enable all group items")];
191     [o_mi_disableGroup setTitle: _NS("Disable all group items")];
192     [o_mi_info setTitle: _NS("Properties")];
193 #endif
194     [[o_tc_name headerCell] setStringValue:_NS("Name")];
195     [[o_tc_author headerCell] setStringValue:_NS("Author")];
196     [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
197     [o_random_ckb setTitle: _NS("Random")];
198 #if 0
199     [o_search_button setTitle: _NS("Search")];
200 #endif
201     [o_btn_playlist setToolTip: _NS("Playlist")];
202     [[o_loop_popup itemAtIndex:0] setTitle: _NS("Standard Play")];
203     [[o_loop_popup itemAtIndex:1] setTitle: _NS("Repeat One")];
204     [[o_loop_popup itemAtIndex:2] setTitle: _NS("Repeat All")];
205 }
206
207 - (void)playlistUpdated
208 {
209     [o_outline_view reloadData];
210 }
211
212 - (IBAction)playItem:(id)sender
213 {
214     intf_thread_t * p_intf = VLCIntf;
215     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
216                                                        FIND_ANYWHERE );
217
218     if( p_playlist != NULL )
219     {
220         playlist_item_t *p_item;
221         playlist_view_t *p_view;
222         p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
223         p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
224         
225         if( p_item )
226             playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VIEW_SIMPLE, p_view ? p_view->p_root : NULL, p_item );
227         vlc_object_release( p_playlist );
228     }
229 }
230
231 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
232 {
233     int i_item;
234     intf_thread_t * p_intf = VLCIntf;
235     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
236                                                        FIND_ANYWHERE );
237
238     if( p_playlist == NULL )
239     {
240         return;
241     }
242
243     for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
244     {
245         /* One item */
246         NSDictionary *o_one_item;
247         int j, i_total_options = 0, i_new_id = -1;
248         int i_mode = PLAYLIST_INSERT;
249         BOOL b_rem = FALSE, b_dir = FALSE;
250         NSString *o_uri, *o_name;
251         NSArray *o_options;
252         NSURL *o_true_file;
253         char **ppsz_options = NULL;
254
255         /* Get the item */
256         o_one_item = [o_array objectAtIndex: i_item];
257         o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
258         o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
259         o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
260
261         /* If no name, then make a guess */
262         if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
263
264         if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
265             [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
266                     isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem   )
267         {
268             /* All of this is to make sure CD's play when you D&D them on VLC */
269             /* Converts mountpoint to a /dev file */
270             struct statfs *buf;
271             char *psz_dev;
272             buf = (struct statfs *) malloc (sizeof(struct statfs));
273             statfs( [o_uri fileSystemRepresentation], buf );
274             psz_dev = strdup(buf->f_mntfromname);
275             o_uri = [NSString stringWithCString: psz_dev ];
276         }
277
278         if( o_options && [o_options count] > 0 )
279         {
280             /* Count the input options */
281             i_total_options = [o_options count];
282
283             /* Allocate ppsz_options */
284             for( j = 0; j < i_total_options; j++ )
285             {
286                 if( !ppsz_options )
287                     ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
288
289                 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
290             }
291         }
292
293         /* Add the item */
294         i_new_id = playlist_AddExt( p_playlist, [o_uri fileSystemRepresentation],
295                       [o_name UTF8String], i_mode,
296                       i_position == -1 ? PLAYLIST_END : i_position + i_item,
297                       0, (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options );
298
299         /* clean up
300         for( j = 0; j < i_total_options; j++ )
301             free( ppsz_options[j] );
302         if( ppsz_options ) free( ppsz_options ); */
303
304         /* Recent documents menu */
305         o_true_file = [NSURL fileURLWithPath: o_uri];
306         if( o_true_file != nil )
307         {
308             [[NSDocumentController sharedDocumentController]
309                 noteNewRecentDocumentURL: o_true_file];
310         }
311
312         if( i_item == 0 && !b_enqueue )
313         {
314             playlist_Goto( p_playlist, playlist_GetPositionById( p_playlist, i_new_id ) );
315             playlist_Play( p_playlist );
316         }
317     }
318
319     vlc_object_release( p_playlist );
320 }
321
322 - (IBAction)handlePopUp:(id)sender
323
324 {
325              intf_thread_t * p_intf = VLCIntf;
326              vlc_value_t val1,val2;
327              playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
328                                                         FIND_ANYWHERE );
329              if( p_playlist == NULL )
330              {
331                  return;
332              }
333
334     switch ([o_loop_popup indexOfSelectedItem])
335     {
336         case 1:
337
338              val1.b_bool = 0;
339              var_Set( p_playlist, "loop", val1 );
340              val1.b_bool = 1;
341              var_Set( p_playlist, "repeat", val1 );
342              vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat One" ) );
343         break;
344
345         case 2:
346              val1.b_bool = 0;
347              var_Set( p_playlist, "repeat", val1 );
348              val1.b_bool = 1;
349              var_Set( p_playlist, "loop", val1 );
350              vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat All" ) );
351         break;
352
353         default:
354              var_Get( p_playlist, "repeat", &val1 );
355              var_Get( p_playlist, "loop", &val2 );
356              if (val1.b_bool || val2.b_bool)
357              {
358                   val1.b_bool = 0;
359                   var_Set( p_playlist, "repeat", val1 );
360                   var_Set( p_playlist, "loop", val1 );
361                   vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
362              }
363          break;
364      }
365      vlc_object_release( p_playlist );
366      [self playlistUpdated];
367 }
368
369 - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
370 {
371     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
372                                                        FIND_ANYWHERE );
373     playlist_item_t * p_selected_item;
374     int i_current, i_selected_row;
375
376     if (!p_playlist)
377         return NULL;
378
379     i_selected_row = [o_outline_view selectedRow];
380     if (i_selected_row < 0)
381         i_selected_row = 0;
382
383     p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow:
384                                             i_selected_row] pointerValue];
385
386     for (i_current = 0; i_current < p_item->i_children ; i_current++)
387     {
388         char * psz_temp;
389         NSString * o_current_name, * o_current_author;
390
391         vlc_mutex_lock( &p_playlist->object_lock );
392         o_current_name = [NSString stringWithUTF8String:
393             p_item->pp_children[i_current]->input.psz_name];
394         psz_temp = playlist_ItemGetInfo(p_item ,_("Meta-information"),_("Author") );
395         o_current_author = [NSString stringWithUTF8String: psz_temp];
396         free( psz_temp);
397         vlc_mutex_unlock( &p_playlist->object_lock );
398
399         if (p_selected_item == p_item->pp_children[i_current] &&
400                     b_selected_item_met == NO)
401         {
402             b_selected_item_met = YES;
403         }
404         else if (p_selected_item == p_item->pp_children[i_current] &&
405                     b_selected_item_met == YES)
406         {
407             vlc_object_release(p_playlist);
408             return NULL;
409         }
410         else if (b_selected_item_met == YES &&
411                     ([o_current_name rangeOfString:[o_search_field
412                         stringValue] options:NSCaseInsensitiveSearch ].length ||
413                     [o_current_author rangeOfString:[o_search_field
414                         stringValue] options:NSCaseInsensitiveSearch ].length))
415         {
416             vlc_object_release(p_playlist);
417             /*Adds the parent items in the result array as well, so that we can
418             expand the tree*/
419             return [NSMutableArray arrayWithObject: [NSValue
420                             valueWithPointer: p_item->pp_children[i_current]]];
421         }
422         if (p_item->pp_children[i_current]->i_children > 0)
423         {
424             id o_result = [self subSearchItem:
425                                             p_item->pp_children[i_current]];
426             if (o_result != NULL)
427             {
428                 vlc_object_release(p_playlist);
429                 [o_result insertObject: [NSValue valueWithPointer:
430                                 p_item->pp_children[i_current]] atIndex:0];
431                 return o_result;
432             }
433         }
434     }
435     vlc_object_release(p_playlist);
436     return NULL;
437 }
438
439 - (IBAction)searchItem:(id)sender
440 {
441     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
442                                                        FIND_ANYWHERE );
443     playlist_view_t * p_view;
444     id o_result;
445
446     unsigned int i;
447     int i_row = -1;
448
449     b_selected_item_met = NO;
450
451     if( p_playlist == NULL )
452         return;
453
454     p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
455
456     if (p_view)
457     {
458         /*First, only search after the selected item:*
459          *(b_selected_item_met = NO)                 */
460         o_result = [self subSearchItem:p_view->p_root];
461         if (o_result == NULL)
462         {
463             /* If the first search failed, search again from the beginning */
464             o_result = [self subSearchItem:p_view->p_root];
465         }
466         if (o_result != NULL)
467         {
468             for (i = 0 ; i < [o_result count] - 1 ; i++)
469             {
470                 [o_outline_view expandItem: [o_outline_dict objectForKey:
471                             [NSString stringWithFormat: @"%p",
472                             [[o_result objectAtIndex: i] pointerValue]]]];
473             }
474             i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
475                             [NSString stringWithFormat: @"%p",
476                             [[o_result objectAtIndex: [o_result count] - 1 ]
477                             pointerValue]]]];
478         }
479         if (i_row > -1)
480         {
481             [o_outline_view selectRow:i_row byExtendingSelection: NO];
482             [o_outline_view scrollRowToVisible: i_row];
483         }
484     }
485     vlc_object_release(p_playlist);
486
487 }
488
489 @end
490
491 @implementation VLCPlaylist (NSOutlineViewDataSource)
492
493 /* return the number of children for Obj-C pointer item */ /* DONE */
494 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
495 {
496     int i_return = 0;
497     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
498                                                        FIND_ANYWHERE );
499     if( p_playlist == NULL )
500         return 0;
501
502     if( item == nil )
503     {
504         /* root object */
505         playlist_view_t *p_view;
506         p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
507         if( p_view && p_view->p_root )
508             i_return = p_view->p_root->i_children;
509     }
510     else
511     {
512         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
513         if( p_item )
514             i_return = p_item->i_children;
515     }
516     vlc_object_release( p_playlist );
517     if( i_return == -1 ) i_return = 0;
518     msg_Dbg( p_playlist, "I have %d children", i_return );
519     return i_return;
520 }
521
522 /* return the child at index for the Obj-C pointer item */ /* DONE */
523 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
524 {
525     playlist_item_t *p_return = NULL;
526     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
527                                                        FIND_ANYWHERE );
528     NSValue * o_value;
529
530     if( p_playlist == NULL )
531         return nil;
532
533     if( item == nil )
534     {
535         /* root object */
536         playlist_view_t *p_view;
537         p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
538         if( p_view && index < p_view->p_root->i_children )
539             p_return = p_view->p_root->pp_children[index];
540     }
541     else
542     {
543         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
544         if( p_item && index < p_item->i_children )
545         {
546             p_return = p_item->pp_children[index];
547         }
548     }
549
550     vlc_object_release( p_playlist );
551     msg_Dbg( p_playlist, "childitem with index %d", index );
552
553     o_value = [NSValue valueWithPointer: p_return];
554
555     [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p",                                                                      p_return]];
556 //    [o_status_field setStringValue: [NSString stringWithFormat:
557 //                        _NS("%i items in playlist"), [o_outline_dict count]]];
558
559     return o_value;
560
561 }
562
563 /* is the item expandable */
564 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
565 {
566     int i_return = 0;
567     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
568                                                        FIND_ANYWHERE );
569     if( p_playlist == NULL )
570         return NO;
571
572     if( item == nil )
573     {
574         /* root object */
575         playlist_view_t *p_view;
576         p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
577         if( p_view && p_view->p_root )
578             i_return = p_view->p_root->i_children;
579     }
580     else
581     {
582         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
583         if( p_item )
584             i_return = p_item->i_children;
585     }
586     vlc_object_release( p_playlist );
587
588     if( i_return == -1 || i_return == 0 )
589         return NO;
590     else
591         return YES;
592 }
593
594 /* retrieve the string values for the cells */
595 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
596 {
597     id o_value = nil;
598     intf_thread_t * p_intf = VLCIntf;
599     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
600                                                FIND_ANYWHERE );
601     playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
602
603     if( p_playlist == NULL || p_item == NULL )
604     {
605         return( @"error" );
606     }
607
608     if( [[o_tc identifier] isEqualToString:@"1"] )
609     {
610         o_value = [NSString stringWithUTF8String:
611             p_item->input.psz_name];
612         if( o_value == NULL )
613             o_value = [NSString stringWithCString:
614                 p_item->input.psz_name];
615     }
616     else if( [[o_tc identifier] isEqualToString:@"2"] )
617     {
618         char *psz_temp;
619         psz_temp = playlist_ItemGetInfo( p_item ,_("Meta-information"),_("Artist") );
620
621         if( psz_temp == NULL )
622             o_value = @"";
623         else
624         {
625             o_value = [NSString stringWithUTF8String: psz_temp];
626             if( o_value == NULL )
627             {
628                 o_value = [NSString stringWithCString: psz_temp];
629             }
630             free( psz_temp );
631         }
632     }
633     else if( [[o_tc identifier] isEqualToString:@"3"] )
634     {
635         char psz_duration[MSTRTIME_MAX_SIZE];
636         mtime_t dur = p_item->input.i_duration;
637         if( dur != -1 )
638         {
639             secstotimestr( psz_duration, dur/1000000 );
640             o_value = [NSString stringWithUTF8String: psz_duration];
641         }
642         else
643         {
644             o_value = @"-:--:--";
645         }
646     }
647
648     vlc_object_release( p_playlist );
649
650     return( o_value );
651 }
652
653 /* Required for drag & drop and reordering */
654 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
655 {
656     return NO;
657 }
658
659 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
660 {
661     return NSDragOperationNone;
662 }
663
664 /* Delegate method of NSWindow */
665 - (void)windowWillClose:(NSNotification *)aNotification
666 {
667     [o_btn_playlist setState: NSOffState];
668 }
669
670 @end
671
672