1 /*****************************************************************************
2 * playlist_manager.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2005 the VideoLAN team
7 * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
8 * Clément Stenac <zorglub@videolan.org>
9 * Gildas Bazin <gbazin@videolan.org>
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include "playlist_manager.hpp"
30 #include "interface.hpp"
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"
43 #include <wx/dynarray.h>
44 #include <wx/imaglist.h>
47 /* Callback prototype */
48 static int PlaylistChanged( vlc_object_t *, const char *,
49 vlc_value_t, vlc_value_t, void * );
50 static int PlaylistNext( vlc_object_t *, const char *,
51 vlc_value_t, vlc_value_t, void * );
52 static int ItemChanged( vlc_object_t *, const char *,
53 vlc_value_t, vlc_value_t, void * );
54 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
55 vlc_value_t oval, vlc_value_t nval, void *param );
56 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
57 vlc_value_t oval, vlc_value_t nval, void *param );
59 /*****************************************************************************
61 *****************************************************************************/
63 /* IDs for the controls and the menu commands */
73 DEFINE_LOCAL_EVENT_TYPE( wxEVT_PLAYLIST );
75 BEGIN_EVENT_TABLE(PlaylistManager, wxPanel)
76 /* Tree control events */
77 EVT_TREE_ITEM_ACTIVATED( TreeCtrl_Event, PlaylistManager::OnActivateItem )
80 EVT_COMMAND(-1, wxEVT_PLAYLIST, PlaylistManager::OnPlaylistEvent)
83 /*****************************************************************************
85 ****************************************************************************/
86 class PlaylistItem : public wxTreeItemData
89 PlaylistItem( playlist_item_t *p_item ) : i_id(p_item->input.i_id) {}
93 /*****************************************************************************
95 *****************************************************************************/
96 PlaylistManager::PlaylistManager( intf_thread_t *_p_intf, wxWindow *p_parent ):
97 wxPanel( p_parent, -1, wxDefaultPosition, wxSize(0,0) )
101 b_need_update = VLC_FALSE;
102 i_items_to_append = 0;
103 i_cached_item_id = -1;
104 i_update_counter = 0;
106 p_playlist = (playlist_t *)
107 vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
108 if( p_playlist == NULL ) return;
110 var_Create( p_intf, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
111 var_Create( p_intf, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
112 var_Create( p_intf, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );;
114 /* Create the tree */
115 treectrl = new wxTreeCtrl( this, TreeCtrl_Event,
116 wxDefaultPosition, wxDefaultSize,
117 wxTR_HIDE_ROOT | wxTR_LINES_AT_ROOT|
119 wxTR_HAS_BUTTONS | wxTR_TWIST_BUTTONS |
120 wxTR_MULTIPLE | wxTR_EXTENDED );
122 /* Add everything to the panel */
123 sizer = new wxBoxSizer( wxHORIZONTAL );
125 sizer->Add( treectrl, 1, wxEXPAND );
126 treectrl->SetSizeHints( wxSize(500, 150) );
130 /* Create image list */
131 wxImageList *p_images = new wxImageList( 16 , 16, TRUE );
133 /* FIXME: absolutely needs to be in the right order FIXME */
134 p_images->Add( wxIcon( type_unknown_xpm ) );
135 p_images->Add( wxIcon( type_afile_xpm ) );
136 p_images->Add( wxIcon( type_vfile_xpm ) );
137 p_images->Add( wxIcon( type_directory_xpm ) );
138 p_images->Add( wxIcon( type_disc_xpm ) );
139 p_images->Add( wxIcon( type_cdda_xpm ) );
140 p_images->Add( wxIcon( type_card_xpm ) );
141 p_images->Add( wxIcon( type_net_xpm ) );
142 p_images->Add( wxIcon( type_playlist_xpm ) );
143 p_images->Add( wxIcon( type_node_xpm ) );
144 treectrl->AssignImageList( p_images );
146 /* Reduce font size */
147 wxFont font = treectrl->GetFont(); font.SetPointSize(9);
148 treectrl->SetFont( font );
150 #if wxUSE_DRAG_AND_DROP
151 /* Associate drop targets with the playlist */
152 SetDropTarget( new DragAndDrop( p_intf, VLC_TRUE ) );
155 /* Update the playlist */
159 * We want to be notified of playlist changes
162 /* Some global changes happened -> Rebuild all */
163 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
165 /* We went to the next item */
166 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
168 /* One item has been updated */
169 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
171 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
172 var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
175 PlaylistManager::~PlaylistManager()
177 if( p_playlist == NULL ) return;
179 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
180 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
181 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
182 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
183 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
184 vlc_object_release( p_playlist );
187 /*****************************************************************************
188 * PlaylistChanged: callback triggered by the intf-change playlist variable
189 * We don't rebuild the playlist directly here because we don't want the
190 * caller to block for a too long time.
191 *****************************************************************************/
192 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
193 vlc_value_t oval, vlc_value_t nval, void *param )
195 PlaylistManager *p_playlist = (PlaylistManager *)param;
196 p_playlist->b_need_update = VLC_TRUE;
200 /*****************************************************************************
201 * Next: callback triggered by the playlist-current playlist variable
202 *****************************************************************************/
203 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
204 vlc_value_t oval, vlc_value_t nval, void *param )
206 PlaylistManager *p_playlist = (PlaylistManager *)param;
208 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
209 event.SetInt( oval.i_int );
210 p_playlist->AddPendingEvent( event );
211 event.SetInt( nval.i_int );
212 p_playlist->AddPendingEvent( event );
217 /*****************************************************************************
219 *****************************************************************************/
220 void PlaylistManager::CreateNode( playlist_item_t *p_node, wxTreeItemId parent)
223 treectrl->AppendItem( parent, wxL2U( p_node->input.psz_name ), -1, -1,
224 new PlaylistItem( p_node ) );
225 treectrl->SetItemImage( node, p_node->input.i_type );
227 UpdateNodeChildren( p_node, node );
230 void PlaylistManager::UpdateNode( playlist_item_t *p_node, wxTreeItemId node )
232 wxTreeItemIdValue cookie;
235 for( int i = 0; i < p_node->i_children ; i++ )
237 if( !i ) child = treectrl->GetFirstChild( node, cookie);
238 else child = treectrl->GetNextChild( node, cookie );
242 /* Not enough children */
243 CreateNode( p_node->pp_children[i], node );
244 /* Keep the tree pointer up to date */
245 child = treectrl->GetNextChild( node, cookie );
249 treectrl->SetItemImage( node, p_node->input.i_type );
253 void PlaylistManager::UpdateNodeChildren( playlist_item_t *p_node,
256 for( int i = 0; i< p_node->i_children ; i++ )
258 /* Append the item */
259 if( p_node->pp_children[i]->i_children == -1 )
262 treectrl->AppendItem( node,
263 wxL2U( p_node->pp_children[i]->input.psz_name ), -1,-1,
264 new PlaylistItem( p_node->pp_children[i]) );
266 UpdateTreeItem( item );
270 CreateNode( p_node->pp_children[i], node );
275 void PlaylistManager::UpdateTreeItem( wxTreeItemId item )
277 if( ! item.IsOk() ) return;
279 wxTreeItemData *p_data = treectrl->GetItemData( item );
280 if( !p_data ) return;
282 LockPlaylist( p_intf->p_sys, p_playlist );
283 playlist_item_t *p_item =
284 playlist_ItemGetById( p_playlist, ((PlaylistItem *)p_data)->i_id );
287 UnlockPlaylist( p_intf->p_sys, p_playlist );
292 wxString duration = wxU( "" );
294 vlc_input_item_GetInfo( &p_item->input,
295 _("Meta-information"), _("Artist"));
298 UnlockPlaylist( p_intf->p_sys, p_playlist );
302 char psz_duration[MSTRTIME_MAX_SIZE];
303 mtime_t dur = p_item->input.i_duration;
307 secstotimestr( psz_duration, dur/1000000 );
308 duration.Append( wxU( " ( " ) + wxString( wxU( psz_duration ) ) +
312 if( !strcmp( psz_author, "" ) || p_item->input.b_fixed_name == VLC_TRUE )
314 msg = wxString( wxU( p_item->input.psz_name ) ) + duration;
318 msg = wxString(wxU( psz_author )) + wxT(" - ") +
319 wxString(wxU(p_item->input.psz_name)) + duration;
322 treectrl->SetItemText( item , msg );
323 treectrl->SetItemImage( item, p_item->input.i_type );
325 if( p_playlist->status.p_item == p_item )
327 treectrl->SetItemBold( item, true );
328 while( treectrl->GetItemParent( item ).IsOk() )
330 item = treectrl->GetItemParent( item );
331 treectrl->Expand( item );
336 treectrl->SetItemBold( item, false );
338 UnlockPlaylist( p_intf->p_sys, p_playlist );
341 void PlaylistManager::AppendItem( wxCommandEvent& event )
343 playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
344 playlist_item_t *p_item = NULL;
345 wxTreeItemId item, node;
349 /* No need to do anything if the playlist is going to be rebuilt */
350 if( b_need_update ) return;
352 //if( p_add->i_view != i_current_view ) goto update;
354 node = FindItem( treectrl->GetRootItem(), p_add->i_node );
355 if( !node.IsOk() ) goto update;
357 p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
358 if( !p_item ) goto update;
360 item = FindItem( treectrl->GetRootItem(), p_add->i_item );
361 if( item.IsOk() ) goto update;
363 item = treectrl->AppendItem( node, wxL2U( p_item->input.psz_name ), -1,-1,
364 new PlaylistItem( p_item ) );
365 treectrl->SetItemImage( item, p_item->input.i_type );
367 if( item.IsOk() && p_item->i_children == -1 ) UpdateTreeItem( item );
373 void PlaylistManager::UpdateItem( int i )
375 if( i < 0 ) return; /* Sanity check */
377 wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
378 if( item.IsOk() ) UpdateTreeItem( item );
381 void PlaylistManager::RemoveItem( int i )
383 if( i <= 0 ) return; /* Sanity check */
385 wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
388 treectrl->Delete( item );
390 /* Invalidate cache */
391 i_cached_item_id = -1;
395 /* This function is called on a regular basis */
396 void PlaylistManager::Update()
400 /* If the playlist isn't show there's no need to update it */
401 if( !IsShown() ) return;
403 if( this->b_need_update )
405 this->b_need_update = VLC_FALSE;
409 /* Updating the playing status every 0.5s is enough */
410 if( i_update_counter % 5 ) return;
413 /**********************************************************************
414 * Rebuild the playlist
415 **********************************************************************/
416 void PlaylistManager::Rebuild( vlc_bool_t b_root )
418 playlist_view_t *p_view;
420 i_items_to_append = 0;
421 i_cached_item_id = -1;
423 p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
425 treectrl->DeleteAllItems();
426 treectrl->AddRoot( wxU(_("root" )), -1, -1,
427 new PlaylistItem( p_view->p_root ) );
429 wxTreeItemId root = treectrl->GetRootItem();
430 UpdateNode( p_view->p_root, root );
433 /**********************************************************************
434 * Search functions (internal)
435 **********************************************************************/
437 /* Find a wxItem from a playlist id */
438 wxTreeItemId PlaylistManager::FindItem( wxTreeItemId root, int i_id )
440 wxTreeItemIdValue cookie;
441 PlaylistItem *p_wxcurrent;
442 wxTreeItemId dummy, search, item, child;
444 if( i_id < 0 ) return dummy;
445 if( i_cached_item_id == i_id ) return cached_item;
447 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
448 if( !p_wxcurrent ) return dummy;
450 if( p_wxcurrent->i_id == i_id )
452 i_cached_item_id = i_id;
457 item = treectrl->GetFirstChild( root, cookie );
460 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
463 item = treectrl->GetNextChild( root, cookie );
467 if( p_wxcurrent->i_id == i_id )
469 i_cached_item_id = i_id;
474 if( treectrl->ItemHasChildren( item ) )
476 wxTreeItemId search = FindItem( item, i_id );
477 if( search.IsOk() ) return search;
480 item = treectrl->GetNextChild( root, cookie );
486 /********************************************************************
488 ********************************************************************/
489 void PlaylistManager::OnActivateItem( wxTreeEvent& event )
491 playlist_item_t *p_item, *p_node;
492 wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
493 PlaylistItem *p_wxitem = (PlaylistItem *)
494 treectrl->GetItemData( event.GetItem() );
496 if( !p_wxitem || !parent.IsOk() ) return;
498 PlaylistItem *p_wxparent = (PlaylistItem *)treectrl->GetItemData( parent );
499 if( !p_wxparent ) return;
501 LockPlaylist( p_intf->p_sys, p_playlist );
502 p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id );
503 p_node = playlist_ItemGetById( p_playlist, p_wxparent->i_id );
504 if( !p_item || p_item->i_children >= 0 )
510 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VIEW_CATEGORY,
512 UnlockPlaylist( p_intf->p_sys, p_playlist );
515 void PlaylistManager::OnPlaylistEvent( wxCommandEvent& event )
517 switch( event.GetId() )
519 case UpdateItem_Event:
520 UpdateItem( event.GetInt() );
522 case AppendItem_Event:
525 case RemoveItem_Event:
526 RemoveItem( event.GetInt() );
531 /*****************************************************************************
532 * ItemChanged: callback triggered by the item-change playlist variable
533 *****************************************************************************/
534 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
535 vlc_value_t old_val, vlc_value_t new_val, void *param )
537 PlaylistManager *p_playlist = (PlaylistManager *)param;
539 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
540 event.SetInt( new_val.i_int );
541 p_playlist->AddPendingEvent( event );
545 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
546 vlc_value_t old_val, vlc_value_t new_val, void *param )
548 PlaylistManager *p_playlist = (PlaylistManager *)param;
550 wxCommandEvent event( wxEVT_PLAYLIST, RemoveItem_Event );
551 event.SetInt( new_val.i_int );
552 p_playlist->AddPendingEvent( event );
557 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
558 vlc_value_t oval, vlc_value_t nval, void *param )
560 PlaylistManager *p_playlist = (PlaylistManager *)param;
562 playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
563 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
565 if( p_playlist->i_items_to_append++ > 50 )
567 /* Too many items waiting to be added, it will be quicker to rebuild
568 * the whole playlist */
569 p_playlist->b_need_update = VLC_TRUE;
573 wxCommandEvent event( wxEVT_PLAYLIST, AppendItem_Event );
574 event.SetClientData( (void *)p_add );
575 p_playlist->AddPendingEvent( event );