1 /*****************************************************************************
2 * playlist.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2002-2004 VideoLAN
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>
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.
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.
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 *****************************************************************************/
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
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
45 /*****************************************************************************
47 *****************************************************************************/
48 #include <stdlib.h> /* malloc(), free() */
49 #include <sys/param.h> /* for MAXPATHLEN */
52 #include <sys/mount.h>
60 /*****************************************************************************
61 * VLCPlaylistView implementation
62 *****************************************************************************/
63 @implementation VLCPlaylistView
65 - (NSMenu *)menuForEvent:(NSEvent *)o_event
67 return( [[self delegate] menuForEvent: o_event] );
70 - (void)keyDown:(NSEvent *)o_event
74 NSMutableArray *o_to_delete;
77 playlist_t * p_playlist;
78 intf_thread_t * p_intf = VLCIntf;
80 if( [[o_event characters] length] )
82 key = [[o_event characters] characterAtIndex: 0];
85 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
88 if ( p_playlist == NULL )
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];
102 for( i = 0; i < c; i++ ) {
103 o_number = [o_to_delete lastObject];
104 i_row = [o_number intValue];
106 if( p_playlist->i_index == i_row && p_playlist->status.i_status )
108 playlist_Stop( p_playlist );
110 [o_to_delete removeObject: o_number];
111 [self deselectRow: i_row];
112 playlist_Delete( p_playlist, i_row );
118 [super keyDown: o_event];
122 if( p_playlist != NULL )
124 vlc_object_release( p_playlist );
131 /*****************************************************************************
132 * VLCPlaylist implementation
133 *****************************************************************************/
134 @implementation VLCPlaylist
148 [o_outline_view setTarget: self];
149 [o_outline_view setDelegate: self];
150 [o_outline_view setDataSource: self];
152 //[o_outline_view setDoubleAction: @selector(playItem:)];
154 [o_outline_view registerForDraggedTypes:
155 [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
156 [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
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*/
161 if( [[NSTableView class] respondsToSelector:@selector(_defaultTableHeaderSortImage)] )
163 o_ascendingSortingImage = [[NSTableView class] _defaultTableHeaderSortImage];
167 o_ascendingSortingImage = nil;
170 if( [[NSTableView class] respondsToSelector:@selector(_defaultTableHeaderReverseSortImage)] )
172 o_descendingSortingImage = [[NSTableView class] _defaultTableHeaderReverseSortImage];
176 o_descendingSortingImage = nil;
180 [self playlistUpdated];
186 [o_window setTitle: _NS("Playlist")];
187 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
188 [o_mi_play setTitle: _NS("Play")];
189 [o_mi_delete setTitle: _NS("Delete")];
190 [o_mi_selectall setTitle: _NS("Select All")];
191 [o_mi_toggleItemsEnabled setTitle: _NS("Item Enabled")];
192 [o_mi_enableGroup setTitle: _NS("Enable all group items")];
193 [o_mi_disableGroup setTitle: _NS("Disable all group items")];
194 [o_mi_info setTitle: _NS("Properties")];
196 [[o_tc_name headerCell] setStringValue:_NS("Name")];
197 [[o_tc_author headerCell] setStringValue:_NS("Author")];
198 [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
199 [o_random_ckb setTitle: _NS("Random")];
200 [o_search_button setTitle: _NS("Search")];
201 [o_btn_playlist setToolTip: _NS("Playlist")];
202 [[o_loop_popup itemAtIndex:0] setTitle: _NS("Standard Play")];
203 [[o_loop_popup itemAtIndex:1] setTitle: _NS("Repeat One")];
204 [[o_loop_popup itemAtIndex:2] setTitle: _NS("Repeat All")];
210 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
213 intf_thread_t * p_intf = VLCIntf;
214 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
217 if( p_playlist == NULL )
222 for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
225 NSDictionary *o_one_item;
226 int j, i_total_options = 0, i_new_id = -1;
227 int i_mode = PLAYLIST_INSERT;
228 BOOL b_rem = FALSE, b_dir = FALSE;
229 NSString *o_uri, *o_name;
232 char **ppsz_options = NULL;
235 o_one_item = [o_array objectAtIndex: i_item];
236 o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
237 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
238 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
240 /* If no name, then make a guess */
241 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
243 if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
244 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
245 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
247 /* All of this is to make sure CD's play when you D&D them on VLC */
248 /* Converts mountpoint to a /dev file */
251 buf = (struct statfs *) malloc (sizeof(struct statfs));
252 statfs( [o_uri fileSystemRepresentation], buf );
253 psz_dev = strdup(buf->f_mntfromname);
254 o_uri = [NSString stringWithCString: psz_dev ];
257 if( o_options && [o_options count] > 0 )
259 /* Count the input options */
260 i_total_options = [o_options count];
262 /* Allocate ppsz_options */
263 for( j = 0; j < i_total_options; j++ )
266 ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
268 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
273 i_new_id = playlist_AddExt( p_playlist, [o_uri fileSystemRepresentation],
274 [o_name UTF8String], i_mode,
275 i_position == -1 ? PLAYLIST_END : i_position + i_item,
276 0, (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options );
279 for( j = 0; j < i_total_options; j++ )
280 free( ppsz_options[j] );
281 if( ppsz_options ) free( ppsz_options ); */
283 /* Recent documents menu */
284 o_true_file = [NSURL fileURLWithPath: o_uri];
285 if( o_true_file != nil )
287 [[NSDocumentController sharedDocumentController]
288 noteNewRecentDocumentURL: o_true_file];
291 if( i_item == 0 && !b_enqueue )
293 playlist_Goto( p_playlist, playlist_GetPositionById( p_playlist, i_new_id ) );
294 playlist_Play( p_playlist );
298 vlc_object_release( p_playlist );
303 @implementation VLCPlaylist (NSOutlineViewDataSource)
305 /* return the number of children for Obj-C pointer item */ /* DONE */
306 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
309 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
311 if( p_playlist == NULL )
317 playlist_view_t *p_view;
318 p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
319 if( p_view && p_view->p_root )
320 i_return = p_view->p_root->i_children;
324 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
326 i_return = p_item->i_children;
328 vlc_object_release( p_playlist );
329 if( i_return == -1 ) i_return = 0;
333 /* return the child at index for the Obj-C pointer item */ /* DONE */
334 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
336 playlist_item *p_return = NULL;
337 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
339 if( p_playlist == NULL )
345 playlist_view_t *p_view;
346 p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
347 if( p_view && index < p_view->p_root->i_children )
348 p_return = p_view->p_root->pp_children[index];
352 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
353 if( p_item && index < p_item->i_children )
355 p_return = p_item->pp_children[index];
359 vlc_object_release( p_playlist );
360 return [NSValue valueWithPointer: p_return];
363 /* is the item expandable */
364 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
367 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
369 if( p_playlist == NULL )
375 playlist_view_t *p_view;
376 p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
377 if( p_view && p_view->p_root )
378 i_return = p_view->p_root->i_children;
382 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
384 i_return = p_item->i_children;
386 vlc_object_release( p_playlist );
394 /* retrieve the string values for the cells */
395 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
398 intf_thread_t * p_intf = VLCIntf;
399 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
401 playlist_item_t * p_item = [item pointerValue];
403 if( p_playlist == NULL || p_item == NULL )
408 if( [[o_tc identifier] isEqualToString:@"1"] )
410 o_value = [NSString stringWithUTF8String:
411 p_item->input.psz_name];
412 if( o_value == NULL )
413 o_value = [NSString stringWithCString:
414 p_item->input.psz_name];
416 else if( [[o_tc identifier] isEqualToString:@"2"] )
419 psz_temp = playlist_GetInfo( p_item ,_("Meta-information"),_("Artist") );
421 if( psz_temp == NULL )
425 o_value = [NSString stringWithUTF8String: psz_temp];
426 if( o_value == NULL )
428 o_value = [NSString stringWithCString: psz_temp];
433 else if( [[o_tc identifier] isEqualToString:@"3"] )
435 char psz_duration[MSTRTIME_MAX_SIZE];
436 mtime_t dur = p_item->input.i_duration;
439 secstotimestr( psz_duration, dur/1000000 );
440 o_value = [NSString stringWithUTF8String: psz_duration];
444 o_value = @"-:--:--";
448 vlc_object_release( p_playlist );
453 /* Required for drag & drop and reordering */
454 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
459 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
461 return NSDragOperationNone;
464 /* Delegate method of NSWindow */
465 - (void)windowWillClose:(NSNotification *)aNotification
467 [o_btn_playlist setState: NSOffState];