]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlistinfo.m
* implemented the statistics-tab. No auto-update yet.
[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("Properties")];
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         [o_info_window orderOut: sender];
119     }
120     else
121     {
122         intf_thread_t * p_intf = VLCIntf;
123         playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
124                                           FIND_ANYWHERE );
125
126         if( p_playlist )
127         {
128             p_item = p_playlist->status.p_item;
129             vlc_object_release( p_playlist );
130         }
131         [self initPanel:sender];
132     }
133 }
134
135 - (void)initPanel:(id)sender
136 {
137     char *psz_temp;
138     vlc_mutex_lock( &p_item->input.lock );
139
140     /*fill uri / title / author info */
141     if( p_item->input.psz_uri )
142     {
143         [o_uri_txt setStringValue:
144             ([NSString stringWithUTF8String:p_item->input.psz_uri] == nil ) ?
145             [NSString stringWithCString:p_item->input.psz_uri] :
146             [NSString stringWithUTF8String:p_item->input.psz_uri]];
147     }
148
149     if( p_item->input.psz_name )
150     {
151         [o_title_txt setStringValue:
152             ([NSString stringWithUTF8String:p_item->input.psz_name] == nil ) ?
153             [NSString stringWithCString:p_item->input.psz_name] :
154             [NSString stringWithUTF8String:p_item->input.psz_name]];
155     }
156     vlc_mutex_unlock( &p_item->input.lock );
157
158     psz_temp = vlc_input_item_GetInfo( &p_item->input, _("Meta-information"), _("Artist") );
159
160     if( psz_temp )
161     {
162         [o_author_txt setStringValue: [NSString stringWithUTF8String: psz_temp]];
163         free( psz_temp );
164     }
165
166     /* fill the other fields */
167     [self setMeta: VLC_META_GENRE forLabel: o_genre_txt];
168     [self setMeta: VLC_META_COPYRIGHT forLabel: o_copyright_txt];
169     [self setMeta: VLC_META_COLLECTION forLabel: o_collection_txt];
170     [self setMeta: VLC_META_SEQ_NUM forLabel: o_seqNum_txt];
171     [self setMeta: VLC_META_DESCRIPTION forLabel: o_description_txt];
172     [self setMeta: VLC_META_RATING forLabel: o_rating_txt];
173     [self setMeta: VLC_META_DATE forLabel: o_date_txt];
174     [self setMeta: VLC_META_LANGUAGE forLabel: o_language_txt];
175     [self setMeta: VLC_META_NOW_PLAYING forLabel: o_nowPlaying_txt];
176     [self setMeta: VLC_META_PUBLISHER forLabel: o_publisher_txt];
177
178     /* reload the advanced table */
179     [[VLCInfoTreeItem rootItem] refresh];
180     [o_outline_view reloadData];
181     
182     [self updateStatistics];
183
184     [o_info_window makeKeyAndOrderFront: sender];
185 }
186
187 - (void)setMeta: (char *)meta forLabel: (id)theItem
188 {
189     char *psz_meta = vlc_input_item_GetInfo( &p_item->input, \
190         _(VLC_META_INFO_CAT), _(meta) );
191     if( psz_meta != NULL && *psz_meta)
192         [theItem setStringValue: [NSString stringWithUTF8String: psz_meta]];
193     else
194         [theItem setStringValue: @"-"];
195 }
196
197 - (void)updateStatistics
198 {
199     vlc_mutex_lock( &p_item->input.p_stats->lock );
200
201     /* input */
202     [o_read_bytes_txt setStringValue: [NSString stringWithFormat: @"%8.0f kB", \
203         (float)(p_item->input.p_stats->i_read_bytes)/1000]];
204     [o_input_bitrate_txt setStringValue: [NSString stringWithFormat: @"%6.0f kb/s", \
205         (float)(p_item->input.p_stats->f_input_bitrate)/1000]];
206     [o_demux_bytes_txt setStringValue: [NSString stringWithFormat: @"%8.0f kB",\
207         (float)(p_item->input.p_stats->i_demux_read_bytes)/1000]];
208     [o_demux_bitrate_txt setStringValue: [NSString stringWithFormat: @"%6.0f kb/s", \
209         (float)(p_item->input.p_stats->f_demux_bitrate)/1000]];
210
211     /* Video */
212     [o_video_decoded_txt setStringValue: [NSString stringWithFormat: @"%8.0f kB", \
213         p_item->input.p_stats->i_decoded_video]];
214     [o_displayed_txt setStringValue: [NSString stringWithFormat: @"%5i", \
215         p_item->input.p_stats->i_displayed_pictures]];
216     [o_lost_frames_txt setStringValue: [NSString stringWithFormat: @"%5i", \
217         p_item->input.p_stats->i_lost_pictures]];
218
219     /* Sout */
220     [o_sent_packets_txt setStringValue: [NSString stringWithFormat: @"%5i", \
221         p_item->input.p_stats->i_sent_packets]];
222     [o_sent_bytes_txt setStringValue: [NSString stringWithFormat: @"%6.0f kB", \
223         (float)(p_item->input.p_stats->i_sent_bytes)/1000]];
224     [o_sent_bitrate_txt setStringValue: [NSString stringWithFormat: @"%6.0f kb/s", \
225         (float)(p_item->input.p_stats->f_send_bitrate*8)*1000]];
226
227     /* Audio */
228     [o_audio_decoded_txt setStringValue: [NSString stringWithFormat: @"%5i", \
229         p_item->input.p_stats->i_decoded_audio]];
230     [o_played_abuffers_txt setStringValue: [NSString stringWithFormat: @"%5i", \
231         p_item->input.p_stats->i_played_abuffers]];
232     [o_lost_abuffers_txt setStringValue: [NSString stringWithFormat: @"%5i", \
233         p_item->input.p_stats->i_lost_abuffers]];
234
235     vlc_mutex_unlock( &p_item->input.p_stats->lock );
236 }
237
238 - (IBAction)infoCancel:(id)sender
239 {
240     [o_info_window orderOut: self];
241 }
242
243
244 - (IBAction)infoOk:(id)sender
245 {
246     intf_thread_t * p_intf = VLCIntf;
247     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
248                                           FIND_ANYWHERE );
249     vlc_value_t val;
250
251     if( [self isItemInPlaylist: p_item] )
252     {
253         vlc_mutex_lock( &p_item->input.lock );
254
255         p_item->input.psz_uri = strdup( [[o_uri_txt stringValue] UTF8String] );
256         p_item->input.psz_name = strdup( [[o_title_txt stringValue] UTF8String] );
257         vlc_mutex_unlock( &p_item->input.lock );
258         vlc_input_item_AddInfo( &p_item->input, _("Meta-information"), _("Artist"), [[o_author_txt stringValue] UTF8String]);
259         
260         val.b_bool = VLC_TRUE;
261         var_Set( p_playlist, "intf-change", val );
262     }
263     vlc_object_release( p_playlist );
264     [o_info_window orderOut: self];
265 }
266
267 - (playlist_item_t *)getItem
268 {
269     return p_item;
270 }
271
272 - (BOOL)isItemInPlaylist:(playlist_item_t *)p_local_item
273 {
274     intf_thread_t * p_intf = VLCIntf;
275     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
276                                           FIND_ANYWHERE );
277     int i;
278
279     if( p_playlist == NULL )
280     {
281         return NO;
282     }
283
284     for( i = 0 ; i < p_playlist->i_size ; i++ )
285     {
286         if( p_playlist->pp_items[i] == p_local_item )
287         {
288             vlc_object_release( p_playlist );
289             return YES;
290         }
291     }
292     vlc_object_release( p_playlist );
293     return NO;
294 }
295
296 @end
297
298 @implementation VLCInfo (NSMenuValidation)
299
300 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
301 {
302     BOOL bEnabled = TRUE;
303
304     intf_thread_t * p_intf = VLCIntf;
305     input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
306                                                        FIND_ANYWHERE );
307
308     if( [[o_mi title] isEqualToString: _NS("Info")] )
309     {
310         if( p_input == NULL )
311         {
312             bEnabled = FALSE;
313         }
314     }
315     if( p_input ) vlc_object_release( p_input );
316
317     return( bEnabled );
318 }
319
320 @end
321
322 @implementation VLCInfo (NSTableDataSource)
323
324 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
325 {
326     return (item == nil) ? [[VLCInfoTreeItem rootItem] numberOfChildren] : [item numberOfChildren];
327 }
328
329 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
330     return ([item numberOfChildren] > 0);
331 }
332
333 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
334 {
335     return (item == nil) ? [[VLCInfoTreeItem rootItem] childAtIndex:index] : [item childAtIndex:index];
336 }
337
338 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
339 {
340     if ([[tableColumn identifier] isEqualToString:@"0"])
341     {
342         return (item == nil) ? @"" : (id)[item getName];
343     }
344     else
345     {
346         return (item == nil) ? @"" : (id)[item getValue];
347     }
348 }
349
350
351 @end
352
353 @implementation VLCInfoTreeItem
354
355 static VLCInfoTreeItem *o_root_item = nil;
356
357 #define IsALeafNode ((id)-1)
358
359 - (id)initWithName: (NSString *)o_item_name value: (NSString *)o_item_value ID: (int)i_id parent:(VLCInfoTreeItem *)o_parent_item
360 {
361     self = [super init];
362
363     if( self != nil )
364     {
365         o_name = [o_item_name copy];
366         o_value = [o_item_value copy];
367         i_object_id = i_id;
368         o_parent = o_parent_item;
369         if( [[VLCMain sharedInstance] getInfo] != nil )
370             p_item = [[[VLCMain sharedInstance] getInfo] getItem];
371         else
372             p_item = NULL;
373     }
374     return( self );
375 }
376
377 + (VLCInfoTreeItem *)rootItem {
378     if (o_root_item == nil) o_root_item = [[VLCInfoTreeItem alloc] initWithName:@"main" value: @"" ID: 0 parent:nil];
379     return o_root_item;
380 }
381
382 - (void)dealloc
383 {
384     if( o_children != IsALeafNode ) [o_children release];
385     [o_name release];
386     [super dealloc];
387 }
388
389 /* Creates and returns the array of children
390  * Loads children incrementally */
391 - (NSArray *)children
392 {
393     if (o_children == NULL)
394     {
395         int i;
396
397         if( [[[VLCMain sharedInstance] getInfo] isItemInPlaylist: p_item] )
398         {
399             if( self == o_root_item )
400             {
401                 vlc_mutex_lock( &p_item->input.lock );
402                 o_children = [[NSMutableArray alloc] initWithCapacity:
403                                                 p_item->input.i_categories];
404                 for (i = 0 ; i < p_item->input.i_categories ; i++)
405                 {
406                     [o_children addObject:[[VLCInfoTreeItem alloc]
407                         initWithName: [NSString stringWithUTF8String:
408                             p_item->input.pp_categories[i]->psz_name]
409                         value: @""
410                         ID: i
411                         parent: self]];
412                 }
413                 vlc_mutex_unlock( &p_item->input.lock );
414             }
415             else if( o_parent == o_root_item )
416             {
417                 vlc_mutex_lock( &p_item->input.lock );
418                 o_children = [[NSMutableArray alloc] initWithCapacity:
419                     p_item->input.pp_categories[i_object_id]->i_infos];
420
421                 for (i = 0 ; i < p_item->input.pp_categories[i_object_id]->i_infos ; i++)
422                 {
423                     [o_children addObject:[[VLCInfoTreeItem alloc]
424                     initWithName: [NSString stringWithUTF8String:
425                             p_item->input.pp_categories[i_object_id]->pp_infos[i]->psz_name]
426                         value: [NSString stringWithUTF8String:
427                             p_item->input.pp_categories[i_object_id]->pp_infos[i]->psz_value]
428                         ID: i
429                         parent: self]];
430                 }
431                 vlc_mutex_unlock( &p_item->input.lock );
432             }
433             else
434             {
435                 o_children = IsALeafNode;
436             }
437         }
438     }
439     return o_children;
440 }
441
442 - (NSString *)getName
443 {
444     return o_name;
445 }
446
447 - (NSString *)getValue
448 {
449     return o_value;
450 }
451
452 - (VLCInfoTreeItem *)childAtIndex:(int)i_index {
453     return [[self children] objectAtIndex:i_index];
454 }
455
456 - (int)numberOfChildren {
457     id i_tmp = [self children];
458     return ( i_tmp == IsALeafNode ) ? (-1) : (int)[i_tmp count];
459 }
460
461 /*- (int)selectedPlaylistItem
462 {
463     return i_item;
464 }
465 */
466 - (void)refresh
467 {
468     p_item = [[[VLCMain sharedInstance] getInfo] getItem];
469     if( o_children != NULL )
470     {
471         [o_children release];
472         o_children = NULL;
473     }
474 }
475
476 @end
477