]> git.sesse.net Git - vlc/blobdiff - modules/gui/wxwindows/playlist.cpp
wx/playlist: never store the pointer to p_item when the playlist lock isn't
[vlc] / modules / gui / wxwindows / playlist.cpp
index de8833a0e493c4e37f4b9a8fe79fd2b863d093f0..c0dc98cd55642253a77d1bebec011fc7a1ab7f87 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * playlist.cpp : wxWindows plugin for vlc
  *****************************************************************************
- * Copyright (C) 2000-2004 VideoLAN
+ * Copyright (C) 2000-2005 VideoLAN
  * $Id$
  *
  * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
 #include "bitmaps/loop.xpm"
 
 #include "bitmaps/type_unknown.xpm"
+#include "bitmaps/type_afile.xpm"
+#include "bitmaps/type_vfile.xpm"
 #include "bitmaps/type_net.xpm"
 #include "bitmaps/type_card.xpm"
 #include "bitmaps/type_disc.xpm"
+#include "bitmaps/type_cdda.xpm"
 #include "bitmaps/type_directory.xpm"
 #include "bitmaps/type_playlist.xpm"
+#include "bitmaps/type_node.xpm"
 
 #include <wx/dynarray.h>
 #include <wx/imaglist.h>
 
 #define HELP_SHUFFLE N_( "Shuffle" )
-#define HELP_LOOP N_( "Loop" )
-#define HELP_REPEAT N_( "Repeat" )
+#define HELP_LOOP N_( "Repeat All" )
+#define HELP_REPEAT N_( "Repeat One" )
 
 /* Callback prototype */
 static int PlaylistChanged( vlc_object_t *, const char *,
@@ -55,6 +59,10 @@ static int PlaylistNext( vlc_object_t *, const char *,
                          vlc_value_t, vlc_value_t, void * );
 static int ItemChanged( vlc_object_t *, const char *,
                         vlc_value_t, vlc_value_t, void * );
+static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
+                      vlc_value_t oval, vlc_value_t nval, void *param );
+static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
+                      vlc_value_t oval, vlc_value_t nval, void *param );
 
 /*****************************************************************************
  * Event Table.
@@ -65,6 +73,7 @@ enum
 {
     /* menu items */
     AddFile_Event = 1,
+    AddDir_Event,
     AddMRL_Event,
     Close_Event,
     Open_Event,
@@ -72,13 +81,8 @@ enum
 
     SortTitle_Event,
     RSortTitle_Event,
-    SortAuthor_Event,
-    RSortAuthor_Event,
     Randomize_Event,
 
-    EnableSelection_Event,
-    DisableSelection_Event,
-
     InvertSelection_Event,
     DeleteSelection_Event,
     Random_Event,
@@ -86,14 +90,11 @@ enum
     Repeat_Event,
     SelectAll_Event,
 
-    Up_Event,
-    Down_Event,
-    Infos_Event,
-
     PopupPlay_Event,
     PopupPlayThis_Event,
+    PopupPreparse_Event,
+    PopupSort_Event,
     PopupDel_Event,
-    PopupEna_Event,
     PopupInfo_Event,
 
     SearchText_Event,
@@ -106,8 +107,16 @@ enum
 
     /* custom events */
     UpdateItem_Event,
+    AppendItem_Event,
+    RemoveItem_Event,
+
+    MenuDummy_Event = wxID_HIGHEST + 999,
 
     FirstView_Event = wxID_HIGHEST + 1000,
+    LastView_Event = wxID_HIGHEST + 1100,
+
+    FirstSD_Event = wxID_HIGHEST + 2000,
+    LastSD_Event = wxID_HIGHEST + 2100,
 };
 
 DEFINE_LOCAL_EVENT_TYPE( wxEVT_PLAYLIST );
@@ -117,6 +126,7 @@ BEGIN_EVENT_TABLE(Playlist, wxFrame)
 
     /* Menu events */
     EVT_MENU(AddFile_Event, Playlist::OnAddFile)
+    EVT_MENU(AddDir_Event, Playlist::OnAddDir)
     EVT_MENU(AddMRL_Event, Playlist::OnAddMRL)
     EVT_MENU(Close_Event, Playlist::OnClose)
     EVT_MENU(Open_Event, Playlist::OnOpen)
@@ -124,17 +134,12 @@ BEGIN_EVENT_TABLE(Playlist, wxFrame)
 
     EVT_MENU(SortTitle_Event, Playlist::OnSort)
     EVT_MENU(RSortTitle_Event, Playlist::OnSort)
-    EVT_MENU(SortAuthor_Event, Playlist::OnSort)
-    EVT_MENU(RSortAuthor_Event, Playlist::OnSort)
 
     EVT_MENU(Randomize_Event, Playlist::OnSort)
 
-    EVT_MENU(EnableSelection_Event, Playlist::OnEnableSelection)
-    EVT_MENU(DisableSelection_Event, Playlist::OnDisableSelection)
     EVT_MENU(InvertSelection_Event, Playlist::OnInvertSelection)
     EVT_MENU(DeleteSelection_Event, Playlist::OnDeleteSelection)
     EVT_MENU(SelectAll_Event, Playlist::OnSelectAll)
-    EVT_MENU(Infos_Event, Playlist::OnInfos)
 
     EVT_MENU_OPEN( Playlist::OnMenuOpen )
     EVT_MENU( -1, Playlist::OnMenuEvent )
@@ -146,8 +151,9 @@ BEGIN_EVENT_TABLE(Playlist, wxFrame)
     /* Popup events */
     EVT_MENU( PopupPlay_Event, Playlist::OnPopupPlay)
     EVT_MENU( PopupPlayThis_Event, Playlist::OnPopupPlay)
+    EVT_MENU( PopupPreparse_Event, Playlist::OnPopupPreparse)
+    EVT_MENU( PopupSort_Event, Playlist::OnPopupSort)
     EVT_MENU( PopupDel_Event, Playlist::OnPopupDel)
-    EVT_MENU( PopupEna_Event, Playlist::OnPopupEna)
     EVT_MENU( PopupInfo_Event, Playlist::OnPopupInfo)
 
     /* Tree control events */
@@ -158,10 +164,6 @@ BEGIN_EVENT_TABLE(Playlist, wxFrame)
     /* Button events */
     EVT_BUTTON( Search_Event, Playlist::OnSearch)
     EVT_BUTTON( Save_Event, Playlist::OnSave)
-    EVT_BUTTON( Infos_Event, Playlist::OnInfos)
-
-    EVT_BUTTON( Up_Event, Playlist::OnUp)
-    EVT_BUTTON( Down_Event, Playlist::OnDown)
 
     EVT_TEXT(SearchText_Event, Playlist::OnSearchTextChange)
 
@@ -179,12 +181,12 @@ END_EVENT_TABLE()
 class PlaylistItem : public wxTreeItemData
 {
 public:
-    PlaylistItem( playlist_item_t *_p_item ) : wxTreeItemData()
+    PlaylistItem( playlist_item_t *p_item ) : wxTreeItemData()
     {
-        p_item = _p_item;
+        i_id = p_item->input.i_id;
     }
 protected:
-    playlist_item_t *p_item;
+    int i_id;
 friend class Playlist;
 };
 
@@ -193,7 +195,7 @@ friend class Playlist;
  *****************************************************************************/
 Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
     wxFrame( p_parent, -1, wxU(_("Playlist")), wxDefaultPosition,
-             wxSize(345,400), wxDEFAULT_FRAME_STYLE )
+             wxSize(500,300), wxDEFAULT_FRAME_STYLE )
 {
     vlc_value_t val;
 
@@ -202,14 +204,22 @@ Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
     i_update_counter = 0;
     i_sort_mode = MODE_NONE;
     b_need_update = VLC_FALSE;
+    p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
+                                                FIND_ANYWHERE );
+    if( p_playlist == NULL )
+    {
+        return;
+    }
+
     SetIcon( *p_intf->p_sys->p_icon );
 
     p_view_menu = NULL;
+    p_sd_menu = SDMenu();
 
     i_current_view = VIEW_SIMPLE;
