playlist.cpp 53.2 KB
Newer Older
1
/*****************************************************************************
2
 * playlist.cpp : wxWindows plugin for vlc
3
 *****************************************************************************
4
 * Copyright (C) 2000-2005 the VideoLAN team
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.
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>
50
#include "vlc_charset.h"
51

52
#define HELP_SHUFFLE N_( "Shuffle" )
53 54
#define HELP_LOOP N_( "Repeat All" )
#define HELP_REPEAT N_( "Repeat One" )
55

56
namespace wxvlc {
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 */
77
    AddFile_Event = 1,
78
    AddDir_Event,
79
    AddMRL_Event,
80
    Close_Event,
81 82
    Open_Event,
    Save_Event,
83

84 85
    SortTitle_Event,
    RSortTitle_Event,
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

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 */
131
    EVT_MENU(AddFile_Event, Playlist::OnAddFile)
132
    EVT_MENU(AddDir_Event, Playlist::OnAddDir)
133
    EVT_MENU(AddMRL_Event, Playlist::OnAddMRL)
134
    EVT_MENU(Close_Event, Playlist::OnMenuClose)
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)

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 */
172
    EVT_BUTTON( Search_Event, Playlist::OnSearch)
173
    EVT_BUTTON( Save_Event, Playlist::OnSave)
174

175 176
    /*EVT_TEXT( SearchText_Event, Playlist::OnSearchTextChange )*/
    EVT_TEXT_ENTER( SearchText_Event, Playlist::OnSearch )
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.
 *****************************************************************************/
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
{
211 212
    vlc_value_t val;

213 214
    /* Initializations */
    p_intf = _p_intf;
215
    pp_sds = NULL;
216
    i_update_counter = 0;
217
    i_sort_mode = MODE_NONE;
218
    b_need_update = false;
219
    i_items_to_append = 0;
Antoine Cellerier's avatar
Antoine Cellerier committed
220
    p_playlist = pl_Hold( p_intf );
221
    if( p_playlist == NULL ) return;
222

223
    SetIcon( *p_intf->p_sys->p_icon );
224

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

228
    p_current_viewroot = p_playlist->p_root_category;
229
    p_current_treeroot = NULL;
230

231 232
    i_title_sorted = 0;
    i_group_sorted = 0;
233
    i_duration_sorted = 0;
234

Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
235 236 237
    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 );;
238

239 240
    /* Create our "Manage" menu */
    wxMenu *manage_menu = new wxMenu;
241 242
    manage_menu->Append( AddFile_Event, wxU(_("&Simple Add File...")) );
    manage_menu->Append( AddDir_Event, wxU(_("Add &Directory...")) );
243
    manage_menu->Append( AddMRL_Event, wxU(_("&Add URL...")) );
244
    manage_menu->AppendSeparator();
245
    manage_menu->Append( MenuDummy_Event, wxU(_("Services Discovery")),
246 247
                         p_sd_menu );
    manage_menu->AppendSeparator();
248 249
    manage_menu->Append( Open_Event, wxU(_("&Open Playlist...")) );
    manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
250
    manage_menu->AppendSeparator();
251
    manage_menu->Append( Close_Event, wxU(_("&Close")) );
252

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

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

264 265
    /* Create our "View" menu */
    ViewMenu();
266

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

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

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

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

293 294 295 296
    /* Create a panel to put everything in */
    wxPanel *playlist_panel = new wxPanel( this, -1 );
    playlist_panel->SetAutoLayout( TRUE );

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

    /* Create the random tool */
    toolbar->AddTool( Random_Event, wxT(""), wxBitmap(shuffle_on_xpm),
                       wxBitmap(shuffle_on_xpm), wxITEM_CHECK,
                       wxU(_(HELP_SHUFFLE) ) );
305
    var_Get( p_intf, "random", &val );
306
    toolbar->ToggleTool( Random_Event, val.b_bool );
307

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

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

322
    /* Create the Search Textbox */
