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] );
70 [[self delegate] initDict];
74 - (bool)isItem:(playlist_item_t *)p_item inNode:(playlist_item_t *)p_node
76 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
78 playlist_item_t * p_temp_item = p_item;
80 if ( p_playlist == NULL )
85 while ( p_temp_item->i_parents > 0 )
88 for (i = 0; i < p_temp_item->i_parents ; i++)
90 if (p_temp_item->pp_parents[i]->i_view == VIEW_SIMPLE)
92 if (p_temp_item->pp_parents[i]->p_parent == p_node)
94 vlc_object_release(p_playlist);
99 p_temp_item = p_temp_item->pp_parents[i]->p_parent;
106 vlc_object_release(p_playlist);
110 - (void)keyDown:(NSEvent *)o_event
114 NSMutableArray *o_to_delete;
117 playlist_t * p_playlist;
118 intf_thread_t * p_intf = VLCIntf;
119 msg_Dbg( p_intf, "KEYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY");
120 if( [[o_event characters] length] )
122 key = [[o_event characters] characterAtIndex: 0];
125 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
128 if ( p_playlist == NULL )
135 case NSDeleteCharacter:
136 case NSDeleteFunctionKey:
137 case NSDeleteCharFunctionKey:
138 case NSBackspaceCharacter:
139 o_to_delete = [NSMutableArray arrayWithArray:[[self selectedRowEnumerator] allObjects]];
140 c = [o_to_delete count];
142 for( i = 0; i < c; i++ ) {
143 playlist_item_t * p_item;
144 o_number = [o_to_delete lastObject];
145 i_row = [o_number intValue];
147 [o_to_delete removeObject: o_number];
148 [self deselectRow: i_row];
149 p_item = (playlist_item_t *)[[self itemAtRow: i_row]pointerValue];
150 if (p_item->i_children > -1)
152 if ([self isItem:p_playlist->status.p_item inNode: p_item]
153 == YES && p_playlist->status.i_status)
155 playlist_Stop( p_playlist );
157 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE);
161 if( p_playlist->status.p_item == [[self itemAtRow: i_row]
162 pointerValue] && p_playlist->status.i_status )
164 playlist_Stop( p_playlist );
166 playlist_Delete( p_playlist, p_item->input.i_id );
168 [self reloadPlaylist];
173 [super keyDown: o_event];
177 if( p_playlist != NULL )
179 vlc_object_release( p_playlist );
185 /*****************************************************************************
186 * VLCPlaylist implementation
187 *****************************************************************************/
188 @implementation VLCPlaylist
202 [o_outline_view setTarget: self];
203 [o_outline_view setDelegate: self];
204 [o_outline_view setDataSource: self];
206 [o_outline_view setDoubleAction: @selector(playItem:)];
208 [o_outline_view registerForDraggedTypes:
209 [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
210 [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
212 /* We need to check whether _defaultTableHeaderSortImage exists, since it
213 belongs to an Apple hidden private API, and then can "disapear" at any time*/
215 if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderSortImage)] )
217 o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
221 o_ascendingSortingImage = nil;
224 if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderReverseSortImage)] )
226 o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
230 o_descendingSortingImage = nil;
233 o_outline_dict = [[NSMutableDictionary alloc] init];
236 //[self playlistUpdated];
241 [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
243 [o_mi_play setTitle: _NS("Play")];
244 [o_mi_delete setTitle: _NS("Delete")];
245 [o_mi_selectall setTitle: _NS("Select All")];
246 [o_mi_toggleItemsEnabled setTitle: _NS("Item Enabled")];
247 [o_mi_enableGroup setTitle: _NS("Enable all group items")];
248 [o_mi_disableGroup setTitle: _NS("Disable all group items")];
249 [o_mi_info setTitle: _NS("Properties")];
251 [[o_tc_name headerCell] setStringValue:_NS("Name")];
252 [[o_tc_author headerCell] setStringValue:_NS("Author")];
253 [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
254 [o_status_field setStringValue: [NSString stringWithFormat:
255 _NS("0 items in playlist")]];
257 [o_random_ckb setTitle: _NS("Random")];
259 [o_search_button setTitle: _NS("Search")];
261 [o_btn_playlist setToolTip: _NS("Playlist")];
262 [[o_loop_popup itemAtIndex:0] setTitle: _NS("Standard Play")];
263 [[o_loop_popup itemAtIndex:1] setTitle: _NS("Repeat One")];
264 [[o_loop_popup itemAtIndex:2] setTitle: _NS("Repeat All")];
267 - (void)playlistUpdated
269 [o_outline_view reloadData];
272 - (IBAction)playItem:(id)sender
274 intf_thread_t * p_intf = VLCIntf;
275 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
278 if( p_playlist != NULL )
280 playlist_item_t *p_item;
281 playlist_item_t *p_node = NULL;
284 p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
288 if (p_item->i_children == -1)
290 for (i = 0 ; i < p_item->i_parents ; i++)
292 if (p_item->pp_parents[i]->i_view == VIEW_SIMPLE)
294 p_node = p_item->pp_parents[i]->p_parent;
301 if (p_node->pp_children[0]->i_children == -1 &&
302 p_node->i_children > 0)
304 p_item = p_node->pp_children[0];
312 // p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
315 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VIEW_SIMPLE, p_node, p_item );
316 // playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VIEW_SIMPLE, p_view ? p_view->p_root : NULL, p_item );
318 vlc_object_release( p_playlist );
322 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
325 intf_thread_t * p_intf = VLCIntf;
326 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
329 if( p_playlist == NULL )
334 for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
337 NSDictionary *o_one_item;
338 int j, i_total_options = 0, i_new_id = -1;
339 int i_mode = PLAYLIST_INSERT;
340 BOOL b_rem = FALSE, b_dir = FALSE;
341 NSString *o_uri, *o_name;
344 char **ppsz_options = NULL;
347 o_one_item = [o_array objectAtIndex: i_item];
348 o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
349 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
350 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
352 /* If no name, then make a guess */
353 if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
355 if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
356 [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
357 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem )
359 /* All of this is to make sure CD's play when you D&D them on VLC */
360 /* Converts mountpoint to a /dev file */
363 buf = (struct statfs *) malloc (sizeof(struct statfs));
364 statfs( [o_uri fileSystemRepresentation], buf );
365 psz_dev = strdup(buf->f_mntfromname);
366 o_uri = [NSString stringWithCString: psz_dev ];
369 if( o_options && [o_options count] > 0 )
371 /* Count the input options */
372 i_total_options = [o_options count];
374 /* Allocate ppsz_options */
375 for( j = 0; j < i_total_options; j++ )
378 ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
380 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
385 i_new_id = playlist_AddExt( p_playlist, [o_uri fileSystemRepresentation],
386 [o_name UTF8String], i_mode,
387 i_position == -1 ? PLAYLIST_END : i_position + i_item,
388 0, (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options );
391 for( j = 0; j < i_total_options; j++ )
392 free( ppsz_options[j] );
393 if( ppsz_options ) free( ppsz_options ); */
395 /* Recent documents menu */
396 o_true_file = [NSURL fileURLWithPath: o_uri];
397 if( o_true_file != nil )
399 [[NSDocumentController sharedDocumentController]
400 noteNewRecentDocumentURL: o_true_file];
403 if( i_item == 0 && !b_enqueue )
405 playlist_Goto( p_playlist, playlist_GetPositionById( p_playlist, i_new_id ) );
406 playlist_Play( p_playlist );
410 vlc_object_release( p_playlist );
413 - (IBAction)handlePopUp:(id)sender
416 intf_thread_t * p_intf = VLCIntf;
417 vlc_value_t val1,val2;
418 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
420 if( p_playlist == NULL )
425 switch ([o_loop_popup indexOfSelectedItem])
430 var_Set( p_playlist, "loop", val1 );
432 var_Set( p_playlist, "repeat", val1 );
433 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat One" ) );
438 var_Set( p_playlist, "repeat", val1 );
440 var_Set( p_playlist, "loop", val1 );
441 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat All" ) );
445 var_Get( p_playlist, "repeat", &val1 );
446 var_Get( p_playlist, "loop", &val2 );
447 if (val1.b_bool || val2.b_bool)
450 var_Set( p_playlist, "repeat", val1 );
451 var_Set( p_playlist, "loop", val1 );
452 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
456 vlc_object_release( p_playlist );
457 [self playlistUpdated];
460 - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
462 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
464 playlist_item_t * p_selected_item;
465 int i_current, i_selected_row;
470 i_selected_row = [o_outline_view selectedRow];
471 if (i_selected_row < 0)
474 p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow:
475 i_selected_row] pointerValue];
477 for (i_current = 0; i_current < p_item->i_children ; i_current++)
480 NSString * o_current_name, * o_current_author;
482 vlc_mutex_lock( &p_playlist->object_lock );
483 o_current_name = [NSString stringWithUTF8String:
484 p_item->pp_children[i_current]->input.psz_name];
485 psz_temp = playlist_ItemGetInfo(p_item ,_("Meta-information"),_("Author") );
486 o_current_author = [NSString stringWithUTF8String: psz_temp];
488 vlc_mutex_unlock( &p_playlist->object_lock );
490 if (p_selected_item == p_item->pp_children[i_current] &&
491 b_selected_item_met == NO)
493 b_selected_item_met = YES;
495 else if (p_selected_item == p_item->pp_children[i_current] &&
496 b_selected_item_met == YES)
498 vlc_object_release(p_playlist);
501 else if (b_selected_item_met == YES &&
502 ([o_current_name rangeOfString:[o_search_field
503 stringValue] options:NSCaseInsensitiveSearch ].length ||
504 [o_current_author rangeOfString:[o_search_field
505 stringValue] options:NSCaseInsensitiveSearch ].length))
507 vlc_object_release(p_playlist);
508 /*Adds the parent items in the result array as well, so that we can
510 return [NSMutableArray arrayWithObject: [NSValue
511 valueWithPointer: p_item->pp_children[i_current]]];
513 if (p_item->pp_children[i_current]->i_children > 0)
515 id o_result = [self subSearchItem:
516 p_item->pp_children[i_current]];
517 if (o_result != NULL)
519 vlc_object_release(p_playlist);
520 [o_result insertObject: [NSValue valueWithPointer:
521 p_item->pp_children[i_current]] atIndex:0];
526 vlc_object_release(p_playlist);
530 - (IBAction)searchItem:(id)sender
532 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
534 playlist_view_t * p_view;
540 b_selected_item_met = NO;
542 if( p_playlist == NULL )
545 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
549 /*First, only search after the selected item:*
550 *(b_selected_item_met = NO) */
551 o_result = [self subSearchItem:p_view->p_root];
552 if (o_result == NULL)
554 /* If the first search failed, search again from the beginning */
555 o_result = [self subSearchItem:p_view->p_root];
557 if (o_result != NULL)
559 for (i = 0 ; i < [o_result count] - 1 ; i++)
561 [o_outline_view expandItem: [o_outline_dict objectForKey:
562 [NSString stringWithFormat: @"%p",
563 [[o_result objectAtIndex: i] pointerValue]]]];
565 i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
566 [NSString stringWithFormat: @"%p",
567 [[o_result objectAtIndex: [o_result count] - 1 ]
572 [o_outline_view selectRow:i_row byExtendingSelection: NO];
573 [o_outline_view scrollRowToVisible: i_row];
576 vlc_object_release(p_playlist);
581 - (NSMenu *)menuForEvent:(NSEvent *)o_event
583 intf_thread_t * p_intf = VLCIntf;
584 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
587 bool b_itemstate = FALSE;
591 vlc_bool_t b_item_sel;
593 pt = [o_table_view convertPoint: [o_event locationInWindow]
595 b_item_sel = ( [o_table_view rowAtPoint: pt] != -1 &&
596 [o_table_view selectedRow] != -1 );
597 b_rows = [o_table_view numberOfRows] != 0;
599 [o_mi_play setEnabled: b_item_sel];
600 [o_mi_delete setEnabled: b_item_sel];
601 [o_mi_selectall setEnabled: b_rows];
602 [o_mi_info setEnabled: b_item_sel];
603 [o_mi_toggleItemsEnabled setEnabled: b_item_sel];
604 [o_mi_enableGroup setEnabled: b_item_sel];
605 [o_mi_disableGroup setEnabled: b_item_sel];
609 b_itemstate = ([o_table_view selectedRow] > -1) ?
610 p_playlist->pp_items[[o_table_view selectedRow]]->b_enabled : FALSE;
611 vlc_object_release(p_playlist);
614 [o_mi_toggleItemsEnabled setState: b_itemstate];
616 return( o_ctx_menu );
621 [o_outline_dict removeAllObjects];
626 @implementation VLCPlaylist (NSOutlineViewDataSource)
628 /* return the number of children for Obj-C pointer item */ /* DONE */
629 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
632 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
634 if( p_playlist == NULL )
640 playlist_view_t *p_view;
641 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
642 if( p_view && p_view->p_root )
643 i_return = p_view->p_root->i_children;
647 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
649 i_return = p_item->i_children;
651 vlc_object_release( p_playlist );
652 if( i_return == -1 ) i_return = 0;
653 msg_Dbg( p_playlist, "I have %d children", i_return );
657 /* return the child at index for the Obj-C pointer item */ /* DONE */
658 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
660 playlist_item_t *p_return = NULL;
661 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
665 if( p_playlist == NULL )
671 playlist_view_t *p_view;
672 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
673 if( p_view && index < p_view->p_root->i_children )
674 p_return = p_view->p_root->pp_children[index];
678 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
679 if( p_item && index < p_item->i_children )
681 p_return = p_item->pp_children[index];
685 [o_status_field setStringValue: [NSString stringWithFormat:
686 _NS("%i items in playlist"), p_playlist->i_size]];
688 vlc_object_release( p_playlist );
689 msg_Dbg( p_playlist, "childitem with index %d", index );
691 o_value = [NSValue valueWithPointer: p_return];
693 [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p", p_return]];
697 /* is the item expandable */
698 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
701 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
703 if( p_playlist == NULL )
709 playlist_view_t *p_view;
710 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
711 if( p_view && p_view->p_root )
712 i_return = p_view->p_root->i_children;
716 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
718 i_return = p_item->i_children;
720 vlc_object_release( p_playlist );
722 if( i_return == -1 || i_return == 0 )
728 /* retrieve the string values for the cells */
729 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
732 intf_thread_t * p_intf = VLCIntf;
733 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
735 playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
737 if( p_playlist == NULL || p_item == NULL )
742 if( [[o_tc identifier] isEqualToString:@"1"] )
744 o_value = [NSString stringWithUTF8String:
745 p_item->input.psz_name];
746 if( o_value == NULL )
747 o_value = [NSString stringWithCString:
748 p_item->input.psz_name];
750 else if( [[o_tc identifier] isEqualToString:@"2"] )
753 psz_temp = playlist_ItemGetInfo( p_item ,_("Meta-information"),_("Artist") );
755 if( psz_temp == NULL )
759 o_value = [NSString stringWithUTF8String: psz_temp];
760 if( o_value == NULL )
762 o_value = [NSString stringWithCString: psz_temp];
767 else if( [[o_tc identifier] isEqualToString:@"3"] )
769 char psz_duration[MSTRTIME_MAX_SIZE];
770 mtime_t dur = p_item->input.i_duration;
773 secstotimestr( psz_duration, dur/1000000 );
774 o_value = [NSString stringWithUTF8String: psz_duration];
778 o_value = @"-:--:--";
782 vlc_object_release( p_playlist );
787 /* Required for drag & drop and reordering */
788 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
793 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
795 return NSDragOperationNone;
798 /* Delegate method of NSWindow */
799 - (void)windowWillClose:(NSNotification *)aNotification
801 [o_btn_playlist setState: NSOffState];