playlist.cpp 55 KB
Newer Older
1
/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
2
 * playlist.cpp : wxWindows plugin for vlc
3
 *****************************************************************************
4
 * Copyright (C) 2000-2005 the VideoLAN team
Clément Stenac's avatar
Clément Stenac committed
5
 * $Id$
6
 *
7 8
 * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
 *          Clément Stenac <zorglub@videolan.org>
9
 *
10
 * This program is free software; you can redistribute it and/OR MODIFy
11 12 13
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
Gildas Bazin's avatar
 
Gildas Bazin committed
14
 *
15 16 17 18 19 20 21
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28 29
#include "dialogs/playlist.hpp"
#include "dialogs/iteminfo.hpp"
30

Clément Stenac's avatar
Clément Stenac committed
31
#include "bitmaps/shuffle.xpm"
32 33 34
#include "bitmaps/repeat.xpm"
#include "bitmaps/loop.xpm"

35
#include "bitmaps/type_unknown.xpm"
Clément Stenac's avatar
Clément Stenac committed
36 37
#include "bitmaps/type_afile.xpm"
#include "bitmaps/type_vfile.xpm"
38 39 40
#include "bitmaps/type_net.xpm"
#include "bitmaps/type_card.xpm"
#include "bitmaps/type_disc.xpm"
Clément Stenac's avatar
Clément Stenac committed
41
#include "bitmaps/type_cdda.xpm"
42 43
#include "bitmaps/type_directory.xpm"
#include "bitmaps/type_playlist.xpm"
Clément Stenac's avatar
Clément Stenac committed
44
#include "bitmaps/type_node.xpm"
45 46

#include <wx/dynarray.h>
47
#include <wx/imaglist.h>
48

49
#include <vlc_meta.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
50
#include "charset.h"
51

52
#define HELP_SHUFFLE N_( "Shuffle" )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
53 54
#define HELP_LOOP N_( "Repeat All" )
#define HELP_REPEAT N_( "Repeat One" )
55

56
namespace wxvlc {
Gildas Bazin's avatar
 
Gildas Bazin committed
57
/* Callback prototype */
58 59 60 61 62 63
static int PlaylistChanged( vlc_object_t *, const char *,
                            vlc_value_t, vlc_value_t, void * );
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 * );
64
static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
65
                         vlc_value_t oval, vlc_value_t nval, void *param );
66
static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
67
                        vlc_value_t oval, vlc_value_t nval, void *param );
68

69 70 71 72 73 74 75 76
/*****************************************************************************
 * Event Table.
 *****************************************************************************/

/* IDs for the controls and the menu commands */
enum
{
    /* menu items */
Gildas Bazin's avatar
 
Gildas Bazin committed
77
    AddFile_Event = 1,
Clément Stenac's avatar
Clément Stenac committed
78
    AddDir_Event,
Gildas Bazin's avatar
 
Gildas Bazin committed
79
    AddMRL_Event,
80
    Close_Event,
Gildas Bazin's avatar
 
Gildas Bazin committed
81 82
    Open_Event,
    Save_Event,
83

84 85
    SortTitle_Event,
    RSortTitle_Event,
Clément Stenac's avatar
Clément Stenac committed
86
    Randomize_Event,
87

88
    DeleteSelection_Event,
89 90
    Random_Event,
    Loop_Event,
91
    Repeat_Event,
92

93
    PopupPlay_Event,
94
    PopupPlayThis_Event,
95
    PopupPreparse_Event,
96
    PopupSort_Event,
97 98
    PopupDel_Event,
    PopupInfo_Event,
99
    PopupAddNode_Event,
100

Clément Stenac's avatar
Clément Stenac committed
101 102 103
    SearchText_Event,
    Search_Event,

104
    /* controls */
105
    Source_Event,
106
    TreeCtrl_Event,
107 108

    Browse_Event,  /* For export playlist */
109 110

    /* custom events */
111
    UpdateItem_Event,
112
    AppendItem_Event,
113
    RemoveItem_Event,
114

115 116
    MenuDummy_Event = wxID_HIGHEST + 999,

117
    FirstView_Event = wxID_HIGHEST + 1000,
118
    CategoryView_Event, OneLevelView_Event,
119 120 121 122
    LastView_Event = wxID_HIGHEST + 1100,

