]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlist.m
Add "enable/disable all group items" functions in playlist context menu
[vlc] / modules / gui / macosx / playlist.m
1 /*****************************************************************************
2  * playlist.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2002-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *          Derk-Jan Hartman <hartman at videolan dot org>
9  *          Benjamin Pracht <bigben at videolab dot org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>                                      /* malloc(), free() */
30 #include <sys/param.h>                                    /* for MAXPATHLEN */
31 #include <string.h>
32 #include <math.h>
33 #include <sys/mount.h>
34 #include <vlc_keys.h>
35
36 #include "intf.h"
37 #include "playlist.h"
38 #include "controls.h"
39 #include <OSD.h>
40
41 /*****************************************************************************
42  * VLCPlaylistView implementation 
43  *****************************************************************************/
44 @implementation VLCPlaylistView
45
46 - (NSMenu *)menuForEvent:(NSEvent *)o_event
47 {
48     return( [[self delegate] menuForEvent: o_event] );
49 }
50
51 - (void)keyDown:(NSEvent *)o_event
52 {
53     unichar key = 0;
54     int i, c, i_row;
55     NSMutableArray *o_to_delete;
56     NSNumber *o_number;
57     
58     playlist_t * p_playlist;
59     intf_thread_t * p_intf = [NSApp getIntf];
60
61     if( [[o_event characters] length] )
62     {
63         key = [[o_event characters] characterAtIndex: 0];
64     }
65
66     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
67                                           FIND_ANYWHERE );
68     
69     if ( p_playlist == NULL )
70     {
71         return;
72     }
73     
74     switch( key )
75     {
76         case NSDeleteCharacter:
77         case NSDeleteFunctionKey:
78         case NSDeleteCharFunctionKey:
79         case NSBackspaceCharacter:
80             o_to_delete = [NSMutableArray arrayWithArray:[[self selectedRowEnumerator] allObjects]];
81             c = [o_to_delete count];
82             
83             for( i = 0; i < c; i++ ) {
84                 o_number = [o_to_delete lastObject];
85                 i_row = [o_number intValue];
86                 
87                 if( p_playlist->i_index == i_row && p_playlist->i_status )
88                 {
89                     playlist_Stop( p_playlist );
90                 }
91                 [o_to_delete removeObject: o_number];
92                 [self deselectRow: i_row];
93                 playlist_Delete( p_playlist, i_row );
94             }
95             [self reloadData];
96             break;
97             
98         default:
99             [super keyDown: o_event];
100             break;
101     }
102
103     if( p_playlist != NULL )
104     {
105         vlc_object_release( p_playlist );
106     }
107 }
108
109
110 @end
111
112 /*****************************************************************************
113  * VLCPlaylist implementation 
114  *****************************************************************************/
115 @implementation VLCPlaylist
116
117 - (id)init
118 {
119     self = [super init];
120     if ( self !=nil )
121     {
122         i_moveRow = -1;
123     }
124     return self;
125 }
126
127 - (void)awakeFromNib
128 {
129     [o_table_view setTarget: self];
130     [o_table_view setDelegate: self];
131     [o_table_view setDataSource: self];
132
133     [o_table_view setDoubleAction: @selector(playItem:)];
134
135     [o_table_view registerForDraggedTypes: 
136         [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
137     [o_table_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
138     [o_window setExcludedFromWindowsMenu: TRUE];
139
140
141 //    [o_tbv_info setDataSource: [VLCInfoDataSource init]];
142
143 /* We need to check whether _defaultTableHeaderSortImage exists, since it 
144 belongs to an Apple hidden private API, and then can "disapear" at any time*/
145
146     if( [[NSTableView class] respondsToSelector:@selector(_defaultTableHeaderSortImage)] )
147     {
148         o_ascendingSortingImage = [[NSTableView class] _defaultTableHeaderSortImage];
149     }
150     else
151     {
152         o_ascendingSortingImage = nil;
153     }
154
155     if( [[NSTableView class] respondsToSelector:@selector(_defaultTableHeaderReverseSortImage)] )
156     {
157         o_descendingSortingImage = [[NSTableView class] _defaultTableHeaderReverseSortImage];
158     }
159     else
160     {
161         o_descendingSortingImage = nil;
162     }
163
164     [self initStrings];
165     [self playlistUpdated];
166 }
167
168 - (void)initStrings
169 {
170     [o_window setTitle: _NS("Playlist")];
171     [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
172     [o_mi_play setTitle: _NS("Play")];
173     [o_mi_delete setTitle: _NS("Delete")];
174     [o_mi_selectall setTitle: _NS("Select All")];
175     [o_mi_toggleItemsEnabled setTitle: _NS("Item Enabled")];
176     [o_mi_enableGroup setTitle: _NS("Enable all group items")];
177     [o_mi_disableGroup setTitle: _NS("Disable all group items")];
178     [o_mi_info setTitle: _NS("Properties")];
179
180     [[o_tc_name headerCell] setStringValue:_NS("Name")];
181     [[o_tc_author headerCell] setStringValue:_NS("Author")];
182     [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
183     [o_random_ckb setTitle: _NS("Random")];
184     [o_search_button setTitle: _NS("Search")];
185     [o_btn_playlist setToolTip: _NS("Playlist")];
186     [[o_loop_popup itemAtIndex:0] setTitle: _NS("Standard Play")];
187     [[o_loop_popup itemAtIndex:1] setTitle: _NS("Repeat One")];
188     [[o_loop_popup itemAtIndex:2] setTitle: _NS("Repeat All")];
189 }
190
191 - (void) tableView:(NSTableView*)o_tv
192                   didClickTableColumn:(NSTableColumn *)o_tc
193 {
194     intf_thread_t * p_intf = [NSApp getIntf];
195     playlist_t *p_playlist =
196         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
197                                        FIND_ANYWHERE );
198
199     int max = [[o_table_view tableColumns] count];
200     int i;
201
202     if( p_playlist == NULL )
203     {
204         return;
205     }
206
207     if( o_tc_sortColumn == o_tc )
208     {
209         b_isSortDescending = !b_isSortDescending;
210     }
211     else if( o_tc == o_tc_name || o_tc == o_tc_author || 
212         o_tc == o_tc_id )
213     {
214         b_isSortDescending = VLC_FALSE;
215         [o_table_view setHighlightedTableColumn:o_tc];
216         o_tc_sortColumn = o_tc;
217         for( i=0 ; i<max ; i++ )
218         {
219             [o_table_view setIndicatorImage:nil inTableColumn:[[o_table_view tableColumns] objectAtIndex:i]];
220         }
221     }
222
223     if( o_tc_id == o_tc && !b_isSortDescending )
224     {    
225         playlist_SortID( p_playlist , ORDER_NORMAL );
226         [o_table_view setIndicatorImage:o_ascendingSortingImage inTableColumn:o_tc];    
227     }
228     else if( o_tc_name == o_tc && !b_isSortDescending )
229     {    
230         playlist_SortTitle( p_playlist , ORDER_NORMAL );
231         [o_table_view setIndicatorImage:o_ascendingSortingImage inTableColumn:o_tc];    
232     }
233     else if( o_tc_author == o_tc && !b_isSortDescending )
234     {
235         playlist_SortAuthor( p_playlist , ORDER_NORMAL );
236         [o_table_view setIndicatorImage:o_ascendingSortingImage inTableColumn:o_tc];
237     }
238     else if( o_tc_id == o_tc && b_isSortDescending )
239     {    
240         playlist_SortID( p_playlist , ORDER_REVERSE );
241         [o_table_view setIndicatorImage:o_ascendingSortingImage inTableColumn:o_tc];    
242     }
243     else if( o_tc_name == o_tc && b_isSortDescending )
244     {    
245         playlist_SortTitle( p_playlist , ORDER_REVERSE );
246         [o_table_view setIndicatorImage:o_descendingSortingImage inTableColumn:o_tc];
247     }
248     else if( o_tc_author == o_tc && b_isSortDescending )
249     {
250         playlist_SortAuthor( p_playlist , ORDER_REVERSE );
251         [o_table_view setIndicatorImage:o_descendingSortingImage inTableColumn:o_tc];
252     } 
253     vlc_object_release( p_playlist );
254     [self playlistUpdated];
255 }
256
257
258 - (BOOL)tableView:(NSTableView *)o_tv 
259                   shouldEditTableColumn:(NSTableColumn *)o_tc
260                   row:(int)i_row
261 {
262     return( NO );
263 }
264
265 - (NSMenu *)menuForEvent:(NSEvent *)o_event
266 {
267     intf_thread_t * p_intf = [NSApp getIntf];
268     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
269                                             FIND_ANYWHERE );
270
271     bool b_itemstate = FALSE;
272
273     NSPoint pt;
274     vlc_bool_t b_rows;
275     vlc_bool_t b_item_sel;
276
277     pt = [o_table_view convertPoint: [o_event locationInWindow] 
278                                                  fromView: nil];
279     b_item_sel = ( [o_table_view rowAtPoint: pt] != -1 &&
280                    [o_table_view selectedRow] != -1 );
281     b_rows = [o_table_view numberOfRows] != 0;
282
283     [o_mi_play setEnabled: b_item_sel];
284     [o_mi_delete setEnabled: b_item_sel];
285     [o_mi_selectall setEnabled: b_rows];
286     [o_mi_info setEnabled: b_item_sel];
287     [o_mi_toggleItemsEnabled setEnabled: b_item_sel];
288     [o_mi_enableGroup setEnabled: b_item_sel];
289     [o_mi_disableGroup setEnabled: b_item_sel];
290
291     if (p_playlist)
292     {
293         b_itemstate = ([o_table_view selectedRow] > -1) ?
294             p_playlist->pp_items[[o_table_view selectedRow]]->b_enabled : FALSE;
295         vlc_object_release(p_playlist);
296     }
297
298     [o_mi_toggleItemsEnabled setState: b_itemstate];
299
300     return( o_ctx_menu );
301 }
302
303 - (IBAction)toggleWindow:(id)sender
304 {
305     if( [o_window isVisible] )
306     {
307         [o_window orderOut:sender];
308         [o_btn_playlist setState:NSOffState];
309     }
310     else
311     {
312         [o_window makeKeyAndOrderFront:sender];
313         [o_btn_playlist setState:NSOnState];
314     }
315 }
316
317 - (IBAction)savePlaylist:(id)sender
318 {
319     intf_thread_t * p_intf = [NSApp getIntf];
320     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
321                                                        FIND_ANYWHERE );
322     
323     NSSavePanel *o_save_panel = [NSSavePanel savePanel];
324     NSString * o_name = [NSString stringWithFormat: @"%@.m3u", _NS("Untitled")];
325     [o_save_panel setTitle: _NS("Save Playlist")];
326     [o_save_panel setPrompt: _NS("Save")];
327
328     if( [o_save_panel runModalForDirectory: nil
329             file: o_name] == NSOKButton )
330     {
331         playlist_Export( p_playlist, [[o_save_panel filename] fileSystemRepresentation], "export-m3u" );
332     }
333
334 }
335
336 - (IBAction)playItem:(id)sender
337 {
338     intf_thread_t * p_intf = [NSApp getIntf];
339     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
340                                                        FIND_ANYWHERE );
341
342     if( p_playlist != NULL )
343     {
344         playlist_Goto( p_playlist, [o_table_view selectedRow] );
345         vlc_object_release( p_playlist );
346     }
347 }
348
349 - (IBAction)deleteItems:(id)sender
350 {
351     int i, c, i_row;
352     NSMutableArray *o_to_delete;
353     NSNumber *o_number;
354
355     intf_thread_t * p_intf = [NSApp getIntf];
356     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
357                                                        FIND_ANYWHERE );
358
359     if( p_playlist == NULL )
360     {
361         return;
362     }
363     
364     o_to_delete = [NSMutableArray arrayWithArray:[[o_table_view selectedRowEnumerator] allObjects]];
365     c = (int)[o_to_delete count];
366     
367     for( i = 0; i < c; i++ ) {
368         o_number = [o_to_delete lastObject];
369         i_row = [o_number intValue];
370         
371         if( p_playlist->i_index == i_row && p_playlist->i_status )
372         {
373             playlist_Stop( p_playlist );
374         }
375         [o_to_delete removeObject: o_number];
376         [o_table_view deselectRow: i_row];
377         playlist_Delete( p_playlist, i_row );
378     }
379
380     vlc_object_release( p_playlist );
381
382     /* this is actually duplicity, because the intf.m manage also updates the view
383      * when the playlist changes. we do this on purpose, because else there is a 
384      * delay of .5 sec or so when we delete an item */
385     [self playlistUpdated];
386     [self updateRowSelection];
387 }
388
389 - (IBAction)toggleItemsEnabled:(id)sender
390 {
391     int i, c, i_row;
392     NSMutableArray *o_selected;
393     NSNumber *o_number;
394
395     intf_thread_t * p_intf = [NSApp getIntf];
396     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
397                                                        FIND_ANYWHERE );
398
399     if( p_playlist == NULL )
400     {
401         return;
402     }
403
404     o_selected = [NSMutableArray arrayWithArray:[[o_table_view selectedRowEnumerator] allObjects]];
405     c = (int)[o_selected count];
406
407     if (p_playlist->pp_items[[o_table_view selectedRow]]->b_enabled)
408     {
409         for( i = 0; i < c; i++ )
410         {
411             o_number = [o_selected lastObject];
412             i_row = [o_number intValue];
413             if( p_playlist->i_index == i_row && p_playlist->i_status )
414             {
415                 playlist_Stop( p_playlist );
416             }
417             [o_selected removeObject: o_number];
418             playlist_Disable( p_playlist, i_row );
419         }
420     }
421     else
422     {
423         for( i = 0; i < c; i++ )
424         {
425             o_number = [o_selected lastObject];
426             i_row = [o_number intValue];
427             [o_selected removeObject: o_number];
428             playlist_Enable( p_playlist, i_row );
429         }
430     }
431     vlc_object_release( p_playlist );
432     [self playlistUpdated];
433 }
434
435 - (IBAction)enableGroup:(id)sender
436 {
437     intf_thread_t * p_intf = [NSApp getIntf];
438     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
439                                                        FIND_ANYWHERE );
440
441     if (p_playlist)
442     {
443         playlist_EnableGroup(p_playlist,
444                 p_playlist->pp_items[[o_table_view selectedRow]]->i_group);
445         vlc_object_release(p_playlist);
446     }
447 }
448
449 - (IBAction)disableGroup:(id)sender 
450 {
451     intf_thread_t * p_intf = [NSApp getIntf];
452     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
453                                                        FIND_ANYWHERE );
454
455     if (p_playlist)
456     {
457         playlist_DisableGroup(p_playlist,
458                 p_playlist->pp_items[[o_table_view selectedRow]]->i_group);
459         vlc_object_release(p_playlist);
460     }
461 }
462
463 - (IBAction)selectAll:(id)sender
464 {
465     [o_table_view selectAll: nil];
466 }
467
468
469 - (IBAction)searchItem:(id)sender
470 {
471     int i_current = -1;
472     NSString *o_current_name;
473     NSString *o_current_author;
474
475     intf_thread_t * p_intf = [NSApp getIntf];
476     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
477                                                FIND_ANYWHERE );
478     
479     if( p_playlist == NULL )
480     {
481         return;
482     }
483     if( [o_table_view numberOfRows] < 1 )
484     {
485         return;
486     }
487
488     if( [o_table_view selectedRow] == [o_table_view numberOfRows]-1 )
489     {
490         i_current = -1;
491     }
492     else
493     {
494         i_current = [o_table_view selectedRow]; 
495     }
496
497     do
498     {
499         i_current++;
500
501         vlc_mutex_lock( &p_playlist->object_lock );
502         o_current_name = [NSString stringWithUTF8String: 
503             p_playlist->pp_items[i_current]->input.psz_name];
504         o_current_author = [NSString stringWithUTF8String: 
505             playlist_GetInfo(p_playlist, i_current ,_("General"),_("Author") )];
506         vlc_mutex_unlock( &p_playlist->object_lock );
507
508
509         if( [o_current_name rangeOfString:[o_search_keyword stringValue] options:NSCaseInsensitiveSearch ].length ||
510              [o_current_author rangeOfString:[o_search_keyword stringValue] options:NSCaseInsensitiveSearch ].length )
511         {
512              [o_table_view selectRow: i_current byExtendingSelection: NO];
513              [o_table_view scrollRowToVisible: i_current];
514              break;
515         }
516         if( i_current == [o_table_view numberOfRows] - 1 )
517         {
518              i_current = -1;
519         }
520     }
521     while (i_current != [o_table_view selectedRow]);
522     vlc_object_release( p_playlist );
523 }
524
525
526 - (IBAction)handlePopUp:(id)sender
527
528 {
529              intf_thread_t * p_intf = [NSApp getIntf];
530              vlc_value_t val1,val2;
531              playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
532                                                         FIND_ANYWHERE );
533              if( p_playlist == NULL )
534              {
535                  return;
536              }
537
538     switch ([o_loop_popup indexOfSelectedItem])
539     {
540         case 1:
541
542              val1.b_bool = 0;
543              var_Set( p_playlist, "loop", val1 );
544              val1.b_bool = 1;
545              var_Set( p_playlist, "repeat", val1 );
546              vout_OSDMessage( p_intf, _( "Repeat One" ) );
547         break;
548
549         case 2:
550              val1.b_bool = 0;
551              var_Set( p_playlist, "repeat", val1 );
552              val1.b_bool = 1;
553              var_Set( p_playlist, "loop", val1 );
554              vout_OSDMessage( p_intf, _( "Repeat All" ) );
555         break;
556
557         default:
558              var_Get( p_playlist, "repeat", &val1 );
559              var_Get( p_playlist, "loop", &val2 );
560              if (val1.b_bool || val2.b_bool)
561              {
562                   val1.b_bool = 0;
563                   var_Set( p_playlist, "repeat", val1 );
564                   var_Set( p_playlist, "loop", val1 );
565                   vout_OSDMessage( p_intf, _( "Repeat Off" ) );
566              }
567          break;
568      }
569      vlc_object_release( p_playlist );
570      [self playlistUpdated];
571 }
572
573
574 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
575 {
576     int i_item;
577     intf_thread_t * p_intf = [NSApp getIntf];
578     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
579                                                        FIND_ANYWHERE );
580
581     if( p_playlist == NULL )
582     {
583         return;
584     }
585
586     for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
587     {
588         /* One item */
589         NSDictionary *o_one_item;
590         int j, i_total_options = 0, i_new_id = -1;
591         int i_mode = PLAYLIST_INSERT;
592         BOOL b_rem = FALSE, b_dir = FALSE;
593         NSString *o_uri, *o_name;
594         NSArray *o_options;
595         NSURL *o_true_file;
596         char **ppsz_options = NULL;
597     
598         /* Get the item */
599         o_one_item = [o_array objectAtIndex: i_item];
600         o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
601         o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
602         o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
603         
604         /* If no name, then make a guess */
605         if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
606     
607         if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
608             [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
609                     isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem   )
610         {
611             /* All of this is to make sure CD's play when you D&D them on VLC */
612             /* Converts mountpoint to a /dev file */
613             struct statfs *buf;
614             char *psz_dev;
615             buf = (struct statfs *) malloc (sizeof(struct statfs));
616             statfs( [o_uri fileSystemRepresentation], buf );
617             psz_dev = strdup(buf->f_mntfromname);
618             o_uri = [NSString stringWithCString: psz_dev ];
619         }
620
621         if( o_options && [o_options count] > 0 )
622         {
623             /* Count the input options */
624             i_total_options = [o_options count];
625     
626             /* Allocate ppsz_options */
627             for( j = 0; j < i_total_options; j++ )
628             {
629                 if( !ppsz_options )
630                     ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
631     
632                 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
633             }
634         }
635
636         /* Add the item */
637         i_new_id = playlist_AddExt( p_playlist, [o_uri fileSystemRepresentation], 
638                       [o_name UTF8String], i_mode, 
639                       i_position == -1 ? PLAYLIST_END : i_position + i_item,
640                       0, (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options );
641
642         /* clean up 
643         for( j = 0; j < i_total_options; j++ )
644             free( ppsz_options[j] );
645         if( ppsz_options ) free( ppsz_options ); */
646
647         /* Recent documents menu */
648         o_true_file = [NSURL fileURLWithPath: o_uri];
649         if( o_true_file != nil )
650         { 
651             [[NSDocumentController sharedDocumentController]
652                 noteNewRecentDocumentURL: o_true_file]; 
653         }
654         
655         if( i_item == 0 && !b_enqueue )
656         {
657             playlist_Goto( p_playlist, playlist_GetPositionById( p_playlist, i_new_id ) );
658             playlist_Play( p_playlist );
659         }
660     }
661
662     vlc_object_release( p_playlist );
663 }
664
665 - (void)playlistUpdated
666 {
667     vlc_value_t val1, val2;
668     intf_thread_t * p_intf = [NSApp getIntf];
669     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
670                                                        FIND_ANYWHERE );
671     if( p_playlist != NULL )
672     {
673         var_Get( p_playlist, "random", &val1 );
674         [o_random_ckb setState: val1.b_bool];
675
676         var_Get( p_playlist, "repeat", &val1 );
677         var_Get( p_playlist, "loop", &val2 );
678         if(val1.b_bool)
679         {
680             [o_loop_popup selectItemAtIndex:1];
681         }
682         else if(val2.b_bool)
683         {
684             [o_loop_popup selectItemAtIndex:2];
685         }
686         else
687         {
688             [o_loop_popup selectItemAtIndex:0];
689         }
690         vlc_object_release( p_playlist );
691     }
692     [o_table_view reloadData];
693 }
694
695 - (void)updateRowSelection
696 {
697     int i_row;
698
699     intf_thread_t * p_intf = [NSApp getIntf];
700     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
701                                                        FIND_ANYWHERE );
702
703     if( p_playlist == NULL )
704     {
705         return;
706     }
707
708     i_row = p_playlist->i_index;
709     vlc_object_release( p_playlist );
710
711     [o_table_view selectRow: i_row byExtendingSelection: NO];
712     [o_table_view scrollRowToVisible: i_row];
713 }
714
715 - (int)selectedPlaylistItem
716 {
717     return [o_table_view selectedRow];
718 }
719
720 - (NSColor *)getColor:(int)i_group
721 {
722     NSColor * o_color = nil;
723     switch ( i_group % 8 )
724     {
725         case 1:
726             /*white*/
727             o_color = [NSColor colorWithDeviceRed:1.0 green:1.0 blue:1.0 alpha:1.0];
728         break;
729
730         case 2:
731             /*red*/
732            o_color = [NSColor colorWithDeviceRed:1.0 green:0.76471 blue:0.76471 alpha:1.0];
733         break;
734
735             case 3:
736               /*dark blue*/
737                    o_color = [NSColor colorWithDeviceRed:0.76471 green:0.76471 blue:1.0 alpha:1.0];
738         break; 
739
740         case 4:
741                /*orange*/
742            o_color = [NSColor colorWithDeviceRed:1.0 green:0.89804 blue:0.76471 alpha:1.0];
743         break;
744
745         case 5:
746                /*purple*/
747            o_color = [NSColor colorWithDeviceRed:1.0 green:0.76471 blue:1.0 alpha:1.0];
748         break;
749  
750         case 6:
751               /*green*/
752            o_color = [NSColor colorWithDeviceRed:0.76471 green:1.0 blue:0.76471 alpha:1.0];
753         break; 
754
755         case 7:
756               /*light blue*/
757            o_color = [NSColor colorWithDeviceRed:0.76471 green:1.0 blue:1.0 alpha:1.0];
758         break;
759
760         case 0:
761               /*yellow*/
762            o_color = [NSColor colorWithDeviceRed:1.0 green:1.0 blue:0.76471 alpha:1.0];
763         break;
764         }
765     return o_color;
766 }
767
768 @end
769
770 @implementation VLCPlaylist (NSTableDataSource)
771
772 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
773 {
774     int i_count = 0;
775     intf_thread_t * p_intf = [NSApp getIntf];
776     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
777                                                        FIND_ANYWHERE );
778
779     if( p_playlist != NULL )
780     {
781         vlc_mutex_lock( &p_playlist->object_lock );
782         i_count = p_playlist->i_size;
783         vlc_mutex_unlock( &p_playlist->object_lock );
784         vlc_object_release( p_playlist );
785     }
786     [o_status_field setStringValue: [NSString stringWithFormat:_NS("%i items in playlist"), i_count]];
787     return( i_count );
788 }
789
790 - (id)tableView:(NSTableView *)o_tv 
791                 objectValueForTableColumn:(NSTableColumn *)o_tc 
792                 row:(int)i_row
793 {
794     id o_value = nil;
795     intf_thread_t * p_intf = [NSApp getIntf];
796     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
797                                                FIND_ANYWHERE );
798
799     if( p_playlist == NULL )
800     {
801         return( nil );
802     }
803
804     if( [[o_tc identifier] isEqualToString:@"0"] )
805     {
806         o_value = [NSString stringWithFormat:@"%i", i_row + 1];
807     }
808     else if( [[o_tc identifier] isEqualToString:@"1"] )
809     {
810         vlc_mutex_lock( &p_playlist->object_lock );
811         o_value = [NSString stringWithUTF8String: 
812             p_playlist->pp_items[i_row]->input.psz_name];
813         if( o_value == NULL )
814             o_value = [NSString stringWithCString: 
815                 p_playlist->pp_items[i_row]->input.psz_name];
816         vlc_mutex_unlock( &p_playlist->object_lock );
817     }
818     else if( [[o_tc identifier] isEqualToString:@"2"] )
819     {
820         vlc_mutex_lock( &p_playlist->object_lock );
821         o_value = [NSString stringWithUTF8String: 
822             playlist_GetInfo(p_playlist, i_row ,_("General"),_("Author") )];
823         if( o_value == NULL )
824             o_value = [NSString stringWithCString: 
825                 playlist_GetInfo(p_playlist, i_row ,_("General"),_("Author") )];
826         vlc_mutex_unlock( &p_playlist->object_lock );
827     }
828     else if( [[o_tc identifier] isEqualToString:@"3"] )
829     {
830         char psz_duration[MSTRTIME_MAX_SIZE];
831         mtime_t dur = p_playlist->pp_items[i_row]->input.i_duration;
832         if( dur != -1 )
833         {
834             secstotimestr( psz_duration, dur/1000000 );
835             o_value = [NSString stringWithUTF8String: psz_duration];
836         }
837         else
838         {
839             o_value = @"-:--:--";
840         }
841     }
842
843     vlc_object_release( p_playlist );
844
845     return( o_value );
846 }
847
848 - (void)tableView:(NSTableView *)o_tv
849                     willDisplayCell:(id)o_cell
850                     forTableColumn:(NSTableColumn *)o_tc
851                     row:(int)i_rows
852 {
853     intf_thread_t * p_intf = [NSApp getIntf];
854     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
855                                                FIND_ANYWHERE );
856     if (p_playlist)
857     {
858         if ((p_playlist->i_groups) > 1 )
859         {
860             [o_cell setDrawsBackground: VLC_TRUE];
861             [o_cell setBackgroundColor:
862                 [self getColor:p_playlist->pp_items[i_rows]->i_group]];
863        
864         }
865
866         if (!p_playlist->pp_items[i_rows]->b_enabled)
867         {
868             [o_cell setTextColor: [NSColor colorWithDeviceRed:0.3686 green:0.3686 blue:0.3686 alpha:1.0]];
869         }
870         else
871         {
872             [o_cell setTextColor:[NSColor colorWithDeviceRed:0.0 green:0.0 blue:0.0 alpha:1.0]];
873         }
874     vlc_object_release( p_playlist );
875     }
876 }
877
878 - (BOOL)tableView:(NSTableView *)o_tv
879                     writeRows:(NSArray*)o_rows
880                     toPasteboard:(NSPasteboard*)o_pasteboard 
881 {
882     int i_rows = [o_rows count];
883     NSArray *o_filenames = [NSArray array];
884     
885     [o_pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
886     [o_pasteboard setPropertyList:o_filenames forType:NSFilenamesPboardType];
887     if ( i_rows == 1 )
888     {
889         i_moveRow = [[o_rows objectAtIndex:0]intValue];
890         return YES;
891     }
892     return NO;
893 }
894
895 - (NSDragOperation)tableView:(NSTableView*)o_tv
896                     validateDrop:(id <NSDraggingInfo>)o_info
897                     proposedRow:(int)i_row
898                     proposedDropOperation:(NSTableViewDropOperation)o_operation 
899 {
900     if ( o_operation == NSTableViewDropAbove )
901     {
902         if ( i_moveRow >= 0 )
903         {
904             if ( i_row != i_moveRow )
905             {
906                 return NSDragOperationMove;
907             }
908             /* what if in the previous run, the row wasn't actually moved? 
909                then we can't drop new files on this location */
910             return NSDragOperationNone;
911         }
912         return NSDragOperationGeneric;
913     }
914     return NSDragOperationNone;
915 }
916
917 - (BOOL)tableView:(NSTableView*)o_tv
918                     acceptDrop:(id <NSDraggingInfo>)o_info
919                     row:(int)i_proposed_row
920                     dropOperation:(NSTableViewDropOperation)o_operation 
921 {
922     if (  i_moveRow >= 0 )
923     {
924         if (i_moveRow != -1 && i_proposed_row != -1)
925         {
926             intf_thread_t * p_intf = [NSApp getIntf];
927             playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
928                                                             FIND_ANYWHERE );
929         
930             if( p_playlist == NULL )
931             {
932                 i_moveRow = -1;
933                 return NO;
934             }
935     
936             playlist_Move( p_playlist, i_moveRow, i_proposed_row ); 
937         
938             vlc_object_release( p_playlist );
939         }
940         [self playlistUpdated];
941         i_moveRow = -1;
942         return YES;
943     }
944     else
945     {
946         NSPasteboard * o_pasteboard;
947         o_pasteboard = [o_info draggingPasteboard];
948         
949         if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
950         {
951             int i;
952             NSArray *o_array = [NSArray array];
953             NSArray *o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
954                         sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
955
956             for( i = 0; i < (int)[o_values count]; i++)
957             {
958                 NSDictionary *o_dic;
959                 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
960                 o_array = [o_array arrayByAddingObject: o_dic];
961             }
962             [self appendArray: o_array atPos: i_proposed_row enqueue:YES];
963             return YES;
964         }
965         return NO;
966     }
967     [self updateRowSelection];
968 }
969
970 /* Delegate method of NSWindow */
971 - (void)windowWillClose:(NSNotification *)aNotification
972 {
973     [o_btn_playlist setState: NSOffState];
974 }
975
976 @end
977
978