]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlistinfo.m
* fixing the OSX playlist (at least it compiles again)
[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         i_item = -1;
45         o_selected = NULL;
46     }
47     return( self );
48 }
49
50 - (void)dealloc
51 {
52     [o_selected release];
53     [super dealloc];
54 }
55
56 - (void)awakeFromNib
57 {
58     [o_info_window setExcludedFromWindowsMenu: TRUE];
59
60     [o_info_window setTitle: _NS("Properties")];
61     [o_uri_lbl setStringValue: _NS("URI")];
62     [o_title_lbl setStringValue: _NS("Title")];
63     [o_author_lbl setStringValue: _NS("Author")];
64     [o_btn_ok setTitle: _NS("OK")];
65     [o_btn_cancel setTitle: _NS("Cancel")];
66     [o_btn_delete_group setTitle: _NS("Delete Group")];
67     [o_btn_add_group setTitle: _NS("Add Group")];
68     [o_group_lbl setStringValue: _NS("Group")];
69 }
70
71 - (IBAction)togglePlaylistInfoPanel:(id)sender
72 {
73     if( [o_info_window isVisible] )
74     {
75         [o_info_window orderOut: sender];
76     }
77     else
78     {
79         i_item = [[[VLCMain sharedInstance] getPlaylist] selectedPlaylistItem];
80         o_selected = [[[VLCMain sharedInstance] getPlaylist] selectedPlaylistItemsList];
81         [o_selected retain];
82         [self initPanel:sender];
83     }
84 }
85
86 - (IBAction)toggleInfoPanel:(id)sender
87 {
88     if( [o_info_window isVisible] )
89     {
90         [o_info_window orderOut: sender];
91     }
92     else
93     {
94         intf_thread_t * p_intf = VLCIntf;
95         playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
96                                           FIND_ANYWHERE );
97
98         if (p_playlist)
99         {
100             i_item = p_playlist->i_index;
101             o_selected = [NSMutableArray arrayWithObject:
102                             [NSNumber numberWithInt:i_item]];
103             [o_selected retain];
104             vlc_object_release(p_playlist);
105         }
106         [self initPanel:sender];
107     }
108 }
109
110 - (void)initPanel:(id)sender
111 {
112     intf_thread_t * p_intf = VLCIntf;
113     playlist_t * p_playlist;
114
115     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
116                                           FIND_ANYWHERE );
117
118
119     if( p_playlist )
120     {
121         char *psz_temp;
122
123         /*fill uri / title / author info */
124         [o_uri_txt setStringValue:
125             ([NSString stringWithUTF8String:p_playlist->
126                pp_items[i_item]->input.psz_uri] == nil ) ?
127             [NSString stringWithCString:p_playlist->
128                 pp_items[i_item]->input.psz_uri] :
129             [NSString stringWithUTF8String:p_playlist->
130                 pp_items[i_item]->input.psz_uri]];
131
132         [o_title_txt setStringValue:
133             ([NSString stringWithUTF8String:p_playlist->
134                 pp_items[i_item]->input.psz_name] == nil ) ?
135             [NSString stringWithCString:p_playlist->
136                 pp_items[i_item]->input.psz_name] :
137             [NSString stringWithUTF8String:p_playlist->
138                 pp_items[i_item]->input.psz_name]];
139
140         psz_temp = playlist_GetInfo( p_playlist, i_item ,_("General"),_("Author") );
141         [o_author_txt setStringValue: [NSString stringWithUTF8String: psz_temp]];
142         free( psz_temp );
143
144         [[VLCInfoTreeItem rootItem] refresh];
145         [o_outline_view reloadData];
146
147         [self createComboBox];
148         [self handleGroup:self];
149
150         vlc_object_release( p_playlist );
151     }
152     [o_info_window makeKeyAndOrderFront: sender];
153 }
154
155 - (IBAction)infoCancel:(id)sender
156 {
157     [o_info_window orderOut: self];
158 }
159
160
161 - (IBAction)infoOk:(id)sender
162 {
163     int i,i_row,c;
164     intf_thread_t * p_intf = VLCIntf;
165     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
166                                           FIND_ANYWHERE );
167     vlc_value_t val;
168     NSNumber * o_number;
169
170
171     if (p_playlist)
172     {
173         vlc_mutex_lock(&p_playlist->pp_items[i_item]->input.lock);
174
175         p_playlist->pp_items[i_item]->input.psz_uri =
176             strdup([[o_uri_txt stringValue] cString]);
177         p_playlist->pp_items[i_item]->input.psz_name =
178             strdup([[o_title_txt stringValue] cString]);
179         playlist_ItemAddInfo(p_playlist->pp_items[i_item],_("General"),_("Author"), [[o_author_txt stringValue] cString]);
180
181         c = (int)[o_selected count];
182 #if 0
183         if ([[o_group_cbx stringValue] isEqual:
184                     [o_group_cbx objectValueOfSelectedItem]])
185         {
186             for (i = 0 ; i < c ; i++)
187             {
188                 o_number = [o_selected lastObject];
189                 i_row = [o_number intValue];
190                 p_playlist->pp_items[i_row]->i_group = p_playlist->
191                     pp_groups[[o_group_cbx indexOfSelectedItem]]->i_id;
192                 [o_selected removeObject: o_number];
193             }
194         }
195         else
196         {
197             playlist_group_t * p_group = playlist_CreateGroup( p_playlist,
198                 strdup([[o_group_cbx stringValue] cString]));
199             if (p_group)
200             {
201                 for (i = 0 ; i < c ; i++)
202                 {
203                     o_number = [o_selected lastObject];
204                     i_row = [o_number intValue];
205                     p_playlist->pp_items[i_row]->i_group = p_group->i_id;
206                     [o_selected removeObject: o_number];
207                 }
208             }
209         }
210
211 #endif
212         vlc_mutex_unlock(&p_playlist->pp_items[i_item]->input.lock);
213         val.b_bool = VLC_TRUE;
214         var_Set( p_playlist,"intf-change",val );
215         vlc_object_release ( p_playlist );
216     }
217     [o_info_window orderOut: self];
218 }
219
220 - (IBAction)handleGroup:(id)sender
221 {
222 #if 0
223     intf_thread_t * p_intf = VLCIntf;
224     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
225                                           FIND_ANYWHERE );
226
227     if (p_playlist)
228     {
229         if ([[o_group_cbx stringValue] isEqual:
230                     [o_group_cbx objectValueOfSelectedItem]])
231         {
232             [o_group_color setBackgroundColor:[[[VLCMain sharedInstance] getPlaylist]
233                 getColor: p_playlist->pp_groups[
234                 [o_group_cbx indexOfSelectedItem]]->i_id]];
235         }
236         else
237         {
238             [o_group_color setBackgroundColor:[[[VLCMain sharedInstance] getPlaylist]
239                 getColor:p_playlist->pp_groups[
240                 [o_group_cbx numberOfItems] - 1]->i_id + 1]];
241         }
242     vlc_object_release(p_playlist);
243     }
244 #endif
245 }
246
247 - (IBAction)deleteOutlineGroup:(id)sender
248 {
249 #if 0
250     intf_thread_t * p_intf = VLCIntf;
251     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
252                                           FIND_ANYWHERE );
253
254     if(p_playlist)
255     {
256         if ([[o_group_cbx stringValue] isEqual:
257                     [o_group_cbx objectValueOfSelectedItem]])
258         {
259             [[[VLCMain sharedInstance] getPlaylist] deleteGroup:p_playlist->pp_groups[
260                     [o_group_cbx indexOfSelectedItem]]->i_id];
261             [self createComboBox];
262             [self handleGroup:self];
263             [o_group_cbx reloadData];
264         }
265         else
266         {
267             msg_Warn(p_playlist,"Group doesn't exist, cannot delete");
268         }
269     vlc_object_release(p_playlist);
270     }
271 #endif
272 }
273
274 - (IBAction)createOutlineGroup:(id)sender;
275 {
276 #if 0
277     intf_thread_t * p_intf = VLCIntf;
278     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
279                                           FIND_ANYWHERE );
280     if(p_playlist)
281     {
282         playlist_CreateGroup( p_playlist,
283                     strdup([[o_group_cbx stringValue] cString]));
284         [self createComboBox];
285         [o_group_cbx reloadData];
286         [[[VLCMain sharedInstance] getPlaylist] playlistUpdated];
287         vlc_object_release(p_playlist);
288     }
289 #endif
290 }
291
292 -(void)createComboBox
293 {
294 #if 0
295     intf_thread_t * p_intf = VLCIntf;
296     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
297                                           FIND_ANYWHERE );
298     int i;
299
300     [o_group_cbx removeAllItems];
301
302     if (p_playlist)
303     {
304         for (i = 0; i < p_playlist->i_groups ; i++)
305         {
306             [o_group_cbx addItemWithObjectValue:
307                 [NSString stringWithUTF8String:
308                 p_playlist->pp_groups[i]->psz_name]];
309             if (p_playlist->pp_items[i_item]->i_group == p_playlist
310                 ->pp_groups[i]->i_id)
311             {
312                 [o_group_cbx selectItemAtIndex:i];
313             }
314         }
315     vlc_object_release(p_playlist);
316     }
317 #endif
318 }
319
320 - (int)getItem
321 {
322     return i_item;
323 }
324
325 @end
326
327 @implementation VLCInfo (NSMenuValidation)
328
329 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
330 {
331     BOOL bEnabled = TRUE;
332
333     intf_thread_t * p_intf = VLCIntf;
334     input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
335                                                        FIND_ANYWHERE );
336
337     if( [[o_mi title] isEqualToString: _NS("Info")] )
338     {
339         if( p_input == NULL )
340         {
341             bEnabled = FALSE;
342         }
343     }
344     if( p_input ) vlc_object_release( p_input );
345
346     return( bEnabled );
347 }
348
349 @end
350
351 @implementation VLCInfo (NSTableDataSource)
352
353 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
354 {
355     return (item == nil) ? [[VLCInfoTreeItem rootItem] numberOfChildren] : [item numberOfChildren];
356 }
357
358 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
359     return ([item numberOfChildren] > 0);
360 }
361
362 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
363 {
364     return (item == nil) ? [[VLCInfoTreeItem rootItem] childAtIndex:index] : [item childAtIndex:index];
365 }
366
367 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
368 {
369     if ([[tableColumn identifier] isEqualToString:@"0"])
370     {
371         return (item == nil) ? @"" : (id)[item getName];
372     }
373     else
374     {
375         return (item == nil) ? @"" : (id)[item getValue];
376     }
377 }
378
379
380 @end
381
382 @implementation VLCInfoTreeItem
383
384 static VLCInfoTreeItem *o_root_item = nil;
385
386 #define IsALeafNode ((id)-1)
387
388 - (id)initWithName: (NSString *)o_item_name value: (NSString *)o_item_value ID: (int)i_id parent:(VLCInfoTreeItem *)o_parent_item
389 {
390     self = [super init];
391
392     if( self != nil )
393     {
394         o_name = [o_item_name copy];
395         o_value = [o_item_value copy];
396         i_object_id = i_id;
397         o_parent = o_parent_item;
398         i_item = [[[VLCMain sharedInstance] getInfo] getItem];
399     }
400     return( self );
401 }
402
403 + (VLCInfoTreeItem *)rootItem {
404     if (o_root_item == nil) o_root_item = [[VLCInfoTreeItem alloc] initWithName:@"main" value: @"" ID: 0 parent:nil];
405     return o_root_item;
406 }
407
408 - (void)dealloc
409 {
410     if (o_children != IsALeafNode) [o_children release];
411     [o_name release];
412     [super dealloc];
413 }
414
415 /* Creates and returns the array of children
416  * Loads children incrementally */
417 - (NSArray *)children
418 {
419     if (o_children == NULL)
420     {
421         intf_thread_t * p_intf = VLCIntf;
422         playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
423                                           FIND_ANYWHERE );
424         int i;
425
426         if (p_playlist)
427         {
428             if (i_item > -1)
429             {
430                 if (self == o_root_item)
431                 {
432                     o_children = [[NSMutableArray alloc] initWithCapacity:p_playlist->pp_items[i_item]->input.i_categories];
433                     for (i = 0 ; i<p_playlist->pp_items[i_item]->input.i_categories ; i++)
434                     {
435                         [o_children addObject:[[VLCInfoTreeItem alloc]
436                             initWithName: [NSString stringWithUTF8String:
437                                 p_playlist->pp_items[i_item]->input.
438                                 pp_categories[i]->psz_name]
439                             value: @""
440                             ID: i
441                             parent: self]];
442                     }
443                 }
444                 else if (o_parent == o_root_item)
445                 {
446                     o_children = [[NSMutableArray alloc] initWithCapacity:
447                         p_playlist->pp_items[i_item]->input.
448                         pp_categories[i_object_id]->i_infos];
449                     for (i = 0 ; i<p_playlist->pp_items[i_item]->input.
450                            pp_categories[i_object_id]->i_infos ; i++)
451                     {
452                         [o_children addObject:[[VLCInfoTreeItem alloc]
453                         initWithName: [NSString stringWithUTF8String:
454                                 p_playlist->pp_items[i_item]->input.
455                                 pp_categories[i_object_id]->
456                                 pp_infos[i]->psz_name]
457                             value: [NSString stringWithUTF8String:
458                                 p_playlist->pp_items[i_item]->input.
459                                 pp_categories[i_object_id]->
460                                 pp_infos[i]->psz_value]
461                             ID: i
462                             parent: self]];
463                     }
464                 }
465                 else
466                 {
467                     o_children = IsALeafNode;
468                 }
469             }
470             vlc_object_release(p_playlist);
471         }
472     }
473     return o_children;
474 }
475
476 - (NSString *)getName
477 {
478     return o_name;
479 }
480
481 - (NSString *)getValue
482 {
483     return o_value;
484 }
485
486 - (VLCInfoTreeItem *)childAtIndex:(int)i_index {
487     return [[self children] objectAtIndex:i_index];
488 }
489
490 - (int)numberOfChildren {
491     id i_tmp = [self children];
492     return (i_tmp == IsALeafNode) ? (-1) : (int)[i_tmp count];
493 }
494
495 /*- (int)selectedPlaylistItem
496 {
497     return i_item;
498 }
499 */
500 - (void)refresh
501 {
502     i_item = [[[VLCMain sharedInstance] getInfo] getItem];
503     if (o_children != NULL)
504     {
505         [o_children release];
506         o_children = NULL;
507     }
508 }
509
510 @end
511