    FirstSD_Event = wxID_HIGHEST + 2000,
    LastSD_Event = wxID_HIGHEST + 2100,
123 124
};

125 126
DEFINE_LOCAL_EVENT_TYPE( wxEVT_PLAYLIST );

127
BEGIN_EVENT_TABLE(Playlist, wxFrame)
128 129
    EVT_SIZE(Playlist::OnSize)

130
    /* Menu events */
Gildas Bazin's avatar
 
Gildas Bazin committed
131
    EVT_MENU(AddFile_Event, Playlist::OnAddFile)
Clément Stenac's avatar
Clément Stenac committed
132
    EVT_MENU(AddDir_Event, Playlist::OnAddDir)
Gildas Bazin's avatar
 
Gildas Bazin committed
133
    EVT_MENU(AddMRL_Event, Playlist::OnAddMRL)
134
    EVT_MENU(Close_Event, Playlist::OnMenuClose)
Gildas Bazin's avatar
 
Gildas Bazin committed
135 136
    EVT_MENU(Open_Event, Playlist::OnOpen)
    EVT_MENU(Save_Event, Playlist::OnSave)
137 138 139 140

    EVT_MENU(SortTitle_Event, Playlist::OnSort)
    EVT_MENU(RSortTitle_Event, Playlist::OnSort)

Clément Stenac's avatar
Clément Stenac committed
141 142
    EVT_MENU(Randomize_Event, Playlist::OnSort)

143
    EVT_MENU(DeleteSelection_Event, Playlist::OnDeleteSelection)
144

145 146 147
    EVT_MENU_OPEN( Playlist::OnMenuOpen )
    EVT_MENU( -1, Playlist::OnMenuEvent )

148 149 150
    EVT_TOOL(Random_Event, Playlist::OnRandom)
    EVT_TOOL(Repeat_Event, Playlist::OnRepeat)
    EVT_TOOL(Loop_Event, Playlist::OnLoop)
151

152 153
    /* Popup events */
    EVT_MENU( PopupPlay_Event, Playlist::OnPopupPlay)
154
    EVT_MENU( PopupPlayThis_Event, Playlist::OnPopupPlay)
155
    EVT_MENU( PopupPreparse_Event, Playlist::OnPopupPreparse)
156
    EVT_MENU( PopupSort_Event, Playlist::OnPopupSort)
157 158
    EVT_MENU( PopupDel_Event, Playlist::OnPopupDel)
    EVT_MENU( PopupInfo_Event, Playlist::OnPopupInfo)
159
    EVT_MENU( PopupAddNode_Event, Playlist::OnPopupAddNode)
160

161 162
    /* Source selector */
    EVT_LIST_ITEM_SELECTED( Source_Event, Playlist::OnSourceSelected )
163 164
    /* Tree control events */
    EVT_TREE_ITEM_ACTIVATED( TreeCtrl_Event, Playlist::OnActivateItem )
165
    EVT_TREE_KEY_DOWN( -1, Playlist::OnKeyDown )
166 167
    EVT_TREE_BEGIN_DRAG( TreeCtrl_Event, Playlist::OnDragItemBegin )
    EVT_TREE_END_DRAG( TreeCtrl_Event, Playlist::OnDragItemEnd )
168 169

    EVT_CONTEXT_MENU( Playlist::OnPopup )
170 171

    /* Button events */
Clément Stenac's avatar
Clément Stenac committed
172
    EVT_BUTTON( Search_Event, Playlist::OnSearch)
Gildas Bazin's avatar
 
Gildas Bazin committed
173
    EVT_BUTTON( Save_Event, Playlist::OnSave)
174

175 176
    /*EVT_TEXT( SearchText_Event, Playlist::OnSearchTextChange )*/
    EVT_TEXT_ENTER( SearchText_Event, Playlist::OnSearch )
Clément Stenac's avatar
Clément Stenac committed
177

