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 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <stdlib.h> /* malloc(), free() */
30 #include <sys/param.h> /* for MAXPATHLEN */
33 #include <sys/mount.h>
41 /*****************************************************************************
42 * VLCPlaylistView implementation
43 *****************************************************************************/
44 @implementation VLCPlaylistView
46 - (NSMenu *)menuForEvent:(NSEvent *)o_event
48 return( [[self delegate] menuForEvent: o_event] );
51 - (void)keyDown:(NSEvent *)o_event
55 NSMutableArray *o_to_delete;
58 playlist_t * p_playlist;
59 intf_thread_t * p_intf = VLCIntf;
61 if( [[o_event characters] length] )
63 key = [[o_event characters] characterAtIndex: 0];
66 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
69 if ( p_playlist == NULL )
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];
83 for( i = 0; i < c; i++ ) {
84 o_number = [o_to_delete lastObject];
85 i_row = [o_number intValue];
87 if( p_playlist->i_index == i_row && p_playlist->status.i_status )
89 playlist_Stop( p_playlist );
91 [o_to_delete removeObject: o_number];
92 [self deselectRow: i_row];
93 playlist_Delete( p_playlist, i_row );
99 [super keyDown: o_event];
103 if( p_playlist != NULL )
105 vlc_object_release( p_playlist );
112 /*****************************************************************************
113 * VLCPlaylist implementation
114 *****************************************************************************/
115 @implementation VLCPlaylist
129 [o_outline_view setTarget: self];
130 [o_outline_view setDelegate: self];
131 [o_outline_view setDataSource: self];
133 //[o_outline_view setDoubleAction: @selector(playItem:)];
135 [o_outline_view registerForDraggedTypes:
136 [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
137 [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
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*/
142 if( [[NSTableView class] respondsToSelector:@selector(_defaultTableHeaderSortImage)] )
144 o_ascendingSortingImage = [[NSTableView class] _defaultTableHeaderSortImage];
148 o_ascendingSortingImage = nil;
151 if( [[NSTableView class] respondsToSelector:@selector(_defaultTableHeaderReverseSortImage)] )
153 o_descendingSortingImage = [[NSTableView class] _defaultTableHeaderReverseSortImage];
157 o_descendingSortingImage = nil;
161 [self playlistUpdated];
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")];
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")];
191 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
194 intf_thread_t * p_intf = VLCIntf;
195 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
198 if( p_playlist == NULL )
203 for ( i_item = 0; i_item < (int)[o_array count]; i_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;
213 char **ppsz_options = NULL;
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"];
221 /* If no name, then make a guess */
222 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
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 )
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 */
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 ];
238 if( o_options && [o_options count] > 0 )
240 /* Count the input options */
241 i_total_options = [o_options count];
243 /* Allocate ppsz_options */
244 for( j = 0; j < i_total_options; j++ )
247 ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
249 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
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 );
260 for( j = 0; j < i_total_options; j++ )
261 free( ppsz_options[j] );
262 if( ppsz_options ) free( ppsz_options ); */
264 /* Recent documents menu */
265 o_true_file = [NSURL fileURLWithPath: o_uri];
266 if( o_true_file != nil )
268 [[NSDocumentController sharedDocumentController]
269 noteNewRecentDocumentURL: o_true_file];
272 if( i_item == 0 && !b_enqueue )
274 playlist_Goto( p_playlist, playlist_GetPositionById( p_playlist, i_new_id ) );
275 playlist_Play( p_playlist );
279 vlc_object_release( p_playlist );
284 @implementation VLCPlaylist (NSOutlineViewDataSource)
286 /* return the number of children for Obj-C pointer item */ /* DONE */
287 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
290 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
292 if( p_playlist == NULL )
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;
305 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
307 i_return = p_item->i_children;
309 vlc_object_release( p_playlist );
310 if( i_return == -1 ) i_return = 0;
314 /* return the child at index for the Obj-C pointer item */ /* DONE */
315 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
317 playlist_item *p_return = NULL;
318 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
320 if( p_playlist == NULL )
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];
333 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
334 if( p_item && index < p_item->i_children )
336 p_return = p_item->pp_children[index];
340 vlc_object_release( p_playlist );
341 return [NSValue valueWithPointer: p_return];
344 /* is the item expandable */
345 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
348 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
350 if( p_playlist == NULL )
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;
363 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
365 i_return = p_item->i_children;
367 vlc_object_release( p_playlist );
375 /* retrieve the string values for the cells */
376 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
379 intf_thread_t * p_intf = VLCIntf;
380 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
382 playlist_item_t * p_item = [item pointerValue];
384 if( p_playlist == NULL || p_item == NULL )
389 if( [[o_tc identifier] isEqualToString:@"1"] )
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];
397 else if( [[o_tc identifier] isEqualToString:@"2"] )
400 psz_temp = playlist_GetInfo( p_item ,_("Meta-information"),_("Artist") );
402 if( psz_temp == NULL )
406 o_value = [NSString stringWithUTF8String: psz_temp];
407 if( o_value == NULL )
409 o_value = [NSString stringWithCString: psz_temp];
414 else if( [[o_tc identifier] isEqualToString:@"3"] )
416 char psz_duration[MSTRTIME_MAX_SIZE];
417 mtime_t dur = p_item->input.i_duration;
420 secstotimestr( psz_duration, dur/1000000 );
421 o_value = [NSString stringWithUTF8String: psz_duration];
425 o_value = @"-:--:--";
429 vlc_object_release( p_playlist );
434 /* Required for drag & drop and reordering */
435 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
440 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
442 return NSDragOperationNone;
445 /* Delegate method of NSWindow */
446 - (void)windowWillClose:(NSNotification *)aNotification
448 [o_btn_playlist setState: NSOffState];