playlist.cpp 53.1 KB
Newer Older
1
/*****************************************************************************
gbazin's avatar
   
gbazin committed
2
 * playlist.cpp : wxWindows plugin for vlc
3
 *****************************************************************************
4
 * Copyright (C) 2000-2005 the VideoLAN team
zorglub's avatar
zorglub 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.
gbazin's avatar
   
gbazin 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
dionoea's avatar
dionoea 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

zorglub's avatar
zorglub committed
31
#include "bitmaps/shuffle.xpm"
32
33
34
#include "bitmaps/repeat.xpm"
#include "bitmaps/loop.xpm"

35
#include "bitmaps/type_unknown.xpm"
zorglub's avatar
zorglub 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"
zorglub's avatar
zorglub committed
41
#include "bitmaps/type_cdda.xpm"
42
43
#include "bitmaps/type_directory.xpm"
#include "bitmaps/type_playlist.xpm"
zorglub's avatar
zorglub committed
44
#include "bitmaps/type_node.xpm"
45
46

#include <wx/dynarray.h>
47
#include <wx/imaglist.h>
zorglub's avatar
zorglub committed
48

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

zorglub's avatar
zorglub committed
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" )
zorglub's avatar
zorglub committed
55

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

zorglub's avatar
zorglub committed
84
85
    SortTitle_Event,
    RSortTitle_Event,
zorglub's avatar
zorglub committed
86
    Randomize_Event,
zorglub's avatar
zorglub committed
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

zorglub's avatar
zorglub committed
101
102
103
    SearchText_Event,
    Search_Event,

104
    /* controls */
105
    TreeCtrl_Event,
zorglub's avatar
zorglub committed
106
107

    Browse_Event,  /* For export playlist */
108
109

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

114
115
    MenuDummy_Event = wxID_HIGHEST + 999,

116
    FirstView_Event = wxID_HIGHEST + 1000,
117
118
119
120
    LastView_Event = wxID_HIGHEST + 1100,

    FirstSD_Event = wxID_HIGHEST + 2000,
    LastSD_Event = wxID_HIGHEST + 2100,
121
122
};

123
124
DEFINE_LOCAL_EVENT_TYPE( wxEVT_PLAYLIST );

125
BEGIN_EVENT_TABLE(Playlist, wxFrame)
126
127
    EVT_SIZE(Playlist::OnSize)

128
    /* Menu events */
gbazin's avatar
   
gbazin committed
129
    EVT_MENU(AddFile_Event, Playlist::OnAddFile)
zorglub's avatar
zorglub committed
130
    EVT_MENU(AddDir_Event, Playlist::OnAddDir)
gbazin's avatar
   
gbazin committed
131
    EVT_MENU(AddMRL_Event, Playlist::OnAddMRL)
132
    EVT_MENU(Close_Event, Playlist::OnMenuClose)
gbazin's avatar
   
gbazin committed
133
134
    EVT_MENU(Open_Event, Playlist::OnOpen)
    EVT_MENU(Save_Event, Playlist::OnSave)
zorglub's avatar
zorglub committed
135
136
137
138

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

zorglub's avatar
zorglub committed
139
140
    EVT_MENU(Randomize_Event, Playlist::OnSort)

141
    EVT_MENU(DeleteSelection_Event, Playlist::OnDeleteSelection)
142

143
144
145
    EVT_MENU_OPEN( Playlist::OnMenuOpen )
    EVT_MENU( -1, Playlist::OnMenuEvent )

146
147
148
    EVT_TOOL(Random_Event, Playlist::OnRandom)
    EVT_TOOL(Repeat_Event, Playlist::OnRepeat)
    EVT_TOOL(Loop_Event, Playlist::OnLoop)
149

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

159
160
    /* Tree control events */
    EVT_TREE_ITEM_ACTIVATED( TreeCtrl_Event, Playlist::OnActivateItem )
161
    EVT_TREE_KEY_DOWN( -1, Playlist::OnKeyDown )
162
163
    EVT_TREE_BEGIN_DRAG( TreeCtrl_Event, Playlist::OnDragItemBegin )
    EVT_TREE_END_DRAG( TreeCtrl_Event, Playlist::OnDragItemEnd )
164
165

    EVT_CONTEXT_MENU( Playlist::OnPopup )
166
167

    /* Button events */
