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