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