178 179 180
    /* Custom events */
    EVT_COMMAND(-1, wxEVT_PLAYLIST, Playlist::OnPlaylistEvent)

181 182 183 184 185
    /* Special events : we don't want to destroy the window when the user
     * clicks on (X) */
    EVT_CLOSE(Playlist::OnClose)
END_EVENT_TABLE()

186 187 188 189 190 191
/*****************************************************************************
 * PlaylistItem class
 ****************************************************************************/
class PlaylistItem : public wxTreeItemData
{
public:
192
    PlaylistItem( playlist_item_t *p_item ) : wxTreeItemData()
193
    {
194 195
        i_id = p_item->i_id;
        i_input_id = p_item->p_input->i_id;
196 197
    }
protected:
198
    int i_input_id;
199
    int i_id;
200
friend class Playlist;
201
friend class PlaylistFileDropTarget;
202
};
203

204 205 206
/*****************************************************************************
 * Constructor.
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
207 208
Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
    wxFrame( p_parent, -1, wxU(_("Playlist")), wxDefaultPosition,
209
             wxSize(500,300), wxDEFAULT_FRAME_STYLE )
210
{
Gildas Bazin's avatar
 
Gildas Bazin committed
211 212
    vlc_value_t val;

213 214
    /* Initializations */
    p_intf = _p_intf;
215
    pp_sds = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
216
    i_update_counter = 0;
217
    i_sort_mode = MODE_NONE;
Gildas Bazin's avatar
 
Gildas Bazin committed
218
    b_need_update = VLC_FALSE;
219
    i_items_to_append = 0;
220 221
    p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
                                                FIND_ANYWHERE );
222
    if( p_playlist == NULL ) return;
223

Gildas Bazin's avatar
 
Gildas Bazin committed
224
    SetIcon( *p_intf->p_sys->p_icon );
225

226
    p_view_menu = NULL;
227
    p_sd_menu = SDMenu();
228

229 230 231
//    i_current_view = VIEW_ONELEVEL;
    p_current_viewroot = p_playlist->p_root_onelevel;
    p_current_treeroot = p_playlist->p_local_onelevel;
232

233 234
    i_title_sorted = 0;
    i_group_sorted = 0;
235
    i_duration_sorted = 0;
236

Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
237 238 239
    var_Create( p_intf, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_intf, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_intf, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );;
240

241 242
    /* Create our "Manage" menu */
    wxMenu *manage_menu = new wxMenu;
Clément Stenac's avatar
Clément Stenac committed
243 244
    manage_menu->Append( AddFile_Event, wxU(_("&Simple Add File...")) );
    manage_menu->Append( AddDir_Event, wxU(_("Add &Directory...")) );
245
    manage_menu->Append( AddMRL_Event, wxU(_("&Add URL...")) );
246
    manage_menu->AppendSeparator();
247
    manage_menu->Append( MenuDummy_Event, wxU(_("Services Discovery")),
248 249
                         p_sd_menu );
    manage_menu->AppendSeparator();
Gildas Bazin's avatar
 
Gildas Bazin committed
250 251
    manage_menu->Append( Open_Event, wxU(_("&Open Playlist...")) );
    manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
252
    manage_menu->AppendSeparator();
Gildas Bazin's avatar
 
Gildas Bazin committed
253
    manage_menu->Append( Close_Event, wxU(_("&Close")) );
254

255 256
    /* Create our "Sort" menu */
    wxMenu *sort_menu = new wxMenu;
257 258
    sort_menu->Append( SortTitle_Event, wxU(_("Sort by &Title")) );
    sort_menu->Append( RSortTitle_Event, wxU(_("&Reverse Sort by Title")) );
259
    sort_menu->AppendSeparator();
260
    sort_menu->Append( Randomize_Event, wxU(_("&Shuffle")) );
261

262 263
    /* Create our "Selection" menu */
    wxMenu *selection_menu = new wxMenu;
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
264
    selection_menu->Append( DeleteSelection_Event, wxU(_("D&elete")) );
265

266 267
    /* Create our "View" menu */
    ViewMenu();
268

269
    /* Append the freshly created menus to the menu bar */
