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 if( [[o_event characters] length] )
74 key = [[o_event characters] characterAtIndex: 0];
79 case NSDeleteCharacter:
80 case NSDeleteFunctionKey:
81 case NSDeleteCharFunctionKey:
82 case NSBackspaceCharacter:
83 [[self delegate] deleteItem:self];
87 [super keyDown: o_event];
94 /*****************************************************************************
95 * VLCPlaylist implementation
96 *****************************************************************************/
97 @implementation VLCPlaylist
111 [o_outline_view setTarget: self];
112 [o_outline_view setDelegate: self];
113 [o_outline_view setDataSource: self];
115 [o_outline_view setDoubleAction: @selector(playItem:)];
117 [o_outline_view registerForDraggedTypes:
118 [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
119 [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
121 /* We need to check whether _defaultTableHeaderSortImage exists, since it
122 belongs to an Apple hidden private API, and then can "disapear" at any time*/
124 if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderSortImage)] )
126 o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
130 o_ascendingSortingImage = nil;
133 if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderReverseSortImage)] )
135 o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
139 o_descendingSortingImage = nil;
142 o_outline_dict = [[NSMutableDictionary alloc] init];
145 //[self playlistUpdated];
150 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
151 [o_mi_play setTitle: _NS("Play")];
152 [o_mi_delete setTitle: _NS("Delete")];
153 [o_mi_selectall setTitle: _NS("Select All")];
154 [o_mi_info setTitle: _NS("Properties")];
155 [[o_tc_name headerCell] setStringValue:_NS("Name")];
156 [[o_tc_author headerCell] setStringValue:_NS("Author")];
157 [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
158 [o_status_field setStringValue: [NSString stringWithFormat:
159 _NS("0 items in playlist")]];
161 [o_random_ckb setTitle: _NS("Random")];
163 [o_search_button setTitle: _NS("Search")];
165 [o_btn_playlist setToolTip: _NS("Playlist")];
166 [[o_loop_popup itemAtIndex:0] setTitle: _NS("Standard Play")];
167 [[o_loop_popup itemAtIndex:1] setTitle: _NS("Repeat One")];
168 [[o_loop_popup itemAtIndex:2] setTitle: _NS("Repeat All")];
171 - (void)playlistUpdated
173 [o_outline_dict removeAllObjects];
174 [o_outline_view reloadData];
177 - (bool)isItem:(playlist_item_t *)p_item inNode:(playlist_item_t *)p_node
179 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
181 playlist_item_t * p_temp_item = p_item;
183 if ( p_playlist == NULL )
188 while ( p_temp_item->i_parents > 0 )
191 for (i = 0; i < p_temp_item->i_parents ; i++)
193 if (p_temp_item->pp_parents[i]->i_view == VIEW_SIMPLE)
195 if (p_temp_item->pp_parents[i]->p_parent == p_node)
197 vlc_object_release(p_playlist);
202 p_temp_item = p_temp_item->pp_parents[i]->p_parent;
209 vlc_object_release(p_playlist);
214 - (IBAction)playItem:(id)sender
216 intf_thread_t * p_intf = VLCIntf;
217 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
220 if( p_playlist != NULL )
222 playlist_item_t *p_item;
223 playlist_item_t *p_node = NULL;
226 p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
230 if (p_item->i_children == -1)
232 for (i = 0 ; i < p_item->i_parents ; i++)
234 if (p_item->pp_parents[i]->i_view == VIEW_SIMPLE)
236 p_node = p_item->pp_parents[i]->p_parent;
243 if (p_node->pp_children[0]->i_children == -1 &&
244 p_node->i_children > 0)
246 p_item = p_node->pp_children[0];
254 // p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
257 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VIEW_SIMPLE, p_node, p_item );
258 // playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VIEW_SIMPLE, p_view ? p_view->p_root : NULL, p_item );
260 vlc_object_release( p_playlist );
264 - (IBAction)selectAll:(id)sender
266 [o_outline_view selectAll: nil];
269 - (IBAction)deleteItem:(id)sender
272 NSMutableArray *o_to_delete;
275 playlist_t * p_playlist;
276 intf_thread_t * p_intf = VLCIntf;
278 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
281 if ( p_playlist == NULL )
285 o_to_delete = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
286 c = [o_to_delete count];
288 for( i = 0; i < c; i++ ) {
289 playlist_item_t * p_item;
290 o_number = [o_to_delete lastObject];
291 i_row = [o_number intValue];
293 [o_to_delete removeObject: o_number];
294 [o_outline_view deselectRow: i_row];
295 p_item = (playlist_item_t *)[[o_outline_view itemAtRow: i_row]pointerValue];
296 if (p_item->i_children > -1)
298 if (p_playlist->status.i_status)
300 if ([self isItem:p_playlist->status.p_item inNode: p_item]
301 == YES && p_playlist->status.i_status)
303 playlist_Stop( p_playlist );
306 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE);
310 if( p_playlist->status.p_item == [[o_outline_view itemAtRow: i_row]
311 pointerValue] && p_playlist->status.i_status )
313 playlist_Stop( p_playlist );
315 playlist_Delete( p_playlist, p_item->input.i_id );
317 [self playlistUpdated];
321 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
324 intf_thread_t * p_intf = VLCIntf;
325 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
328 if( p_playlist == NULL )
333 for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
336 NSDictionary *o_one_item;
337 int j, i_total_options = 0, i_new_id = -1;
338 int i_mode = PLAYLIST_INSERT;
339 BOOL b_rem = FALSE, b_dir = FALSE;
340 NSString *o_uri, *o_name;
343 char **ppsz_options = NULL;
346 o_one_item = [o_array objectAtIndex: i_item];
347 o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
348 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
349 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
351 /* If no name, then make a guess */
352 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
354 if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
355 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
356 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
358 /* All of this is to make sure CD's play when you D&D them on VLC */
359 /* Converts mountpoint to a /dev file */
362 buf = (struct statfs *) malloc (sizeof(struct statfs));
363 statfs( [o_uri fileSystemRepresentation], buf );
364 psz_dev = strdup(buf->f_mntfromname);
365 o_uri = [NSString stringWithCString: psz_dev ];
368 if( o_options && [o_options count] > 0 )
370 /* Count the input options */
371 i_total_options = [o_options count];
373 /* Allocate ppsz_options */
374 for( j = 0; j < i_total_options; j++ )
377 ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
379 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
384 i_new_id = playlist_AddExt( p_playlist, [o_uri fileSystemRepresentation],
385 [o_name UTF8String], i_mode,
386 i_position == -1 ? PLAYLIST_END : i_position + i_item,
387 0, (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options );
390 for( j = 0; j < i_total_options; j++ )
391 free( ppsz_options[j] );
392 if( ppsz_options ) free( ppsz_options ); */
394 /* Recent documents menu */
395 o_true_file = [NSURL fileURLWithPath: o_uri];
396 if( o_true_file != nil )
398 [[NSDocumentController sharedDocumentController]
399 noteNewRecentDocumentURL: o_true_file];
402 if( i_item == 0 && !b_enqueue )
404 playlist_Goto( p_playlist, playlist_GetPositionById( p_playlist, i_new_id ) );
405 playlist_Play( p_playlist );
409 vlc_object_release( p_playlist );
412 - (IBAction)handlePopUp:(id)sender
415 intf_thread_t * p_intf = VLCIntf;
416 vlc_value_t val1,val2;
417 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
419 if( p_playlist == NULL )
424 switch ([o_loop_popup indexOfSelectedItem])
429 var_Set( p_playlist, "loop", val1 );
431 var_Set( p_playlist, "repeat", val1 );
432 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat One" ) );
437 var_Set( p_playlist, "repeat", val1 );
439 var_Set( p_playlist, "loop", val1 );
440 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat All" ) );
444 var_Get( p_playlist, "repeat", &val1 );
445 var_Get( p_playlist, "loop", &val2 );
446 if (val1.b_bool || val2.b_bool)
449 var_Set( p_playlist, "repeat", val1 );
450 var_Set( p_playlist, "loop", val1 );
451 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
455 vlc_object_release( p_playlist );
456 [self playlistUpdated];
459 - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
461 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
463 playlist_item_t * p_selected_item;
464 int i_current, i_selected_row;
469 i_selected_row = [o_outline_view selectedRow];
470 if (i_selected_row < 0)
473 p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow:
474 i_selected_row] pointerValue];
476 for (i_current = 0; i_current < p_item->i_children ; i_current++)
479 NSString * o_current_name, * o_current_author;
481 vlc_mutex_lock( &p_playlist->object_lock );
482 o_current_name = [NSString stringWithUTF8String:
483 p_item->pp_children[i_current]->input.psz_name];
484 psz_temp = playlist_ItemGetInfo(p_item ,_("Meta-information"),_("Author") );
485 o_current_author = [NSString stringWithUTF8String: psz_temp];
487 vlc_mutex_unlock( &p_playlist->object_lock );
489 if (p_selected_item == p_item->pp_children[i_current] &&
490 b_selected_item_met == NO)
492 b_selected_item_met = YES;
494 else if (p_selected_item == p_item->pp_children[i_current] &&
495 b_selected_item_met == YES)
497 vlc_object_release(p_playlist);
500 else if (b_selected_item_met == YES &&
501 ([o_current_name rangeOfString:[o_search_field
502 stringValue] options:NSCaseInsensitiveSearch ].length ||
503 [o_current_author rangeOfString:[o_search_field
504 stringValue] options:NSCaseInsensitiveSearch ].length))
506 vlc_object_release(p_playlist);
507 /*Adds the parent items in the result array as well, so that we can
509 return [NSMutableArray arrayWithObject: [NSValue
510 valueWithPointer: p_item->pp_children[i_current]]];
512 if (p_item->pp_children[i_current]->i_children > 0)
514 id o_result = [self subSearchItem:
515 p_item->pp_children[i_current]];
516 if (o_result != NULL)
518 vlc_object_release(p_playlist);
519 [o_result insertObject: [NSValue valueWithPointer:
520 p_item->pp_children[i_current]] atIndex:0];
525 vlc_object_release(p_playlist);
529 - (IBAction)searchItem:(id)sender
531 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
533 playlist_view_t * p_view;
539 b_selected_item_met = NO;
541 if( p_playlist == NULL )
544 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
548 /*First, only search after the selected item:*
549 *(b_selected_item_met = NO) */
550 o_result = [self subSearchItem:p_view->p_root];
551 if (o_result == NULL)
553 /* If the first search failed, search again from the beginning */
554 o_result = [self subSearchItem:p_view->p_root];
556 if (o_result != NULL)
558 for (i = 0 ; i < [o_result count] - 1 ; i++)
560 [o_outline_view expandItem: [o_outline_dict objectForKey:
561 [NSString stringWithFormat: @"%p",
562 [[o_result objectAtIndex: i] pointerValue]]]];
564 i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
565 [NSString stringWithFormat: @"%p",
566 [[o_result objectAtIndex: [o_result count] - 1 ]
571 [o_outline_view selectRow:i_row byExtendingSelection: NO];
572 [o_outline_view scrollRowToVisible: i_row];
575 vlc_object_release(p_playlist);
579 - (NSMenu *)menuForEvent:(NSEvent *)o_event
583 vlc_bool_t b_item_sel;
585 pt = [o_outline_view convertPoint: [o_event locationInWindow]
587 b_item_sel = ( [o_outline_view rowAtPoint: pt] != -1 &&
588 [o_outline_view selectedRow] != -1 );
589 b_rows = [o_outline_view numberOfRows] != 0;
591 [o_mi_play setEnabled: b_item_sel];
592 [o_mi_delete setEnabled: b_item_sel];
593 [o_mi_selectall setEnabled: b_rows];
594 [o_mi_info setEnabled: b_item_sel];
596 return( o_ctx_menu );
599 - (playlist_item_t *)selectedPlaylistItem
601 return [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
607 @implementation VLCPlaylist (NSOutlineViewDataSource)
609 /* return the number of children for Obj-C pointer item */ /* DONE */
610 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
613 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
615 if( p_playlist == NULL )
621 playlist_view_t *p_view;
622 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
623 if( p_view && p_view->p_root )
624 i_return = p_view->p_root->i_children;
628 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
630 i_return = p_item->i_children;
632 vlc_object_release( p_playlist );
633 if( i_return == -1 ) i_return = 0;
634 msg_Dbg( p_playlist, "I have %d children", i_return );
638 /* return the child at index for the Obj-C pointer item */ /* DONE */
639 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
641 playlist_item_t *p_return = NULL;
642 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
646 if( p_playlist == NULL )
652 playlist_view_t *p_view;
653 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
654 if( p_view && index < p_view->p_root->i_children )
655 p_return = p_view->p_root->pp_children[index];
659 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
660 if( p_item && index < p_item->i_children )
662 p_return = p_item->pp_children[index];
666 [o_status_field setStringValue: [NSString stringWithFormat:
667 _NS("%i items in playlist"), p_playlist->i_size]];
669 vlc_object_release( p_playlist );
670 msg_Dbg( p_playlist, "childitem with index %d", index );
672 o_value = [NSValue valueWithPointer: p_return];
674 [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p", p_return]];
678 /* is the item expandable */
679 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
682 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
684 if( p_playlist == NULL )
690 playlist_view_t *p_view;
691 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
692 if( p_view && p_view->p_root )
693 i_return = p_view->p_root->i_children;
697 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
699 i_return = p_item->i_children;
701 vlc_object_release( p_playlist );
703 if( i_return == -1 || i_return == 0 )
709 /* retrieve the string values for the cells */
710 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
713 intf_thread_t * p_intf = VLCIntf;
714 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
716 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
718 if( p_playlist == NULL || p_item == NULL )
723 if( [[o_tc identifier] isEqualToString:@"1"] )
725 o_value = [NSString stringWithUTF8String:
726 p_item->input.psz_name];
727 if( o_value == NULL )
728 o_value = [NSString stringWithCString:
729 p_item->input.psz_name];
731 else if( [[o_tc identifier] isEqualToString:@"2"] )
734 psz_temp = playlist_ItemGetInfo( p_item ,_("Meta-information"),_("Artist") );
736 if( psz_temp == NULL )
740 o_value = [NSString stringWithUTF8String: psz_temp];
741 if( o_value == NULL )
743 o_value = [NSString stringWithCString: psz_temp];
748 else if( [[o_tc identifier] isEqualToString:@"3"] )
750 char psz_duration[MSTRTIME_MAX_SIZE];
751 mtime_t dur = p_item->input.i_duration;
754 secstotimestr( psz_duration, dur/1000000 );
755 o_value = [NSString stringWithUTF8String: psz_duration];
759 o_value = @"-:--:--";
763 vlc_object_release( p_playlist );
768 /* Required for drag & drop and reordering */
769 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
774 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
776 return NSDragOperationNone;
779 /* Delegate method of NSWindow */
780 - (void)windowWillClose:(NSNotification *)aNotification
782 [o_btn_playlist setState: NSOffState];