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