]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlist.m
Added random, repeat one and repeat all checkboxes to the playlist
[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.39 2003/11/16 11:21:48 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
34 #include "intf.h"
35 #include "playlist.h"
36 #include "controls.h"
37
38 /*****************************************************************************
39  * VLCPlaylistView implementation 
40  *****************************************************************************/
41 @implementation VLCPlaylistView
42
43 - (NSMenu *)menuForEvent:(NSEvent *)o_event
44 {
45     return( [[self delegate] menuForEvent: o_event] );
46 }
47
48 - (void)keyDown:(NSEvent *)o_event
49 {
50     unichar key = 0;
51     int i, c, i_row;
52     NSMutableArray *o_to_delete;
53     NSNumber *o_number;
54     
55     playlist_t * p_playlist;
56     intf_thread_t * p_intf = [NSApp getIntf];
57
58     if( [[o_event characters] length] )
59     {
60         key = [[o_event characters] characterAtIndex: 0];
61     }
62
63     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
64                                           FIND_ANYWHERE );
65     
66     if ( p_playlist == NULL )
67     {
68         return;
69     }
70     
71     switch( key )
72     {
73         case NSDeleteCharacter:
74         case NSDeleteFunctionKey:
75         case NSDeleteCharFunctionKey:
76         case NSBackspaceCharacter:
77             o_to_delete = [NSMutableArray arrayWithArray:[[self selectedRowEnumerator] allObjects]];
78             c = [o_to_delete count];
79             
80             for( i = 0; i < c; i++ ) {
81                 o_number = [o_to_delete lastObject];
82                 i_row = [o_number intValue];
83                 
84                 if( p_playlist->i_index == i_row && p_playlist->i_status )
85                 {
86                     playlist_Stop( p_playlist );
87                 }
88                 [o_to_delete removeObject: o_number];
89                 [self deselectRow: i_row];
90                 playlist_Delete( p_playlist, i_row );
91             }
92             [self reloadData];
93             break;
94             
95         default:
96             [super keyDown: o_event];
97             break;
98     }
99
100     if( p_playlist != NULL )
101     {
102         vlc_object_release( p_playlist );
103     }
104 }
105
106 @end
107
108 /*****************************************************************************
109  * VLCPlaylist implementation 
110  *****************************************************************************/
111 @implementation VLCPlaylist
112
113 - (id)init
114 {
115     self = [super init];
116     if ( self !=nil )
117     {
118         i_moveRow = -1;
119     }
120     return self;
121 }
122
123 - (void)awakeFromNib
124 {
125     [o_table_view setTarget: self];
126     [o_table_view setDelegate: self];
127     [o_table_view setDataSource: self];
128
129     [o_table_view setDoubleAction: @selector(playItem:)];
130
131     [o_table_view registerForDraggedTypes: 
132         [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
133
134     [o_window setExcludedFromWindowsMenu: TRUE];
135
136     [o_window setTitle: _NS("Playlist")];
137     [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
138     [o_mi_play setTitle: _NS("Play")];
139     [o_mi_delete setTitle: _NS("Delete")];
140     [o_mi_selectall setTitle: _NS("Select All")];
141     [[o_tc_name headerCell] setStringValue:_NS("Name")];
142     [[o_tc_author headerCell] setStringValue:_NS("Author")];
143     [o_random_ckb setTitle: _NS("Random")];
144     [o_loop_ckb setTitle: _NS("Repeat All")];
145     [o_repeat_ckb setTitle: _NS("Repeat One")];
146 }
147
148 - (BOOL)tableView:(NSTableView *)o_tv 
149                   shouldEditTableColumn:(NSTableColumn *)o_tc
150                   row:(int)i_row
151 {
152     return( NO );
153 }
154
155 - (NSMenu *)menuForEvent:(NSEvent *)o_event
156 {
157     NSPoint pt;
158     vlc_bool_t b_rows;
159     vlc_bool_t b_item_sel;
160
161     pt = [o_table_view convertPoint: [o_event locationInWindow] 
162                                                  fromView: nil];
163     b_item_sel = ( [o_table_view rowAtPoint: pt] != -1 &&
164                    [o_table_view selectedRow] != -1 );
165     b_rows = [o_table_view numberOfRows] != 0;
166
167     [o_mi_play setEnabled: b_item_sel];
168     [o_mi_delete setEnabled: b_item_sel];
169     [o_mi_selectall setEnabled: b_rows];
170
171     return( o_ctx_menu );
172 }
173
174 - (IBAction)toggleWindow:(id)sender
175 {
176     if( [o_window isVisible] )
177     {
178         [o_window orderOut:sender];
179     }
180     else
181     {
182         [o_window makeKeyAndOrderFront:sender];
183     }
184 }
185
186 - (IBAction)savePlaylist:(id)sender
187 {
188     intf_thread_t * p_intf = [NSApp getIntf];
189     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
190                                                        FIND_ANYWHERE );
191     
192     NSSavePanel *o_save_panel = [NSSavePanel savePanel];
193     NSString * o_name = [NSString stringWithFormat: @"%@.m3u", _NS("Untitled")];
194     [o_save_panel setTitle: _NS("Save Playlist")];
195     [o_save_panel setPrompt: _NS("Save")];
196
197     if( [o_save_panel runModalForDirectory: nil
198             file: o_name] == NSOKButton )
199     {
200         playlist_SaveFile( p_playlist, [[o_save_panel filename] fileSystemRepresentation] );
201     }
202
203 }
204
205 - (IBAction)playItem:(id)sender
206 {
207     intf_thread_t * p_intf = [NSApp getIntf];
208     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
209                                                        FIND_ANYWHERE );
210
211     if( p_playlist != NULL )
212     {
213         playlist_Goto( p_playlist, [o_table_view selectedRow] );
214         vlc_object_release( p_playlist );
215     }
216 }
217
218 - (IBAction)deleteItems:(id)sender
219 {
220     int i, c, i_row;
221     NSMutableArray *o_to_delete;
222     NSNumber *o_number;
223
224     intf_thread_t * p_intf = [NSApp getIntf];
225     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
226                                                        FIND_ANYWHERE );
227
228     if( p_playlist == NULL )
229     {
230         return;
231     }
232     
233     o_to_delete = [NSMutableArray arrayWithArray:[[o_table_view selectedRowEnumerator] allObjects]];
234     c = (int)[o_to_delete count];
235     
236     for( i = 0; i < c; i++ ) {
237         o_number = [o_to_delete lastObject];
238         i_row = [o_number intValue];
239         
240         if( p_playlist->i_index == i_row && p_playlist->i_status )
241         {
242             playlist_Stop( p_playlist );
243         }
244         [o_to_delete removeObject: o_number];
245         [o_table_view deselectRow: i_row];
246         playlist_Delete( p_playlist, i_row );
247     }
248
249     vlc_object_release( p_playlist );
250
251     /* this is actually duplicity, because the intf.m manage also updates the view
252      * when the playlist changes. we do this on purpose, because else there is a 
253      * delay of .5 sec or so when we delete an item */
254     [self playlistUpdated];
255     [self updateRowSelection];
256 }
257
258 - (IBAction)selectAll:(id)sender
259 {
260     [o_table_view selectAll: nil];
261 }
262
263 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
264 {
265     int i_item;
266     intf_thread_t * p_intf = [NSApp getIntf];
267     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
268                                                        FIND_ANYWHERE );
269
270     if( p_playlist == NULL )
271     {
272         return;
273     }
274
275     for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
276     {
277         /* One item */
278         NSDictionary *o_one_item;
279         int j;
280         int i_total_options = 0;
281         int i_mode = PLAYLIST_INSERT;
282         BOOL b_rem = FALSE, b_dir = FALSE;
283         NSString *o_url, *o_name;
284         NSArray *o_options;
285         char **ppsz_options = NULL;
286     
287         /* Get the item */
288         o_one_item = [o_array objectAtIndex: i_item];
289         o_url = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
290         o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
291         o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
292         
293         /* If no name, then make a guess */
294         if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_url];
295     
296         if( [[NSFileManager defaultManager] fileExistsAtPath:o_url isDirectory:&b_dir] && b_dir &&
297             [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_url isRemovable: &b_rem
298                     isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem   )
299         {
300             /* All of this is to make sure CD's play when you D&D them on VLC */
301             /* Converts mountpoint to a /dev file */
302             struct statfs *buf;
303             char *psz_dev, *temp;
304             buf = (struct statfs *) malloc (sizeof(struct statfs));
305             statfs( [o_url fileSystemRepresentation], buf );
306             psz_dev = strdup(buf->f_mntfromname);
307             free( buf );
308             temp = strrchr( psz_dev , 's' );
309             psz_dev[temp - psz_dev] = '\0';
310             o_url = [NSString stringWithCString: psz_dev ];
311         }
312     
313         if (i_item == 0 && !b_enqueue)
314             i_mode |= PLAYLIST_GO;
315     
316         if( o_options && [o_options count] > 0 )
317         {
318             /* Count the input options */
319             i_total_options = [o_options count];
320     
321             /* Allocate ppsz_options */
322             for( j = 0; j < i_total_options; j++ )
323             {
324                 if( !ppsz_options )
325                     ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
326     
327                 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
328             }
329         }
330     
331         playlist_AddExt( p_playlist, [o_url fileSystemRepresentation], [o_name UTF8String], -1, 
332             (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options,
333             i_mode, i_position == -1 ? PLAYLIST_END : i_position + i_item);
334     
335         /* clean up */
336         for( j = 0; j < i_total_options; j++ )
337             free( ppsz_options[j] );
338         if( ppsz_options ) free( ppsz_options );
339     
340         /* Recent documents menu */
341         NSURL *o_true_url = [NSURL fileURLWithPath: o_url];
342         if( o_true_url != nil )
343         { 
344             [[NSDocumentController sharedDocumentController]
345                 noteNewRecentDocumentURL: o_true_url]; 
346         }
347     }
348
349     vlc_object_release( p_playlist );
350 }
351
352 - (void)playlistUpdated
353 {
354     [o_table_view reloadData];
355 }
356
357 - (void)updateRowSelection
358 {
359     int i_row;
360
361     intf_thread_t * p_intf = [NSApp getIntf];
362     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
363                                                        FIND_ANYWHERE );
364
365     if( p_playlist == NULL )
366     {
367         return;
368     }
369
370     i_row = p_playlist->i_index;
371     vlc_object_release( p_playlist );
372
373     [o_table_view selectRow: i_row byExtendingSelection: NO];
374     [o_table_view scrollRowToVisible: i_row];
375 }
376
377 @end
378
379 @implementation VLCPlaylist (NSTableDataSource)
380
381 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
382 {
383     int i_count = 0;
384     intf_thread_t * p_intf = [NSApp getIntf];
385     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
386                                                        FIND_ANYWHERE );
387
388     if( p_playlist != NULL )
389     {
390         vlc_mutex_lock( &p_playlist->object_lock );
391         i_count = p_playlist->i_size;
392         vlc_mutex_unlock( &p_playlist->object_lock );
393         vlc_object_release( p_playlist );
394     }
395     [o_status_field setStringValue: [NSString stringWithFormat:_NS("%i items in playlist"), i_count]];
396     return( i_count );
397 }
398
399 - (id)tableView:(NSTableView *)o_tv 
400                 objectValueForTableColumn:(NSTableColumn *)o_tc 
401                 row:(int)i_row
402 {
403     id o_value = nil;
404     intf_thread_t * p_intf = [NSApp getIntf];
405     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
406                                                FIND_ANYWHERE );
407
408     if( p_playlist == NULL )
409     {
410         return( nil );
411     }
412
413     if( [[o_tc identifier] isEqualToString:@"0"] )
414     {
415         o_value = [NSString stringWithFormat:@"%i", i_row + 1];
416     }
417     else if( [[o_tc identifier] isEqualToString:@"1"] )
418     {
419         vlc_mutex_lock( &p_playlist->object_lock );
420         o_value = [[NSString stringWithUTF8String: 
421             p_playlist->pp_items[i_row]->psz_name] lastPathComponent]; 
422         vlc_mutex_unlock( &p_playlist->object_lock );
423     }
424     else if( [[o_tc identifier] isEqualToString:@"2"] )
425     {
426         vlc_mutex_lock( &p_playlist->object_lock );
427         o_value = [NSString stringWithUTF8String: 
428             p_playlist->pp_items[i_row]->psz_author]; 
429         vlc_mutex_unlock( &p_playlist->object_lock );
430     }
431
432     vlc_object_release( p_playlist );
433
434     return( o_value );
435 }
436
437 - (BOOL)tableView:(NSTableView *)o_tv
438                     writeRows:(NSArray*)o_rows
439                     toPasteboard:(NSPasteboard*)o_pasteboard 
440 {
441     int i_rows = [o_rows count];
442     NSArray *o_filenames = [NSArray array];
443     
444     [o_pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
445     [o_pasteboard setPropertyList:o_filenames forType:NSFilenamesPboardType];
446     if ( i_rows == 1 )
447     {
448         i_moveRow = [[o_rows objectAtIndex:0]intValue];
449         return YES;
450     }
451     return NO;
452 }
453
454 - (NSDragOperation)tableView:(NSTableView*)o_tv
455                     validateDrop:(id <NSDraggingInfo>)o_info
456                     proposedRow:(int)i_row
457                     proposedDropOperation:(NSTableViewDropOperation)o_operation 
458 {
459     if ( o_operation == NSTableViewDropAbove )
460     {
461         if ( i_moveRow >= 0 )
462         {
463             if ( i_row != i_moveRow )
464             {
465                 return NSDragOperationMove;
466             }
467             /* what if in the previous run, the row wasn't actually moved? 
468                then we can't drop new files on this location */
469             return NSDragOperationNone;
470         }
471         return NSDragOperationGeneric;
472     }
473     return NSDragOperationNone;
474 }
475
476 - (BOOL)tableView:(NSTableView*)o_tv
477                     acceptDrop:(id <NSDraggingInfo>)o_info
478                     row:(int)i_proposed_row
479                     dropOperation:(NSTableViewDropOperation)o_operation 
480 {
481     if (  i_moveRow >= 0 )
482     {
483         if (i_moveRow != -1 && i_proposed_row != -1)
484         {
485             intf_thread_t * p_intf = [NSApp getIntf];
486             playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
487                                                             FIND_ANYWHERE );
488         
489             if( p_playlist == NULL )
490             {
491                 i_moveRow = -1;
492                 return NO;
493             }
494     
495             playlist_Move( p_playlist, i_moveRow, i_proposed_row ); 
496         
497             vlc_object_release( p_playlist );
498         }
499         [self playlistUpdated];
500         i_moveRow = -1;
501         return YES;
502     }
503     else
504     {
505         NSPasteboard * o_pasteboard;
506         o_pasteboard = [o_info draggingPasteboard];
507         
508         if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
509         {
510             int i;
511             NSArray *o_array = [NSArray array];
512             NSArray *o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
513                         sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
514
515             for( i = 0; i < (int)[o_values count]; i++)
516             {
517                 NSDictionary *o_dic;
518                 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
519                 o_array = [o_array arrayByAddingObject: o_dic];
520             }
521             [self appendArray: o_array atPos: i_proposed_row enqueue:YES];
522             return YES;
523         }
524         return NO;
525     }
526     [self updateRowSelection];
527 }
528
529 @end
530