]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlistinfo.m
macosx: redesigned info panel to HUD
[vlc] / modules / gui / macosx / playlistinfo.m
1 /*****************************************************************************
2  r playlistinfo.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2002-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Benjamin Pracht <bigben at videolan dot org>
8  *          Felix Paul Kühne <fkuehne at videolan dot org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #include "intf.h"
30 #include "playlistinfo.h"
31 #include "playlist.h"
32 #include <vlc_url.h>
33
34 /*****************************************************************************
35  * VLCPlaylistInfo Implementation
36  *****************************************************************************/
37
38 @implementation VLCInfo
39
40 static VLCInfo *_o_sharedInstance = nil;
41
42 + (VLCInfo *)sharedInstance
43 {
44     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
45 }
46
47 - (id)init
48 {
49     if( _o_sharedInstance )
50     {
51         [self dealloc];
52     }
53     else
54     {
55         _o_sharedInstance = [super init];
56         
57         if( _o_sharedInstance != nil )
58         {
59             p_item = NULL;
60             [self updatePanelWithItem: NULL];
61             rootItem = [[VLCInfoTreeItem alloc] init];
62         }
63     }
64
65     return _o_sharedInstance;
66 }
67
68 - (void)awakeFromNib
69 {
70     [o_info_window setExcludedFromWindowsMenu: YES];
71     [o_info_window setFloatingPanel: NO];
72
73     [o_info_window setTitle: _NS("Media Information")];
74     [o_uri_lbl setStringValue: _NS("Location")];
75     [o_title_lbl setStringValue: _NS("Title")];
76     [o_author_lbl setStringValue: _NS("Artist")];
77     [o_saveMetaData_btn setStringValue: _NS("Save Metadata" )];
78
79     [[o_tab_view tabViewItemAtIndex: 0] setLabel: _NS("General")];
80     [[o_tab_view tabViewItemAtIndex: 1] setLabel: _NS("Codec Details")];
81     [[o_tab_view tabViewItemAtIndex: 2] setLabel: _NS("Statistics")];
82     [o_tab_view selectTabViewItemAtIndex: 0];
83
84     /* constants defined in vlc_meta.h */
85     [o_genre_lbl setStringValue: _NS(VLC_META_GENRE)];
86     [o_copyright_lbl setStringValue: _NS(VLC_META_COPYRIGHT)];
87     [o_collection_lbl setStringValue: _NS(VLC_META_ALBUM)];
88     [o_seqNum_lbl setStringValue: _NS(VLC_META_TRACK_NUMBER)];
89     [o_description_lbl setStringValue: _NS(VLC_META_DESCRIPTION)];
90     [o_date_lbl setStringValue: _NS(VLC_META_DATE)];
91     [o_language_lbl setStringValue: _NS(VLC_META_LANGUAGE)];
92     [o_nowPlaying_lbl setStringValue: _NS(VLC_META_NOW_PLAYING)];
93     [o_publisher_lbl setStringValue: _NS(VLC_META_PUBLISHER)];
94     [o_encodedby_lbl setStringValue: _NS(VLC_META_ENCODED_BY)];
95
96     /* statistics */
97     [o_input_lbl setStringValue: _NS("Input")];
98     [o_read_bytes_lbl setStringValue: _NS("Read at media")];
99     [o_input_bitrate_lbl setStringValue: _NS("Input bitrate")];
100     [o_demux_bytes_lbl setStringValue: _NS("Demuxed")];
101     [o_demux_bitrate_lbl setStringValue: _NS("Stream bitrate")];
102
103     [o_video_lbl setStringValue: _NS("Video")];
104     [o_video_decoded_lbl setStringValue: _NS("Decoded blocks")];
105     [o_displayed_lbl setStringValue: _NS("Displayed frames")];
106     [o_lost_frames_lbl setStringValue: _NS("Lost frames")];
107
108     [o_sout_lbl setStringValue: _NS("Streaming")];
109     [o_sent_packets_lbl setStringValue: _NS("Sent packets")];
110     [o_sent_bytes_lbl setStringValue: _NS("Sent bytes")];
111     [o_sent_bitrate_lbl setStringValue: _NS("Send rate")];
112
113     [o_audio_lbl setStringValue: _NS("Audio")];
114     [o_audio_decoded_lbl setStringValue: _NS("Decoded blocks")];
115     [o_played_abuffers_lbl setStringValue: _NS("Played buffers")];
116     [o_lost_abuffers_lbl setStringValue: _NS("Lost buffers")];
117
118     [o_info_window setInitialFirstResponder: o_uri_txt];
119     [o_info_window setDelegate: self];
120
121     /* We may be awoken from nib way after initialisation
122      * Update ourselves */
123     [self updatePanelWithItem:p_item];
124 }
125
126
127 - (void)dealloc
128 {
129     [rootItem release];
130
131     if( p_item ) vlc_gc_decref( p_item );
132
133     [super dealloc];
134 }
135
136 - (void)initPanel
137 {
138     BOOL b_stats = config_GetInt(VLCIntf, "stats");
139     if( !b_stats )
140     {
141         if( [o_tab_view numberOfTabViewItems] > 2 )
142             [o_tab_view removeTabViewItem: [o_tab_view tabViewItemAtIndex: 2]];
143     }
144
145     [self initMediaPanelStats];
146     [o_info_window makeKeyAndOrderFront: self];
147 }
148
149 - (void)initMediaPanelStats
150 {
151     //Initializing Input Variables
152     [o_read_bytes_txt setStringValue: [NSString stringWithFormat:@"%8.0f KiB", (float)0]];
153     [o_input_bitrate_txt setStringValue: [NSString stringWithFormat:@"%6.0f kb/s", (float)0]];
154     [o_demux_bytes_txt setStringValue: [NSString stringWithFormat:@"%8.0f KiB", (float)0]];
155     [o_demux_bitrate_txt setStringValue: [NSString stringWithFormat:@"%6.0f kb/s", (float)0]];
156     
157     //Initializing Video Variables
158     [o_video_decoded_txt setIntValue:0];
159     [o_displayed_txt setIntValue:0];
160     [o_lost_frames_txt setIntValue:0];
161
162     //Initializing Output Variables
163     [o_sent_packets_txt setIntValue: 0];
164     [o_sent_bytes_txt setStringValue: [NSString stringWithFormat:@"%8.0f KiB", (float)0]];
165     [o_sent_bitrate_txt setStringValue: [NSString stringWithFormat:@"%6.0f kb/s", (float)0]];
166
167     //Initializing Audio Variables
168     [o_audio_decoded_txt setIntValue:0];
169     [o_played_abuffers_txt setIntValue: 0];
170     [o_lost_abuffers_txt setIntValue: 0];
171
172 }
173
174 - (void)updatePanelWithItem:(input_item_t *)_p_item;
175 {
176     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
177     if( _p_item != p_item )
178     {
179         if( p_item ) vlc_gc_decref( p_item );
180         [o_saveMetaData_btn setEnabled: NO];
181         if( _p_item ) vlc_gc_incref( _p_item );
182         p_item = _p_item;
183     }
184
185     if( !p_item )
186     {
187         /* Erase */
188     #define SET( foo ) \
189         [self setMeta: "" forLabel: o_##foo##_txt];
190         SET( uri );
191         SET( title );
192         SET( author );
193         SET( collection );
194         SET( seqNum );
195         SET( genre );
196         SET( copyright );
197         SET( publisher );
198         SET( nowPlaying );
199         SET( language );
200         SET( date );
201         SET( description );
202         SET( encodedby );
203     #undef SET
204         [o_image_well setImage: [NSImage imageNamed: @"noart.png"]];
205     }
206     else
207     {
208         if( !input_item_IsPreparsed( p_item ) )
209         {
210             playlist_PreparseEnqueue( pl_Get( VLCIntf ), p_item );
211         }
212
213         /* fill uri info */
214         char * psz_url = decode_URI( input_item_GetURI( p_item ) );
215         [o_uri_txt setStringValue: [NSString stringWithUTF8String: psz_url ? psz_url : ""]];
216         free( psz_url );
217
218         /* fill title info */
219         char * psz_title = input_item_GetTitle( p_item );
220         if( !psz_title )
221             psz_title = input_item_GetName( p_item );
222         [o_title_txt setStringValue: [NSString stringWithUTF8String: psz_title ? : ""  ]];
223         free( psz_title );
224
225     #define SET( foo, bar ) \
226         char *psz_##foo = input_item_Get##bar ( p_item ); \
227         [self setMeta: psz_##foo forLabel: o_##foo##_txt]; \
228         FREENULL( psz_##foo );
229
230         /* fill the other fields */
231         SET( author, Artist );
232         SET( collection, Album );
233         SET( seqNum, TrackNum );
234         SET( genre, Genre );
235         SET( copyright, Copyright );
236         SET( publisher, Publisher );
237         SET( nowPlaying, NowPlaying );
238         SET( language, Language );
239         SET( date, Date );
240         SET( description, Description );
241         SET( encodedby, EncodedBy );
242
243     #undef SET
244
245         char *psz_meta;
246         NSImage *o_image;
247         psz_meta = input_item_GetArtURL( p_item );
248
249         /* FIXME Can also be attachment:// */
250         if( psz_meta && strncmp( psz_meta, "attachment://", 13 ) )
251             o_image = [[NSImage alloc] initWithContentsOfURL: [NSURL URLWithString:[NSString stringWithUTF8String: psz_meta]]];
252         else
253             o_image = [[NSImage imageNamed: @"noart.png"] retain];
254         [o_image_well setImage: o_image];
255         [o_image release];
256         FREENULL( psz_meta );
257     }
258
259     /* reload the advanced table */
260     [rootItem refresh];
261     [o_outline_view reloadData];
262
263     /* update the stats once to display p_item change faster */
264     [self updateStatistics];
265     [o_pool release];
266 }
267
268 - (void)setMeta: (char *)psz_meta forLabel: (id)theItem
269 {
270     if( psz_meta != NULL && *psz_meta)
271         [theItem setStringValue: [NSString stringWithUTF8String:psz_meta]];
272     else
273         [theItem setStringValue: @""];
274 }
275
276 - (void)updateStatistics
277 {
278     if ([o_info_window isVisible])
279     {
280         if( !p_item || !p_item->p_stats )
281         {
282             [self initMediaPanelStats];
283             return;
284         }
285
286         vlc_mutex_lock( &p_item->p_stats->lock );
287
288         /* input */
289         [o_read_bytes_txt setStringValue: [NSString stringWithFormat:
290             @"%8.0f KiB", (float)(p_item->p_stats->i_read_bytes)/1024]];
291         [o_input_bitrate_txt setStringValue: [NSString stringWithFormat:
292             @"%6.0f kb/s", (float)(p_item->p_stats->f_input_bitrate)*8000]];
293         [o_demux_bytes_txt setStringValue: [NSString stringWithFormat:
294             @"%8.0f KiB", (float)(p_item->p_stats->i_demux_read_bytes)/1024]];
295         [o_demux_bitrate_txt setStringValue: [NSString stringWithFormat:
296             @"%6.0f kb/s", (float)(p_item->p_stats->f_demux_bitrate)*8000]];
297
298         /* Video */
299         [o_video_decoded_txt setIntValue: p_item->p_stats->i_decoded_video];
300         [o_displayed_txt setIntValue: p_item->p_stats->i_displayed_pictures];
301         [o_lost_frames_txt setIntValue: p_item->p_stats->i_lost_pictures];
302
303         /* Sout */
304         [o_sent_packets_txt setIntValue: p_item->p_stats->i_sent_packets];
305         [o_sent_bytes_txt setStringValue: [NSString stringWithFormat: @"%8.0f KiB",
306             (float)(p_item->p_stats->i_sent_bytes)/1024]];
307         [o_sent_bitrate_txt setStringValue: [NSString stringWithFormat:
308             @"%6.0f kb/s", (float)(p_item->p_stats->f_send_bitrate*8)*1000]];
309
310         /* Audio */
311         [o_audio_decoded_txt setIntValue: p_item->p_stats->i_decoded_audio];
312         [o_played_abuffers_txt setIntValue: p_item->p_stats->i_played_abuffers];
313         [o_lost_abuffers_txt setIntValue: p_item->p_stats->i_lost_abuffers];
314
315         vlc_mutex_unlock( &p_item->p_stats->lock );
316     }
317 }
318
319 - (IBAction)metaFieldChanged:(id)sender
320 {
321     [o_saveMetaData_btn setEnabled: YES];
322 }
323
324 - (IBAction)saveMetaData:(id)sender
325 {
326     if( !p_item ) goto error;
327
328     #define utf8( o_blub ) \
329         [[o_blub stringValue] UTF8String]
330
331     input_item_SetName( p_item, utf8( o_title_txt ) );
332     input_item_SetTitle( p_item, utf8( o_title_txt ) );
333     input_item_SetArtist( p_item, utf8( o_author_txt ) );
334     input_item_SetAlbum( p_item, utf8( o_collection_txt ) );
335     input_item_SetGenre( p_item, utf8( o_genre_txt ) );
336     input_item_SetTrackNum( p_item, utf8( o_seqNum_txt ) );
337     input_item_SetDate( p_item, utf8( o_date_txt ) );
338     input_item_SetCopyright( p_item, utf8( o_copyright_txt ) );
339     input_item_SetPublisher( p_item, utf8( o_publisher_txt ) );
340     input_item_SetDescription( p_item, utf8( o_description_txt ) );
341     input_item_SetLanguage( p_item, utf8( o_language_txt ) );
342
343     playlist_t * p_playlist = pl_Get( VLCIntf );
344     input_item_WriteMeta( VLC_OBJECT(p_playlist), p_item );
345
346     var_SetBool( p_playlist, "intf-change", true );
347     [self updatePanelWithItem: p_item];
348
349     [o_saveMetaData_btn setEnabled: NO];
350     return;
351
352 error:
353     NSRunAlertPanel(_NS("Error while saving meta"),
354         _NS("VLC was unable to save the meta data."),
355         _NS("OK"), nil, nil);
356 }
357
358 - (IBAction)downloadCoverArt:(id)sender
359 {
360     playlist_t * p_playlist = pl_Get( VLCIntf );
361     if( p_item) playlist_AskForArtEnqueue( p_playlist, p_item );
362 }
363
364 - (input_item_t *)item
365 {
366     if( p_item ) vlc_gc_incref( p_item );
367     return p_item;
368 }
369
370 @end
371
372 @implementation VLCInfo (NSMenuValidation)
373
374 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
375 {
376     BOOL bEnabled = TRUE;
377
378     if( [[o_mi title] isEqualToString: _NS("Information")] )
379     {
380         return ![[[VLCMain sharedInstance] playlist] isSelectionEmpty];
381     }
382
383     return TRUE;
384 }
385
386 @end
387
388 @implementation VLCInfo (NSTableDataSource)
389
390 - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
391 {
392     return (item == nil) ? [rootItem numberOfChildren] : [item numberOfChildren];
393 }
394
395 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
396     return ([item numberOfChildren] > 0);
397 }
398
399 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
400 {
401     return (item == nil) ? [rootItem childAtIndex:index] : (id)[item childAtIndex:index];
402 }
403
404 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
405 {
406     if ([[tableColumn identifier] isEqualToString:@"0"])
407     {
408         return (item == nil) ? @"" : (id)[item name];
409     }
410     else
411     {
412         return (item == nil) ? @"" : (id)[item value];
413     }
414 }
415
416 @end
417
418 @implementation VLCInfoTreeItem
419
420 #define IsALeafNode ((id)-1)
421
422 - (id)initWithName: (NSString *)o_item_name value: (NSString *)o_item_value ID: (int)i_id
423        parent:(VLCInfoTreeItem *)o_parent_item
424 {
425     self = [super init];
426
427     if( self != nil )
428     {
429         o_name = [o_item_name copy];
430         o_value = [o_item_value copy];
431         i_object_id = i_id;
432         o_parent = o_parent_item;
433         p_item = [[[VLCMain sharedInstance] info] item];
434         o_children = nil;
435     }
436     return( self );
437 }
438
439 - (id)init
440 {
441     return [self initWithName:@"main" value:@"" ID:-1 parent:nil];
442 }
443
444 - (void)dealloc
445 {
446     if( o_children != IsALeafNode ) [o_children release];
447     [o_name release];
448     [o_value release];
449     if( p_item ) vlc_gc_decref( p_item );
450     [super dealloc];
451 }
452
453 /* Creates and returns the array of children
454  * Loads children incrementally */
455 - (NSArray *)children
456 {
457     if( !p_item ) return nil;
458
459     if (o_children == NULL)
460     {
461         int i;
462
463         if( i_object_id == -1 )
464         {
465             vlc_mutex_lock( &p_item->lock );
466             o_children = [[NSMutableArray alloc] initWithCapacity: p_item->i_categories];
467             for (i = 0 ; i < p_item->i_categories ; i++)
468             {
469                 NSString * name = [NSString stringWithUTF8String: p_item->pp_categories[i]->psz_name];
470                 VLCInfoTreeItem * item = [[VLCInfoTreeItem alloc] initWithName:name value:@"" ID:i parent:self];
471                 [item autorelease];
472                 [o_children addObject:item];
473             }
474             vlc_mutex_unlock( &p_item->lock );
475         }
476         else if( o_parent->i_object_id == -1 )
477         {
478             vlc_mutex_lock( &p_item->lock );
479             info_category_t * cat = p_item->pp_categories[i_object_id];
480             o_children = [[NSMutableArray alloc] initWithCapacity: cat->i_infos];
481             for (i = 0 ; i < cat->i_infos ; i++)
482             {
483                 NSString * name = [NSString stringWithUTF8String: cat->pp_infos[i]->psz_name];
484                 NSString * value = [NSString stringWithUTF8String: cat->pp_infos[i]->psz_value ? : ""];
485                 VLCInfoTreeItem * item = [[VLCInfoTreeItem alloc] initWithName:name value:value ID:i parent:self];
486                 [item autorelease];
487                 [o_children addObject:item];
488             }
489             vlc_mutex_unlock( &p_item->lock );
490         }
491         else
492         {
493             o_children = IsALeafNode;
494         }
495     }
496     return o_children;
497 }
498
499 - (NSString *)name
500 {
501     return [[o_name retain] autorelease];
502 }
503
504 - (NSString *)value
505 {
506     return [[o_value retain] autorelease];
507 }
508
509 - (void)refresh
510 {
511     input_item_t * oldItem = p_item;
512     p_item = [[[VLCMain sharedInstance] info] item];
513     if( oldItem && oldItem != p_item ) vlc_gc_decref( oldItem );
514
515     [o_children release];
516     o_children = nil;
517 }
518
519 - (VLCInfoTreeItem *)childAtIndex:(NSUInteger)i_index {
520     return [[self children] objectAtIndex:i_index];
521 }
522
523 - (int)numberOfChildren {
524
525     id i_tmp = [self children];
526     return ( i_tmp == IsALeafNode ) ? (-1) : (int)[i_tmp count];
527 }
528
529 @end
530