+    b_changed_view = VLC_FALSE;
 
     i_title_sorted = 0;
-    i_author_sorted = 0;
     i_group_sorted = 0;
     i_duration_sorted = 0;
 
@@ -219,9 +229,13 @@ Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
 
     /* Create our "Manage" menu */
     wxMenu *manage_menu = new wxMenu;
-    manage_menu->Append( AddFile_Event, wxU(_("&Simple Add...")) );
+    manage_menu->Append( AddFile_Event, wxU(_("&Simple Add File...")) );
+    manage_menu->Append( AddDir_Event, wxU(_("Add &Directory...")) );
     manage_menu->Append( AddMRL_Event, wxU(_("&Add MRL...")) );
     manage_menu->AppendSeparator();
+    manage_menu->Append( MenuDummy_Event, wxU(_("Services discovery")),
+                         p_sd_menu );
+    manage_menu->AppendSeparator();
     manage_menu->Append( Open_Event, wxU(_("&Open Playlist...")) );
     manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
     manage_menu->AppendSeparator();
@@ -232,16 +246,10 @@ Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
     sort_menu->Append( SortTitle_Event, wxU(_("Sort by &title")) );
     sort_menu->Append( RSortTitle_Event, wxU(_("&Reverse sort by title")) );
     sort_menu->AppendSeparator();
-    sort_menu->Append( SortAuthor_Event, wxU(_("Sort by &author")) );
-    sort_menu->Append( RSortAuthor_Event, wxU(_("Reverse sort by author")) );
-    sort_menu->AppendSeparator();
     sort_menu->Append( Randomize_Event, wxU(_("&Shuffle Playlist")) );
 
     /* Create our "Selection" menu */
     wxMenu *selection_menu = new wxMenu;
-    selection_menu->Append( EnableSelection_Event, wxU(_("&Enable")) );
-    selection_menu->Append( DisableSelection_Event, wxU(_("&Disable")) );
-    selection_menu->AppendSeparator();
     selection_menu->Append( InvertSelection_Event, wxU(_("&Invert")) );
     selection_menu->Append( DeleteSelection_Event, wxU(_("D&elete")) );
     selection_menu->Append( SelectAll_Event, wxU(_("&Select All")) );
@@ -250,7 +258,7 @@ Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
     ViewMenu();
 
     /* Append the freshly created menus to the menu bar */
-    wxMenuBar *menubar = new wxMenuBar( wxMB_DOCKABLE );
+    wxMenuBar *menubar = new wxMenuBar();
     menubar->Append( manage_menu, wxU(_("&Manage")) );
     menubar->Append( sort_menu, wxU(_("S&ort")) );
     menubar->Append( selection_menu, wxU(_("&Selection")) );
@@ -260,12 +268,19 @@ Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
     SetMenuBar( menubar );
 
     /* Create the popup menu */
-    popup_menu = new wxMenu;
-    popup_menu->Append( PopupPlay_Event, wxU(_("Play")) );
-    popup_menu->Append( PopupPlayThis_Event, wxU(_("Play this branch")) );
-    popup_menu->Append( PopupDel_Event, wxU(_("Delete")) );
-    popup_menu->Append( PopupEna_Event, wxU(_("Enable/Disable")) );
-    popup_menu->Append( PopupInfo_Event, wxU(_("Info")) );
+    node_popup = new wxMenu;
+    node_popup->Append( PopupPlay_Event, wxU(_("Play")) );
+    node_popup->Append( PopupPlayThis_Event, wxU(_("Play this branch")) );
+    node_popup->Append( PopupPreparse_Event, wxU(_("Preparse")) );
+    node_popup->Append( PopupSort_Event, wxU(_("Sort this branch")) );
+    node_popup->Append( PopupDel_Event, wxU(_("Delete")) );
+    node_popup->Append( PopupInfo_Event, wxU(_("Info")) );
+
+    item_popup = new wxMenu;
+    item_popup->Append( PopupPlay_Event, wxU(_("Play")) );
+    item_popup->Append( PopupPreparse_Event, wxU(_("Preparse")) );
+    item_popup->Append( PopupDel_Event, wxU(_("Delete")) );
+    item_popup->Append( PopupInfo_Event, wxU(_("Info")) );
 
     /* Create a panel to put everything in */
     wxPanel *playlist_panel = new wxPanel( this, -1 );
@@ -273,7 +288,7 @@ Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
 
     /* Create the toolbar */
     wxToolBar *toolbar =
-        CreateToolBar( wxTB_HORIZONTAL | wxTB_FLAT | wxTB_DOCKABLE );
+        CreateToolBar( wxTB_HORIZONTAL | wxTB_FLAT );
 
     /* Create the random tool */
     toolbar->AddTool( Random_Event, wxT(""), wxBitmap(shuffle_on_xpm),
@@ -322,67 +337,45 @@ Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
                                wxTR_MULTIPLE | wxTR_EXTENDED );
 
     /* Create image list */
-
-    wxImageList *p_images = new wxImageList( 16 , 16, TRUE);
-
-    wxIcon icons[10];
-    icons[ ITEM_TYPE_UNKNOWN ] = wxIcon( type_unknown_xpm );
-    icons[ ITEM_TYPE_DISC ] = wxIcon( type_disc_xpm );
-    icons[ ITEM_TYPE_DIRECTORY ] = wxIcon( type_directory_xpm );
-    icons[ ITEM_TYPE_PLAYLIST ] = wxIcon( type_playlist_xpm );
-    icons[ ITEM_TYPE_NET ] = wxIcon( type_net_xpm );
-    icons[ ITEM_TYPE_CARD ] = wxIcon( type_card_xpm );
-
-    for( unsigned int i = 0; i< WXSIZEOF( icons ) ; i++ )
-    {
-         p_images->Add( wxIcon( icons[i] ));
-    }
-
+    wxImageList *p_images = new wxImageList( 16 , 16, TRUE );
+
+    /* FIXME: absolutely needs to be in the right order FIXME */
+    p_images->Add( wxIcon( type_unknown_xpm ) );
+    p_images->Add( wxIcon( type_afile_xpm ) );
+    p_images->Add( wxIcon( type_vfile_xpm ) );
+    p_images->Add( wxIcon( type_directory_xpm ) );
+    p_images->Add( wxIcon( type_disc_xpm ) );
+    p_images->Add( wxIcon( type_cdda_xpm ) );
+    p_images->Add( wxIcon( type_card_xpm ) );
+    p_images->Add( wxIcon( type_net_xpm ) );
+    p_images->Add( wxIcon( type_playlist_xpm ) );
+    p_images->Add( wxIcon( type_node_xpm ) );
     treectrl->AssignImageList( p_images );
 
     treectrl->AddRoot( wxU(_("root" )), -1, -1, NULL );
 
     /* Reduce font size */
     wxFont font= treectrl->GetFont();
-    font.SetPointSize(8);
+    font.SetPointSize(9);
     treectrl->SetFont( font );
 
-    /* Create the Up-Down buttons */
-#if 0
-    wxButton *up_button =
-        new wxButton( playlist_panel, Up_Event, wxU(_("Up") ) );
-    wxButton *down_button =
-        new wxButton( playlist_panel, Down_Event, wxU(_("Down") ) );
-
-    wxBoxSizer *updown_sizer = new wxBoxSizer( wxHORIZONTAL );
-    updown_sizer->Layout();
-    /* The top and bottom sizers */
-    wxBoxSizer *bottom_sizer = new wxBoxSizer( wxHORIZONTAL );
-    bottom_sizer->Add( up_button, 0, wxALIGN_LEFT | wxRIGHT, 3);
-    bottom_sizer->Add( down_button, 0, wxALIGN_LEFT | wxLEFT, 3);
-    bottom_sizer->Layout();
-#endif
     wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
     panel_sizer->Add( treectrl, 1, wxEXPAND | wxALL, 5 );
-#if 0
-    panel_sizer->Add( bottom_sizer, 0, wxALL, 5);
-#endif
     panel_sizer->Layout();
 
     playlist_panel->SetSizerAndFit( panel_sizer );
 