323 324 325
    search_text = new wxTextCtrl( toolbar, SearchText_Event, wxT(""),
                                  wxDefaultPosition, wxSize(100, -1),
                                  wxTE_PROCESS_ENTER);
326 327

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

330 331
    toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
                         wxSize(16, 16), wxBORDER_NONE ) );
332
    toolbar->AddControl( search_text );
333 334
    toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
                         wxSize(5, 5), wxBORDER_NONE ) );
335
    toolbar->AddControl( search_button );
336
    search_button->SetDefault();
337
    toolbar->Realize();
338

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

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

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

    /* 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
357 358
    p_images->Add( wxIcon( type_afile_xpm ) );
    p_images->Add( wxIcon( type_vfile_xpm ) );
359
    p_images->Add( wxIcon( type_directory_xpm ) );
Clément Stenac's avatar
Clément Stenac committed
360
    p_images->Add( wxIcon( type_disc_xpm ) );
Clément Stenac's avatar
Clément Stenac committed
361
    p_images->Add( wxIcon( type_cdda_xpm ) );
362
    p_images->Add( wxIcon( type_card_xpm ) );
Clément Stenac's avatar
Clément Stenac committed
363 364
    p_images->Add( wxIcon( type_net_xpm ) );
    p_images->Add( wxIcon( type_playlist_xpm ) );
Clément Stenac's avatar
Clément Stenac committed
365
    p_images->Add( wxIcon( type_node_xpm ) );
366 367
    treectrl->AssignImageList( p_images );

368
    /* Reduce font size */
369
    wxFont font= treectrl->GetFont();
370
    font.SetPointSize(9);
371
    treectrl->SetFont( font );
372

373 374
    wxBoxSizer *panel_sizer = new wxBoxSizer( wxHORIZONTAL );
    panel_sizer->Add( source_sel, 0, wxALL | wxEXPAND, 5 );
375
    panel_sizer->Add( treectrl, 1, wxEXPAND | wxALL, 5 );
376
    panel_sizer->Layout();
377

378
    playlist_panel->SetSizerAndFit( panel_sizer );
379

380 381 382 383
    int pi_widths[1] =  { -1 };
    statusbar = CreateStatusBar( 1 );
    statusbar->SetStatusWidths( 1, pi_widths );

384
#if wxUSE_DRAG_AND_DROP
385
    /* Associate drop targets with the playlist */
386
    SetDropTarget( new PlaylistFileDropTarget( this ) );
387 388
    menubar->SetDropTarget( new PlaylistFileDropTarget( this ) );
    toolbar->SetDropTarget( new PlaylistFileDropTarget( this ) );
389
#endif
390

391
    i_saved_id = -1;
392
    i_saved_input_id = -1;
393 394

    /* Some global changes happened -> Rebuild all */
395
    var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
396 397 398 399 400 401 402

    /* 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 );

403
    var_AddCallback( p_playlist, "item-append", ItemAppended, this );
404
    var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
405

406
    /* Update the playlist */
407
    p_current_treeroot = p_playlist->p_local_category;
408
    Rebuild( true );
409 410 411 412
}

Playlist::~Playlist()
{
413 414 415 416 417 418
    if( pp_sds != NULL )
    {
        char **pp_sd = pp_sds;
        for( ; *pp_sd; pp_sd++ ) free( *pp_sd );
        free( pp_sds );
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
419

420
    if( p_playlist == NULL ) return;
421

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

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

/* Update a node */
435
void Playlist::UpdateNode( playlist_item_t *p_node, wxTreeItemId node )
436
{
437
    wxTreeItemIdValue cookie;
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
    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 */
453
            CreateNode( p_node->pp_children[i], node );
454 455 456 457
            /* Keep the tree pointer up to date */
            child = treectrl->GetNextChild( node, cookie );
        }
    }
458
    treectrl->SetItemImage( node, p_node->p_input->i_type );
459 460

}
461

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

470
    UpdateNodeChildren( p_node, node );
