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