+    int pi_widths[1] =  { -1 };
+    statusbar = CreateStatusBar( 1 );
+    statusbar->SetStatusWidths( 1, pi_widths );
+
 #if wxUSE_DRAG_AND_DROP
     /* Associate drop targets with the playlist */
     SetDropTarget( new DragAndDrop( p_intf, VLC_TRUE ) );
 #endif
 
-    playlist_t *p_playlist =
-        (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                       FIND_ANYWHERE );
-    if( p_playlist == NULL )
-    {
-        return;
-    }
+    i_saved_id = 0;
+
 
     /* We want to be noticed of playlist changes */
 
@@ -395,28 +388,19 @@ Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
     /* One item has been updated */
     var_AddCallback( p_playlist, "item-change", ItemChanged, this );
 
-    vlc_object_release( p_playlist );
+    var_AddCallback( p_playlist, "item-append", ItemAppended, this );
+    var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
 
     /* Update the playlist */
-    Rebuild();
-}
+    Rebuild( VLC_TRUE );
 
-void Playlist::OnSize( wxSizeEvent& event)
-{
-#if 0
-    wxSize size = GetClientSize();
-    if( listview )
-        listview->SetColumnWidth( 0, size.x - listview->GetColumnWidth(1)
-                        - 15 /* margins */ );
-#endif
-    event.Skip();
 }
 
 Playlist::~Playlist()
 {
-    playlist_t *p_playlist =
-        (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                       FIND_ANYWHERE );
+    if( pp_sds != NULL )
+        free( pp_sds );
+
     if( p_playlist == NULL )
     {
         return;
@@ -425,14 +409,17 @@ Playlist::~Playlist()
     var_DelCallback( p_playlist, "item-change", ItemChanged, this );
     var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
     var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
+    var_DelCallback( p_playlist, "item-append", ItemAppended, this );
+    var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
     vlc_object_release( p_playlist );
 }
 
 /**********************************************************************
- * Update one playlist item
+ * Update functions
  **********************************************************************/
-void Playlist::UpdateNode( playlist_t *p_playlist, playlist_item_t *p_node,
-                           wxTreeItemId node )
+
+/* Update a node */
+void Playlist::UpdateNode( playlist_item_t *p_node, wxTreeItemId node )
 {
     long cookie;
     wxTreeItemId child;
@@ -450,26 +437,31 @@ void Playlist::UpdateNode( playlist_t *p_playlist, playlist_item_t *p_node,
         if( !child.IsOk() )
         {
             /* Not enough children */
-            CreateNode( p_playlist, p_node->pp_children[i], node );
+            CreateNode( p_node->pp_children[i], node );
             /* Keep the tree pointer up to date */
             child = treectrl->GetNextChild( node, cookie );
         }
-        else
-        {
-        }
     }
+    treectrl->SetItemImage( node, p_node->input.i_type );
 
 }
+
 /* Creates the node p_node as last child of parent */
-void Playlist::CreateNode( playlist_t *p_playlist, playlist_item_t *p_node,
-                           wxTreeItemId parent )
+void Playlist::CreateNode( playlist_item_t *p_node, wxTreeItemId parent )
 {
-    long cookie;
     wxTreeItemId node =
         treectrl->AppendItem( parent, wxL2U( p_node->input.psz_name ),
                               -1,-1, new PlaylistItem( p_node ) );
     treectrl->SetItemImage( node, p_node->input.i_type );
 
+    UpdateNodeChildren( p_node, node );
+}
+
+/* Update all children (recursively) of this node */
+void Playlist::UpdateNodeChildren( playlist_item_t *p_node,
+                                   wxTreeItemId node )
+{
+
     for( int i = 0; i< p_node->i_children ; i++ )
     {
         /* Append the item */
@@ -480,18 +472,155 @@ void Playlist::CreateNode( playlist_t *p_playlist, playlist_item_t *p_node,
                     wxL2U( p_node->pp_children[i]->input.psz_name ), -1,-1,
                            new PlaylistItem( p_node->pp_children[i]) );
 
-            treectrl->SetItemImage( item,
-                                    p_node->pp_children[i]->input.i_type );
+            UpdateTreeItem( item );
         }
         else
         {
-            CreateNode( p_playlist, p_node->pp_children[i],
-                        node );
+            CreateNode( p_node->pp_children[i], node );
+        }
+    }
+}
+
+/* Update an item in the tree */
+void Playlist::UpdateTreeItem( wxTreeItemId item )
+{
+    if( ! item.IsOk() ) return;
+
+    wxTreeItemData *p_data = treectrl->GetItemData( item );
+    if( !p_data ) return;
+    
+    LockPlaylist( p_intf->p_sys, p_playlist );
+    playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
+                                          ((PlaylistItem *)p_data)->i_id );
+    if( !p_item ) return;
+
+    wxString msg;
+    wxString duration = wxU( "" );
+    char *psz_author = vlc_input_item_GetInfo( &p_item->input,
+                                                     _("Meta-information"),
+                                                     _("Artist"));
+    if( psz_author == NULL )
+        return;
+    char psz_duration[MSTRTIME_MAX_SIZE];
+    mtime_t dur = p_item->input.i_duration;
+
+    if( dur != -1 )
+    {
+        secstotimestr( psz_duration, dur/1000000 );
+        duration.Append( wxU( " ( " ) +  wxString( wxU( psz_duration ) ) +
+                         wxU( " )" ) );
+    }
+
+    if( !strcmp( psz_author, "" ) || p_item->input.b_fixed_name == VLC_TRUE )
+    {
+        msg.Printf( wxString( wxU( p_item->input.psz_name ) ) + duration );
+    }
+    else
+    {
+        msg.Printf( wxString(wxU( psz_author )) + wxT(" - ") +
+                    wxString(wxU(p_item->input.psz_name)) + duration );
+    }
+    free( psz_author );
+    treectrl->SetItemText( item , msg );
+    treectrl->SetItemImage( item, p_item->input.i_type );
+
+    if( p_playlist->status.p_item == p_item )
+    {
+        treectrl->SetItemBold( item, true );
+        treectrl->EnsureVisible( item );
+    }
+    else
+    {
+        treectrl->SetItemBold( item, false );
+    }
+    UnlockPlaylist( p_intf->p_sys, p_playlist );
+}
+
+/* Process a AppendIt em request */
+void Playlist::AppendItem( wxCommandEvent& event )
+{
+    playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
+
+    wxTreeItemId item,node;
+
+    if( p_add->i_view != i_current_view || !p_add->p_node )
+    {
+        goto update;
+    }
+
+    node = FindItem( treectrl->GetRootItem(), p_add->p_node->input.i_id );
+    if( !node.IsOk() )
+    {
+        goto update;
+    }
+
+    item = treectrl->AppendItem( node,
+                                 wxL2U( p_add->p_item->input.psz_name ), -1,-1,
+                                 new PlaylistItem( p_add->p_item ) );
+    treectrl->SetItemImage( item, p_add->p_item->input.i_type );
+
+    if( item.IsOk() && p_add->p_item->i_children == -1 )
+    {
+        UpdateTreeItem( item );
+    }
+
+update:
+    int i_count = CountItems( treectrl->GetRootItem());
+    if( i_count != p_playlist->i_size )
+    {
+        statusbar->SetStatusText( wxString::Format( wxU(_(
+                                  "%i items in playlist (%i not shown)")),
+                                  p_playlist->i_size,
+                                  p_playlist->i_size - i_count ) );
+        if( !b_changed_view )
+        {
+            i_current_view = VIEW_CATEGORY;
+            b_changed_view = VLC_TRUE;
+            b_need_update = VLC_TRUE;
         }
     }
+    else
+    {
+        statusbar->SetStatusText( wxString::Format( wxU(_(
+                                  "%i items in playlist")),
+                                  p_playlist->i_size ), 0 );
+    }
+
+    return;
+}
+
+/* Process a updateitem request */
+void Playlist::UpdateItem( int i )
+{
+    if( i < 0 ) return; /* Sanity check */
+    
+    wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
+
+    if( item.IsOk() )
+    {
+        UpdateTreeItem( item );
+    }
+}
+
+void Playlist::RemoveItem( int i )
+{
+    if( i <= 0 ) return; /* Sanity check */
+
+    wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
+
+    if( item.IsOk() )
+    {
+        treectrl->Delete( item );
+    }
 }
 
