]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlistinfo.m
efbf622b732479b85622e4a4f8c39604cfe4d89d
[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     
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 #if GC_ENABLED
115 - (void)finalize
116 {
117     /* since dealloc isn't called with enabled GC on 10.5, we need this to prevent core crashes */
118     if( [o_statUpdateTimer isValid] )
119         [o_statUpdateTimer invalidate];
120     [super finalize];
121 }
122 #endif
123     
124 - (IBAction)togglePlaylistInfoPanel:(id)sender
125 {
126     if( [o_info_window isVisible] )
127     {
128         [self windowShouldClose: nil];
129         [o_info_window orderOut: sender];
130     }
131     else
132     {
133         p_item = [[[VLCMain sharedInstance] getPlaylist] selectedPlaylistItem];
134         [self initPanel:sender];
135     }
136 }
137
138 - (IBAction)toggleInfoPanel:(id)sender
139 {
140     if( [o_info_window isVisible] )
141     {
142         [self windowShouldClose: nil];
143         [o_info_window orderOut: sender];
144     }
145     else
146     {
147         intf_thread_t * p_intf = VLCIntf;
148         playlist_t * p_playlist = pl_Yield( p_intf );
149
150         p_item = p_playlist->status.p_item;
151         vlc_object_release( p_playlist );
152         
153         [self initPanel:sender];
154     }
155 }
156
157 - (void)initPanel:(id)sender
158 {
159     BOOL b_stats = config_GetInt(VLCIntf, "stats");
160     if( b_stats )
161     {
162         o_statUpdateTimer = [NSTimer scheduledTimerWithTimeInterval: 1
163             target: self selector: @selector(updateStatistics:)
164             userInfo: nil repeats: YES]; 
165         [o_statUpdateTimer fire];
166         [o_statUpdateTimer retain];
167     }
168     else
169     {
170         if( [o_tab_view numberOfTabViewItems] > 2 )
171             [o_tab_view removeTabViewItem: [o_tab_view tabViewItemAtIndex: 2]];
172     }
173
174     [self updatePanel];
175     [o_info_window makeKeyAndOrderFront: sender];
176 }
177
178 - (void)updatePanel
179 {
180     /* make sure that we got the current item and not an outdated one */
181     intf_thread_t * p_intf = VLCIntf;
182         playlist_t * p_playlist = pl_Yield( p_intf );
183
184     p_item = p_playlist->status.p_item;
185     vlc_object_release( p_playlist );
186
187     /* check whether our item is valid, because we would crash if not */
188     if(! [self isItemInPlaylist: p_item] ) return;
189
190     /* fill uri info */
191     char *psz_uri = input_item_GetURI( p_item->p_input );
192     if( psz_uri )
193     {
194         [o_uri_txt setStringValue:
195             ([NSString stringWithUTF8String:psz_uri] == nil ) ?
196             [NSString stringWithCString:psz_uri] :
197             [NSString stringWithUTF8String:psz_uri]];
198     }
199     free( psz_uri );
200
201 #define SET( foo, bar ) \
202     char *psz_##foo = input_item_Get##bar ( p_item->p_input ); \
203     [self setMeta: psz_##foo forlabel: o_##foo##_txt]; \
204     free( psz_##foo );
205
206     /* fill the other fields */
207     SET( title, Title );
208     SET( author, Artist );
209     SET( collection, Album );
210     SET( seqNum, TrackNum );
211     SET( genre, Genre );
212     SET( copyright, Copyright );
213     SET( rating, Rating );
214     SET( publisher, Publisher );
215     SET( nowPlaying, NowPlaying );
216     SET( language, Language );
217     SET( date, Date );
218
219 #undef SET
220
221     /* reload the advanced table */
222     [[VLCInfoTreeItem rootItem] refresh];
223     [o_outline_view reloadData];
224
225     /* update the stats once to display p_item change faster */
226     [self updateStatistics: nil];
227 }
228
229 - (void)setMeta: (char *)psz_meta forLabel: (id)theItem
230 {
231     if( psz_meta != NULL && *psz_meta)
232         [theItem setStringValue: 
233             ([NSString stringWithUTF8String:psz_meta] == nil ) ? 
234             [NSString stringWithCString:psz_meta] :
235             [NSString stringWithUTF8String:psz_meta]];
236     else
237         [theItem setStringValue: @"-"];
238 }
239
240 - (void)updateStatistics:(NSTimer*)theTimer
241 {
242     if( [self isItemInPlaylist: p_item] )
243     {
244         /* we can only do that if there's a valid input around */
245
246         vlc_mutex_lock( &p_item->p_input->p_stats->lock );
247
248         /* input */
249         [o_read_bytes_txt setStringValue: [NSString stringWithFormat:
250             @"%8.0f kB", (float)(p_item->p_input->p_stats->i_read_bytes)/1000]];
251         [o_input_bitrate_txt setStringValue: [NSString stringWithFormat:
252             @"%6.0f kb/s", (float)(p_item->p_input->p_stats->f_input_bitrate)*8000]];
253         [o_demux_bytes_txt setStringValue: [NSString stringWithFormat:
254             @"%8.0f kB", (float)(p_item->p_input->p_stats->i_demux_read_bytes)/1000]];
255         [o_demux_bitrate_txt setStringValue: [NSString stringWithFormat:
256             @"%6.0f kb/s", (float)(p_item->p_input->p_stats->f_demux_bitrate)*8000]];
257
258         /* Video */
259         [o_video_decoded_txt setStringValue: [NSString stringWithFormat: @"%5i",
260             p_item->p_input->p_stats->i_decoded_video]];
261         [o_displayed_txt setStringValue: [NSString stringWithFormat: @"%5i",
262             p_item->p_input->p_stats->i_displayed_pictures]];
263         [o_lost_frames_txt setStringValue: [NSString stringWithFormat: @"%5i",
264             p_item->p_input->p_stats->i_lost_pictures]];
265
266         /* Sout */
267         [o_sent_packets_txt setStringValue: [NSString stringWithFormat: @"%5i",
268             p_item->p_input->p_stats->i_sent_packets]];
269         [o_sent_bytes_txt setStringValue: [NSString stringWithFormat: @"%8.0f kB",
270             (float)(p_item->p_input->p_stats->i_sent_bytes)/1000]];
271         [o_sent_bitrate_txt setStringValue: [NSString stringWithFormat:
272             @"%6.0f kb/s", (float)(p_item->p_input->p_stats->f_send_bitrate*8)*1000]];
273
274         /* Audio */
275         [o_audio_decoded_txt setStringValue: [NSString stringWithFormat: @"%5i",
276             p_item->p_input->p_stats->i_decoded_audio]];
277         [o_played_abuffers_txt setStringValue: [NSString stringWithFormat: @"%5i",
278             p_item->p_input->p_stats->i_played_abuffers]];
279         [o_lost_abuffers_txt setStringValue: [NSString stringWithFormat: @"%5i",
280             p_item->p_input->p_stats->i_lost_abuffers]];
281
282         vlc_mutex_unlock( &p_item->p_input->p_stats->lock );
283     }
284 }
285
286 - (IBAction)infoCancel:(id)sender
287 {
288     [self windowShouldClose: nil];
289     [o_info_window orderOut: self];
290 }
291
292
293 - (IBAction)infoOk:(id)sender
294 {
295     intf_thread_t * p_intf = VLCIntf;
296     playlist_t * p_playlist = pl_Yield( p_intf );
297     vlc_value_t val;
298
299     if( [self isItemInPlaylist: p_item] )
300     {
301         input_item_SetName( p_item->p_input, (char*)
302                 [[o_title_txt stringValue] UTF8String] );
303         input_item_SetURI( p_item->p_input, (char*)
304                 [[o_uri_txt stringValue] UTF8String] );
305         input_item_SetArtist( p_item->p_input, (char*)
306                 [[o_author_txt stringValue] UTF8String] );
307
308         val.b_bool = VLC_TRUE;
309         var_Set( p_playlist, "intf-change", val );
310     }
311     vlc_object_release( p_playlist );
312     [self windowShouldClose: nil];
313     [o_info_window orderOut: self];
314 }
315
316 - (playlist_item_t *)getItem
317 {
318     return p_item;
319 }
320
321 - (BOOL)isItemInPlaylist:(playlist_item_t *)p_local_item
322 {
323     intf_thread_t * p_intf = VLCIntf;
324     playlist_t * p_playlist = pl_Yield( p_intf );
325     int i;
326
327     for( i = 0 ; i < p_playlist->all_items.i_size ; i++ )
328     {
329         if( ARRAY_VAL( p_playlist->all_items, i ) == p_local_item )
330         {
331             vlc_object_release( p_playlist );
332             return YES;
333         }
334     }
335     vlc_object_release( p_playlist );
336     return NO;
337 }
338
339 - (BOOL)windowShouldClose:(id)sender
340 {
341     if( [o_statUpdateTimer isValid] )
342         [o_statUpdateTimer invalidate];
343
344     if( o_statUpdateTimer )
345         [o_statUpdateTimer release];
346
347     return YES;
348 }
349
350 @end
351
352 @implementation VLCInfo (NSMenuValidation)
353
354 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
355 {
356     BOOL bEnabled = TRUE;
357
358     intf_thread_t * p_intf = VLCIntf;
359     input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
360                                                        FIND_ANYWHERE );
361
362     if( [[o_mi title] isEqualToString: _NS("Information")] )
363     {
364         if( p_input == NULL )
365         {
366             bEnabled = FALSE;
367         }
368     }
369     if( p_input ) vlc_object_release( p_input );
370
371     return( bEnabled );
372 }
373
374 @end
375
376 @implementation VLCInfo (NSTableDataSource)
377
378 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
379 {
380     return (item == nil) ? [[VLCInfoTreeItem rootItem] numberOfChildren] : [item numberOfChildren];
381 }
382
383 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
384     return ([item numberOfChildren] > 0);
385 }
386
387 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
388 {
389     return (item == nil) ? [[VLCInfoTreeItem rootItem] childAtIndex:index] : (id)[item childAtIndex:index];
390 }
391
392 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
393 {
394     if ([[tableColumn identifier] isEqualToString:@"0"])
395     {
396         return (item == nil) ? @"" : (id)[item getName];
397     }
398     else
399     {
400         return (item == nil) ? @"" : (id)[item getValue];
401     }
402 }
403
404
405 @end
406
407 @implementation VLCInfoTreeItem
408
409 static VLCInfoTreeItem *o_root_item = nil;
410
411 #define IsALeafNode ((id)-1)
412
413 - (id)initWithName: (NSString *)o_item_name value: (NSString *)o_item_value ID: (int)i_id parent:(VLCInfoTreeItem *)o_parent_item
414 {
415     self = [super init];
416
417     if( self != nil )
418     {
419         o_name = [o_item_name copy];
420         o_value = [o_item_value copy];
421         i_object_id = i_id;
422         o_parent = o_parent_item;
423         if( [[VLCMain sharedInstance] getInfo] != nil )
424             p_item = [[[VLCMain sharedInstance] getInfo] getItem];
425         else
426             p_item = NULL;
427     }
428     return( self );
429 }
430
431 + (VLCInfoTreeItem *)rootItem {
432     if( o_root_item == nil )
433         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