zorglub's avatar
zorglub committed
168
    EVT_BUTTON( Search_Event, Playlist::OnSearch)
gbazin's avatar
   
gbazin committed
169
    EVT_BUTTON( Save_Event, Playlist::OnSave)
zorglub's avatar
zorglub committed
170

171
172
    /*EVT_TEXT( SearchText_Event, Playlist::OnSearchTextChange )*/
    EVT_TEXT_ENTER( SearchText_Event, Playlist::OnSearch )
zorglub's avatar
zorglub committed
173

174
175
176
    /* Custom events */
    EVT_COMMAND(-1, wxEVT_PLAYLIST, Playlist::OnPlaylistEvent)

177
178
179
180
181
    /* Special events : we don't want to destroy the window when the user
     * clicks on (X) */
    EVT_CLOSE(Playlist::OnClose)
END_EVENT_TABLE()

182
183
184
185
186
187
/*****************************************************************************
 * PlaylistItem class
 ****************************************************************************/
class PlaylistItem : public wxTreeItemData
{
public:
188
    PlaylistItem( playlist_item_t *p_item ) : wxTreeItemData()
189
    {
190
        i_id = p_item->input.i_id;
191
192
    }
protected:
193
    int i_id;
194
friend class Playlist;
195
friend class PlaylistFileDropTarget;
196
};
zorglub's avatar
zorglub committed
197

198
199
200
/*****************************************************************************
 * Constructor.
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
201
202
Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
    wxFrame( p_parent, -1, wxU(_("Playlist")), wxDefaultPosition,
203
             wxSize(500,300), wxDEFAULT_FRAME_STYLE )
204
{
gbazin's avatar
   
gbazin committed
205
206
    vlc_value_t val;

207
208
    /* Initializations */
    p_intf = _p_intf;
209
    pp_sds = NULL;
gbazin's avatar
   
gbazin committed
210
    i_update_counter = 0;
zorglub's avatar
zorglub committed
211
    i_sort_mode = MODE_NONE;
gbazin's avatar
   
gbazin committed
212
    b_need_update = VLC_FALSE;
213
    i_items_to_append = 0;
214
215
    p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
                                                FIND_ANYWHERE );
216
    if( p_playlist == NULL ) return;
217

gbazin's avatar
   
gbazin committed
218
    SetIcon( *p_intf->p_sys->p_icon );
219

220
    p_view_menu = NULL;
221
    p_sd_menu = SDMenu();
222

zorglub's avatar
zorglub committed
223
    i_current_view = VIEW_CATEGORY;
zorglub's avatar
zorglub committed
224
    b_changed_view = VLC_FALSE;
225

226
227
    i_title_sorted = 0;
    i_group_sorted = 0;
zorglub's avatar
zorglub committed
228
    i_duration_sorted = 0;
229

hartman's avatar
hartman committed
230
231
232
    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 );;
233

234
235
    /* Create our "Manage" menu */
    wxMenu *manage_menu = new wxMenu;
zorglub's avatar
zorglub committed
236
237
    manage_menu->Append( AddFile_Event, wxU(_("&Simple Add File...")) );
    manage_menu->Append( AddDir_Event, wxU(_("Add &Directory...")) );
gbazin's avatar
   
gbazin committed
238
    manage_menu->Append( AddMRL_Event, wxU(_("&Add MRL...")) );
zorglub's avatar
zorglub committed
239
    manage_menu->AppendSeparator();
240
241
242
    manage_menu->Append( MenuDummy_Event, wxU(_("Services discovery")),
                         p_sd_menu );
    manage_menu->AppendSeparator();
gbazin's avatar
   
gbazin committed
243
244
    manage_menu->Append( Open_Event, wxU(_("&Open Playlist...")) );
    manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
245
    manage_menu->AppendSeparator();
gbazin's avatar
   
gbazin committed
246
    manage_menu->Append( Close_Event, wxU(_("&Close")) );
247

zorglub's avatar
zorglub committed
248
249
    /* Create our "Sort" menu */
    wxMenu *sort_menu = new wxMenu;
zorglub's avatar
zorglub committed
250
    sort_menu->Append( SortTitle_Event, wxU(_("Sort by &title")) );
zorglub's avatar
zorglub committed
251
252
    sort_menu->Append( RSortTitle_Event, wxU(_("&Reverse sort by title")) );
    sort_menu->AppendSeparator();