270
    wxMenuBar *menubar = new wxMenuBar();
Gildas Bazin's avatar
 
Gildas Bazin committed
271
    menubar->Append( manage_menu, wxU(_("&Manage")) );
272
    menubar->Append( sort_menu, wxU(_("S&ort")) );
Gildas Bazin's avatar
 
Gildas Bazin committed
273
    menubar->Append( selection_menu, wxU(_("&Selection")) );
274
    menubar->Append( p_view_menu, wxU(_("&View items") ) );
275 276 277 278

    /* Attach the menu bar to the frame */
    SetMenuBar( menubar );

279
    /* Create the popup menu */
280 281
    node_popup = new wxMenu;
    node_popup->Append( PopupPlay_Event, wxU(_("Play")) );
282
    node_popup->Append( PopupPlayThis_Event, wxU(_("Play this Branch")) );
283
    node_popup->Append( PopupPreparse_Event, wxU(_("Preparse")) );
284
    node_popup->Append( PopupSort_Event, wxU(_("Sort this Branch")) );
285 286
    node_popup->Append( PopupDel_Event, wxU(_("Delete")) );
    node_popup->Append( PopupInfo_Event, wxU(_("Info")) );
287
    node_popup->Append( PopupAddNode_Event, wxU(_("Add Node")) );
288 289 290 291 292 293

    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")) );
294

Gildas Bazin's avatar
 
Gildas Bazin committed
295 296 297 298
    /* Create a panel to put everything in */
    wxPanel *playlist_panel = new wxPanel( this, -1 );
    playlist_panel->SetAutoLayout( TRUE );

299 300
    /* Create the toolbar */
    wxToolBar *toolbar =
301
        CreateToolBar( wxTB_HORIZONTAL | wxTB_FLAT );
302 303 304 305 306

    /* Create the random tool */
    toolbar->AddTool( Random_Event, wxT(""), wxBitmap(shuffle_on_xpm),
                       wxBitmap(shuffle_on_xpm), wxITEM_CHECK,
                       wxU(_(HELP_SHUFFLE) ) );
Gildas Bazin's avatar
 
Gildas Bazin committed
307
    var_Get( p_intf, "random", &val );
308
    toolbar->ToggleTool( Random_Event, val.b_bool );
309

310 311 312 313
    /* Create the Loop tool */
    toolbar->AddTool( Loop_Event, wxT(""), wxBitmap( loop_xpm),
                      wxBitmap( loop_xpm), wxITEM_CHECK,
                      wxU(_(HELP_LOOP )  ) );
314
    var_Get( p_intf, "loop", &val );
315
    toolbar->ToggleTool( Loop_Event, val.b_bool );
316

317
    /* Create the Repeat one checkbox */
318 319 320
    toolbar->AddTool( Repeat_Event, wxT(""), wxBitmap( repeat_xpm),
                      wxBitmap( repeat_xpm), wxITEM_CHECK,
                      wxU(_(HELP_REPEAT )  ) );
321
    var_Get( p_intf, "repeat", &val );
322
    toolbar->ToggleTool( Repeat_Event, val.b_bool ) ;
323

Clément Stenac's avatar
Clément Stenac committed
324
    /* Create the Search Textbox */
325 326 327
    search_text = new wxTextCtrl( toolbar, SearchText_Event, wxT(""),
                                  wxDefaultPosition, wxSize(100, -1),
                                  wxTE_PROCESS_ENTER);
Clément Stenac's avatar
Clément Stenac committed
328 329

    /* Create the search button */
330
    search_button = new wxButton( toolbar , Search_Event, wxU(_("Search")) );
331

332 333
    toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
                         wxSize(16, 16), wxBORDER_NONE ) );
334
    toolbar->AddControl( search_text );
335 336
    toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
                         wxSize(5, 5), wxBORDER_NONE ) );
337
    toolbar->AddControl( search_button );
338
    search_button->SetDefault();
339
    toolbar->Realize();
Clément Stenac's avatar
Clément Stenac committed
340

341
    /* Create teh source selector */
