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