hartman's avatar
hartman committed
253
    sort_menu->Append( Randomize_Event, wxU(_("&Shuffle Playlist")) );
zorglub's avatar
zorglub committed
254

255
256
    /* Create our "Selection" menu */
    wxMenu *selection_menu = new wxMenu;
hartman's avatar
hartman committed
257
    selection_menu->Append( DeleteSelection_Event, wxU(_("D&elete")) );
258

259
260
    /* Create our "View" menu */
    ViewMenu();
zorglub's avatar
zorglub committed
261

262
    /* Append the freshly created menus to the menu bar */
263
    wxMenuBar *menubar = new wxMenuBar();
gbazin's avatar
   
gbazin committed
264
    menubar->Append( manage_menu, wxU(_("&Manage")) );
zorglub's avatar
zorglub committed
265
    menubar->Append( sort_menu, wxU(_("S&ort")) );
gbazin's avatar
   
gbazin committed
266
    menubar->Append( selection_menu, wxU(_("&Selection")) );
267
    menubar->Append( p_view_menu, wxU(_("&View items") ) );
268
269
270
271

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

272
    /* Create the popup menu */
273
274
275
276
277
278
279
    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")) );
280
    node_popup->Append( PopupAddNode_Event, wxU(_("Add node")) );
281
282
283
284
285
286

    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")) );
zorglub's avatar
zorglub committed
287

gbazin's avatar
   
gbazin committed
288
289
290
291
    /* Create a panel to put everything in */
    wxPanel *playlist_panel = new wxPanel( this, -1 );
    playlist_panel->SetAutoLayout( TRUE );

292
293
    /* Create the toolbar */
    wxToolBar *toolbar =
294
        CreateToolBar( wxTB_HORIZONTAL | wxTB_FLAT );
295
296
297
298
299

    /* Create the random tool */
    toolbar->AddTool( Random_Event, wxT(""), wxBitmap(shuffle_on_xpm),
                       wxBitmap(shuffle_on_xpm), wxITEM_CHECK,
                       wxU(_(HELP_SHUFFLE) ) );
gbazin's avatar
   
gbazin committed
300
    var_Get( p_intf, "random", &val );
301
    toolbar->ToggleTool( Random_Event, val.b_bool );
302

303
304
305
306
    /* Create the Loop tool */
    toolbar->AddTool( Loop_Event, wxT(""), wxBitmap( loop_xpm),
                      wxBitmap( loop_xpm), wxITEM_CHECK,
                      wxU(_(HELP_LOOP )  ) );
307
    var_Get( p_intf, "loop", &val );
308
    toolbar->ToggleTool( Loop_Event, val.b_bool );
309

310
    /* Create the Repeat one checkbox */
311
312
313
    toolbar->AddTool( Repeat_Event, wxT(""), wxBitmap( repeat_xpm),
                      wxBitmap( repeat_xpm), wxITEM_CHECK,
                      wxU(_(HELP_REPEAT )  ) );
314
    var_Get( p_intf, "repeat", &val );
315
    toolbar->ToggleTool( Repeat_Event, val.b_bool ) ;
316

zorglub's avatar
zorglub committed
317
    /* Create the Search Textbox */
318
319
320
    search_text = new wxTextCtrl( toolbar, SearchText_Event, wxT(""),
                                  wxDefaultPosition, wxSize(100, -1),
                                  wxTE_PROCESS_ENTER);
zorglub's avatar
zorglub committed
321
322

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

325
326
    toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
                         wxSize(16, 16), wxBORDER_NONE ) );
327
    toolbar->AddControl( search_text );
328
329
    toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
                         wxSize(5, 5), wxBORDER_NONE ) );
330
    toolbar->AddControl( search_button );
331
    search_button->SetDefault();
332
    toolbar->Realize();
zorglub's avatar
zorglub committed
333

334
335
    /* Create the tree */
    treectrl = new wxTreeCtrl( playlist_panel, TreeCtrl_Event,
336
                               wxDefaultPosition, wxDefaultSize,
337
338
339
340
341
342
                               wxTR_HIDE_ROOT | wxTR_LINES_AT_ROOT|
                               wxTR_NO_LINES |
                               wxTR_HAS_BUTTONS | wxTR_TWIST_BUTTONS |
                               wxTR_MULTIPLE | wxTR_EXTENDED );

    /* Create image list */
343
    wxImageList *p_images = new wxImageList( 16 , 16, TRUE );