-wxTreeItemId Playlist::FindItem( wxTreeItemId root, playlist_item_t *p_item )
+
+/**********************************************************************
+ * Search functions (internal)
+ **********************************************************************/
+
+/* Find a wxItem from a playlist id */
+wxTreeItemId Playlist::FindItem( wxTreeItemId root, int i_id )
 {
     long cookie;
     PlaylistItem *p_wxcurrent;
@@ -499,32 +628,77 @@ wxTreeItemId Playlist::FindItem( wxTreeItemId root, playlist_item_t *p_item )
     wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
     wxTreeItemId child;
 
+    p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
+
+    if( i_id < 0 )
+    {
+        wxTreeItemId dummy;
+        return dummy;
+    }
+
+    if( !p_wxcurrent )
+    {
+        wxTreeItemId dummy;
+        return dummy;
+    }        
+
+    if( p_wxcurrent->i_id == i_id )
+    {
+        return root;
+    }
+
     while( item.IsOk() )
     {
         p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
-        if( p_wxcurrent->p_item == p_item )
+        if( p_wxcurrent->i_id == i_id )
         {
             return item;
         }
         if( treectrl->ItemHasChildren( item ) )
         {
-            wxTreeItemId search = FindItem( item, p_item );
+            wxTreeItemId search = FindItem( item, i_id );
             if( search.IsOk() )
             {
                 return search;
             }
         }
-        item = treectrl->GetNextChild( root, cookie);
+        item = treectrl->GetNextChild( root, cookie );
     }
     /* Not found */
     wxTreeItemId dummy;
     return dummy;
 }
 
-/*wxTreeItemId Playlist::FindItemByName( wxTreeItemId root, wxString search_string, wxTreeItemId current )
+int Playlist::CountItems( wxTreeItemId root )
+{
+    long cookie;
+    int count = 0;
+    wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
+    
+    while( item.IsOk() )
+    {
+        if( treectrl->ItemHasChildren( item ) )
+        {
+            count += CountItems( item );
+        }
+        else
+        {
+            playlist_item_t *p_item;
+            LockPlaylist( p_intf->p_sys, p_playlist );
+            p_item = playlist_ItemGetById( p_playlist, ((PlaylistItem *)treectrl->GetItemData( item ))->i_id );
+            if( p_item && p_item->i_children == -1 )
+                count++;
+            UnlockPlaylist( p_intf->p_sys, p_playlist );
+        }
+        item = treectrl->GetNextChild( root, cookie );
+    }
+    return count;
+}
+
+/* Find a wxItem from a name (from current) */
+wxTreeItemId Playlist::FindItemByName( wxTreeItemId root, wxString search_string, wxTreeItemId current, vlc_bool_t *pb_current_found )
 {
     long cookie;
-    PlaylistItem *p_wxcurrent;
     wxTreeItemId search;
     wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
     wxTreeItemId child;
@@ -534,10 +708,19 @@ wxTreeItemId Playlist::FindItem( wxTreeItemId root, playlist_item_t *p_item )
         if( treectrl->GetItemText( item).Lower().Contains(
                                                  search_string.Lower() ) )
         {
-            return item;
+            if( !current.IsOk() || *pb_current_found == VLC_TRUE )
+            {
+                return item;
+            }
+            else if( current.IsOk() && item == current )
+            {
+                *pb_current_found = VLC_TRUE;
+            }
+        }
         if( treectrl->ItemHasChildren( item ) )
         {
-            wxTreeItemId search = FindItem( item, p_item );
+            wxTreeItemId search = FindItemByName( item, search_string, current,
+                                                  pb_current_found );
             if( search.IsOk() )
             {
                 return search;
@@ -545,160 +728,86 @@ wxTreeItemId Playlist::FindItem( wxTreeItemId root, playlist_item_t *p_item )
         }
         item = treectrl->GetNextChild( root, cookie);
     }
-  */  /* Not found */
-    /*wxTreeItemId dummy;
+    /* Not found */
+    wxTreeItemId dummy;
     return dummy;
 }
-*/
-
-
-
-void Playlist::SetCurrentItem( wxTreeItemId item )
-{
-    if( item.IsOk() )
-    {
-        treectrl->SetItemBold( item, true );
-        treectrl->EnsureVisible( item );
-    }
-}
-
-void Playlist::UpdateItem( int i )
-{
-    if( i < 0 ) return; /* Sanity check */
-    playlist_item_t *p_item;
-
-    playlist_t *p_playlist =
-        (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                       FIND_ANYWHERE );
-
-    if( p_playlist == NULL )
-    {
-        return;
-    }
-
-    p_item = playlist_ItemGetById( p_playlist, i );
-
-    wxTreeItemId item = FindItem( treectrl->GetRootItem(), p_item);
-
-    UpdateTreeItem( p_playlist, item );
-
-    vlc_object_release(p_playlist);
-
-}
-
-void Playlist::UpdateTreeItem( playlist_t *p_playlist ,wxTreeItemId item )
-{
-    playlist_item_t *p_item;
-
-   if( !item.IsOk() )
-   {
-        return;
-   }
-
-    p_item  =  ((PlaylistItem *)treectrl->GetItemData( item ))->p_item;
-
-    if( !p_item )
-    {
-        return;
-    }
-
-    wxString msg;
-    char *psz_author = playlist_ItemGetInfo( p_item, _("Meta-information"),
-                                                         _("Artist"));
-    char psz_duration[MSTRTIME_MAX_SIZE];
-    mtime_t dur = p_item->input.i_duration;
-    if( dur != -1 )
-        secstotimestr( psz_duration, dur/1000000 );
-    else
-        memcpy( psz_duration, "-:--:--", sizeof("-:--:--") );
-
-    if( !strcmp( psz_author, "" ) )
-    {
-        msg.Printf( wxString( wxL2U( p_item->input.psz_name ) ) + wxU( " ( ") +
-                    wxString(wxL2U(psz_duration ) ) + wxU( ")") );
-    }
-    else
-    {
-        msg.Printf( wxString(wxU( psz_author )) + wxT(" - ") +
-                    wxString(wxL2U(p_item->input.psz_name)) + wxU( " ( ") +
-                    wxString(wxL2U(psz_duration ) ) + wxU( ")") );
-    }
-    treectrl->SetItemText( item , msg );
-
-    if( p_playlist->status.p_item == p_item )
-    {
-        SetCurrentItem( item );
-    }
-    else
-    {
-        treectrl->SetItemBold( item, false );
-    }
-#if 0
-    if( p_item->b_enabled == VLC_FALSE )
-    {
-        wxListItem listitem;
-        listitem.m_itemId = i;
-        listitem.SetTextColour( *wxLIGHT_GREY);
-        listview->SetItem(listitem);
-    }
-#endif
-}
 
 /**********************************************************************
  * Rebuild the playlist
  **********************************************************************/
-void Playlist::Rebuild()
+void Playlist::Rebuild( vlc_bool_t b_root )
 {
     playlist_view_t *p_view;
-    playlist_t *p_playlist =
-        (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                       FIND_ANYWHERE );
-    if( p_playlist == NULL )
+
+    /* We can remove the callbacks before locking, anyway, we won't
+     * miss anything */
+    if( b_root )
     {
-        return;
-    }
+        var_DelCallback( p_playlist, "item-change", ItemChanged, this );
+        var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
+        var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
+        var_DelCallback( p_playlist, "item-append", ItemAppended, this );
+        var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
 
-    /* ...and rebuild it */
-    vlc_mutex_lock( &p_playlist->object_lock );
+        /* ...and rebuild it */
+        LockPlaylist( p_intf->p_sys, p_playlist );
+    }
 
     p_view = playlist_ViewFind( p_playlist, i_current_view ); /* FIXME */
 
     /* HACK we should really get new*/
