]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlistinfo.m
* re-designed the information/properties panel to show meta information and statistics
[vlc] / modules / gui / macosx / playlistinfo.m
1 /*****************************************************************************
2  r playlistinfo.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2002-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Benjamin Pracht <bigben at videolan dot org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #include "intf.h"
29 #include "playlistinfo.h"
30 #include "playlist.h"
31
32 /*****************************************************************************
33  * VLCPlaylistInfo Implementation
34  *****************************************************************************/
35
36 @implementation VLCInfo
37
38 - (id)init
39 {
40     self = [super init];
41
42     if( self != nil )
43     {
44         p_item = NULL;
45     }
46     return( self );
47 }
48
49 - (void)awakeFromNib
50 {
51     [o_info_window setExcludedFromWindowsMenu: TRUE];
52
53     [o_info_window setTitle: _NS("Properties")];
54     [o_uri_lbl setStringValue: _NS("URI")];
55     [o_title_lbl setStringValue: _NS("Title")];
56     [o_author_lbl setStringValue: _NS("Author")];
57     [o_btn_ok setTitle: _NS("OK")];
58     [o_btn_cancel setTitle: _NS("Cancel")];
59     
60     [[o_tab_view tabViewItemAtIndex: 0] setLabel: _NS("General")];
61     [[o_tab_view tabViewItemAtIndex: 1] setLabel: _NS("Advanced Information")];
62     [[o_tab_view tabViewItemAtIndex: 2] setLabel: _NS("Statistics")];
63     [o_tab_view selectTabViewItemAtIndex: 0];
64
65     /* constants defined in vlc_meta.h */
66     [o_genre_lbl setStringValue: _NS(VLC_META_GENRE)];
67     [o_copyright_lbl setStringValue: _NS(VLC_META_COPYRIGHT)];
68     [o_collection_lbl setStringValue: _NS(VLC_META_COLLECTION)];
69     [o_seqNum_lbl setStringValue: _NS(VLC_META_SEQ_NUM)];
70     [o_description_lbl setStringValue: _NS(VLC_META_DESCRIPTION)];
71     [o_rating_lbl setStringValue: _NS(VLC_META_RATING)];
72     [o_date_lbl setStringValue: _NS(VLC_META_DATE)];
73     [o_language_lbl setStringValue: _NS(VLC_META_LANGUAGE)];
74     [o_nowPlaying_lbl setStringValue: _NS(VLC_META_NOW_PLAYING)];
75     [o_publisher_lbl setStringValue: _NS(VLC_META_PUBLISHER)];
76 }
77
78 - (IBAction)togglePlaylistInfoPanel:(id)sender
79 {
80     if( [o_info_window isVisible] )
81     {
82         [o_info_window orderOut: sender];
83     }
84     else
85     {
86         p_item = [[[VLCMain sharedInstance] getPlaylist] selectedPlaylistItem];
87         [self initPanel:sender];
88     }
89 }
90
91 - (IBAction)toggleInfoPanel:(id)sender
92 {
93     if( [o_info_window isVisible] )
94     {
95         [o_info_window orderOut: sender];
96     }
97     else
98     {
99         intf_thread_t * p_intf = VLCIntf;
100         playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
101                                           FIND_ANYWHERE );
102
103         if( p_playlist )
104         {
105             p_item = p_playlist->status.p_item;
106             vlc_object_release( p_playlist );
107         }
108         [self initPanel:sender];
109     }
110 }
111
112 - (void)initPanel:(id)sender
113 {
114     char *psz_temp;
115     vlc_mutex_lock( &p_item->input.lock );
116
117     /*fill uri / title / author info */
118     if( p_item->input.psz_uri )
119     {
120         [o_uri_txt setStringValue:
121             ([NSString stringWithUTF8String:p_item->input.psz_uri] == nil ) ?
122             [NSString stringWithCString:p_item->input.psz_uri] :
123             [NSString stringWithUTF8String:p_item->input.psz_uri]];
124     }
125
126     if( p_item->input.psz_name )
127     {
128         [o_title_txt setStringValue:
129             ([NSString stringWithUTF8String:p_item->input.psz_name] == nil ) ?
130             [NSString stringWithCString:p_item->input.psz_name] :
131             [NSString stringWithUTF8String:p_item->input.psz_name]];
132     }
133     vlc_mutex_unlock( &p_item->input.lock );
134
135     psz_temp = vlc_input_item_GetInfo( &p_item->input, _("Meta-information"), _("Artist") );
136
137     if( psz_temp )
138     {
139         [o_author_txt setStringValue: [NSString stringWithUTF8String: psz_temp]];
140         free( psz_temp );
141     }
142
143     /* fill the other fields */
144     [self setMeta: VLC_META_GENRE forLabel: o_genre_txt];
145     [self setMeta: VLC_META_COPYRIGHT forLabel: o_copyright_txt];
146     [self setMeta: VLC_META_COLLECTION forLabel: o_collection_txt];
147     [self setMeta: VLC_META_SEQ_NUM forLabel: o_seqNum_txt];
148     [self setMeta: VLC_META_DESCRIPTION forLabel: o_description_txt];
149     [self setMeta: VLC_META_RATING forLabel: o_rating_txt];
150     [self setMeta: VLC_META_DATE forLabel: o_date_txt];
151     [self setMeta: VLC_META_LANGUAGE forLabel: o_language_txt];
152     [self setMeta: VLC_META_NOW_PLAYING forLabel: o_nowPlaying_txt];
153     [self setMeta: VLC_META_PUBLISHER forLabel: o_publisher_txt];
154
155     /* reload the advanced table */
156     [[VLCInfoTreeItem rootItem] refresh];
157     [o_outline_view reloadData];
158
159     [o_info_window makeKeyAndOrderFront: sender];
160 }
161
162 - (void)setMeta: (char *)meta forLabel: (id)theItem
163 {
164     char *psz_meta = vlc_input_item_GetInfo( &p_item->input, \
165         _(VLC_META_INFO_CAT), _(meta) );
166     if( psz_meta != NULL && *psz_meta)
167         [theItem setStringValue: [NSString stringWithUTF8String: psz_meta]];
168     else
169         [theItem setStringValue: @"-"];
170 }
171
172 - (IBAction)infoCancel:(id)sender
173 {
174     [o_info_window orderOut: self];
175 }
176
177
178 - (IBAction)infoOk:(id)sender
179 {
180     intf_thread_t * p_intf = VLCIntf;
181     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
182                                           FIND_ANYWHERE );
183     vlc_value_t val;
184
185     if( [self isItemInPlaylist: p_item] )
186     {
187         vlc_mutex_lock( &p_item->input.lock );
188
189         p_item->input.psz_uri = strdup( [[o_uri_txt stringValue] UTF8String] );
190         p_item->input.psz_name = strdup( [[o_title_txt stringValue] UTF8String] );
191         vlc_mutex_unlock( &p_item->input.lock );
192         vlc_input_item_AddInfo( &p_item->input, _("Meta-information"), _("Artist"), [[o_author_txt stringValue] UTF8String]);
193         
194         val.b_bool = VLC_TRUE;
195         var_Set( p_playlist, "intf-change", val );
196     }
197     vlc_object_release( p_playlist );
198     [o_info_window orderOut: self];
199 }
200
201 - (playlist_item_t *)getItem
202 {
203     return p_item;
204 }
205
206 - (BOOL)isItemInPlaylist:(playlist_item_t *)p_local_item
207 {
208     intf_thread_t * p_intf = VLCIntf;
209     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
210                                           FIND_ANYWHERE );
211     int i;
212
213     if( p_playlist == NULL )
214     {
215         return NO;
216     }
217
218     for( i = 0 ; i < p_playlist->i_size ; i++ )
219     {
220         if( p_playlist->pp_items[i] == p_local_item )
221         {
222             vlc_object_release( p_playlist );
223             return YES;
224         }
225     }
226     vlc_object_release( p_playlist );
227     return NO;
228 }
229
230 @end
231
232 @implementation VLCInfo (NSMenuValidation)
233
234 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
235 {
236     BOOL bEnabled = TRUE;
237
238     intf_thread_t * p_intf = VLCIntf;
239     input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
240                                                        FIND_ANYWHERE );
241
242     if( [[o_mi title] isEqualToString: _NS("Info")] )
243     {
244         if( p_input == NULL )
245         {
246             bEnabled = FALSE;
247         }
248     }
249     if( p_input ) vlc_object_release( p_input );
250
251     return( bEnabled );
252 }
253
254 @end
255
256 @implementation VLCInfo (NSTableDataSource)
257
258 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
259 {
260     return (item == nil) ? [[VLCInfoTreeItem rootItem] numberOfChildren] : [item numberOfChildren];
261 }
262
263 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
264     return ([item numberOfChildren] > 0);
265 }
266
267 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
268 {
269     return (item == nil) ? [[VLCInfoTreeItem rootItem] childAtIndex:index] : [item childAtIndex:index];
270 }
271
272 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
273 {
274     if ([[tableColumn identifier] isEqualToString:@"0"])
275     {
276         return (item == nil) ? @"" : (id)[item getName];
277     }
278     else
279     {
280         return (item == nil) ? @"" : (id)[item getValue];
281     }
282 }
283
284
285 @end
286
287 @implementation VLCInfoTreeItem
288
289 static VLCInfoTreeItem *o_root_item = nil;
290
291 #define IsALeafNode ((id)-1)
292
293 - (id)initWithName: (NSString *)o_item_name value: (NSString *)o_item_value ID: (int)i_id parent:(VLCInfoTreeItem *)o_parent_item
294 {
295     self = [super init];
296
297     if( self != nil )
298     {
299         o_name = [o_item_name copy];
300         o_value = [o_item_value copy];
301         i_object_id = i_id;
302         o_parent = o_parent_item;
303         if( [[VLCMain sharedInstance] getInfo] != nil )
304             p_item = [[[VLCMain sharedInstance] getInfo] getItem];
305         else
306             p_item = NULL;
307     }
308     return( self );
309 }
310
311 + (VLCInfoTreeItem *)rootItem {
312     if (o_root_item == nil) o_root_item = [[VLCInfoTreeItem alloc] initWithName:@"main" value: @"" ID: 0 parent:nil];
313     return o_root_item;
314 }
315
316 - (void)dealloc
317 {
318     if( o_children != IsALeafNode ) [o_children release];
319     [o_name release];
320     [super dealloc];
321 }
322
323 /* Creates and returns the array of children
324  * Loads children incrementally */
325 - (NSArray *)children
326 {
327     if (o_children == NULL)
328     {
329         int i;
330
331         if( [[[VLCMain sharedInstance] getInfo] isItemInPlaylist: p_item] )
332         {
333             if( self == o_root_item )
334             {
335                 vlc_mutex_lock( &p_item->input.lock );
336                 o_children = [[NSMutableArray alloc] initWithCapacity:
337                                                 p_item->input.i_categories];
338                 for (i = 0 ; i < p_item->input.i_categories ; i++)
339                 {
340                     [o_children addObject:[[VLCInfoTreeItem alloc]
341                         initWithName: [NSString stringWithUTF8String:
342                             p_item->input.pp_categories[i]->psz_name]
343                         value: @""
344                         ID: i
345                         parent: self]];
346                 }
347                 vlc_mutex_unlock( &p_item->input.lock );
348             }
349             else if( o_parent == o_root_item )
350             {
351                 vlc_mutex_lock( &p_item->input.lock );
352                 o_children = [[NSMutableArray alloc] initWithCapacity:
353                     p_item->input.pp_categories[i_object_id]->i_infos];
354
355                 for (i = 0 ; i < p_item->input.pp_categories[i_object_id]->i_infos ; i++)
356                 {
357                     [o_children addObject:[[VLCInfoTreeItem alloc]
358                     initWithName: [NSString stringWithUTF8String:
359                             p_item->input.pp_categories[i_object_id]->pp_infos[i]->psz_name]
360                         value: [NSString stringWithUTF8String:
361                             p_item->input.pp_categories[i_object_id]->pp_infos[i]->psz_value]
362                         ID: i
363                         parent: self]];
364                 }
365                 vlc_mutex_unlock( &p_item->input.lock );
366             }
367             else
368             {
369                 o_children = IsALeafNode;
370             }
371         }
372     }
373     return o_children;
374 }
375
376 - (NSString *)getName
377 {
378     return o_name;
379 }
380
381 - (NSString *)getValue
382 {
383     return o_value;
384 }
385
386 - (VLCInfoTreeItem *)childAtIndex:(int)i_index {
387     return [[self children] objectAtIndex:i_index];
388 }
389
390 - (int)numberOfChildren {
391     id i_tmp = [self children];
392     return ( i_tmp == IsALeafNode ) ? (-1) : (int)[i_tmp count];
393 }
394
395 /*- (int)selectedPlaylistItem
396 {
397     return i_item;
398 }
399 */
400 - (void)refresh
401 {
402     p_item = [[[VLCMain sharedInstance] getInfo] getItem];
403     if( o_children != NULL )
404     {
405         [o_children release];
406         o_children = NULL;
407     }
408 }
409
410 @end
411