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