342 343 344
    source_sel = new wxListView( playlist_panel, Source_Event,
                                 wxDefaultPosition, wxDefaultSize,
                                 wxLC_AUTOARRANGE|wxLC_SINGLE_SEL );
345

346 347
    /* Create the tree */
    treectrl = new wxTreeCtrl( playlist_panel, TreeCtrl_Event,
348
                               wxDefaultPosition, wxDefaultSize,
349 350 351 352 353 354
                               wxTR_HIDE_ROOT | wxTR_LINES_AT_ROOT|
                               wxTR_NO_LINES |
                               wxTR_HAS_BUTTONS | wxTR_TWIST_BUTTONS |
                               wxTR_MULTIPLE | wxTR_EXTENDED );

    /* Create image list */
355
    wxImageList *p_images = new wxImageList( 16 , 16, TRUE );
Clément Stenac's avatar
Clément Stenac committed
356 357 358

    /* FIXME: absolutely needs to be in the right order FIXME */
    p_images->Add( wxIcon( type_unknown_xpm ) );
Clément Stenac's avatar
Clément Stenac committed
359 360
    p_images->Add( wxIcon( type_afile_xpm ) );
    p_images->Add( wxIcon( type_vfile_xpm ) );
361
    p_images->Add( wxIcon( type_directory_xpm ) );
Clément Stenac's avatar
Clément Stenac committed
362
    p_images->Add( wxIcon( type_disc_xpm ) );
Clément Stenac's avatar
Clément Stenac committed
363
    p_images->Add( wxIcon( type_cdda_xpm ) );
364
    p_images->Add( wxIcon( type_card_xpm ) );
Clément Stenac's avatar
Clément Stenac committed
365 366
    p_images->Add( wxIcon( type_net_xpm ) );
    p_images->Add( wxIcon( type_playlist_xpm ) );
Clément Stenac's avatar
Clément Stenac committed
367
    p_images->Add( wxIcon( type_node_xpm ) );
368 369 370
    treectrl->AssignImageList( p_images );

    treectrl->AddRoot( wxU(_("root" )), -1, -1, NULL );
371

372
    /* Reduce font size */
373
    wxFont font= treectrl->GetFont();
374
    font.SetPointSize(9);
375
    treectrl->SetFont( font );
376

377 378
    wxBoxSizer *panel_sizer = new wxBoxSizer( wxHORIZONTAL );
    panel_sizer->Add( source_sel, 0, wxALL | wxEXPAND, 5 );
379
    panel_sizer->Add( treectrl, 1, wxEXPAND | wxALL, 5 );
Gildas Bazin's avatar
 
Gildas Bazin committed
380
    panel_sizer->Layout();
Clément Stenac's avatar
Clément Stenac committed
381

Gildas Bazin's avatar
 
Gildas Bazin committed
382
    playlist_panel->SetSizerAndFit( panel_sizer );
383

Clément Stenac's avatar
Clément Stenac committed
384 385 386 387
    int pi_widths[1] =  { -1 };
    statusbar = CreateStatusBar( 1 );
    statusbar->SetStatusWidths( 1, pi_widths );

388
#if wxUSE_DRAG_AND_DROP
Gildas Bazin's avatar
 
Gildas Bazin committed
389
    /* Associate drop targets with the playlist */
390
    SetDropTarget( new PlaylistFileDropTarget( this ) );
391 392
    menubar->SetDropTarget( new PlaylistFileDropTarget( this ) );
    toolbar->SetDropTarget( new PlaylistFileDropTarget( this ) );
Gildas Bazin's avatar
 
Gildas Bazin committed
393
#endif
Gildas Bazin's avatar
 
Gildas Bazin committed
394

395
    i_saved_id = -1;
396
    i_saved_input_id = -1;
397 398

    /* Some global changes happened -> Rebuild all */
Gildas Bazin's avatar
 
Gildas Bazin committed
399
    var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
400 401 402 403 404 405 406

    /* We went to the next item */
    var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );

    /* One item has been updated */
    var_AddCallback( p_playlist, "item-change", ItemChanged, this );

407
    var_AddCallback( p_playlist, "item-append", ItemAppended, this );
408
    var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
409

410
    /* Update the playlist */