zorglub's avatar
zorglub committed
344
345
346

    /* FIXME: absolutely needs to be in the right order FIXME */
    p_images->Add( wxIcon( type_unknown_xpm ) );
zorglub's avatar
zorglub committed
347
348
    p_images->Add( wxIcon( type_afile_xpm ) );
    p_images->Add( wxIcon( type_vfile_xpm ) );
349
    p_images->Add( wxIcon( type_directory_xpm ) );
zorglub's avatar
zorglub committed
350
    p_images->Add( wxIcon( type_disc_xpm ) );
zorglub's avatar
zorglub committed
351
    p_images->Add( wxIcon( type_cdda_xpm ) );
352
    p_images->Add( wxIcon( type_card_xpm ) );
zorglub's avatar
zorglub committed
353
354
    p_images->Add( wxIcon( type_net_xpm ) );
    p_images->Add( wxIcon( type_playlist_xpm ) );
zorglub's avatar
zorglub committed
355
    p_images->Add( wxIcon( type_node_xpm ) );
356
357
358
    treectrl->AssignImageList( p_images );

    treectrl->AddRoot( wxU(_("root" )), -1, -1, NULL );
zorglub's avatar
zorglub committed
359

360
    /* Reduce font size */
361
    wxFont font= treectrl->GetFont();
362
    font.SetPointSize(9);
363
    treectrl->SetFont( font );
zorglub's avatar
zorglub committed
364

gbazin's avatar
   
gbazin committed
365
    wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
366
    panel_sizer->Add( treectrl, 1, wxEXPAND | wxALL, 5 );
gbazin's avatar
   
gbazin committed
367
    panel_sizer->Layout();
zorglub's avatar
zorglub committed
368

gbazin's avatar
   
gbazin committed
369
    playlist_panel->SetSizerAndFit( panel_sizer );
370

zorglub's avatar
zorglub committed
371
372
373
374
    int pi_widths[1] =  { -1 };
    statusbar = CreateStatusBar( 1 );
    statusbar->SetStatusWidths( 1, pi_widths );

375
#if wxUSE_DRAG_AND_DROP
gbazin's avatar
   
gbazin committed
376
    /* Associate drop targets with the playlist */
377
    SetDropTarget( new PlaylistFileDropTarget( this ) );
378
379
    menubar->SetDropTarget( new PlaylistFileDropTarget( this ) );
    toolbar->SetDropTarget( new PlaylistFileDropTarget( this ) );
gbazin's avatar
   
gbazin committed
380
#endif
gbazin's avatar
   
gbazin committed
381

382
    i_saved_id = -1;
383

384

zorglub's avatar
zorglub committed
385
    /* We want to be noticed of playlist changes */
386
387

    /* Some global changes happened -> Rebuild all */
gbazin's avatar
   
gbazin committed
388
    var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
389
390
391
392
393
394
395

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

396
    var_AddCallback( p_playlist, "item-append", ItemAppended, this );
397
    var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
398

399
    /* Update the playlist */
400
    Rebuild( VLC_TRUE );
zorglub's avatar
zorglub committed
401

402
403
404
405
}

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

408
    if( p_playlist == NULL ) return;
gbazin's avatar
   
gbazin committed
409

hartman's avatar
hartman committed
410
411
    var_DelCallback( p_playlist, "item-change", ItemChanged, this );
    var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
gbazin's avatar
   
gbazin committed
412
    var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
413
    var_DelCallback( p_playlist, "item-append", ItemAppended, this );
414
    var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
gbazin's avatar
   
gbazin committed
415
    vlc_object_release( p_playlist );
416
417
}

418
/**********************************************************************
zorglub's avatar
zorglub committed
419
 * Update functions
420
 **********************************************************************/
zorglub's avatar
zorglub committed
421
422

/* Update a node */
423
void Playlist::UpdateNode( playlist_item_t *p_node, wxTreeItemId node )
424
{
425
    wxTreeItemIdValue cookie;
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
    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 */
441
            CreateNode( p_node->pp_children[i], node );
442
443
444
445
            /* Keep the tree pointer up to date */
            child = treectrl->GetNextChild( node, cookie );
        }
    }
446
    treectrl->SetItemImage( node, p_node->input.i_type );
447
448

}
zorglub's avatar
zorglub committed
449

