]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlistinfo.m
* playlistinfo.m: fixed a rare crash and made sure that the timer is run in every...
[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         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     
91     [o_sout_box setTitle: _NS("Streaming")];
92     [o_sent_packets_lbl setStringValue: _NS("Sent packets")];
93     [o_sent_bytes_lbl setStringValue: _NS("Sent bytes")];
94     [o_sent_bitrate_lbl setStringValue: _NS("Send rate")];
95     
96     [o_audio_box setTitle: _NS("Audio")];
97     [o_audio_decoded_lbl setStringValue: _NS("Decoded blocks")];
98     [o_played_abuffers_lbl setStringValue: _NS("Played buffers")];
99     [o_lost_abuffers_lbl setStringValue: _NS("Lost buffers")];
100 }
101
102 - (void)dealloc
103 {
104     /* make sure that the timer is released in any case */
105     if( [o_statUpdateTimer isValid] )
106         [o_statUpdateTimer invalidate];
107
108     if ( o_statUpdateTimer )
109         [o_statUpdateTimer release];
110
111     [super dealloc];
112 }
113
114 - (IBAction)togglePlaylistInfoPanel:(id)sender
115 {
116     if( [o_info_window isVisible] )
117     {
118         [self windowShouldClose: nil];
119         [o_info_window orderOut: sender];
120     }
121     else
122     {
123         p_item = [[[VLCMain sharedInstance] getPlaylist] selectedPlaylistItem];
124         [self initPanel:sender];
125     }
126 }
127
128 - (IBAction)toggleInfoPanel:(id)sender
129 {
130     if( [o_info_window isVisible] )
131     {
132         [self windowShouldClose: nil];
133         [o_info_window orderOut: sender];
134     }
135     else
136     {
137         intf_thread_t * p_intf = VLCIntf;
138         playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
139                                           FIND_ANYWHERE );
140
141         if( p_playlist )
142         {
143             p_item = p_playlist->status.p_item;
144             vlc_object_release( p_playlist );
145         }
146
147         [self initPanel:sender];
148     }
149 }
150
151 - (void)initPanel:(id)sender
152 {
153     BOOL b_stats = config_GetInt(VLCIntf, "stats");
154     if( b_stats )
155     {
156         o_statUpdateTimer = [NSTimer scheduledTimerWithTimeInterval: 1
157             target: self selector: @selector(updateStatistics:)
158             userInfo: nil repeats: YES]; 
159         [o_statUpdateTimer fire];
160         [o_statUpdateTimer retain];
161     }
162     else
163     {
164         if( [o_tab_view numberOfTabViewItems] > 2 )
165             [o_tab_view removeTabViewItem: [o_tab_view tabViewItemAtIndex: 2]];
166     }
167
168     [self updatePanel];
169     [o_info_window makeKeyAndOrderFront: sender];
170 }
171
172 - (void)updatePanel
173 {
174     /* make sure that we got the current item and not an outdated one */
175     intf_thread_t * p_intf = VLCIntf;
176         playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
177                                           FIND_ANYWHERE );
178
179     if( p_playlist )
180     {
181         p_item = p_playlist->status.p_item;
182         vlc_object_release( p_playlist );
183     }
184
185     /* check whether our item is valid, because we would crash if not */
186     if(! [self isItemInPlaylist: p_item] ) return;
187
188     vlc_mutex_lock( &p_item->p_input->lock );
189
190     /* fill uri info */
191     if( p_item->p_input->psz_uri )
192     {
193         [o_uri_txt setStringValue:
194             ([NSString stringWithUTF8String:p_item->p_input->psz_uri] == nil ) ?
195             [NSString stringWithCString:p_item->p_input->psz_uri] :
196             [NSString stringWithUTF8String:p_item->p_input->psz_uri]];
197     }
198
199     /* fill the other fields */
200 #define p_m p_item->p_input->p_meta
201     [self setMeta: p_m->psz_title forLabel: o_title_txt];
202     [self setMeta: p_m->psz_artist forLabel: o_author_txt];
203     [self setMeta: p_m->psz_album forLabel: o_collection_txt];
204     [self setMeta: p_m->psz_tracknum forLabel: o_seqNum_txt];
205     [self setMeta: p_m->psz_genre forLabel: o_genre_txt];
206     [self setMeta: p_m->psz_copyright forLabel: o_copyright_txt];
207     [self setMeta: p_m->psz_rating forLabel: o_rating_txt];
208     [self setMeta: p_m->psz_publisher forLabel: o_publisher_txt];
209     [self setMeta: p_m->psz_nowplaying forLabel: o_nowPlaying_txt];
210     [self setMeta: p_m->psz_language forLabel: o_language_txt];
211     [self setMeta: p_m->psz_date forLabel: o_date_txt];
212 #undef p_m
213     vlc_mutex_unlock( &p_item->p_input->lock );
214
215     /* reload the advanced table */
216     [[VLCInfoTreeItem rootItem] refresh];
217     [o_outline_view reloadData];
218
219     /* update the stats once to display p_item change faster */
220     [self updateStatistics: nil];
221 }
222
223 - (void)setMeta: (char *)psz_meta forLabel: (id)theItem
224 {
225     if( psz_meta != NULL && *psz_meta)
226         [theItem setStringValue: 
227             ([NSString stringWithUTF8String:psz_meta] == nil ) ? 
228             [NSString stringWithCString:psz_meta] :
229             [NSString stringWithUTF8String:psz_meta]];
230     else
231         [theItem setStringValue: @"-"];
232 }
233
234 - (void)updateStatistics:(NSTimer*)theTimer
235 {
236     if( [self isItemInPlaylist: p_item] )
237     {
238         /* we can only do that if there's a valid input around */
239
240         vlc_mutex_lock( &p_item->p_input->p_stats->lock );
241
242         /* input */
243         [o_read_bytes_txt setStringValue: [NSString stringWithFormat:
244             @"%8.0f kB", (float)(p_item->p_input->p_stats->i_read_bytes)/1000]];
245         [o_input_bitrate_txt setStringValue: [NSString stringWithFormat:
246             @"%6.0f kb/s", (float)(p_item->p_input->p_stats->f_input_bitrate)*8000]];
247         [o_demux_bytes_txt setStringValue: [NSString stringWithFormat:
248             @"%8.0f kB", (float)(p_item->p_input->p_stats->i_demux_read_bytes)/1000]];
249         [o_demux_bitrate_txt setStringValue: [NSString stringWithFormat:
250             @"%6.0f kb/s", (float)(p_item->p_input->p_stats->f_demux_bitrate)*8000]];
251
252         /* Video */
253         [o_video_decoded_txt setStringValue: [NSString stringWithFormat: @"%5i",
254             p_item->p_input->p_stats->i_decoded_video]];
255         [o_displayed_txt setStringValue: [NSString stringWithFormat: @"%5i",
256             p_item->p_input->p_stats->i_displayed_pictures]];
257         [o_lost_frames_txt setStringValue: [NSString stringWithFormat: @"%5i",
258             p_item->p_input->p_stats->i_lost_pictures]];
259
260         /* Sout */
261         [o_sent_packets_txt setStringValue: [NSString stringWithFormat: @"%5i",
262             p_item->p_input->p_stats->i_sent_packets]];
263         [o_sent_bytes_txt setStringValue: [NSString stringWithFormat: @"%8.0f kB",
264             (float)(p_item->p_input->p_stats->i_sent_bytes)/1000]];
265         [o_sent_bitrate_txt setStringValue: [NSString stringWithFormat:
266             @"%6.0f kb/s", (float)(p_item->p_input->p_stats->f_send_bitrate*8)*1000]];
267
268         /* Audio */
269         [o_audio_decoded_txt setStringValue: [NSString stringWithFormat: @"%5i",
270             p_item->p_input->p_stats->i_decoded_audio]];
271         [o_played_abuffers_txt setStringValue: [NSString stringWithFormat: @"%5i",
272             p_item->p_input->p_stats->i_played_abuffers]];
273         [o_lost_abuffers_txt setStringValue: [NSString stringWithFormat: @"%5i",
274             p_item->p_input->p_stats->i_lost_abuffers]];
275
276         vlc_mutex_unlock( &p_item->p_input->p_stats->lock );
277     }
278 }
279
280 - (IBAction)infoCancel:(id)sender
281 {
282     [self windowShouldClose: nil];
283     [o_info_window orderOut: self];
284 }
285
286
287 - (IBAction)infoOk:(id)sender
288 {
289     intf_thread_t * p_intf = VLCIntf;
290     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
291                                           FIND_ANYWHERE );
292     vlc_value_t val;
293
294     if( [self isItemInPlaylist: p_item] )
295     {
296         vlc_mutex_lock( &p_item->p_input->lock );
297
298         p_item->p_input->psz_uri = strdup( [[o_uri_txt stringValue] UTF8String] );
299         p_item->p_input->psz_name = strdup( [[o_title_txt stringValue] UTF8String] );
300         vlc_meta_SetArtist( p_item->p_input->p_meta, [[o_author_txt stringValue] UTF8String] )
301         vlc_mutex_unlock( &p_item->p_input->lock );
302
303         val.b_bool = VLC_TRUE;
304         var_Set( p_playlist, "intf-change", val );
305     }
306     vlc_object_release( p_playlist );
307     [self windowShouldClose: nil];
308     [o_info_window orderOut: self];
309 }
310
311 - (playlist_item_t *)getItem
312 {
313     return p_item;
314 }
315
316 - (BOOL)isItemInPlaylist:(playlist_item_t *)p_local_item
317 {
318     intf_thread_t * p_intf = VLCIntf;
319     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
320                                           FIND_ANYWHERE );
321     int i;
322
323     if( p_playlist == NULL )
324     {
325         return NO;
326     }
327
328     for( i = 0 ; i < p_playlist->i_all_size ; i++ )
329     {
330         if( p_playlist->pp_all_items[i] == p_local_item )
331         {
332             vlc_object_release( p_playlist );
333             return YES;
334         }
335     }
336     vlc_object_release( p_playlist );
337     return NO;
338 }
339
340 - (BOOL)windowShouldClose:(id)sender
341 {
342     if( [o_statUpdateTimer isValid] )
343         [o_statUpdateTimer invalidate];
344
345     if( o_statUpdateTimer )
346         [o_statUpdateTimer release];
347
348     return YES;
349 }
350
351 @end
352
353 @implementation VLCInfo (NSMenuValidation)
354
355 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
356 {
357     BOOL bEnabled = TRUE;
358
359     intf_thread_t * p_intf = VLCIntf;
360     input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
361                                                        FIND_ANYWHERE );
362
363     if( [[o_mi title] isEqualToString: _NS("Information")] )
364     {
365         if( p_input == NULL )
366         {
367             bEnabled = FALSE;
368         }
369     }
370     if( p_input ) vlc_object_release( p_input );
371
372     return( bEnabled );
373 }
374
375 @end
376
377 @implementation VLCInfo (NSTableDataSource)
378
379 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
380 {
381     return (item == nil) ? [[VLCInfoTreeItem rootItem] numberOfChildren] : [item numberOfChildren];
382 }
383
384 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
385     return ([item numberOfChildren] > 0);
386 }
387
388 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
389 {
390     return (item == nil) ? [[VLCInfoTreeItem rootItem] childAtIndex:index] : (id)[item childAtIndex:index];
391 }
392
393 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
394 {
395     if ([[tableColumn identifier] isEqualToString:@"0"])
396     {
397         return (item == nil) ? @"" : (id)[item getName];
398     }
399     else
400     {
401         return (item == nil) ? @"" : (id)[item getValue];
402     }
403 }
404
405
406 @end
407
408 @implementation VLCInfoTreeItem
409
410 static VLCInfoTreeItem *o_root_item = nil;
411
412 #define IsALeafNode ((id)-1)
413
414 - (id)initWithName: (NSString *)o_item_name value: (NSString *)o_item_value ID: (int)i_id parent:(VLCInfoTreeItem *)o_parent_item
415 {
416     self = [super init];
417
418     if( self != nil )
419     {
420         o_name = [o_item_name copy];
421         o_value = [o_item_value copy];
422         i_object_id = i_id;
423         o_parent = o_parent_item;
424         if( [[VLCMain sharedInstance] getInfo] != nil )
425             p_item = [[[VLCMain sharedInstance] getInfo] getItem];
426         else
427             p_item = NULL;
428     }
429     return( self );
430 }
431
432 + (VLCInfoTreeItem *)rootItem {
433     if (o_root_item == nil) o_root_item = [[VLCInfoTreeItem alloc] initWithName:@"main" value: @"" ID: 0 parent:nil];
434     return o_root_item;
435 }
436
437 - (void)dealloc
438 {
439     if( o_children != IsALeafNode ) [o_children release];
440     [o_name release];
441     [super dealloc];
442 }
443
444 /* Creates and returns the array of children
445  * Loads children incrementally */
446 - (NSArray *)children
447 {
448     if (o_children == NULL)
449     {
450         int i;
451
452         if( [[[VLCMain sharedInstance] getInfo] isItemInPlaylist: p_item] )
453         {
454             if( self == o_root_item )
455             {
456                 vlc_mutex_lock( &p_item->p_input->lock );
457                 o_children = [[NSMutableArray alloc] initWithCapacity:
458                                                 p_item->p_input->i_categories];
459                 for (i = 0 ; i < p_item->p_input->i_categories ; i++)
460                 {
461                     [o_children addObject:[[VLCInfoTreeItem alloc]
462                         initWithName: [NSString stringWithUTF8String:
463                             p_item->p_input->pp_categories[i]->psz_name]
464                         value: @""
465                         ID: i
466                         parent: self]];
467                 }
468                 vlc_mutex_unlock( &p_item->p_input->lock );
469             }
470             else if( o_parent == o_root_item )
471             {
472                 vlc_mutex_lock( &p_item->p_input->lock );
473                 o_children = [[NSMutableArray alloc] initWithCapacity:
474                     p_item->p_input->pp_categories[i_object_id]->i_infos];
475
476                 for (i = 0 ; i < p_item->p_input->pp_categories[i_object_id]->i_infos ; i++)
477                 {
478                     [o_children addObject:[[VLCInfoTreeItem alloc]
479                     initWithName: [NSString stringWithUTF8String:
480                             p_item->p_input->pp_categories[i_object_id]->pp_infos[i]->psz_name]
481                         value: [NSString stringWithUTF8String:
482                             p_item->p_input->pp_categories[i_object_id]->pp_infos[i]->psz_value]
483                         ID: i
484                         parent: self]];
485                 }
486                 vlc_mutex_unlock( &p_item->p_input->lock );
487             }
488             else
489             {
490                 o_children = IsALeafNode;
491             }
492         }
493     }
494     return o_children;
495 }
496
497 - (NSString *)getName
498 {
499     return o_name;
500 }
501
502 - (NSString *)getValue
503 {
504     return o_value;
505 }
506
507 - (VLCInfoTreeItem *)childAtIndex:(int)i_index {
508     return [[self children] objectAtIndex:i_index];
509 }
510
511 - (int)numberOfChildren {
512     id i_tmp = [self children];
513     return ( i_tmp == IsALeafNode ) ? (-1) : (int)[i_tmp count];
514 }
515
516 /*- (int)selectedPlaylistItem
517 {
518     return i_item;
519 }
520 */
521 - (void)refresh
522 {
523     p_item = [[[VLCMain sharedInstance] getInfo] getItem];
524     if( o_children != NULL )
525     {
526         [o_children release];
527         o_children = NULL;
528     }
529 }
530
531 @end
532