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