450
/* Creates the node p_node as last child of parent */
451
void Playlist::CreateNode( playlist_item_t *p_node, wxTreeItemId parent )
452
{
453
454
455
    wxTreeItemId node =
        treectrl->AppendItem( parent, wxL2U( p_node->input.psz_name ),
                              -1,-1, new PlaylistItem( p_node ) );
456
    treectrl->SetItemImage( node, p_node->input.i_type );
457

458
    UpdateNodeChildren( p_node, node );
zorglub's avatar
zorglub committed
459
460
}

zorglub's avatar
zorglub committed
461
/* Update all children (recursively) of this node */
462
void Playlist::UpdateNodeChildren( playlist_item_t *p_node,
zorglub's avatar
zorglub committed
463
464
465
                                   wxTreeItemId node )
{

466
467
468
469
470
    for( int i = 0; i< p_node->i_children ; i++ )
    {
        /* Append the item */
        if( p_node->pp_children[i]->i_children == -1 )
        {
471
472
473
474
            wxTreeItemId item =
                treectrl->AppendItem( node,
                    wxL2U( p_node->pp_children[i]->input.psz_name ), -1,-1,
                           new PlaylistItem( p_node->pp_children[i]) );
475

476
            UpdateTreeItem( item );
477
478
479
        }
        else
        {
480
            CreateNode( p_node->pp_children[i], node );
481
482
483
484
        }
    }
}

zorglub's avatar
zorglub committed
485
/* Update an item in the tree */
486
void Playlist::UpdateTreeItem( wxTreeItemId item )
zorglub's avatar
zorglub committed
487
{
488
489
    if( ! item.IsOk() ) return;

490
491
    wxTreeItemData *p_data = treectrl->GetItemData( item );
    if( !p_data ) return;
dionoea's avatar
dionoea committed
492

493
494
495
    LockPlaylist( p_intf->p_sys, p_playlist );
    playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
                                          ((PlaylistItem *)p_data)->i_id );
496
497
498
499
500
    if( !p_item )
    {
        UnlockPlaylist( p_intf->p_sys, p_playlist );
        return;
    }
501

zorglub's avatar
zorglub committed
502
    wxString msg;
503
    wxString duration = wxU( "" );
504
    char *psz_author = vlc_input_item_GetInfo( &p_item->input,
505
                                               _(VLC_META_INFO_CAT), _(VLC_META_ARTIST) );
506
507
508
    if( !psz_author )
    {
        UnlockPlaylist( p_intf->p_sys, p_playlist );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
509
        return;
510
511
    }

zorglub's avatar
zorglub committed
512
513
    char psz_duration[MSTRTIME_MAX_SIZE];
    mtime_t dur = p_item->input.i_duration;
514

zorglub's avatar
zorglub committed
515
    if( dur != -1 )
516
    {
zorglub's avatar
zorglub committed
517
        secstotimestr( psz_duration, dur/1000000 );
518
        duration.Append( wxU( " ( " ) +  wxString( wxU( psz_duration ) ) +
519
                         wxU( " )" ) );
520
    }
521

zorglub's avatar
zorglub committed
522
    if( !strcmp( psz_author, "" ) || p_item->input.b_fixed_name == VLC_TRUE )
523
    {
524
        msg = wxString( wxU( p_item->input.psz_name ) ) + duration;
525
    }
zorglub's avatar
zorglub committed
526
527
    else
    {
528
        msg = wxString(wxU( psz_author )) + wxT(" - ") +
529
              wxString(wxU(p_item->input.psz_name)) + duration;
zorglub's avatar
zorglub committed
530
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
531
    free( psz_author );
zorglub's avatar
zorglub committed
532
    treectrl->SetItemText( item , msg );
533
    treectrl->SetItemImage( item, p_item->input.i_type );
534

zorglub's avatar
zorglub committed
535
    if( p_playlist->status.p_item == p_item )
536
    {
537
        treectrl->SetItemBold( item, true );
538
539
540
541
542
        while( treectrl->GetItemParent( item ).IsOk() )
        {
            item = treectrl->GetItemParent( item );
            treectrl->Expand( item );
        }
zorglub's avatar
zorglub committed
543
544
545
546
    }
    else
    {
        treectrl->SetItemBold( item, false );
547
    }
548
    UnlockPlaylist( p_intf->p_sys, p_playlist );
549
550
}

