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