471 472
}

473
/* Update all children (recursively) of this node */
474
void Playlist::UpdateNodeChildren( playlist_item_t *p_node,
475 476
                                   wxTreeItemId node )
{
477 478 479 480 481
    for( int i = 0; i< p_node->i_children ; i++ )
    {
        /* Append the item */
        if( p_node->pp_children[i]->i_children == -1 )
        {
482 483 484 485 486
            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,
487
                           new PlaylistItem( p_node->pp_children[i]) );
488

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

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

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

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

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

519 520
    char *psz_artist = input_item_GetArtist( p_item->p_input );
    char *psz_name = input_item_GetName( p_item->p_input );
521

522
    char psz_duration[MSTRTIME_MAX_SIZE];
523
    mtime_t dur = input_item_GetDuration( p_item->p_input );
524

525
    if( dur != -1 )
526
    {
527
        secstotimestr( psz_duration, dur/1000000 );
528
        duration.Append( wxU( " ( " ) +  wxString( wxU( psz_duration ) ) +
529
                         wxU( " )" ) );
530
    }
531

532
    if( !psz_artist || !strcmp( psz_artist, "" ) || p_item->p_input->b_fixed_name == true )
533
    {
534
        msg = wxString( wxU( psz_name ) ) + duration;
535
    }
536 537
    else
    {
Clément Stenac's avatar
Clément Stenac committed
538
        msg = wxString(wxU( psz_artist )) + wxT(" - ") +
539
              wxString(wxU(psz_name)) + duration;
540
    }
541
    free( psz_artist );
542
    free( psz_name );
543
    treectrl->SetItemText( item , msg );
544
    treectrl->SetItemImage( item, p_item->p_input->i_type );
545

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

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

571
    i_items_to_append--;
572

573 574 575
    /* 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
576
    node = FindItem( treectrl->GetRootItem(), p_add->i_node );
577
    if( !node.IsOk() ) goto update;
578

579
    p_item = playlist_ItemGetById( p_playlist, p_add->i_item, true );
580
    if( !p_item ) goto update;
581
    if( (p_item->i_flags & PLAYLIST_DBL_FLAG ) ) goto update;
582 583 584

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

586
    item = treectrl->AppendItem( node,
587
                                 wxL2U( p_item->p_input->psz_name ), -1,-1,
Clément Stenac's avatar
Clément Stenac committed
588
                                 new PlaylistItem( p_item ) );
589
    treectrl->SetItemImage( item, p_item->p_input->i_type );
590

Clément Stenac's avatar
Clément Stenac committed
591
    if( item.IsOk() && p_item->i_children == -1 )
592
    {
593
        UpdateTreeItem( item );
594
    }
595

596 597
update:
    int i_count = CountItems( treectrl->GetRootItem());
598 599
    statusbar->SetStatusText( wxString::Format( wxU(_(
                                  "%i items in playlist" ) ), i_count ) );
600
    return;
601 602
}

603
/* Process a updateitem request */
604 605
void Playlist::UpdateItem( int i )
{
606
    if( i < 0 ) return; /* Sanity check */
607
    wxTreeItemId item = FindItemByInput( treectrl->GetRootItem(), i );
608 609
    if( item.IsOk() )
    {
610
        UpdateTreeItem( item );
611
    }
612 613
}

614 615 616
void Playlist::RemoveItem( int i )
{
    if( i <= 0 ) return; /* Sanity check */
617
    if( i == i_saved_id ) i_saved_id = -1;
618

619 620 621 622 623
    /* Hack: always invalidate input item cache */
    i_saved_input_id = -1;

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

624 625 626 627 628
    wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );

    if( item.IsOk() )
    {
        treectrl->Delete( item );
629
    }
630
}
631 632


633
/**********************************************************************
634
 * Search functions (internal)
635
 **********************************************************************/
636

637 638
/* Find a wxItem from a playlist id */
wxTreeItemId Playlist::FindItem( wxTreeItemId root, int i_id )
639 640 641 642 643 644 645 646 647 648
{
    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 )
649
{
650
    wxTreeItemIdValue cookie;
651 652 653 654 655 656 657 658 659
    PlaylistItem *p_wxcurrent;
    wxTreeItemId search;
    wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
    wxTreeItemId child;

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

    if( i_id < 0 )
    {
660
        wxTreeItemId dummy; dummy.Unset(); return dummy;
661
    }
662 663 664
    if( b_byinput && i_saved_input_id == i_id )
        return saved_input_tree_item;
    if( !b_byinput && i_saved_id == i_id)
665
        return saved_tree_item;
666 667

    if( !p_wxcurrent )
668
    {
669
        wxTreeItemId dummy; dummy.Unset(); return dummy;
670
    }
671

672
    if( !b_byinput && p_wxcurrent->i_id == i_id  )
673
    {
674 675
        i_saved_id = i_id;
        saved_tree_item = root;
676
        return root;
677
    }
678 679 680 681 682 683
    if( b_byinput && p_wxcurrent->i_input_id == i_id )
    {
        i_saved_input_id = i_id;
        saved_input_tree_item = root;
        return root;
    }
684

685
    while( item.IsOk() )
686
    {
687
        p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
688
        if( !b_byinput && p_wxcurrent->i_id == i_id )
689
        {
690 691
            i_saved_id = i_id;
            saved_tree_item = item;
692 693
            return item;
        }
694 695 696 697 698 699
        else if( b_byinput && p_wxcurrent->i_input_id == i_id )
        {
            i_saved_input_id = i_id;
            saved_input_tree_item = item;
            return item;
        }
700 701
        if( treectrl->ItemHasChildren( item ) )
        {
702
            wxTreeItemId search = FindItemInner( item, i_id, b_byinput );
703 704
            if( search.IsOk() )
            {
705 706 707 708 709 710 711 712 713 714 715 716 717
                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;

                }
718 719 720
            }
        }
        item = treectrl->GetNextChild( root, cookie );
721
    }
722
    /* Not found */
723
    wxTreeItemId dummy; dummy.Unset(); return dummy;
724 725 726 727
}

