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