]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlistinfo.m
dc316dcb31fded7b85182607b7a7131494cbda90
[vlc] / modules / gui / macosx / playlistinfo.m
1 /*****************************************************************************
2  r playlistinfo.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2002-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Benjamin Pracht <bigben at videolan dot org>
8  *          Felix Paul 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 #include <vlc_url.h>
33
34 /*****************************************************************************
35  * VLCPlaylistInfo Implementation
36  *****************************************************************************/
37
38 @implementation VLCInfo
39
40 static VLCInfo *_o_sharedInstance = nil;
41
42 + (VLCInfo *)sharedInstance
43 {
44     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
45 }
46
47 - (id)init
48 {
49     if( _o_sharedInstance )
50     {
51         [self dealloc];
52     }
53     else
54     {
55         _o_sharedInstance = [super init];
56         
57         if( _o_sharedInstance != nil )
58         {
59             p_item = NULL;
60             o_statUpdateTimer = nil;
61             [self updatePanelWithItem: NULL];
62             rootItem = [[VLCInfoTreeItem alloc] init];
63         }
64     }
65
66     return _o_sharedInstance;
67 }
68
69 - (void)awakeFromNib
70 {
71     [o_info_window setExcludedFromWindowsMenu: YES];
72     [o_info_window setFloatingPanel: NO];
73
74     [o_info_window setTitle: _NS("Media Information")];
75     [o_uri_lbl setStringValue: _NS("Location")];
76     [o_title_lbl setStringValue: _NS("Title")];
77     [o_author_lbl setStringValue: _NS("Artist")];
78     [o_saveMetaData_btn setStringValue: _NS("Save Metadata" )];
79
80     [[o_tab_view tabViewItemAtIndex: 0] setLabel: _NS("General")];
81     [[o_tab_view tabViewItemAtIndex: 1] setLabel: _NS("Codec Details")];
82     [[o_tab_view tabViewItemAtIndex: 2] setLabel: _NS("Statistics")];
83     [o_tab_view selectTabViewItemAtIndex: 0];
84
85     /* constants defined in vlc_meta.h */
86     [o_genre_lbl setStringValue: _NS(VLC_META_GENRE)];
87     [o_copyright_lbl setStringValue: _NS(VLC_META_COPYRIGHT)];
88     [o_collection_lbl setStringValue: _NS(VLC_META_ALBUM)];
89     [o_seqNum_lbl setStringValue: _NS(VLC_META_TRACK_NUMBER)];
90     [o_description_lbl setStringValue: _NS(VLC_META_DESCRIPTION)];
91     [o_date_lbl setStringValue: _NS(VLC_META_DATE)];
92     [o_language_lbl setStringValue: _NS(VLC_META_LANGUAGE)];
93     [o_nowPlaying_lbl setStringValue: _NS(VLC_META_NOW_PLAYING)];
94     [o_publisher_lbl setStringValue: _NS(VLC_META_PUBLISHER)];
95
96     /* statistics */
97     [o_input_box setTitle: _NS("Input")];
98     [o_read_bytes_lbl setStringValue: _NS("Read at media")];
99     [o_input_bitrate_lbl setStringValue: _NS("Input bitrate")];
100     [o_demux_bytes_lbl setStringValue: _NS("Demuxed")];
101     [o_demux_bitrate_lbl setStringValue: _NS("Stream bitrate")];
102
103     [o_video_box setTitle: _NS("Video")];
104     [o_video_decoded_lbl setStringValue: _NS("Decoded blocks")];
105     [o_displayed_lbl setStringValue: _NS("Displayed frames")];
106     [o_lost_frames_lbl setStringValue: _NS("Lost frames")];
107         [o_fps_lbl setStringValue: _NS("Frames per Second")];
108
109     [o_sout_box setTitle: _NS("Streaming")];
110     [o_sent_packets_lbl setStringValue: _NS("Sent packets")];
111     [o_sent_bytes_lbl setStringValue: _NS("Sent bytes")];
112     [o_sent_bitrate_lbl setStringValue: _NS("Send rate")];
113
114     [o_audio_box setTitle: _NS("Audio")];
115     [o_audio_decoded_lbl setStringValue: _NS("Decoded blocks")];
116     [o_played_abuffers_lbl setStringValue: _NS("Played buffers")];
117     [o_lost_abuffers_lbl setStringValue: _NS("Lost buffers")];
118
119     [o_info_window setInitialFirstResponder: o_uri_txt];
120     [o_info_window setDelegate: self];
121
122     /* We may be awoken from nib way after initialisation
123      * Update ourselves */
124     [self updatePanelWithItem:p_item];
125 }
126
127 - (void)stopTimers
128 {
129     /* make sure that the timer is released in any case */
130     if( o_statUpdateTimer && [o_statUpdateTimer isValid] )
131         [o_statUpdateTimer invalidate];
132
133     [rootItem release];
134
135     [o_statUpdateTimer release];
136     o_statUpdateTimer = nil;
137 }
138
139 - (void)dealloc
140 {
141     /* make sure that the timer is released in any case */
142     if( o_statUpdateTimer && [o_statUpdateTimer isValid] )
143         [o_statUpdateTimer invalidate];
144
145     [o_statUpdateTimer release];
146
147     if( p_item ) vlc_gc_decref( p_item );
148
149     [super dealloc];
150 }
151
152 - (void)windowDidBecomeKey:(NSNotification *)notification
153 {
154     BOOL b_stats = config_GetInt(VLCIntf, "stats");
155     if( b_stats )
156     {
157         if( o_statUpdateTimer )
158         {
159             [o_statUpdateTimer invalidate];
160             [o_statUpdateTimer release];
161             o_statUpdateTimer = nil;
162         }
163         o_statUpdateTimer = [NSTimer scheduledTimerWithTimeInterval: 1
164             target: self selector: @selector(updateStatistics:)
165             userInfo: nil repeats: YES];
166         [o_statUpdateTimer fire];
167         [o_statUpdateTimer retain];
168     }
169 }
170
171 - (BOOL)windowShouldClose:(id)sender
172 {
173     if( [o_statUpdateTimer isValid] )
174         [o_statUpdateTimer invalidate];
175
176     if( o_statUpdateTimer )
177         [o_statUpdateTimer release];
178     o_statUpdateTimer = nil;
179     return YES;
180 }
181
182 - (void)initPanel
183 {
184     BOOL b_stats = config_GetInt(VLCIntf, "stats");
185     if( !b_stats )
186     {
187         if( [o_tab_view numberOfTabViewItems] > 2 )
188             [o_tab_view removeTabViewItem: [o_tab_view tabViewItemAtIndex: 2]];
189     }
190
191     [self initMediaPanelStats];
192     [o_info_window makeKeyAndOrderFront: self];
193 }
194
195 - (void)initMediaPanelStats
196 {
197     //Initializing Input Variables
198     [o_read_bytes_txt setStringValue: [NSString stringWithFormat:@"%8.0f kB", (float)0]];
199     [o_input_bitrate_txt setStringValue: [NSString stringWithFormat:@"%6.0f kb/s", (float)0]];
200     [o_demux_bytes_txt setStringValue: [NSString stringWithFormat:@"%8.0f kB", (float)0]];
201     [o_demux_bitrate_txt setStringValue: [NSString stringWithFormat:@"%6.0f kb/s", (float)0]];
202     
203     //Initializing Video Variables
204     [o_video_decoded_txt setIntValue:0];
205     [o_displayed_txt setIntValue:0];
206     [o_lost_frames_txt setIntValue:0];
207     [o_fps_txt setFloatValue:0];
208
209     //Initializing Output Variables
210     [o_sent_packets_txt setIntValue: 0];
211     [o_sent_bytes_txt setStringValue: [NSString stringWithFormat:@"%8.0f kB", (float)0]];
212     [o_sent_bitrate_txt setStringValue: [NSString stringWithFormat:@"%6.0f kb/s", (float)0]];
213
214     //Initializing Audio Variables
215     [o_audio_decoded_txt setIntValue:0];
216     [o_played_abuffers_txt setIntValue: 0];
217     [o_lost_abuffers_txt setIntValue: 0];
218
219 }
220
221 - (void)updatePanelWithItem:(input_item_t *)_p_item;
222 {
223     if( _p_item != p_item )
224     {
225         if( p_item ) vlc_gc_decref( p_item );
226         [o_saveMetaData_btn setEnabled: NO];
227         if( _p_item ) vlc_gc_incref( _p_item );
228         p_item = _p_item;
229     }
230
231     if( !p_item )
232     {
233         /* Erase */
234     #define SET( foo ) \
235         [self setMeta: "" forLabel: o_##foo##_txt];
236         SET( uri );
237         SET( title );
238         SET( author );
239         SET( collection );
240         SET( seqNum );
241         SET( genre );
242         SET( copyright );
243         SET( publisher );
244         SET( nowPlaying );
245         SET( language );
246         SET( date );
247         SET( description );
248     #undef SET
249         [o_image_well setImage: [NSImage imageNamed: @"noart.png"]];
250     }
251     else
252     {
253         if( !input_item_IsPreparsed( p_item ) )
254         {
255             playlist_t * p_playlist = pl_Hold( VLCIntf );
256             playlist_PreparseEnqueue( p_playlist, p_item, pl_Unlocked );
257             pl_Release( VLCIntf );
258         }
259
260         /* fill uri info */
261         char * psz_url = input_item_GetURI( p_item );
262         [o_uri_txt setStringValue: [NSString stringWithUTF8String: psz_url ? psz_url : ""  ]];
263         free( psz_url );
264
265         /* fill title info */
266         char * psz_title = input_item_GetTitle( p_item );
267         if( !psz_title )
268             psz_title = input_item_GetName( p_item );
269         [o_title_txt setStringValue: [NSString stringWithUTF8String: psz_title ? : ""  ]];
270         free( psz_title );
271
272     #define SET( foo, bar ) \
273         char *psz_##foo = input_item_Get##bar ( p_item ); \
274         [self setMeta: psz_##foo forLabel: o_##foo##_txt]; \
275         FREENULL( psz_##foo );
276
277         /* fill the other fields */
278         SET( author, Artist );
279         SET( collection, Album );
280         SET( seqNum, TrackNum );
281         SET( genre, Genre );
282         SET( copyright, Copyright );
283         SET( publisher, Publisher );
284         SET( nowPlaying, NowPlaying );
285         SET( language, Language );
286         SET( date, Date );
287         SET( description, Description );
288
289     #undef SET
290
291         char *psz_meta;
292         NSImage *o_image;
293         psz_meta = input_item_GetArtURL( p_item );
294
295         /* FIXME Can also be attachment:// */
296         if( psz_meta && strncmp( psz_meta, "attachment://", 13 ) )
297             o_image = [[NSImage alloc] initWithContentsOfURL: [NSURL URLWithString:[NSString stringWithUTF8String: psz_meta]]];
298         else
299             o_image = [[NSImage imageNamed: @"noart.png"] retain];
300         [o_image_well setImage: o_image];
301         [o_image release];
302         FREENULL( psz_meta );
303     }
304
305     /* reload the advanced table */
306     [rootItem refresh];
307     [o_outline_view reloadData];
308
309     /* update the stats once to display p_item change faster */
310     [self updateStatistics: nil];
311 }
312
313 - (void)setMeta: (char *)psz_meta forLabel: (id)theItem
314 {
315     if( psz_meta != NULL && *psz_meta)
316         [theItem setStringValue: [NSString stringWithUTF8String:psz_meta]];
317     else
318         [theItem setStringValue: @""];
319 }
320
321 - (void)updateStatistics:(NSTimer*)theTimer
322 {
323     if( !p_item || !p_item->p_stats )
324     {
325         [self initMediaPanelStats];
326         return;
327     }
328
329     vlc_mutex_lock( &p_item->p_stats->lock );
330
331     /* input */
332     [o_read_bytes_txt setStringValue: [NSString stringWithFormat:
333         @"%8.0f kB", (float)(p_item->p_stats->i_read_bytes)/1000]];
334     [o_input_bitrate_txt setStringValue: [NSString stringWithFormat:
335         @"%6.0f kb/s", (float)(p_item->p_stats->f_input_bitrate)*8000]];
336     [o_demux_bytes_txt setStringValue: [NSString stringWithFormat:
337         @"%8.0f kB", (float)(p_item->p_stats->i_demux_read_bytes)/1000]];
338     [o_demux_bitrate_txt setStringValue: [NSString stringWithFormat:
339         @"%6.0f kb/s", (float)(p_item->p_stats->f_demux_bitrate)*8000]];
340
341     /* Video */
342     [o_video_decoded_txt setIntValue: p_item->p_stats->i_decoded_video];
343     [o_displayed_txt setIntValue: p_item->p_stats->i_displayed_pictures];
344     [o_lost_frames_txt setIntValue: p_item->p_stats->i_lost_pictures];
345     float f_fps = 0;
346     /* FIXME: input_Control( p_item, INPUT_GET_VIDEO_FPS, &f_fps ); */
347     [o_fps_txt setFloatValue: f_fps];
348
349     /* Sout */
350     [o_sent_packets_txt setIntValue: p_item->p_stats->i_sent_packets];
351     [o_sent_bytes_txt setStringValue: [NSString stringWithFormat: @"%8.0f kB",
352         (float)(p_item->p_stats->i_sent_bytes)/1000]];
353     [o_sent_bitrate_txt setStringValue: [NSString stringWithFormat:
354         @"%6.0f kb/s", (float)(p_item->p_stats->f_send_bitrate*8)*1000]];
355
356     /* Audio */
357     [o_audio_decoded_txt setIntValue: p_item->p_stats->i_decoded_audio];
358     [o_played_abuffers_txt setIntValue: p_item->p_stats->i_played_abuffers];
359     [o_lost_abuffers_txt setIntValue: p_item->p_stats->i_lost_abuffers];
360
361     vlc_mutex_unlock( &p_item->p_stats->lock );
362 }
363
364 - (IBAction)metaFieldChanged:(id)sender
365 {
366     [o_saveMetaData_btn setEnabled: YES];
367 }
368
369 - (IBAction)saveMetaData:(id)sender
370 {
371     playlist_t * p_playlist = pl_Hold( VLCIntf );
372
373     if( !p_item ) goto error;
374
375     meta_export_t p_export;
376     p_export.p_item = p_item;
377
378     /* we can write meta data only in a file */
379     vlc_mutex_lock( &p_item->lock );
380     int i_type = p_item->i_type;
381     vlc_mutex_unlock( &p_item->lock );
382
383     if( i_type != ITEM_TYPE_FILE )
384         goto error;
385
386     char *psz_uri_orig = input_item_GetURI( p_item );
387     char *psz_uri = psz_uri_orig;
388     if( !strncmp( psz_uri, "file://", 7 ) )
389         psz_uri += 7; /* strlen("file://") = 7 */
390
391     p_export.psz_file = strndup( psz_uri, PATH_MAX );
392     free( psz_uri_orig );
393
394     #define utf8( o_blub ) \
395         [[o_blub stringValue] UTF8String]
396
397     input_item_SetName( p_item, utf8( o_title_txt ) );
398     input_item_SetTitle( p_item, utf8( o_title_txt ) );
399     input_item_SetArtist( p_item, utf8( o_author_txt ) );
400     input_item_SetAlbum( p_item, utf8( o_collection_txt ) );
401     input_item_SetGenre( p_item, utf8( o_genre_txt ) );
402     input_item_SetTrackNum( p_item, utf8( o_seqNum_txt ) );
403     input_item_SetDate( p_item, utf8( o_date_txt ) );
404     input_item_SetCopyright( p_item, utf8( o_copyright_txt ) );
405     input_item_SetPublisher( p_item, utf8( o_publisher_txt ) );
406     input_item_SetDescription( p_item, utf8( o_description_txt ) );
407     input_item_SetLanguage( p_item, utf8( o_language_txt ) );
408
409     PL_LOCK;
410     p_playlist->p_private = &p_export;
411
412     module_t *p_mod = module_need( p_playlist, "meta writer", NULL, false );
413     if( p_mod )
414         module_unneed( p_playlist, p_mod );
415     PL_UNLOCK;
416
417     var_SetBool( p_playlist, "intf-change", true );
418     [self updatePanelWithItem: p_item];
419
420     pl_Release( VLCIntf );
421     [o_saveMetaData_btn setEnabled: NO];
422     return;
423
424 error:
425     pl_Release( VLCIntf );
426     NSRunAlertPanel(_NS("Error while saving meta"),
427         _NS("VLC was unable to save the meta data."),
428         _NS("OK"), nil, nil);
429 }
430
431 - (IBAction)downloadCoverArt:(id)sender
432 {
433     playlist_t * p_playlist = pl_Hold( VLCIntf );
434     if( p_item) playlist_AskForArtEnqueue( p_playlist, p_item, pl_Unlocked );
435     pl_Release( VLCIntf );
436 }
437
438 - (input_item_t *)item
439 {
440     if( p_item ) vlc_gc_incref( p_item );
441     return p_item;
442 }
443
444 @end
445
446 @implementation VLCInfo (NSMenuValidation)
447
448 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
449 {
450     BOOL bEnabled = TRUE;
451
452     if( [[o_mi title] isEqualToString: _NS("Information")] )
453     {
454         return ![[[VLCMain sharedInstance] playlist] isSelectionEmpty];
455     }
456
457     return TRUE;
458 }
459
460 @end
461
462 @implementation VLCInfo (NSTableDataSource)
463
464 - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
465 {
466     return (item == nil) ? [rootItem numberOfChildren] : [item numberOfChildren];
467 }
468
469 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
470     return ([item numberOfChildren] > 0);
471 }
472
473 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
474 {
475     return (item == nil) ? [rootItem childAtIndex:index] : (id)[item childAtIndex:index];
476 }
477
478 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
479 {
480     if ([[tableColumn identifier] isEqualToString:@"0"])
481     {
482         return (item == nil) ? @"" : (id)[item name];
483     }
484     else
485     {
486         return (item == nil) ? @"" : (id)[item value];
487     }
488 }
489
490 @end
491
492 @implementation VLCInfoTreeItem
493
494 #define IsALeafNode ((id)-1)
495
496 - (id)initWithName: (NSString *)o_item_name value: (NSString *)o_item_value ID: (int)i_id
497        parent:(VLCInfoTreeItem *)o_parent_item
498 {
499     self = [super init];
500
501     if( self != nil )
502     {
503         o_name = [o_item_name copy];
504         o_value = [o_item_value copy];
505         i_object_id = i_id;
506         o_parent = o_parent_item;
507         p_item = [[[VLCMain sharedInstance] info] item];
508         o_children = nil;
509     }
510     return( self );
511 }
512
513 - (id)init
514 {
515     return [self initWithName:@"main" value:@"" ID:-1 parent:nil];
516 }
517
518 - (void)dealloc
519 {
520     if( o_children != IsALeafNode ) [o_children release];
521     [o_name release];
522     [o_value release];
523     if( p_item ) vlc_gc_decref( p_item );
524     [super dealloc];
525 }
526
527 /* Creates and returns the array of children
528  * Loads children incrementally */
529 - (NSArray *)children
530 {
531     if( !p_item ) return nil;
532
533     if (o_children == NULL)
534     {
535         int i;
536
537         if( i_object_id == -1 )
538         {
539             vlc_mutex_lock( &p_item->lock );
540             o_children = [[NSMutableArray alloc] initWithCapacity: p_item->i_categories];
541             for (i = 0 ; i < p_item->i_categories ; i++)
542             {
543                 NSString * name = [NSString stringWithUTF8String: p_item->pp_categories[i]->psz_name];
544                 VLCInfoTreeItem * item = [[VLCInfoTreeItem alloc] initWithName:name value:@"" ID:i parent:self];
545                 [item autorelease];
546                 [o_children addObject:item];
547             }
548             vlc_mutex_unlock( &p_item->lock );
549         }
550         else if( o_parent->i_object_id == -1 )
551         {
552             vlc_mutex_lock( &p_item->lock );
553             info_category_t * cat = p_item->pp_categories[i_object_id];
554             o_children = [[NSMutableArray alloc] initWithCapacity: cat->i_infos];
555             for (i = 0 ; i < cat->i_infos ; i++)
556             {
557                 NSString * name = [NSString stringWithUTF8String: cat->pp_infos[i]->psz_name];
558                 NSString * value = [NSString stringWithUTF8String: cat->pp_infos[i]->psz_value ? : ""];
559                 VLCInfoTreeItem * item = [[VLCInfoTreeItem alloc] initWithName:name value:value ID:i parent:self];
560                 [item autorelease];
561                 [o_children addObject:item];
562             }
563             vlc_mutex_unlock( &p_item->lock );
564         }
565         else
566         {
567             o_children = IsALeafNode;
568         }
569     }
570     return o_children;
571 }
572
573 - (NSString *)name
574 {
575     return [[o_name retain] autorelease];
576 }
577
578 - (NSString *)value
579 {
580     return [[o_value retain] autorelease];
581 }
582
583 - (void)refresh
584 {
585     input_item_t * oldItem = p_item;
586     p_item = [[[VLCMain sharedInstance] info] item];
587     if( oldItem && oldItem != p_item ) vlc_gc_decref( oldItem );
588
589     [o_children release];
590     o_children = nil;
591 }
592
593 - (VLCInfoTreeItem *)childAtIndex:(NSUInteger)i_index {
594     return [[self children] objectAtIndex:i_index];
595 }
596
597 - (int)numberOfChildren {
598
599     id i_tmp = [self children];
600     return ( i_tmp == IsALeafNode ) ? (-1) : (int)[i_tmp count];
601 }
602
603 @end
604