int Playlist::CountItems( wxTreeItemId root )
{
728
    wxTreeItemIdValue cookie;
729 730
    int count = 0;
    wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
731

732
    while( item.IsOk() )
733
    {
734 735 736 737
        if( treectrl->ItemHasChildren( item ) )
        {
            count += CountItems( item );
        }
738 739 740 741
        else
        {
            playlist_item_t *p_item;
            LockPlaylist( p_intf->p_sys, p_playlist );
742
            p_item = playlist_ItemGetById( p_playlist, ((PlaylistItem *)treectrl->GetItemData( item ))->i_id, true );
743 744 745 746
            if( p_item && p_item->i_children == -1 )
                count++;
            UnlockPlaylist( p_intf->p_sys, p_playlist );
        }
747
        item = treectrl->GetNextChild( root, cookie );
748
    }
749 750 751 752
    return count;
}

/* Find a wxItem from a name (from current) */
753
wxTreeItemId Playlist::FindItemByName( wxTreeItemId root, wxString search_string, wxTreeItemId current, bool *pb_current_found )
754
{
755
    wxTreeItemIdValue cookie;
756 757 758 759 760
    wxTreeItemId search;
    wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
    wxTreeItemId child;

    while( item.IsOk() )
761
    {
762 763 764
        if( treectrl->GetItemText( item).Lower().Contains(
                                                 search_string.Lower() ) )
        {
765
            if( !current.IsOk() || *pb_current_found == true )
766 767 768 769 770
            {
                return item;
            }
            else if( current.IsOk() && item == current )
            {
771
                *pb_current_found = true;
772 773 774 775 776 777 778 779 780 781 782 783
            }
        }
        if( treectrl->ItemHasChildren( item ) )
        {
            wxTreeItemId search = FindItemByName( item, search_string, current,
                                                  pb_current_found );
            if( search.IsOk() )
            {
                return search;
            }
        }
        item = treectrl->GetNextChild( root, cookie);
784
    }
785
    /* Not found */
786
    wxTreeItemId dummy; dummy.Unset();
787
    return dummy;
788 789
}

