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