-    msg_Dbg( p_intf, "rebuilding tree" );
     treectrl->DeleteAllItems();
     treectrl->AddRoot( wxU(_("root" )), -1, -1,
                          new PlaylistItem( p_view->p_root) );
 
     wxTreeItemId root = treectrl->GetRootItem();
-    UpdateNode( p_playlist, p_view->p_root, root );
+    UpdateNode( p_view->p_root, root );
 
-    wxTreeItemId item;
-    if( p_playlist->status.p_item != NULL )
+    int i_count = CountItems( treectrl->GetRootItem() );
+
+    if( i_count < p_playlist->i_size && !b_changed_view )
     {
-        item = FindItem( root, p_playlist->status.p_item );
+        i_current_view = VIEW_CATEGORY;
+        b_changed_view = VLC_TRUE;
+        Rebuild( VLC_FALSE );
     }
-    else if( p_playlist->status.p_node != NULL )
+    else if( i_count != p_playlist->i_size )
     {
-        item = FindItem( root, p_playlist->status.p_node );
+        statusbar->SetStatusText( wxString::Format( wxU(_(
+                                  "%i items in playlist (%i not shown)")),
+                                  p_playlist->i_size,
+                                  p_playlist->i_size - i_count ) );
     }
     else
     {
-        item = root;
+        statusbar->SetStatusText( wxString::Format( wxU(_(
+                                  "%i items in playlist")),
+                                  p_playlist->i_size ), 0 );
     }
 
-    SetCurrentItem( item );
-
-    vlc_mutex_unlock( &p_playlist->object_lock );
+    if( b_root )
+    {
+        /* Put callbacks back online */
+        var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
+        var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
+        var_AddCallback( p_playlist, "item-change", ItemChanged, this );
+        var_AddCallback( p_playlist, "item-append", ItemAppended, this );
+        var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
 
-    vlc_object_release( p_playlist );
+        UnlockPlaylist( p_intf->p_sys, p_playlist );
+    }
 }
 
+
+
 void Playlist::ShowPlaylist( bool show )
 {
-    if( show ) Rebuild();
+    if( show ) Rebuild( VLC_TRUE );
     Show( show );
 }
 
+/* This function is called on a regular basis */
 void Playlist::UpdatePlaylist()
 {
     i_update_counter++;
@@ -709,39 +818,11 @@ void Playlist::UpdatePlaylist()
     if( this->b_need_update )
     {
         this->b_need_update = VLC_FALSE;
-        Rebuild();
+        Rebuild( VLC_TRUE );
     }
 
     /* Updating the playing status every 0.5s is enough */
     if( i_update_counter % 5 ) return;
-
-    playlist_t *p_playlist =
-        (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                       FIND_ANYWHERE );
-    if( p_playlist == NULL )
-    {
-        return;
-    }
-#if 0
-    /* Update the colour of items */
-    int i_playlist_index = p_playlist->i_index;
-    if( p_intf->p_sys->i_playing != i_playlist_index )
-    {
-        wxListItem listitem;
-        listitem.m_itemId = i_playlist_index;
-        listitem.SetTextColour( *wxRED );
-        listview->SetItem( listitem );
-
-        if( p_intf->p_sys->i_playing != -1 )
-        {
-            listitem.m_itemId = p_intf->p_sys->i_playing;
-            listitem.SetTextColour( *wxBLACK );
-            listview->SetItem( listitem );
-        }
-        p_intf->p_sys->i_playing = i_playlist_index;
-    }
-#endif
-    vlc_object_release( p_playlist );
 }
 
 /*****************************************************************************
@@ -749,20 +830,16 @@ void Playlist::UpdatePlaylist()
  *****************************************************************************/
 void Playlist::DeleteItem( int item_id )
 {
-    playlist_t *p_playlist =
-        (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                       FIND_ANYWHERE );
-    if( p_playlist == NULL )
-    {
-        return;
-    }
-
-    playlist_Delete( p_playlist, item_id );
+    playlist_LockDelete( p_playlist, item_id );
+}
 
-    vlc_object_release( p_playlist );
+void Playlist::DeleteNode( playlist_item_t *p_item )
+{
+    playlist_NodeDelete( p_playlist, p_item, VLC_TRUE , VLC_FALSE );
 }
 
-void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
+
+void Playlist::OnClose( wxCloseEvent& WXUNUSED(event) )
 {
     Hide();
 }
@@ -778,19 +855,10 @@ void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
 
     wxString filter = wxT("");
 
-    playlist_t * p_playlist =
-                (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                               FIND_ANYWHERE );
-
-    if( ! p_playlist )
-    {
-        return;
-    }
     if( p_playlist->i_size == 0 )
     {
         wxMessageBox( wxU(_("Playlist is empty") ), wxU(_("Can't save")),
                       wxICON_WARNING | wxOK, this );
-        vlc_object_release( p_playlist );
         return;
     }
 
@@ -813,20 +881,10 @@ void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
         }
     }
 
-    vlc_object_release( p_playlist );
-
 }
 
 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
 {
-    playlist_t *p_playlist =
-        (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                       FIND_ANYWHERE );
-    if( p_playlist == NULL )
-    {
-        return;
-    }
-
     wxFileDialog dialog( this, wxU(_("Open playlist")), wxT(""), wxT(""),
         wxT("All playlists|*.pls;*.m3u;*.asx;*.b4s|M3U files|*.m3u"), wxOPEN );
 
@@ -834,8 +892,6 @@ void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
     {
         playlist_Import( p_playlist, dialog.GetPath().mb_str() );
     }
-
-    vlc_object_release( p_playlist );
 }
 
 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
@@ -844,57 +900,16 @@ void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
 
 }
 
-void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
+void Playlist::OnAddDir( wxCommandEvent& WXUNUSED(event) )
 {
-    p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
+    p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_DIRECTORY, 0, 0 );
 
 }
 
-/********************************************************************
- * Move functions
- ********************************************************************/
-void Playlist::OnUp( wxCommandEvent& event )
+void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
 {
-    playlist_t *p_playlist =
-        (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                       FIND_ANYWHERE );
-    if( p_playlist == NULL )
-    {
-        return;
-    }
-#if 0
-    /* We use the first selected item, so find it */
-    long i_item = listview->GetNextItem( -1, wxLIST_NEXT_ALL,
-                                         wxLIST_STATE_SELECTED);
-    if( i_item > 0 && i_item < p_playlist->i_size )
-    {
-        playlist_Move( p_playlist, i_item, i_item - 1 );
-        listview->Focus( i_item - 1 );
-    }
-#endif
-    vlc_object_release( p_playlist );
-}
+    p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
 
