-/*****************************************************************************
- * VLCPlaylistCommon implementation
- *
- * This class the superclass of the VLCPlaylist and VLCPlaylistWizard.
- * It contains the common methods and elements of these 2 entities.
- *****************************************************************************/
-@implementation VLCPlaylistCommon
-
-- (id)init
-{
- playlist_t * p_playlist = pl_Get( VLCIntf );
- p_current_root_item = p_playlist->p_local_category;
-
- self = [super init];
- if ( self != nil )
- {
- o_outline_dict = [[NSMutableDictionary alloc] init];
- }
- return self;
-}
-
-- (void)awakeFromNib
-{
- playlist_t * p_playlist = pl_Get( VLCIntf );
- [o_outline_view setTarget: self];
- [o_outline_view setDelegate: self];
- [o_outline_view setDataSource: self];
- [o_outline_view setAllowsEmptySelection: NO];
- [o_outline_view expandItem: [o_outline_view itemAtRow:0]];
-
- [o_outline_view_other setTarget: self];
- [o_outline_view_other setDelegate: self];
- [o_outline_view_other setDataSource: self];
- [o_outline_view_other setAllowsEmptySelection: NO];
-
- [self initStrings];
-}
-
-- (void)initStrings
-{
- [[o_tc_name headerCell] setStringValue:_NS("Name")];
- [[o_tc_author headerCell] setStringValue:_NS("Author")];
- [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
-
- [[o_tc_name_other headerCell] setStringValue:_NS("Name")];
- [[o_tc_author_other headerCell] setStringValue:_NS("Author")];
- [[o_tc_duration_other headerCell] setStringValue:_NS("Duration")];
-}
-
-- (void)setPlaylistRoot: (playlist_item_t *)root_item
-{
- p_current_root_item = root_item;
- [o_outline_view reloadData];
- [o_outline_view_other reloadData];
-}
-
-- (playlist_item_t *)currentPlaylistRoot
-{
- return p_current_root_item;
-}
-
-- (void)swapPlaylists:(id)newList
-{
- if(newList != o_outline_view)
- {
- id o_outline_view_temp = o_outline_view;
- id o_tc_author_temp = o_tc_author;
- id o_tc_duration_temp = o_tc_duration;
- id o_tc_name_temp = o_tc_name;
- o_outline_view = o_outline_view_other;
- o_tc_author = o_tc_author_other;
- o_tc_duration = o_tc_duration_other;
- o_tc_name = o_tc_name_other;
- o_outline_view_other = o_outline_view_temp;
- o_tc_author_other = o_tc_author_temp;
- o_tc_duration_other = o_tc_duration_temp;
- o_tc_name_other = o_tc_name_temp;
- }
-}
-
-- (NSOutlineView *)outlineView
-{
- return o_outline_view;
-}
-
-- (playlist_item_t *)selectedPlaylistItem
-{
- return [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
- pointerValue];
-}
-
-@end
-
-@implementation VLCPlaylistCommon (NSOutlineViewDataSource)
-/* return the number of children for Obj-C pointer item */ /* DONE */
-- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
-{
- int i_return = 0;
- playlist_item_t *p_item = NULL;
- playlist_t * p_playlist = pl_Get( VLCIntf );
- //assert( outlineView == o_outline_view );
-
- PL_LOCK;
- if( !item )
- {
- p_item = p_current_root_item;
- }
- else
- p_item = (playlist_item_t *)[item pointerValue];
-
- if( p_item )
- i_return = p_item->i_children;
- PL_UNLOCK;
-
- return i_return > 0 ? i_return : 0;
-}
-
-/* return the child at index for the Obj-C pointer item */ /* DONE */
-- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
-{
- playlist_item_t *p_return = NULL, *p_item = NULL;
- NSValue *o_value;
- playlist_t * p_playlist = pl_Get( VLCIntf );
-
- PL_LOCK;
- if( item == nil )
- {
- /* root object */
- p_item = p_current_root_item;
- }
- else
- {
- p_item = (playlist_item_t *)[item pointerValue];
- }
- if( p_item && index < p_item->i_children && index >= 0 )
- p_return = p_item->pp_children[index];
- PL_UNLOCK;
-
- o_value = [o_outline_dict objectForKey:[NSString stringWithFormat: @"%p", p_return]];
-
- if( o_value == nil )
- {
- /* FIXME: Why is there a warning if that happens all the time and seems
- * to be normal? Add an assert and fix it.
- * msg_Warn( VLCIntf, "playlist item misses pointer value, adding one" ); */
- o_value = [[NSValue valueWithPointer: p_return] retain];
- }
- return o_value;
-}
-
-/* is the item expandable */
-- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
-{
- int i_return = 0;
- playlist_t *p_playlist = pl_Get( VLCIntf );
-
- PL_LOCK;
- if( item == nil )
- {
- /* root object */
- if( p_current_root_item )
- {
- i_return = p_current_root_item->i_children;
- }
- }
- else
- {
- playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
- if( p_item )
- i_return = p_item->i_children;
- }
- PL_UNLOCK;
-
- return (i_return >= 0);
-}
-
-/* retrieve the string values for the cells */
-- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
-{
- id o_value = nil;
- playlist_item_t *p_item;
-
- /* For error handling */
- static BOOL attempted_reload = NO;
-
- if( item == nil || ![item isKindOfClass: [NSValue class]] )
- {
- /* Attempt to fix the error by asking for a data redisplay
- * This might cause infinite loop, so add a small check */
- if( !attempted_reload )
- {
- attempted_reload = YES;
- [outlineView reloadData];
- }
- return @"error" ;
- }
-
- p_item = (playlist_item_t *)[item pointerValue];
- if( !p_item || !p_item->p_input )
- {
- /* Attempt to fix the error by asking for a data redisplay
- * This might cause infinite loop, so add a small check */
- if( !attempted_reload )
- {
- attempted_reload = YES;
- [outlineView reloadData];
- }
- return @"error";
- }
-
- attempted_reload = NO;
-
- if( [[o_tc identifier] isEqualToString:@"name"] )
- {
- /* sanity check to prevent the NSString class from crashing */
- char *psz_title = input_item_GetTitleFbName( p_item->p_input );
- if( psz_title )
- {
- o_value = [NSString stringWithUTF8String: psz_title];
- free( psz_title );
- }
- }
- else if( [[o_tc identifier] isEqualToString:@"artist"] )
- {
- char *psz_artist = input_item_GetArtist( p_item->p_input );
- if( psz_artist )
- o_value = [NSString stringWithUTF8String: psz_artist];
- free( psz_artist );
- }
- else if( [[o_tc identifier] isEqualToString:@"duration"] )
- {
- char psz_duration[MSTRTIME_MAX_SIZE];
- mtime_t dur = input_item_GetDuration( p_item->p_input );
- if( dur != -1 )
- {
- secstotimestr( psz_duration, dur/1000000 );
- o_value = [NSString stringWithUTF8String: psz_duration];
- }
- else
- o_value = @"--:--";
- }
- else if( [[o_tc identifier] isEqualToString:@"status"] )
- {
- if( input_item_HasErrorWhenReading( p_item->p_input ) )
- {
- o_value = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kAlertCautionIcon)];
- [o_value setSize: NSMakeSize(16,16)];
- }
- }
- return o_value;
-}
-
-@end
-