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