-void Playlist::OnDown( wxCommandEvent& event )
-{
-    playlist_t *p_playlist =
-        (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                       FIND_ANYWHERE );
-    if( p_playlist == NULL )
-    {
-        return;
-    }
-#if 0
-    /* We use the first selected item, so find it */
-    long i_item = listview->GetNextItem( -1, wxLIST_NEXT_ALL,
-                                         wxLIST_STATE_SELECTED );
-    if( i_item >= 0 && i_item < p_playlist->i_size - 1 )
-    {
-        playlist_Move( p_playlist, i_item, i_item + 2 );
-        listview->Focus( i_item + 1 );
-    }
-#endif
-    vlc_object_release( p_playlist );
 }
 
 /********************************************************************
@@ -902,38 +917,27 @@ void Playlist::OnDown( wxCommandEvent& event )
  ********************************************************************/
 void Playlist::OnSort( wxCommandEvent& event )
 {
-    playlist_t *p_playlist =
-        (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                       FIND_ANYWHERE );
-    if( p_playlist == NULL )
-    {
-        return;
-    }
+    PlaylistItem *p_wxitem;
+    p_wxitem = (PlaylistItem *)treectrl->GetItemData( treectrl->GetRootItem() );
+
+    LockPlaylist( p_intf->p_sys, p_playlist );
     switch( event.GetId() )
     {
         case SortTitle_Event:
-           playlist_SortTitle( p_playlist, ORDER_NORMAL );
-           break;
+            playlist_RecursiveNodeSort( p_playlist, playlist_ItemGetById( p_playlist, p_wxitem->i_id ),
+                                        SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
+            break;
         case RSortTitle_Event:
-           playlist_SortTitle( p_playlist, ORDER_REVERSE );
-           break;
-        case SortAuthor_Event:
-           playlist_SortAuthor(p_playlist, ORDER_NORMAL );
-           break;
-        case RSortAuthor_Event:
-           playlist_SortAuthor( p_playlist, ORDER_REVERSE );
-           break;
-        case Randomize_Event:
-           playlist_Sort( p_playlist, SORT_RANDOM, ORDER_NORMAL );
-           break;
+            playlist_RecursiveNodeSort( p_playlist, playlist_ItemGetById( p_playlist, p_wxitem->i_id ),
+                                        SORT_TITLE_NODES_FIRST, ORDER_REVERSE );
     }
-    vlc_object_release( p_playlist );
+    UnlockPlaylist( p_intf->p_sys, p_playlist );
 
-    Rebuild();
+    Rebuild( VLC_TRUE );
 }
 
 /**********************************************************************
- * Search functions
+ * Search functions (user)
  **********************************************************************/
 void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
 {
@@ -944,91 +948,30 @@ void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
 {
     wxString search_string = search_text->GetValue();
 
-    bool b_ok = false;
-    int i_current;
-    int i_first = 0 ;
-    int i_item = -1;
-}
-
-#if 0
-    for( i_current = 0; i_current < listview->GetItemCount(); i_current++ )
-    {
-        if( listview->GetItemState( i_current, wxLIST_STATE_SELECTED ) ==
-              wxLIST_STATE_SELECTED )
-        {
-            i_first = i_current;
-            break;
-        }
-    }
+    vlc_bool_t pb_found = VLC_FALSE;
 
-    if( i_first == listview->GetItemCount() )
-    {
-        i_first = -1;
-    }
+    wxTreeItemId found =
+     FindItemByName( treectrl->GetRootItem(), search_string,
+                     search_current, &pb_found );
 
-    for( i_current = i_first + 1; i_current < listview->GetItemCount();
-         i_current++ )
+    if( found.IsOk() )
     {
-        wxListItem listitem;
-        listitem.SetId( i_current );
-        listview->GetItem( listitem );
-        if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
-        {
-            i_item = i_current;
-            b_ok = true;
-            break;
-        }
-        listitem.SetColumn( 1 );
-        listview->GetItem( listitem );
-        if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
-        {
-            i_item = i_current;
-            b_ok = true;
-            break;
-        }
+        search_current = found;
+        treectrl->SelectItem( found, true );
     }
-    if( !b_ok )
+    else
     {
-        for( i_current = -1 ; i_current < i_first - 1;
-             i_current++ )
+        wxTreeItemId dummy;
+        search_current = dummy;
+        found =  FindItemByName( treectrl->GetRootItem(), search_string,
+                                 search_current, &pb_found );
+        if( found.IsOk() )
         {
-            wxListItem listitem;
-            listitem.SetId( i_current );
-            listview->GetItem( listitem );
-            if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
-            {
-                i_item = i_current;
-                b_ok = true;
-                break;
-            }
-            listitem.SetColumn( 1 );
-            listview->GetItem( listitem );
-            if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
-            {
-                i_item = i_current;
-                b_ok = true;
-                break;
-            }
+            search_current = found;
+            treectrl->SelectItem( found, true );
         }
     }
-
-    if( i_item < 0 || i_item >= listview->GetItemCount() )
-    {
-        return;
-    }
-
-    for( long item = 0; item < listview->GetItemCount(); item++ )
-    {
-        listview->Select( item, FALSE );
-    }
-
-    wxListItem listitem;
-    listitem.SetId(i_item);
-    listitem.m_state = wxLIST_STATE_SELECTED;
-    listview->Select( i_item, TRUE );
-    listview->Focus( i_item );
 }
-#endif
 
 /**********************************************************************
  * Selection functions
@@ -1039,63 +982,11 @@ void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
 
 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
 {
-    Rebuild();
-}
-
-void Playlist::OnEnableSelection( wxCommandEvent& WXUNUSED(event) )
-{
-    playlist_t *p_playlist =
-        (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                       FIND_ANYWHERE );
-    if( p_playlist == NULL )
-    {
-        return;
-    }
-#if 0
-    for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
-    {
-        if( listview->IsSelected( item ) )
-        {
-            /*XXX*/
-            playlist_Enable( p_playlist, item );
-            UpdateItem( item );
-        }
-    }
-#endif
-    vlc_object_release( p_playlist);
-}
-
-void Playlist::OnDisableSelection( wxCommandEvent& WXUNUSED(event) )
-{
-    playlist_t *p_playlist =
-        (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                       FIND_ANYWHERE );
-    if( p_playlist == NULL )
-    {
-        return;
-    }
-#if 0
-    for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
-    {
-        if( listview->IsSelected( item ) )
-        {
-            /*XXX*/
-            playlist_Disable( p_playlist, item );
-            UpdateItem( item );
-        }
-    }
-#endif
-    vlc_object_release( p_playlist);
+    Rebuild( VLC_TRUE );
 }
 
 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
 {
-#if 0
-    for( long item = 0; item < listview->GetItemCount(); item++ )
-    {
-        listview->Select( item, TRUE );
-    }
-#endif
 }
 
 /**********************************************************************
@@ -1105,74 +996,52 @@ void Playlist::OnRandom( wxCommandEvent& event )
 {
     vlc_value_t val;
     val.b_bool = event.IsChecked();
-    playlist_t *p_playlist =
-        (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                       FIND_ANYWHERE );
-    if( p_playlist == NULL )
-    {
-        return;
-    }
     var_Set( p_playlist, "random", val);
-    vlc_object_release( p_playlist );
 }
 
 void Playlist::OnLoop( wxCommandEvent& event )
 {
     vlc_value_t val;
     val.b_bool = event.IsChecked();
-    playlist_t *p_playlist =
-        (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                       FIND_ANYWHERE );
-    if( p_playlist == NULL )
-    {
-        return;
-    }
     var_Set( p_playlist, "loop", val);
-    vlc_object_release( p_playlist );
 }
 
 void Playlist::OnRepeat( wxCommandEvent& event )
 {
     vlc_value_t val;
     val.b_bool = event.IsChecked();
-    playlist_t *p_playlist =
-        (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                       FIND_ANYWHERE );
-    if( p_playlist == NULL )
-    {
-        return;
-    }
     var_Set( p_playlist, "repeat", val);
-    vlc_object_release( p_playlist );
 }
 
+/********************************************************************
+ * Event
+ ********************************************************************/
 void Playlist::OnActivateItem( wxTreeEvent& event )
 {
-    playlist_item_t *p_item,*p_node;
-    playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
-                                    VLC_OBJECT_PLAYLIST,FIND_ANYWHERE );
+    playlist_item_t *p_item,*p_node,*p_item2,*p_node2;
+
     PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
-                                                        event.GetItem() );
+                                                            event.GetItem() );
     wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
 
     PlaylistItem *p_wxparent = (PlaylistItem *)treectrl->GetItemData( parent );
 
-    if( p_playlist == NULL )
-    {
-        return;
-    }
-
-    if( p_wxitem->p_item->i_children == -1 )
+    LockPlaylist( p_intf->p_sys, p_playlist );
+    
+    p_item2 = playlist_ItemGetById(p_playlist, p_wxitem->i_id);
+    p_node2 = playlist_ItemGetById(p_playlist, p_wxparent->i_id);
+    if( p_item2 && p_item2->i_children == -1 )
     {
-        p_node = p_wxparent->p_item;
-        p_item = p_wxitem->p_item;
+        p_node = p_node2;
+        p_item = p_item2;
     }
     else
     {
-        p_node = p_wxitem->p_item;
-        if( p_wxitem->p_item->i_children > 0 )
+        p_node = p_item2;
+        if( p_node && p_node->i_children > 0 &&
+            p_node->pp_children[0]->i_children == -1)
         {
-            p_item = p_wxitem->p_item->pp_children[0];
+            p_item = p_node->pp_children[0];
         }
         else
         {
@@ -1182,8 +1051,7 @@ void Playlist::OnActivateItem( wxTreeEvent& event )
 
     playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view,
                       p_node, p_item );
-
-    vlc_object_release( p_playlist );
+    UnlockPlaylist( p_intf->p_sys, p_playlist );
 }
 
 void Playlist::OnKeyDown( wxTreeEvent& event )
