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