]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlistinfo.m
*all :
[vlc] / modules / gui / macosx / playlistinfo.m
1 /*****************************************************************************
2  r playlistinfo.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2002-2004 VideoLAN
5  * $Id: playlistinfo.m 7015 2004-03-08 15:22:58Z bigben $
6  *
7  * Authors: Benjmaib 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 VLCPlaylistInfo
37
38 - (void)awakeFromNib
39 {
40     [o_info_window setExcludedFromWindowsMenu: TRUE];
41
42     [o_info_window setTitle: _NS("Properties")];
43     [o_uri_lbl setStringValue: _NS("URI")];
44     [o_title_lbl setStringValue: _NS("Title")];
45     [o_author_lbl setStringValue: _NS("Author")];
46     [o_btn_info_ok setTitle: _NS("OK")];
47     [o_btn_info_cancel setTitle: _NS("Cancel")];
48     [o_btn_delete_group setTitle: _NS("Delete Group")];
49     [o_btn_add_group setTitle: _NS("Add Group")];
50     [o_group_lbl setStringValue: _NS("Group")];
51 }
52
53 - (IBAction)togglePlaylistInfoPanel:(id)sender
54 {
55     intf_thread_t * p_intf = [NSApp getIntf];
56     playlist_t * p_playlist;
57
58     if( [o_info_window isVisible] )
59     {
60         [o_info_window orderOut: sender];
61     }
62     else
63     {
64         p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
65                                           FIND_ANYWHERE );
66
67         if (p_playlist)
68         {
69             /*fill uri / title / author info */
70             int i_item = [o_vlc_playlist selectedPlaylistItem];
71             [o_uri_txt setStringValue:
72                 ([NSString stringWithUTF8String:p_playlist->
73                     pp_items[i_item]->input.psz_uri] == nil ) ?
74                 [NSString stringWithCString:p_playlist->
75                     pp_items[i_item]->input.psz_uri] :
76                 [NSString stringWithUTF8String:p_playlist->
77                     pp_items[i_item]->input.psz_uri]];
78
79             [o_title_txt setStringValue:
80                 ([NSString stringWithUTF8String:p_playlist->
81                     pp_items[i_item]->input.psz_name] == nil ) ?
82                 [NSString stringWithCString:p_playlist->
83                     pp_items[i_item]->input.psz_name] :
84                 [NSString stringWithUTF8String:p_playlist->
85                     pp_items[i_item]->input.psz_name]];
86
87             [o_author_txt setStringValue:
88                 [NSString stringWithUTF8String:playlist_GetInfo
89                 (p_playlist, i_item ,_("General"),_("Author") )]];
90
91             [[VLCInfoTreeItem rootItem] refresh];
92             [o_outline_view reloadData];
93
94             [self createComboBox];
95             [self handleGroup:self];
96
97             vlc_object_release( p_playlist );
98         }
99         [o_info_window makeKeyAndOrderFront: sender];
100     }
101 }
102
103 - (IBAction)infoCancel:(id)sender
104 {
105     [self togglePlaylistInfoPanel:self];
106 }
107
108
109 - (IBAction)infoOk:(id)sender
110 {
111     int i,i_row,c;
112     int i_item = [o_vlc_playlist selectedPlaylistItem];
113     intf_thread_t * p_intf = [NSApp getIntf];
114     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
115                                           FIND_ANYWHERE );
116     vlc_value_t val;
117     NSMutableArray * o_selected = [o_vlc_playlist selectedPlaylistItemsList];
118     NSNumber * o_number;
119
120
121     if (p_playlist)
122     {
123         vlc_mutex_lock(&p_playlist->pp_items[i_item]->input.lock);
124
125         p_playlist->pp_items[i_item]->input.psz_uri =
126             strdup([[o_uri_txt stringValue] cString]);
127         p_playlist->pp_items[i_item]->input.psz_name =
128             strdup([[o_title_txt stringValue] cString]);
129         playlist_ItemAddInfo(p_playlist->pp_items[i_item],_("General"),_("Author"), [[o_author_txt stringValue] cString]);
130
131         c = (int)[o_selected count];
132
133         if ([[o_group_cbx stringValue] isEqual:
134                     [o_group_cbx objectValueOfSelectedItem]])
135         {
136             for (i = 0 ; i < c ; i++)
137             {
138                 o_number = [o_selected lastObject];
139                 i_row = [o_number intValue];
140                 p_playlist->pp_items[i_row]->i_group = p_playlist->
141                     pp_groups[[o_group_cbx indexOfSelectedItem]]->i_id;
142                 [o_selected removeObject: o_number];
143             }
144         }
145         else
146         {
147             playlist_group_t * p_group = playlist_CreateGroup( p_playlist,
148                 strdup([[o_group_cbx stringValue] cString]));
149             if (p_group)
150             {
151                 for (i = 0 ; i < c ; i++)
152                 {
153                     o_number = [o_selected lastObject];
154                     i_row = [o_number intValue];
155                     p_playlist->pp_items[i_row]->i_group = p_group->i_id;
156                     [o_selected removeObject: o_number];
157                 }
158             }
159         }
160
161
162         vlc_mutex_unlock(&p_playlist->pp_items[i_item]->input.lock);
163         val.b_bool = VLC_TRUE;
164         var_Set( p_playlist,"intf-change",val );
165         vlc_object_release ( p_playlist );
166     }
167     [self togglePlaylistInfoPanel:self];
168 }
169
170 - (IBAction)handleGroup:(id)sender
171 {
172     intf_thread_t * p_intf = [NSApp getIntf];
173     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
174                                           FIND_ANYWHERE );
175
176     if (p_playlist)
177     {
178         if ([[o_group_cbx stringValue] isEqual: 
179                     [o_group_cbx objectValueOfSelectedItem]])
180         {
181             [o_group_color setBackgroundColor:[o_vlc_playlist 
182                 getColor: p_playlist->pp_groups[
183                 [o_group_cbx indexOfSelectedItem]]->i_id]];
184         }
185         else
186         {
187             [o_group_color setBackgroundColor:[o_vlc_playlist 
188                 getColor:p_playlist->pp_groups[
189                 [o_group_cbx numberOfItems] - 1]->i_id + 1]];
190         }
191     vlc_object_release(p_playlist);
192     }
193 }
194
195 - (IBAction)deleteOutlineGroup:(id)sender
196 {
197     intf_thread_t * p_intf = [NSApp getIntf];
198     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
199                                           FIND_ANYWHERE );
200
201     if(p_playlist)
202     {
203         if ([[o_group_cbx stringValue] isEqual:
204                     [o_group_cbx objectValueOfSelectedItem]])
205         {
206             [o_vlc_playlist deleteGroup:p_playlist->pp_groups[
207                     [o_group_cbx indexOfSelectedItem]]->i_id];
208             [self createComboBox];
209             [self handleGroup:self];
210             [o_group_cbx reloadData];
211         }
212         else
213         {
214             msg_Warn(p_playlist,"Group doesn't exist, cannot delete");
215         } 
216     vlc_object_release(p_playlist);
217     }
218 }
219
220 - (IBAction)createOutlineGroup:(id)sender; 
221 {
222     intf_thread_t * p_intf = [NSApp getIntf];
223     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
224                                           FIND_ANYWHERE );
225     if(p_playlist)
226     {
227         playlist_CreateGroup( p_playlist,
228                     strdup([[o_group_cbx stringValue] cString]));
229         [self createComboBox];
230         [o_group_cbx reloadData];
231         [o_vlc_playlist playlistUpdated];
232         vlc_object_release(p_playlist);
233     }
234 }
235
236 -(void)createComboBox
237 {
238     intf_thread_t * p_intf = [NSApp getIntf];
239     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
240                                           FIND_ANYWHERE );
241     int i;
242     int i_item = [o_vlc_playlist selectedPlaylistItem];
243
244     [o_group_cbx removeAllItems];
245
246     if (p_playlist)
247     {
248         for (i = 0; i < p_playlist->i_groups ; i++)
249         {
250             [o_group_cbx addItemWithObjectValue:
251                 [NSString stringWithUTF8String:
252                 p_playlist->pp_groups[i]->psz_name]];
253             if (p_playlist->pp_items[i_item]->i_group == p_playlist
254                 ->pp_groups[i]->i_id)
255             {
256                 [o_group_cbx selectItemAtIndex:i];
257             }
258         }
259     vlc_object_release(p_playlist);
260     }
261 }      
262
263 @end
264
265
266 @implementation VLCPlaylistInfo (NSTableDataSource)
267
268 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item  
269 {
270     return (item == nil) ? [[VLCInfoTreeItem rootItem] numberOfChildren] : [item numberOfChildren];
271 }
272
273 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
274     return ([item numberOfChildren] > 0);
275 }
276
277 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item  
278 {
279     return (item == nil) ? [[VLCInfoTreeItem rootItem] childAtIndex:index] : [item childAtIndex:index];
280 }
281
282 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
283 {
284     if ([[tableColumn identifier] isEqualToString:@"0"])
285     {
286         return (item == nil) ? @"" : (id)[item getName];
287     }
288     else
289     {
290         return (item == nil) ? @"" : (id)[item getValue];
291     }
292 }
293
294 @end
295
296 @implementation VLCInfoTreeItem
297
298 static VLCInfoTreeItem *o_root_item = nil;
299
300 #define IsALeafNode ((id)-1)
301
302 - (id)initWithName: (NSString *)o_item_name value: (NSString *)o_item_value ID: (int)i_id parent:(VLCInfoTreeItem *)o_parent_item
303 {
304     self = [super init];
305  
306     if( self != nil )
307     {
308
309         i_item = [[[NSApp delegate] getPlaylist] selectedPlaylistItem];
310         o_name = [o_item_name copy];
311         o_value = [o_item_value copy];
312         i_object_id = i_id;
313         o_parent = o_parent_item;
314     }
315     return( self );
316 }
317
318 + (VLCInfoTreeItem *)rootItem {
319     if (o_root_item == nil) o_root_item = [[VLCInfoTreeItem alloc] initWithName:@"main" value: @"" ID: 0 parent:nil];
320     return o_root_item;
321 }
322
323 - (void)dealloc
324 {
325     if (o_children != IsALeafNode) [o_children release];
326     [o_name release];
327     [super dealloc];
328 }
329
330 /* Creates and returns the array of children
331  * Loads children incrementally */
332 - (NSArray *)children
333 {
334     if (o_children == NULL)
335     {
336         intf_thread_t * p_intf = [NSApp getIntf];
337         playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
338                                           FIND_ANYWHERE );
339         int i;
340
341         if (p_playlist)
342         {
343             if (i_item > -1)
344             {
345                 if (self == o_root_item)
346                 {
347                     o_children = [[NSMutableArray alloc] initWithCapacity:p_playlist->pp_items[i_item]->input.i_categories];
348                     for (i = 0 ; i<p_playlist->pp_items[i_item]->input.i_categories ; i++)
349                     {
350                         [o_children addObject:[[VLCInfoTreeItem alloc] 
351                             initWithName: [NSString stringWithUTF8String:
352                                 p_playlist->pp_items[i_item]->input.
353                                 pp_categories[i]->psz_name]
354                             value: @""
355                             ID: i 
356                             parent: self]];
357                     }
358                 }
359                 else if (o_parent == o_root_item)
360                 {
361                     o_children = [[NSMutableArray alloc] initWithCapacity:
362                         p_playlist->pp_items[i_item]->input.
363                         pp_categories[i_object_id]->i_infos];
364                     for (i = 0 ; i<p_playlist->pp_items[i_item]->input.
365                            pp_categories[i_object_id]->i_infos ; i++)
366                     {
367                         [o_children addObject:[[VLCInfoTreeItem alloc]
368                         initWithName: [NSString stringWithUTF8String:
369                                 p_playlist->pp_items[i_item]->input.
370                                 pp_categories[i_object_id]->
371                                 pp_infos[i]->psz_name]
372                             value: [NSString stringWithUTF8String:
373                                 p_playlist->pp_items[i_item]->input.
374                                 pp_categories[i_object_id]->
375                                 pp_infos[i]->psz_value]
376                             ID: i
377                             parent: self]];
378                     }
379                 }
380                 else
381                 {
382                     o_children = IsALeafNode;
383                 }
384             }
385             vlc_object_release(p_playlist);
386         }
387     }
388     return o_children;
389 }
390
391 - (NSString *)getName
392 {
393     return o_name;
394 }
395
396 - (NSString *)getValue
397 {
398     return o_value;
399 }
400
401 - (VLCInfoTreeItem *)childAtIndex:(int)i_index {
402     return [[self children] objectAtIndex:i_index];
403 }
404
405 - (int)numberOfChildren {
406     id i_tmp = [self children];
407     return (i_tmp == IsALeafNode) ? (-1) : (int)[i_tmp count];
408 }
409
410 - (int)selectedPlaylistItem
411 {
412     return i_item; 
413 }
414
415 - (void)refresh
416 {
417     if (o_children != NULL)
418     {
419         [o_children release];
420         o_children = NULL;
421     }
422     i_item = [[[NSApp delegate] getPlaylist] selectedPlaylistItem];
423 }
424
425 @end
426