@@ -1197,39 +1065,9 @@ void Playlist::OnKeyDown( wxTreeEvent& event )
     }
 }
 
-void Playlist::ShowInfos( int i_item )
-{
-}
-
-void Playlist::OnInfos( wxCommandEvent& WXUNUSED(event) )
-{
-    /* We use the first selected item, so find it */
-#if 0
-    long i_item = listview->GetNextItem( -1, wxLIST_NEXT_ALL,
-                                         wxLIST_STATE_SELECTED );
-    ShowInfos( i_item );
-#endif
-}
-
 void Playlist::OnEnDis( wxCommandEvent& event )
 {
-    playlist_t *p_playlist =
-        (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                       FIND_ANYWHERE );
-    if( p_playlist == NULL )
-    {
-        return;
-    }
-#if 0
-    long i_item = listview->GetNextItem( -1, wxLIST_NEXT_ALL,
-                                         wxLIST_STATE_SELECTED );
-
-    if( i_item >= 0 && i_item < p_playlist->i_size )
-    {
-       Rebuild();
-    }
-#endif
-    vlc_object_release( p_playlist );
+    msg_Warn( p_intf, "not implemented" );
 }
 
 /**********************************************************************
@@ -1253,55 +1091,58 @@ void Playlist::OnMenuOpen( wxMenuEvent& event)
 
 void Playlist::OnMenuEvent( wxCommandEvent& event )
 {
-    playlist_t *p_playlist =
-        (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                       FIND_ANYWHERE );
-    if( p_playlist == NULL )
-    {
-        return;
-    }
-
     if( event.GetId() < FirstView_Event )
     {
         event.Skip();
         return;
     }
+    else if( event.GetId() < LastView_Event )
+    {
 
-    int i_new_view = event.GetId() - FirstView_Event;
+        int i_new_view = event.GetId() - FirstView_Event;
 
-    playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_new_view );
+        playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_new_view );
 
-    if( p_view != NULL )
-    {
-        i_current_view = i_new_view;
-        playlist_ViewUpdate( p_playlist, i_new_view );
-        Rebuild();
-        vlc_object_release( p_playlist );
-        return;
-    }
-    else if( i_new_view >= VIEW_FIRST_SORTED && i_new_view <= VIEW_LAST_SORTED )
-    {
-        playlist_ViewInsert( p_playlist, i_new_view, "View" );
-        playlist_ViewUpdate( p_playlist, i_new_view );
+        if( p_view != NULL )
+        {
+            b_changed_view = VLC_TRUE;
+            i_current_view = i_new_view;
+            playlist_ViewUpdate( p_playlist, i_new_view );
+            Rebuild( VLC_TRUE );
+            return;
+        }
+        else if( i_new_view >= VIEW_FIRST_SORTED &&
+                 i_new_view <= VIEW_LAST_SORTED )
+        {
+            b_changed_view = VLC_TRUE;
+            playlist_ViewInsert( p_playlist, i_new_view, "View" );
+            playlist_ViewUpdate( p_playlist, i_new_view );
 
-        i_current_view = i_new_view;
+            i_current_view = i_new_view;
 
-        Rebuild();
+            Rebuild( VLC_TRUE );
+        }
+    }
+    else if( event.GetId() >= FirstSD_Event && event.GetId() < LastSD_Event )
+    {
+        if( !playlist_IsServicesDiscoveryLoaded( p_playlist,
+                                pp_sds[event.GetId() - FirstSD_Event] ) )
+        {
+            playlist_ServicesDiscoveryAdd( p_playlist,
+                            pp_sds[event.GetId() - FirstSD_Event] );
+        }
+        else
+        {
+            wxMutexGuiLeave();
+            playlist_ServicesDiscoveryRemove( p_playlist,
+                            pp_sds[event.GetId() - FirstSD_Event] );
+            wxMutexGuiEnter();
+        }
     }
-
-    vlc_object_release( p_playlist );
 }
 
 wxMenu * Playlist::ViewMenu()
 {
-    playlist_t *p_playlist =
-        (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                       FIND_ANYWHERE );
-    if( p_playlist == NULL )
-    {
-        return NULL;
-    }
-
     if( !p_view_menu )
     {
         p_view_menu = new wxMenu;
@@ -1319,24 +1160,57 @@ wxMenu * Playlist::ViewMenu()
 
     /* FIXME : have a list of "should have" views */
     p_view_menu->Append( FirstView_Event + VIEW_CATEGORY,
-                           wxU(_("By category") ) );
-    p_view_menu->Append( FirstView_Event + VIEW_SIMPLE,
+                           wxU(_("Normal") ) );
+/*    p_view_menu->Append( FirstView_Event + VIEW_SIMPLE,
                            wxU(_("Manually added") ) );
     p_view_menu->Append( FirstView_Event + VIEW_ALL,
-                           wxU(_("All items, unsorted") ) );
+                           wxU(_("All items, unsorted") ) ); */
     p_view_menu->Append( FirstView_Event + VIEW_S_AUTHOR,
-                           wxU(_("Sorted by author") ) );
-#if 0
-    for( int i = 0; i< p_playlist->i_views; i++ )
+                           wxU(_("Sorted by artist") ) );
+
+    return p_view_menu;
+}
+
+wxMenu *Playlist::SDMenu()
+{
+    p_sd_menu = new wxMenu;
+
+    vlc_list_t *p_list = vlc_list_find( p_playlist, VLC_OBJECT_MODULE,
+                                        FIND_ANYWHERE );
+
+    int i_number = 0;
+    for( int i_index = 0; i_index < p_list->i_count; i_index++ )
     {
-        p_view_menu->Append( FirstView_Event + p_playlist->pp_views[i]->i_id,
-                             wxU( p_playlist->pp_views[i]->psz_name ) );
+        module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
+
+        if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
+            i_number++;
     }
-#endif
+    if( i_number ) pp_sds = (char **)calloc( i_number, sizeof(void *) );
 
-    vlc_object_release( p_playlist);
+    i_number = 0;
+    for( int i_index = 0; i_index < p_list->i_count; i_index++ )
+    {
+        module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
 
-    return p_view_menu;
+        if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
+        {
+            p_sd_menu->AppendCheckItem( FirstSD_Event + i_number ,
+                wxU( p_parser->psz_longname ? p_parser->psz_longname :
+                     (p_parser->psz_shortname ?
+                      p_parser->psz_shortname : p_parser->psz_object_name) ) );
+
+            if( playlist_IsServicesDiscoveryLoaded( p_playlist,
+                                    p_parser->psz_object_name ) )
+            {
+                p_sd_menu->Check( FirstSD_Event + i_number, TRUE );
+            }
+
+            pp_sds[i_number++] = p_parser->psz_object_name;
+        }
+    }
+    vlc_list_release( p_list );
+    return p_sd_menu;
 }
 
 
