1 /*****************************************************************************
2 r playlistinfo.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2002-2012 VLC authors and VideoLAN
7 * Authors: Benjamin Pracht <bigben at videolan dot org>
8 * Felix Paul Kühne <fkuehne at videolan dot org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 #import "CompatibilityFixes.h"
31 #import "playlistinfo.h"
35 /*****************************************************************************
36 * VLCPlaylistInfo Implementation
37 *****************************************************************************/
39 @implementation VLCInfo
41 static VLCInfo *_o_sharedInstance = nil;
43 + (VLCInfo *)sharedInstance
45 return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
50 if( _o_sharedInstance )
53 _o_sharedInstance = [super init];
55 if( _o_sharedInstance != nil ) {
57 [self updatePanelWithItem: NULL];
58 rootItem = [[VLCInfoTreeItem alloc] init];
62 return _o_sharedInstance;
67 [o_info_window setExcludedFromWindowsMenu: YES];
68 [o_info_window setFloatingPanel: NO];
69 if (!OSX_SNOW_LEOPARD)
70 [o_info_window setCollectionBehavior: NSWindowCollectionBehaviorFullScreenAuxiliary];
72 [o_info_window setTitle: _NS("Media Information")];
73 [o_uri_lbl setStringValue: _NS("Location")];
74 [o_title_lbl setStringValue: _NS("Title")];
75 [o_author_lbl setStringValue: _NS("Artist")];
76 [o_saveMetaData_btn setStringValue: _NS("Save Metadata" )];
78 [[o_tab_view tabViewItemAtIndex: 0] setLabel: _NS("General")];
79 [[o_tab_view tabViewItemAtIndex: 1] setLabel: _NS("Codec Details")];
80 [[o_tab_view tabViewItemAtIndex: 2] setLabel: _NS("Statistics")];
81 [o_tab_view selectTabViewItemAtIndex: 0];
83 /* constants defined in vlc_meta.h */
84 [o_genre_lbl setStringValue: _NS(VLC_META_GENRE)];
85 [o_copyright_lbl setStringValue: _NS(VLC_META_COPYRIGHT)];
86 [o_collection_lbl setStringValue: _NS(VLC_META_ALBUM)];
87 [o_seqNum_lbl setStringValue: _NS(VLC_META_TRACK_NUMBER)];
88 [o_description_lbl setStringValue: _NS(VLC_META_DESCRIPTION)];
89 [o_date_lbl setStringValue: _NS(VLC_META_DATE)];
90 [o_language_lbl setStringValue: _NS(VLC_META_LANGUAGE)];
91 [o_nowPlaying_lbl setStringValue: _NS(VLC_META_NOW_PLAYING)];
92 [o_publisher_lbl setStringValue: _NS(VLC_META_PUBLISHER)];
93 [o_encodedby_lbl setStringValue: _NS(VLC_META_ENCODED_BY)];
96 [o_input_lbl setStringValue: _NS("Input")];
97 [o_read_bytes_lbl setStringValue: _NS("Read at media")];
98 [o_input_bitrate_lbl setStringValue: _NS("Input bitrate")];
99 [o_demux_bytes_lbl setStringValue: _NS("Demuxed")];
100 [o_demux_bitrate_lbl setStringValue: _NS("Stream bitrate")];
102 [o_video_lbl setStringValue: _NS("Video")];
103 [o_video_decoded_lbl setStringValue: _NS("Decoded blocks")];
104 [o_displayed_lbl setStringValue: _NS("Displayed frames")];
105 [o_lost_frames_lbl setStringValue: _NS("Lost frames")];
107 [o_sout_lbl setStringValue: _NS("Streaming")];
108 [o_sent_packets_lbl setStringValue: _NS("Sent packets")];
109 [o_sent_bytes_lbl setStringValue: _NS("Sent bytes")];
110 [o_sent_bitrate_lbl setStringValue: _NS("Send rate")];
112 [o_audio_lbl setStringValue: _NS("Audio")];
113 [o_audio_decoded_lbl setStringValue: _NS("Decoded blocks")];
114 [o_played_abuffers_lbl setStringValue: _NS("Played buffers")];
115 [o_lost_abuffers_lbl setStringValue: _NS("Lost buffers")];
117 [o_info_window setInitialFirstResponder: o_uri_txt];
118 [o_info_window setDelegate: self];
120 b_awakeFromNib = YES;
122 /* We may be awoken from nib way after initialisation
123 * Update ourselves */
124 [self updatePanelWithItem:p_item];
133 vlc_gc_decref( p_item );
140 BOOL b_stats = config_GetInt(VLCIntf, "stats");
142 if( [o_tab_view numberOfTabViewItems] > 2 )
143 [o_tab_view removeTabViewItem: [o_tab_view tabViewItemAtIndex: 2]];
146 [self initMediaPanelStats];
147 [o_info_window makeKeyAndOrderFront: self];
150 - (void)initMediaPanelStats
152 //Initializing Input Variables
153 [o_read_bytes_txt setStringValue: [NSString stringWithFormat:@"%8.0f KiB", (float)0]];
154 [o_input_bitrate_txt setStringValue: [NSString stringWithFormat:@"%6.0f kb/s", (float)0]];
155 [o_demux_bytes_txt setStringValue: [NSString stringWithFormat:@"%8.0f KiB", (float)0]];
156 [o_demux_bitrate_txt setStringValue: [NSString stringWithFormat:@"%6.0f kb/s", (float)0]];
158 //Initializing Video Variables
159 [o_video_decoded_txt setIntValue:0];
160 [o_displayed_txt setIntValue:0];
161 [o_lost_frames_txt setIntValue:0];
163 //Initializing Output Variables
164 [o_sent_packets_txt setIntValue: 0];
165 [o_sent_bytes_txt setStringValue: [NSString stringWithFormat:@"%8.0f KiB", (float)0]];
166 [o_sent_bitrate_txt setStringValue: [NSString stringWithFormat:@"%6.0f kb/s", (float)0]];
168 //Initializing Audio Variables
169 [o_audio_decoded_txt setIntValue:0];
170 [o_played_abuffers_txt setIntValue: 0];
171 [o_lost_abuffers_txt setIntValue: 0];
175 - (void)updatePanelWithItem:(input_item_t *)_p_item;
177 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
178 if( _p_item != p_item ) {
179 if( p_item ) vlc_gc_decref( p_item );
180 [o_saveMetaData_btn setEnabled: NO];
181 if( _p_item ) vlc_gc_incref( _p_item );
188 [self setMeta: "" forLabel: o_##foo##_txt];
203 [o_image_well setImage: [NSImage imageNamed: @"noart.png"]];
205 if( !input_item_IsPreparsed( p_item ) )
206 playlist_PreparseEnqueue( pl_Get( VLCIntf ), p_item );
209 char * psz_url = decode_URI( input_item_GetURI( p_item ) );
210 [o_uri_txt setStringValue: [NSString stringWithUTF8String: psz_url ? psz_url : ""]];
213 /* fill title info */
214 char * psz_title = input_item_GetTitle( p_item );
216 psz_title = input_item_GetName( p_item );
217 [o_title_txt setStringValue: [NSString stringWithUTF8String: psz_title ? : "" ]];
220 #define SET( foo, bar ) \
221 char *psz_##foo = input_item_Get##bar ( p_item ); \
222 [self setMeta: psz_##foo forLabel: o_##foo##_txt]; \
223 FREENULL( psz_##foo );
225 /* fill the other fields */
226 SET( author, Artist );
227 SET( collection, Album );
228 SET( seqNum, TrackNum );
230 SET( copyright, Copyright );
231 SET( publisher, Publisher );
232 SET( nowPlaying, NowPlaying );
233 SET( language, Language );
235 SET( description, Description );
236 SET( encodedby, EncodedBy );
242 psz_meta = input_item_GetArtURL( p_item );
244 /* FIXME Can also be attachment:// */
245 if( psz_meta && strncmp( psz_meta, "attachment://", 13 ) )
246 o_image = [[NSImage alloc] initWithContentsOfURL: [NSURL URLWithString:[NSString stringWithUTF8String: psz_meta]]];
248 o_image = [[NSImage imageNamed: @"noart.png"] retain];
249 [o_image_well setImage: o_image];
251 FREENULL( psz_meta );
254 /* reload the advanced table */
256 [o_outline_view reloadData];
258 /* update the stats once to display p_item change faster */
259 [self updateStatistics];
263 - (void)setMeta: (char *)psz_meta forLabel: (id)theItem
265 if( psz_meta != NULL && *psz_meta)
266 [theItem setStringValue: [NSString stringWithUTF8String:psz_meta]];
268 [theItem setStringValue: @""];
271 - (void)updateStatistics
276 if ([o_info_window isVisible]) {
277 if( !p_item || !p_item->p_stats ) {
278 [self initMediaPanelStats];
282 vlc_mutex_lock( &p_item->p_stats->lock );
285 [o_read_bytes_txt setStringValue: [NSString stringWithFormat:
286 @"%8.0f KiB", (float)(p_item->p_stats->i_read_bytes)/1024]];
287 [o_input_bitrate_txt setStringValue: [NSString stringWithFormat:
288 @"%6.0f kb/s", (float)(p_item->p_stats->f_input_bitrate)*8000]];
289 [o_demux_bytes_txt setStringValue: [NSString stringWithFormat:
290 @"%8.0f KiB", (float)(p_item->p_stats->i_demux_read_bytes)/1024]];
291 [o_demux_bitrate_txt setStringValue: [NSString stringWithFormat:
292 @"%6.0f kb/s", (float)(p_item->p_stats->f_demux_bitrate)*8000]];
295 [o_video_decoded_txt setIntValue: p_item->p_stats->i_decoded_video];
296 [o_displayed_txt setIntValue: p_item->p_stats->i_displayed_pictures];
297 [o_lost_frames_txt setIntValue: p_item->p_stats->i_lost_pictures];
300 [o_sent_packets_txt setIntValue: p_item->p_stats->i_sent_packets];
301 [o_sent_bytes_txt setStringValue: [NSString stringWithFormat: @"%8.0f KiB",
302 (float)(p_item->p_stats->i_sent_bytes)/1024]];
303 [o_sent_bitrate_txt setStringValue: [NSString stringWithFormat:
304 @"%6.0f kb/s", (float)(p_item->p_stats->f_send_bitrate*8)*1000]];
307 [o_audio_decoded_txt setIntValue: p_item->p_stats->i_decoded_audio];
308 [o_played_abuffers_txt setIntValue: p_item->p_stats->i_played_abuffers];
309 [o_lost_abuffers_txt setIntValue: p_item->p_stats->i_lost_abuffers];
311 vlc_mutex_unlock( &p_item->p_stats->lock );
315 - (IBAction)metaFieldChanged:(id)sender
317 [o_saveMetaData_btn setEnabled: YES];
320 - (IBAction)saveMetaData:(id)sender
325 #define utf8( o_blub ) \
326 [[o_blub stringValue] UTF8String]
328 input_item_SetName( p_item, utf8( o_title_txt ) );
329 input_item_SetTitle( p_item, utf8( o_title_txt ) );
330 input_item_SetArtist( p_item, utf8( o_author_txt ) );
331 input_item_SetAlbum( p_item, utf8( o_collection_txt ) );
332 input_item_SetGenre( p_item, utf8( o_genre_txt ) );
333 input_item_SetTrackNum( p_item, utf8( o_seqNum_txt ) );
334 input_item_SetDate( p_item, utf8( o_date_txt ) );
335 input_item_SetCopyright( p_item, utf8( o_copyright_txt ) );
336 input_item_SetPublisher( p_item, utf8( o_publisher_txt ) );
337 input_item_SetDescription( p_item, utf8( o_description_txt ) );
338 input_item_SetLanguage( p_item, utf8( o_language_txt ) );
340 playlist_t * p_playlist = pl_Get( VLCIntf );
341 input_item_WriteMeta( VLC_OBJECT(p_playlist), p_item );
343 var_SetBool( p_playlist, "intf-change", true );
344 [self updatePanelWithItem: p_item];
346 [o_saveMetaData_btn setEnabled: NO];
350 NSRunAlertPanel(_NS("Error while saving meta"),
351 _NS("VLC was unable to save the meta data."),
352 _NS("OK"), nil, nil);
355 - (IBAction)downloadCoverArt:(id)sender
357 playlist_t * p_playlist = pl_Get( VLCIntf );
358 if( p_item) playlist_AskForArtEnqueue( p_playlist, p_item );
361 - (input_item_t *)item
363 if( p_item ) vlc_gc_incref( p_item );
369 @implementation VLCInfo (NSMenuValidation)
371 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
373 BOOL bEnabled = TRUE;
375 if( [[o_mi title] isEqualToString: _NS("Information")] ) {
376 return ![[[VLCMain sharedInstance] playlist] isSelectionEmpty];
384 @implementation VLCInfo (NSTableDataSource)
386 - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
388 return (item == nil) ? [rootItem numberOfChildren] : [item numberOfChildren];
391 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
392 return ([item numberOfChildren] > 0);
395 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
397 return (item == nil) ? [rootItem childAtIndex:index] : (id)[item childAtIndex:index];
400 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
402 if ([[tableColumn identifier] isEqualToString:@"0"])
403 return (item == nil) ? @"" : (id)[item name];
405 return (item == nil) ? @"" : (id)[item value];
410 @implementation VLCInfoTreeItem
412 @synthesize name = o_name, value = o_value;
414 #define IsALeafNode ((id)-1)
416 - (id)initWithName: (NSString *)o_item_name value: (NSString *)o_item_value ID: (int)i_id
417 parent:(VLCInfoTreeItem *)o_parent_item
422 o_name = [o_item_name copy];
423 o_value = [o_item_value copy];
425 o_parent = o_parent_item;
426 p_item = [(VLCInfo *)[[VLCMain sharedInstance] info] item];
434 return [self initWithName:@"main" value:@"" ID:-1 parent:nil];
439 if( o_children != IsALeafNode ) [o_children release];
442 if( p_item ) vlc_gc_decref( p_item );
446 /* Creates and returns the array of children
447 * Loads children incrementally */
448 - (NSArray *)children
450 if( !p_item ) return nil;
452 if (o_children == NULL) {
453 if( i_object_id == -1 ) {
454 vlc_mutex_lock( &p_item->lock );
455 o_children = [[NSMutableArray alloc] initWithCapacity: p_item->i_categories];
456 for (int i = 0 ; i < p_item->i_categories ; i++) {
457 NSString * name = [NSString stringWithUTF8String: p_item->pp_categories[i]->psz_name];
458 VLCInfoTreeItem * item = [[VLCInfoTreeItem alloc] initWithName:name value:@"" ID:i parent:self];
460 [o_children addObject:item];
462 vlc_mutex_unlock( &p_item->lock );
464 else if( o_parent->i_object_id == -1 ) {
465 vlc_mutex_lock( &p_item->lock );
466 info_category_t * cat = p_item->pp_categories[i_object_id];
467 o_children = [[NSMutableArray alloc] initWithCapacity: cat->i_infos];
468 for (int i = 0 ; i < cat->i_infos ; i++) {
469 NSString * name = [NSString stringWithUTF8String: cat->pp_infos[i]->psz_name];
470 NSString * value = [NSString stringWithUTF8String: cat->pp_infos[i]->psz_value ? : ""];
471 VLCInfoTreeItem * item = [[VLCInfoTreeItem alloc] initWithName:name value:value ID:i parent:self];
473 [o_children addObject:item];
475 vlc_mutex_unlock( &p_item->lock );
478 o_children = IsALeafNode;
485 input_item_t * oldItem = p_item;
486 p_item = [(VLCInfo *)[[VLCMain sharedInstance] info] item];
487 if( oldItem && oldItem != p_item ) vlc_gc_decref( oldItem );
489 [o_children release];
493 - (VLCInfoTreeItem *)childAtIndex:(NSUInteger)i_index {
494 return [[self children] objectAtIndex:i_index];
497 - (int)numberOfChildren {
499 id i_tmp = [self children];
500 return ( i_tmp == IsALeafNode ) ? (-1) : (int)[i_tmp count];