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