790 791 792
/**********************************************************************
 * Rebuild the playlist
 **********************************************************************/
793
void Playlist::Rebuild( bool b_root )
794
{
795 796
    i_items_to_append = 0;

797
    LockPlaylist( p_intf->p_sys, p_playlist );
798

799
    /* Invalidate cache */
800
    i_saved_id = -1;
801
    i_saved_input_id = -1;
802

803 804 805 806 807 808 809 810 811 812 813
    /* Rebuild the list */
    source_sel->ClearAll();
    for( int i = 0 ; i< p_current_viewroot->i_children ; i++ )
    {
        source_sel->InsertItem( i,
               wxL2U( p_current_viewroot->pp_children[i]->p_input->psz_name) );
        source_sel->SetItemData( i,
                        p_current_viewroot->pp_children[i]->i_id );
        if( p_current_viewroot->pp_children[i] == p_current_treeroot )
            source_sel->Select( i );
    }
814 815 816 817

    /* HACK we should really get new*/
    treectrl->DeleteAllItems();
    treectrl->AddRoot( wxU(_("root" )), -1, -1,
818
                         new PlaylistItem( p_current_treeroot ) );
819 820

    wxTreeItemId root = treectrl->GetRootItem();
821
    UpdateNodeChildren( p_current_treeroot, root );
822

Clément Stenac's avatar
Clément Stenac committed
823
    int i_count = CountItems( treectrl->GetRootItem() );
824

825 826
    statusbar->SetStatusText( wxString::Format( wxU(_(
                              "%i items in playlist")), i_count ), 0 );
827

828
    UnlockPlaylist( p_intf->p_sys, p_playlist );
829 830
}

831
void Playlist::ShowPlaylist( bool show )
832
{
833
    if( show ) Rebuild( true );
834 835 836
    Show( show );
}

837
/* This function is called on a regular basis */
838 839 840 841 842 843 844 845 846
void Playlist::UpdatePlaylist()
{
    i_update_counter++;

    /* If the playlist isn't show there's no need to update it */
    if( !IsShown() ) return;

    if( this->b_need_update )
    {
847 848
        this->b_need_update = false;
        Rebuild( true );
849
    }
850 851 852

    /* Updating the playing status every 0.5s is enough */
    if( i_update_counter % 5 ) return;
853 854 855 856 857
}

/*****************************************************************************
 * Private methods.
 *****************************************************************************/
858 859 860 861 862 863 864
void Playlist::DeleteTreeItem( wxTreeItemId item )
{
   PlaylistItem *p_wxitem;
   playlist_item_t *p_item;
   p_wxitem = (PlaylistItem *)treectrl->GetItemData( item );

   LockPlaylist( p_intf->p_sys, p_playlist );
865
   p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id, true );
866 867 868 869 870 871 872

   if( !p_item )
   {
       UnlockPlaylist( p_intf->p_sys, p_playlist );
       return;
   }

873
   if( p_item->i_children == -1 ) DeleteItem( p_item->p_input->i_id );
874 875
   else DeleteNode( p_item );

876
   RemoveItem( p_item->i_id );
877
   UnlockPlaylist( p_intf->p_sys, p_playlist );
878 879
}

880
void Playlist::DeleteItem( int item_id )
881
{
882
    playlist_DeleteFromInput( p_playlist, item_id, true );
883 884
}

885 886
void Playlist::DeleteNode( playlist_item_t *p_item )
{
887
    playlist_NodeDelete( p_playlist, p_item, true , false );
888 889
}

890 891 892 893 894
void Playlist::OnMenuClose( wxCommandEvent& event )
{
    wxCloseEvent cevent;
    OnClose(cevent);
}
895

