]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlist.m
29bd0894115ecd09aaddcdbb60f9573edd4dda37
[vlc] / modules / gui / macosx / playlist.m
1 /*****************************************************************************
2  * playlist.m: MacOS X interface plugin
3  *****************************************************************************
4  * Copyright (C) 2002-2003 VideoLAN
5  * $Id: playlist.m,v 1.46 2003/11/20 02:39:09 hartman Exp $
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *          Derk-Jan Hartman <thedj@users.sourceforge.net>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <sys/param.h>                                    /* for MAXPATHLEN */
30 #include <string.h>
31 #include <math.h>
32 #include <sys/mount.h>
33 #include <vlc_keys.h>
34
35 #include "intf.h"
36 #include "playlist.h"
37 #include "controls.h"
38
39 /*****************************************************************************
40  * VLCPlaylistView implementation 
41  *****************************************************************************/
42 @implementation VLCPlaylistView
43
44 - (NSMenu *)menuForEvent:(NSEvent *)o_event
45 {
46     return( [[self delegate] menuForEvent: o_event] );
47 }
48
49 - (void)keyDown:(NSEvent *)o_event
50 {
51     unichar key = 0;
52     int i, c, i_row;
53     NSMutableArray *o_to_delete;
54     NSNumber *o_number;
55     
56     playlist_t * p_playlist;
57     intf_thread_t * p_intf = [NSApp getIntf];
58
59     if( [[o_event characters] length] )
60     {
61         key = [[o_event characters] characterAtIndex: 0];
62     }
63
64     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
65                                           FIND_ANYWHERE );
66     
67     if ( p_playlist == NULL )
68     {
69         return;
70     }
71     
72     switch( key )
73     {
74         case NSDeleteCharacter:
75         case NSDeleteFunctionKey:
76         case NSDeleteCharFunctionKey:
77         case NSBackspaceCharacter:
78             o_to_delete = [NSMutableArray arrayWithArray:[[self selectedRowEnumerator] allObjects]];
79             c = [o_to_delete count];
80             
81             for( i = 0; i < c; i++ ) {
82                 o_number = [o_to_delete lastObject];
83                 i_row = [o_number intValue];
84                 
85                 if( p_playlist->i_index == i_row && p_playlist->i_status )
86                 {
87                     playlist_Stop( p_playlist );
88                 }
89                 [o_to_delete removeObject: o_number];
90                 [self deselectRow: i_row];
91                 playlist_Delete( p_playlist, i_row );
92             }
93             [self reloadData];
94             break;
95             
96         default:
97             [super keyDown: o_event];
98             break;
99     }
100
101     if( p_playlist != NULL )
102     {
103         vlc_object_release( p_playlist );
104     }
105 }
106
107 @end
108
109 /*****************************************************************************
110  * VLCPlaylist implementation 
111  *****************************************************************************/
112 @implementation VLCPlaylist
113
114 - (id)init
115 {
116     self = [super init];
117     if ( self !=nil )
118     {
119         i_moveRow = -1;
120     }
121     return self;
122 }
123
124 - (void)awakeFromNib
125 {
126     [o_table_view setTarget: self];
127     [o_table_view setDelegate: self];
128     [o_table_view setDataSource: self];
129
130     [o_table_view setDoubleAction: @selector(playItem:)];
131
132     [o_table_view registerForDraggedTypes: 
133         [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
134
135     [o_window setExcludedFromWindowsMenu: TRUE];
136
137     [o_window setTitle: _NS("Playlist")];
138     [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
139     [o_mi_play setTitle: _NS("Play")];
140     [o_mi_delete setTitle: _NS("Delete")];
141     [o_mi_selectall setTitle: _NS("Select All")];
142     [[o_tc_name headerCell] setStringValue:_NS("Name")];
143     [[o_tc_author headerCell] setStringValue:_NS("Author")];
144     [o_random_ckb setTitle: _NS("Shuffle")];
145     [o_loop_ckb setTitle: _NS("Repeat Playlist")];
146     [o_repeat_ckb setTitle: _NS("Repeat Item")];
147     [o_search_button setTitle: _NS("Search")];
148 }
149
150 - (BOOL)tableView:(NSTableView *)o_tv 
151                   shouldEditTableColumn:(NSTableColumn *)o_tc
152                   row:(int)i_row
153 {
154     return( NO );
155 }
156
157 - (NSMenu *)menuForEvent:(NSEvent *)o_event
158 {
159     NSPoint pt;
160     vlc_bool_t b_rows;
161     vlc_bool_t b_item_sel;
162
163     pt = [o_table_view convertPoint: [o_event locationInWindow] 
164                                                  fromView: nil];
165     b_item_sel = ( [o_table_view rowAtPoint: pt] != -1 &&
166                    [o_table_view selectedRow] != -1 );
167     b_rows = [o_table_view numberOfRows] != 0;
168
169     [o_mi_play setEnabled: b_item_sel];
170     [o_mi_delete setEnabled: b_item_sel];
171     [o_mi_selectall setEnabled: b_rows];
172
173     return( o_ctx_menu );
174 }
175
176 - (IBAction)toggleWindow:(id)sender
177 {
178     if( [o_window isVisible] )
179     {
180         [o_window orderOut:sender];
181     }
182     else
183     {
184         [o_window makeKeyAndOrderFront:sender];
185     }
186 }
187
188 - (IBAction)savePlaylist:(id)sender
189 {
190     intf_thread_t * p_intf = [NSApp getIntf];
191     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
192                                                        FIND_ANYWHERE );
193     
194     NSSavePanel *o_save_panel = [NSSavePanel savePanel];
195     NSString * o_name = [NSString stringWithFormat: @"%@.m3u", _NS("Untitled")];
196     [o_save_panel setTitle: _NS("Save Playlist")];
197     [o_save_panel setPrompt: _NS("Save")];
198
199     if( [o_save_panel runModalForDirectory: nil
200             file: o_name] == NSOKButton )
201     {
202         playlist_SaveFile( p_playlist, [[o_save_panel filename] fileSystemRepresentation] );
203     }
204
205 }
206
207 - (IBAction)playItem:(id)sender
208 {
209     intf_thread_t * p_intf = [NSApp getIntf];
210     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
211                                                        FIND_ANYWHERE );
212
213     if( p_playlist != NULL )
214     {
215         playlist_Goto( p_playlist, [o_table_view selectedRow] );
216         vlc_object_release( p_playlist );
217     }
218 }
219
220 - (IBAction)deleteItems:(id)sender
221 {
222     int i, c, i_row;
223     NSMutableArray *o_to_delete;
224     NSNumber *o_number;
225
226     intf_thread_t * p_intf = [NSApp getIntf];
227     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
228                                                        FIND_ANYWHERE );
229
230     if( p_playlist == NULL )
231     {
232         return;
233     }
234     
235     o_to_delete = [NSMutableArray arrayWithArray:[[o_table_view selectedRowEnumerator] allObjects]];
236     c = (int)[o_to_delete count];
237     
238     for( i = 0; i < c; i++ ) {
239         o_number = [o_to_delete lastObject];
240         i_row = [o_number intValue];
241         
242         if( p_playlist->i_index == i_row && p_playlist->i_status )
243         {
244             playlist_Stop( p_playlist );
245         }
246         [o_to_delete removeObject: o_number];
247         [o_table_view deselectRow: i_row];
248         playlist_Delete( p_playlist, i_row );
249     }
250
251     vlc_object_release( p_playlist );
252
253     /* this is actually duplicity, because the intf.m manage also updates the view
254      * when the playlist changes. we do this on purpose, because else there is a 
255      * delay of .5 sec or so when we delete an item */
256     [self playlistUpdated];
257     [self updateRowSelection];
258 }
259
260 - (IBAction)selectAll:(id)sender
261 {
262     [o_table_view selectAll: nil];
263 }
264
265
266 - (IBAction)searchItem:(id)sender
267 {
268     int i_current = 0;
269     NSString *o_current_name;
270     NSString *o_current_author;
271
272     intf_thread_t * p_intf = [NSApp getIntf];
273     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
274                                                FIND_ANYWHERE );
275     
276     if( p_playlist == NULL )
277     {
278         return;
279     }
280
281     if ([o_table_view selectedRow] == [o_table_view numberOfRows]-1 )
282     {
283         i_current = 0;
284     }
285     else
286     {
287         i_current = [o_table_view selectedRow]+1; 
288     }
289
290     while (i_current != [o_table_view selectedRow])
291     {
292         vlc_mutex_lock( &p_playlist->object_lock );
293         o_current_name = [NSString stringWithUTF8String: 
294             p_playlist->pp_items[i_current]->psz_name];
295         o_current_author = [NSString stringWithUTF8String: 
296             p_playlist->pp_items[i_current]->psz_author];
297         vlc_mutex_unlock( &p_playlist->object_lock );
298
299
300         if( [o_current_name rangeOfString:[o_search_keyword stringValue] options:NSCaseInsensitiveSearch ].length ||
301              [o_current_author rangeOfString:[o_search_keyword stringValue] options:NSCaseInsensitiveSearch ].length )
302         {
303              [o_table_view selectRow: i_current byExtendingSelection: NO];
304              [o_table_view scrollRowToVisible: i_current];
305              break;
306         }
307         if ( i_current == [o_table_view numberOfRows] - 1 )
308         {
309              i_current = 0;
310         }
311         else
312         {
313              i_current++;
314         }
315     }
316     vlc_object_release( p_playlist );
317 }
318
319 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
320 {
321     int i_item;
322     intf_thread_t * p_intf = [NSApp getIntf];
323     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
324                                                        FIND_ANYWHERE );
325
326     if( p_playlist == NULL )
327     {
328         return;
329     }
330
331     for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
332     {
333         /* One item */
334         NSDictionary *o_one_item;
335         int j;
336         int i_total_options = 0;
337         int i_mode = PLAYLIST_INSERT;
338         BOOL b_rem = FALSE, b_dir = FALSE;
339         NSString *o_url, *o_name;
340         NSArray *o_options;
341         char **ppsz_options = NULL;
342     
343         /* Get the item */
344         o_one_item = [o_array objectAtIndex: i_item];
345         o_url = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
346         o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
347         o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
348         
349         /* If no name, then make a guess */
350         if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_url];
351     
352         if( [[NSFileManager defaultManager] fileExistsAtPath:o_url isDirectory:&b_dir] && b_dir &&
353             [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_url isRemovable: &b_rem
354                     isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem   )
355         {
356             /* All of this is to make sure CD's play when you D&D them on VLC */
357             /* Converts mountpoint to a /dev file */
358             struct statfs *buf;
359             char *psz_dev, *temp;
360             buf = (struct statfs *) malloc (sizeof(struct statfs));
361             statfs( [o_url fileSystemRepresentation], buf );
362             psz_dev = strdup(buf->f_mntfromname);
363             free( buf );
364             temp = strrchr( psz_dev , 's' );
365             psz_dev[temp - psz_dev] = '\0';
366             o_url = [NSString stringWithCString: psz_dev ];
367         }
368     
369         if (i_item == 0 && !b_enqueue)
370             i_mode |= PLAYLIST_GO;
371     
372         if( o_options && [o_options count] > 0 )
373         {
374             /* Count the input options */
375             i_total_options = [o_options count];
376     
377             /* Allocate ppsz_options */
378             for( j = 0; j < i_total_options; j++ )
379             {
380                 if( !ppsz_options )
381                     ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
382     
383                 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
384             }
385         }
386     
387         playlist_AddExt( p_playlist, [o_url fileSystemRepresentation], [o_name UTF8String], -1, 
388             (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options,
389             i_mode, i_position == -1 ? PLAYLIST_END : i_position + i_item);
390     
391         /* clean up */
392         for( j = 0; j < i_total_options; j++ )
393             free( ppsz_options[j] );
394         if( ppsz_options ) free( ppsz_options );
395     
396         /* Recent documents menu */
397         NSURL *o_true_url = [NSURL fileURLWithPath: o_url];
398         if( o_true_url != nil )
399         { 
400             [[NSDocumentController sharedDocumentController]
401                 noteNewRecentDocumentURL: o_true_url]; 
402         }
403     }
404
405     vlc_object_release( p_playlist );
406 }
407
408 - (void)playlistUpdated
409 {
410     vlc_value_t val;
411     intf_thread_t * p_intf = [NSApp getIntf];
412     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
413                                                        FIND_ANYWHERE );
414     if( p_playlist != NULL )
415     {
416         var_Get( p_playlist, "random", &val );
417         [o_random_ckb setState: val.b_bool];
418
419         var_Get( p_playlist, "loop", &val );
420         [o_loop_ckb setState: val.b_bool];
421
422         var_Get( p_playlist, "repeat", &val );
423         [o_repeat_ckb setState: val.b_bool];
424
425         vlc_object_release( p_playlist );
426     }
427     [o_table_view reloadData];
428 }
429
430 - (void)updateRowSelection
431 {
432     int i_row;
433
434     intf_thread_t * p_intf = [NSApp getIntf];
435     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
436                                                        FIND_ANYWHERE );
437
438     if( p_playlist == NULL )
439     {
440         return;
441     }
442
443     i_row = p_playlist->i_index;
444     vlc_object_release( p_playlist );
445
446     [o_table_view selectRow: i_row byExtendingSelection: NO];
447     [o_table_view scrollRowToVisible: i_row];
448 }
449     
450
451 @end
452
453 @implementation VLCPlaylist (NSTableDataSource)
454
455 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
456 {
457     int i_count = 0;
458     intf_thread_t * p_intf = [NSApp getIntf];
459     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
460                                                        FIND_ANYWHERE );
461
462     if( p_playlist != NULL )
463     {
464         vlc_mutex_lock( &p_playlist->object_lock );
465         i_count = p_playlist->i_size;
466         vlc_mutex_unlock( &p_playlist->object_lock );
467         vlc_object_release( p_playlist );
468     }
469     [o_status_field setStringValue: [NSString stringWithFormat:_NS("%i items in playlist"), i_count]];
470     return( i_count );
471 }
472
473 - (id)tableView:(NSTableView *)o_tv 
474                 objectValueForTableColumn:(NSTableColumn *)o_tc 
475                 row:(int)i_row
476 {
477     id o_value = nil;
478     intf_thread_t * p_intf = [NSApp getIntf];
479     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
480                                                FIND_ANYWHERE );
481
482     if( p_playlist == NULL )
483     {
484         return( nil );
485     }
486
487     if( [[o_tc identifier] isEqualToString:@"0"] )
488     {
489         o_value = [NSString stringWithFormat:@"%i", i_row + 1];
490     }
491     else if( [[o_tc identifier] isEqualToString:@"1"] )
492     {
493         vlc_mutex_lock( &p_playlist->object_lock );
494         o_value = [[NSString stringWithUTF8String: 
495             p_playlist->pp_items[i_row]->psz_name] lastPathComponent]; 
496         vlc_mutex_unlock( &p_playlist->object_lock );
497     }
498     else if( [[o_tc identifier] isEqualToString:@"2"] )
499     {
500         vlc_mutex_lock( &p_playlist->object_lock );
501         o_value = [NSString stringWithUTF8String: 
502             p_playlist->pp_items[i_row]->psz_author]; 
503         vlc_mutex_unlock( &p_playlist->object_lock );
504     }
505
506     vlc_object_release( p_playlist );
507
508     return( o_value );
509 }
510
511 - (BOOL)tableView:(NSTableView *)o_tv
512                     writeRows:(NSArray*)o_rows
513                     toPasteboard:(NSPasteboard*)o_pasteboard 
514 {
515     int i_rows = [o_rows count];
516     NSArray *o_filenames = [NSArray array];
517     
518     [o_pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
519     [o_pasteboard setPropertyList:o_filenames forType:NSFilenamesPboardType];
520     if ( i_rows == 1 )
521     {
522         i_moveRow = [[o_rows objectAtIndex:0]intValue];
523         return YES;
524     }
525     return NO;
526 }
527
528 - (NSDragOperation)tableView:(NSTableView*)o_tv
529                     validateDrop:(id <NSDraggingInfo>)o_info
530                     proposedRow:(int)i_row
531                     proposedDropOperation:(NSTableViewDropOperation)o_operation 
532 {
533     if ( o_operation == NSTableViewDropAbove )
534     {
535         if ( i_moveRow >= 0 )
536         {
537             if ( i_row != i_moveRow )
538             {
539                 return NSDragOperationMove;
540             }
541             /* what if in the previous run, the row wasn't actually moved? 
542                then we can't drop new files on this location */
543             return NSDragOperationNone;
544         }
545         return NSDragOperationGeneric;
546     }
547     return NSDragOperationNone;
548 }
549
550 - (BOOL)tableView:(NSTableView*)o_tv
551                     acceptDrop:(id <NSDraggingInfo>)o_info
552                     row:(int)i_proposed_row
553                     dropOperation:(NSTableViewDropOperation)o_operation 
554 {
555     if (  i_moveRow >= 0 )
556     {
557         if (i_moveRow != -1 && i_proposed_row != -1)
558         {
559             intf_thread_t * p_intf = [NSApp getIntf];
560             playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
561                                                             FIND_ANYWHERE );
562         
563             if( p_playlist == NULL )
564             {
565                 i_moveRow = -1;
566                 return NO;
567             }
568     
569             playlist_Move( p_playlist, i_moveRow, i_proposed_row ); 
570         
571             vlc_object_release( p_playlist );
572         }
573         [self playlistUpdated];
574         i_moveRow = -1;
575         return YES;
576     }
577     else
578     {
579         NSPasteboard * o_pasteboard;
580         o_pasteboard = [o_info draggingPasteboard];
581         
582         if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
583         {
584             int i;
585             NSArray *o_array = [NSArray array];
586             NSArray *o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
587                         sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
588
589             for( i = 0; i < (int)[o_values count]; i++)
590             {
591                 NSDictionary *o_dic;
592                 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
593                 o_array = [o_array arrayByAddingObject: o_dic];
594             }
595             [self appendArray: o_array atPos: i_proposed_row enqueue:YES];
596             return YES;
597         }
598         return NO;
599     }
600     [self updateRowSelection];
601 }
602
603 @end
604