551
/* Process a AppendIt em request */
552
553
554
void Playlist::AppendItem( wxCommandEvent& event )
{
    playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
zorglub's avatar
zorglub committed
555
    playlist_item_t *p_item = NULL;
556
    wxTreeItemId item, node;
557

558
    i_items_to_append--;
559

560
561
562
563
    /* No need to do anything if the playlist is going to be rebuilt */
    if( b_need_update ) return;

    if( p_add->i_view != i_current_view ) goto update;
564

zorglub's avatar
zorglub committed
565
    node = FindItem( treectrl->GetRootItem(), p_add->i_node );
566
    if( !node.IsOk() ) goto update;
567

zorglub's avatar
zorglub committed
568
    p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
569
570
571
572
    if( !p_item ) goto update;

    item = FindItem( treectrl->GetRootItem(), p_add->i_item );
    if( item.IsOk() ) goto update;
zorglub's avatar
zorglub committed
573

zorglub's avatar
zorglub committed
574
    item = treectrl->AppendItem( node,
zorglub's avatar
zorglub committed
575
576
577
                                 wxL2U( p_item->input.psz_name ), -1,-1,
                                 new PlaylistItem( p_item ) );
    treectrl->SetItemImage( item, p_item->input.i_type );
578

zorglub's avatar
zorglub committed
579
    if( item.IsOk() && p_item->i_children == -1 )
580
    {
581
        UpdateTreeItem( item );
582
    }
zorglub's avatar
zorglub committed
583

zorglub's avatar
zorglub committed
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
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 );
    }
zorglub's avatar
zorglub committed
605

zorglub's avatar
zorglub committed
606
    return;
607
608
}

zorglub's avatar
zorglub committed
609
/* Process a updateitem request */
610
611
void Playlist::UpdateItem( int i )
{
gbazin's avatar
   
gbazin committed
612
    if( i < 0 ) return; /* Sanity check */
dionoea's avatar
dionoea committed
613

614
    wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
615

zorglub's avatar
zorglub committed
616
617
    if( item.IsOk() )
    {
618
        UpdateTreeItem( item );
zorglub's avatar
zorglub committed
619
    }
620
621
}

622
623
624
void Playlist::RemoveItem( int i )
{
    if( i <= 0 ) return; /* Sanity check */
625
    if( i == i_saved_id ) i_saved_id = -1;
626
627
628
629
630
631

    wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );

    if( item.IsOk() )
    {
        treectrl->Delete( item );
632
    }
633
}
634
635


zorglub's avatar
zorglub committed
636
/**********************************************************************
637
 * Search functions (internal)
zorglub's avatar
zorglub committed
638
 **********************************************************************/
zorglub's avatar
zorglub committed
639

640
641
642
/* Find a wxItem from a playlist id */
wxTreeItemId Playlist::FindItem( wxTreeItemId root, int i_id )
{
643
    wxTreeItemIdValue cookie;
644
645
646
647
648
649
650
651
652
653
654
655
    PlaylistItem *p_wxcurrent;
    wxTreeItemId search;
    wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
    wxTreeItemId child;

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

    if( i_id < 0 )
    {
        wxTreeItemId dummy;
        return dummy;
    }
656
657
658
659
    if( i_saved_id == i_id )
    {
        return saved_tree_item;
    }
660
661

    if( !p_wxcurrent )
662
    {
zorglub's avatar
zorglub committed
663
664
        wxTreeItemId dummy;
        return dummy;
dionoea's avatar
dionoea committed
665
    }
666
667
668

    if( p_wxcurrent->i_id == i_id )
    {
669
670
        i_saved_id = i_id;
        saved_tree_item = root;
671
        return root;
672
673
    }

zorglub's avatar
zorglub committed
674
    while( item.IsOk() )
675
    {
zorglub's avatar
zorglub committed
676
        p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
677
        if( p_wxcurrent->i_id == i_id )
zorglub's avatar
zorglub committed
678
        {
679
680
            i_saved_id = i_id;
            saved_tree_item = item;
zorglub's avatar
zorglub committed
681
682
683
684
            return item;
        }
        if( treectrl->ItemHasChildren( item ) )
        {
685
            wxTreeItemId search = FindItem( item, i_id );
zorglub's avatar
zorglub committed
686
687
            if( search.IsOk() )
            {
688
689
                i_saved_id = i_id;
                saved_tree_item = search;
zorglub's avatar
zorglub committed
690
691
692
693
                return search;
            }
        }
        item = treectrl->GetNextChild( root, cookie );
694
    }
zorglub's avatar
zorglub committed
695
696
697
698
699
700
701
    /* Not found */
    wxTreeItemId dummy;
    return dummy;
}