896
void Playlist::OnClose( wxCloseEvent& WXUNUSED(event) )
897 898 899 900
{
    Hide();
}

901 902
void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
{
903 904 905 906
    struct {
        char *psz_desc;
        char *psz_filter;
        char *psz_module;
907
    } formats[] = {//{ _("M3U file"), "*.m3u", "export-m3u" },
908 909
                   { _("XSPF playlist"), "*.xspf", "export-xspf"}
    };
910

911
    wxString filter = wxT("");
912

913
    if( playlist_IsEmpty( p_playlist ) )
914 915 916 917 918 919
    {
        wxMessageBox( wxU(_("Playlist is empty") ), wxU(_("Can't save")),
                      wxICON_WARNING | wxOK, this );
        return;
    }

920 921
    for( unsigned int i = 0; i < sizeof(formats)/sizeof(formats[0]); i++)
    {
922
        filter.Append( wxU(formats[i].psz_desc) );
923
        filter.Append( wxT("|") );
924
        filter.Append( wxU(formats[i].psz_filter) );
925 926 927 928 929 930 931
        filter.Append( wxT("|") );
    }
    wxFileDialog dialog( this, wxU(_("Save playlist")),
                         wxT(""), wxT(""), filter, wxSAVE );

    if( dialog.ShowModal() == wxID_OK )
    {
932
        if( dialog.GetPath().mb_str(wxConvUTF8) )
933
        {
934 935 936
            /* what root should we export? */
            if( p_playlist->p_root_category->i_children > 0 )
            {
937
                playlist_Export( p_playlist, dialog.GetPath().mb_str(wxConvUTF8),
938 939 940
                                 p_playlist->p_root_category->pp_children[0],
                                 formats[dialog.GetFilterIndex()].psz_module );
            }
941
        }
942
    }
943

944 945 946
}

void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
947
{
948
    wxFileDialog dialog( this, wxU(_("Open playlist")), wxT(""), wxT(""),
949
        wxT("All playlists|" EXTENSIONS_PLAYLIST "|XSPF playlist|*.xspf|M3U files|*.m3u"), wxOPEN );
950 951 952

    if( dialog.ShowModal() == wxID_OK )
    {
953
        playlist_Import( p_playlist, dialog.GetPath().mb_str(wxConvUTF8) );
954
    }
955 956
}

957 958
void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
{
959
    p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
960 961 962

}

963 964 965 966 967 968
void Playlist::OnAddDir( wxCommandEvent& WXUNUSED(event) )
{
    p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_DIRECTORY, 0, 0 );

}

969
void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
970
{
971
    p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
972

973 974
}

975 976 977 978 979
/********************************************************************
 * Sorting functions
 ********************************************************************/
void Playlist::OnSort( wxCommandEvent& event )
{
980 981 982
    PlaylistItem *p_wxitem;
    p_wxitem = (PlaylistItem *)treectrl->GetItemData( treectrl->GetRootItem() );

983
    LockPlaylist( p_intf->p_sys, p_playlist );
984 985 986
    switch( event.GetId() )
    {
        case SortTitle_Event:
987
            playlist_RecursiveNodeSort( p_playlist,
988
                            playlist_ItemGetById( p_playlist, p_wxitem->i_id,
989
                                                  true ),
990
                            SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
991
            break;
992
        case RSortTitle_Event:
993
            playlist_RecursiveNodeSort( p_playlist,
994
                            playlist_ItemGetById( p_playlist, p_wxitem->i_id,
995
                                                  true ),
996
                            SORT_TITLE_NODES_FIRST, ORDER_REVERSE );
997
    }
998
    UnlockPlaylist( p_intf->p_sys, p_playlist );
999

1000
    Rebuild( true );
1001 1002
}

1003
/**********************************************************************
1004
 * Search functions (user)
1005
 **********************************************************************/
1006
/*void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
1007
{
1008
   search_button->SetDefault();