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