]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlist.m
small clean-up
[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.44 2003/11/19 14:58:54 bigben 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("Random")];
145     [o_loop_ckb setTitle: _NS("Repeat All")];
146     [o_repeat_ckb setTitle: _NS("Repeat One")];
147     [o_search_button setTitle: _NS("Search")];
148
149
150     vlc_value_t val;
151     intf_thread_t * p_intf = [NSApp getIntf];
152     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
153                                                        FIND_ANYWHERE );
154     if( p_playlist != NULL )
155     {
156         var_Get( p_playlist, "random", &val );
157         [o_random_ckb setState: val.b_bool];
158
159         var_Get( p_playlist, "loop", &val );
160         [o_loop_ckb setState: val.b_bool];
161
162         var_Get( p_playlist, "repeat", &val );
163         [o_repeat_ckb setState: val.b_bool];
164
165         vlc_object_release( p_playlist );
166     }
167 }
168
169 - (BOOL)tableView:(NSTableView *)o_tv 
170                   shouldEditTableColumn:(NSTableColumn *)o_tc
171                   row:(int)i_row
172 {
173     return( NO );
174 }
175
176 - (NSMenu *)menuForEvent:(NSEvent *)o_event
177 {
178     NSPoint pt;
179     vlc_bool_t b_rows;
180     vlc_bool_t b_item_sel;
181
182     pt = [o_table_view convertPoint: [o_event locationInWindow] 
183                                                  fromView: nil];
184     b_item_sel = ( [o_table_view rowAtPoint: pt] != -1 &&
185                    [o_table_view selectedRow] != -1 );
186     b_rows = [o_table_view numberOfRows] != 0;
187
188     [o_mi_play setEnabled: b_item_sel];
189     [o_mi_delete setEnabled: b_item_sel];
190     [o_mi_selectall setEnabled: b_rows];
191
192     return( o_ctx_menu );
193 }
194
195 - (IBAction)toggleWindow:(id)sender
196 {
197     if( [o_window isVisible] )
198     {
199         [o_window orderOut:sender];
200     }
201     else
202     {
203         [o_window makeKeyAndOrderFront:sender];
204     }
205 }
206
207 - (IBAction)savePlaylist:(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     NSSavePanel *o_save_panel = [NSSavePanel savePanel];
214     NSString * o_name = [NSString stringWithFormat: @"%@.m3u", _NS("Untitled")];
215     [o_save_panel setTitle: _NS("Save Playlist")];
216     [o_save_panel setPrompt: _NS("Save")];
217
218     if( [o_save_panel runModalForDirectory: nil
219             file: o_name] == NSOKButton )
220     {
221         playlist_SaveFile( p_playlist, [[o_save_panel filename] fileSystemRepresentation] );
222     }
223
224 }
225
226 - (IBAction)playItem:(id)sender
227 {
228     intf_thread_t * p_intf = [NSApp getIntf];
229     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
230                                                        FIND_ANYWHERE );
231
232     if( p_playlist != NULL )
233     {
234         playlist_Goto( p_playlist, [o_table_view selectedRow] );
235         vlc_object_release( p_playlist );
236     }
237 }
238
239 - (IBAction)deleteItems:(id)sender
240 {
241     int i, c, i_row;
242     NSMutableArray *o_to_delete;
243     NSNumber *o_number;
244
245     intf_thread_t * p_intf = [NSApp getIntf];
246     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
247                                                        FIND_ANYWHERE );
248
249     if( p_playlist == NULL )
250     {
251         return;
252     }
253     
254     o_to_delete = [NSMutableArray arrayWithArray:[[o_table_view selectedRowEnumerator] allObjects]];
255     c = (int)[o_to_delete count];
256     
257     for( i = 0; i < c; i++ ) {
258         o_number = [o_to_delete lastObject];
259         i_row = [o_number intValue];
260         
261         if( p_playlist->i_index == i_row && p_playlist->i_status )
262         {
263             playlist_Stop( p_playlist );
264         }
265         [o_to_delete removeObject: o_number];
266         [o_table_view deselectRow: i_row];
267         playlist_Delete( p_playlist, i_row );
268     }
269
270     vlc_object_release( p_playlist );
271
272     /* this is actually duplicity, because the intf.m manage also updates the view
273      * when the playlist changes. we do this on purpose, because else there is a 
274      * delay of .5 sec or so when we delete an item */
275     [self playlistUpdated];
276     [self updateRowSelection];
277 }
278
279 - (IBAction)selectAll:(id)sender
280 {
281     [o_table_view selectAll: nil];
282 }
283
284
285 - (IBAction)searchItem:(id)sender
286 {
287     int i_current = 0, i_counter;
288     NSString *o_current_name;
289     NSString *o_current_author;
290
291     intf_thread_t * p_intf = [NSApp getIntf];
292     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
293                                                FIND_ANYWHERE );
294     
295     if( p_playlist == NULL )
296     {
297         return;
298     }
299
300     if ([o_table_view selectedRow] == [o_table_view numberOfRows]-1 )
301     {
302         i_current = 0;
303     }
304     else
305     {
306         i_current = [o_table_view selectedRow]+1; 
307     }
308
309     while (i_current != [o_table_view selectedRow])
310     {
311         vlc_mutex_lock( &p_playlist->object_lock );
312         o_current_name = [NSString stringWithUTF8String: 
313             p_playlist->pp_items[i_current]->psz_name];
314         o_current_author = [NSString stringWithUTF8String: 
315             p_playlist->pp_items[i_current]->psz_author];
316         vlc_mutex_unlock( &p_playlist->object_lock );
317
318
319         if( [o_current_name rangeOfString:[o_search_keyword stringValue] options:NSCaseInsensitiveSearch ].length ||
320              [o_current_author rangeOfString:[o_search_keyword stringValue] options:NSCaseInsensitiveSearch ].length )
321         {
322              [o_table_view selectRow: i_current byExtendingSelection: NO];
323              [o_table_view scrollRowToVisible: i_current];
324              break;
325         }
326         if ( i_current == [o_table_view numberOfRows] - 1 )
327         {
328              i_current = 0;
329         }
330         else
331         {
332              i_current++;
333         }
334     }
335     vlc_object_release( p_playlist );
336 }
337
338 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
339 {
340     int i_item;
341     intf_thread_t * p_intf = [NSApp getIntf];
342     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
343                                                        FIND_ANYWHERE );
344
345     if( p_playlist == NULL )
346     {
347         return;
348     }
349
350     for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
351     {
352         /* One item */
353         NSDictionary *o_one_item;
354         int j;
355         int i_total_options = 0;
356         int i_mode = PLAYLIST_INSERT;
357         BOOL b_rem = FALSE, b_dir = FALSE;
358         NSString *o_url, *o_name;
359         NSArray *o_options;
360         char **ppsz_options = NULL;
361     
362         /* Get the item */
363         o_one_item = [o_array objectAtIndex: i_item];
364         o_url = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
365         o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
366         o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
367         
368         /* If no name, then make a guess */
369         if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_url];
370     
371         if( [[NSFileManager defaultManager] fileExistsAtPath:o_url isDirectory:&b_dir] && b_dir &&
372             [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_url isRemovable: &b_rem
373                     isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem   )
374         {
375             /* All of this is to make sure CD's play when you D&D them on VLC */
376             /* Converts mountpoint to a /dev file */
377             struct statfs *buf;
378             char *psz_dev, *temp;
379             buf = (struct statfs *) malloc (sizeof(struct statfs));
380             statfs( [o_url fileSystemRepresentation], buf );
381             psz_dev = strdup(buf->f_mntfromname);
382             free( buf );
383             temp = strrchr( psz_dev , 's' );
384             psz_dev[temp - psz_dev] = '\0';
385             o_url = [NSString stringWithCString: psz_dev ];
386         }
387     
388         if (i_item == 0 && !b_enqueue)
389             i_mode |= PLAYLIST_GO;
390     
391         if( o_options && [o_options count] > 0 )
392         {
393             /* Count the input options */
394             i_total_options = [o_options count];
395     
396             /* Allocate ppsz_options */
397             for( j = 0; j < i_total_options; j++ )
398             {
399                 if( !ppsz_options )
400                     ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
401     
402                 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
403             }
404         }
405     
406         playlist_AddExt( p_playlist, [o_url fileSystemRepresentation], [o_name UTF8String], -1, 
407             (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options,
408             i_mode, i_position == -1 ? PLAYLIST_END : i_position + i_item);
409     
410         /* clean up */
411         for( j = 0; j < i_total_options; j++ )
412             free( ppsz_options[j] );
413         if( ppsz_options ) free( ppsz_options );
414     
415         /* Recent documents menu */
416         NSURL *o_true_url = [NSURL fileURLWithPath: o_url];
417         if( o_true_url != nil )
418         { 
419             [[NSDocumentController sharedDocumentController]
420                 noteNewRecentDocumentURL: o_true_url]; 
421         }
422     }
423
424     vlc_object_release( p_playlist );
425 }
426
427 - (void)playlistUpdated
428 {
429     [o_table_view reloadData];
430 }
431
432 - (void)updateRowSelection
433 {
434     int i_row;
435
436     intf_thread_t * p_intf = [NSApp getIntf];
437     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
438                                                        FIND_ANYWHERE );
439
440     if( p_playlist == NULL )
441     {
442         return;
443     }
444
445     i_row = p_playlist->i_index;
446     vlc_object_release( p_playlist );
447
448     [o_table_view selectRow: i_row byExtendingSelection: NO];
449     [o_table_view scrollRowToVisible: i_row];
450 }
451     
452
453 @end
454
455 @implementation VLCPlaylist (NSTableDataSource)
456
457 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
458 {
459     int i_count = 0;
460     intf_thread_t * p_intf = [NSApp getIntf];
461     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
462                                                        FIND_ANYWHERE );
463
464     if( p_playlist != NULL )
465     {
466         vlc_mutex_lock( &p_playlist->object_lock );
467         i_count = p_playlist->i_size;
468         vlc_mutex_unlock( &p_playlist->object_lock );
469         vlc_object_release( p_playlist );
470     }
471     [o_status_field setStringValue: [NSString stringWithFormat:_NS("%i items in playlist"), i_count]];
472     return( i_count );
473 }
474
475 - (id)tableView:(NSTableView *)o_tv 
476                 objectValueForTableColumn:(NSTableColumn *)o_tc 
477                 row:(int)i_row
478 {
479     id o_value = nil;
480     intf_thread_t * p_intf = [NSApp getIntf];
481     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
482                                                FIND_ANYWHERE );
483
484     if( p_playlist == NULL )
485     {
486         return( nil );
487     }
488
489     if( [[o_tc identifier] isEqualToString:@"0"] )
490     {
491         o_value = [NSString stringWithFormat:@"%i", i_row + 1];
492     }
493     else if( [[o_tc identifier] isEqualToString:@"1"] )
494     {
495         vlc_mutex_lock( &p_playlist->object_lock );
496         o_value = [[NSString stringWithUTF8String: 
497             p_playlist->pp_items[i_row]->psz_name] lastPathComponent]; 
498         vlc_mutex_unlock( &p_playlist->object_lock );
499     }
500     else if( [[o_tc identifier] isEqualToString:@"2"] )
501     {
502         vlc_mutex_lock( &p_playlist->object_lock );
503         o_value = [NSString stringWithUTF8String: 
504             p_playlist->pp_items[i_row]->psz_author]; 
505         vlc_mutex_unlock( &p_playlist->object_lock );
506     }
507
508     vlc_object_release( p_playlist );
509
510     return( o_value );
511 }
512
513 - (BOOL)tableView:(NSTableView *)o_tv
514                     writeRows:(NSArray*)o_rows
515                     toPasteboard:(NSPasteboard*)o_pasteboard 
516 {
517     int i_rows = [o_rows count];
518     NSArray *o_filenames = [NSArray array];
519     
520     [o_pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
521     [o_pasteboard setPropertyList:o_filenames forType:NSFilenamesPboardType];
522     if ( i_rows == 1 )
523     {
524         i_moveRow = [[o_rows objectAtIndex:0]intValue];
525         return YES;
526     }
527     return NO;
528 }
529
530 - (NSDragOperation)tableView:(NSTableView*)o_tv
531                     validateDrop:(id <NSDraggingInfo>)o_info
532                     proposedRow:(int)i_row
533                     proposedDropOperation:(NSTableViewDropOperation)o_operation 
534 {
535     if ( o_operation == NSTableViewDropAbove )
536     {
537         if ( i_moveRow >= 0 )
538         {
539             if ( i_row != i_moveRow )
540             {
541                 return NSDragOperationMove;
542             }
543             /* what if in the previous run, the row wasn't actually moved? 
544                then we can't drop new files on this location */
545             return NSDragOperationNone;
546         }
547         return NSDragOperationGeneric;
548     }
549     return NSDragOperationNone;
550 }
551
552 - (BOOL)tableView:(NSTableView*)o_tv
553                     acceptDrop:(id <NSDraggingInfo>)o_info
554                     row:(int)i_proposed_row
555                     dropOperation:(NSTableViewDropOperation)o_operation 
556 {
557     if (  i_moveRow >= 0 )
558     {
559         if (i_moveRow != -1 && i_proposed_row != -1)
560         {
561             intf_thread_t * p_intf = [NSApp getIntf];
562             playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
563                                                             FIND_ANYWHERE );
564         
565             if( p_playlist == NULL )
566             {
567                 i_moveRow = -1;
568                 return NO;
569             }
570     
571             playlist_Move( p_playlist, i_moveRow, i_proposed_row ); 
572         
573             vlc_object_release( p_playlist );
574         }
575         [self playlistUpdated];
576         i_moveRow = -1;
577         return YES;
578     }
579     else
580     {
581         NSPasteboard * o_pasteboard;
582         o_pasteboard = [o_info draggingPasteboard];
583         
584         if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
585         {
586             int i;
587             NSArray *o_array = [NSArray array];
588             NSArray *o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
589                         sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
590
591             for( i = 0; i < (int)[o_values count]; i++)
592             {
593                 NSDictionary *o_dic;
594                 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
595                 o_array = [o_array arrayByAddingObject: o_dic];
596             }
597             [self appendArray: o_array atPos: i_proposed_row enqueue:YES];
598             return YES;
599         }
600         return NO;
601     }
602     [self updateRowSelection];
603 }
604
605 @end
606