]> git.sesse.net Git - vlc/blob - modules/gui/wxwidgets/dialogs/playlist.cpp
modules/gui/wxwidgets/dialogs/playlist.cpp: add some more checks. Fixes infinite...
[vlc] / modules / gui / wxwidgets / dialogs / playlist.cpp
1 /*****************************************************************************
2  * playlist.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
8  *          Clément Stenac <zorglub@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/OR MODIFy
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include "dialogs/playlist.hpp"
29 #include "dialogs/iteminfo.hpp"
30 #include "interface.hpp" // Needed for D&D - TODO: Split
31
32 #include "bitmaps/shuffle.xpm"
33 #include "bitmaps/repeat.xpm"
34 #include "bitmaps/loop.xpm"
35
36 #include "bitmaps/type_unknown.xpm"
37 #include "bitmaps/type_afile.xpm"
38 #include "bitmaps/type_vfile.xpm"
39 #include "bitmaps/type_net.xpm"
40 #include "bitmaps/type_card.xpm"
41 #include "bitmaps/type_disc.xpm"
42 #include "bitmaps/type_cdda.xpm"
43 #include "bitmaps/type_directory.xpm"
44 #include "bitmaps/type_playlist.xpm"
45 #include "bitmaps/type_node.xpm"
46
47 #include <wx/dynarray.h>
48 #include <wx/imaglist.h>
49
50 #include <vlc_meta.h>
51
52 #define HELP_SHUFFLE N_( "Shuffle" )
53 #define HELP_LOOP N_( "Repeat All" )
54 #define HELP_REPEAT N_( "Repeat One" )
55
56 namespace wxvlc {
57 /* Callback prototype */
58 static int PlaylistChanged( vlc_object_t *, const char *,
59                             vlc_value_t, vlc_value_t, void * );
60 static int PlaylistNext( vlc_object_t *, const char *,
61                          vlc_value_t, vlc_value_t, void * );
62 static int ItemChanged( vlc_object_t *, const char *,
63                         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  * Event Table.
71  *****************************************************************************/
72
73 /* IDs for the controls and the menu commands */
74 enum
75 {
76     /* menu items */
77     AddFile_Event = 1,
78     AddDir_Event,
79     AddMRL_Event,
80     Close_Event,
81     Open_Event,
82     Save_Event,
83
84     SortTitle_Event,
85     RSortTitle_Event,
86     Randomize_Event,
87
88     DeleteSelection_Event,
89     Random_Event,
90     Loop_Event,
91     Repeat_Event,
92
93     PopupPlay_Event,
94     PopupPlayThis_Event,
95     PopupPreparse_Event,
96     PopupSort_Event,
97     PopupDel_Event,
98     PopupInfo_Event,
99     PopupAddNode_Event,
100
101     SearchText_Event,
102     Search_Event,
103
104     /* controls */
105     TreeCtrl_Event,
106
107     Browse_Event,  /* For export playlist */
108
109     /* custom events */
110     UpdateItem_Event,
111     AppendItem_Event,
112     RemoveItem_Event,
113
114     MenuDummy_Event = wxID_HIGHEST + 999,
115
116     FirstView_Event = wxID_HIGHEST + 1000,
117     LastView_Event = wxID_HIGHEST + 1100,
118
119     FirstSD_Event = wxID_HIGHEST + 2000,
120     LastSD_Event = wxID_HIGHEST + 2100,
121 };
122
123 DEFINE_LOCAL_EVENT_TYPE( wxEVT_PLAYLIST );
124
125 BEGIN_EVENT_TABLE(Playlist, wxFrame)
126     EVT_SIZE(Playlist::OnSize)
127
128     /* Menu events */
129     EVT_MENU(AddFile_Event, Playlist::OnAddFile)
130     EVT_MENU(AddDir_Event, Playlist::OnAddDir)
131     EVT_MENU(AddMRL_Event, Playlist::OnAddMRL)
132     EVT_MENU(Close_Event, Playlist::OnMenuClose)
133     EVT_MENU(Open_Event, Playlist::OnOpen)
134     EVT_MENU(Save_Event, Playlist::OnSave)
135
136     EVT_MENU(SortTitle_Event, Playlist::OnSort)
137     EVT_MENU(RSortTitle_Event, Playlist::OnSort)
138
139     EVT_MENU(Randomize_Event, Playlist::OnSort)
140
141     EVT_MENU(DeleteSelection_Event, Playlist::OnDeleteSelection)
142
143     EVT_MENU_OPEN( Playlist::OnMenuOpen )
144     EVT_MENU( -1, Playlist::OnMenuEvent )
145
146     EVT_TOOL(Random_Event, Playlist::OnRandom)
147     EVT_TOOL(Repeat_Event, Playlist::OnRepeat)
148     EVT_TOOL(Loop_Event, Playlist::OnLoop)
149
150     /* Popup events */
151     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     EVT_MENU( PopupDel_Event, Playlist::OnPopupDel)
156     EVT_MENU( PopupInfo_Event, Playlist::OnPopupInfo)
157     EVT_MENU( PopupAddNode_Event, Playlist::OnPopupAddNode)
158
159     /* Tree control events */
160     EVT_TREE_ITEM_ACTIVATED( TreeCtrl_Event, Playlist::OnActivateItem )
161     EVT_TREE_KEY_DOWN( -1, Playlist::OnKeyDown )
162     EVT_TREE_BEGIN_DRAG( TreeCtrl_Event, Playlist::OnDragItemBegin )
163     EVT_TREE_END_DRAG( TreeCtrl_Event, Playlist::OnDragItemEnd )
164
165     EVT_CONTEXT_MENU( Playlist::OnPopup )
166
167     /* Button events */
168     EVT_BUTTON( Search_Event, Playlist::OnSearch)
169     EVT_BUTTON( Save_Event, Playlist::OnSave)
170
171     /*EVT_TEXT( SearchText_Event, Playlist::OnSearchTextChange )*/
172     EVT_TEXT_ENTER( SearchText_Event, Playlist::OnSearch )
173
174     /* Custom events */
175     EVT_COMMAND(-1, wxEVT_PLAYLIST, Playlist::OnPlaylistEvent)
176
177     /* Special events : we don't want to destroy the window when the user
178      * clicks on (X) */
179     EVT_CLOSE(Playlist::OnClose)
180 END_EVENT_TABLE()
181
182 /*****************************************************************************
183  * PlaylistItem class
184  ****************************************************************************/
185 class PlaylistItem : public wxTreeItemData
186 {
187 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 };
196
197 /*****************************************************************************
198  * Constructor.
199  *****************************************************************************/
200 Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
201     wxFrame( p_parent, -1, wxU(_("Playlist")), wxDefaultPosition,
202              wxSize(500,300), wxDEFAULT_FRAME_STYLE )
203 {
204     vlc_value_t val;
205
206     /* Initializations */
207     p_intf = _p_intf;
208     pp_sds = NULL;
209     i_update_counter = 0;
210     i_sort_mode = MODE_NONE;
211     b_need_update = VLC_FALSE;
212     i_items_to_append = 0;
213     p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
214                                                 FIND_ANYWHERE );
215     if( p_playlist == NULL ) return;
216
217     SetIcon( *p_intf->p_sys->p_icon );
218
219     p_view_menu = NULL;
220     p_sd_menu = SDMenu();
221
222     i_current_view = VIEW_CATEGORY;
223     b_changed_view = VLC_FALSE;
224
225     i_title_sorted = 0;
226     i_group_sorted = 0;
227     i_duration_sorted = 0;
228
229     var_Create( p_intf, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
230     var_Create( p_intf, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
231     var_Create( p_intf, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );;
232
233     /* Create our "Manage" menu */
234     wxMenu *manage_menu = new wxMenu;
235     manage_menu->Append( AddFile_Event, wxU(_("&Simple Add File...")) );
236     manage_menu->Append( AddDir_Event, wxU(_("Add &Directory...")) );
237     manage_menu->Append( AddMRL_Event, wxU(_("&Add MRL...")) );
238     manage_menu->AppendSeparator();
239     manage_menu->Append( MenuDummy_Event, wxU(_("Services discovery")),
240                          p_sd_menu );
241     manage_menu->AppendSeparator();
242     manage_menu->Append( Open_Event, wxU(_("&Open Playlist...")) );
243     manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
244     manage_menu->AppendSeparator();
245     manage_menu->Append( Close_Event, wxU(_("&Close")) );
246
247     /* Create our "Sort" menu */
248     wxMenu *sort_menu = new wxMenu;
249     sort_menu->Append( SortTitle_Event, wxU(_("Sort by &title")) );
250     sort_menu->Append( RSortTitle_Event, wxU(_("&Reverse sort by title")) );
251     sort_menu->AppendSeparator();
252     sort_menu->Append( Randomize_Event, wxU(_("&Shuffle Playlist")) );
253
254     /* Create our "Selection" menu */
255     wxMenu *selection_menu = new wxMenu;
256     selection_menu->Append( DeleteSelection_Event, wxU(_("D&elete")) );
257
258     /* Create our "View" menu */
259     ViewMenu();
260
261     /* Append the freshly created menus to the menu bar */
262     wxMenuBar *menubar = new wxMenuBar();
263     menubar->Append( manage_menu, wxU(_("&Manage")) );
264     menubar->Append( sort_menu, wxU(_("S&ort")) );
265     menubar->Append( selection_menu, wxU(_("&Selection")) );
266     menubar->Append( p_view_menu, wxU(_("&View items") ) );
267
268     /* Attach the menu bar to the frame */
269     SetMenuBar( menubar );
270
271     /* Create the popup menu */
272     node_popup = new wxMenu;
273     node_popup->Append( PopupPlay_Event, wxU(_("Play")) );
274     node_popup->Append( PopupPlayThis_Event, wxU(_("Play this branch")) );
275     node_popup->Append( PopupPreparse_Event, wxU(_("Preparse")) );
276     node_popup->Append( PopupSort_Event, wxU(_("Sort this branch")) );
277     node_popup->Append( PopupDel_Event, wxU(_("Delete")) );
278     node_popup->Append( PopupInfo_Event, wxU(_("Info")) );
279     node_popup->Append( PopupAddNode_Event, wxU(_("Add node")) );
280
281     item_popup = new wxMenu;
282     item_popup->Append( PopupPlay_Event, wxU(_("Play")) );
283     item_popup->Append( PopupPreparse_Event, wxU(_("Preparse")) );
284     item_popup->Append( PopupDel_Event, wxU(_("Delete")) );
285     item_popup->Append( PopupInfo_Event, wxU(_("Info")) );
286
287     /* Create a panel to put everything in */
288     wxPanel *playlist_panel = new wxPanel( this, -1 );
289     playlist_panel->SetAutoLayout( TRUE );
290
291     /* Create the toolbar */
292     wxToolBar *toolbar =
293         CreateToolBar( wxTB_HORIZONTAL | wxTB_FLAT );
294
295     /* Create the random tool */
296     toolbar->AddTool( Random_Event, wxT(""), wxBitmap(shuffle_on_xpm),
297                        wxBitmap(shuffle_on_xpm), wxITEM_CHECK,
298                        wxU(_(HELP_SHUFFLE) ) );
299     var_Get( p_intf, "random", &val );
300     toolbar->ToggleTool( Random_Event, val.b_bool );
301
302     /* Create the Loop tool */
303     toolbar->AddTool( Loop_Event, wxT(""), wxBitmap( loop_xpm),
304                       wxBitmap( loop_xpm), wxITEM_CHECK,
305                       wxU(_(HELP_LOOP )  ) );
306     var_Get( p_intf, "loop", &val );
307     toolbar->ToggleTool( Loop_Event, val.b_bool );
308
309     /* Create the Repeat one checkbox */
310     toolbar->AddTool( Repeat_Event, wxT(""), wxBitmap( repeat_xpm),
311                       wxBitmap( repeat_xpm), wxITEM_CHECK,
312                       wxU(_(HELP_REPEAT )  ) );
313     var_Get( p_intf, "repeat", &val );
314     toolbar->ToggleTool( Repeat_Event, val.b_bool ) ;
315
316     /* Create the Search Textbox */
317     search_text = new wxTextCtrl( toolbar, SearchText_Event, wxT(""),
318                                   wxDefaultPosition, wxSize(100, -1),
319                                   wxTE_PROCESS_ENTER);
320
321     /* Create the search button */
322     search_button = new wxButton( toolbar , Search_Event, wxU(_("Search")) );
323
324     toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
325                          wxSize(16, 16), wxBORDER_NONE ) );
326     toolbar->AddControl( search_text );
327     toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
328                          wxSize(5, 5), wxBORDER_NONE ) );
329     toolbar->AddControl( search_button );
330     search_button->SetDefault();
331     toolbar->Realize();
332
333     /* Create the tree */
334     treectrl = new wxTreeCtrl( playlist_panel, TreeCtrl_Event,
335                                wxDefaultPosition, wxDefaultSize,
336                                wxTR_HIDE_ROOT | wxTR_LINES_AT_ROOT|
337                                wxTR_NO_LINES |
338                                wxTR_HAS_BUTTONS | wxTR_TWIST_BUTTONS |
339                                wxTR_MULTIPLE | wxTR_EXTENDED );
340
341     /* Create image list */
342     wxImageList *p_images = new wxImageList( 16 , 16, TRUE );
343
344     /* FIXME: absolutely needs to be in the right order FIXME */
345     p_images->Add( wxIcon( type_unknown_xpm ) );
346     p_images->Add( wxIcon( type_afile_xpm ) );
347     p_images->Add( wxIcon( type_vfile_xpm ) );
348     p_images->Add( wxIcon( type_directory_xpm ) );
349     p_images->Add( wxIcon( type_disc_xpm ) );
350     p_images->Add( wxIcon( type_cdda_xpm ) );
351     p_images->Add( wxIcon( type_card_xpm ) );
352     p_images->Add( wxIcon( type_net_xpm ) );
353     p_images->Add( wxIcon( type_playlist_xpm ) );
354     p_images->Add( wxIcon( type_node_xpm ) );
355     treectrl->AssignImageList( p_images );
356
357     treectrl->AddRoot( wxU(_("root" )), -1, -1, NULL );
358
359     /* Reduce font size */
360     wxFont font= treectrl->GetFont();
361     font.SetPointSize(9);
362     treectrl->SetFont( font );
363
364     wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
365     panel_sizer->Add( treectrl, 1, wxEXPAND | wxALL, 5 );
366     panel_sizer->Layout();
367
368     playlist_panel->SetSizerAndFit( panel_sizer );
369
370     int pi_widths[1] =  { -1 };
371     statusbar = CreateStatusBar( 1 );
372     statusbar->SetStatusWidths( 1, pi_widths );
373
374 #if wxUSE_DRAG_AND_DROP
375     /* Associate drop targets with the playlist */
376     SetDropTarget( new DragAndDrop( p_intf, VLC_TRUE ) );
377 #endif
378
379     i_saved_id = -1;
380
381
382     /* We want to be noticed of playlist changes */
383
384     /* Some global changes happened -> Rebuild all */
385     var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
386
387     /* We went to the next item */
388     var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
389
390     /* One item has been updated */
391     var_AddCallback( p_playlist, "item-change", ItemChanged, this );
392
393     var_AddCallback( p_playlist, "item-append", ItemAppended, this );
394     var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
395
396     /* Update the playlist */
397     Rebuild( VLC_TRUE );
398
399 }
400
401 Playlist::~Playlist()
402 {
403     if( pp_sds != NULL ) free( pp_sds );
404
405     if( p_playlist == NULL ) return;
406
407     var_DelCallback( p_playlist, "item-change", ItemChanged, this );
408     var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
409     var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
410     var_DelCallback( p_playlist, "item-append", ItemAppended, this );
411     var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
412     vlc_object_release( p_playlist );
413 }
414
415 /**********************************************************************
416  * Update functions
417  **********************************************************************/
418
419 /* Update a node */
420 void Playlist::UpdateNode( playlist_item_t *p_node, wxTreeItemId node )
421 {
422     wxTreeItemIdValue cookie;
423     wxTreeItemId child;
424     for( int i = 0; i< p_node->i_children ; i++ )
425     {
426         if( i == 0 )
427         {
428             child = treectrl->GetFirstChild( node, cookie);
429         }
430         else
431         {
432             child = treectrl->GetNextChild( node, cookie );
433         }
434
435         if( !child.IsOk() )
436         {
437             /* Not enough children */
438             CreateNode( p_node->pp_children[i], node );
439             /* Keep the tree pointer up to date */
440             child = treectrl->GetNextChild( node, cookie );
441         }
442     }
443     treectrl->SetItemImage( node, p_node->input.i_type );
444
445 }
446
447 /* Creates the node p_node as last child of parent */
448 void Playlist::CreateNode( playlist_item_t *p_node, wxTreeItemId parent )
449 {
450     wxTreeItemId node =
451         treectrl->AppendItem( parent, wxL2U( p_node->input.psz_name ),
452                               -1,-1, new PlaylistItem( p_node ) );
453     treectrl->SetItemImage( node, p_node->input.i_type );
454
455     UpdateNodeChildren( p_node, node );
456 }
457
458 /* Update all children (recursively) of this node */
459 void Playlist::UpdateNodeChildren( playlist_item_t *p_node,
460                                    wxTreeItemId node )
461 {
462
463     for( int i = 0; i< p_node->i_children ; i++ )
464     {
465         /* Append the item */
466         if( p_node->pp_children[i]->i_children == -1 )
467         {
468             wxTreeItemId item =
469                 treectrl->AppendItem( node,
470                     wxL2U( p_node->pp_children[i]->input.psz_name ), -1,-1,
471                            new PlaylistItem( p_node->pp_children[i]) );
472
473             UpdateTreeItem( item );
474         }
475         else
476         {
477             CreateNode( p_node->pp_children[i], node );
478         }
479     }
480 }
481
482 /* Update an item in the tree */
483 void Playlist::UpdateTreeItem( wxTreeItemId item )
484 {
485     if( ! item.IsOk() ) return;
486
487     wxTreeItemData *p_data = treectrl->GetItemData( item );
488     if( !p_data ) return;
489
490     LockPlaylist( p_intf->p_sys, p_playlist );
491     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
492                                           ((PlaylistItem *)p_data)->i_id );
493     if( !p_item )
494     {
495         UnlockPlaylist( p_intf->p_sys, p_playlist );
496         return;
497     }
498
499     wxString msg;
500     wxString duration = wxU( "" );
501     char *psz_author = vlc_input_item_GetInfo( &p_item->input,
502                                                _(VLC_META_INFO_CAT), _(VLC_META_ARTIST) );
503     if( !psz_author )
504     {
505         UnlockPlaylist( p_intf->p_sys, p_playlist );
506         return;
507     }
508
509     char psz_duration[MSTRTIME_MAX_SIZE];
510     mtime_t dur = p_item->input.i_duration;
511
512     if( dur != -1 )
513     {
514         secstotimestr( psz_duration, dur/1000000 );
515         duration.Append( wxU( " ( " ) +  wxString( wxU( psz_duration ) ) +
516                          wxU( " )" ) );
517     }
518
519     if( !strcmp( psz_author, "" ) || p_item->input.b_fixed_name == VLC_TRUE )
520     {
521         msg = wxString( wxU( p_item->input.psz_name ) ) + duration;
522     }
523     else
524     {
525         msg = wxString(wxU( psz_author )) + wxT(" - ") +
526               wxString(wxU(p_item->input.psz_name)) + duration;
527     }
528     free( psz_author );
529     treectrl->SetItemText( item , msg );
530     treectrl->SetItemImage( item, p_item->input.i_type );
531
532     if( p_playlist->status.p_item == p_item )
533     {
534         treectrl->SetItemBold( item, true );
535         while( treectrl->GetItemParent( item ).IsOk() )
536         {
537             item = treectrl->GetItemParent( item );
538             treectrl->Expand( item );
539         }
540     }
541     else
542     {
543         treectrl->SetItemBold( item, false );
544     }
545     UnlockPlaylist( p_intf->p_sys, p_playlist );
546 }
547
548 /* Process a AppendIt em request */
549 void Playlist::AppendItem( wxCommandEvent& event )
550 {
551     playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
552     playlist_item_t *p_item = NULL;
553     wxTreeItemId item, node;
554
555     i_items_to_append--;
556
557     /* No need to do anything if the playlist is going to be rebuilt */
558     if( b_need_update ) return;
559
560     if( p_add->i_view != i_current_view ) goto update;
561
562     node = FindItem( treectrl->GetRootItem(), p_add->i_node );
563     if( !node.IsOk() ) goto update;
564
565     p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
566     if( !p_item ) goto update;
567
568     item = FindItem( treectrl->GetRootItem(), p_add->i_item );
569     if( item.IsOk() ) goto update;
570
571     item = treectrl->AppendItem( node,
572                                  wxL2U( p_item->input.psz_name ), -1,-1,
573                                  new PlaylistItem( p_item ) );
574     treectrl->SetItemImage( item, p_item->input.i_type );
575
576     if( item.IsOk() && p_item->i_children == -1 )
577     {
578         UpdateTreeItem( item );
579     }
580
581 update:
582     int i_count = CountItems( treectrl->GetRootItem());
583     if( i_count != p_playlist->i_size )
584     {
585         statusbar->SetStatusText( wxString::Format( wxU(_(
586                                   "%i items in playlist (%i not shown)")),
587                                   p_playlist->i_size,
588                                   p_playlist->i_size - i_count ) );
589         if( !b_changed_view )
590         {
591             i_current_view = VIEW_CATEGORY;
592             b_changed_view = VLC_TRUE;
593             b_need_update = VLC_TRUE;
594         }
595     }
596     else
597     {
598         statusbar->SetStatusText( wxString::Format( wxU(_(
599                                   "%i items in playlist")),
600                                   p_playlist->i_size ), 0 );
601     }
602
603     return;
604 }
605
606 /* Process a updateitem request */
607 void Playlist::UpdateItem( int i )
608 {
609     if( i < 0 ) return; /* Sanity check */
610
611     wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
612
613     if( item.IsOk() )
614     {
615         UpdateTreeItem( item );
616     }
617 }
618
619 void Playlist::RemoveItem( int i )
620 {
621     if( i <= 0 ) return; /* Sanity check */
622     if( i == i_saved_id ) i_saved_id = -1;
623
624     wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
625
626     if( item.IsOk() )
627     {
628         treectrl->Delete( item );
629     }
630 }
631
632
633 /**********************************************************************
634  * Search functions (internal)
635  **********************************************************************/
636
637 /* Find a wxItem from a playlist id */
638 wxTreeItemId Playlist::FindItem( wxTreeItemId root, int i_id )
639 {
640     wxTreeItemIdValue cookie;
641     PlaylistItem *p_wxcurrent;
642     wxTreeItemId search;
643     wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
644     wxTreeItemId child;
645
646     p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
647
648     if( i_id < 0 )
649     {
650         wxTreeItemId dummy;
651         return dummy;
652     }
653     if( i_saved_id == i_id )
654     {
655         return saved_tree_item;
656     }
657
658     if( !p_wxcurrent )
659     {
660         wxTreeItemId dummy;
661         return dummy;
662     }
663
664     if( p_wxcurrent->i_id == i_id )
665     {
666         i_saved_id = i_id;
667         saved_tree_item = root;
668         return root;
669     }
670
671     while( item.IsOk() )
672     {
673         p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
674         if( p_wxcurrent->i_id == i_id )
675         {
676             i_saved_id = i_id;
677             saved_tree_item = item;
678             return item;
679         }
680         if( treectrl->ItemHasChildren( item ) )
681         {
682             wxTreeItemId search = FindItem( item, i_id );
683             if( search.IsOk() )
684             {
685                 i_saved_id = i_id;
686                 saved_tree_item = search;
687                 return search;
688             }
689         }
690         item = treectrl->GetNextChild( root, cookie );
691     }
692     /* Not found */
693     wxTreeItemId dummy;
694     return dummy;
695 }
696
697 int Playlist::CountItems( wxTreeItemId root )
698 {
699     wxTreeItemIdValue cookie;
700     int count = 0;
701     wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
702
703     while( item.IsOk() )
704     {
705         if( treectrl->ItemHasChildren( item ) )
706         {
707             count += CountItems( item );
708         }
709         else
710         {
711             playlist_item_t *p_item;
712             LockPlaylist( p_intf->p_sys, p_playlist );
713             p_item = playlist_ItemGetById( p_playlist, ((PlaylistItem *)treectrl->GetItemData( item ))->i_id );
714             if( p_item && p_item->i_children == -1 )
715                 count++;
716             UnlockPlaylist( p_intf->p_sys, p_playlist );
717         }
718         item = treectrl->GetNextChild( root, cookie );
719     }
720     return count;
721 }
722
723 /* Find a wxItem from a name (from current) */
724 wxTreeItemId Playlist::FindItemByName( wxTreeItemId root, wxString search_string, wxTreeItemId current, vlc_bool_t *pb_current_found )
725 {
726     wxTreeItemIdValue cookie;
727     wxTreeItemId search;
728     wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
729     wxTreeItemId child;
730
731     while( item.IsOk() )
732     {
733         if( treectrl->GetItemText( item).Lower().Contains(
734                                                  search_string.Lower() ) )
735         {
736             if( !current.IsOk() || *pb_current_found == VLC_TRUE )
737             {
738                 return item;
739             }
740             else if( current.IsOk() && item == current )
741             {
742                 *pb_current_found = VLC_TRUE;
743             }
744         }
745         if( treectrl->ItemHasChildren( item ) )
746         {
747             wxTreeItemId search = FindItemByName( item, search_string, current,
748                                                   pb_current_found );
749             if( search.IsOk() )
750             {
751                 return search;
752             }
753         }
754         item = treectrl->GetNextChild( root, cookie);
755     }
756     /* Not found */
757     wxTreeItemId dummy;
758     return dummy;
759 }
760
761 /**********************************************************************
762  * Rebuild the playlist
763  **********************************************************************/
764 void Playlist::Rebuild( vlc_bool_t b_root )
765 {
766     playlist_view_t *p_view;
767
768     i_items_to_append = 0;
769
770     /* We can remove the callbacks before locking, anyway, we won't
771      * miss anything */
772     if( b_root )
773     {
774         var_DelCallback( p_playlist, "item-change", ItemChanged, this );
775         var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
776         var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
777         var_DelCallback( p_playlist, "item-append", ItemAppended, this );
778         var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
779
780         /* ...and rebuild it */
781         LockPlaylist( p_intf->p_sys, p_playlist );
782     }
783     i_saved_id = -1;
784
785     p_view = playlist_ViewFind( p_playlist, i_current_view ); /* FIXME */
786
787     /* HACK we should really get new*/
788     treectrl->DeleteAllItems();
789     treectrl->AddRoot( wxU(_("root" )), -1, -1,
790                          new PlaylistItem( p_view->p_root) );
791
792     wxTreeItemId root = treectrl->GetRootItem();
793     UpdateNode( p_view->p_root, root );
794
795     int i_count = CountItems( treectrl->GetRootItem() );
796
797     if( i_count < p_playlist->i_size && !b_changed_view )
798     {
799         i_current_view = VIEW_CATEGORY;
800         b_changed_view = VLC_TRUE;
801         Rebuild( VLC_FALSE );
802     }
803     else if( i_count != p_playlist->i_size )
804     {
805         statusbar->SetStatusText( wxString::Format( wxU(_(
806                                   "%i items in playlist (%i not shown)")),
807                                   p_playlist->i_size,
808                                   p_playlist->i_size - i_count ) );
809     }
810     else
811     {
812         statusbar->SetStatusText( wxString::Format( wxU(_(
813                                   "%i items in playlist")),
814                                   p_playlist->i_size ), 0 );
815     }
816
817     if( b_root )
818     {
819         /* Put callbacks back online */
820         var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
821         var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
822         var_AddCallback( p_playlist, "item-change", ItemChanged, this );
823         var_AddCallback( p_playlist, "item-append", ItemAppended, this );
824         var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
825
826         UnlockPlaylist( p_intf->p_sys, p_playlist );
827     }
828 }
829
830
831
832 void Playlist::ShowPlaylist( bool show )
833 {
834     if( show ) Rebuild( VLC_TRUE );
835     Show( show );
836 }
837
838 /* This function is called on a regular basis */
839 void Playlist::UpdatePlaylist()
840 {
841     i_update_counter++;
842
843     /* If the playlist isn't show there's no need to update it */
844     if( !IsShown() ) return;
845
846     if( this->b_need_update )
847     {
848         this->b_need_update = VLC_FALSE;
849         Rebuild( VLC_TRUE );
850     }
851
852     /* Updating the playing status every 0.5s is enough */
853     if( i_update_counter % 5 ) return;
854 }
855
856 /*****************************************************************************
857  * Private methods.
858  *****************************************************************************/
859 void Playlist::DeleteTreeItem( wxTreeItemId item )
860 {
861    PlaylistItem *p_wxitem;
862    playlist_item_t *p_item;
863    p_wxitem = (PlaylistItem *)treectrl->GetItemData( item );
864
865    LockPlaylist( p_intf->p_sys, p_playlist );
866    p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id );
867
868    if( !p_item )
869    {
870        UnlockPlaylist( p_intf->p_sys, p_playlist );
871        return;
872    }
873
874    if( p_item->i_children == -1 ) DeleteItem( p_item->input.i_id );
875    else DeleteNode( p_item );
876
877    RemoveItem( item );
878    UnlockPlaylist( p_intf->p_sys, p_playlist );
879 }
880
881 void Playlist::DeleteItem( int item_id )
882 {
883     playlist_Delete( p_playlist, item_id );
884 }
885
886 void Playlist::DeleteNode( playlist_item_t *p_item )
887 {
888     playlist_NodeDelete( p_playlist, p_item, VLC_TRUE , VLC_FALSE );
889 }
890
891 void Playlist::OnMenuClose( wxCommandEvent& event )
892 {
893     wxCloseEvent cevent;
894     OnClose(cevent);
895 }
896
897 void Playlist::OnClose( wxCloseEvent& WXUNUSED(event) )
898 {
899     Hide();
900 }
901
902 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
903 {
904     struct {
905         char *psz_desc;
906         char *psz_filter;
907         char *psz_module;
908     } formats[] = {{ _("M3U file"), "*.m3u", "export-m3u" }};
909
910     wxString filter = wxT("");
911
912     if( p_playlist->i_size == 0 )
913     {
914         wxMessageBox( wxU(_("Playlist is empty") ), wxU(_("Can't save")),
915                       wxICON_WARNING | wxOK, this );
916         return;
917     }
918
919     for( unsigned int i = 0; i < sizeof(formats)/sizeof(formats[0]); i++)
920     {
921         filter.Append( wxU(formats[i].psz_desc) );
922         filter.Append( wxT("|") );
923         filter.Append( wxU(formats[i].psz_filter) );
924         filter.Append( wxT("|") );
925     }
926     wxFileDialog dialog( this, wxU(_("Save playlist")),
927                          wxT(""), wxT(""), filter, wxSAVE );
928
929     if( dialog.ShowModal() == wxID_OK )
930     {
931         if( dialog.GetPath().mb_str() )
932         {
933             playlist_Export( p_playlist, dialog.GetPath().mb_str(),
934                              formats[dialog.GetFilterIndex()].psz_module );
935         }
936     }
937
938 }
939
940 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
941 {
942     wxFileDialog dialog( this, wxU(_("Open playlist")), wxT(""), wxT(""),
943         wxT("All playlists|*.pls;*.m3u;*.asx;*.b4s|M3U files|*.m3u"), wxOPEN );
944
945     if( dialog.ShowModal() == wxID_OK )
946     {
947         playlist_Import( p_playlist, dialog.GetPath().mb_str() );
948     }
949 }
950
951 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
952 {
953     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
954
955 }
956
957 void Playlist::OnAddDir( wxCommandEvent& WXUNUSED(event) )
958 {
959     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_DIRECTORY, 0, 0 );
960
961 }
962
963 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
964 {
965     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
966
967 }
968
969 /********************************************************************
970  * Sorting functions
971  ********************************************************************/
972 void Playlist::OnSort( wxCommandEvent& event )
973 {
974     PlaylistItem *p_wxitem;
975     p_wxitem = (PlaylistItem *)treectrl->GetItemData( treectrl->GetRootItem() );
976
977     LockPlaylist( p_intf->p_sys, p_playlist );
978     switch( event.GetId() )
979     {
980         case SortTitle_Event:
981             playlist_RecursiveNodeSort( p_playlist,
982                             playlist_ItemGetById( p_playlist, p_wxitem->i_id ),
983                             SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
984             break;
985         case RSortTitle_Event:
986             playlist_RecursiveNodeSort( p_playlist,
987                             playlist_ItemGetById( p_playlist, p_wxitem->i_id ),
988                             SORT_TITLE_NODES_FIRST, ORDER_REVERSE );
989     }
990     UnlockPlaylist( p_intf->p_sys, p_playlist );
991
992     Rebuild( VLC_TRUE );
993 }
994
995 /**********************************************************************
996  * Search functions (user)
997  **********************************************************************/
998 /*void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
999 {
1000    search_button->SetDefault();
1001 }*/
1002
1003 void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
1004 {
1005     wxString search_string = search_text->GetValue();
1006
1007     vlc_bool_t pb_found = VLC_FALSE;
1008
1009     wxTreeItemId found =
1010      FindItemByName( treectrl->GetRootItem(), search_string,
1011                      search_current, &pb_found );
1012
1013     if( !found.IsOk() )
1014     {
1015         wxTreeItemId dummy;
1016         search_current = dummy;
1017         found =  FindItemByName( treectrl->GetRootItem(), search_string,
1018                                  search_current, &pb_found );
1019     }
1020
1021     if( found.IsOk() )
1022     {
1023         search_current = found;
1024         treectrl->EnsureVisible( found );
1025         treectrl->UnselectAll();
1026         treectrl->SelectItem( found, true );
1027     }
1028 }
1029
1030 /**********************************************************************
1031  * Selection functions
1032  **********************************************************************/
1033 void Playlist::RecursiveDeleteSelection(  wxTreeItemId root )
1034 {
1035     wxTreeItemIdValue cookie;
1036     wxTreeItemId child = treectrl->GetFirstChild( root, cookie );
1037     while( child.IsOk() )
1038     {
1039         if( treectrl->ItemHasChildren( child ) )
1040         {
1041             RecursiveDeleteSelection( child );
1042             if( treectrl->IsSelected(child ) ) DeleteTreeItem( child );
1043         }
1044         else if( treectrl->IsSelected( child ) )
1045             DeleteTreeItem( child );
1046         child = treectrl->GetNextChild( root, cookie );
1047     }
1048 }
1049
1050 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
1051 {
1052     RecursiveDeleteSelection( treectrl->GetRootItem() );
1053 }
1054
1055 /**********************************************************************
1056  * Playlist mode functions
1057  **********************************************************************/
1058 void Playlist::OnRandom( wxCommandEvent& event )
1059 {
1060     vlc_value_t val;
1061     val.b_bool = event.IsChecked();
1062     var_Set( p_playlist, "random", val);
1063 }
1064
1065 void Playlist::OnLoop( wxCommandEvent& event )
1066 {
1067     vlc_value_t val;
1068     val.b_bool = event.IsChecked();
1069     var_Set( p_playlist, "loop", val);
1070 }
1071
1072 void Playlist::OnRepeat( wxCommandEvent& event )
1073 {
1074     vlc_value_t val;
1075     val.b_bool = event.IsChecked();
1076     var_Set( p_playlist, "repeat", val);
1077 }
1078
1079 /********************************************************************
1080  * Event
1081  ********************************************************************/
1082 void Playlist::OnActivateItem( wxTreeEvent& event )
1083 {
1084     playlist_item_t *p_item,*p_node,*p_item2,*p_node2;
1085
1086     PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1087                                                             event.GetItem() );
1088     wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
1089
1090     PlaylistItem *p_wxparent = (PlaylistItem *)treectrl->GetItemData( parent );
1091
1092     LockPlaylist( p_intf->p_sys, p_playlist );
1093
1094     if( !( p_wxitem && p_wxparent ) )
1095     {
1096         UnlockPlaylist( p_intf->p_sys, p_playlist );
1097         return;
1098     }
1099
1100     p_item2 = playlist_ItemGetById(p_playlist, p_wxitem->i_id);
1101     p_node2 = playlist_ItemGetById(p_playlist, p_wxparent->i_id);
1102     if( p_item2 && p_item2->i_children == -1 )
1103     {
1104         p_node = p_node2;
1105         p_item = p_item2;
1106     }
1107     else
1108     {
1109         p_node = p_item2;
1110         p_item = NULL;
1111 /*        if( p_node && p_node->i_children > 0 &&
1112             p_node->pp_children[0]->i_children == -1)
1113         {
1114             p_item = p_node->pp_children[0];
1115         }
1116         else
1117         {
1118             p_item = NULL;
1119         }*/
1120     }
1121
1122     playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view,
1123                       p_node, p_item );
1124     UnlockPlaylist( p_intf->p_sys, p_playlist );
1125 }
1126
1127 void Playlist::OnKeyDown( wxTreeEvent& event )
1128 {
1129     long keycode = event.GetKeyCode();
1130     /* Delete selected items */
1131     if( keycode == WXK_BACK || keycode == WXK_DELETE )
1132     {
1133         /* We send a dummy event */
1134         OnDeleteSelection( event );
1135     }
1136     else
1137     {
1138         event.Skip();
1139     }
1140 }
1141
1142 void Playlist::OnEnDis( wxCommandEvent& event )
1143 {
1144     msg_Warn( p_intf, "not implemented" );
1145 }
1146
1147 void Playlist::OnDragItemBegin( wxTreeEvent& event )
1148 {
1149     event.Allow();
1150     draged_tree_item = event.GetItem();
1151 }
1152
1153 void Playlist::OnDragItemEnd( wxTreeEvent& event )
1154 {
1155     wxTreeItemId dest_tree_item = event.GetItem();
1156
1157     if( !dest_tree_item.IsOk() ) return;
1158
1159     /* check that we're not trying to move a node into one of it's children */
1160     wxTreeItemId parent = dest_tree_item;
1161     while( parent != treectrl->GetRootItem() )
1162     {
1163         if( draged_tree_item == parent ) return;
1164         parent = treectrl->GetItemParent( parent );
1165     }
1166
1167     LockPlaylist( p_intf->p_sys, p_playlist );
1168
1169     PlaylistItem *p_wxdrageditem =
1170         (PlaylistItem *)treectrl->GetItemData( draged_tree_item );
1171     PlaylistItem *p_wxdestitem =
1172         (PlaylistItem *)treectrl->GetItemData( dest_tree_item );
1173     if( !p_wxdrageditem || !p_wxdestitem )
1174     {
1175         UnlockPlaylist( p_intf->p_sys, p_playlist );
1176         return;
1177     }
1178
1179     playlist_item_t *p_drageditem =
1180         playlist_ItemGetById(p_playlist, p_wxdrageditem->i_id );
1181     playlist_item_t *p_destitem =
1182         playlist_ItemGetById(p_playlist, p_wxdestitem->i_id );
1183     if( !p_drageditem || !p_destitem )
1184     {
1185         UnlockPlaylist( p_intf->p_sys, p_playlist );
1186         return;
1187     }
1188
1189     if( p_destitem->i_children == -1 )
1190     /* this is a leaf */
1191     {
1192         parent = treectrl->GetItemParent( dest_tree_item );
1193         PlaylistItem *p_parent =
1194             (PlaylistItem *)treectrl->GetItemData( parent );
1195         if( !p_parent )
1196         {
1197             UnlockPlaylist( p_intf->p_sys, p_playlist );
1198             return;
1199         }
1200         playlist_item_t *p_destitem2 =
1201             playlist_ItemGetById( p_playlist, p_parent->i_id );
1202         if( !p_destitem2 )
1203         {
1204             UnlockPlaylist( p_intf->p_sys, p_playlist );
1205             return;
1206         }
1207         int i;
1208         for( i = 0; i < p_destitem2->i_children; i++ )
1209         {
1210             if( p_destitem2->pp_children[i] == p_destitem ) break;
1211         }
1212         playlist_TreeMove( p_playlist, p_drageditem, p_destitem2,
1213                            i, i_current_view );
1214     }
1215     else
1216     /* this is a node */
1217     {
1218         playlist_TreeMove( p_playlist, p_drageditem, p_destitem,
1219                            0, i_current_view );
1220     }
1221
1222     UnlockPlaylist( p_intf->p_sys, p_playlist );
1223
1224     Rebuild( VLC_TRUE );
1225 }
1226
1227 /**********************************************************************
1228  * Menu
1229  **********************************************************************/
1230
1231 void Playlist::OnMenuOpen( wxMenuEvent& event)
1232 {
1233 #if defined( __WXMSW__ )
1234 #   define GetEventObject GetMenu
1235 #endif
1236
1237     if( event.GetEventObject() == p_view_menu )
1238     {
1239         p_view_menu = ViewMenu();
1240     }
1241 #if defined( __WXMSW__ )
1242 #   undef GetEventObject
1243 #endif
1244 }
1245
1246 void Playlist::OnMenuEvent( wxCommandEvent& event )
1247 {
1248     if( event.GetId() < FirstView_Event )
1249     {
1250         event.Skip();
1251         return;
1252     }
1253     else if( event.GetId() < LastView_Event )
1254     {
1255
1256         int i_new_view = event.GetId() - FirstView_Event;
1257
1258         playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_new_view );
1259
1260         if( p_view != NULL )
1261         {
1262             b_changed_view = VLC_TRUE;
1263             i_current_view = i_new_view;
1264             playlist_ViewUpdate( p_playlist, i_new_view );
1265             Rebuild( VLC_TRUE );
1266             return;
1267         }
1268         else if( i_new_view >= VIEW_FIRST_SORTED &&
1269                  i_new_view <= VIEW_LAST_SORTED )
1270         {
1271             b_changed_view = VLC_TRUE;
1272             playlist_ViewInsert( p_playlist, i_new_view, "View" );
1273             playlist_ViewUpdate( p_playlist, i_new_view );
1274
1275             i_current_view = i_new_view;
1276
1277             Rebuild( VLC_TRUE );
1278         }
1279     }
1280     else if( event.GetId() >= FirstSD_Event && event.GetId() < LastSD_Event )
1281     {
1282         if( !playlist_IsServicesDiscoveryLoaded( p_playlist,
1283                                 pp_sds[event.GetId() - FirstSD_Event] ) )
1284         {
1285             playlist_ServicesDiscoveryAdd( p_playlist,
1286                             pp_sds[event.GetId() - FirstSD_Event] );
1287         }
1288         else
1289         {
1290             //wxMutexGuiLeave();
1291             playlist_ServicesDiscoveryRemove( p_playlist,
1292                             pp_sds[event.GetId() - FirstSD_Event] );
1293             //wxMutexGuiEnter();
1294         }
1295     }
1296 }
1297
1298 wxMenu * Playlist::ViewMenu()
1299 {
1300     if( !p_view_menu )
1301     {
1302         p_view_menu = new wxMenu;
1303     }
1304     else
1305     {
1306         wxMenuItemList::Node *node = p_view_menu->GetMenuItems().GetFirst();
1307         for( ; node; )
1308         {
1309             wxMenuItem *item = node->GetData();
1310             node = node->GetNext();
1311             p_view_menu->Delete( item );
1312         }
1313     }
1314
1315     /* FIXME : have a list of "should have" views */
1316     p_view_menu->Append( FirstView_Event + VIEW_CATEGORY,
1317                            wxU(_("Normal") ) );
1318     p_view_menu->Append( FirstView_Event + VIEW_S_AUTHOR,
1319                            wxU(_("Sorted by artist") ) );
1320     p_view_menu->Append( FirstView_Event + VIEW_S_ALBUM,
1321                            wxU(_("Sorted by Album") ) );
1322
1323     return p_view_menu;
1324 }
1325
1326 wxMenu *Playlist::SDMenu()
1327 {
1328     p_sd_menu = new wxMenu;
1329
1330     vlc_list_t *p_list = vlc_list_find( p_playlist, VLC_OBJECT_MODULE,
1331                                         FIND_ANYWHERE );
1332
1333     int i_number = 0;
1334     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1335     {
1336         module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1337
1338         if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1339             i_number++;
1340     }
1341     if( i_number ) pp_sds = (char **)calloc( i_number, sizeof(void *) );
1342
1343     i_number = 0;
1344     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1345     {
1346         module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1347
1348         if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1349         {
1350             p_sd_menu->AppendCheckItem( FirstSD_Event + i_number ,
1351                 wxU( p_parser->psz_longname ? p_parser->psz_longname :
1352                      (p_parser->psz_shortname ?
1353                       p_parser->psz_shortname : p_parser->psz_object_name) ) );
1354
1355             if( playlist_IsServicesDiscoveryLoaded( p_playlist,
1356                                     p_parser->psz_object_name ) )
1357             {
1358                 p_sd_menu->Check( FirstSD_Event + i_number, TRUE );
1359             }
1360
1361             pp_sds[i_number++] = p_parser->psz_object_name;
1362         }
1363     }
1364     vlc_list_release( p_list );
1365     return p_sd_menu;
1366 }
1367
1368
1369 /*****************************************************************************
1370  * Popup management functions
1371  *****************************************************************************/
1372 void Playlist::OnPopup( wxContextMenuEvent& event )
1373 {
1374     wxPoint pt = event.GetPosition();
1375     playlist_item_t *p_item;
1376
1377     i_wx_popup_item = treectrl->HitTest( ScreenToClient( pt ) );
1378     if( i_wx_popup_item.IsOk() )
1379     {
1380         PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1381                                                             i_wx_popup_item );
1382         PlaylistItem *p_wxparent= (PlaylistItem *)treectrl->GetItemData(
1383                                   treectrl->GetItemParent( i_wx_popup_item ) );
1384         i_popup_item = p_wxitem->i_id;
1385         i_popup_parent = p_wxparent->i_id;
1386         treectrl->SelectItem( i_wx_popup_item );
1387
1388         LockPlaylist( p_intf->p_sys, p_playlist );
1389         p_item = playlist_ItemGetById( p_playlist, i_popup_item );
1390
1391         if( !p_item )
1392         {
1393             UnlockPlaylist( p_intf->p_sys, p_playlist );
1394             return;
1395         }
1396         if( p_item->i_children == -1 )
1397         {
1398             UnlockPlaylist( p_intf->p_sys, p_playlist );
1399             Playlist::PopupMenu( item_popup,
1400                                  ScreenToClient( wxGetMousePosition() ) );
1401         }
1402         else
1403         {
1404             UnlockPlaylist( p_intf->p_sys, p_playlist );
1405             Playlist::PopupMenu( node_popup,
1406                                  ScreenToClient( wxGetMousePosition() ) );
1407         }
1408     }
1409 }
1410
1411 void Playlist::OnPopupPlay( wxCommandEvent& event )
1412 {
1413     playlist_item_t *p_popup_item, *p_popup_parent;
1414     LockPlaylist( p_intf->p_sys, p_playlist );
1415     p_popup_item = playlist_ItemGetById( p_playlist, i_popup_item );
1416     p_popup_parent = playlist_ItemGetById( p_playlist, i_popup_parent );
1417     if( p_popup_item != NULL )
1418     {
1419         if( p_popup_item->i_children > -1 )
1420         {
1421             if( event.GetId() == PopupPlay_Event &&
1422                 p_popup_item->i_children > 0 )
1423             {
1424                 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1425                                   i_current_view, p_popup_item,
1426                                   p_popup_item->pp_children[0] );
1427             }
1428             else
1429             {
1430                 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1431                                   i_current_view, p_popup_item, NULL );
1432             }
1433         }
1434         else
1435         {
1436             if( event.GetId() == PopupPlay_Event )
1437             {
1438                 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1439                                   i_current_view, p_popup_parent,
1440                                   p_popup_item );
1441             }
1442         }
1443     }
1444     UnlockPlaylist( p_intf->p_sys, p_playlist );
1445 }
1446
1447 void Playlist::OnPopupPreparse( wxCommandEvent& event )
1448 {
1449     Preparse();
1450 }
1451
1452 void Playlist::Preparse()
1453 {
1454     playlist_item_t *p_popup_item;
1455     LockPlaylist( p_intf->p_sys, p_playlist );
1456     p_popup_item = playlist_ItemGetById( p_playlist, i_popup_item );
1457
1458     if( p_popup_item != NULL )
1459     {
1460         if( p_popup_item->i_children == -1 )
1461         {
1462             playlist_PreparseEnqueue( p_playlist, &p_popup_item->input );
1463         }
1464         else
1465         {
1466             int i = 0;
1467             playlist_item_t *p_parent = p_popup_item;
1468             for( i = 0; i< p_parent->i_children ; i++ )
1469             {
1470                 wxMenuEvent dummy;
1471                 i_wx_popup_item = FindItem( treectrl->GetRootItem(),
1472                                          p_parent->pp_children[i]->input.i_id );
1473                 i_popup_item = p_parent->pp_children[i]->input.i_id;
1474                 Preparse();
1475             }
1476         }
1477     }
1478     UnlockPlaylist( p_intf->p_sys, p_playlist );
1479 }
1480
1481 void Playlist::OnPopupDel( wxCommandEvent& event )
1482 {
1483     DeleteTreeItem( i_wx_popup_item );
1484 }
1485
1486 void Playlist::OnPopupSort( wxCommandEvent& event )
1487 {
1488     PlaylistItem *p_wxitem;
1489     playlist_item_t *p_item;
1490
1491     p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_wx_popup_item );
1492     LockPlaylist( p_intf->p_sys, p_playlist );
1493
1494     p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id );
1495     if( p_item->i_children >= 0 )
1496     {
1497         playlist_RecursiveNodeSort( p_playlist, p_item,
1498                                     SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
1499
1500         treectrl->DeleteChildren( i_wx_popup_item );
1501         i_saved_id = -1;
1502         UpdateNodeChildren( p_item, i_wx_popup_item );
1503
1504     }
1505     UnlockPlaylist( p_intf->p_sys, p_playlist );
1506 }
1507
1508 void Playlist::OnPopupInfo( wxCommandEvent& event )
1509 {
1510     LockPlaylist( p_intf->p_sys, p_playlist );
1511     playlist_item_t *p_popup_item = playlist_ItemGetById( p_playlist, i_popup_item );
1512     if( p_popup_item )
1513     {
1514         iteminfo_dialog = new ItemInfoDialog( p_intf, p_popup_item, this );
1515         if( iteminfo_dialog->ShowModal() == wxID_OK )
1516         {
1517             UpdateItem( i_wx_popup_item );
1518         }
1519         delete iteminfo_dialog;
1520     }
1521     UnlockPlaylist( p_intf->p_sys, p_playlist );
1522 }
1523
1524 void Playlist::OnPopupAddNode( wxCommandEvent& event )
1525 {
1526     wxTextEntryDialog text( NULL, wxU(_( "Please enter node name" )),
1527         wxU(_( "Add node" )), wxU(_( "New node" )) );
1528     if( text.ShowModal() != wxID_OK ) return;
1529
1530     char *psz_name = wxFromLocale( text.GetValue() );
1531
1532     LockPlaylist( p_intf->p_sys, p_playlist );
1533
1534     PlaylistItem *p_wxitem;
1535     playlist_item_t *p_item;
1536
1537     p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_wx_popup_item );
1538
1539     p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id );
1540
1541     playlist_NodeCreate( p_playlist, 0, psz_name, p_item );
1542
1543     UnlockPlaylist( p_intf->p_sys, p_playlist );
1544     Rebuild( VLC_TRUE );
1545
1546     wxLocaleFree( psz_name );
1547 }
1548
1549
1550 /*****************************************************************************
1551  * Custom events management
1552  *****************************************************************************/
1553 void Playlist::OnPlaylistEvent( wxCommandEvent& event )
1554 {
1555     switch( event.GetId() )
1556     {
1557         case UpdateItem_Event:
1558             UpdateItem( event.GetInt() );
1559             break;
1560         case AppendItem_Event:
1561             AppendItem( event );
1562             break;
1563         case RemoveItem_Event:
1564             RemoveItem( event.GetInt() );
1565             break;
1566     }
1567 }
1568
1569 /*****************************************************************************
1570  * PlaylistChanged: callback triggered by the intf-change playlist variable
1571  *  We don't rebuild the playlist directly here because we don't want the
1572  *  caller to block for a too long time.
1573  *****************************************************************************/
1574 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1575                             vlc_value_t oval, vlc_value_t nval, void *param )
1576 {
1577     Playlist *p_playlist_dialog = (Playlist *)param;
1578     p_playlist_dialog->b_need_update = VLC_TRUE;
1579     return VLC_SUCCESS;
1580 }
1581
1582 /*****************************************************************************
1583  * Next: callback triggered by the playlist-current playlist variable
1584  *****************************************************************************/
1585 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
1586                          vlc_value_t oval, vlc_value_t nval, void *param )
1587 {
1588     Playlist *p_playlist_dialog = (Playlist *)param;
1589
1590     wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1591     event.SetInt( oval.i_int );
1592     p_playlist_dialog->AddPendingEvent( event );
1593     event.SetInt( nval.i_int );
1594     p_playlist_dialog->AddPendingEvent( event );
1595
1596     return 0;
1597 }
1598
1599 /*****************************************************************************
1600  * ItemChanged: callback triggered by the item-change playlist variable
1601  *****************************************************************************/
1602 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
1603                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1604 {
1605     Playlist *p_playlist_dialog = (Playlist *)param;
1606
1607     wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1608     event.SetInt( new_val.i_int );
1609     p_playlist_dialog->AddPendingEvent( event );
1610
1611     return 0;
1612 }
1613 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
1614                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1615 {
1616     Playlist *p_playlist_dialog = (Playlist *)param;
1617
1618     wxCommandEvent event( wxEVT_PLAYLIST, RemoveItem_Event );
1619     event.SetInt( new_val.i_int );
1620     p_playlist_dialog->AddPendingEvent( event );
1621
1622     return 0;
1623 }
1624
1625 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
1626                          vlc_value_t oval, vlc_value_t nval, void *param )
1627 {
1628     Playlist *p_playlist_dialog = (Playlist *)param;
1629
1630     playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
1631     memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
1632
1633     if( ++p_playlist_dialog->i_items_to_append >= 50 )
1634     {
1635         /* Too many items waiting to be added, it will be quicker to rebuild
1636          * the whole playlist */
1637         p_playlist_dialog->b_need_update = VLC_TRUE;
1638         return VLC_SUCCESS;
1639     }
1640
1641     wxCommandEvent event( wxEVT_PLAYLIST, AppendItem_Event );
1642     event.SetClientData( (void *)p_add );
1643     p_playlist_dialog->AddPendingEvent( event );
1644
1645     return VLC_SUCCESS;
1646 }
1647 }