]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlist.m
* Fix display of playlist on OSX. Looks briljant :)
[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 /* TODO
27  * connect delegates, actions and outlets in IB
28  * implement play by double click
29  * implement delete by backspace
30  * implement playlist item rightclick menu
31  * implement sorting
32  * reconnect menu items etc
33  * add 'icons' for different types of nodes?
34  * create a new 'playlist toggle' that hides the playlist and in effect give you the old controller
35  * create a new search field build with pictures from the 'regular' search field, so it can be emulated on 10.2
36  * create toggle buttons for the shuffle, repeat one, repeat all functions.
37  * implement drag and drop and item reordering.
38  * reimplement enable/disable item
39  * create a new 'tool' button (see the gear button in the Finder window) for 'actions'
40    (adding service discovery, other views, new node/playlist, save node/playlist) stuff like that
41  */
42
43
44
45 /*****************************************************************************
46  * Preamble
47  *****************************************************************************/
48 #include <stdlib.h>                                      /* malloc(), free() */
49 #include <sys/param.h>                                    /* for MAXPATHLEN */
50 #include <string.h>
51 #include <math.h>
52 #include <sys/mount.h>
53 #include <vlc_keys.h>
54
55 #include "intf.h"
56 #include "playlist.h"
57 #include "controls.h"
58 #include <OSD.h>
59
60 /*****************************************************************************
61  * VLCPlaylistView implementation 
62  *****************************************************************************/
63 @implementation VLCPlaylistView
64
65 - (NSMenu *)menuForEvent:(NSEvent *)o_event
66 {
67     return( [[self delegate] menuForEvent: o_event] );
68 }
69
70 - (void)keyDown:(NSEvent *)o_event
71 {
72     unichar key = 0;
73     int i, c, i_row;
74     NSMutableArray *o_to_delete;
75     NSNumber *o_number;
76
77     playlist_t * p_playlist;
78     intf_thread_t * p_intf = VLCIntf;
79
80     if( [[o_event characters] length] )
81     {
82         key = [[o_event characters] characterAtIndex: 0];
83     }
84
85     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
86                                           FIND_ANYWHERE );
87
88     if ( p_playlist == NULL )
89     {
90         return;
91     }
92
93     switch( key )
94     {
95         case NSDeleteCharacter:
96         case NSDeleteFunctionKey:
97         case NSDeleteCharFunctionKey:
98         case NSBackspaceCharacter:
99             o_to_delete = [NSMutableArray arrayWithArray:[[self selectedRowEnumerator] allObjects]];
100             c = [o_to_delete count];
101
102             for( i = 0; i < c; i++ ) {
103                 o_number = [o_to_delete lastObject];
104                 i_row = [o_number intValue];
105
106                 if( p_playlist->i_index == i_row && p_playlist->status.i_status )
107                 {
108                     playlist_Stop( p_playlist );
109                 }
110                 [o_to_delete removeObject: o_number];
111                 [self deselectRow: i_row];
112                 playlist_Delete( p_playlist, i_row );
113             }
114             [self reloadData];
115             break;
116
117         default:
118             [super keyDown: o_event];
119             break;
120     }
121
122     if( p_playlist != NULL )
123     {
124         vlc_object_release( p_playlist );
125     }
126 }
127
128
129 @end
130
131 /*****************************************************************************
132  * VLCPlaylist implementation 
133  *****************************************************************************/
134 @implementation VLCPlaylist
135
136 - (id)init
137 {
138     self = [super init];
139     if ( self !=nil )
140     {
141         //i_moveRow = -1;
142     }
143     return self;
144 }
145
146 - (void)awakeFromNib
147 {
148     [o_outline_view setTarget: self];
149     [o_outline_view setDelegate: self];
150     [o_outline_view setDataSource: self];
151
152     //[o_outline_view setDoubleAction: @selector(playItem:)];
153
154     [o_outline_view registerForDraggedTypes: 
155         [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
156     [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
157
158 /* We need to check whether _defaultTableHeaderSortImage exists, since it 
159 belongs to an Apple hidden private API, and then can "disapear" at any time*/
160
161     if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderSortImage)] )
162     {
163         o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
164     }
165     else
166     {
167         o_ascendingSortingImage = nil;
168     }
169
170     if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderReverseSortImage)] )
171     {
172         o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
173     }
174     else
175     {
176         o_descendingSortingImage = nil;
177     }
178
179     [self initStrings];
180     //[self playlistUpdated];
181 }
182
183 - (void)initStrings
184 {
185 #if 0
186     [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
187     [o_mi_play setTitle: _NS("Play")];
188     [o_mi_delete setTitle: _NS("Delete")];
189     [o_mi_selectall setTitle: _NS("Select All")];
190     [o_mi_toggleItemsEnabled setTitle: _NS("Item Enabled")];
191     [o_mi_enableGroup setTitle: _NS("Enable all group items")];
192     [o_mi_disableGroup setTitle: _NS("Disable all group items")];
193     [o_mi_info setTitle: _NS("Properties")];
194 #endif
195     [[o_tc_name headerCell] setStringValue:_NS("Name")];
196     [[o_tc_author headerCell] setStringValue:_NS("Author")];
197     [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
198 #if 0
199     [o_random_ckb setTitle: _NS("Random")];
200     [o_search_button setTitle: _NS("Search")];
201 #endif
202     [o_btn_playlist setToolTip: _NS("Playlist")];
203 #if 0    
204     [[o_loop_popup itemAtIndex:0] setTitle: _NS("Standard Play")];
205     [[o_loop_popup itemAtIndex:1] setTitle: _NS("Repeat One")];
206     [[o_loop_popup itemAtIndex:2] setTitle: _NS("Repeat All")];
207 #endif
208 }
209
210 - (void)playlistUpdated
211 {
212     [o_outline_view reloadData];
213 }
214
215
216 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
217 {
218     int i_item;
219     intf_thread_t * p_intf = VLCIntf;
220     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
221                                                        FIND_ANYWHERE );
222
223     if( p_playlist == NULL )
224     {
225         return;
226     }
227
228     for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
229     {
230         /* One item */
231         NSDictionary *o_one_item;
232         int j, i_total_options = 0, i_new_id = -1;
233         int i_mode = PLAYLIST_INSERT;
234         BOOL b_rem = FALSE, b_dir = FALSE;
235         NSString *o_uri, *o_name;
236         NSArray *o_options;
237         NSURL *o_true_file;
238         char **ppsz_options = NULL;
239
240         /* Get the item */
241         o_one_item = [o_array objectAtIndex: i_item];
242         o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
243         o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
244         o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
245
246         /* If no name, then make a guess */
247         if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
248
249         if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
250             [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
251                     isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem   )
252         {
253             /* All of this is to make sure CD's play when you D&D them on VLC */
254             /* Converts mountpoint to a /dev file */
255             struct statfs *buf;
256             char *psz_dev;
257             buf = (struct statfs *) malloc (sizeof(struct statfs));
258             statfs( [o_uri fileSystemRepresentation], buf );
259             psz_dev = strdup(buf->f_mntfromname);
260             o_uri = [NSString stringWithCString: psz_dev ];
261         }
262
263         if( o_options && [o_options count] > 0 )
264         {
265             /* Count the input options */
266             i_total_options = [o_options count];
267
268             /* Allocate ppsz_options */
269             for( j = 0; j < i_total_options; j++ )
270             {
271                 if( !ppsz_options )
272                     ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
273
274                 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
275             }
276         }
277
278         /* Add the item */
279         i_new_id = playlist_AddExt( p_playlist, [o_uri fileSystemRepresentation],
280                       [o_name UTF8String], i_mode,
281                       i_position == -1 ? PLAYLIST_END : i_position + i_item,
282                       0, (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options );
283
284         /* clean up
285         for( j = 0; j < i_total_options; j++ )
286             free( ppsz_options[j] );
287         if( ppsz_options ) free( ppsz_options ); */
288
289         /* Recent documents menu */
290         o_true_file = [NSURL fileURLWithPath: o_uri];
291         if( o_true_file != nil )
292         {
293             [[NSDocumentController sharedDocumentController]
294                 noteNewRecentDocumentURL: o_true_file];
295         }
296
297         if( i_item == 0 && !b_enqueue )
298         {
299             playlist_Goto( p_playlist, playlist_GetPositionById( p_playlist, i_new_id ) );
300             playlist_Play( p_playlist );
301         }
302     }
303
304     vlc_object_release( p_playlist );
305 }
306
307 @end
308
309 @implementation VLCPlaylist (NSOutlineViewDataSource)
310
311 /* return the number of children for Obj-C pointer item */ /* DONE */
312 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
313 {
314     int i_return = 0;
315     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
316                                                        FIND_ANYWHERE );
317     if( p_playlist == NULL )
318         return 0;
319
320     if( item == nil )
321     {
322         /* root object */
323         playlist_view_t *p_view;
324         p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
325         if( p_view && p_view->p_root )
326             i_return = p_view->p_root->i_children;
327     }
328     else
329     {
330         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
331         if( p_item )
332             i_return = p_item->i_children;
333     }
334     vlc_object_release( p_playlist );
335     if( i_return == -1 ) i_return = 0;
336     msg_Dbg( p_playlist, "I have %d children", i_return );
337     return i_return;
338 }
339
340 /* return the child at index for the Obj-C pointer item */ /* DONE */
341 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
342 {
343     playlist_item_t *p_return = NULL;
344     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
345                                                        FIND_ANYWHERE );
346     if( p_playlist == NULL )
347         return nil;
348
349     if( item == nil )
350     {
351         /* root object */
352         playlist_view_t *p_view;
353         p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
354         if( p_view && index < p_view->p_root->i_children )
355             p_return = p_view->p_root->pp_children[index];
356     }
357     else
358     {
359         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
360         if( p_item && index < p_item->i_children )
361         {
362             p_return = p_item->pp_children[index];
363         }
364     }
365
366     vlc_object_release( p_playlist );
367     msg_Dbg( p_playlist, "childitem with index %d", index );
368     return [[NSValue valueWithPointer: p_return] retain];
369 }
370
371 /* is the item expandable */
372 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
373 {
374     int i_return = 0;
375     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
376                                                        FIND_ANYWHERE );
377     if( p_playlist == NULL )
378         return NO;
379
380     if( item == nil )
381     {
382         /* root object */
383         playlist_view_t *p_view;
384         p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
385         if( p_view && p_view->p_root )
386             i_return = p_view->p_root->i_children;
387     }
388     else
389     {
390         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
391         if( p_item )
392             i_return = p_item->i_children;
393     }
394     vlc_object_release( p_playlist );
395
396     if( i_return == -1 || i_return == 0 )
397         return NO;
398     else
399         return YES;
400 }
401
402 /* retrieve the string values for the cells */
403 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
404 {
405     id o_value = nil;
406     intf_thread_t * p_intf = VLCIntf;
407     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
408                                                FIND_ANYWHERE );
409     playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
410
411     if( p_playlist == NULL || p_item == NULL )
412     {
413         return( @"error" );
414     }
415
416     if( [[o_tc identifier] isEqualToString:@"1"] )
417     {
418         o_value = [NSString stringWithUTF8String:
419             p_item->input.psz_name];
420         if( o_value == NULL )
421             o_value = [NSString stringWithCString:
422                 p_item->input.psz_name];
423     }
424     else if( [[o_tc identifier] isEqualToString:@"2"] )
425     {
426         char *psz_temp;
427         psz_temp = playlist_ItemGetInfo( p_item ,_("Meta-information"),_("Artist") );
428
429         if( psz_temp == NULL )
430             o_value = @"";
431         else
432         {
433             o_value = [NSString stringWithUTF8String: psz_temp];
434             if( o_value == NULL )
435             {
436                 o_value = [NSString stringWithCString: psz_temp];
437             }
438             free( psz_temp );
439         }
440     }
441     else if( [[o_tc identifier] isEqualToString:@"3"] )
442     {
443         char psz_duration[MSTRTIME_MAX_SIZE];
444         mtime_t dur = p_item->input.i_duration;
445         if( dur != -1 )
446         {
447             secstotimestr( psz_duration, dur/1000000 );
448             o_value = [NSString stringWithUTF8String: psz_duration];
449         }
450         else
451         {
452             o_value = @"-:--:--";
453         }
454     }
455
456     vlc_object_release( p_playlist );
457
458     return( o_value );
459 }
460
461 /* Required for drag & drop and reordering */
462 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
463 {
464     return NO;
465 }
466
467 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
468 {
469     return NSDragOperationNone;
470 }
471
472 /* Delegate method of NSWindow */
473 - (void)windowWillClose:(NSNotification *)aNotification
474 {
475     [o_btn_playlist setState: NSOffState];
476 }
477
478 @end
479
480