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