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