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(500,300) )
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 );
127 /* Create image list */
128 wxImageList *p_images = new wxImageList( 16 , 16, TRUE );
130 /* FIXME: absolutely needs to be in the right order FIXME */
131 p_images->Add( wxIcon( type_unknown_xpm ) );
132 p_images->Add( wxIcon( type_afile_xpm ) );
133 p_images->Add( wxIcon( type_vfile_xpm ) );
134 p_images->Add( wxIcon( type_directory_xpm ) );
135 p_images->Add( wxIcon( type_disc_xpm ) );
136 p_images->Add( wxIcon( type_cdda_xpm ) );
137 p_images->Add( wxIcon( type_card_xpm ) );
138 p_images->Add( wxIcon( type_net_xpm ) );
139 p_images->Add( wxIcon( type_playlist_xpm ) );
140 p_images->Add( wxIcon( type_node_xpm ) );
141 treectrl->AssignImageList( p_images );
143 /* Reduce font size */
144 wxFont font = treectrl->GetFont(); font.SetPointSize(9);
145 treectrl->SetFont( font );
147 #if wxUSE_DRAG_AND_DROP
148 /* Associate drop targets with the playlist */
149 SetDropTarget( new DragAndDrop( p_intf, VLC_TRUE ) );
152 /* Update the playlist */
156 * We want to be notified of playlist changes
159 /* Some global changes happened -> Rebuild all */
160 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
162 /* We went to the next item */
163 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
165 /* One item has been updated */
166 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
168 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
169 var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
172 PlaylistManager::~PlaylistManager()
174 if( p_playlist == NULL ) return;
176 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
177 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
178 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
179 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
180 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
181 vlc_object_release( p_playlist );
184 /*****************************************************************************
185 * PlaylistChanged: callback triggered by the intf-change playlist variable
186 * We don't rebuild the playlist directly here because we don't want the
187 * caller to block for a too long time.
188 *****************************************************************************/
189 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
190 vlc_value_t oval, vlc_value_t nval, void *param )
192 PlaylistManager *p_playlist = (PlaylistManager *)param;
193 p_playlist->b_need_update = VLC_TRUE;
197 /*****************************************************************************
198 * Next: callback triggered by the playlist-current playlist variable
199 *****************************************************************************/
200 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
201 vlc_value_t oval, vlc_value_t nval, void *param )
203 PlaylistManager *p_playlist = (PlaylistManager *)param;
205 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
206 event.SetInt( oval.i_int );
207 p_playlist->AddPendingEvent( event );
208 event.SetInt( nval.i_int );
209 p_playlist->AddPendingEvent( event );
214 /*****************************************************************************
216 *****************************************************************************/
217 void PlaylistManager::CreateNode( playlist_item_t *p_node, wxTreeItemId parent)
220 treectrl->AppendItem( parent, wxL2U( p_node->input.psz_name ), -1, -1,
221 new PlaylistItem( p_node ) );
222 treectrl->SetItemImage( node, p_node->input.i_type );
224 UpdateNodeChildren( p_node, node );
227 void PlaylistManager::UpdateNode( playlist_item_t *p_node, wxTreeItemId node )
229 wxTreeItemIdValue cookie;
232 for( int i = 0; i < p_node->i_children ; i++ )
234 if( !i ) child = treectrl->GetFirstChild( node, cookie);
235 else child = treectrl->GetNextChild( node, cookie );
239 /* Not enough children */
240 CreateNode( p_node->pp_children[i], node );
241 /* Keep the tree pointer up to date */
242 child = treectrl->GetNextChild( node, cookie );
246 treectrl->SetItemImage( node, p_node->input.i_type );
250 void PlaylistManager::UpdateNodeChildren( playlist_item_t *p_node,
253 for( int i = 0; i< p_node->i_children ; i++ )
255 /* Append the item */
256 if( p_node->pp_children[i]->i_children == -1 )
259 treectrl->AppendItem( node,
260 wxL2U( p_node->pp_children[i]->input.psz_name ), -1,-1,
261 new PlaylistItem( p_node->pp_children[i]) );
263 UpdateTreeItem( item );
267 CreateNode( p_node->pp_children[i], node );
272 void PlaylistManager::UpdateTreeItem( wxTreeItemId item )
274 if( ! item.IsOk() ) return;
276 wxTreeItemData *p_data = treectrl->GetItemData( item );
277 if( !p_data ) return;
279 LockPlaylist( p_intf->p_sys, p_playlist );
280 playlist_item_t *p_item =
281 playlist_ItemGetById( p_playlist, ((PlaylistItem *)p_data)->i_id );
284 UnlockPlaylist( p_intf->p_sys, p_playlist );
289 wxString duration = wxU( "" );
291 vlc_input_item_GetInfo( &p_item->input,
292 _("Meta-information"), _("Artist"));
295 UnlockPlaylist( p_intf->p_sys, p_playlist );
299 char psz_duration[MSTRTIME_MAX_SIZE];
300 mtime_t dur = p_item->input.i_duration;
304 secstotimestr( psz_duration, dur/1000000 );
305 duration.Append( wxU( " ( " ) + wxString( wxU( psz_duration ) ) +
309 if( !strcmp( psz_author, "" ) || p_item->input.b_fixed_name == VLC_TRUE )
311 msg = wxString( wxU( p_item->input.psz_name ) ) + duration;
315 msg = wxString(wxU( psz_author )) + wxT(" - ") +
316 wxString(wxU(p_item->input.psz_name)) + duration;
319 treectrl->SetItemText( item , msg );
320 treectrl->SetItemImage( item, p_item->input.i_type );
322 if( p_playlist->status.p_item == p_item )
324 treectrl->SetItemBold( item, true );
325 while( treectrl->GetItemParent( item ).IsOk() )
327 item = treectrl->GetItemParent( item );
328 treectrl->Expand( item );
333 treectrl->SetItemBold( item, false );
335 UnlockPlaylist( p_intf->p_sys, p_playlist );
338 void PlaylistManager::AppendItem( wxCommandEvent& event )
340 playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
341 playlist_item_t *p_item = NULL;
342 wxTreeItemId item, node;
346 /* No need to do anything if the playlist is going to be rebuilt */
347 if( b_need_update ) return;
349 //if( p_add->i_view != i_current_view ) goto update;
351 node = FindItem( treectrl->GetRootItem(), p_add->i_node );
352 if( !node.IsOk() ) goto update;
354 p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
355 if( !p_item ) goto update;
357 item = FindItem( treectrl->GetRootItem(), p_add->i_item );
358 if( item.IsOk() ) goto update;
360 item = treectrl->AppendItem( node, wxL2U( p_item->input.psz_name ), -1,-1,
361 new PlaylistItem( p_item ) );
362 treectrl->SetItemImage( item, p_item->input.i_type );
364 if( item.IsOk() && p_item->i_children == -1 ) UpdateTreeItem( item );
370 void PlaylistManager::UpdateItem( int i )
372 if( i < 0 ) return; /* Sanity check */
374 wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
375 if( item.IsOk() ) UpdateTreeItem( item );
378 void PlaylistManager::RemoveItem( int i )
380 if( i <= 0 ) return; /* Sanity check */
382 wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
385 treectrl->Delete( item );
387 /* Invalidate cache */
388 i_cached_item_id = -1;
392 /* This function is called on a regular basis */
393 void PlaylistManager::Update()
397 /* If the playlist isn't show there's no need to update it */
398 if( !IsShown() ) return;
400 if( this->b_need_update )
402 this->b_need_update = VLC_FALSE;
406 /* Updating the playing status every 0.5s is enough */
407 if( i_update_counter % 5 ) return;
410 /**********************************************************************
411 * Rebuild the playlist
412 **********************************************************************/
413 void PlaylistManager::Rebuild( vlc_bool_t b_root )
415 playlist_view_t *p_view;
417 i_items_to_append = 0;
418 i_cached_item_id = -1;
420 p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
422 treectrl->DeleteAllItems();
423 treectrl->AddRoot( wxU(_("root" )), -1, -1,
424 new PlaylistItem( p_view->p_root ) );
426 wxTreeItemId root = treectrl->GetRootItem();
427 UpdateNode( p_view->p_root, root );
430 /**********************************************************************
431 * Search functions (internal)
432 **********************************************************************/
434 /* Find a wxItem from a playlist id */
435 wxTreeItemId PlaylistManager::FindItem( wxTreeItemId root, int i_id )
437 wxTreeItemIdValue cookie;
438 PlaylistItem *p_wxcurrent;
439 wxTreeItemId dummy, search, item, child;
441 if( i_id < 0 ) return dummy;
442 if( i_cached_item_id == i_id ) return cached_item;
444 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
445 if( !p_wxcurrent ) return dummy;
447 if( p_wxcurrent->i_id == i_id )
449 i_cached_item_id = i_id;
454 item = treectrl->GetFirstChild( root, cookie );
457 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
460 item = treectrl->GetNextChild( root, cookie );
464 if( p_wxcurrent->i_id == i_id )
466 i_cached_item_id = i_id;
471 if( treectrl->ItemHasChildren( item ) )
473 wxTreeItemId search = FindItem( item, i_id );
474 if( search.IsOk() ) return search;
477 item = treectrl->GetNextChild( root, cookie );
483 /********************************************************************
485 ********************************************************************/
486 void PlaylistManager::OnActivateItem( wxTreeEvent& event )
488 playlist_item_t *p_item, *p_node;
489 wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
490 PlaylistItem *p_wxitem = (PlaylistItem *)
491 treectrl->GetItemData( event.GetItem() );
493 if( !p_wxitem || !parent.IsOk() ) return;
495 PlaylistItem *p_wxparent = (PlaylistItem *)treectrl->GetItemData( parent );
496 if( !p_wxparent ) return;
498 LockPlaylist( p_intf->p_sys, p_playlist );
499 p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id );
500 p_node = playlist_ItemGetById( p_playlist, p_wxparent->i_id );
501 if( !p_item || p_item->i_children >= 0 )
507 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VIEW_CATEGORY,
509 UnlockPlaylist( p_intf->p_sys, p_playlist );
512 void PlaylistManager::OnPlaylistEvent( wxCommandEvent& event )
514 switch( event.GetId() )
516 case UpdateItem_Event:
517 UpdateItem( event.GetInt() );
519 case AppendItem_Event:
522 case RemoveItem_Event:
523 RemoveItem( event.GetInt() );
528 /*****************************************************************************
529 * ItemChanged: callback triggered by the item-change playlist variable
530 *****************************************************************************/
531 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
532 vlc_value_t old_val, vlc_value_t new_val, void *param )
534 PlaylistManager *p_playlist = (PlaylistManager *)param;
536 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
537 event.SetInt( new_val.i_int );
538 p_playlist->AddPendingEvent( event );
542 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
543 vlc_value_t old_val, vlc_value_t new_val, void *param )
545 PlaylistManager *p_playlist = (PlaylistManager *)param;
547 wxCommandEvent event( wxEVT_PLAYLIST, RemoveItem_Event );
548 event.SetInt( new_val.i_int );
549 p_playlist->AddPendingEvent( event );
554 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
555 vlc_value_t oval, vlc_value_t nval, void *param )
557 PlaylistManager *p_playlist = (PlaylistManager *)param;
559 playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
560 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
562 if( p_playlist->i_items_to_append++ > 50 )
564 /* Too many items waiting to be added, it will be quicker to rebuild
565 * the whole playlist */
566 p_playlist->b_need_update = VLC_TRUE;
570 wxCommandEvent event( wxEVT_PLAYLIST, AppendItem_Event );
571 event.SetClientData( (void *)p_add );
572 p_playlist->AddPendingEvent( event );