playlist.m
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:56k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * playlist.m: MacOS X interface module
  3.  *****************************************************************************
  4. * Copyright (C) 2002-2009 the VideoLAN team
  5.  * $Id: c02736316538139ebdbfe196118cc831caa08348 $
  6.  *
  7.  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
  8.  *          Derk-Jan Hartman <hartman at videola/n 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  24.  *****************************************************************************/
  25. /* TODO
  26.  * add 'icons' for different types of nodes? (http://www.cocoadev.com/index.pl?IconAndTextInTableCell)
  27.  * reimplement enable/disable item
  28.  * create a new 'tool' button (see the gear button in the Finder window) for 'actions'
  29.    (adding service discovery, other views, new node/playlist, save node/playlist) stuff like that
  30.  */
  31. /*****************************************************************************
  32.  * Preamble
  33.  *****************************************************************************/
  34. #include <stdlib.h>                                      /* malloc(), free() */
  35. #include <sys/param.h>                                    /* for MAXPATHLEN */
  36. #include <string.h>
  37. #include <math.h>
  38. #include <sys/mount.h>
  39. #include <vlc_keys.h>
  40. #import "intf.h"
  41. #import "wizard.h"
  42. #import "bookmarks.h"
  43. #import "playlistinfo.h"
  44. #import "playlist.h"
  45. #import "controls.h"
  46. #import "vlc_osd.h"
  47. #import "misc.h"
  48. #import <vlc_interface.h>
  49. #import <vlc_services_discovery.h>
  50. /*****************************************************************************
  51.  * VLCPlaylistView implementation
  52.  *****************************************************************************/
  53. @implementation VLCPlaylistView
  54. - (NSMenu *)menuForEvent:(NSEvent *)o_event
  55. {
  56.     return( [[self delegate] menuForEvent: o_event] );
  57. }
  58. - (void)keyDown:(NSEvent *)o_event
  59. {
  60.     unichar key = 0;
  61.     if( [[o_event characters] length] )
  62.     {
  63.         key = [[o_event characters] characterAtIndex: 0];
  64.     }
  65.     switch( key )
  66.     {
  67.         case NSDeleteCharacter:
  68.         case NSDeleteFunctionKey:
  69.         case NSDeleteCharFunctionKey:
  70.         case NSBackspaceCharacter:
  71.             [[self delegate] deleteItem:self];
  72.             break;
  73.         case NSEnterCharacter:
  74.         case NSCarriageReturnCharacter:
  75.             [(VLCPlaylist *)[[VLCMain sharedInstance] playlist] playItem:self];
  76.             break;
  77.         default:
  78.             [super keyDown: o_event];
  79.             break;
  80.     }
  81. }
  82. @end
  83. /*****************************************************************************
  84.  * VLCPlaylistCommon implementation
  85.  *
  86.  * This class the superclass of the VLCPlaylist and VLCPlaylistWizard.
  87.  * It contains the common methods and elements of these 2 entities.
  88.  *****************************************************************************/
  89. @implementation VLCPlaylistCommon
  90. - (id)init
  91. {
  92.     self = [super init];
  93.     if ( self != nil )
  94.     {
  95.         o_outline_dict = [[NSMutableDictionary alloc] init];
  96.     }
  97.     return self;
  98. }
  99. - (void)awakeFromNib
  100. {
  101.     playlist_t * p_playlist = pl_Hold( VLCIntf );
  102.     [o_outline_view setTarget: self];
  103.     [o_outline_view setDelegate: self];
  104.     [o_outline_view setDataSource: self];
  105.     [o_outline_view setAllowsEmptySelection: NO];
  106.     [o_outline_view expandItem: [o_outline_view itemAtRow:0]];
  107.     pl_Release( VLCIntf );
  108.     [self initStrings];
  109. }
  110. - (void)initStrings
  111. {
  112.     [[o_tc_name headerCell] setStringValue:_NS("Name")];
  113.     [[o_tc_author headerCell] setStringValue:_NS("Author")];
  114.     [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
  115. }
  116. - (NSOutlineView *)outlineView
  117. {
  118.     return o_outline_view;
  119. }
  120. - (playlist_item_t *)selectedPlaylistItem
  121. {
  122.     return [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
  123.                                                                 pointerValue];
  124. }
  125. @end
  126. @implementation VLCPlaylistCommon (NSOutlineViewDataSource)
  127. /* return the number of children for Obj-C pointer item */ /* DONE */
  128. - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
  129. {
  130.     int i_return = 0;
  131.     playlist_item_t *p_item = NULL;
  132.     playlist_t * p_playlist = pl_Hold( VLCIntf );
  133.     assert( outlineView == o_outline_view );
  134.     if( !item )
  135.         p_item = p_playlist->p_root_category;
  136.     else
  137.         p_item = (playlist_item_t *)[item pointerValue];
  138.     if( p_item )
  139.         i_return = p_item->i_children;
  140.     pl_Release( VLCIntf );
  141.     return i_return > 0 ? i_return : 0;
  142. }
  143. /* return the child at index for the Obj-C pointer item */ /* DONE */
  144. - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
  145. {
  146.     playlist_item_t *p_return = NULL, *p_item = NULL;
  147.     NSValue *o_value;
  148.     playlist_t * p_playlist = pl_Hold( VLCIntf );
  149.     PL_LOCK;
  150.     if( item == nil )
  151.     {
  152.         /* root object */
  153.         p_item = p_playlist->p_root_category;
  154.     }
  155.     else
  156.     {
  157.         p_item = (playlist_item_t *)[item pointerValue];
  158.     }
  159.     if( p_item && index < p_item->i_children && index >= 0 )
  160.         p_return = p_item->pp_children[index];
  161.     PL_UNLOCK;
  162.     pl_Release( VLCIntf );
  163.     o_value = [o_outline_dict objectForKey:[NSString stringWithFormat: @"%p", p_return]];
  164.     if( o_value == nil )
  165.     {
  166.         /* FIXME: Why is there a warning if that happens all the time and seems
  167.          * to be normal? Add an assert and fix it. 
  168.          * msg_Warn( VLCIntf, "playlist item misses pointer value, adding one" ); */
  169.         o_value = [[NSValue valueWithPointer: p_return] retain];
  170.     }
  171.     return o_value;
  172. }
  173. /* is the item expandable */
  174. - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
  175. {
  176.     int i_return = 0;
  177.     playlist_t *p_playlist = pl_Hold( VLCIntf );
  178.     PL_LOCK;
  179.     if( item == nil )
  180.     {
  181.         /* root object */
  182.         if( p_playlist->p_root_category )
  183.         {
  184.             i_return = p_playlist->p_root_category->i_children;
  185.         }
  186.     }
  187.     else
  188.     {
  189.         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
  190.         if( p_item )
  191.             i_return = p_item->i_children;
  192.     }
  193.     PL_UNLOCK;
  194.     pl_Release( VLCIntf );
  195.     return (i_return >= 0);
  196. }
  197. /* retrieve the string values for the cells */
  198. - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
  199. {
  200.     id o_value = nil;
  201.     playlist_item_t *p_item;
  202.     /* For error handling */
  203.     static BOOL attempted_reload = NO;
  204.     if( item == nil || ![item isKindOfClass: [NSValue class]] )
  205.     {
  206.         /* Attempt to fix the error by asking for a data redisplay
  207.          * This might cause infinite loop, so add a small check */
  208.         if( !attempted_reload )
  209.         {
  210.             attempted_reload = YES;
  211.             [outlineView reloadData];
  212.         }
  213.         return @"error" ;
  214.     }
  215.  
  216.     p_item = (playlist_item_t *)[item pointerValue];
  217.     if( !p_item || !p_item->p_input )
  218.     {
  219.         /* Attempt to fix the error by asking for a data redisplay
  220.          * This might cause infinite loop, so add a small check */
  221.         if( !attempted_reload )
  222.         {
  223.             attempted_reload = YES;
  224.             [outlineView reloadData];
  225.         }
  226.         return @"error";
  227.     }
  228.  
  229.     attempted_reload = NO;
  230.     if( [[o_tc identifier] isEqualToString:@"name"] )
  231.     {
  232.         /* sanity check to prevent the NSString class from crashing */
  233.         char *psz_title =  input_item_GetTitleFbName( p_item->p_input );
  234.         if( psz_title )
  235.         {
  236.             o_value = [NSString stringWithUTF8String: psz_title];
  237.             free( psz_title );
  238.         }
  239.     }
  240.     else if( [[o_tc identifier] isEqualToString:@"artist"] )
  241.     {
  242.         char *psz_artist = input_item_GetArtist( p_item->p_input );
  243.         if( psz_artist )
  244.             o_value = [NSString stringWithUTF8String: psz_artist];
  245.         free( psz_artist );
  246.     }
  247.     else if( [[o_tc identifier] isEqualToString:@"duration"] )
  248.     {
  249.         char psz_duration[MSTRTIME_MAX_SIZE];
  250.         mtime_t dur = input_item_GetDuration( p_item->p_input );
  251.         if( dur != -1 )
  252.         {
  253.             secstotimestr( psz_duration, dur/1000000 );
  254.             o_value = [NSString stringWithUTF8String: psz_duration];
  255.         }
  256.         else
  257.             o_value = @"--:--";
  258.     }
  259.     else if( [[o_tc identifier] isEqualToString:@"status"] )
  260.     {
  261.         if( input_item_HasErrorWhenReading( p_item->p_input ) )
  262.         {
  263.             o_value = [NSImage imageWithWarningIcon];
  264.         }
  265.     }
  266.     return o_value;
  267. }
  268. @end
  269. /*****************************************************************************
  270.  * VLCPlaylistWizard implementation
  271.  *****************************************************************************/
  272. @implementation VLCPlaylistWizard
  273. - (IBAction)reloadOutlineView
  274. {
  275.     /* Only reload the outlineview if the wizard window is open since this can
  276.        be quite long on big playlists */
  277.     if( [[o_outline_view window] isVisible] )
  278.     {
  279.         [o_outline_view reloadData];
  280.     }
  281. }
  282. @end
  283. /*****************************************************************************
  284.  * extension to NSOutlineView's interface to fix compilation warnings
  285.  * and let us access these 2 functions properly
  286.  * this uses a private Apple-API, but works fine on all current OSX releases
  287.  * keep checking for compatiblity with future releases though
  288.  *****************************************************************************/
  289. @interface NSOutlineView (UndocumentedSortImages)
  290. + (NSImage *)_defaultTableHeaderSortImage;
  291. + (NSImage *)_defaultTableHeaderReverseSortImage;
  292. @end
  293. /*****************************************************************************
  294.  * VLCPlaylist implementation
  295.  *****************************************************************************/
  296. @implementation VLCPlaylist
  297. - (id)init
  298. {
  299.     self = [super init];
  300.     if ( self != nil )
  301.     {
  302.         o_nodes_array = [[NSMutableArray alloc] init];
  303.         o_items_array = [[NSMutableArray alloc] init];
  304.     }
  305.     return self;
  306. }
  307. - (void)dealloc
  308. {
  309.     [o_nodes_array release];
  310.     [o_items_array release];
  311.     [super dealloc];
  312. }
  313. - (void)awakeFromNib
  314. {
  315.     playlist_t * p_playlist = pl_Hold( VLCIntf );
  316.     int i;
  317.     [super awakeFromNib];
  318.     [o_outline_view setDoubleAction: @selector(playItem:)];
  319.     [o_outline_view registerForDraggedTypes:
  320.         [NSArray arrayWithObjects: NSFilenamesPboardType,
  321.         @"VLCPlaylistItemPboardType", nil]];
  322.     [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
  323.     /* This uses private Apple API which works fine until 10.5.
  324.      * We need to keep checking in the future!
  325.      * These methods are being added artificially to NSOutlineView's interface above */
  326.     o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
  327.     o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
  328.     o_tc_sortColumn = nil;
  329.     char ** ppsz_name;
  330.     char ** ppsz_services = vlc_sd_GetNames( &ppsz_name );
  331.     if( !ppsz_services )
  332.     {
  333.         pl_Release( VLCIntf );
  334.         return;
  335.     }
  336.     
  337.     for( i = 0; ppsz_services[i]; i++ )
  338.     {
  339.         bool  b_enabled;
  340.         NSMenuItem  *o_lmi;
  341.         char * name = ppsz_name[i] ? ppsz_name[i] : ppsz_services[i];
  342.         /* Check whether to enable these menuitems */
  343.         b_enabled = playlist_IsServicesDiscoveryLoaded( p_playlist, ppsz_services[i] );
  344.         /* Create the menu entries used in the playlist menu */
  345.         o_lmi = [[o_mi_services submenu] addItemWithTitle:
  346.                  [NSString stringWithUTF8String: name]
  347.                                          action: @selector(servicesChange:)
  348.                                          keyEquivalent: @""];
  349.         [o_lmi setTarget: self];
  350.         [o_lmi setRepresentedObject: [NSString stringWithUTF8String: ppsz_services[i]]];
  351.         if( b_enabled ) [o_lmi setState: NSOnState];
  352.         /* Create the menu entries for the main menu */
  353.         o_lmi = [[o_mm_mi_services submenu] addItemWithTitle:
  354.                  [NSString stringWithUTF8String: name]
  355.                                          action: @selector(servicesChange:)
  356.                                          keyEquivalent: @""];
  357.         [o_lmi setTarget: self];
  358.         [o_lmi setRepresentedObject: [NSString stringWithUTF8String: ppsz_services[i]]];
  359.         if( b_enabled ) [o_lmi setState: NSOnState];
  360.         free( ppsz_services[i] );
  361.         free( ppsz_name[i] );
  362.     }
  363.     free( ppsz_services );
  364.     free( ppsz_name );
  365.     pl_Release( VLCIntf );
  366. }
  367. - (void)searchfieldChanged:(NSNotification *)o_notification
  368. {
  369.     [o_search_field setStringValue:[[o_notification object] stringValue]];
  370. }
  371. - (void)initStrings
  372. {
  373.     [super initStrings];
  374.     [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
  375.     [o_mi_play setTitle: _NS("Play")];
  376.     [o_mi_delete setTitle: _NS("Delete")];
  377.     [o_mi_recursive_expand setTitle: _NS("Expand Node")];
  378.     [o_mi_selectall setTitle: _NS("Select All")];
  379.     [o_mi_info setTitle: _NS("Media Information...")];
  380.     [o_mi_dl_cover_art setTitle: _NS("Download Cover Art")];
  381.     [o_mi_preparse setTitle: _NS("Fetch Meta Data")];
  382.     [o_mi_revealInFinder setTitle: _NS("Reveal in Finder")];
  383.     [o_mm_mi_revealInFinder setTitle: _NS("Reveal in Finder")];
  384.     [[o_mm_mi_revealInFinder menu] setAutoenablesItems: NO];
  385.     [o_mi_sort_name setTitle: _NS("Sort Node by Name")];
  386.     [o_mi_sort_author setTitle: _NS("Sort Node by Author")];
  387.     [o_mi_services setTitle: _NS("Services discovery")];
  388.     [o_mm_mi_services setTitle: _NS("Services discovery")];
  389.     [o_status_field setStringValue: _NS("No items in the playlist")];
  390.     [o_search_field setToolTip: _NS("Search in Playlist")];
  391.     [o_mi_addNode setTitle: _NS("Add Folder to Playlist")];
  392.     [o_save_accessory_text setStringValue: _NS("File Format:")];
  393.     [[o_save_accessory_popup itemAtIndex:0] setTitle: _NS("Extended M3U")];
  394.     [[o_save_accessory_popup itemAtIndex:1] setTitle: _NS("XML Shareable Playlist Format (XSPF)")];
  395.     [[o_save_accessory_popup itemAtIndex:2] setTitle: _NS("HTML Playlist")];
  396. }
  397. - (void)playlistUpdated
  398. {
  399.     /* Clear indications of any existing column sorting */
  400.     for( unsigned int i = 0 ; i < [[o_outline_view tableColumns] count] ; i++ )
  401.     {
  402.         [o_outline_view setIndicatorImage:nil inTableColumn:
  403.                             [[o_outline_view tableColumns] objectAtIndex:i]];
  404.     }
  405.     [o_outline_view setHighlightedTableColumn:nil];
  406.     o_tc_sortColumn = nil;
  407.     // TODO Find a way to keep the dict size to a minimum
  408.     //[o_outline_dict removeAllObjects];
  409.     [o_outline_view reloadData];
  410.     [[[[VLCMain sharedInstance] wizard] playlistWizard] reloadOutlineView];
  411.     [[[[VLCMain sharedInstance] bookmarks] dataTable] reloadData];
  412.     playlist_t *p_playlist = pl_Hold( VLCIntf );
  413.     PL_LOCK;
  414.     if( playlist_CurrentSize( p_playlist ) >= 2 )
  415.     {
  416.         [o_status_field setStringValue: [NSString stringWithFormat:
  417.                     _NS("%i items"),
  418.                 playlist_CurrentSize( p_playlist )]];
  419.     }
  420.     else
  421.     {
  422.         if( playlist_IsEmpty( p_playlist ) )
  423.             [o_status_field setStringValue: _NS("No items in the playlist")];
  424.         else
  425.             [o_status_field setStringValue: _NS("1 item")];
  426.     }
  427.     PL_UNLOCK;
  428.     pl_Release( VLCIntf );
  429.     [self outlineViewSelectionDidChange: nil];
  430. }
  431. - (void)playModeUpdated
  432. {
  433.     playlist_t *p_playlist = pl_Hold( VLCIntf );
  434.     bool loop = var_GetBool( p_playlist, "loop" );
  435.     bool repeat = var_GetBool( p_playlist, "repeat" );
  436.     if( repeat )
  437.         [[[VLCMain sharedInstance] controls] repeatOne];
  438.     else if( loop )
  439.         [[[VLCMain sharedInstance] controls] repeatAll];
  440.     else
  441.         [[[VLCMain sharedInstance] controls] repeatOff];
  442.     [[[VLCMain sharedInstance] controls] shuffle];
  443.     pl_Release( VLCIntf );
  444. }
  445. - (void)outlineViewSelectionDidChange:(NSNotification *)notification
  446. {
  447.     // FIXME: unsafe
  448.     playlist_item_t * p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
  449.     if( p_item )
  450.     {
  451.         /* update the state of our Reveal-in-Finder menu items */
  452.         NSMutableString *o_mrl;
  453.         char *psz_uri = input_item_GetURI( p_item->p_input );
  454.         [o_mi_revealInFinder setEnabled: NO];
  455.         [o_mm_mi_revealInFinder setEnabled: NO];
  456.         if( psz_uri )
  457.         {
  458.             o_mrl = [NSMutableString stringWithUTF8String: psz_uri];
  459.             /* perform some checks whether it is a file and if it is local at all... */
  460.             NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
  461.             if( prefix_range.location != NSNotFound )
  462.                 [o_mrl deleteCharactersInRange: prefix_range];
  463.             if( [o_mrl characterAtIndex:0] == '/' )
  464.             {
  465.                 [o_mi_revealInFinder setEnabled: YES];
  466.                 [o_mm_mi_revealInFinder setEnabled: YES];
  467.             }
  468.             free( psz_uri );
  469.         }
  470.         if( [[VLCMain sharedInstance] isPlaylistCollapsed] == NO )
  471.         {
  472.             /* update our info-panel to reflect the new item, if we aren't collapsed */
  473.             [[[VLCMain sharedInstance] info] updatePanelWithItem:p_item->p_input];
  474.         }
  475.     }
  476. }
  477. - (BOOL)isSelectionEmpty
  478. {
  479.     return [o_outline_view selectedRow] == -1;
  480. }
  481. - (void)updateRowSelection
  482. {
  483.     int i_row;
  484.     unsigned int j;
  485.     // FIXME: unsafe
  486.     playlist_t *p_playlist = pl_Hold( VLCIntf );
  487.     playlist_item_t *p_item, *p_temp_item;
  488.     NSMutableArray *o_array = [NSMutableArray array];
  489.     PL_LOCK;
  490.     p_item = playlist_CurrentPlayingItem( p_playlist );
  491.     if( p_item == NULL )
  492.     {
  493.         PL_UNLOCK;
  494.         pl_Release( VLCIntf );
  495.         return;
  496.     }
  497.     p_temp_item = p_item;
  498.     while( p_temp_item->p_parent )
  499.     {
  500.         [o_array insertObject: [NSValue valueWithPointer: p_temp_item] atIndex: 0];
  501.         p_temp_item = p_temp_item->p_parent;
  502.     }
  503.     PL_UNLOCK;
  504.     for( j = 0; j < [o_array count] - 1; j++ )
  505.     {
  506.         id o_item;
  507.         if( ( o_item = [o_outline_dict objectForKey:
  508.                             [NSString stringWithFormat: @"%p",
  509.                             [[o_array objectAtIndex:j] pointerValue]]] ) != nil )
  510.         {
  511.             [o_outline_view expandItem: o_item];
  512.         }
  513.     }
  514.     pl_Release( VLCIntf );
  515. }
  516. /* Check if p_item is a child of p_node recursively. We need to check the item
  517.    existence first since OSX sometimes tries to redraw items that have been
  518.    deleted. We don't do it when not required since this verification takes
  519.    quite a long time on big playlists (yes, pretty hacky). */
  520. - (BOOL)isItem: (playlist_item_t *)p_item
  521.                     inNode: (playlist_item_t *)p_node
  522.                     checkItemExistence:(BOOL)b_check
  523.                     locked:(BOOL)b_locked
  524. {
  525.     playlist_t * p_playlist = pl_Hold( VLCIntf );
  526.     playlist_item_t *p_temp_item = p_item;
  527.     if( p_node == p_item )
  528.     {
  529.         pl_Release( VLCIntf );
  530.         return YES;
  531.     }
  532.     if( p_node->i_children < 1)
  533.     {
  534.         pl_Release( VLCIntf );
  535.         return NO;
  536.     }
  537.     if ( p_temp_item )
  538.     {
  539.         int i;
  540.         if(!b_locked) PL_LOCK;
  541.         if( b_check )
  542.         {
  543.         /* Since outlineView: willDisplayCell:... may call this function with
  544.            p_items that don't exist anymore, first check if the item is still
  545.            in the playlist. Any cleaner solution welcomed. */
  546.             for( i = 0; i < p_playlist->all_items.i_size; i++ )
  547.             {
  548.                 if( ARRAY_VAL( p_playlist->all_items, i) == p_item ) break;
  549.                 else if ( i == p_playlist->all_items.i_size - 1 )
  550.                 {
  551.                     if(!b_locked) PL_UNLOCK;
  552.                     pl_Release( VLCIntf );
  553.                     return NO;
  554.                 }
  555.             }
  556.         }
  557.         while( p_temp_item )
  558.         {
  559.             p_temp_item = p_temp_item->p_parent;
  560.             if( p_temp_item == p_node )
  561.             {
  562.                 if(!b_locked) PL_UNLOCK;
  563.                 pl_Release( VLCIntf );
  564.                 return YES;
  565.             }
  566.         }
  567.         if(!b_locked) PL_UNLOCK;
  568.     }
  569.     pl_Release( VLCIntf );
  570.     return NO;
  571. }
  572. - (BOOL)isItem: (playlist_item_t *)p_item
  573.                     inNode: (playlist_item_t *)p_node
  574.                     checkItemExistence:(BOOL)b_check
  575. {
  576.     [self isItem:p_item inNode:p_node checkItemExistence:b_check locked:NO];
  577. }
  578. /* This method is usefull for instance to remove the selected children of an
  579.    already selected node */
  580. - (void)removeItemsFrom:(id)o_items ifChildrenOf:(id)o_nodes
  581. {
  582.     unsigned int i, j;
  583.     for( i = 0 ; i < [o_items count] ; i++ )
  584.     {
  585.         for ( j = 0 ; j < [o_nodes count] ; j++ )
  586.         {
  587.             if( o_items == o_nodes)
  588.             {
  589.                 if( j == i ) continue;
  590.             }
  591.             if( [self isItem: [[o_items objectAtIndex:i] pointerValue]
  592.                     inNode: [[o_nodes objectAtIndex:j] pointerValue]
  593.                     checkItemExistence: NO locked:NO] )
  594.             {
  595.                 [o_items removeObjectAtIndex:i];
  596.                 /* We need to execute the next iteration with the same index
  597.                    since the current item has been deleted */
  598.                 i--;
  599.                 break;
  600.             }
  601.         }
  602.     }
  603. }
  604. - (IBAction)savePlaylist:(id)sender
  605. {
  606.     playlist_t * p_playlist = pl_Hold( VLCIntf );
  607.     NSSavePanel *o_save_panel = [NSSavePanel savePanel];
  608.     NSString * o_name = [NSString stringWithFormat: @"%@", _NS("Untitled")];
  609.     [o_save_panel setTitle: _NS("Save Playlist")];
  610.     [o_save_panel setPrompt: _NS("Save")];
  611.     [o_save_panel setAccessoryView: o_save_accessory_view];
  612.     if( [o_save_panel runModalForDirectory: nil
  613.             file: o_name] == NSOKButton )
  614.     {
  615.         NSString *o_filename = [o_save_panel filename];
  616.         if( [o_save_accessory_popup indexOfSelectedItem] == 0 )
  617.         {
  618.             NSString * o_real_filename;
  619.             NSRange range;
  620.             range.location = [o_filename length] - [@".m3u" length];
  621.             range.length = [@".m3u" length];
  622.             if( [o_filename compare:@".m3u" options: NSCaseInsensitiveSearch
  623.                                              range: range] != NSOrderedSame )
  624.             {
  625.                 o_real_filename = [NSString stringWithFormat: @"%@.m3u", o_filename];
  626.             }
  627.             else
  628.             {
  629.                 o_real_filename = o_filename;
  630.             }
  631.             playlist_Export( p_playlist,
  632.                 [o_real_filename fileSystemRepresentation],
  633.                 p_playlist->p_local_category, "export-m3u" );
  634.         }
  635.         else if( [o_save_accessory_popup indexOfSelectedItem] == 1 )
  636.         {
  637.             NSString * o_real_filename;
  638.             NSRange range;
  639.             range.location = [o_filename length] - [@".xspf" length];
  640.             range.length = [@".xspf" length];
  641.             if( [o_filename compare:@".xspf" options: NSCaseInsensitiveSearch
  642.                                              range: range] != NSOrderedSame )
  643.             {
  644.                 o_real_filename = [NSString stringWithFormat: @"%@.xspf", o_filename];
  645.             }
  646.             else
  647.             {
  648.                 o_real_filename = o_filename;
  649.             }
  650.             playlist_Export( p_playlist,
  651.                 [o_real_filename fileSystemRepresentation],
  652.                 p_playlist->p_local_category, "export-xspf" );
  653.         }
  654.         else
  655.         {
  656.             NSString * o_real_filename;
  657.             NSRange range;
  658.             range.location = [o_filename length] - [@".html" length];
  659.             range.length = [@".html" length];
  660.             if( [o_filename compare:@".html" options: NSCaseInsensitiveSearch
  661.                                              range: range] != NSOrderedSame )
  662.             {
  663.                 o_real_filename = [NSString stringWithFormat: @"%@.html", o_filename];
  664.             }
  665.             else
  666.             {
  667.                 o_real_filename = o_filename;
  668.             }
  669.             playlist_Export( p_playlist,
  670.                 [o_real_filename fileSystemRepresentation],
  671.                 p_playlist->p_local_category, "export-html" );
  672.         }
  673.     }
  674.     pl_Release( VLCIntf );
  675. }
  676. /* When called retrieves the selected outlineview row and plays that node or item */
  677. - (IBAction)playItem:(id)sender
  678. {
  679.     intf_thread_t * p_intf = VLCIntf;
  680.     playlist_t * p_playlist = pl_Hold( p_intf );
  681.     playlist_item_t *p_item;
  682.     playlist_item_t *p_node = NULL;
  683.     p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
  684.     PL_LOCK;
  685.     if( p_item )
  686.     {
  687.         if( p_item->i_children == -1 )
  688.         {
  689.             p_node = p_item->p_parent;
  690.         }
  691.         else
  692.         {
  693.             p_node = p_item;
  694.             if( p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1 )
  695.             {
  696.                 p_item = p_node->pp_children[0];
  697.             }
  698.             else
  699.             {
  700.                 p_item = NULL;
  701.             }
  702.         }
  703.         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, p_node, p_item );
  704.     }
  705.     PL_UNLOCK;
  706.     pl_Release( p_intf );
  707. }
  708. - (IBAction)revealItemInFinder:(id)sender
  709. {
  710.     playlist_item_t * p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
  711.     NSMutableString * o_mrl = nil;
  712.     if(! p_item || !p_item->p_input )
  713.         return;
  714.     
  715.     char *psz_uri = input_item_GetURI( p_item->p_input );
  716.     if( psz_uri )
  717.         o_mrl = [NSMutableString stringWithUTF8String: psz_uri];
  718.     /* perform some checks whether it is a file and if it is local at all... */
  719.     NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
  720.     if( prefix_range.location != NSNotFound )
  721.         [o_mrl deleteCharactersInRange: prefix_range];
  722.     
  723.     if( [o_mrl characterAtIndex:0] == '/' )
  724.         [[NSWorkspace sharedWorkspace] selectFile: o_mrl inFileViewerRootedAtPath: o_mrl];
  725. }    
  726. /* When called retrieves the selected outlineview row and plays that node or item */
  727. - (IBAction)preparseItem:(id)sender
  728. {
  729.     int i_count;
  730.     NSMutableArray *o_to_preparse;
  731.     intf_thread_t * p_intf = VLCIntf;
  732.     playlist_t * p_playlist = pl_Hold( p_intf );
  733.  
  734.     o_to_preparse = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
  735.     i_count = [o_to_preparse count];
  736.     int i, i_row;
  737.     NSNumber *o_number;
  738.     playlist_item_t *p_item = NULL;
  739.     for( i = 0; i < i_count; i++ )
  740.     {
  741.         o_number = [o_to_preparse lastObject];
  742.         i_row = [o_number intValue];
  743.         p_item = [[o_outline_view itemAtRow:i_row] pointerValue];
  744.         [o_to_preparse removeObject: o_number];
  745.         [o_outline_view deselectRow: i_row];
  746.         if( p_item )
  747.         {
  748.             if( p_item->i_children == -1 )
  749.             {
  750.                 playlist_PreparseEnqueue( p_playlist, p_item->p_input, pl_Unlocked );
  751.             }
  752.             else
  753.             {
  754.                 msg_Dbg( p_intf, "preparsing nodes not implemented" );
  755.             }
  756.         }
  757.     }
  758.     pl_Release( p_intf );
  759.     [self playlistUpdated];
  760. }
  761. - (IBAction)downloadCoverArt:(id)sender
  762. {
  763.     int i_count;
  764.     NSMutableArray *o_to_preparse;
  765.     intf_thread_t * p_intf = VLCIntf;
  766.     playlist_t * p_playlist = pl_Hold( p_intf );
  767.     o_to_preparse = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
  768.     i_count = [o_to_preparse count];
  769.     int i, i_row;
  770.     NSNumber *o_number;
  771.     playlist_item_t *p_item = NULL;
  772.     for( i = 0; i < i_count; i++ )
  773.     {
  774.         o_number = [o_to_preparse lastObject];
  775.         i_row = [o_number intValue];
  776.         p_item = [[o_outline_view itemAtRow:i_row] pointerValue];
  777.         [o_to_preparse removeObject: o_number];
  778.         [o_outline_view deselectRow: i_row];
  779.         if( p_item && p_item->i_children == -1 )
  780.         {
  781.             playlist_AskForArtEnqueue( p_playlist, p_item->p_input, pl_Unlocked );
  782.         }
  783.     }
  784.     pl_Release( p_intf );
  785.     [self playlistUpdated];
  786. }
  787. - (IBAction)servicesChange:(id)sender
  788. {
  789.     NSMenuItem *o_mi = (NSMenuItem *)sender;
  790.     NSString *o_string = [o_mi representedObject];
  791.     playlist_t * p_playlist = pl_Hold( VLCIntf );
  792.     if( !playlist_IsServicesDiscoveryLoaded( p_playlist, [o_string UTF8String] ) )
  793.         playlist_ServicesDiscoveryAdd( p_playlist, [o_string UTF8String] );
  794.     else
  795.         playlist_ServicesDiscoveryRemove( p_playlist, [o_string UTF8String] );
  796.     [o_mi setState: playlist_IsServicesDiscoveryLoaded( p_playlist,
  797.                                           [o_string UTF8String] ) ? YES : NO];
  798.     pl_Release( VLCIntf );
  799.     [self playlistUpdated];
  800.     return;
  801. }
  802. - (IBAction)selectAll:(id)sender
  803. {
  804.     [o_outline_view selectAll: nil];
  805. }
  806. - (IBAction)deleteItem:(id)sender
  807. {
  808.     int i_count, i_row;
  809.     NSMutableArray *o_to_delete;
  810.     NSNumber *o_number;
  811.     playlist_t * p_playlist;
  812.     intf_thread_t * p_intf = VLCIntf;
  813.     o_to_delete = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
  814.     i_count = [o_to_delete count];
  815.     p_playlist = pl_Hold( p_intf );
  816.     for( int i = 0; i < i_count; i++ )
  817.     {
  818.         o_number = [o_to_delete lastObject];
  819.         i_row = [o_number intValue];
  820.         id o_item = [o_outline_view itemAtRow: i_row];
  821.         [o_outline_view deselectRow: i_row];
  822.         PL_LOCK;
  823.         playlist_item_t *p_item = [o_item pointerValue];
  824. #ifndef NDEBUG
  825.         msg_Dbg( p_intf, "deleting item %i (of %i) with id "%i", pointerValue "%p" and %i children", i+1, i_count, 
  826.                 p_item->p_input->i_id, [o_item pointerValue], p_item->i_children +1 );
  827. #endif
  828.         [o_to_delete removeObject: o_number];
  829.         if( p_item->i_children != -1 )
  830.         //is a node and not an item
  831.         {
  832.             if( playlist_Status( p_playlist ) != PLAYLIST_STOPPED &&
  833.                 [self isItem: playlist_CurrentPlayingItem( p_playlist ) inNode:
  834.                         ((playlist_item_t *)[o_item pointerValue])
  835.                         checkItemExistence: NO locked:YES] == YES )
  836.                 // if current item is in selected node and is playing then stop playlist
  837.                 playlist_Control(p_playlist, PLAYLIST_STOP, pl_Locked );
  838.     
  839.             playlist_NodeDelete( p_playlist, p_item, true, false );
  840.         }
  841.         else
  842.             playlist_DeleteFromInput( p_playlist, p_item->p_input->i_id, pl_Locked );
  843.         PL_UNLOCK;
  844.         [o_outline_dict removeObjectForKey:[NSString stringWithFormat:@"%p",
  845.                                                      [o_item pointerValue]]];
  846.         [o_item release];
  847.     }
  848.     [self playlistUpdated];
  849.     pl_Release( p_intf );
  850. }
  851. - (IBAction)sortNodeByName:(id)sender
  852. {
  853.     [self sortNode: SORT_TITLE];
  854. }
  855. - (IBAction)sortNodeByAuthor:(id)sender
  856. {
  857.     [self sortNode: SORT_ARTIST];
  858. }
  859. - (void)sortNode:(int)i_mode
  860. {
  861.     playlist_t * p_playlist = pl_Hold( VLCIntf );
  862.     playlist_item_t * p_item;
  863.     if( [o_outline_view selectedRow] > -1 )
  864.     {
  865.         p_item = [[o_outline_view itemAtRow: [o_outline_view selectedRow]] pointerValue];
  866.     }
  867.     else
  868.     /*If no item is selected, sort the whole playlist*/
  869.     {
  870.         p_item = p_playlist->p_root_category;
  871.     }
  872.     PL_LOCK;
  873.     if( p_item->i_children > -1 ) // the item is a node
  874.     {
  875.         playlist_RecursiveNodeSort( p_playlist, p_item, i_mode, ORDER_NORMAL );
  876.     }
  877.     else
  878.     {
  879.         playlist_RecursiveNodeSort( p_playlist,
  880.                 p_item->p_parent, i_mode, ORDER_NORMAL );
  881.     }
  882.     PL_UNLOCK;
  883.     pl_Release( VLCIntf );
  884.     [self playlistUpdated];
  885. }
  886. - (input_item_t *)createItem:(NSDictionary *)o_one_item
  887. {
  888.     intf_thread_t * p_intf = VLCIntf;
  889.     playlist_t * p_playlist = pl_Hold( p_intf );
  890.     input_item_t *p_input;
  891.     int i;
  892.     BOOL b_rem = FALSE, b_dir = FALSE;
  893.     NSString *o_uri, *o_name;
  894.     NSArray *o_options;
  895.     NSURL *o_true_file;
  896.     /* Get the item */
  897.     o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
  898.     o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
  899.     o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
  900.     /* Find the name for a disc entry (i know, can you believe the trouble?) */
  901.     if( ( !o_name || [o_name isEqualToString:@""] ) && [o_uri rangeOfString: @"/dev/"].location != NSNotFound )
  902.     {
  903.         int i_count, i_index;
  904.         struct statfs *mounts = NULL;
  905.         i_count = getmntinfo (&mounts, MNT_NOWAIT);
  906.         /* getmntinfo returns a pointer to static data. Do not free. */
  907.         for( i_index = 0 ; i_index < i_count; i_index++ )
  908.         {
  909.             NSMutableString *o_temp, *o_temp2;
  910.             o_temp = [NSMutableString stringWithString: o_uri];
  911.             o_temp2 = [NSMutableString stringWithUTF8String: mounts[i_index].f_mntfromname];
  912.             [o_temp replaceOccurrencesOfString: @"/dev/rdisk" withString: @"/dev/disk" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
  913.             [o_temp2 replaceOccurrencesOfString: @"s0" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp2 length]) ];
  914.             [o_temp2 replaceOccurrencesOfString: @"s1" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp2 length]) ];
  915.             if( strstr( [o_temp fileSystemRepresentation], [o_temp2 fileSystemRepresentation] ) != NULL )
  916.             {
  917.                 o_name = [[NSFileManager defaultManager] displayNameAtPath: [NSString stringWithUTF8String:mounts[i_index].f_mntonname]];
  918.             }
  919.         }
  920.     }
  921.     if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
  922.         [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
  923.                 isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem   )
  924.     {
  925.         /* All of this is to make sure CD's play when you D&D them on VLC */
  926.         /* Converts mountpoint to a /dev file */
  927.         struct statfs *buf;
  928.         char *psz_dev;
  929.         NSMutableString *o_temp;
  930.         buf = (struct statfs *) malloc (sizeof(struct statfs));
  931.         statfs( [o_uri fileSystemRepresentation], buf );
  932.         psz_dev = strdup(buf->f_mntfromname);
  933.         o_temp = [NSMutableString stringWithUTF8String: psz_dev ];
  934.         [o_temp replaceOccurrencesOfString: @"/dev/disk" withString: @"/dev/rdisk" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
  935.         [o_temp replaceOccurrencesOfString: @"s0" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
  936.         [o_temp replaceOccurrencesOfString: @"s1" withString: @"" options:NSLiteralSearch range:NSMakeRange(0, [o_temp length]) ];
  937.         o_uri = o_temp;
  938.     }
  939.     p_input = input_item_New( p_playlist, [o_uri fileSystemRepresentation], o_name ? [o_name UTF8String] : NULL );
  940.     if( !p_input )
  941.     {
  942.         pl_Release( p_intf );
  943.         return NULL;
  944.     }
  945.     if( o_options )
  946.     {
  947.         for( i = 0; i < (int)[o_options count]; i++ )
  948.         {
  949.             input_item_AddOption( p_input, [[o_options objectAtIndex:i] UTF8String],
  950.                                   VLC_INPUT_OPTION_TRUSTED );
  951.         }
  952.     }
  953.     /* Recent documents menu */
  954.     o_true_file = [NSURL fileURLWithPath: o_uri];
  955.     if( o_true_file != nil && (BOOL)config_GetInt( p_playlist, "macosx-recentitems" ) == YES )
  956.     {
  957.         [[NSDocumentController sharedDocumentController]
  958.             noteNewRecentDocumentURL: o_true_file];
  959.     }
  960.     pl_Release( p_intf );
  961.     return p_input;
  962. }
  963. - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
  964. {
  965.     int i_item;
  966.     playlist_t * p_playlist = pl_Hold( VLCIntf );
  967.     PL_LOCK;
  968.     for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
  969.     {
  970.         input_item_t *p_input;
  971.         NSDictionary *o_one_item;
  972.         /* Get the item */
  973.         o_one_item = [o_array objectAtIndex: i_item];
  974.         p_input = [self createItem: o_one_item];
  975.         if( !p_input )
  976.         {
  977.             continue;
  978.         }
  979.         /* Add the item */
  980.         /* FIXME: playlist_AddInput() can fail */
  981.         
  982.         playlist_AddInput( p_playlist, p_input, PLAYLIST_INSERT,
  983.              i_position == -1 ? PLAYLIST_END : i_position + i_item, true,
  984.          pl_Locked );
  985.         if( i_item == 0 && !b_enqueue )
  986.         {
  987.             playlist_item_t *p_item = NULL;
  988.             playlist_item_t *p_node = NULL;
  989.             p_item = playlist_ItemGetByInput( p_playlist, p_input );
  990.             if( p_item )
  991.             {
  992.                 if( p_item->i_children == -1 )
  993.                     p_node = p_item->p_parent;
  994.                 else
  995.                 {
  996.                     p_node = p_item;
  997.                     if( p_node->i_children > 0 && p_node->pp_children[0]->i_children == -1 )
  998.                         p_item = p_node->pp_children[0];
  999.                     else
  1000.                         p_item = NULL;
  1001.                 }
  1002.                 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, p_node, p_item );
  1003.             }
  1004.         }
  1005.         vlc_gc_decref( p_input );
  1006.     }
  1007.     PL_UNLOCK;
  1008.     [self playlistUpdated];
  1009.     pl_Release( VLCIntf );
  1010. }
  1011. - (void)appendNodeArray:(NSArray*)o_array inNode:(playlist_item_t *)p_node atPos:(int)i_position enqueue:(BOOL)b_enqueue
  1012. {
  1013.     int i_item;
  1014.     playlist_t * p_playlist = pl_Hold( VLCIntf );
  1015.     for( i_item = 0; i_item < (int)[o_array count]; i_item++ )
  1016.     {
  1017.         input_item_t *p_input;
  1018.         NSDictionary *o_one_item;
  1019.         /* Get the item */
  1020.         o_one_item = [o_array objectAtIndex: i_item];
  1021.         p_input = [self createItem: o_one_item];
  1022.         if( !p_input ) continue;
  1023.         /* Add the item */
  1024.         /* FIXME: playlist_BothAddInput() can fail */
  1025.         PL_LOCK;
  1026.         playlist_BothAddInput( p_playlist, p_input, p_node,
  1027.                                       PLAYLIST_INSERT,
  1028.                                       i_position == -1 ?
  1029.                                       PLAYLIST_END : i_position + i_item,
  1030.                                       NULL, NULL, pl_Locked );
  1031.         if( i_item == 0 && !b_enqueue )
  1032.         {
  1033.             playlist_item_t *p_item;
  1034.             p_item = playlist_ItemGetByInput( p_playlist, p_input );
  1035.             playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, p_node, p_item );
  1036.         }
  1037.         PL_UNLOCK;
  1038.         vlc_gc_decref( p_input );
  1039.     }
  1040.     [self playlistUpdated];
  1041.     pl_Release( VLCIntf );
  1042. }
  1043. - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
  1044. {
  1045.     playlist_t *p_playlist = pl_Hold( VLCIntf );
  1046.     playlist_item_t *p_selected_item;
  1047.     int i_current, i_selected_row;
  1048.     i_selected_row = [o_outline_view selectedRow];
  1049.     if (i_selected_row < 0)
  1050.         i_selected_row = 0;
  1051.     p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow:
  1052.                                             i_selected_row] pointerValue];
  1053.     for( i_current = 0; i_current < p_item->i_children ; i_current++ )
  1054.     {
  1055.         char *psz_temp;
  1056.         NSString *o_current_name, *o_current_author;
  1057.         PL_LOCK;
  1058.         o_current_name = [NSString stringWithUTF8String:
  1059.             p_item->pp_children[i_current]->p_input->psz_name];
  1060.         psz_temp = input_item_GetInfo( p_item->p_input ,
  1061.                    _("Meta-information"),_("Artist") );
  1062.         o_current_author = [NSString stringWithUTF8String: psz_temp];
  1063.         free( psz_temp);
  1064.         PL_UNLOCK;
  1065.         if( p_selected_item == p_item->pp_children[i_current] &&
  1066.                     b_selected_item_met == NO )
  1067.         {
  1068.             b_selected_item_met = YES;
  1069.         }
  1070.         else if( p_selected_item == p_item->pp_children[i_current] &&
  1071.                     b_selected_item_met == YES )
  1072.         {
  1073.             pl_Release( VLCIntf );
  1074.             return NULL;
  1075.         }
  1076.         else if( b_selected_item_met == YES &&
  1077.                     ( [o_current_name rangeOfString:[o_search_field
  1078.                         stringValue] options:NSCaseInsensitiveSearch].length ||
  1079.                       [o_current_author rangeOfString:[o_search_field
  1080.                         stringValue] options:NSCaseInsensitiveSearch].length ) )
  1081.         {
  1082.             pl_Release( VLCIntf );
  1083.             /*Adds the parent items in the result array as well, so that we can
  1084.             expand the tree*/
  1085.             return [NSMutableArray arrayWithObject: [NSValue
  1086.                             valueWithPointer: p_item->pp_children[i_current]]];
  1087.         }
  1088.         if( p_item->pp_children[i_current]->i_children > 0 )
  1089.         {
  1090.             id o_result = [self subSearchItem:
  1091.                                             p_item->pp_children[i_current]];
  1092.             if( o_result != NULL )
  1093.             {
  1094.                 pl_Release( VLCIntf );
  1095.                 [o_result insertObject: [NSValue valueWithPointer:
  1096.                                 p_item->pp_children[i_current]] atIndex:0];
  1097.                 return o_result;
  1098.             }
  1099.         }
  1100.     }
  1101.     pl_Release( VLCIntf );
  1102.     return NULL;
  1103. }
  1104. - (IBAction)searchItem:(id)sender
  1105. {
  1106.     playlist_t * p_playlist = pl_Hold( VLCIntf );
  1107.     id o_result;
  1108.     unsigned int i;
  1109.     int i_row = -1;
  1110.     b_selected_item_met = NO;
  1111.         /*First, only search after the selected item:*
  1112.          *(b_selected_item_met = NO)                 */
  1113.     o_result = [self subSearchItem:p_playlist->p_root_category];
  1114.     if( o_result == NULL )
  1115.     {
  1116.         /* If the first search failed, search again from the beginning */
  1117.         o_result = [self subSearchItem:p_playlist->p_root_category];
  1118.     }
  1119.     if( o_result != NULL )
  1120.     {
  1121.         int i_start;
  1122.         if( [[o_result objectAtIndex: 0] pointerValue] ==
  1123.                                                     p_playlist->p_local_category )
  1124.         i_start = 1;
  1125.         else
  1126.         i_start = 0;
  1127.         for( i = i_start ; i < [o_result count] - 1 ; i++ )
  1128.         {
  1129.             [o_outline_view expandItem: [o_outline_dict objectForKey:
  1130.                         [NSString stringWithFormat: @"%p",
  1131.                         [[o_result objectAtIndex: i] pointerValue]]]];
  1132.         }
  1133.         i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
  1134.                         [NSString stringWithFormat: @"%p",
  1135.                         [[o_result objectAtIndex: [o_result count] - 1 ]
  1136.                         pointerValue]]]];
  1137.     }
  1138.     if( i_row > -1 )
  1139.     {
  1140.         [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:i_row] byExtendingSelection:NO];
  1141.         [o_outline_view scrollRowToVisible: i_row];
  1142.     }
  1143.     pl_Release( VLCIntf );
  1144. }
  1145. - (IBAction)recursiveExpandNode:(id)sender
  1146. {
  1147.     id o_item = [o_outline_view itemAtRow: [o_outline_view selectedRow]];
  1148.     playlist_item_t *p_item = (playlist_item_t *)[o_item pointerValue];
  1149.     if( ![[o_outline_view dataSource] outlineView: o_outline_view
  1150.                                                     isItemExpandable: o_item] )
  1151.     {
  1152.         o_item = [o_outline_dict objectForKey: [NSString
  1153.                    stringWithFormat: @"%p", p_item->p_parent]];
  1154.     }
  1155.     /* We need to collapse the node first, since OSX refuses to recursively
  1156.        expand an already expanded node, even if children nodes are collapsed. */
  1157.     [o_outline_view collapseItem: o_item collapseChildren: YES];
  1158.     [o_outline_view expandItem: o_item expandChildren: YES];
  1159. }
  1160. - (NSMenu *)menuForEvent:(NSEvent *)o_event
  1161. {
  1162.     NSPoint pt;
  1163.     bool b_rows;
  1164.     bool b_item_sel;
  1165.     pt = [o_outline_view convertPoint: [o_event locationInWindow]
  1166.                                                  fromView: nil];
  1167.     int row = [o_outline_view rowAtPoint:pt];
  1168.     if( row != -1 )
  1169.         [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
  1170.     b_item_sel = ( row != -1 && [o_outline_view selectedRow] != -1 );
  1171.     b_rows = [o_outline_view numberOfRows] != 0;
  1172.     [o_mi_play setEnabled: b_item_sel];
  1173.     [o_mi_delete setEnabled: b_item_sel];
  1174.     [o_mi_selectall setEnabled: b_rows];
  1175.     [o_mi_info setEnabled: b_item_sel];
  1176.     [o_mi_preparse setEnabled: b_item_sel];
  1177.     [o_mi_recursive_expand setEnabled: b_item_sel];
  1178.     [o_mi_sort_name setEnabled: b_item_sel];
  1179.     [o_mi_sort_author setEnabled: b_item_sel];
  1180.     return( o_ctx_menu );
  1181. }
  1182. - (void)outlineView: (NSOutlineView *)o_tv
  1183.                   didClickTableColumn:(NSTableColumn *)o_tc
  1184. {
  1185.     int i_mode, i_type = 0;
  1186.     intf_thread_t *p_intf = VLCIntf;
  1187.     playlist_t *p_playlist = pl_Hold( p_intf );
  1188.     /* Check whether the selected table column header corresponds to a
  1189.        sortable table column*/
  1190.     if( !( o_tc == o_tc_name || o_tc == o_tc_author ) )
  1191.     {
  1192.         pl_Release( p_intf );
  1193.         return;
  1194.     }
  1195.     if( o_tc_sortColumn == o_tc )
  1196.     {
  1197.         b_isSortDescending = !b_isSortDescending;
  1198.     }
  1199.     else
  1200.     {
  1201.         b_isSortDescending = false;
  1202.     }
  1203.     if( o_tc == o_tc_name )
  1204.     {
  1205.         i_mode = SORT_TITLE;
  1206.     }
  1207.     else if( o_tc == o_tc_author )
  1208.     {
  1209.         i_mode = SORT_ARTIST;
  1210.     }
  1211.     if( b_isSortDescending )
  1212.     {
  1213.         i_type = ORDER_REVERSE;
  1214.     }
  1215.     else
  1216.     {
  1217.         i_type = ORDER_NORMAL;
  1218.     }
  1219.     PL_LOCK;
  1220.     playlist_RecursiveNodeSort( p_playlist, p_playlist->p_root_category, i_mode, i_type );
  1221.     PL_UNLOCK;
  1222.     pl_Release( p_intf );
  1223.     [self playlistUpdated];
  1224.     o_tc_sortColumn = o_tc;
  1225.     [o_outline_view setHighlightedTableColumn:o_tc];
  1226.     if( b_isSortDescending )
  1227.     {
  1228.         [o_outline_view setIndicatorImage:o_descendingSortingImage
  1229.                                                         inTableColumn:o_tc];
  1230.     }
  1231.     else
  1232.     {
  1233.         [o_outline_view setIndicatorImage:o_ascendingSortingImage
  1234.                                                         inTableColumn:o_tc];
  1235.     }
  1236. }
  1237. - (void)outlineView:(NSOutlineView *)outlineView
  1238.                                 willDisplayCell:(id)cell
  1239.                                 forTableColumn:(NSTableColumn *)tableColumn
  1240.                                 item:(id)item
  1241. {
  1242.     playlist_t *p_playlist = pl_Hold( VLCIntf );
  1243.     id o_playing_item;
  1244.     PL_LOCK;
  1245.     o_playing_item = [o_outline_dict objectForKey:
  1246.                 [NSString stringWithFormat:@"%p",  playlist_CurrentPlayingItem( p_playlist )]];
  1247.     PL_UNLOCK;
  1248.     if( [self isItem: [o_playing_item pointerValue] inNode:
  1249.                         [item pointerValue] checkItemExistence: YES]
  1250.                         || [o_playing_item isEqual: item] )
  1251.     {
  1252.         [cell setFont: [[NSFontManager sharedFontManager] convertFont:[cell font] toHaveTrait:NSBoldFontMask]];
  1253.     }
  1254.     else
  1255.     {
  1256.         [cell setFont: [[NSFontManager sharedFontManager] convertFont:[cell font] toNotHaveTrait:NSBoldFontMask]];
  1257.     }
  1258.     pl_Release( VLCIntf );
  1259. }
  1260. - (IBAction)addNode:(id)sender
  1261. {
  1262.     playlist_t * p_playlist = pl_Hold( VLCIntf );
  1263.     vlc_thread_set_priority( p_playlist, VLC_THREAD_PRIORITY_LOW );
  1264.     PL_LOCK;
  1265.     playlist_NodeCreate( p_playlist, _("Empty Folder"),
  1266.                                       p_playlist->p_local_category, 0, NULL );
  1267.     PL_UNLOCK;
  1268.     pl_Release( VLCIntf );
  1269.     [self playlistUpdated];
  1270. }
  1271. @end
  1272. @implementation VLCPlaylist (NSOutlineViewDataSource)
  1273. - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
  1274. {
  1275.     id o_value = [super outlineView: outlineView child: index ofItem: item];
  1276.     playlist_t *p_playlist = pl_Hold( VLCIntf );
  1277.     PL_LOCK;
  1278.     if( playlist_CurrentSize( p_playlist )  >= 2 )
  1279.     {
  1280.         [o_status_field setStringValue: [NSString stringWithFormat:
  1281.                     _NS("%i items"),
  1282.              playlist_CurrentSize( p_playlist )]];
  1283.     }
  1284.     else
  1285.     {
  1286.         if( playlist_IsEmpty( p_playlist ) )
  1287.         {
  1288.             [o_status_field setStringValue: _NS("No items in the playlist")];
  1289.         }
  1290.         else
  1291.         {
  1292.             [o_status_field setStringValue: _NS("1 item")];
  1293.         }
  1294.     }
  1295.     PL_UNLOCK;
  1296.     pl_Release( VLCIntf );
  1297.     [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p",
  1298.                                                     [o_value pointerValue]]];
  1299.     return o_value;
  1300. }
  1301. /* Required for drag & drop and reordering */
  1302. - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
  1303. {
  1304.     unsigned int i;
  1305.     playlist_t *p_playlist = pl_Hold( VLCIntf );
  1306.     /* First remove the items that were moved during the last drag & drop
  1307.        operation */
  1308.     [o_items_array removeAllObjects];
  1309.     [o_nodes_array removeAllObjects];
  1310.     for( i = 0 ; i < [items count] ; i++ )
  1311.     {
  1312.         id o_item = [items objectAtIndex: i];
  1313.         /* Refuse to move items that are not in the General Node
  1314.            (Service Discovery) */
  1315.         if( ![self isItem: [o_item pointerValue] inNode:
  1316.                         p_playlist->p_local_category checkItemExistence: NO] &&
  1317.             var_CreateGetBool( p_playlist, "media-library" ) &&
  1318.             ![self isItem: [o_item pointerValue] inNode:
  1319.                         p_playlist->p_ml_category checkItemExistence: NO] ||
  1320.             [o_item pointerValue] == p_playlist->p_local_category ||
  1321.             [o_item pointerValue] == p_playlist->p_ml_category )
  1322.         {
  1323.             pl_Release( VLCIntf );
  1324.             return NO;
  1325.         }
  1326.         /* Fill the items and nodes to move in 2 different arrays */
  1327.         if( ((playlist_item_t *)[o_item pointerValue])->i_children > 0 )
  1328.             [o_nodes_array addObject: o_item];
  1329.         else
  1330.             [o_items_array addObject: o_item];
  1331.     }
  1332.     /* Now we need to check if there are selected items that are in already
  1333.        selected nodes. In that case, we only want to move the nodes */
  1334.     [self removeItemsFrom: o_nodes_array ifChildrenOf: o_nodes_array];
  1335.     [self removeItemsFrom: o_items_array ifChildrenOf: o_nodes_array];
  1336.     /* We add the "VLCPlaylistItemPboardType" type to be able to recognize
  1337.        a Drop operation coming from the playlist. */
  1338.     [pboard declareTypes: [NSArray arrayWithObjects:
  1339.         @"VLCPlaylistItemPboardType", nil] owner: self];
  1340.     [pboard setData:[NSData data] forType:@"VLCPlaylistItemPboardType"];
  1341.     pl_Release( VLCIntf );
  1342.     return YES;
  1343. }
  1344. - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)index
  1345. {
  1346.     playlist_t *p_playlist = pl_Hold( VLCIntf );
  1347.     NSPasteboard *o_pasteboard = [info draggingPasteboard];
  1348.     if( !p_playlist ) return NSDragOperationNone;
  1349.     /* Dropping ON items is not allowed if item is not a node */
  1350.     if( item )
  1351.     {
  1352.         if( index == NSOutlineViewDropOnItemIndex &&
  1353.                 ((playlist_item_t *)[item pointerValue])->i_children == -1 )
  1354.         {
  1355.             pl_Release( VLCIntf );
  1356.             return NSDragOperationNone;
  1357.         }
  1358.     }
  1359.     /* Don't allow on drop on playlist root element's child */
  1360.     if( !item && index != NSOutlineViewDropOnItemIndex)
  1361.     {
  1362.         pl_Release( VLCIntf );
  1363.         return NSDragOperationNone;
  1364.     }
  1365.     /* We refuse to drop an item in anything else than a child of the General
  1366.        Node. We still accept items that would be root nodes of the outlineview
  1367.        however, to allow drop in an empty playlist. */
  1368.     if( !( ([self isItem: [item pointerValue] inNode: p_playlist->p_local_category checkItemExistence: NO] || 
  1369.         ( var_CreateGetBool( p_playlist, "media-library" ) && [self isItem: [item pointerValue] inNode: p_playlist->p_ml_category checkItemExistence: NO] ) ) || item == nil ) )
  1370.     {
  1371.         pl_Release( VLCIntf );
  1372.         return NSDragOperationNone;
  1373.     }
  1374.     /* Drop from the Playlist */
  1375.     if( [[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] )
  1376.     {
  1377.         unsigned int i;
  1378.         for( i = 0 ; i < [o_nodes_array count] ; i++ )
  1379.         {
  1380.             /* We refuse to Drop in a child of an item we are moving */
  1381.             if( [self isItem: [item pointerValue] inNode:
  1382.                     [[o_nodes_array objectAtIndex: i] pointerValue]
  1383.                     checkItemExistence: NO] )
  1384.             {
  1385.                 pl_Release( VLCIntf );
  1386.                 return NSDragOperationNone;
  1387.             }
  1388.         }
  1389.         pl_Release( VLCIntf );
  1390.         return NSDragOperationMove;
  1391.     }
  1392.     /* Drop from the Finder */
  1393.     else if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
  1394.     {
  1395.         pl_Release( VLCIntf );
  1396.         return NSDragOperationGeneric;
  1397.     }
  1398.     pl_Release( VLCIntf );
  1399.     return NSDragOperationNone;
  1400. }
  1401. - (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(NSInteger)index
  1402. {
  1403.     playlist_t * p_playlist =  pl_Hold( VLCIntf );
  1404.     NSPasteboard *o_pasteboard = [info draggingPasteboard];
  1405.     /* Drag & Drop inside the playlist */
  1406.     if( [[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] )
  1407.     {
  1408.         int i_row, i_removed_from_node = 0;
  1409.         unsigned int i;
  1410.         playlist_item_t *p_new_parent, *p_item = NULL;
  1411.         NSArray *o_all_items = [o_nodes_array arrayByAddingObjectsFromArray:
  1412.                                                                 o_items_array];
  1413.         /* If the item is to be dropped as root item of the outline, make it a
  1414.            child of the General node.
  1415.            Else, choose the proposed parent as parent. */
  1416.         if( item == nil ) p_new_parent = p_playlist->p_local_category;
  1417.         else p_new_parent = [item pointerValue];
  1418.         /* Make sure the proposed parent is a node.
  1419.            (This should never be true) */
  1420.         if( p_new_parent->i_children < 0 )
  1421.         {
  1422.             pl_Release( VLCIntf );
  1423.             return NO;
  1424.         }
  1425.         for( i = 0; i < [o_all_items count]; i++ )
  1426.         {
  1427.             playlist_item_t *p_old_parent = NULL;
  1428.             int i_old_index = 0;
  1429.             p_item = [[o_all_items objectAtIndex:i] pointerValue];
  1430.             p_old_parent = p_item->p_parent;
  1431.             if( !p_old_parent )
  1432.             continue;
  1433.             /* We may need the old index later */
  1434.             if( p_new_parent == p_old_parent )
  1435.             {
  1436.                 int j;
  1437.                 for( j = 0; j < p_old_parent->i_children; j++ )
  1438.                 {
  1439.                     if( p_old_parent->pp_children[j] == p_item )
  1440.                     {
  1441.                         i_old_index = j;
  1442.                         break;
  1443.                     }
  1444.                 }
  1445.             }
  1446.             PL_LOCK;
  1447.             // Actually detach the item from the old position
  1448.             if( playlist_NodeRemoveItem( p_playlist, p_item, p_old_parent ) ==
  1449.                 VLC_SUCCESS )
  1450.             {
  1451.                 int i_new_index;
  1452.                 /* Calculate the new index */
  1453.                 if( index == -1 )
  1454.                 i_new_index = -1;
  1455.                 /* If we move the item in the same node, we need to take into
  1456.                    account that one item will be deleted */
  1457.                 else
  1458.                 {
  1459.                     if ((p_new_parent == p_old_parent &&
  1460.                                    i_old_index < index + (int)i) )
  1461.                     {
  1462.                         i_removed_from_node++;
  1463.                     }
  1464.                     i_new_index = index + i - i_removed_from_node;
  1465.                 }
  1466.                 // Reattach the item to the new position
  1467.                 playlist_NodeInsert( p_playlist, p_item, p_new_parent, i_new_index );
  1468.             }
  1469.             PL_UNLOCK;
  1470.         }
  1471.         [self playlistUpdated];
  1472.         i_row = [o_outline_view rowForItem:[o_outline_dict
  1473.             objectForKey:[NSString stringWithFormat: @"%p",
  1474.             [[o_all_items objectAtIndex: 0] pointerValue]]]];
  1475.         if( i_row == -1 )
  1476.         {
  1477.             i_row = [o_outline_view rowForItem:[o_outline_dict
  1478.             objectForKey:[NSString stringWithFormat: @"%p", p_new_parent]]];
  1479.         }
  1480.         [o_outline_view deselectAll: self];
  1481.         [o_outline_view selectRowIndexes:[NSIndexSet indexSetWithIndex:i_row] byExtendingSelection:NO];
  1482.         [o_outline_view scrollRowToVisible: i_row];
  1483.         pl_Release( VLCIntf );
  1484.         return YES;
  1485.     }
  1486.     else if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
  1487.     {
  1488.         int i;
  1489.         playlist_item_t *p_node = [item pointerValue];
  1490.         NSArray *o_array = [NSArray array];
  1491.         NSArray *o_values = [[o_pasteboard propertyListForType:
  1492.                                         NSFilenamesPboardType]
  1493.                                 sortedArrayUsingSelector:
  1494.                                         @selector(caseInsensitiveCompare:)];
  1495.         for( i = 0; i < (int)[o_values count]; i++)
  1496.         {
  1497.             NSDictionary *o_dic;
  1498.             o_dic = [NSDictionary dictionaryWithObject:[o_values
  1499.                         objectAtIndex:i] forKey:@"ITEM_URL"];
  1500.             o_array = [o_array arrayByAddingObject: o_dic];
  1501.         }
  1502.         if ( item == nil )
  1503.         {
  1504.             [self appendArray:o_array atPos:index enqueue: YES];
  1505.         }
  1506.         else
  1507.         {
  1508.             assert( p_node->i_children != -1 );
  1509.             [self appendNodeArray:o_array inNode: p_node
  1510.                 atPos:index enqueue:YES];
  1511.         }
  1512.         pl_Release( VLCIntf );
  1513.         return YES;
  1514.     }
  1515.     pl_Release( VLCIntf );
  1516.     return NO;
  1517. }
  1518. @end