int Playlist::CountItems( wxTreeItemId root )
{
702
    wxTreeItemIdValue cookie;
zorglub's avatar
zorglub committed
703
704
    int count = 0;
    wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
dionoea's avatar
dionoea committed
705

zorglub's avatar
zorglub committed
706
    while( item.IsOk() )
707
    {
zorglub's avatar
zorglub committed
708
709
710
711
        if( treectrl->ItemHasChildren( item ) )
        {
            count += CountItems( item );
        }
712
713
714
715
716
717
718
719
720
        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 );
        }
zorglub's avatar
zorglub committed
721
        item = treectrl->GetNextChild( root, cookie );
722
    }
zorglub's avatar
zorglub committed
723
724
725
726
727
728
    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 )
{
729
    wxTreeItemIdValue cookie;
zorglub's avatar
zorglub committed
730
731
732
733
734
    wxTreeItemId search;
    wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
    wxTreeItemId child;

    while( item.IsOk() )
735
    {
zorglub's avatar
zorglub committed
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
        if( treectrl->GetItemText( item).Lower().Contains(
                                                 search_string.Lower() ) )
        {
            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 = FindItemByName( item, search_string, current,
                                                  pb_current_found );
            if( search.IsOk() )
            {
                return search;
            }
        }
        item = treectrl->GetNextChild( root, cookie);
758
    }
zorglub's avatar
zorglub committed
759
760
761
    /* Not found */
    wxTreeItemId dummy;
    return dummy;
762
763
}

zorglub's avatar
zorglub committed
764
765
766
/**********************************************************************
 * Rebuild the playlist
 **********************************************************************/
767
void Playlist::Rebuild( vlc_bool_t b_root )
768
{
769
    playlist_view_t *p_view;
770

771
772
    i_items_to_append = 0;

773
774
    /* We can remove the callbacks before locking, anyway, we won't
     * miss anything */
775
776
777
778
779
780
781
    if( b_root )
    {
        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 );
782

783
        /* ...and rebuild it */
784
        LockPlaylist( p_intf->p_sys, p_playlist );
785
    }
786
    i_saved_id = -1;
787
788
789
790
791
792
793
794
795

    p_view = playlist_ViewFind( p_playlist, i_current_view ); /* FIXME */

    /* HACK we should really get new*/
    treectrl->DeleteAllItems();
    treectrl->AddRoot( wxU(_("root" )), -1, -1,
                         new PlaylistItem( p_view->p_root) );

    wxTreeItemId root = treectrl->GetRootItem();
796
    UpdateNode( p_view->p_root, root );
797

zorglub's avatar
zorglub committed
798
    int i_count = CountItems( treectrl->GetRootItem() );
799

zorglub's avatar
zorglub committed
800
801
802
803
    if( i_count < p_playlist->i_size && !b_changed_view )
    {
        i_current_view = VIEW_CATEGORY;
        b_changed_view = VLC_TRUE;
804
        Rebuild( VLC_FALSE );
zorglub's avatar
zorglub committed
805
806
    }
    else if( i_count != p_playlist->i_size )
zorglub's avatar
zorglub committed
807
    {
zorglub's avatar
zorglub committed
808
        statusbar->SetStatusText( wxString::Format( wxU(_(
zorglub's avatar
zorglub committed
809
810
                                  "%i items in playlist (%i not shown)")),
                                  p_playlist->i_size,
zorglub's avatar
zorglub committed
811
                                  p_playlist->i_size - i_count ) );
zorglub's avatar
zorglub committed
812
813
814
    }
    else
    {
sigmunau's avatar
sigmunau committed
815
        statusbar->SetStatusText( wxString::Format( wxU(_(
zorglub's avatar
zorglub committed
816
817
                                  "%i items in playlist")),
                                  p_playlist->i_size ), 0 );
zorglub's avatar
zorglub committed
818
    }
zorglub's avatar
zorglub committed
819

820
821
822
823
824
825
826
827
    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 );
828

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

zorglub's avatar
zorglub committed
833
834


gbazin's avatar
   
gbazin committed
835
void Playlist::ShowPlaylist( bool show )
836
{