411
    Rebuild( VLC_TRUE );
Clément Stenac's avatar
Clément Stenac committed
412

413 414 415 416
}

Playlist::~Playlist()
{
417
    if( pp_sds != NULL ) free( pp_sds );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
418

419
    if( p_playlist == NULL ) return;
Gildas Bazin's avatar
 
Gildas Bazin committed
420

Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
421 422
    var_DelCallback( p_playlist, "item-change", ItemChanged, this );
    var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
Gildas Bazin's avatar
 
Gildas Bazin committed
423
    var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
424
    var_DelCallback( p_playlist, "item-append", ItemAppended, this );
425
    var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
Gildas Bazin's avatar
 
Gildas Bazin committed
426
    vlc_object_release( p_playlist );
427 428
}

429
/**********************************************************************
430
 * Update functions
431
 **********************************************************************/
432 433

/* Update a node */
434
void Playlist::UpdateNode( playlist_item_t *p_node, wxTreeItemId node )
435
{
436
    wxTreeItemIdValue cookie;
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
    wxTreeItemId child;
    for( int i = 0; i< p_node->i_children ; i++ )
    {
        if( i == 0 )
        {
            child = treectrl->GetFirstChild( node, cookie);
        }
        else
        {
            child = treectrl->GetNextChild( node, cookie );
        }

        if( !child.IsOk() )
        {
            /* Not enough children */
452
            CreateNode( p_node->pp_children[i], node );
453 454 455 456
            /* Keep the tree pointer up to date */
            child = treectrl->GetNextChild( node, cookie );
        }
    }
457
    treectrl->SetItemImage( node, p_node->p_input->i_type );
458 459

}
460

461
/* Creates the node p_node as last child of parent */
462
void Playlist::CreateNode( playlist_item_t *p_node, wxTreeItemId parent )
463
{
464
    wxTreeItemId node =
465
        treectrl->AppendItem( parent, wxL2U( p_node->p_input->psz_name ),
466
                              -1,-1, new PlaylistItem( p_node ) );
467
    treectrl->SetItemImage( node, p_node->p_input->i_type );
468

469
    UpdateNodeChildren( p_node, node );
470 471
}

472
/* Update all children (recursively) of this node */
473
void Playlist::UpdateNodeChildren( playlist_item_t *p_node,
474 475
                                   wxTreeItemId node )
{
476 477 478 479 480
    for( int i = 0; i< p_node->i_children ; i++ )
    {
        /* Append the item */
        if( p_node->pp_children[i]->i_children == -1 )
        {
481 482 483 484 485
            if( !(p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG) )
            {
                wxTreeItemId item =
                    treectrl->AppendItem( node,
                    wxL2U( p_node->pp_children[i]->p_input->psz_name ), -1,-1,
486
                           new PlaylistItem( p_node->pp_children[i]) );
487

488 489
                UpdateTreeItem( item );
            }
490 491 492
        }
        else
        {
493
            CreateNode( p_node->pp_children[i], node );
494 495 496 497
        }
    }
}

498
/* Update an item in the tree */
499
void Playlist::UpdateTreeItem( wxTreeItemId item )
500
{
501
    LockPlaylist( p_intf->p_sys, p_playlist );
502 503
    if( ! item.IsOk() ) return;

504 505
    wxTreeItemData *p_data = treectrl->GetItemData( item );
    if( !p_data ) return;
506

507
    playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
508
                                    ((PlaylistItem *)p_data)->i_id, VLC_TRUE );
509 510 511 512 513
    if( !p_item )
    {
        UnlockPlaylist( p_intf->p_sys, p_playlist );
        return;
    }
514

515
    wxString msg;
516
    wxString duration = wxU( "" );
517

Clément Stenac's avatar
Clément Stenac committed
518
    char *psz_artist;
519
    if( p_item->p_input->p_meta )
520
    {
Clément Stenac's avatar
Clément Stenac committed
521
        psz_artist= p_item->p_input->p_meta->psz_artist ?
522 523
                        strdup( p_item->p_input->p_meta->psz_artist ) :
                        strdup("");
524
    }
525
    else
