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 delete by backspace
29 * implement playlist item rightclick menu
31 * add 'icons' for different types of nodes? (http://www.cocoadev.com/index.pl?IconAndTextInTableCell)
32 * create a new 'playlist toggle' that hides the playlist and in effect give you the old controller
33 * create a new search field build with pictures from the 'regular' search field, so it can be emulated on 10.2
34 * create toggle buttons for the shuffle, repeat one, repeat all functions.
35 * implement drag and drop and item reordering.
36 * reimplement enable/disable item
37 * create a new 'tool' button (see the gear button in the Finder window) for 'actions'
38 (adding service discovery, other views, new node/playlist, save node/playlist) stuff like that
43 /*****************************************************************************
45 *****************************************************************************/
46 #include <stdlib.h> /* malloc(), free() */
47 #include <sys/param.h> /* for MAXPATHLEN */
50 #include <sys/mount.h>
58 /*****************************************************************************
59 * VLCPlaylistView implementation
60 *****************************************************************************/
61 @implementation VLCPlaylistView
63 - (NSMenu *)menuForEvent:(NSEvent *)o_event
65 return( [[self delegate] menuForEvent: o_event] );
68 - (void)keyDown:(NSEvent *)o_event
72 NSMutableArray *o_to_delete;
75 playlist_t * p_playlist;
76 intf_thread_t * p_intf = VLCIntf;
77 msg_Dbg( p_intf, "KEYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY");
78 if( [[o_event characters] length] )
80 key = [[o_event characters] characterAtIndex: 0];
83 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
86 if ( p_playlist == NULL )
93 case NSDeleteCharacter:
94 case NSDeleteFunctionKey:
95 case NSDeleteCharFunctionKey:
96 case NSBackspaceCharacter:
97 o_to_delete = [NSMutableArray arrayWithArray:[[self selectedRowEnumerator] allObjects]];
98 c = [o_to_delete count];
100 for( i = 0; i < c; i++ ) {
101 o_number = [o_to_delete lastObject];
102 i_row = [o_number intValue];
104 if( p_playlist->status.p_item == [[self itemAtRow: i_row] pointerValue] && p_playlist->status.i_status )
106 playlist_Stop( p_playlist );
108 [o_to_delete removeObject: o_number];
109 [self deselectRow: i_row];
110 playlist_ItemDelete( [[self itemAtRow: i_row] pointerValue] );
115 [super keyDown: o_event];
119 if( p_playlist != NULL )
121 vlc_object_release( p_playlist );
128 /*****************************************************************************
129 * VLCPlaylist implementation
130 *****************************************************************************/
131 @implementation VLCPlaylist
145 [o_outline_view setTarget: self];
146 [o_outline_view setDelegate: self];
147 [o_outline_view setDataSource: self];
149 [o_outline_view setDoubleAction: @selector(playItem:)];
151 [o_outline_view registerForDraggedTypes:
152 [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
153 [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
155 /* We need to check whether _defaultTableHeaderSortImage exists, since it
156 belongs to an Apple hidden private API, and then can "disapear" at any time*/
158 if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderSortImage)] )
160 o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
164 o_ascendingSortingImage = nil;
167 if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderReverseSortImage)] )
169 o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
173 o_descendingSortingImage = nil;
177 //[self playlistUpdated];
183 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
184 [o_mi_play setTitle: _NS("Play")];
185 [o_mi_delete setTitle: _NS("Delete")];
186 [o_mi_selectall setTitle: _NS("Select All")];
187 [o_mi_toggleItemsEnabled setTitle: _NS("Item Enabled")];
188 [o_mi_enableGroup setTitle: _NS("Enable all group items")];
189 [o_mi_disableGroup setTitle: _NS("Disable all group items")];
190 [o_mi_info setTitle: _NS("Properties")];
192 [[o_tc_name headerCell] setStringValue:_NS("Name")];
193 [[o_tc_author headerCell] setStringValue:_NS("Author")];
194 [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
196 [o_random_ckb setTitle: _NS("Random")];
197 [o_search_button setTitle: _NS("Search")];
199 [o_btn_playlist setToolTip: _NS("Playlist")];
201 [[o_loop_popup itemAtIndex:0] setTitle: _NS("Standard Play")];
202 [[o_loop_popup itemAtIndex:1] setTitle: _NS("Repeat One")];
203 [[o_loop_popup itemAtIndex:2] setTitle: _NS("Repeat All")];
207 - (void)playlistUpdated
209 [o_outline_view reloadData];
212 - (IBAction)playItem:(id)sender
214 intf_thread_t * p_intf = VLCIntf;
215 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
218 if( p_playlist != NULL )
220 playlist_item_t *p_item;
221 playlist_view_t *p_view;
222 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
223 p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
226 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VIEW_SIMPLE, p_view ? p_view->p_root : NULL, p_item );
227 vlc_object_release( p_playlist );
231 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
234 intf_thread_t * p_intf = VLCIntf;
235 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
238 if( p_playlist == NULL )
243 for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
246 NSDictionary *o_one_item;
247 int j, i_total_options = 0, i_new_id = -1;
248 int i_mode = PLAYLIST_INSERT;
249 BOOL b_rem = FALSE, b_dir = FALSE;
250 NSString *o_uri, *o_name;
253 char **ppsz_options = NULL;
256 o_one_item = [o_array objectAtIndex: i_item];
257 o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
258 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
259 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
261 /* If no name, then make a guess */
262 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
264 if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
265 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
266 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
268 /* All of this is to make sure CD's play when you D&D them on VLC */
269 /* Converts mountpoint to a /dev file */
272 buf = (struct statfs *) malloc (sizeof(struct statfs));
273 statfs( [o_uri fileSystemRepresentation], buf );
274 psz_dev = strdup(buf->f_mntfromname);
275 o_uri = [NSString stringWithCString: psz_dev ];
278 if( o_options && [o_options count] > 0 )
280 /* Count the input options */
281 i_total_options = [o_options count];
283 /* Allocate ppsz_options */
284 for( j = 0; j < i_total_options; j++ )
287 ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
289 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
294 i_new_id = playlist_AddExt( p_playlist, [o_uri fileSystemRepresentation],
295 [o_name UTF8String], i_mode,
296 i_position == -1 ? PLAYLIST_END : i_position + i_item,
297 0, (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options );
300 for( j = 0; j < i_total_options; j++ )
301 free( ppsz_options[j] );
302 if( ppsz_options ) free( ppsz_options ); */
304 /* Recent documents menu */
305 o_true_file = [NSURL fileURLWithPath: o_uri];
306 if( o_true_file != nil )
308 [[NSDocumentController sharedDocumentController]
309 noteNewRecentDocumentURL: o_true_file];
312 if( i_item == 0 && !b_enqueue )
314 playlist_Goto( p_playlist, playlist_GetPositionById( p_playlist, i_new_id ) );
315 playlist_Play( p_playlist );
319 vlc_object_release( p_playlist );
324 @implementation VLCPlaylist (NSOutlineViewDataSource)
326 /* return the number of children for Obj-C pointer item */ /* DONE */
327 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
330 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
332 if( p_playlist == NULL )
338 playlist_view_t *p_view;
339 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
340 if( p_view && p_view->p_root )
341 i_return = p_view->p_root->i_children;
345 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
347 i_return = p_item->i_children;
349 vlc_object_release( p_playlist );
350 if( i_return == -1 ) i_return = 0;
351 msg_Dbg( p_playlist, "I have %d children", i_return );
355 /* return the child at index for the Obj-C pointer item */ /* DONE */
356 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
358 playlist_item_t *p_return = NULL;
359 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
361 if( p_playlist == NULL )
367 playlist_view_t *p_view;
368 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
369 if( p_view && index < p_view->p_root->i_children )
370 p_return = p_view->p_root->pp_children[index];
374 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
375 if( p_item && index < p_item->i_children )
377 p_return = p_item->pp_children[index];
381 vlc_object_release( p_playlist );
382 msg_Dbg( p_playlist, "childitem with index %d", index );
383 return [[NSValue valueWithPointer: p_return] retain];
386 /* is the item expandable */
387 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
390 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
392 if( p_playlist == NULL )
398 playlist_view_t *p_view;
399 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
400 if( p_view && p_view->p_root )
401 i_return = p_view->p_root->i_children;
405 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
407 i_return = p_item->i_children;
409 vlc_object_release( p_playlist );
411 if( i_return == -1 || i_return == 0 )
417 /* retrieve the string values for the cells */
418 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
421 intf_thread_t * p_intf = VLCIntf;
422 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
424 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
426 if( p_playlist == NULL || p_item == NULL )
431 if( [[o_tc identifier] isEqualToString:@"1"] )
433 o_value = [NSString stringWithUTF8String:
434 p_item->input.psz_name];
435 if( o_value == NULL )
436 o_value = [NSString stringWithCString:
437 p_item->input.psz_name];
439 else if( [[o_tc identifier] isEqualToString:@"2"] )
442 psz_temp = playlist_ItemGetInfo( p_item ,_("Meta-information"),_("Artist") );
444 if( psz_temp == NULL )
448 o_value = [NSString stringWithUTF8String: psz_temp];
449 if( o_value == NULL )
451 o_value = [NSString stringWithCString: psz_temp];
456 else if( [[o_tc identifier] isEqualToString:@"3"] )
458 char psz_duration[MSTRTIME_MAX_SIZE];
459 mtime_t dur = p_item->input.i_duration;
462 secstotimestr( psz_duration, dur/1000000 );
463 o_value = [NSString stringWithUTF8String: psz_duration];
467 o_value = @"-:--:--";
471 vlc_object_release( p_playlist );
476 /* Required for drag & drop and reordering */
477 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
482 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
484 return NSDragOperationNone;
487 /* Delegate method of NSWindow */
488 - (void)windowWillClose:(NSNotification *)aNotification
490 [o_btn_playlist setState: NSOffState];