@@ -1346,34 +1220,48 @@ wxMenu * Playlist::ViewMenu()
 void Playlist::OnPopup( wxContextMenuEvent& event )
 {
     wxPoint pt = event.GetPosition();
+    playlist_item_t *p_item;
 
-    i_popup_item = treectrl->HitTest( ScreenToClient( pt ) );
-    if( i_popup_item.IsOk() )
+    i_wx_popup_item = treectrl->HitTest( ScreenToClient( pt ) );
+    if( i_wx_popup_item.IsOk() )
     {
         PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
-                                                            i_popup_item );
-        PlaylistItem *p_wxparent= (PlaylistItem *) treectrl->GetItemData(
-                                   treectrl->GetItemParent( i_popup_item ) );
-        p_popup_item = p_wxitem->p_item;
-        p_popup_parent = p_wxparent->p_item;
-        treectrl->SelectItem( i_popup_item );
-        Playlist::PopupMenu( popup_menu,
-                             ScreenToClient( wxGetMousePosition() ) );
-    }
-    else
-    {
+                                                            i_wx_popup_item );
+        PlaylistItem *p_wxparent= (PlaylistItem *)treectrl->GetItemData(
+                                  treectrl->GetItemParent( i_wx_popup_item ) );
+        i_popup_item = p_wxitem->i_id;
+        i_popup_parent = p_wxparent->i_id;
+        treectrl->SelectItem( i_wx_popup_item );
+
+        LockPlaylist( p_intf->p_sys, p_playlist );
+        p_item = playlist_ItemGetById( p_playlist, i_popup_item );
+
+        if( !p_item )
+        {
+            UnlockPlaylist( p_intf->p_sys, p_playlist );
+            return;
+        }
+        if( p_item->i_children == -1 )
+        {
+            UnlockPlaylist( p_intf->p_sys, p_playlist );
+            Playlist::PopupMenu( item_popup,
+                                 ScreenToClient( wxGetMousePosition() ) );
+        }
+        else
+        {
+            UnlockPlaylist( p_intf->p_sys, p_playlist );
+            Playlist::PopupMenu( node_popup,
+                                 ScreenToClient( wxGetMousePosition() ) );
+        }
     }
 }
 
-void Playlist::OnPopupPlay( wxMenuEvent& event )
+void Playlist::OnPopupPlay( wxCommandEvent& event )
 {
-    playlist_t *p_playlist =
-        (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                       FIND_ANYWHERE );
-    if( p_playlist == NULL )
-    {
-        return;
-    }
+    playlist_item_t *p_popup_item, *p_popup_parent;
+    LockPlaylist( p_intf->p_sys, p_playlist );
+    p_popup_item = playlist_ItemGetById( p_playlist, i_popup_item );
+    p_popup_parent = playlist_ItemGetById( p_playlist, i_popup_parent );
     if( p_popup_item != NULL )
     {
         if( p_popup_item->i_children > -1 )
@@ -1401,52 +1289,105 @@ void Playlist::OnPopupPlay( wxMenuEvent& event )
             }
         }
     }
-    vlc_object_release( p_playlist );
+    UnlockPlaylist( p_intf->p_sys, p_playlist );
 }
 
-void Playlist::OnPopupDel( wxMenuEvent& event )
+void Playlist::OnPopupPreparse( wxCommandEvent& event )
 {
-    PlaylistItem *p_wxitem;
+    Preparse();
+}
+
+void Playlist::Preparse()
+{
+    playlist_item_t *p_popup_item;
+    LockPlaylist( p_intf->p_sys, p_playlist );
+    p_popup_item = playlist_ItemGetById( p_playlist, i_popup_item );
+    
+    if( p_popup_item != NULL )
+    {
+        if( p_popup_item->i_children == -1 )
+        {
+            playlist_PreparseEnqueue( p_playlist, &p_popup_item->input );
+        }
+        else
+        {
+            int i = 0;
+            playlist_item_t *p_parent = p_popup_item;
+            for( i = 0; i< p_parent->i_children ; i++ )
+            {
+                wxMenuEvent dummy;
+                i_wx_popup_item = FindItem( treectrl->GetRootItem(),
+                                         p_parent->pp_children[i]->input.i_id );
+                i_popup_item = p_parent->pp_children[i]->input.i_id;
+                Preparse();
+            }
+        }
+    }
+    UnlockPlaylist( p_intf->p_sys, p_playlist );
+}
 
-    p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_popup_item );
+void Playlist::OnPopupDel( wxCommandEvent& event )
+{
+    PlaylistItem *p_wxitem;
+    playlist_item_t *p_item;
 
-    if( p_wxitem->p_item->i_children == -1 )
+    p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_wx_popup_item );
+    
+    LockPlaylist( p_intf->p_sys, p_playlist );
+    p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id );
+    if( !p_item )
     {
-        DeleteItem( p_wxitem->p_item->input.i_id );
+        UnlockPlaylist( p_intf->p_sys, p_playlist );
+        return;
+    }
+    
+    if( p_item->i_children == -1 )
+    {
+        UnlockPlaylist( p_intf->p_sys, p_playlist );
+        DeleteItem( p_item->input.i_id );
     }
     else
     {
-        //DeleteNode( p_wxitem->p_item );
+        UnlockPlaylist( p_intf->p_sys, p_playlist );
+        DeleteNode( p_item );
     }
 }
 
-void Playlist::OnPopupEna( wxMenuEvent& event )
+void Playlist::OnPopupSort( wxCommandEvent& event )
 {
-    playlist_t *p_playlist =
-        (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                       FIND_ANYWHERE );
-    if( p_playlist == NULL )
-    {
-        return;
-    }
+    PlaylistItem *p_wxitem;
+    playlist_item_t *p_item;
+
+    p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_wx_popup_item );
+    LockPlaylist( p_intf->p_sys, p_playlist );
 
-    p_popup_item->b_enabled = VLC_TRUE - p_popup_item->b_enabled;
+    p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id );
+    if( p_item->i_children >= 0 )
+    {
+        playlist_RecursiveNodeSort( p_playlist, p_item,
+                                    SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
+        
+        treectrl->DeleteChildren( i_wx_popup_item );
+        UpdateNodeChildren( p_item, i_wx_popup_item );
 
-    vlc_object_release( p_playlist);
-    UpdateItem( i_popup_item );
+    }
+    UnlockPlaylist( p_intf->p_sys, p_playlist );
 }
 
-void Playlist::OnPopupInfo( wxMenuEvent& event )
+void Playlist::OnPopupInfo( wxCommandEvent& event )
 {
+    LockPlaylist( p_intf->p_sys, p_playlist );
+    playlist_item_t *p_popup_item = playlist_ItemGetById( p_playlist, i_popup_item );
     if( p_popup_item )
     {
         iteminfo_dialog = new ItemInfoDialog( p_intf, p_popup_item, this );
         if( iteminfo_dialog->ShowModal() == wxID_OK )
         {
-            UpdateItem( i_popup_item );
+            UpdateItem( i_wx_popup_item );
         }
         delete iteminfo_dialog;
     }
+    UnlockPlaylist( p_intf->p_sys, p_playlist );
 }
 
 
@@ -1457,9 +1398,15 @@ void Playlist::OnPlaylistEvent( wxCommandEvent& event )
 {
     switch( event.GetId() )
     {
-    case UpdateItem_Event:
-        UpdateItem( event.GetInt() );
-        break;
+        case UpdateItem_Event:
+            UpdateItem( event.GetInt() );
+            break;
+        case AppendItem_Event:
+            AppendItem( event );
+            break;
+        case RemoveItem_Event:
+            RemoveItem( event.GetInt() );
+            break;
     }
 }
 
@@ -1507,3 +1454,30 @@ static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
 
     return 0;
 }
+static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
+                        vlc_value_t old_val, vlc_value_t new_val, void *param )
+{
+    Playlist *p_playlist_dialog = (Playlist *)param;
+
+    wxCommandEvent event( wxEVT_PLAYLIST, RemoveItem_Event );
+    event.SetInt( new_val.i_int );
+    p_playlist_dialog->AddPendingEvent( event );
+
+    return 0;
+}
+
+static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
+                         vlc_value_t oval, vlc_value_t nval, void *param )
+{
+    Playlist *p_playlist_dialog = (Playlist *)param;
+
+    playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
+
+    memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
+
+    wxCommandEvent event( wxEVT_PLAYLIST, AppendItem_Event );
+    event.SetClientData( (void *)p_add );
+    p_playlist_dialog->AddPendingEvent( event );
+
+    return VLC_SUCCESS;
+}