Clément Stenac's avatar
Clément Stenac committed
526
        psz_artist = strdup( "" );
527

528
    char psz_duration[MSTRTIME_MAX_SIZE];
529
    mtime_t dur = p_item->p_input->i_duration;
530

531
    if( dur != -1 )
532
    {
533
        secstotimestr( psz_duration, dur/1000000 );
534
        duration.Append( wxU( " ( " ) +  wxString( wxU( psz_duration ) ) +
535
                         wxU( " )" ) );
536
    }
537

Clément Stenac's avatar
Clément Stenac committed
538
    if( !strcmp( psz_artist, "" ) || p_item->p_input->b_fixed_name == VLC_TRUE )
539
    {
540
        msg = wxString( wxU( p_item->p_input->psz_name ) ) + duration;
541
    }
542 543
    else
    {
Clément Stenac's avatar
Clément Stenac committed
544
        msg = wxString(wxU( psz_artist )) + wxT(" - ") +
545
              wxString(wxU(p_item->p_input->psz_name)) + duration;
546
    }
Clément Stenac's avatar
Clément Stenac committed
547
    free( psz_artist );
548
    treectrl->SetItemText( item , msg );
549
    treectrl->SetItemImage( item, p_item->p_input->i_type );
550

551
    if( p_playlist->status.p_item == p_item )
552
    {
553
        treectrl->SetItemBold( item, true );
554 555 556
        while( treectrl->GetItemParent( item ).IsOk() )
        {
            item = treectrl->GetItemParent( item );
557 558 559
            if( ! (item == treectrl->GetRootItem() &&
                treectrl->HasFlag( wxTR_HIDE_ROOT ) ) )
                treectrl->Expand( item );
560
        }
561 562 563 564
    }
    else
    {
        treectrl->SetItemBold( item, false );
565
    }
566
    UnlockPlaylist( p_intf->p_sys, p_playlist );
567 568
}

569
/* Process a AppendIt em request */
570 571 572
void Playlist::AppendItem( wxCommandEvent& event )
{
    playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
Clément Stenac's avatar
Clément Stenac committed
573
    playlist_item_t *p_item = NULL;
574
    wxTreeItemId item, node;
575

576
    i_items_to_append--;
577

578 579 580
    /* No need to do anything if the playlist is going to be rebuilt */
    if( b_need_update ) return;

Clément Stenac's avatar
Clément Stenac committed
581
    node = FindItem( treectrl->GetRootItem(), p_add->i_node );
582
    if( !node.IsOk() ) goto update;
583

584
    p_item = playlist_ItemGetById( p_playlist, p_add->i_item, VLC_TRUE );
585
    if( !p_item ) goto update;
586
    if( (p_item->i_flags & PLAYLIST_DBL_FLAG ) ) goto update;
587 588 589

    item = FindItem( treectrl->GetRootItem(), p_add->i_item );
    if( item.IsOk() ) goto update;
Clément Stenac's avatar
Clément Stenac committed
590

591
    item = treectrl->AppendItem( node,
592
                                 wxL2U( p_item->p_input->psz_name ), -1,-1,
Clément Stenac's avatar
Clément Stenac committed
593
                                 new PlaylistItem( p_item ) );
594
    treectrl->SetItemImage( item, p_item->p_input->i_type );
595

Clément Stenac's avatar
Clément Stenac committed
596
    if( item.IsOk() && p_item->i_children == -1 )
597
    {
598
        UpdateTreeItem( item );
599
    }
Clément Stenac's avatar
Clément Stenac committed
600

601 602
update:
    int i_count = CountItems( treectrl->GetRootItem());
603 604
    statusbar->SetStatusText( wxString::Format( wxU(_(
                                  "%i items in playlist" ) ), i_count ) );
605
    return;
606 607
}

608
/* Process a updateitem request */
609 610
void Playlist::UpdateItem( int i )
{
Gildas Bazin's avatar
 
Gildas Bazin committed
611
    if( i < 0 ) return; /* Sanity check */
612
    wxTreeItemId item = FindItemByInput( treectrl->GetRootItem(), i );
613 614
    if( item.IsOk() )
    {
615
        UpdateTreeItem( item );
616
    }
617 618
}

