]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlistinfo.m
5031f9203a63b100d2781a1a424c4fd8b50622df
[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
33 /*****************************************************************************
34  * VLCPlaylistInfo Implementation
35  *****************************************************************************/
36
37 @implementation VLCInfo
38
39 static VLCInfo *_o_sharedInstance = nil;
40
41 + (VLCInfo *)sharedInstance
42 {
43     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
44 }
45
46 - (id)init
47 {
48     if( _o_sharedInstance )
49     {
50         [self dealloc];
51     }
52     else
53     {
54         _o_sharedInstance = [super init];
55         
56         if( _o_sharedInstance != nil )
57         {
58             p_item = NULL;
59             o_statUpdateTimer = nil;
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
95     /* statistics */
96     [o_input_box setTitle: _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_box setTitle: _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         [o_fps_lbl setStringValue: _NS("Frames per Second")];
107
108     [o_sout_box setTitle: _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_box setTitle: _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 - (void)stopTimers
127 {
128     /* make sure that the timer is released in any case */
129     if( o_statUpdateTimer && [o_statUpdateTimer isValid] )
130         [o_statUpdateTimer invalidate];
131
132     [rootItem release];
133
134     [o_statUpdateTimer release];
135     o_statUpdateTimer = nil;
136 }
137
138 - (void)dealloc
139 {
140     /* make sure that the timer is released in any case */
141     if( o_statUpdateTimer && [o_statUpdateTimer isValid] )
142         [o_statUpdateTimer invalidate];
143
144     [o_statUpdateTimer release];
145
146     if( p_item ) vlc_gc_decref( p_item );
147
148     [super dealloc];
149 }
150
151 - (void)windowDidBecomeKey:(NSNotification *)notification
152 {
153     BOOL b_stats = config_GetInt(VLCIntf, "stats");
154     if( b_stats )
155     {
156         if( o_statUpdateTimer )
157         {
158             [o_statUpdateTimer invalidate];
159             [o_statUpdateTimer release];
160             o_statUpdateTimer = nil;
161         }
162         o_statUpdateTimer = [NSTimer scheduledTimerWithTimeInterval: 1
163             target: self selector: @selector(updateStatistics:)
164             userInfo: nil repeats: YES];
165         [o_statUpdateTimer fire];
166         [o_statUpdateTimer retain];
167     }
168 }
169
170 - (BOOL)windowShouldClose:(id)sender
171 {
172     if( [o_statUpdateTimer isValid] )
173         [o_statUpdateTimer invalidate];
174
175     if( o_statUpdateTimer )
176         [o_statUpdateTimer release];
177     o_statUpdateTimer = nil;
178     return YES;
179 }
180
181 - (void)initPanel
182 {
183     BOOL b_stats = config_GetInt(VLCIntf, "stats");
184     if( !b_stats )
185     {
186         if( [o_tab_view numberOfTabViewItems] > 2 )
187             [o_tab_view removeTabViewItem: [o_tab_view tabViewItemAtIndex: 2]];
188     }
189
190     [self initMediaPanelStats];
191     [o_info_window makeKeyAndOrderFront: self];
192 }
193
194 - (void)initMediaPanelStats
195 {
196     //Initializing Input Variables
197     [o_read_bytes_txt setStringValue: [NSString stringWithFormat:@"%8.0f kB", (float)0]];
198     [o_input_bitrate_txt setStringValue: [NSString stringWithFormat:@"%6.0f kb/s", (float)0]];
199     [o_demux_bytes_txt setStringValue: [NSString stringWithFormat:@"%8.0f kB", (float)0]];
200     [o_demux_bitrate_txt setStringValue: [NSString stringWithFormat:@"%6.0f kb/s", (float)0]];
201     
202     //Initializing Video Variables
203     [o_video_decoded_txt setIntValue:0];
204     [o_displayed_txt setIntValue:0];
205     [o_lost_frames_txt setIntValue:0];
206     [o_fps_txt setFloatValue:0];
207
208     //Initializing Output Variables
209     [o_sent_packets_txt setIntValue: 0];
210     [o_sent_bytes_txt setStringValue: [NSString stringWithFormat:@"%8.0f kB", (float)0]];
211     [o_sent_bitrate_txt setStringValue: [NSString stringWithFormat:@"%6.0f kb/s", (float)0]];
212
213     //Initializing Audio Variables
214     [o_audio_decoded_txt setIntValue:0];
215     [o_played_abuffers_txt setIntValue: 0];
216     [o_lost_abuffers_txt setIntValue: 0];
217
218 }
219
220 - (void)updatePanelWithItem:(input_item_t *)_p_item;
221 {
222     if( _p_item != p_item )
223     {
224         if( p_item ) vlc_gc_decref( p_item );
225         [o_saveMetaData_btn setEnabled: NO];
226         if( _p_item ) vlc_gc_incref( _p_item );
227         p_item = _p_item;
228     }
229
230     if( !p_item )
231     {
232         /* Erase */
233     #define SET( foo ) \
234         [self setMeta: "" forLabel: o_##foo##_txt];
235         SET( uri );
236         SET( title );
237         SET( author );
238         SET( collection );
239         SET( seqNum );
240         SET( genre );
241         SET( copyright );
242         SET( publisher );
243         SET( nowPlaying );
244         SET( language );
245         SET( date );
246         SET( description );
247     #undef SET
248         [o_image_well setImage: [NSImage imageNamed: @"noart.png"]];
249     }
250     else
251     {
252         if( !input_item_IsPreparsed( p_item ) )
253         {
254             playlist_t * p_playlist = pl_Hold( VLCIntf );
255             PL_LOCK;
256             playlist_PreparseEnqueue( p_playlist, p_item, pl_Locked );
257             PL_UNLOCK;
258             pl_Release( VLCIntf );
259         }
260
261         /* fill uri info */
262         char * psz_url = input_item_GetURI( p_item );
263         [o_uri_txt setStringValue: [NSString stringWithUTF8String: psz_url ? psz_url : ""  ]];
264         free( psz_url );
265
266         /* fill title info */
267         char * psz_title = input_item_GetTitle( p_item );
268         if( !psz_title )
269             psz_title = input_item_GetName( p_item );
270         [o_title_txt setStringValue: [NSString stringWithUTF8String: psz_title ? : ""  ]];
271         free( psz_title );
272
273     #define SET( foo, bar ) \
274         char *psz_##foo = input_item_Get##bar ( p_item ); \
275         [self setMeta: psz_##foo forLabel: o_##foo##_txt]; \
276         FREENULL( psz_##foo );
277
278         /* fill the other fields */
279         SET( author, Artist );
280         SET( collection, Album );
281         SET( seqNum, TrackNum );
282         SET( genre, Genre );
283         SET( copyright, Copyright );
284         SET( publisher, Publisher );
285         SET( nowPlaying, NowPlaying );
286         SET( language, Language );
287         SET( date, Date );
288         SET( description, Description );
289
290     #undef SET
291
292         char *psz_meta;
293         NSImage *o_image;
294         psz_meta = input_item_GetArtURL( p_item );
295         if( psz_meta && !strncmp( psz_meta, "file://", 7 ) )
296             o_image = [[NSImage alloc] initWithContentsOfFile: [NSString stringWithUTF8String: psz_meta+7]];
297         else
298             o_image = [[NSImage imageNamed: @"noart.png"] retain];
299         [o_image_well setImage: o_image];
300         [o_image release];
301         FREENULL( psz_meta );
302     }
303
304     /* reload the advanced table */
305     [rootItem refresh];
306     [o_outline_view reloadData];
307
308     /* update the stats once to display p_item change faster */
309     [self updateStatistics: nil];
310 }
311
312 - (void)setMeta: (char *)psz_meta forLabel: (id)theItem
313 {
314     if( psz_meta != NULL && *psz_meta)
315         [theItem setStringValue: [NSString stringWithUTF8String:psz_meta]];
316     else
317         [theItem setStringValue: @""];
318 }
319
320 - (void)updateStatistics:(NSTimer*)theTimer
321 {
322     if( !p_item || !p_item->p_stats )
323     {
324         [self initMediaPanelStats];
325         return;
326     }
327
328     vlc_mutex_lock( &p_item->p_stats->lock );
329
330     /* input */
331     [o_read_bytes_txt setStringValue: [NSString stringWithFormat:
332         @"%8.0f kB", (float)(p_item->p_stats->i_read_bytes)/1000]];
333     [o_input_bitrate_txt setStringValue: [NSString stringWithFormat:
334         @"%6.0f kb/s", (float)(p_item->p_stats->f_input_bitrate)*8000]];
335     [o_demux_bytes_txt setStringValue: [NSString stringWithFormat:
336         @"%8.0f kB", (float)(p_item->p_stats->i_demux_read_bytes)/1000]];
337     [o_demux_bitrate_txt setStringValue: [NSString stringWithFormat:
338         @"%6.0f kb/s", (float)(p_item->p_stats->f_demux_bitrate)*8000]];
339
340     /* Video */
341     [o_video_decoded_txt setIntValue: p_item->p_stats->i_decoded_video];
342     [o_displayed_txt setIntValue: p_item->p_stats->i_displayed_pictures];
343     [o_lost_frames_txt setIntValue: p_item->p_stats->i_lost_pictures];
344     float f_fps = 0;
345     /* FIXME: input_Control( p_item, INPUT_GET_VIDEO_FPS, &f_fps ); */
346     [o_fps_txt setFloatValue: f_fps];
347
348     /* Sout */
349     [o_sent_packets_txt setIntValue: p_item->p_stats->i_sent_packets];
350     [o_sent_bytes_txt setStringValue: [NSString stringWithFormat: @"%8.0f kB",
351         (float)(p_item->p_stats->i_sent_bytes)/1000]];
352     [o_sent_bitrate_txt setStringValue: [NSString stringWithFormat:
353         @"%6.0f kb/s", (float)(p_item->p_stats->f_send_bitrate*8)*1000]];
354
355     /* Audio */
356     [o_audio_decoded_txt setIntValue: p_item->p_stats->i_decoded_audio];
357     [o_played_abuffers_txt setIntValue: p_item->p_stats->i_played_abuffers];
358     [o_lost_abuffers_txt setIntValue: p_item->p_stats->i_lost_abuffers];
359
360     vlc_mutex_unlock( &p_item->p_stats->lock );
361 }
362
363 - (IBAction)metaFieldChanged:(id)sender
364 {
365     [o_saveMetaData_btn setEnabled: YES];
366 }
367
368 - (IBAction)saveMetaData:(id)sender
369 {
370     playlist_t * p_playlist = pl_Hold( VLCIntf );
371     vlc_value_t val;
372
373     if( !p_item ) goto error;
374
375     meta_export_t p_export;
376     p_export.p_item = p_item;
377
378     /* we can write meta data only in a file */
379     vlc_mutex_lock( &p_item->lock );
380     int i_type = p_item->i_type;
381     vlc_mutex_unlock( &p_item->lock );
382
383     if( i_type != ITEM_TYPE_FILE )
384         goto error;
385
386     char *psz_uri_orig = input_item_GetURI( p_item );
387     char *psz_uri = psz_uri_orig;
388     if( !strncmp( psz_uri, "file://", 7 ) )
389         psz_uri += 7; /* strlen("file://") = 7 */
390
391     p_export.psz_file = strndup( psz_uri, PATH_MAX );
392     free( psz_uri_orig );
393
394     #define utf8( o_blub ) \
395         [[o_blub stringValue] UTF8String]
396
397     input_item_SetName( p_item, utf8( o_title_txt ) );
398     input_item_SetTitle( p_item, utf8( o_title_txt ) );
399     input_item_SetArtist( p_item, utf8( o_author_txt ) );
400     input_item_SetAlbum( p_item, utf8( o_collection_txt ) );
401     input_item_SetGenre( p_item, utf8( o_genre_txt ) );
402     input_item_SetTrackNum( p_item, utf8( o_seqNum_txt ) );
403     input_item_SetDate( p_item, utf8( o_date_txt ) );
404     input_item_SetCopyright( p_item, utf8( o_copyright_txt ) );
405     input_item_SetPublisher( p_item, utf8( o_publisher_txt ) );
406     input_item_SetDescription( p_item, utf8( o_description_txt ) );
407     input_item_SetLanguage( p_item, utf8( o_language_txt ) );
408
409     PL_LOCK;
410     p_playlist->p_private = &p_export;
411
412     module_t *p_mod = module_need( p_playlist, "meta writer", NULL, false );
413     if( p_mod )
414         module_unneed( p_playlist, p_mod );
415     PL_UNLOCK;
416
417     val.b_bool = true;
418     var_Set( p_playlist, "intf-change", val );
419     [self updatePanelWithItem: p_item];
420
421     pl_Release( VLCIntf );
422     [o_saveMetaData_btn setEnabled: NO];
423     return;
424
425 error:
426     pl_Release( VLCIntf );
427     NSRunAlertPanel(_NS("Error while saving meta"),
428         _NS("VLC was unable to save the meta data."),
429         _NS("OK"), nil, nil);
430 }
431
432 - (IBAction)downloadCoverArt:(id)sender
433 {
434     playlist_t * p_playlist = pl_Hold( VLCIntf );
435     if( p_item) playlist_AskForArtEnqueue( p_playlist, p_item, pl_Unlocked );
436     pl_Release( VLCIntf );
437 }
438
439 - (input_item_t *)item
440 {
441     if( p_item ) vlc_gc_incref( p_item );
442     return p_item;
443 }
444
445 @end
446
447 @implementation VLCInfo (NSMenuValidation)
448
449 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
450 {
451     BOOL bEnabled = TRUE;
452
453     if( [[o_mi title] isEqualToString: _NS("Information")] )
454     {
455         return ![[[VLCMain sharedInstance] getPlaylist] isSelectionEmpty];
456     }
457
458     return TRUE;
459 }
460
461 @end
462
463 @implementation VLCInfo (NSTableDataSource)
464
465 - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
466 {
467     return (item == nil) ? [rootItem numberOfChildren] : [item numberOfChildren];
468 }
469
470 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
471     return ([item numberOfChildren] > 0);
472 }
473
474 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
475 {
476     return (item == nil) ? [rootItem childAtIndex:index] : (id)[item childAtIndex:index];
477 }
478
479 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
480 {
481     if ([[tableColumn identifier] isEqualToString:@"0"])
482     {
483         return (item == nil) ? @"" : (id)[item name];
484     }
485     else
486     {
487         return (item == nil) ? @"" : (id)[item value];
488     }
489 }
490
491 @end
492
493 @implementation VLCInfoTreeItem
494
495 #define IsALeafNode ((id)-1)
496
497 - (id)initWithName: (NSString *)o_item_name value: (NSString *)o_item_value ID: (int)i_id
498        parent:(VLCInfoTreeItem *)o_parent_item
499 {
500     self = [super init];
501
502     if( self != nil )
503     {
504         o_name = [o_item_name copy];
505         o_value = [o_item_value copy];
506         i_object_id = i_id;
507         o_parent = o_parent_item;
508         p_item = [[[VLCMain sharedInstance] getInfo] item];
509         o_children = nil;
510     }
511     return( self );
512 }
513
514 - (id)init
515 {
516     return [self initWithName:@"main" value:@"" ID:-1 parent:nil];
517 }
518
519 - (void)dealloc
520 {
521     if( o_children != IsALeafNode ) [o_children release];
522     [o_name release];
523     [o_value release];
524     if( p_item ) vlc_gc_decref( p_item );
525     [super dealloc];
526 }
527
528 /* Creates and returns the array of children
529  * Loads children incrementally */
530 - (NSArray *)children
531 {
532     if( !p_item ) return nil;
533
534     if (o_children == NULL)
535     {
536         int i;
537
538         if( i_object_id == -1 )
539         {
540             vlc_mutex_lock( &p_item->lock );
541             o_children = [[NSMutableArray alloc] initWithCapacity: p_item->i_categories];
542             for (i = 0 ; i < p_item->i_categories ; i++)
543             {
544                 NSString * name = [NSString stringWithUTF8String: p_item->pp_categories[i]->psz_name];
545                 VLCInfoTreeItem * item = [[VLCInfoTreeItem alloc] initWithName:name value:@"" ID:i parent:self];
546                 [item autorelease];
547                 [o_children addObject:item];
548             }
549             vlc_mutex_unlock( &p_item->lock );
550         }
551         else if( o_parent->i_object_id == -1 )
552         {
553             vlc_mutex_lock( &p_item->lock );
554             info_category_t * cat = p_item->pp_categories[i_object_id];
555             o_children = [[NSMutableArray alloc] initWithCapacity: cat->i_infos];
556             for (i = 0 ; i < cat->i_infos ; i++)
557             {
558                 NSString * name = [NSString stringWithUTF8String: cat->pp_infos[i]->psz_name];
559                 NSString * value = [NSString stringWithUTF8String: cat->pp_infos[i]->psz_value ? : ""];
560                 VLCInfoTreeItem * item = [[VLCInfoTreeItem alloc] initWithName:name value:value ID:i parent:self];
561                 [item autorelease];
562                 [o_children addObject:item];
563             }
564             vlc_mutex_unlock( &p_item->lock );
565         }
566         else
567         {
568             o_children = IsALeafNode;
569         }
570     }
571     return o_children;
572 }
573
574 - (NSString *)name
575 {
576     return [[o_name retain] autorelease];
577 }
578
579 - (NSString *)value
580 {
581     return [[o_value retain] autorelease];
582 }
583
584 - (void)refresh
585 {
586     input_item_t * oldItem = p_item;
587     p_item = [[[VLCMain sharedInstance] getInfo] item];
588     if( oldItem && oldItem != p_item ) vlc_gc_decref( oldItem );
589
590     [o_children release];
591     o_children = nil;
592 }
593
594 - (VLCInfoTreeItem *)childAtIndex:(int)i_index {
595     return [[self children] objectAtIndex:i_index];
596 }
597
598 - (int)numberOfChildren {
599
600     id i_tmp = [self children];
601     return ( i_tmp == IsALeafNode ) ? (-1) : (int)[i_tmp count];
602 }
603
604 @end
605