]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlistinfo.m
* don't show the statistics-tab if the stats aren't enabled
[vlc] / modules / gui / macosx / playlistinfo.m
1 /*****************************************************************************
2  r playlistinfo.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2002-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Benjamin Pracht <bigben at videolan dot org>
8  *          Felix 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 - (id)init
40 {
41     self = [super init];
42
43     if( self != nil )
44     {
45         p_item = NULL;
46     }
47     return( self );
48 }
49
50 - (void)awakeFromNib
51 {
52     [o_info_window setExcludedFromWindowsMenu: TRUE];
53
54     [o_info_window setTitle: _NS("Information")];
55     [o_uri_lbl setStringValue: _NS("URI")];
56     [o_title_lbl setStringValue: _NS("Title")];
57     [o_author_lbl setStringValue: _NS("Author")];
58     [o_btn_ok setTitle: _NS("OK")];
59     [o_btn_cancel setTitle: _NS("Cancel")];
60     
61     [[o_tab_view tabViewItemAtIndex: 0] setLabel: _NS("General")];
62     [[o_tab_view tabViewItemAtIndex: 1] setLabel: _NS("Advanced Information")];
63     [[o_tab_view tabViewItemAtIndex: 2] setLabel: _NS("Statistics")];
64     [o_tab_view selectTabViewItemAtIndex: 0];
65
66     /* constants defined in vlc_meta.h */
67     [o_genre_lbl setStringValue: _NS(VLC_META_GENRE)];
68     [o_copyright_lbl setStringValue: _NS(VLC_META_COPYRIGHT)];
69     [o_collection_lbl setStringValue: _NS(VLC_META_COLLECTION)];
70     [o_seqNum_lbl setStringValue: _NS(VLC_META_SEQ_NUM)];
71     [o_description_lbl setStringValue: _NS(VLC_META_DESCRIPTION)];
72     [o_rating_lbl setStringValue: _NS(VLC_META_RATING)];
73     [o_date_lbl setStringValue: _NS(VLC_META_DATE)];
74     [o_language_lbl setStringValue: _NS(VLC_META_LANGUAGE)];
75     [o_nowPlaying_lbl setStringValue: _NS(VLC_META_NOW_PLAYING)];
76     [o_publisher_lbl setStringValue: _NS(VLC_META_PUBLISHER)];
77     
78     /* statistics */
79     [o_input_box setTitle: _NS("Input")];
80     [o_read_bytes_lbl setStringValue: _NS("Read at media")];
81     [o_input_bitrate_lbl setStringValue: _NS("Input bitrate")];
82     [o_demux_bytes_lbl setStringValue: _NS("Demuxed")];
83     [o_demux_bitrate_lbl setStringValue: _NS("Stream bitrate")];
84     
85     [o_video_box setTitle: _NS("Video")];
86     [o_video_decoded_lbl setStringValue: _NS("Decoded blocks")];
87     [o_displayed_lbl setStringValue: _NS("Displayed frames")];
88     [o_lost_frames_lbl setStringValue: _NS("Lost frames")];
89     
90     [o_sout_box setTitle: _NS("Streaming")];
91     [o_sent_packets_lbl setStringValue: _NS("Sent packets")];
92     [o_sent_bytes_lbl setStringValue: _NS("Sent bytes")];
93     [o_sent_bitrate_lbl setStringValue: _NS("Send rate")];
94     
95     [o_audio_box setTitle: _NS("Audio")];
96     [o_audio_decoded_lbl setStringValue: _NS("Decoded blocks")];
97     [o_played_abuffers_lbl setStringValue: _NS("Played buffers")];
98     [o_lost_abuffers_lbl setStringValue: _NS("Lost buffers")];
99 }
100
101 - (IBAction)togglePlaylistInfoPanel:(id)sender
102 {
103     if( [o_info_window isVisible] )
104     {
105         [o_info_window orderOut: sender];
106     }
107     else
108     {
109         p_item = [[[VLCMain sharedInstance] getPlaylist] selectedPlaylistItem];
110         [self initPanel:sender];
111     }
112 }
113
114 - (IBAction)toggleInfoPanel:(id)sender
115 {
116     if( [o_info_window isVisible] )
117     {
118         if( o_statUpdateTimer )
119             [o_statUpdateTimer invalidate];
120         [o_info_window orderOut: sender];
121     }
122     else
123     {
124         intf_thread_t * p_intf = VLCIntf;
125         playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
126                                           FIND_ANYWHERE );
127
128         if( p_playlist )
129         {
130             p_item = p_playlist->status.p_item;
131             vlc_object_release( p_playlist );
132         }
133
134         BOOL b_stats = config_GetInt(VLCIntf, "stats");
135         if( b_stats )
136         {
137             o_statUpdateTimer = [NSTimer scheduledTimerWithTimeInterval: 1 \
138                 target: self selector: @selector(updateStatistics:) \
139                 userInfo: nil repeats: YES]; 
140             [o_statUpdateTimer fire];
141             [o_statUpdateTimer retain];
142         }
143
144         [self initPanel:sender];
145     }
146 }
147
148 - (void)initPanel:(id)sender
149 {
150     /* check whether our item is valid, because we would crash if not */
151     if(! [self isItemInPlaylist: p_item] )
152         return;
153
154     char *psz_temp;
155     vlc_mutex_lock( &p_item->input.lock );
156
157     /* fill uri / title / author info */
158     if( p_item->input.psz_uri )
159     {
160         [o_uri_txt setStringValue:
161             ([NSString stringWithUTF8String:p_item->input.psz_uri] == nil ) ?
162             [NSString stringWithCString:p_item->input.psz_uri] :
163             [NSString stringWithUTF8String:p_item->input.psz_uri]];
164     }
165
166     if( p_item->input.psz_name )
167     {
168         [o_title_txt setStringValue:
169             ([NSString stringWithUTF8String:p_item->input.psz_name] == nil ) ?
170             [NSString stringWithCString:p_item->input.psz_name] :
171             [NSString stringWithUTF8String:p_item->input.psz_name]];
172     }
173     vlc_mutex_unlock( &p_item->input.lock );
174
175     psz_temp = vlc_input_item_GetInfo( &p_item->input, _("Meta-information"), _("Artist") );
176
177     if( psz_temp )
178     {
179         [o_author_txt setStringValue: [NSString stringWithUTF8String: psz_temp]];
180         free( psz_temp );
181     }
182
183     /* fill the other fields */
184     [self setMeta: VLC_META_GENRE forLabel: o_genre_txt];
185     [self setMeta: VLC_META_COPYRIGHT forLabel: o_copyright_txt];
186     [self setMeta: VLC_META_COLLECTION forLabel: o_collection_txt];
187     [self setMeta: VLC_META_SEQ_NUM forLabel: o_seqNum_txt];
188     [self setMeta: VLC_META_DESCRIPTION forLabel: o_description_txt];
189     [self setMeta: VLC_META_RATING forLabel: o_rating_txt];
190     [self setMeta: VLC_META_DATE forLabel: o_date_txt];
191     [self setMeta: VLC_META_LANGUAGE forLabel: o_language_txt];
192     [self setMeta: VLC_META_NOW_PLAYING forLabel: o_nowPlaying_txt];
193     [self setMeta: VLC_META_PUBLISHER forLabel: o_publisher_txt];
194
195     /* reload the advanced table */
196     [[VLCInfoTreeItem rootItem] refresh];
197     [o_outline_view reloadData];
198
199     BOOL b_stats = config_GetInt(VLCIntf, "stats");
200     if(! b_stats )
201     {
202         if( [o_tab_view numberOfTabViewItems] > 2 )
203             [o_tab_view removeTabViewItem: [o_tab_view tabViewItemAtIndex: 2]];
204     }
205     else
206     {
207         [self updateStatistics: nil];
208     }
209
210     [o_info_window makeKeyAndOrderFront: sender];
211 }
212
213 - (void)setMeta: (char *)meta forLabel: (id)theItem
214 {
215     char *psz_meta = vlc_input_item_GetInfo( &p_item->input, \
216         _(VLC_META_INFO_CAT), _(meta) );
217     if( psz_meta != NULL && *psz_meta)
218         [theItem setStringValue: [NSString stringWithUTF8String: psz_meta]];
219     else
220         [theItem setStringValue: @"-"];
221 }
222
223 - (void)updateStatistics:(NSTimer*)theTimer
224 {
225     if( [self isItemInPlaylist: p_item] )
226     {
227         /* we can only do that if there's a valid input around */
228         
229         vlc_mutex_lock( &p_item->input.p_stats->lock );
230
231         /* input */
232         [o_read_bytes_txt setStringValue: [NSString stringWithFormat: \
233             @"%8.0f kB", (float)(p_item->input.p_stats->i_read_bytes)/1000]];
234         [o_input_bitrate_txt setStringValue: [NSString stringWithFormat: \
235             @"%6.0f kb/s", (float)(p_item->input.p_stats->f_input_bitrate)*8000]];
236         [o_demux_bytes_txt setStringValue: [NSString stringWithFormat: \
237             @"%8.0f kB", (float)(p_item->input.p_stats->i_demux_read_bytes)/1000]];
238         [o_demux_bitrate_txt setStringValue: [NSString stringWithFormat: \
239             @"%6.0f kb/s", (float)(p_item->input.p_stats->f_demux_bitrate)*8000]];
240
241         /* Video */
242         [o_video_decoded_txt setStringValue: [NSString stringWithFormat: @"%5i", \
243             p_item->input.p_stats->i_decoded_video]];
244         [o_displayed_txt setStringValue: [NSString stringWithFormat: @"%5i", \
245             p_item->input.p_stats->i_displayed_pictures]];
246         [o_lost_frames_txt setStringValue: [NSString stringWithFormat: @"%5i", \
247             p_item->input.p_stats->i_lost_pictures]];
248
249         /* Sout */
250         [o_sent_packets_txt setStringValue: [NSString stringWithFormat: @"%5i", \
251             p_item->input.p_stats->i_sent_packets]];
252         [o_sent_bytes_txt setStringValue: [NSString stringWithFormat: @"%8.0f kB", \
253             (float)(p_item->input.p_stats->i_sent_bytes)/1000]];
254         [o_sent_bitrate_txt setStringValue: [NSString stringWithFormat: \
255             @"%6.0f kb/s", (float)(p_item->input.p_stats->f_send_bitrate*8)*1000]];
256
257         /* Audio */
258         [o_audio_decoded_txt setStringValue: [NSString stringWithFormat: @"%5i", \
259             p_item->input.p_stats->i_decoded_audio]];
260         [o_played_abuffers_txt setStringValue: [NSString stringWithFormat: @"%5i", \
261             p_item->input.p_stats->i_played_abuffers]];
262         [o_lost_abuffers_txt setStringValue: [NSString stringWithFormat: @"%5i", \
263             p_item->input.p_stats->i_lost_abuffers]];
264
265         vlc_mutex_unlock( &p_item->input.p_stats->lock );
266     }
267 }
268
269 - (IBAction)infoCancel:(id)sender
270 {
271     [o_info_window orderOut: self];
272 }
273
274
275 - (IBAction)infoOk:(id)sender
276 {
277     intf_thread_t * p_intf = VLCIntf;
278     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
279                                           FIND_ANYWHERE );
280     vlc_value_t val;
281
282     if( [self isItemInPlaylist: p_item] )
283     {
284         vlc_mutex_lock( &p_item->input.lock );
285
286         p_item->input.psz_uri = strdup( [[o_uri_txt stringValue] UTF8String] );
287         p_item->input.psz_name = strdup( [[o_title_txt stringValue] UTF8String] );
288         vlc_mutex_unlock( &p_item->input.lock );
289         vlc_input_item_AddInfo( &p_item->input, _("Meta-information"), _("Artist"), [[o_author_txt stringValue] UTF8String]);
290         
291         val.b_bool = VLC_TRUE;
292         var_Set( p_playlist, "intf-change", val );
293     }
294     vlc_object_release( p_playlist );
295     [o_info_window orderOut: self];
296 }
297
298 - (playlist_item_t *)getItem
299 {
300     return p_item;
301 }
302
303 - (BOOL)isItemInPlaylist:(playlist_item_t *)p_local_item
304 {
305     intf_thread_t * p_intf = VLCIntf;
306     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
307                                           FIND_ANYWHERE );
308     int i;
309
310     if( p_playlist == NULL )
311     {
312         return NO;
313     }
314
315     for( i = 0 ; i < p_playlist->i_size ; i++ )
316     {
317         if( p_playlist->pp_items[i] == p_local_item )
318         {
319             vlc_object_release( p_playlist );
320             return YES;
321         }
322     }
323     vlc_object_release( p_playlist );
324     return NO;
325 }
326
327 @end
328
329 @implementation VLCInfo (NSMenuValidation)
330
331 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
332 {
333     BOOL bEnabled = TRUE;
334
335     intf_thread_t * p_intf = VLCIntf;
336     input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
337                                                        FIND_ANYWHERE );
338
339     if( [[o_mi title] isEqualToString: _NS("Info")] )
340     {
341         if( p_input == NULL )
342         {
343             bEnabled = FALSE;
344         }
345     }
346     if( p_input ) vlc_object_release( p_input );
347
348     return( bEnabled );
349 }
350
351 @end
352
353 @implementation VLCInfo (NSTableDataSource)
354
355 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
356 {
357     return (item == nil) ? [[VLCInfoTreeItem rootItem] numberOfChildren] : [item numberOfChildren];
358 }
359
360 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
361     return ([item numberOfChildren] > 0);
362 }
363
364 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
365 {
366     return (item == nil) ? [[VLCInfoTreeItem rootItem] childAtIndex:index] : [item childAtIndex:index];
367 }
368
369 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
370 {
371     if ([[tableColumn identifier] isEqualToString:@"0"])
372     {
373         return (item == nil) ? @"" : (id)[item getName];
374     }
375     else
376     {
377         return (item == nil) ? @"" : (id)[item getValue];
378     }
379 }
380
381
382 @end
383
384 @implementation VLCInfoTreeItem
385
386 static VLCInfoTreeItem *o_root_item = nil;
387
388 #define IsALeafNode ((id)-1)
389
390 - (id)initWithName: (NSString *)o_item_name value: (NSString *)o_item_value ID: (int)i_id parent:(VLCInfoTreeItem *)o_parent_item
391 {
392     self = [super init];
393
394     if( self != nil )
395     {
396         o_name = [o_item_name copy];
397         o_value = [o_item_value copy];
398         i_object_id = i_id;
399         o_parent = o_parent_item;
400         if( [[VLCMain sharedInstance] getInfo] != nil )
401             p_item = [[[VLCMain sharedInstance] getInfo] getItem];
402         else
403             p_item = NULL;
404     }
405     return( self );
406 }
407
408 + (VLCInfoTreeItem *)rootItem {
409     if (o_root_item == nil) o_root_item = [[VLCInfoTreeItem alloc] initWithName:@"main" value: @"" ID: 0 parent:nil];
410     return o_root_item;
411 }
412
413 - (void)dealloc
414 {
415     if( o_children != IsALeafNode ) [o_children release];
416     [o_name release];
417     [super dealloc];
418 }
419
420 /* Creates and returns the array of children
421  * Loads children incrementally */
422 - (NSArray *)children
423 {
424     if (o_children == NULL)
425     {
426         int i;
427
428         if( [[[VLCMain sharedInstance] getInfo] isItemInPlaylist: p_item] )
429         {
430             if( self == o_root_item )
431             {
432                 vlc_mutex_lock( &p_item->input.lock );
433                 o_children = [[NSMutableArray alloc] initWithCapacity:
434                                                 p_item->input.i_categories];
435                 for (i = 0 ; i < p_item->input.i_categories ; i++)
436                 {
437                     [o_children addObject:[[VLCInfoTreeItem alloc]
438                         initWithName: [NSString stringWithUTF8String:
439                             p_item->input.pp_categories[i]->psz_name]
440                         value: @""
441                         ID: i
442                         parent: self]];
443                 }
444                 vlc_mutex_unlock( &p_item->input.lock );
445             }
446             else if( o_parent == o_root_item )
447             {
448                 vlc_mutex_lock( &p_item->input.lock );
449                 o_children = [[NSMutableArray alloc] initWithCapacity:
450                     p_item->input.pp_categories[i_object_id]->i_infos];
451
452                 for (i = 0 ; i < p_item->input.pp_categories[i_object_id]->i_infos ; i++)
453                 {
454                     [o_children addObject:[[VLCInfoTreeItem alloc]
455                     initWithName: [NSString stringWithUTF8String:
456                             p_item->input.pp_categories[i_object_id]->pp_infos[i]->psz_name]
457                         value: [NSString stringWithUTF8String:
458                             p_item->input.pp_categories[i_object_id]->pp_infos[i]->psz_value]
459                         ID: i
460                         parent: self]];
461                 }
462                 vlc_mutex_unlock( &p_item->input.lock );
463             }
464             else
465             {
466                 o_children = IsALeafNode;
467             }
468         }
469     }
470     return o_children;
471 }
472
473 - (NSString *)getName
474 {
475     return o_name;
476 }
477
478 - (NSString *)getValue
479 {
480     return o_value;
481 }
482
483 - (VLCInfoTreeItem *)childAtIndex:(int)i_index {
484     return [[self children] objectAtIndex:i_index];
485 }
486
487 - (int)numberOfChildren {
488     id i_tmp = [self children];
489     return ( i_tmp == IsALeafNode ) ? (-1) : (int)[i_tmp count];
490 }
491
492 /*- (int)selectedPlaylistItem
493 {
494     return i_item;
495 }
496 */
497 - (void)refresh
498 {
499     p_item = [[[VLCMain sharedInstance] getInfo] getItem];
500     if( o_children != NULL )
501     {
502         [o_children release];
503         o_children = NULL;
504     }
505 }
506
507 @end
508