619 620 621
void Playlist::RemoveItem( int i )
{
    if( i <= 0 ) return; /* Sanity check */
622
    if( i == i_saved_id ) i_saved_id = -1;
623

624 625 626 627 628
    /* Hack: always invalidate input item cache */
    i_saved_input_id = -1;

    /// \todo Check if it is in the source selector */

629 630 631 632 633
    wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );

    if( item.IsOk() )
    {
        treectrl->Delete( item );
634
    }
635
}
636 637


638
/**********************************************************************
639
 * Search functions (internal)
640
 **********************************************************************/
641

642 643
/* Find a wxItem from a playlist id */
wxTreeItemId Playlist::FindItem( wxTreeItemId root, int i_id )
644 645 646 647 648 649 650 651 652 653
{
    return FindItemInner( root, i_id, false );
}

wxTreeItemId Playlist::FindItemByInput( wxTreeItemId root, int i_input_id )
{
    return FindItemInner( root, i_input_id, true );
}

wxTreeItemId Playlist::FindItemInner( wxTreeItemId root, int i_id, bool b_byinput )
654
{
655
    wxTreeItemIdValue cookie;
656 657 658 659 660 661 662 663 664
    PlaylistItem *p_wxcurrent;
    wxTreeItemId search;
    wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
    wxTreeItemId child;

    p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );

    if( i_id < 0 )
    {
665
        wxTreeItemId dummy; dummy.Unset(); return dummy;
666
    }
667 668 669
    if( b_byinput && i_saved_input_id == i_id )
        return saved_input_tree_item;
    if( !b_byinput && i_saved_id == i_id)
670
        return saved_tree_item;
671 672

    if( !p_wxcurrent )
673
    {
674
        wxTreeItemId dummy; dummy.Unset(); return dummy;
675
    }
676

677
    if( !b_byinput && p_wxcurrent->i_id == i_id  )
678
    {
679 680
        i_saved_id = i_id;
        saved_tree_item = root;
681
        return root;
682
    }
683 684 685 686 687 688
    if( b_byinput && p_wxcurrent->i_input_id == i_id )
    {
        i_saved_input_id = i_id;
        saved_input_tree_item = root;
        return root;
    }
689

690
    while( item.IsOk() )
691
    {
692
        p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
693
        if( !b_byinput && p_wxcurrent->i_id == i_id )
694
        {
695 696
            i_saved_id = i_id;
            saved_tree_item = item;
697 698
            return item;
        }
699 700 701 702 703 704
        else if( b_byinput && p_wxcurrent->i_input_id == i_id )
        {
            i_saved_input_id = i_id;
            saved_input_tree_item = item;
            return item;
        }
705 706
        if( treectrl->ItemHasChildren( item ) )
        {
707
            wxTreeItemId search = FindItemInner( item, i_id, b_byinput );
708 709
            if( search.IsOk() )
            {
710 711 712 713 714 715 716 717 718 719 720 721 722
                if( !b_byinput )
                {
                    i_saved_id = i_id;
                    saved_tree_item = search;
                    return search;
                }
                else
                {
                    i_saved_input_id = i_id;
                    saved_input_tree_item = search;
                    return search;

                }
723 724 725
            }
        }
        item = treectrl->GetNextChild( root, cookie );
726
    }
727
    /* Not found */
728
    wxTreeItemId dummy; dummy.Unset(); return dummy;
729 730 731 732
}

int Playlist::CountItems( wxTreeItemId root )
{
733
    wxTreeItemIdValue cookie;
734 735
    int count = 0;
    wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
736

737
    while( item.IsOk() )
738
    {
739 740 741 742
        if( treectrl->ItemHasChildren( item ) )
        {
            count += CountItems( item );
        }
743 744 745 746
        else
        {
            playlist_item_t *p_item;
            LockPlaylist( p_intf->p_sys, p_playlist );
747
            p_item = playlist_ItemGetById( p_playlist, ((PlaylistItem *)treectrl->GetItemData( item ))->i_id, VLC_TRUE );