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