]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlist.m
* Start of new playlist for OSX.
[vlc] / modules / gui / macosx / playlist.m
1 /*****************************************************************************
2  * playlist.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2002-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *          Derk-Jan Hartman <hartman at videolan dot org>
9  *          Benjamin Pracht <bigben at videolab dot org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>                                      /* malloc(), free() */
30 #include <sys/param.h>                                    /* for MAXPATHLEN */
31 #include <string.h>
32 #include <math.h>
33 #include <sys/mount.h>
34 #include <vlc_keys.h>
35
36 #include "intf.h"
37 #include "playlist.h"
38 #include "controls.h"
39 #include <OSD.h>
40
41 /*****************************************************************************
42  * VLCPlaylistView implementation 
43  *****************************************************************************/
44 @implementation VLCPlaylistView
45
46 - (NSMenu *)menuForEvent:(NSEvent *)o_event
47 {
48     return( [[self delegate] menuForEvent: o_event] );
49 }
50
51 - (void)keyDown:(NSEvent *)o_event
52 {
53     unichar key = 0;
54     int i, c, i_row;
55     NSMutableArray *o_to_delete;
56     NSNumber *o_number;
57
58     playlist_t * p_playlist;
59     intf_thread_t * p_intf = VLCIntf;
60
61     if( [[o_event characters] length] )
62     {
63         key = [[o_event characters] characterAtIndex: 0];
64     }
65
66     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
67                                           FIND_ANYWHERE );
68
69     if ( p_playlist == NULL )
70     {
71         return;
72     }
73
74     switch( key )
75     {
76         case NSDeleteCharacter:
77         case NSDeleteFunctionKey:
78         case NSDeleteCharFunctionKey:
79         case NSBackspaceCharacter:
80             o_to_delete = [NSMutableArray arrayWithArray:[[self selectedRowEnumerator] allObjects]];
81             c = [o_to_delete count];
82
83             for( i = 0; i < c; i++ ) {
84                 o_number = [o_to_delete lastObject];
85                 i_row = [o_number intValue];
86
87                 if( p_playlist->i_index == i_row && p_playlist->status.i_status )
88                 {
89                     playlist_Stop( p_playlist );
90                 }
91                 [o_to_delete removeObject: o_number];
92                 [self deselectRow: i_row];
93                 playlist_Delete( p_playlist, i_row );
94             }
95             [self reloadData];
96             break;
97
98         default:
99             [super keyDown: o_event];
100             break;
101     }
102
103     if( p_playlist != NULL )
104     {
105         vlc_object_release( p_playlist );
106     }
107 }
108
109
110 @end
111
112 /*****************************************************************************
113  * VLCPlaylist implementation 
114  *****************************************************************************/
115 @implementation VLCPlaylist
116
117 - (id)init
118 {
119     self = [super init];
120     if ( self !=nil )
121     {
122         i_moveRow = -1;
123     }
124     return self;
125 }
126
127 - (void)awakeFromNib
128 {
129     [o_outline_view setTarget: self];
130     [o_outline_view setDelegate: self];
131     [o_outline_view setDataSource: self];
132
133     //[o_outline_view setDoubleAction: @selector(playItem:)];
134
135     [o_outline_view registerForDraggedTypes: 
136         [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
137     [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
138
139 /* We need to check whether _defaultTableHeaderSortImage exists, since it 
140 belongs to an Apple hidden private API, and then can "disapear" at any time*/
141
142     if( [[NSTableView class] respondsToSelector:@selector(_defaultTableHeaderSortImage)] )
143     {
144         o_ascendingSortingImage = [[NSTableView class] _defaultTableHeaderSortImage];
145     }
146     else
147     {
148         o_ascendingSortingImage = nil;
149     }
150
151     if( [[NSTableView class] respondsToSelector:@selector(_defaultTableHeaderReverseSortImage)] )
152     {
153         o_descendingSortingImage = [[NSTableView class] _defaultTableHeaderReverseSortImage];
154     }
155     else
156     {
157         o_descendingSortingImage = nil;
158     }
159
160     [self initStrings];
161     [self playlistUpdated];
162 }
163
164 - (void)initStrings
165 {
166 #if 0
167     [o_window setTitle: _NS("Playlist")];
168     [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
169     [o_mi_play setTitle: _NS("Play")];
170     [o_mi_delete setTitle: _NS("Delete")];
171     [o_mi_selectall setTitle: _NS("Select All")];
172     [o_mi_toggleItemsEnabled setTitle: _NS("Item Enabled")];
173     [o_mi_enableGroup setTitle: _NS("Enable all group items")];
174     [o_mi_disableGroup setTitle: _NS("Disable all group items")];
175     [o_mi_info setTitle: _NS("Properties")];
176
177     [[o_tc_name headerCell] setStringValue:_NS("Name")];
178     [[o_tc_author headerCell] setStringValue:_NS("Author")];
179     [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
180     [o_random_ckb setTitle: _NS("Random")];
181     [o_search_button setTitle: _NS("Search")];
182     [o_btn_playlist setToolTip: _NS("Playlist")];
183     [[o_loop_popup itemAtIndex:0] setTitle: _NS("Standard Play")];
184     [[o_loop_popup itemAtIndex:1] setTitle: _NS("Repeat One")];
185     [[o_loop_popup itemAtIndex:2] setTitle: _NS("Repeat All")];
186 #endif
187 }
188
189
190
191 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
192 {
193     int i_item;
194     intf_thread_t * p_intf = VLCIntf;
195     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
196                                                        FIND_ANYWHERE );
197
198     if( p_playlist == NULL )
199     {
200         return;
201     }
202
203     for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
204     {
205         /* One item */
206         NSDictionary *o_one_item;
207         int j, i_total_options = 0, i_new_id = -1;
208         int i_mode = PLAYLIST_INSERT;
209         BOOL b_rem = FALSE, b_dir = FALSE;
210         NSString *o_uri, *o_name;
211         NSArray *o_options;
212         NSURL *o_true_file;
213         char **ppsz_options = NULL;
214
215         /* Get the item */
216         o_one_item = [o_array objectAtIndex: i_item];
217         o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
218         o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
219         o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
220
221         /* If no name, then make a guess */
222         if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
223
224         if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
225             [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
226                     isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem   )
227         {
228             /* All of this is to make sure CD's play when you D&D them on VLC */
229             /* Converts mountpoint to a /dev file */
230             struct statfs *buf;
231             char *psz_dev;
232             buf = (struct statfs *) malloc (sizeof(struct statfs));
233             statfs( [o_uri fileSystemRepresentation], buf );
234             psz_dev = strdup(buf->f_mntfromname);
235             o_uri = [NSString stringWithCString: psz_dev ];
236         }
237
238         if( o_options && [o_options count] > 0 )
239         {
240             /* Count the input options */
241             i_total_options = [o_options count];
242
243             /* Allocate ppsz_options */
244             for( j = 0; j < i_total_options; j++ )
245             {
246                 if( !ppsz_options )
247                     ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
248
249                 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
250             }
251         }
252
253         /* Add the item */
254         i_new_id = playlist_AddExt( p_playlist, [o_uri fileSystemRepresentation],
255                       [o_name UTF8String], i_mode,
256                       i_position == -1 ? PLAYLIST_END : i_position + i_item,
257                       0, (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options );
258
259         /* clean up
260         for( j = 0; j < i_total_options; j++ )
261             free( ppsz_options[j] );
262         if( ppsz_options ) free( ppsz_options ); */
263
264         /* Recent documents menu */
265         o_true_file = [NSURL fileURLWithPath: o_uri];
266         if( o_true_file != nil )
267         {
268             [[NSDocumentController sharedDocumentController]
269                 noteNewRecentDocumentURL: o_true_file];
270         }
271
272         if( i_item == 0 && !b_enqueue )
273         {
274             playlist_Goto( p_playlist, playlist_GetPositionById( p_playlist, i_new_id ) );
275             playlist_Play( p_playlist );
276         }
277     }
278
279     vlc_object_release( p_playlist );
280 }
281
282 @end
283
284 @implementation VLCPlaylist (NSOutlineViewDataSource)
285
286 /* return the number of children for Obj-C pointer item */ /* DONE */
287 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
288 {
289     int i_return = 0;
290     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
291                                                        FIND_ANYWHERE );
292     if( p_playlist == NULL )
293         return 0;
294
295     if( item == nil )
296     {
297         /* root object */
298         playlist_view_t *p_view;
299         p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
300         if( p_view && p_view->p_root )
301             i_return = p_view->p_root->i_children;
302     }
303     else
304     {
305         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
306         if( p_item )
307             i_return = p_item->i_children;
308     }
309     vlc_object_release( p_playlist );
310     if( i_return == -1 ) i_return = 0;
311     return i_return;
312 }
313
314 /* return the child at index for the Obj-C pointer item */ /* DONE */
315 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
316 {
317     playlist_item *p_return = NULL;
318     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
319                                                        FIND_ANYWHERE );
320     if( p_playlist == NULL )
321         return 0;
322
323     if( item == nil )
324     {
325         /* root object */
326         playlist_view_t *p_view;
327         p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
328         if( p_view && index < p_view->p_root->i_children )
329             p_return = p_view->p_root->pp_children[index];
330     }
331     else
332     {
333         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
334         if( p_item && index < p_item->i_children )
335         {
336             p_return = p_item->pp_children[index];
337         }
338     }
339
340     vlc_object_release( p_playlist );
341     return [NSValue valueWithPointer: p_return];
342 }
343
344 /* is the item expandable */
345 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
346 {
347     int i_return = 0;
348     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
349                                                        FIND_ANYWHERE );
350     if( p_playlist == NULL )
351         return 0;
352
353     if( item == nil )
354     {
355         /* root object */
356         playlist_view_t *p_view;
357         p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
358         if( p_view && p_view->p_root )
359             i_return = p_view->p_root->i_children;
360     }
361     else
362     {
363         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
364         if( p_item )
365             i_return = p_item->i_children;
366     }
367     vlc_object_release( p_playlist );
368
369     if( i_return == -1 )
370         return NO;
371     else
372         return YES;
373 }
374
375 /* retrieve the string values for the cells */
376 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
377 {
378     id o_value = nil;
379     intf_thread_t * p_intf = VLCIntf;
380     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
381                                                FIND_ANYWHERE );
382     playlist_item_t * p_item = [item pointerValue];
383
384     if( p_playlist == NULL || p_item == NULL )
385     {
386         return( nil );
387     }
388
389     if( [[o_tc identifier] isEqualToString:@"1"] )
390     {
391         o_value = [NSString stringWithUTF8String:
392             p_item->input.psz_name];
393         if( o_value == NULL )
394             o_value = [NSString stringWithCString:
395                 p_item->input.psz_name];
396     }
397     else if( [[o_tc identifier] isEqualToString:@"2"] )
398     {
399         char *psz_temp;
400         psz_temp = playlist_GetInfo( p_item ,_("Meta-information"),_("Artist") );
401
402         if( psz_temp == NULL )
403             o_value = @"";
404         else
405         {
406             o_value = [NSString stringWithUTF8String: psz_temp];
407             if( o_value == NULL )
408             {
409                 o_value = [NSString stringWithCString: psz_temp];
410             }
411             free( psz_temp );
412         }
413     }
414     else if( [[o_tc identifier] isEqualToString:@"3"] )
415     {
416         char psz_duration[MSTRTIME_MAX_SIZE];
417         mtime_t dur = p_item->input.i_duration;
418         if( dur != -1 )
419         {
420             secstotimestr( psz_duration, dur/1000000 );
421             o_value = [NSString stringWithUTF8String: psz_duration];
422         }
423         else
424         {
425             o_value = @"-:--:--";
426         }
427     }
428
429     vlc_object_release( p_playlist );
430
431     return( o_value );
432 }
433
434 /* Required for drag & drop and reordering */
435 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
436 {
437     return NO;
438 }
439
440 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
441 {
442     return NSDragOperationNone;
443 }
444
445 /* Delegate method of NSWindow */
446 - (void)windowWillClose:(NSNotification *)aNotification
447 {
448     [o_btn_playlist setState: NSOffState];
449 }
450
451 @end
452
453