]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlistinfo.m
macosx: CAS: sync steppers and fields, and add NumberFormatters (fixes #8598)
[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     [o_info_window setFloatingPanel: NO];
69     if (!OSX_SNOW_LEOPARD)
70         [o_info_window setCollectionBehavior: NSWindowCollectionBehaviorFullScreenAuxiliary];
71
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")];
77
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];
82
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)];
94
95     /* statistics */
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")];
101
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")];
106
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")];
111
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")];
116
117     [o_info_window setInitialFirstResponder: o_uri_txt];
118     [o_info_window setDelegate: self];
119
120     b_awakeFromNib = YES;
121
122     /* We may be awoken from nib way after initialisation
123      * Update ourselves */
124     [self updatePanelWithItem:p_item];
125 }
126
127
128 - (void)dealloc
129 {
130     [rootItem release];
131
132     if (p_item)
133         vlc_gc_decref(p_item);
134
135     [super dealloc];
136 }
137
138 - (void)updateCocoaWindowLevel:(NSInteger)i_level
139 {
140     if (o_info_window && [o_info_window isVisible] && [o_info_window level] != i_level)
141         [o_info_window setLevel: i_level];
142 }
143
144 - (void)initPanel
145 {
146     b_stats = config_GetInt(VLCIntf, "stats");
147     if (!b_stats) {
148         if ([o_tab_view numberOfTabViewItems] > 2)
149             [o_tab_view removeTabViewItem: [o_tab_view tabViewItemAtIndex: 2]];
150     }
151     else
152         [self initMediaPanelStats];
153
154     NSInteger i_level = [[[VLCMain sharedInstance] voutController] currentWindowLevel];
155     [o_info_window setLevel: i_level];
156     [o_info_window makeKeyAndOrderFront: self];
157 }
158
159 - (void)initMediaPanelStats
160 {
161     //Initializing Input Variables
162     [o_read_bytes_txt setStringValue: [NSString stringWithFormat:_NS("%.1f KiB"), (float)0]];
163     [o_input_bitrate_txt setStringValue: [NSString stringWithFormat:@"%6.0f kb/s", (float)0]];
164     [o_demux_bytes_txt setStringValue: [NSString stringWithFormat:_NS("%.1f KiB"), (float)0]];
165     [o_demux_bitrate_txt setStringValue: [NSString stringWithFormat:@"%6.0f kb/s", (float)0]];
166
167     //Initializing Video Variables
168     [o_video_decoded_txt setIntValue:0];
169     [o_displayed_txt setIntValue:0];
170     [o_lost_frames_txt setIntValue:0];
171
172     //Initializing Output Variables
173     [o_sent_packets_txt setIntValue: 0];
174     [o_sent_bytes_txt setStringValue: [NSString stringWithFormat:_NS("%.1f KiB"), (float)0]];
175     [o_sent_bitrate_txt setStringValue: [NSString stringWithFormat:@"%6.0f kb/s", (float)0]];
176
177     //Initializing Audio Variables
178     [o_audio_decoded_txt setIntValue:0];
179     [o_played_abuffers_txt setIntValue: 0];
180     [o_lost_abuffers_txt setIntValue: 0];
181
182 }
183
184 - (void)updatePanelWithItem:(input_item_t *)_p_item;
185 {
186     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
187     if (_p_item != p_item) {
188         if (p_item) vlc_gc_decref(p_item);
189         [o_saveMetaData_btn setEnabled: NO];
190         if (_p_item) vlc_gc_incref(_p_item);
191         p_item = _p_item;
192     }
193
194     if (!p_item) {
195         /* Erase */
196     #define SET( foo ) \
197         [self setMeta: "" forLabel: o_##foo##_txt];
198         SET( uri );
199         SET( title );
200         SET( author );
201         SET( collection );
202         SET( seqNum );
203         SET( genre );
204         SET( copyright );
205         SET( publisher );
206         SET( nowPlaying );
207         SET( language );
208         SET( date );
209         SET( description );
210         SET( encodedby );
211     #undef SET
212         [o_image_well setImage: [NSImage imageNamed: @"noart.png"]];
213     } else {
214         if (!input_item_IsPreparsed(p_item))
215             playlist_PreparseEnqueue(pl_Get(VLCIntf), p_item);
216
217         /* fill uri info */
218         char * psz_url = decode_URI(input_item_GetURI(p_item));
219         [o_uri_txt setStringValue: @(psz_url ? psz_url : "")];
220         free(psz_url);
221
222         /* fill title info */
223         char * psz_title = input_item_GetTitle(p_item);
224         if (!psz_title)
225             psz_title = input_item_GetName(p_item);
226         [o_title_txt setStringValue: @(psz_title ? : "")];
227         free(psz_title);
228
229     #define SET( foo, bar ) \
230         char *psz_##foo = input_item_Get##bar ( p_item ); \
231         [self setMeta: psz_##foo forLabel: o_##foo##_txt]; \
232         FREENULL( psz_##foo );
233
234         /* fill the other fields */
235         SET( author, Artist );
236         SET( collection, Album );
237         SET( seqNum, TrackNum );
238         SET( genre, Genre );
239         SET( copyright, Copyright );
240         SET( publisher, Publisher );
241         SET( nowPlaying, NowPlaying );
242         SET( language, Language );
243         SET( date, Date );
244         SET( description, Description );
245         SET( encodedby, EncodedBy );
246
247     #undef SET
248
249         char *psz_meta;
250         NSImage *o_image;
251         psz_meta = input_item_GetArtURL(p_item);
252
253         /* FIXME Can also be attachment:// */
254         if (psz_meta && strncmp(psz_meta, "attachment://", 13))
255             o_image = [[NSImage alloc] initWithContentsOfURL: [NSURL URLWithString:@(psz_meta)]];
256         else
257             o_image = [[NSImage imageNamed: @"noart.png"] retain];
258         [o_image_well setImage: o_image];
259         [o_image release];
260         FREENULL(psz_meta);
261     }
262
263     /* reload the advanced table */
264     [rootItem refresh];
265     [o_outline_view reloadData];
266
267     /* update the stats once to display p_item change faster */
268     [self updateStatistics];
269     [o_pool release];
270 }
271
272 - (void)setMeta: (char *)psz_meta forLabel: (id)theItem
273 {
274     if (psz_meta != NULL && *psz_meta)
275         [theItem setStringValue: @(psz_meta)];
276     else
277         [theItem setStringValue: @""];
278 }
279
280 - (void)updateStatistics
281 {
282     if (!b_awakeFromNib || !b_stats)
283         return;
284
285     if ([o_info_window isVisible]) {
286         if (!p_item || !p_item->p_stats) {
287             [self initMediaPanelStats];
288             return;
289         }
290
291         vlc_mutex_lock(&p_item->p_stats->lock);
292
293         /* input */
294         [o_read_bytes_txt setStringValue: [NSString stringWithFormat:
295             @"%8.0f KiB", (float)(p_item->p_stats->i_read_bytes)/1024]];
296         [o_input_bitrate_txt setStringValue: [NSString stringWithFormat:
297             @"%6.0f kb/s", (float)(p_item->p_stats->f_input_bitrate)*8000]];
298         [o_demux_bytes_txt setStringValue: [NSString stringWithFormat:
299             @"%8.0f KiB", (float)(p_item->p_stats->i_demux_read_bytes)/1024]];
300         [o_demux_bitrate_txt setStringValue: [NSString stringWithFormat:
301             @"%6.0f kb/s", (float)(p_item->p_stats->f_demux_bitrate)*8000]];
302
303         /* Video */
304         [o_video_decoded_txt setIntValue: p_item->p_stats->i_decoded_video];
305         [o_displayed_txt setIntValue: p_item->p_stats->i_displayed_pictures];
306         [o_lost_frames_txt setIntValue: p_item->p_stats->i_lost_pictures];
307
308         /* Sout */
309         [o_sent_packets_txt setIntValue: p_item->p_stats->i_sent_packets];
310         [o_sent_bytes_txt setStringValue: [NSString stringWithFormat: @"%8.0f KiB",
311             (float)(p_item->p_stats->i_sent_bytes)/1024]];
312         [o_sent_bitrate_txt setStringValue: [NSString stringWithFormat:
313             @"%6.0f kb/s", (float)(p_item->p_stats->f_send_bitrate*8)*1000]];
314
315         /* Audio */
316         [o_audio_decoded_txt setIntValue: p_item->p_stats->i_decoded_audio];
317         [o_played_abuffers_txt setIntValue: p_item->p_stats->i_played_abuffers];
318         [o_lost_abuffers_txt setIntValue: p_item->p_stats->i_lost_abuffers];
319
320         vlc_mutex_unlock(&p_item->p_stats->lock);
321     }
322 }
323
324 - (IBAction)metaFieldChanged:(id)sender
325 {
326     [o_saveMetaData_btn setEnabled: YES];
327 }
328
329 - (IBAction)saveMetaData:(id)sender
330 {
331     if (!p_item)
332         goto error;
333
334     #define utf8( o_blub ) \
335         [[o_blub stringValue] UTF8String]
336
337     input_item_SetName( p_item, utf8( o_title_txt ) );
338     input_item_SetTitle( p_item, utf8( o_title_txt ) );
339     input_item_SetArtist( p_item, utf8( o_author_txt ) );
340     input_item_SetAlbum( p_item, utf8( o_collection_txt ) );
341     input_item_SetGenre( p_item, utf8( o_genre_txt ) );
342     input_item_SetTrackNum( p_item, utf8( o_seqNum_txt ) );
343     input_item_SetDate( p_item, utf8( o_date_txt ) );
344     input_item_SetCopyright( p_item, utf8( o_copyright_txt ) );
345     input_item_SetPublisher( p_item, utf8( o_publisher_txt ) );
346     input_item_SetDescription( p_item, utf8( o_description_txt ) );
347     input_item_SetLanguage( p_item, utf8( o_language_txt ) );
348
349     playlist_t * p_playlist = pl_Get(VLCIntf);
350     input_item_WriteMeta(VLC_OBJECT(p_playlist), p_item);
351
352     var_SetBool(p_playlist, "intf-change", true);
353     [self updatePanelWithItem: p_item];
354
355     [o_saveMetaData_btn setEnabled: NO];
356     return;
357
358 error:
359     NSRunAlertPanel(_NS("Error while saving meta"),
360         _NS("VLC was unable to save the meta data."),
361         _NS("OK"), nil, nil);
362 }
363
364 - (IBAction)downloadCoverArt:(id)sender
365 {
366     playlist_t * p_playlist = pl_Get(VLCIntf);
367     if (p_item) playlist_AskForArtEnqueue(p_playlist, p_item);
368 }
369
370 - (input_item_t *)item
371 {
372     if (p_item) vlc_gc_incref(p_item);
373     return p_item;
374 }
375
376 @end
377
378 @implementation VLCInfo (NSMenuValidation)
379
380 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
381 {
382     BOOL bEnabled = TRUE;
383
384     if ([[o_mi title] isEqualToString: _NS("Information")]) {
385         return ![[[VLCMain sharedInstance] playlist] isSelectionEmpty];
386     }
387
388     return TRUE;
389 }
390
391 @end
392
393 @implementation VLCInfo (NSTableDataSource)
394
395 - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
396 {
397     return (item == nil) ? [rootItem numberOfChildren] : [item numberOfChildren];
398 }
399
400 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
401     return ([item numberOfChildren] > 0);
402 }
403
404 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
405 {
406     return (item == nil) ? [rootItem childAtIndex:index] : (id)[item childAtIndex:index];
407 }
408
409 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
410 {
411     if ([[tableColumn identifier] isEqualToString:@"0"])
412         return (item == nil) ? @"" : (id)[item name];
413     else
414         return (item == nil) ? @"" : (id)[item value];
415 }
416
417 @end
418
419 @implementation VLCInfoTreeItem
420
421 @synthesize name = o_name, value = o_value;
422
423 #define IsALeafNode ((id)-1)
424
425 - (id)initWithName: (NSString *)o_item_name value: (NSString *)o_item_value ID: (int)i_id
426        parent:(VLCInfoTreeItem *)o_parent_item
427 {
428     self = [super init];
429
430     if (self != nil) {
431         o_name = [o_item_name copy];
432         o_value = [o_item_value copy];
433         i_object_id = i_id;
434         o_parent = o_parent_item;
435         p_item = [(VLCInfo *)[[VLCMain sharedInstance] info] item];
436         o_children = nil;
437     }
438     return self;
439 }
440
441 - (id)init
442 {
443     return [self initWithName:@"main" value:@"" ID:-1 parent:nil];
444 }
445
446 - (void)dealloc
447 {
448     if (o_children != IsALeafNode) [o_children release];
449     [o_name release];
450     [o_value release];
451     if (p_item) vlc_gc_decref(p_item);
452     [super dealloc];
453 }
454
455 /* Creates and returns the array of children
456  * Loads children incrementally */
457 - (NSArray *)children
458 {
459     if (!p_item)
460         return nil;
461
462     if (o_children == NULL) {
463         if (i_object_id == -1) {
464             vlc_mutex_lock(&p_item->lock);
465             o_children = [[NSMutableArray alloc] initWithCapacity: p_item->i_categories];
466             for (int i = 0 ; i < p_item->i_categories ; i++) {
467                 NSString * name = @(p_item->pp_categories[i]->psz_name);
468                 VLCInfoTreeItem * item = [[VLCInfoTreeItem alloc] initWithName:name value:@"" ID:i parent:self];
469                 [item autorelease];
470                 [o_children addObject:item];
471             }
472             vlc_mutex_unlock(&p_item->lock);
473         }
474         else if (o_parent->i_object_id == -1) {
475             vlc_mutex_lock(&p_item->lock);
476             info_category_t * cat = p_item->pp_categories[i_object_id];
477             o_children = [[NSMutableArray alloc] initWithCapacity: cat->i_infos];
478             for (int i = 0 ; i < cat->i_infos ; i++) {
479                 NSString * name = @(cat->pp_infos[i]->psz_name);
480                 NSString * value = @(cat->pp_infos[i]->psz_value ? : "");
481                 VLCInfoTreeItem * item = [[VLCInfoTreeItem alloc] initWithName:name value:value ID:i parent:self];
482                 [item autorelease];
483                 [o_children addObject:item];
484             }
485             vlc_mutex_unlock(&p_item->lock);
486         }
487         else
488             o_children = IsALeafNode;
489     }
490     return o_children;
491 }
492
493 - (void)refresh
494 {
495     input_item_t * oldItem = p_item;
496     p_item = [(VLCInfo *)[[VLCMain sharedInstance] info] item];
497     if (oldItem && oldItem != p_item)
498         vlc_gc_decref(oldItem);
499
500     [o_children release];
501     o_children = nil;
502 }
503
504 - (VLCInfoTreeItem *)childAtIndex:(NSUInteger)i_index {
505     return [self children][i_index];
506 }
507
508 - (int)numberOfChildren {
509
510     id i_tmp = [self children];
511     return (i_tmp == IsALeafNode) ? (-1) : (int)[i_tmp count];
512 }
513
514 @end
515