]> git.sesse.net Git - vlc/blob - modules/gui/wxwidgets/playlist_manager.cpp
* Protect input item's meta through setters and getters. That allows tracking of...
[vlc] / modules / gui / wxwidgets / playlist_manager.cpp
1 /*****************************************************************************
2  * playlist_manager.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  *          Gildas Bazin <gbazin@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/OR MODIFy
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include "playlist_manager.hpp"
30 #include "interface.hpp"
31
32 #include "bitmaps/type_unknown.xpm"
33 #include "bitmaps/type_afile.xpm"
34 #include "bitmaps/type_vfile.xpm"
35 #include "bitmaps/type_net.xpm"
36 #include "bitmaps/type_card.xpm"
37 #include "bitmaps/type_disc.xpm"
38 #include "bitmaps/type_cdda.xpm"
39 #include "bitmaps/type_directory.xpm"
40 #include "bitmaps/type_playlist.xpm"
41 #include "bitmaps/type_node.xpm"
42
43 #include <wx/dynarray.h>
44 #include <wx/imaglist.h>
45 #include "vlc_meta.h"
46
47 namespace wxvlc {
48 /* Callback prototype */
49 static int PlaylistChanged( vlc_object_t *, const char *,
50                             vlc_value_t, vlc_value_t, void * );
51 static int PlaylistNext( vlc_object_t *, const char *,
52                          vlc_value_t, vlc_value_t, void * );
53 static int ItemChanged( vlc_object_t *, const char *,
54                         vlc_value_t, vlc_value_t, void * );
55 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
56                       vlc_value_t oval, vlc_value_t nval, void *param );
57 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
58                       vlc_value_t oval, vlc_value_t nval, void *param );
59
60 /*****************************************************************************
61  * Event Table.
62  *****************************************************************************/
63
64 /* IDs for the controls and the menu commands */
65 enum
66 {
67     TreeCtrl_Event,
68
69     UpdateItem_Event,
70     AppendItem_Event,
71     RemoveItem_Event,
72 };
73
74 DEFINE_LOCAL_EVENT_TYPE( wxEVT_PLAYLIST );
75
76 BEGIN_EVENT_TABLE(PlaylistManager, wxPanel)
77     /* Tree control events */
78     EVT_TREE_ITEM_ACTIVATED( TreeCtrl_Event, PlaylistManager::OnActivateItem )
79
80     /* Custom events */
81     EVT_COMMAND(-1, wxEVT_PLAYLIST, PlaylistManager::OnPlaylistEvent)
82 END_EVENT_TABLE()
83
84 /*****************************************************************************
85  * PlaylistItem class
86  ****************************************************************************/
87 class PlaylistItem : public wxTreeItemData
88 {
89 public:
90     PlaylistItem( playlist_item_t *p_item ) : wxTreeItemData()
91     {
92         i_id = p_item->i_id;
93         i_input_id = p_item->p_input->i_id;
94     }
95 protected:
96     int i_input_id;
97     int i_id;
98 friend class PlaylistManager;
99 };
100
101 /*****************************************************************************
102  * Constructor.
103  *****************************************************************************/
104 PlaylistManager::PlaylistManager( intf_thread_t *_p_intf, wxWindow *p_parent ):
105     wxPanel( p_parent, -1, wxDefaultPosition, wxSize(0,0) )
106 {
107     /* Initializations */
108     p_intf = _p_intf;
109     b_need_update = VLC_FALSE;
110     i_items_to_append = 0;
111     i_cached_item_id = -1;
112     i_update_counter = 0;
113
114     p_playlist = (playlist_t *)
115         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
116     if( p_playlist == NULL ) return;
117
118     var_Create( p_intf, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
119     var_Create( p_intf, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
120     var_Create( p_intf, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );;
121
122     /* Create the tree */
123     treectrl = new wxTreeCtrl( this, TreeCtrl_Event,
124                                wxDefaultPosition, wxDefaultSize,
125                                wxTR_HIDE_ROOT | wxTR_LINES_AT_ROOT|
126                                wxTR_NO_LINES |
127                                wxTR_HAS_BUTTONS | wxTR_TWIST_BUTTONS |
128                                wxTR_MULTIPLE | wxTR_EXTENDED );
129
130     /* Add everything to the panel */
131     sizer = new wxBoxSizer( wxHORIZONTAL );
132     SetSizer( sizer );
133     sizer->Add( treectrl, 1, wxEXPAND );
134     sizer->Layout();
135     sizer->Fit( this );
136
137     /* Create image list */
138     wxImageList *p_images = new wxImageList( 16 , 16, TRUE );
139
140     /* FIXME: absolutely needs to be in the right order FIXME */
141     p_images->Add( wxIcon( type_unknown_xpm ) );
142     p_images->Add( wxIcon( type_afile_xpm ) );
143     p_images->Add( wxIcon( type_vfile_xpm ) );
144     p_images->Add( wxIcon( type_directory_xpm ) );
145     p_images->Add( wxIcon( type_disc_xpm ) );
146     p_images->Add( wxIcon( type_cdda_xpm ) );
147     p_images->Add( wxIcon( type_card_xpm ) );
148     p_images->Add( wxIcon( type_net_xpm ) );
149     p_images->Add( wxIcon( type_playlist_xpm ) );
150     p_images->Add( wxIcon( type_node_xpm ) );
151     treectrl->AssignImageList( p_images );
152
153     /* Reduce font size */
154     wxFont font = treectrl->GetFont(); font.SetPointSize(9);
155     treectrl->SetFont( font );
156
157 #if wxUSE_DRAG_AND_DROP
158     /* Associate drop targets with the playlist */
159     SetDropTarget( new DragAndDrop( p_intf, VLC_TRUE ) );
160 #endif
161
162     /* Update the playlist */
163     Rebuild( VLC_TRUE );
164
165     /*
166      * We want to be notified of playlist changes
167      */
168
169     /* Some global changes happened -> Rebuild all */
170     var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
171
172     /* We went to the next item */
173     var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
174
175     /* One item has been updated */
176     var_AddCallback( p_playlist, "item-change", ItemChanged, this );
177
178     var_AddCallback( p_playlist, "item-append", ItemAppended, this );
179     var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
180 }
181
182 PlaylistManager::~PlaylistManager()
183 {
184     if( p_playlist == NULL ) return;
185
186     var_DelCallback( p_playlist, "item-change", ItemChanged, this );
187     var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
188     var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
189     var_DelCallback( p_playlist, "item-append", ItemAppended, this );
190     var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
191     vlc_object_release( p_playlist );
192 }
193
194 /*****************************************************************************
195  * PlaylistChanged: callback triggered by the intf-change playlist variable
196  *  We don't rebuild the playlist directly here because we don't want the
197  *  caller to block for a too long time.
198  *****************************************************************************/
199 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
200                             vlc_value_t oval, vlc_value_t nval, void *param )
201 {
202     PlaylistManager *p_playlist = (PlaylistManager *)param;
203     p_playlist->b_need_update = VLC_TRUE;
204     return VLC_SUCCESS;
205 }
206
207 /*****************************************************************************
208  * Next: callback triggered by the playlist-current playlist variable
209  *****************************************************************************/
210 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
211                          vlc_value_t oval, vlc_value_t nval, void *param )
212 {
213     PlaylistManager *p_playlist = (PlaylistManager *)param;
214
215     wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
216     event.SetInt( oval.i_int );
217     p_playlist->AddPendingEvent( event );
218     event.SetInt( nval.i_int );
219     p_playlist->AddPendingEvent( event );
220
221     return VLC_SUCCESS;
222 }
223
224 /*****************************************************************************
225  * Update functions
226  *****************************************************************************/
227 void PlaylistManager::CreateNode( playlist_item_t *p_node, wxTreeItemId parent)
228 {
229     wxTreeItemId node =
230         treectrl->AppendItem( parent, wxL2U( p_node->p_input->psz_name ), -1, -1,
231                               new PlaylistItem( p_node ) );
232     treectrl->SetItemImage( node, p_node->p_input->i_type );
233
234     UpdateNodeChildren( p_node, node );
235 }
236
237 void PlaylistManager::UpdateNode( playlist_item_t *p_node, wxTreeItemId node )
238 {
239     wxTreeItemIdValue cookie;
240     wxTreeItemId child;
241
242     for( int i = 0; i < p_node->i_children ; i++ )
243     {
244         if( !i ) child = treectrl->GetFirstChild( node, cookie);
245         else child = treectrl->GetNextChild( node, cookie );
246
247         if( !child.IsOk() )
248         {
249             /* Not enough children */
250             CreateNode( p_node->pp_children[i], node );
251             /* Keep the tree pointer up to date */
252             child = treectrl->GetNextChild( node, cookie );
253         }
254     }
255
256     treectrl->SetItemImage( node, p_node->p_input->i_type );
257
258 }
259
260 void PlaylistManager::UpdateNodeChildren( playlist_item_t *p_node,
261                                           wxTreeItemId node )
262 {
263     for( int i = 0; i< p_node->i_children ; i++ )
264     {
265         /* Append the item */
266         if( p_node->pp_children[i]->i_children == -1 )
267         {
268             wxTreeItemId item =
269                 treectrl->AppendItem( node,
270                     wxL2U( p_node->pp_children[i]->p_input->psz_name ), -1,-1,
271                            new PlaylistItem( p_node->pp_children[i]) );
272
273             UpdateTreeItem( item );
274         }
275         else
276         {
277             CreateNode( p_node->pp_children[i], node );
278         }
279     }
280 }
281
282 void PlaylistManager::UpdateTreeItem( wxTreeItemId item )
283 {
284     if( ! item.IsOk() ) return;
285
286     wxTreeItemData *p_data = treectrl->GetItemData( item );
287     if( !p_data ) return;
288
289     LockPlaylist( p_intf->p_sys, p_playlist );
290     playlist_item_t *p_item =
291         playlist_ItemGetById( p_playlist, ((PlaylistItem *)p_data)->i_id,
292                 VLC_TRUE );
293     if( !p_item )
294     {
295         UnlockPlaylist( p_intf->p_sys, p_playlist );
296         return;
297     }
298
299     wxString msg;
300     wxString duration = wxU( "" );
301
302     char *psz_artist;
303     if( input_item_GetArtist( o_item->p_input ) )
304     {
305         psz_artist = strdup( input_item_GetArtist( o_item->p_input ) );
306     }
307     else psz_artist = strdup( "" );
308
309     if( !psz_artist )
310     {
311         UnlockPlaylist( p_intf->p_sys, p_playlist );
312         return;
313     }
314
315     char psz_duration[MSTRTIME_MAX_SIZE];
316     mtime_t dur = p_item->p_input->i_duration;
317
318     if( dur != -1 )
319     {
320         secstotimestr( psz_duration, dur/1000000 );
321         duration.Append( wxU( " ( " ) +  wxString( wxU( psz_duration ) ) +
322                          wxU( " )" ) );
323     }
324
325     if( !strcmp( psz_artist, "" ) || p_item->p_input->b_fixed_name == VLC_TRUE )
326     {
327         msg = wxString( wxU( p_item->p_input->psz_name ) ) + duration;
328     }
329     else
330     {
331         msg = wxString(wxU( psz_artist )) + wxT(" - ") +
332                     wxString(wxU(p_item->p_input->psz_name)) + duration;
333     }
334     free( psz_artist );
335     treectrl->SetItemText( item , msg );
336     treectrl->SetItemImage( item, p_item->p_input->i_type );
337
338     if( p_playlist->status.p_item == p_item )
339     {
340         treectrl->SetItemBold( item, true );
341         while( treectrl->GetItemParent( item ).IsOk() )
342         {
343             item = treectrl->GetItemParent( item );
344             treectrl->Expand( item );
345         }
346     }
347     else
348     {
349         treectrl->SetItemBold( item, false );
350     }
351     UnlockPlaylist( p_intf->p_sys, p_playlist );
352 }
353
354 void PlaylistManager::AppendItem( wxCommandEvent& event )
355 {
356     playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
357     playlist_item_t *p_item = NULL;
358     wxTreeItemId item, node;
359
360     i_items_to_append--;
361
362     /* No need to do anything if the playlist is going to be rebuilt */
363     if( b_need_update ) return;
364
365     //if( p_add->i_view != i_current_view ) goto update;
366
367     node = FindItem( treectrl->GetRootItem(), p_add->i_node );
368     if( !node.IsOk() ) goto update;
369
370     p_item = playlist_ItemGetById( p_playlist, p_add->i_item, VLC_FALSE );
371     if( !p_item ) goto update;
372
373     item = FindItem( treectrl->GetRootItem(), p_add->i_item );
374     if( item.IsOk() ) goto update;
375
376     item = treectrl->AppendItem( node, wxL2U( p_item->p_input->psz_name ), -1,-1,
377                                  new PlaylistItem( p_item ) );
378     treectrl->SetItemImage( item, p_item->p_input->i_type );
379
380     if( item.IsOk() && p_item->i_children == -1 ) UpdateTreeItem( item );
381
382 update:
383     return;
384 }
385
386 void PlaylistManager::UpdateItem( int i )
387 {
388     if( i < 0 ) return; /* Sanity check */
389
390     wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
391     if( item.IsOk() ) UpdateTreeItem( item );
392 }
393
394 void PlaylistManager::RemoveItem( int i )
395 {
396     if( i <= 0 ) return; /* Sanity check */
397
398     wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
399     if( item.IsOk() )
400     {
401         treectrl->Delete( item );
402
403         /* Invalidate cache */
404         i_cached_item_id = -1;
405     }
406 }
407
408 /* This function is called on a regular basis */
409 void PlaylistManager::Update()
410 {
411     i_update_counter++;
412
413     /* If the playlist isn't show there's no need to update it */
414     if( !IsShown() ) return;
415
416     if( this->b_need_update )
417     {
418         this->b_need_update = VLC_FALSE;
419         Rebuild( VLC_TRUE );
420     }
421
422     /* Updating the playing status every 0.5s is enough */
423     if( i_update_counter % 5 ) return;
424 }
425
426 /**********************************************************************
427  * Rebuild the playlist
428  **********************************************************************/
429 void PlaylistManager::Rebuild( vlc_bool_t b_root )
430 {
431     i_items_to_append = 0;
432     i_cached_item_id = -1;
433
434     treectrl->DeleteAllItems();
435     treectrl->AddRoot( wxU(_("root" )), -1, -1,
436                        new PlaylistItem( p_playlist->p_root_category ) );
437
438     wxTreeItemId root = treectrl->GetRootItem();
439     UpdateNode( p_playlist->p_root_category, root );
440 }
441
442 /**********************************************************************
443  * Search functions (internal)
444  **********************************************************************/
445
446 /* Find a wxItem from a playlist id */
447 wxTreeItemId PlaylistManager::FindItem( wxTreeItemId root, int i_id )
448 {
449     wxTreeItemIdValue cookie;
450     PlaylistItem *p_wxcurrent;
451     wxTreeItemId dummy, search, item, child;
452
453     if( i_id < 0 ) return dummy;
454     if( i_cached_item_id == i_id ) return cached_item;
455
456     p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
457     if( !p_wxcurrent ) return dummy;
458
459     if( p_wxcurrent->i_id == i_id )
460     {
461         i_cached_item_id = i_id;
462         cached_item = root;
463         return root;
464     }
465
466     item = treectrl->GetFirstChild( root, cookie );
467     while( item.IsOk() )
468     {
469         p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
470         if( !p_wxcurrent )
471         {
472             item = treectrl->GetNextChild( root, cookie );
473             continue;
474         }
475
476         if( p_wxcurrent->i_id == i_id )
477         {
478             i_cached_item_id = i_id;
479             cached_item = item;
480             return item;
481         }
482
483         if( treectrl->ItemHasChildren( item ) )
484         {
485             wxTreeItemId search = FindItem( item, i_id );
486             if( search.IsOk() ) return search;
487         }
488
489         item = treectrl->GetNextChild( root, cookie );
490     }
491
492     return dummy;
493 }
494
495 /********************************************************************
496  * Events
497  ********************************************************************/
498 void PlaylistManager::OnActivateItem( wxTreeEvent& event )
499 {
500     playlist_item_t *p_item, *p_node;
501     wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
502     PlaylistItem *p_wxitem = (PlaylistItem *)
503         treectrl->GetItemData( event.GetItem() );
504
505     if( !p_wxitem || !parent.IsOk() ) return;
506
507     PlaylistItem *p_wxparent = (PlaylistItem *)treectrl->GetItemData( parent );
508     if( !p_wxparent ) return;
509
510     LockPlaylist( p_intf->p_sys, p_playlist );
511     p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id, VLC_TRUE );
512     p_node = playlist_ItemGetById( p_playlist, p_wxparent->i_id, VLC_TRUE );
513     if( !p_item || p_item->i_children >= 0 )
514     {
515         p_node = p_item;
516         p_item = NULL;
517     }
518     playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VLC_TRUE,  p_node, p_item );
519     UnlockPlaylist( p_intf->p_sys, p_playlist );
520 }
521
522 void PlaylistManager::OnPlaylistEvent( wxCommandEvent& event )
523 {
524     switch( event.GetId() )
525     {
526     case UpdateItem_Event:
527         UpdateItem( event.GetInt() );
528         break;
529     case AppendItem_Event:
530         AppendItem( event );
531         break;
532     case RemoveItem_Event:
533         RemoveItem( event.GetInt() );
534         break;
535     }
536 }
537
538 /*****************************************************************************
539  * ItemChanged: callback triggered by the item-change playlist variable
540  *****************************************************************************/
541 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
542                         vlc_value_t old_val, vlc_value_t new_val, void *param )
543 {
544     PlaylistManager *p_playlist = (PlaylistManager *)param;
545
546     wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
547     event.SetInt( new_val.i_int );
548     p_playlist->AddPendingEvent( event );
549
550     return VLC_SUCCESS;
551 }
552 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
553                         vlc_value_t old_val, vlc_value_t new_val, void *param )
554 {
555     PlaylistManager *p_playlist = (PlaylistManager *)param;
556
557     wxCommandEvent event( wxEVT_PLAYLIST, RemoveItem_Event );
558     event.SetInt( new_val.i_int );
559     p_playlist->AddPendingEvent( event );
560
561     return VLC_SUCCESS;
562 }
563
564 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
565                          vlc_value_t oval, vlc_value_t nval, void *param )
566 {
567     PlaylistManager *p_playlist = (PlaylistManager *)param;
568
569     playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
570     memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
571
572     if( ++p_playlist->i_items_to_append >= 50 )
573     {
574         /* Too many items waiting to be added, it will be quicker to rebuild
575          * the whole playlist */
576         p_playlist->b_need_update = VLC_TRUE;
577         return VLC_SUCCESS;
578     }
579
580     wxCommandEvent event( wxEVT_PLAYLIST, AppendItem_Event );
581     event.SetClientData( (void *)p_add );
582     p_playlist->AddPendingEvent( event );
583
584     return VLC_SUCCESS;
585 }
586 }