]> git.sesse.net Git - vlc/blob - modules/gui/wxwidgets/playlist_manager.cpp
playlist: Make sure we don't pl_Release(p_playlist).
[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 = false;
110     i_items_to_append = 0;
111     i_cached_item_id = -1;
112     i_update_counter = 0;
113
114     p_playlist = pl_Yield( p_intf );
115     if( p_playlist == NULL ) return;
116
117     var_Create( p_intf, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
118     var_Create( p_intf, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
119     var_Create( p_intf, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );;
120
121     /* Create the tree */
122     treectrl = new wxTreeCtrl( this, TreeCtrl_Event,
123                                wxDefaultPosition, wxDefaultSize,
124                                wxTR_HIDE_ROOT | wxTR_LINES_AT_ROOT|
125                                wxTR_NO_LINES |
126                                wxTR_HAS_BUTTONS | wxTR_TWIST_BUTTONS |
127                                wxTR_MULTIPLE | wxTR_EXTENDED );
128
129     /* Add everything to the panel */
130     sizer = new wxBoxSizer( wxHORIZONTAL );
131     SetSizer( sizer );
132     sizer->Add( treectrl, 1, wxEXPAND );
133     sizer->Layout();
134     sizer->Fit( this );
135
136     /* Create image list */
137     wxImageList *p_images = new wxImageList( 16 , 16, TRUE );
138
139     /* FIXME: absolutely needs to be in the right order FIXME */
140     p_images->Add( wxIcon( type_unknown_xpm ) );
141     p_images->Add( wxIcon( type_afile_xpm ) );
142     p_images->Add( wxIcon( type_vfile_xpm ) );
143     p_images->Add( wxIcon( type_directory_xpm ) );
144     p_images->Add( wxIcon( type_disc_xpm ) );
145     p_images->Add( wxIcon( type_cdda_xpm ) );
146     p_images->Add( wxIcon( type_card_xpm ) );
147     p_images->Add( wxIcon( type_net_xpm ) );
148     p_images->Add( wxIcon( type_playlist_xpm ) );
149     p_images->Add( wxIcon( type_node_xpm ) );
150     treectrl->AssignImageList( p_images );
151
152     /* Reduce font size */
153     wxFont font = treectrl->GetFont(); font.SetPointSize(9);
154     treectrl->SetFont( font );
155
156 #if wxUSE_DRAG_AND_DROP
157     /* Associate drop targets with the playlist */
158     SetDropTarget( new DragAndDrop( p_intf, true ) );
159 #endif
160
161     /* Update the playlist */
162     Rebuild( true );
163
164     /*
165      * We want to be notified of playlist changes
166      */
167
168     /* Some global changes happened -> Rebuild all */
169     var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
170
171     /* We went to the next item */
172     var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
173
174     /* One item has been updated */
175     var_AddCallback( p_playlist, "item-change", ItemChanged, this );
176
177     var_AddCallback( p_playlist, "item-append", ItemAppended, this );
178     var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
179 }
180
181 PlaylistManager::~PlaylistManager()
182 {
183     if( p_playlist == NULL ) return;
184
185     var_DelCallback( p_playlist, "item-change", ItemChanged, this );
186     var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
187     var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
188     var_DelCallback( p_playlist, "item-append", ItemAppended, this );
189     var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
190     vlc_object_release( p_playlist );
191 }
192
193 /*****************************************************************************
194  * PlaylistChanged: callback triggered by the intf-change playlist variable
195  *  We don't rebuild the playlist directly here because we don't want the
196  *  caller to block for a too long time.
197  *****************************************************************************/
198 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
199                             vlc_value_t oval, vlc_value_t nval, void *param )
200 {
201     PlaylistManager *p_playlist = (PlaylistManager *)param;
202     p_playlist->b_need_update = true;
203     return VLC_SUCCESS;
204 }
205
206 /*****************************************************************************
207  * Next: callback triggered by the playlist-current playlist variable
208  *****************************************************************************/
209 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
210                          vlc_value_t oval, vlc_value_t nval, void *param )
211 {
212     PlaylistManager *p_playlist = (PlaylistManager *)param;
213
214     wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
215     event.SetInt( oval.i_int );
216     p_playlist->AddPendingEvent( event );
217     event.SetInt( nval.i_int );
218     p_playlist->AddPendingEvent( event );
219
220     return VLC_SUCCESS;
221 }
222
223 /*****************************************************************************
224  * Update functions
225  *****************************************************************************/
226 void PlaylistManager::CreateNode( playlist_item_t *p_node, wxTreeItemId parent)
227 {
228     wxTreeItemId node =
229         treectrl->AppendItem( parent, wxL2U( p_node->p_input->psz_name ), -1, -1,
230                               new PlaylistItem( p_node ) );
231     treectrl->SetItemImage( node, p_node->p_input->i_type );
232
233     UpdateNodeChildren( p_node, node );
234 }
235
236 void PlaylistManager::UpdateNode( playlist_item_t *p_node, wxTreeItemId node )
237 {
238     wxTreeItemIdValue cookie;
239     wxTreeItemId child;
240
241     for( int i = 0; i < p_node->i_children ; i++ )
242     {
243         if( !i ) child = treectrl->GetFirstChild( node, cookie);
244         else child = treectrl->GetNextChild( node, cookie );
245
246         if( !child.IsOk() )
247         {
248             /* Not enough children */
249             CreateNode( p_node->pp_children[i], node );
250             /* Keep the tree pointer up to date */
251             child = treectrl->GetNextChild( node, cookie );
252         }
253     }
254
255     treectrl->SetItemImage( node, p_node->p_input->i_type );
256
257 }
258
259 void PlaylistManager::UpdateNodeChildren( playlist_item_t *p_node,
260                                           wxTreeItemId node )
261 {
262     for( int i = 0; i< p_node->i_children ; i++ )
263     {
264         /* Append the item */
265         if( p_node->pp_children[i]->i_children == -1 )
266         {
267             wxTreeItemId item =
268                 treectrl->AppendItem( node,
269                     wxL2U( p_node->pp_children[i]->p_input->psz_name ), -1,-1,
270                            new PlaylistItem( p_node->pp_children[i]) );
271
272             UpdateTreeItem( item );
273         }
274         else
275         {
276             CreateNode( p_node->pp_children[i], node );
277         }
278     }
279 }
280
281 void PlaylistManager::UpdateTreeItem( wxTreeItemId item )
282 {
283     if( ! item.IsOk() ) return;
284
285     wxTreeItemData *p_data = treectrl->GetItemData( item );
286     if( !p_data ) return;
287
288     LockPlaylist( p_intf->p_sys, p_playlist );
289     playlist_item_t *p_item =
290         playlist_ItemGetById( p_playlist, ((PlaylistItem *)p_data)->i_id,
291                 true );
292     if( !p_item )
293     {
294         UnlockPlaylist( p_intf->p_sys, p_playlist );
295         return;
296     }
297
298     wxString msg;
299     wxString duration = wxU( "" );
300
301     char *psz_artist = input_item_GetArtist( p_item->p_input );
302     if( !psz_artist )
303         psz_artist = strdup( "" );
304
305     char *psz_name = input_item_GetName( p_item->p_input );
306     if( !psz_name )
307         psz_name = strdup( "" );
308
309     char psz_duration[MSTRTIME_MAX_SIZE];
310     mtime_t dur = input_item_GetDuration( p_item->p_input );
311
312     if( dur != -1 )
313     {
314         secstotimestr( psz_duration, dur/1000000 );
315         duration.Append( wxU( " ( " ) +  wxString( wxU( psz_duration ) ) +
316                          wxU( " )" ) );
317     }
318
319     if( !strcmp( psz_artist, "" ) || p_item->p_input->b_fixed_name == true )
320     {
321         msg = wxString( wxU( psz_name ) ) + duration;
322     }
323     else
324     {
325         msg = wxString(wxU( psz_artist )) + wxT(" - ") +
326                     wxString(wxU(psz_name)) + duration;
327     }
328     free( psz_artist );
329     free( psz_name );
330     treectrl->SetItemText( item , msg );
331     treectrl->SetItemImage( item, p_item->p_input->i_type );
332
333     if( p_playlist->status.p_item == p_item )
334     {
335         treectrl->SetItemBold( item, true );
336         while( treectrl->GetItemParent( item ).IsOk() )
337         {
338             item = treectrl->GetItemParent( item );
339             treectrl->Expand( item );
340         }
341     }
342     else
343     {
344         treectrl->SetItemBold( item, false );
345     }
346     UnlockPlaylist( p_intf->p_sys, p_playlist );
347 }
348
349 void PlaylistManager::AppendItem( wxCommandEvent& event )
350 {
351     playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
352     playlist_item_t *p_item = NULL;
353     wxTreeItemId item, node;
354
355     i_items_to_append--;
356
357     /* No need to do anything if the playlist is going to be rebuilt */
358     if( b_need_update ) return;
359
360     //if( p_add->i_view != i_current_view ) goto update;
361
362     node = FindItem( treectrl->GetRootItem(), p_add->i_node );
363     if( !node.IsOk() ) goto update;
364
365     p_item = playlist_ItemGetById( p_playlist, p_add->i_item, false );
366     if( !p_item ) goto update;
367
368     item = FindItem( treectrl->GetRootItem(), p_add->i_item );
369     if( item.IsOk() ) goto update;
370
371     item = treectrl->AppendItem( node, wxL2U( p_item->p_input->psz_name ), -1,-1,
372                                  new PlaylistItem( p_item ) );
373     treectrl->SetItemImage( item, p_item->p_input->i_type );
374
375     if( item.IsOk() && p_item->i_children == -1 ) UpdateTreeItem( item );
376
377 update:
378     return;
379 }
380
381 void PlaylistManager::UpdateItem( int i )
382 {
383     if( i < 0 ) return; /* Sanity check */
384
385     wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
386     if( item.IsOk() ) UpdateTreeItem( item );
387 }
388
389 void PlaylistManager::RemoveItem( int i )
390 {
391     if( i <= 0 ) return; /* Sanity check */
392
393     wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
394     if( item.IsOk() )
395     {
396         treectrl->Delete( item );
397
398         /* Invalidate cache */
399         i_cached_item_id = -1;
400     }
401 }
402
403 /* This function is called on a regular basis */
404 void PlaylistManager::Update()
405 {
406     i_update_counter++;
407
408     /* If the playlist isn't show there's no need to update it */
409     if( !IsShown() ) return;
410
411     if( this->b_need_update )
412     {
413         this->b_need_update = false;
414         Rebuild( true );
415     }
416
417     /* Updating the playing status every 0.5s is enough */
418     if( i_update_counter % 5 ) return;
419 }
420
421 /**********************************************************************
422  * Rebuild the playlist
423  **********************************************************************/
424 void PlaylistManager::Rebuild( bool b_root )
425 {
426     i_items_to_append = 0;
427     i_cached_item_id = -1;
428
429     treectrl->DeleteAllItems();
430     treectrl->AddRoot( wxU(_("root" )), -1, -1,
431                        new PlaylistItem( p_playlist->p_root_category ) );
432
433     wxTreeItemId root = treectrl->GetRootItem();
434     UpdateNode( p_playlist->p_root_category, root );
435 }
436
437 /**********************************************************************
438  * Search functions (internal)
439  **********************************************************************/
440
441 /* Find a wxItem from a playlist id */
442 wxTreeItemId PlaylistManager::FindItem( wxTreeItemId root, int i_id )
443 {
444     wxTreeItemIdValue cookie;
445     PlaylistItem *p_wxcurrent;
446     wxTreeItemId dummy, search, item, child;
447
448     if( i_id < 0 ) return dummy;
449     if( i_cached_item_id == i_id ) return cached_item;
450
451     p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
452     if( !p_wxcurrent ) return dummy;
453
454     if( p_wxcurrent->i_id == i_id )
455     {
456         i_cached_item_id = i_id;
457         cached_item = root;
458         return root;
459     }
460
461     item = treectrl->GetFirstChild( root, cookie );
462     while( item.IsOk() )
463     {
464         p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
465         if( !p_wxcurrent )
466         {
467             item = treectrl->GetNextChild( root, cookie );
468             continue;
469         }
470
471         if( p_wxcurrent->i_id == i_id )
472         {
473             i_cached_item_id = i_id;
474             cached_item = item;
475             return item;
476         }
477
478         if( treectrl->ItemHasChildren( item ) )
479         {
480             wxTreeItemId search = FindItem( item, i_id );
481             if( search.IsOk() ) return search;
482         }
483
484         item = treectrl->GetNextChild( root, cookie );
485     }
486
487     return dummy;
488 }
489
490 /********************************************************************
491  * Events
492  ********************************************************************/
493 void PlaylistManager::OnActivateItem( wxTreeEvent& event )
494 {
495     playlist_item_t *p_item, *p_node;
496     wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
497     PlaylistItem *p_wxitem = (PlaylistItem *)
498         treectrl->GetItemData( event.GetItem() );
499
500     if( !p_wxitem || !parent.IsOk() ) return;
501
502     PlaylistItem *p_wxparent = (PlaylistItem *)treectrl->GetItemData( parent );
503     if( !p_wxparent ) return;
504
505     LockPlaylist( p_intf->p_sys, p_playlist );
506     p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id, true );
507     p_node = playlist_ItemGetById( p_playlist, p_wxparent->i_id, true );
508     if( !p_item || p_item->i_children >= 0 )
509     {
510         p_node = p_item;
511         p_item = NULL;
512     }
513     playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, true,  p_node, p_item );
514     UnlockPlaylist( p_intf->p_sys, p_playlist );
515 }
516
517 void PlaylistManager::OnPlaylistEvent( wxCommandEvent& event )
518 {
519     switch( event.GetId() )
520     {
521     case UpdateItem_Event:
522         UpdateItem( event.GetInt() );
523         break;
524     case AppendItem_Event:
525         AppendItem( event );
526         break;
527     case RemoveItem_Event:
528         RemoveItem( event.GetInt() );
529         break;
530     }
531 }
532
533 /*****************************************************************************
534  * ItemChanged: callback triggered by the item-change playlist variable
535  *****************************************************************************/
536 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
537                         vlc_value_t old_val, vlc_value_t new_val, void *param )
538 {
539     PlaylistManager *p_playlist = (PlaylistManager *)param;
540
541     wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
542     event.SetInt( new_val.i_int );
543     p_playlist->AddPendingEvent( event );
544
545     return VLC_SUCCESS;
546 }
547 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
548                         vlc_value_t old_val, vlc_value_t new_val, void *param )
549 {
550     PlaylistManager *p_playlist = (PlaylistManager *)param;
551
552     wxCommandEvent event( wxEVT_PLAYLIST, RemoveItem_Event );
553     event.SetInt( new_val.i_int );
554     p_playlist->AddPendingEvent( event );
555
556     return VLC_SUCCESS;
557 }
558
559 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
560                          vlc_value_t oval, vlc_value_t nval, void *param )
561 {
562     PlaylistManager *p_playlist = (PlaylistManager *)param;
563
564     playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
565     memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
566
567     if( ++p_playlist->i_items_to_append >= 50 )
568     {
569         /* Too many items waiting to be added, it will be quicker to rebuild
570          * the whole playlist */
571         p_playlist->b_need_update = true;
572         return VLC_SUCCESS;
573     }
574
575     wxCommandEvent event( wxEVT_PLAYLIST, AppendItem_Event );
576     event.SetClientData( (void *)p_add );
577     p_playlist->AddPendingEvent( event );
578
579     return VLC_SUCCESS;
580 }
581 }