]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlistinfo.m
* fix author field of OSX info dialog
[vlc] / modules / gui / macosx / playlistinfo.m
1 /*****************************************************************************
2  r playlistinfo.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2002-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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
61 - (IBAction)togglePlaylistInfoPanel:(id)sender
62 {
63     if( [o_info_window isVisible] )
64     {
65         [o_info_window orderOut: sender];
66     }
67     else
68     {
69         p_item = [[[VLCMain sharedInstance] getPlaylist] selectedPlaylistItem];
70         [self initPanel:sender];
71     }
72 }
73
74 - (IBAction)toggleInfoPanel:(id)sender
75 {
76     if( [o_info_window isVisible] )
77     {
78         [o_info_window orderOut: sender];
79     }
80     else
81     {
82         intf_thread_t * p_intf = VLCIntf;
83         playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
84                                           FIND_ANYWHERE );
85
86         if( p_playlist )
87         {
88             p_item = p_playlist->status.p_item;
89             vlc_object_release( p_playlist );
90         }
91         [self initPanel:sender];
92     }
93 }
94
95 - (void)initPanel:(id)sender
96 {
97     char *psz_temp;
98     vlc_mutex_lock( &p_item->input.lock );
99
100
101     /*fill uri / title / author info */
102     if( p_item->input.psz_uri )
103     {
104         [o_uri_txt setStringValue:
105             ([NSString stringWithUTF8String:p_item->input.psz_uri] == nil ) ?
106             [NSString stringWithCString:p_item->input.psz_uri] :
107             [NSString stringWithUTF8String:p_item->input.psz_uri]];
108     }
109
110     if( p_item->input.psz_name )
111     {
112         [o_title_txt setStringValue:
113             ([NSString stringWithUTF8String:p_item->input.psz_name] == nil ) ?
114             [NSString stringWithCString:p_item->input.psz_name] :
115             [NSString stringWithUTF8String:p_item->input.psz_name]];
116     }
117     vlc_mutex_unlock( &p_item->input.lock );
118     
119     psz_temp = vlc_input_item_GetInfo( &p_item->input, _("Meta-information"), _("Artist") );
120
121     if( psz_temp )
122     {
123         [o_author_txt setStringValue: [NSString stringWithUTF8String: psz_temp]];
124     }
125
126     free( psz_temp );
127
128     [[VLCInfoTreeItem rootItem] refresh];
129     [o_outline_view reloadData];
130
131     [o_info_window makeKeyAndOrderFront: sender];
132 }
133
134 - (IBAction)infoCancel:(id)sender
135 {
136     [o_info_window orderOut: self];
137 }
138
139
140 - (IBAction)infoOk:(id)sender
141 {
142     intf_thread_t * p_intf = VLCIntf;
143     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
144                                           FIND_ANYWHERE );
145     vlc_value_t val;
146
147     if ([self isItemInPlaylist: p_item] )
148     {
149         vlc_mutex_lock( &p_item->input.lock );
150
151         p_item->input.psz_uri = strdup( [[o_uri_txt stringValue] UTF8String] );
152         p_item->input.psz_name = strdup( [[o_title_txt stringValue] UTF8String] );
153         vlc_mutex_unlock( &p_item->input.lock );
154         vlc_input_item_AddInfo( &p_item->input, _("Meta-information"), _("Artist"), [[o_author_txt stringValue] UTF8String]);
155         
156         val.b_bool = VLC_TRUE;
157         var_Set( p_playlist, "intf-change", val );
158     }
159
160     [o_info_window orderOut: self];
161 }
162
163 - (playlist_item_t *)getItem
164 {
165     return p_item;
166 }
167
168 - (BOOL)isItemInPlaylist:(playlist_item_t *)p_local_item
169 {
170     intf_thread_t * p_intf = VLCIntf;
171     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
172                                           FIND_ANYWHERE );
173     int i;
174
175     if( p_playlist == NULL )
176     {
177         return NO;
178     }
179
180     for( i = 0 ; i < p_playlist->i_size ; i++ )
181     {
182         if( p_playlist->pp_items[i] == p_local_item )
183         {
184             vlc_object_release( p_playlist );
185             return YES;
186         }
187     }
188     vlc_object_release( p_playlist );
189     return NO;
190 }
191
192 @end
193
194 @implementation VLCInfo (NSMenuValidation)
195
196 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
197 {
198     BOOL bEnabled = TRUE;
199
200     intf_thread_t * p_intf = VLCIntf;
201     input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
202                                                        FIND_ANYWHERE );
203
204     if( [[o_mi title] isEqualToString: _NS("Info")] )
205     {
206         if( p_input == NULL )
207         {
208             bEnabled = FALSE;
209         }
210     }
211     if( p_input ) vlc_object_release( p_input );
212
213     return( bEnabled );
214 }
215
216 @end
217
218 @implementation VLCInfo (NSTableDataSource)
219
220 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
221 {
222     return (item == nil) ? [[VLCInfoTreeItem rootItem] numberOfChildren] : [item numberOfChildren];
223 }
224
225 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
226     return ([item numberOfChildren] > 0);
227 }
228
229 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
230 {
231     return (item == nil) ? [[VLCInfoTreeItem rootItem] childAtIndex:index] : [item childAtIndex:index];
232 }
233
234 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
235 {
236     if ([[tableColumn identifier] isEqualToString:@"0"])
237     {
238         return (item == nil) ? @"" : (id)[item getName];
239     }
240     else
241     {
242         return (item == nil) ? @"" : (id)[item getValue];
243     }
244 }
245
246
247 @end
248
249 @implementation VLCInfoTreeItem
250
251 static VLCInfoTreeItem *o_root_item = nil;
252
253 #define IsALeafNode ((id)-1)
254
255 - (id)initWithName: (NSString *)o_item_name value: (NSString *)o_item_value ID: (int)i_id parent:(VLCInfoTreeItem *)o_parent_item
256 {
257     self = [super init];
258
259     if( self != nil )
260     {
261         o_name = [o_item_name copy];
262         o_value = [o_item_value copy];
263         i_object_id = i_id;
264         o_parent = o_parent_item;
265         p_item = [[[VLCMain sharedInstance] getInfo] getItem];
266     }
267     return( self );
268 }
269
270 + (VLCInfoTreeItem *)rootItem {
271     if (o_root_item == nil) o_root_item = [[VLCInfoTreeItem alloc] initWithName:@"main" value: @"" ID: 0 parent:nil];
272     return o_root_item;
273 }
274
275 - (void)dealloc
276 {
277     if( o_children != IsALeafNode ) [o_children release];
278     [o_name release];
279     [super dealloc];
280 }
281
282 /* Creates and returns the array of children
283  * Loads children incrementally */
284 - (NSArray *)children
285 {
286     if (o_children == NULL)
287     {
288         int i;
289
290         if( [[[VLCMain sharedInstance] getInfo] isItemInPlaylist: p_item] )
291         {
292             if( self == o_root_item )
293             {
294                 vlc_mutex_lock( &p_item->input.lock );
295                 o_children = [[NSMutableArray alloc] initWithCapacity:
296                                                 p_item->input.i_categories];
297                 for (i = 0 ; i < p_item->input.i_categories ; i++)
298                 {
299                     [o_children addObject:[[VLCInfoTreeItem alloc]
300                         initWithName: [NSString stringWithUTF8String:
301                             p_item->input.pp_categories[i]->psz_name]
302                         value: @""
303                         ID: i
304                         parent: self]];
305                 }
306                 vlc_mutex_unlock( &p_item->input.lock );
307             }
308             else if( o_parent == o_root_item )
309             {
310                 vlc_mutex_lock( &p_item->input.lock );
311                 o_children = [[NSMutableArray alloc] initWithCapacity:
312                     p_item->input.pp_categories[i_object_id]->i_infos];
313
314                 for (i = 0 ; i < p_item->input.pp_categories[i_object_id]->i_infos ; i++)
315                 {
316                     [o_children addObject:[[VLCInfoTreeItem alloc]
317                     initWithName: [NSString stringWithUTF8String:
318                             p_item->input.pp_categories[i_object_id]->pp_infos[i]->psz_name]
319                         value: [NSString stringWithUTF8String:
320                             p_item->input.pp_categories[i_object_id]->pp_infos[i]->psz_value]
321                         ID: i
322                         parent: self]];
323                 }
324                 vlc_mutex_unlock( &p_item->input.lock );
325             }
326             else
327             {
328                 o_children = IsALeafNode;
329             }
330         }
331     }
332     return o_children;
333 }
334
335 - (NSString *)getName
336 {
337     return o_name;
338 }
339
340 - (NSString *)getValue
341 {
342     return o_value;
343 }
344
345 - (VLCInfoTreeItem *)childAtIndex:(int)i_index {
346     return [[self children] objectAtIndex:i_index];
347 }
348
349 - (int)numberOfChildren {
350     id i_tmp = [self children];
351     return ( i_tmp == IsALeafNode ) ? (-1) : (int)[i_tmp count];
352 }
353
354 /*- (int)selectedPlaylistItem
355 {
356     return i_item;
357 }
358 */
359 - (void)refresh
360 {
361     p_item = [[[VLCMain sharedInstance] getInfo] getItem];
362     if( o_children != NULL )
363     {
364         [o_children release];
365         o_children = NULL;
366     }
367 }
368
369 @end
370