]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlist.m
* recrate some missing actions (mainly copy paste from the old 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     [self initStrings];
177     //[self playlistUpdated];
178 }
179
180 - (void)initStrings
181 {
182     [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
183 #if 0
184     [o_mi_play setTitle: _NS("Play")];
185     [o_mi_delete setTitle: _NS("Delete")];
186     [o_mi_selectall setTitle: _NS("Select All")];
187     [o_mi_toggleItemsEnabled setTitle: _NS("Item Enabled")];
188     [o_mi_enableGroup setTitle: _NS("Enable all group items")];
189     [o_mi_disableGroup setTitle: _NS("Disable all group items")];
190     [o_mi_info setTitle: _NS("Properties")];
191 #endif
192     [[o_tc_name headerCell] setStringValue:_NS("Name")];
193     [[o_tc_author headerCell] setStringValue:_NS("Author")];
194     [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
195     [o_random_ckb setTitle: _NS("Random")];
196 #if 0
197     [o_search_button setTitle: _NS("Search")];
198 #endif
199     [o_btn_playlist setToolTip: _NS("Playlist")];
200     [[o_loop_popup itemAtIndex:0] setTitle: _NS("Standard Play")];
201     [[o_loop_popup itemAtIndex:1] setTitle: _NS("Repeat One")];
202     [[o_loop_popup itemAtIndex:2] setTitle: _NS("Repeat All")];
203 }
204
205 - (void)playlistUpdated
206 {
207     [o_outline_view reloadData];
208 }
209
210 - (IBAction)playItem:(id)sender
211 {
212     intf_thread_t * p_intf = VLCIntf;
213     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
214                                                        FIND_ANYWHERE );
215
216     if( p_playlist != NULL )
217     {
218         playlist_item_t *p_item;
219         playlist_view_t *p_view;
220         p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
221         p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
222         
223         if( p_item )
224             playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VIEW_SIMPLE, p_view ? p_view->p_root : NULL, p_item );
225         vlc_object_release( p_playlist );
226     }
227 }
228
229 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
230 {
231     int i_item;
232     intf_thread_t * p_intf = VLCIntf;
233     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
234                                                        FIND_ANYWHERE );
235
236     if( p_playlist == NULL )
237     {
238         return;
239     }
240
241     for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
242     {
243         /* One item */
244         NSDictionary *o_one_item;
245         int j, i_total_options = 0, i_new_id = -1;
246         int i_mode = PLAYLIST_INSERT;
247         BOOL b_rem = FALSE, b_dir = FALSE;
248         NSString *o_uri, *o_name;
249         NSArray *o_options;
250         NSURL *o_true_file;
251         char **ppsz_options = NULL;
252
253         /* Get the item */
254         o_one_item = [o_array objectAtIndex: i_item];
255         o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
256         o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
257         o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
258
259         /* If no name, then make a guess */
260         if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
261
262         if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
263             [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
264                     isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem   )
265         {
266             /* All of this is to make sure CD's play when you D&D them on VLC */
267             /* Converts mountpoint to a /dev file */
268             struct statfs *buf;
269             char *psz_dev;
270             buf = (struct statfs *) malloc (sizeof(struct statfs));
271             statfs( [o_uri fileSystemRepresentation], buf );
272             psz_dev = strdup(buf->f_mntfromname);
273             o_uri = [NSString stringWithCString: psz_dev ];
274         }
275
276         if( o_options && [o_options count] > 0 )
277         {
278             /* Count the input options */
279             i_total_options = [o_options count];
280
281             /* Allocate ppsz_options */
282             for( j = 0; j < i_total_options; j++ )
283             {
284                 if( !ppsz_options )
285                     ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
286
287                 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
288             }
289         }
290
291         /* Add the item */
292         i_new_id = playlist_AddExt( p_playlist, [o_uri fileSystemRepresentation],
293                       [o_name UTF8String], i_mode,
294                       i_position == -1 ? PLAYLIST_END : i_position + i_item,
295                       0, (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options );
296
297         /* clean up
298         for( j = 0; j < i_total_options; j++ )
299             free( ppsz_options[j] );
300         if( ppsz_options ) free( ppsz_options ); */
301
302         /* Recent documents menu */
303         o_true_file = [NSURL fileURLWithPath: o_uri];
304         if( o_true_file != nil )
305         {
306             [[NSDocumentController sharedDocumentController]
307                 noteNewRecentDocumentURL: o_true_file];
308         }
309
310         if( i_item == 0 && !b_enqueue )
311         {
312             playlist_Goto( p_playlist, playlist_GetPositionById( p_playlist, i_new_id ) );
313             playlist_Play( p_playlist );
314         }
315     }
316
317     vlc_object_release( p_playlist );
318 }
319
320 - (IBAction)handlePopUp:(id)sender
321
322 {
323              intf_thread_t * p_intf = VLCIntf;
324              vlc_value_t val1,val2;
325              playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
326                                                         FIND_ANYWHERE );
327              if( p_playlist == NULL )
328              {
329                  return;
330              }
331
332     switch ([o_loop_popup indexOfSelectedItem])
333     {
334         case 1:
335
336              val1.b_bool = 0;
337              var_Set( p_playlist, "loop", val1 );
338              val1.b_bool = 1;
339              var_Set( p_playlist, "repeat", val1 );
340              vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat One" ) );
341         break;
342
343         case 2:
344              val1.b_bool = 0;
345              var_Set( p_playlist, "repeat", val1 );
346              val1.b_bool = 1;
347              var_Set( p_playlist, "loop", val1 );
348              vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat All" ) );
349         break;
350
351         default:
352              var_Get( p_playlist, "repeat", &val1 );
353              var_Get( p_playlist, "loop", &val2 );
354              if (val1.b_bool || val2.b_bool)
355              {
356                   val1.b_bool = 0;
357                   var_Set( p_playlist, "repeat", val1 );
358                   var_Set( p_playlist, "loop", val1 );
359                   vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
360              }
361          break;
362      }
363      vlc_object_release( p_playlist );
364      [self playlistUpdated];
365 }
366
367 @end
368
369 @implementation VLCPlaylist (NSOutlineViewDataSource)
370
371 /* return the number of children for Obj-C pointer item */ /* DONE */
372 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
373 {
374     int i_return = 0;
375     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
376                                                        FIND_ANYWHERE );
377     if( p_playlist == NULL )
378         return 0;
379
380     if( item == nil )
381     {
382         /* root object */
383         playlist_view_t *p_view;
384         p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
385         if( p_view && p_view->p_root )
386             i_return = p_view->p_root->i_children;
387     }
388     else
389     {
390         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
391         if( p_item )
392             i_return = p_item->i_children;
393     }
394     vlc_object_release( p_playlist );
395     if( i_return == -1 ) i_return = 0;
396     msg_Dbg( p_playlist, "I have %d children", i_return );
397     [o_status_field setStringValue: [NSString stringWithFormat:
398                                 _NS("%i items in playlist"), i_return]];
399     return i_return;
400 }
401
402 /* return the child at index for the Obj-C pointer item */ /* DONE */
403 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
404 {
405     playlist_item_t *p_return = NULL;
406     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
407                                                        FIND_ANYWHERE );
408     if( p_playlist == NULL )
409         return nil;
410
411     if( item == nil )
412     {
413         /* root object */
414         playlist_view_t *p_view;
415         p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
416         if( p_view && index < p_view->p_root->i_children )
417             p_return = p_view->p_root->pp_children[index];
418     }
419     else
420     {
421         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
422         if( p_item && index < p_item->i_children )
423         {
424             p_return = p_item->pp_children[index];
425         }
426     }
427
428     vlc_object_release( p_playlist );
429     msg_Dbg( p_playlist, "childitem with index %d", index );
430     return [[NSValue valueWithPointer: p_return] retain];
431 }
432
433 /* is the item expandable */
434 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
435 {
436     int i_return = 0;
437     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
438                                                        FIND_ANYWHERE );
439     if( p_playlist == NULL )
440         return NO;
441
442     if( item == nil )
443     {
444         /* root object */
445         playlist_view_t *p_view;
446         p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
447         if( p_view && p_view->p_root )
448             i_return = p_view->p_root->i_children;
449     }
450     else
451     {
452         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
453         if( p_item )
454             i_return = p_item->i_children;
455     }
456     vlc_object_release( p_playlist );
457
458     if( i_return == -1 || i_return == 0 )
459         return NO;
460     else
461         return YES;
462 }
463
464 /* retrieve the string values for the cells */
465 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
466 {
467     id o_value = nil;
468     intf_thread_t * p_intf = VLCIntf;
469     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
470                                                FIND_ANYWHERE );
471     playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
472
473     if( p_playlist == NULL || p_item == NULL )
474     {
475         return( @"error" );
476     }
477
478     if( [[o_tc identifier] isEqualToString:@"1"] )
479     {
480         o_value = [NSString stringWithUTF8String:
481             p_item->input.psz_name];
482         if( o_value == NULL )
483             o_value = [NSString stringWithCString:
484                 p_item->input.psz_name];
485     }
486     else if( [[o_tc identifier] isEqualToString:@"2"] )
487     {
488         char *psz_temp;
489         psz_temp = playlist_ItemGetInfo( p_item ,_("Meta-information"),_("Artist") );
490
491         if( psz_temp == NULL )
492             o_value = @"";
493         else
494         {
495             o_value = [NSString stringWithUTF8String: psz_temp];
496             if( o_value == NULL )
497             {
498                 o_value = [NSString stringWithCString: psz_temp];
499             }
500             free( psz_temp );
501         }
502     }
503     else if( [[o_tc identifier] isEqualToString:@"3"] )
504     {
505         char psz_duration[MSTRTIME_MAX_SIZE];
506         mtime_t dur = p_item->input.i_duration;
507         if( dur != -1 )
508         {
509             secstotimestr( psz_duration, dur/1000000 );
510             o_value = [NSString stringWithUTF8String: psz_duration];
511         }
512         else
513         {
514             o_value = @"-:--:--";
515         }
516     }
517
518     vlc_object_release( p_playlist );
519
520     return( o_value );
521 }
522
523 /* Required for drag & drop and reordering */
524 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
525 {
526     return NO;
527 }
528
529 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
530 {
531     return NSDragOperationNone;
532 }
533
534 /* Delegate method of NSWindow */
535 - (void)windowWillClose:(NSNotification *)aNotification
536 {
537     [o_btn_playlist setState: NSOffState];
538 }
539
540 @end
541
542