]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlist.m
* fixing the OSX playlist (at least it compiles again)
[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 = VLCIntf;
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->status.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 = VLCIntf;
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 = VLCIntf;
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 = VLCIntf;
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 = VLCIntf;
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 = VLCIntf;
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->status.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 #if 0
392     int i, c, i_row;
393     NSMutableArray *o_selected;
394     NSNumber *o_number;
395
396     intf_thread_t * p_intf = VLCIntf;
397     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
398                                                        FIND_ANYWHERE );
399
400     if( p_playlist == NULL )
401     {
402         return;
403     }
404
405     o_selected = [NSMutableArray arrayWithArray:[[o_table_view selectedRowEnumerator] allObjects]];
406     c = (int)[o_selected count];
407
408     if (p_playlist->pp_items[[o_table_view selectedRow]]->b_enabled)
409     {
410         for( i = 0; i < c; i++ )
411         {
412             o_number = [o_selected lastObject];
413             i_row = [o_number intValue];
414             if( p_playlist->i_index == i_row && p_playlist->status.i_status )
415             {
416                 playlist_Stop( p_playlist );
417             }
418             [o_selected removeObject: o_number];
419             playlist_Disable( p_playlist, i_row );
420         }
421     }
422     else
423     {
424         for( i = 0; i < c; i++ )
425         {
426             o_number = [o_selected lastObject];
427             i_row = [o_number intValue];
428             [o_selected removeObject: o_number];
429             playlist_Enable( p_playlist, i_row );
430         }
431     }
432     vlc_object_release( p_playlist );
433     [self playlistUpdated];
434 #endif
435 }
436
437 - (IBAction)enableGroup:(id)sender
438 {
439 #if 0
440     intf_thread_t * p_intf = VLCIntf;
441     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
442                                                        FIND_ANYWHERE );
443
444     if (p_playlist)
445     {
446         playlist_EnableGroup(p_playlist,
447                 p_playlist->pp_items[[o_table_view selectedRow]]->i_group);
448         vlc_object_release(p_playlist);
449     }
450 #endif
451 }
452
453 - (IBAction)disableGroup:(id)sender
454 {
455 #if 0
456     intf_thread_t * p_intf = VLCIntf;
457     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
458                                                        FIND_ANYWHERE );
459
460     if (p_playlist)
461     {
462         playlist_DisableGroup(p_playlist,
463                 p_playlist->pp_items[[o_table_view selectedRow]]->i_group);
464         vlc_object_release(p_playlist);
465     }
466 #endif
467 }
468
469 - (IBAction)selectAll:(id)sender
470 {
471     [o_table_view selectAll: nil];
472 }
473
474
475 - (IBAction)searchItem:(id)sender
476 {
477     int i_current = -1;
478     NSString *o_current_name;
479     NSString *o_current_author;
480
481     intf_thread_t * p_intf = VLCIntf;
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     if( [o_table_view numberOfRows] < 1 )
490     {
491         return;
492     }
493
494     if( [o_table_view selectedRow] == [o_table_view numberOfRows]-1 )
495     {
496         i_current = -1;
497     }
498     else
499     {
500         i_current = [o_table_view selectedRow];
501     }
502
503     do
504     {
505         char *psz_temp;
506         i_current++;
507
508         vlc_mutex_lock( &p_playlist->object_lock );
509         o_current_name = [NSString stringWithUTF8String:
510             p_playlist->pp_items[i_current]->input.psz_name];
511         psz_temp = playlist_GetInfo(p_playlist, i_current ,_("General"),_("Author") );
512         o_current_author = [NSString stringWithUTF8String: psz_temp];
513         free( psz_temp);
514         vlc_mutex_unlock( &p_playlist->object_lock );
515
516
517         if( [o_current_name rangeOfString:[o_search_keyword stringValue] options:NSCaseInsensitiveSearch ].length ||
518              [o_current_author rangeOfString:[o_search_keyword stringValue] options:NSCaseInsensitiveSearch ].length )
519         {
520              [o_table_view selectRow: i_current byExtendingSelection: NO];
521              [o_table_view scrollRowToVisible: i_current];
522              break;
523         }
524         if( i_current == [o_table_view numberOfRows] - 1 )
525         {
526              i_current = -1;
527         }
528     }
529     while (i_current != [o_table_view selectedRow]);
530     vlc_object_release( p_playlist );
531 }
532
533
534 - (IBAction)handlePopUp:(id)sender
535
536 {
537              intf_thread_t * p_intf = VLCIntf;
538              vlc_value_t val1,val2;
539              playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
540                                                         FIND_ANYWHERE );
541              if( p_playlist == NULL )
542              {
543                  return;
544              }
545
546     switch ([o_loop_popup indexOfSelectedItem])
547     {
548         case 1:
549
550              val1.b_bool = 0;
551              var_Set( p_playlist, "loop", val1 );
552              val1.b_bool = 1;
553              var_Set( p_playlist, "repeat", val1 );
554              vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat One" ) );
555         break;
556
557         case 2:
558              val1.b_bool = 0;
559              var_Set( p_playlist, "repeat", val1 );
560              val1.b_bool = 1;
561              var_Set( p_playlist, "loop", val1 );
562              vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat All" ) );
563         break;
564
565         default:
566              var_Get( p_playlist, "repeat", &val1 );
567              var_Get( p_playlist, "loop", &val2 );
568              if (val1.b_bool || val2.b_bool)
569              {
570                   val1.b_bool = 0;
571                   var_Set( p_playlist, "repeat", val1 );
572                   var_Set( p_playlist, "loop", val1 );
573                   vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
574              }
575          break;
576      }
577      vlc_object_release( p_playlist );
578      [self playlistUpdated];
579 }
580
581
582 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
583 {
584     int i_item;
585     intf_thread_t * p_intf = VLCIntf;
586     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
587                                                        FIND_ANYWHERE );
588
589     if( p_playlist == NULL )
590     {
591         return;
592     }
593
594     for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
595     {
596         /* One item */
597         NSDictionary *o_one_item;
598         int j, i_total_options = 0, i_new_id = -1;
599         int i_mode = PLAYLIST_INSERT;
600         BOOL b_rem = FALSE, b_dir = FALSE;
601         NSString *o_uri, *o_name;
602         NSArray *o_options;
603         NSURL *o_true_file;
604         char **ppsz_options = NULL;
605
606         /* Get the item */
607         o_one_item = [o_array objectAtIndex: i_item];
608         o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
609         o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
610         o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
611
612         /* If no name, then make a guess */
613         if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
614
615         if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
616             [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
617                     isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem   )
618         {
619             /* All of this is to make sure CD's play when you D&D them on VLC */
620             /* Converts mountpoint to a /dev file */
621             struct statfs *buf;
622             char *psz_dev;
623             buf = (struct statfs *) malloc (sizeof(struct statfs));
624             statfs( [o_uri fileSystemRepresentation], buf );
625             psz_dev = strdup(buf->f_mntfromname);
626             o_uri = [NSString stringWithCString: psz_dev ];
627         }
628
629         if( o_options && [o_options count] > 0 )
630         {
631             /* Count the input options */
632             i_total_options = [o_options count];
633
634             /* Allocate ppsz_options */
635             for( j = 0; j < i_total_options; j++ )
636             {
637                 if( !ppsz_options )
638                     ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
639
640                 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
641             }
642         }
643
644         /* Add the item */
645         i_new_id = playlist_AddExt( p_playlist, [o_uri fileSystemRepresentation],
646                       [o_name UTF8String], i_mode,
647                       i_position == -1 ? PLAYLIST_END : i_position + i_item,
648                       0, (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options );
649
650         /* clean up
651         for( j = 0; j < i_total_options; j++ )
652             free( ppsz_options[j] );
653         if( ppsz_options ) free( ppsz_options ); */
654
655         /* Recent documents menu */
656         o_true_file = [NSURL fileURLWithPath: o_uri];
657         if( o_true_file != nil )
658         {
659             [[NSDocumentController sharedDocumentController]
660                 noteNewRecentDocumentURL: o_true_file];
661         }
662
663         if( i_item == 0 && !b_enqueue )
664         {
665             playlist_Goto( p_playlist, playlist_GetPositionById( p_playlist, i_new_id ) );
666             playlist_Play( p_playlist );
667         }
668     }
669
670     vlc_object_release( p_playlist );
671 }
672
673 - (void)playlistUpdated
674 {
675     vlc_value_t val1, val2;
676     intf_thread_t * p_intf = VLCIntf;
677     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
678                                                        FIND_ANYWHERE );
679     if( p_playlist != NULL )
680     {
681         var_Get( p_playlist, "random", &val1 );
682         [o_random_ckb setState: val1.b_bool];
683
684         var_Get( p_playlist, "repeat", &val1 );
685         var_Get( p_playlist, "loop", &val2 );
686         if(val1.b_bool)
687         {
688             [o_loop_popup selectItemAtIndex:1];
689         }
690         else if(val2.b_bool)
691         {
692             [o_loop_popup selectItemAtIndex:2];
693         }
694         else
695         {
696             [o_loop_popup selectItemAtIndex:0];
697         }
698         vlc_object_release( p_playlist );
699     }
700     [o_table_view reloadData];
701 }
702
703 - (void)updateRowSelection
704 {
705     int i_row;
706
707     intf_thread_t * p_intf = VLCIntf;
708     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
709                                                        FIND_ANYWHERE );
710
711     if( p_playlist == NULL )
712     {
713         return;
714     }
715
716     i_row = p_playlist->i_index;
717     vlc_object_release( p_playlist );
718
719     [o_table_view selectRow: i_row byExtendingSelection: NO];
720     [o_table_view scrollRowToVisible: i_row];
721 }
722
723 - (int)selectedPlaylistItem
724 {
725     return [o_table_view selectedRow];
726 }
727
728 - (NSMutableArray *)selectedPlaylistItemsList
729 {
730     return [NSMutableArray arrayWithArray:[[o_table_view
731                         selectedRowEnumerator] allObjects]];
732
733 }
734
735 - (void)deleteGroup:(int)i_id
736 {
737 #if 0
738     intf_thread_t * p_intf = VLCIntf;
739     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
740                                                        FIND_ANYWHERE );
741     int i;
742     int i_newgroup = 0;
743
744     if (p_playlist)
745     {
746
747     /*first, change the group of all the items that belong to the group to
748     delete. Change it to the group with the smallest id.*/
749
750     /*search for the group with the smallest id*/
751
752         if(p_playlist->i_groups == 1)
753         {
754             msg_Warn(p_playlist,"Trying to delete last group, cancelling");
755             vlc_object_release(p_playlist);
756             return;
757         }
758
759         for (i = 0 ; i<p_playlist->i_groups ; i++)
760         {
761             if((i_newgroup == 0 || i_newgroup > p_playlist->pp_groups[i]->i_id)
762                             && p_playlist->pp_groups[i]->i_id != i_id)
763             {
764                 i_newgroup = p_playlist->pp_groups[i]->i_id;
765             }
766         }
767
768         vlc_mutex_lock( &p_playlist->object_lock );
769
770         for (i = 0; i < p_playlist->i_size;i++)
771         {
772             if (p_playlist->pp_items[i]->i_group == i_id)
773             {
774                 vlc_mutex_lock(&p_playlist->pp_items[i]->input.lock);
775                 p_playlist->pp_items[i]->i_group = i_newgroup;
776                 vlc_mutex_unlock(&p_playlist->pp_items[i]->input.lock);
777             }
778         }
779         vlc_mutex_unlock( &p_playlist->object_lock );
780
781         playlist_DeleteGroup( p_playlist, i_id );
782
783         vlc_object_release(p_playlist);
784         [self playlistUpdated];
785     }
786 #endif
787 }
788
789 - (NSColor *)getColor:(int)i_group
790 {
791     NSColor * o_color = nil;
792     switch ( i_group % 8 )
793     {
794         case 1:
795             /*white*/
796             o_color = [NSColor colorWithDeviceRed:1.0 green:1.0 blue:1.0 alpha:1.0];
797         break;
798
799         case 2:
800             /*red*/
801            o_color = [NSColor colorWithDeviceRed:1.0 green:0.76471 blue:0.76471 alpha:1.0];
802         break;
803
804         case 3:
805               /*dark blue*/
806            o_color = [NSColor colorWithDeviceRed:0.76471 green:0.76471 blue:1.0 alpha:1.0];
807         break;
808
809         case 4:
810                /*orange*/
811            o_color = [NSColor colorWithDeviceRed:1.0 green:0.89804 blue:0.76471 alpha:1.0];
812         break;
813
814         case 5:
815                /*purple*/
816            o_color = [NSColor colorWithDeviceRed:1.0 green:0.76471 blue:1.0 alpha:1.0];
817         break;
818
819         case 6:
820               /*green*/
821            o_color = [NSColor colorWithDeviceRed:0.76471 green:1.0 blue:0.76471 alpha:1.0];
822         break;
823
824         case 7:
825               /*light blue*/
826            o_color = [NSColor colorWithDeviceRed:0.76471 green:1.0 blue:1.0 alpha:1.0];
827         break;
828
829         case 0:
830               /*yellow*/
831            o_color = [NSColor colorWithDeviceRed:1.0 green:1.0 blue:0.76471 alpha:1.0];
832         break;
833     }
834     return o_color;
835 }
836
837 @end
838
839 @implementation VLCPlaylist (NSTableDataSource)
840
841 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
842 {
843     int i_count = 0;
844     intf_thread_t * p_intf = VLCIntf;
845     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
846                                                        FIND_ANYWHERE );
847
848     if( p_playlist != NULL )
849     {
850         vlc_mutex_lock( &p_playlist->object_lock );
851         i_count = p_playlist->i_size;
852         vlc_mutex_unlock( &p_playlist->object_lock );
853         vlc_object_release( p_playlist );
854     }
855     [o_status_field setStringValue: [NSString stringWithFormat:_NS("%i items in playlist"), i_count]];
856     return( i_count );
857 }
858
859 - (id)tableView:(NSTableView *)o_tv
860                 objectValueForTableColumn:(NSTableColumn *)o_tc
861                 row:(int)i_row
862 {
863     id o_value = nil;
864     intf_thread_t * p_intf = VLCIntf;
865     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
866                                                FIND_ANYWHERE );
867
868     if( p_playlist == NULL )
869     {
870         return( nil );
871     }
872
873     if( [[o_tc identifier] isEqualToString:@"0"] )
874     {
875         o_value = [NSString stringWithFormat:@"%i", i_row + 1];
876     }
877     else if( [[o_tc identifier] isEqualToString:@"1"] )
878     {
879         vlc_mutex_lock( &p_playlist->object_lock );
880         o_value = [NSString stringWithUTF8String:
881             p_playlist->pp_items[i_row]->input.psz_name];
882         if( o_value == NULL )
883             o_value = [NSString stringWithCString:
884                 p_playlist->pp_items[i_row]->input.psz_name];
885         vlc_mutex_unlock( &p_playlist->object_lock );
886     }
887     else if( [[o_tc identifier] isEqualToString:@"2"] )
888     {
889         char *psz_temp;
890         vlc_mutex_lock( &p_playlist->object_lock );
891         psz_temp = playlist_GetInfo( p_playlist, i_row ,_("Meta-information"),_("Artist") );
892         vlc_mutex_unlock( &p_playlist->object_lock );
893
894         if( psz_temp == NULL )
895         {
896             o_value = @"";
897         }
898         else
899         {
900             o_value = [NSString stringWithUTF8String: psz_temp];
901             if( o_value == NULL )
902             {
903                 o_value = [NSString stringWithCString: psz_temp];
904             }
905             free( psz_temp );
906         }
907     }
908     else if( [[o_tc identifier] isEqualToString:@"3"] )
909     {
910         char psz_duration[MSTRTIME_MAX_SIZE];
911         mtime_t dur = p_playlist->pp_items[i_row]->input.i_duration;
912         if( dur != -1 )
913         {
914             secstotimestr( psz_duration, dur/1000000 );
915             o_value = [NSString stringWithUTF8String: psz_duration];
916         }
917         else
918         {
919             o_value = @"-:--:--";
920         }
921     }
922
923     vlc_object_release( p_playlist );
924
925     return( o_value );
926 }
927
928 - (void)tableView:(NSTableView *)o_tv
929                 willDisplayCell:(id)o_cell
930                 forTableColumn:(NSTableColumn *)o_tc
931                 row:(int)i_rows
932 {
933     intf_thread_t * p_intf = VLCIntf;
934     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
935                                                FIND_ANYWHERE );
936     if (p_playlist)
937     {
938 #if 0
939         if ((p_playlist->i_groups) > 1 )
940         {
941             [o_cell setDrawsBackground: VLC_TRUE];
942             [o_cell setBackgroundColor:
943                 [self getColor:p_playlist->pp_items[i_rows]->i_group]];
944         }
945         else
946         {
947             [o_cell setDrawsBackground: VLC_FALSE];
948         }
949 #endif
950         if (!p_playlist->pp_items[i_rows]->b_enabled)
951         {
952             [o_cell setTextColor: [NSColor colorWithDeviceRed:0.3686 green:0.3686 blue:0.3686 alpha:1.0]];
953         }
954         else
955         {
956             [o_cell setTextColor:[NSColor colorWithDeviceRed:0.0 green:0.0 blue:0.0 alpha:1.0]];
957         }
958     vlc_object_release( p_playlist );
959     }
960 }
961
962 - (BOOL)tableView:(NSTableView *)o_tv
963                     writeRows:(NSArray*)o_rows
964                     toPasteboard:(NSPasteboard*)o_pasteboard
965 {
966     int i_rows = [o_rows count];
967     NSArray *o_filenames = [NSArray array];
968
969     [o_pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
970     [o_pasteboard setPropertyList:o_filenames forType:NSFilenamesPboardType];
971     if ( i_rows == 1 )
972     {
973         i_moveRow = [[o_rows objectAtIndex:0]intValue];
974         return YES;
975     }
976     return NO;
977 }
978
979 - (NSDragOperation)tableView:(NSTableView*)o_tv
980                     validateDrop:(id <NSDraggingInfo>)o_info
981                     proposedRow:(int)i_row
982                     proposedDropOperation:(NSTableViewDropOperation)o_operation
983 {
984     if ( o_operation == NSTableViewDropAbove )
985     {
986         if ( i_moveRow >= 0 )
987         {
988             if ( i_row != i_moveRow )
989             {
990                 return NSDragOperationMove;
991             }
992             /* what if in the previous run, the row wasn't actually moved?
993                then we can't drop new files on this location */
994             return NSDragOperationNone;
995         }
996         return NSDragOperationGeneric;
997     }
998     return NSDragOperationNone;
999 }
1000
1001 - (BOOL)tableView:(NSTableView*)o_tv
1002                     acceptDrop:(id <NSDraggingInfo>)o_info
1003                     row:(int)i_proposed_row
1004                     dropOperation:(NSTableViewDropOperation)o_operation
1005 {
1006     if (  i_moveRow >= 0 )
1007     {
1008         if (i_moveRow != -1 && i_proposed_row != -1)
1009         {
1010             intf_thread_t * p_intf = VLCIntf;
1011             playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1012                                                             FIND_ANYWHERE );
1013
1014             if( p_playlist == NULL )
1015             {
1016                 i_moveRow = -1;
1017                 return NO;
1018             }
1019
1020             playlist_Move( p_playlist, i_moveRow, i_proposed_row );
1021
1022             vlc_object_release( p_playlist );
1023         }
1024         [self playlistUpdated];
1025         i_moveRow = -1;
1026         return YES;
1027     }
1028     else
1029     {
1030         NSPasteboard * o_pasteboard;
1031         o_pasteboard = [o_info draggingPasteboard];
1032
1033         if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
1034         {
1035             int i;
1036             NSArray *o_array = [NSArray array];
1037             NSArray *o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
1038                         sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
1039
1040             for( i = 0; i < (int)[o_values count]; i++)
1041             {
1042                 NSDictionary *o_dic;
1043                 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
1044                 o_array = [o_array arrayByAddingObject: o_dic];
1045             }
1046             [self appendArray: o_array atPos: i_proposed_row enqueue:YES];
1047             return YES;
1048         }
1049         return NO;
1050     }
1051     [self updateRowSelection];
1052 }
1053
1054 /* Delegate method of NSWindow */
1055 - (void)windowWillClose:(NSNotification *)aNotification
1056 {
1057     [o_btn_playlist setState: NSOffState];
1058 }
1059
1060 @end
1061
1062