]> git.sesse.net Git - vlc/blob - modules/gui/skins2/vars/playtree.cpp
The playlist have to be locked when calling playlist_ItemGetById (not tested for...
[vlc] / modules / gui / skins2 / vars / playtree.cpp
1 /*****************************************************************************
2  * playtree.cpp
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea@videolan.org>
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 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <vlc_common.h>
30
31 #include "playtree.hpp"
32 #include <vlc_playlist.h>
33 #include "../utils/ustring.hpp"
34
35 #include "vlc_charset.h"
36
37
38 Playtree::Playtree( intf_thread_t *pIntf ): VarTree( pIntf )
39 {
40     // Get the VLC playlist object
41     m_pPlaylist = pIntf->p_sys->p_playlist;
42
43     i_items_to_append = 0;
44
45     buildTree();
46 }
47
48 Playtree::~Playtree()
49 {
50 }
51
52 void Playtree::delSelected()
53 {
54     Iterator it = begin();
55     playlist_Lock( getIntf()->p_sys->p_playlist );
56     for( it = begin(); it != end(); it = getNextVisibleItem( it ) )
57     {
58         if( (*it).m_selected && !(*it).isReadonly() )
59         {
60             (*it).m_deleted = true;
61         }
62     }
63     /// \todo Do this better (handle item-deleted)
64     tree_update descr;
65     descr.i_type = 3;
66     notify( &descr );
67     it = begin();
68     while( it != end() )
69     {
70         if( (*it).m_deleted )
71         {
72             VarTree::Iterator it2;
73             playlist_item_t *p_item = (playlist_item_t *)(it->m_pData);
74             if( p_item->i_children == -1 )
75             {
76                 playlist_DeleteFromInput( getIntf()->p_sys->p_playlist,
77                                           p_item->p_input->i_id, pl_Locked );
78                 it2 = getNextVisibleItem( it ) ;
79                 it->parent()->removeChild( it );
80                 it = it2;
81             }
82             else
83             {
84                 playlist_NodeDelete( getIntf()->p_sys->p_playlist, p_item,
85                                      true, false );
86                 it2 = getNextSibling( it );
87                 it->parent()->removeChild( it );
88                 it = it2;
89             }
90         }
91         else
92         {
93             it = getNextVisibleItem( it );
94         }
95     }
96     playlist_Unlock( getIntf()->p_sys->p_playlist );
97 }
98
99 void Playtree::action( VarTree *pItem )
100 {
101     playlist_Lock( m_pPlaylist );
102     VarTree::Iterator it;
103
104     playlist_item_t *p_item = (playlist_item_t *)pItem->m_pData;
105     playlist_item_t *p_parent = p_item;
106     while( p_parent )
107     {
108         if( p_parent == m_pPlaylist->p_root_category )
109             break;
110         p_parent = p_parent->p_parent;
111     }
112
113     if( p_parent )
114     {
115         playlist_Control( m_pPlaylist, PLAYLIST_VIEWPLAY, pl_Locked, p_parent, p_item );
116     }
117     playlist_Unlock( m_pPlaylist );
118 }
119
120 void Playtree::onChange()
121 {
122     buildTree();
123     tree_update descr;
124     descr.i_type = 1;
125     notify( &descr );
126 }
127
128 void Playtree::onUpdateItem( int id )
129 {
130     Iterator it = findById( id );
131     tree_update descr;
132     descr.b_active_item = false;
133     if( it != end() )
134     {
135         // Update the item
136         playlist_item_t* pNode = (playlist_item_t*)(it->m_pData);
137         UString *pName = new UString( getIntf(), pNode->p_input->psz_name );
138         it->m_cString = UStringPtr( pName );
139         it->m_playing = playlist_CurrentPlayingItem( m_pPlaylist ) == pNode;
140         if( it->m_playing ) descr.b_active_item = true;
141     }
142     else
143     {
144         msg_Warn(getIntf(), "cannot find node with id %d", id );
145     }
146     descr.i_type = 0;
147     notify( &descr );
148 }
149
150 /// \todo keep a list of "recently removed" to avoid looking up if we
151 //  already removed it
152 void Playtree::onDelete( int i_id )
153 {
154     tree_update descr;
155     descr.i_id = i_id;
156     descr.i_type = 3;
157     Iterator item = findById( i_id ) ;
158     if( item != end() )
159     {
160         if( item->parent() )
161             item->parent()->removeChild( item );
162         descr.b_visible = item->parent() ? true : item->parent()->m_expanded;
163         notify( &descr );
164     }
165 }
166
167 void Playtree::onAppend( playlist_add_t *p_add )
168 {
169     i_items_to_append --;
170
171     Iterator node = findById( p_add->i_node );
172     if( node != end() )
173     {
174         Iterator item =  findById( p_add->i_item );
175         if( item == end() )
176         {
177             playlist_Lock( m_pPlaylist );
178             playlist_item_t *p_item = playlist_ItemGetById(
179                                         m_pPlaylist, p_add->i_item );
180             if( !p_item )
181             {
182                 playlist_Unlock( m_pPlaylist );
183                 return;
184             }
185             UString *pName = new UString( getIntf(),
186                                           p_item->p_input->psz_name );
187             node->add( p_add->i_item, UStringPtr( pName ),
188                       false,false, false, p_item->i_flags & PLAYLIST_RO_FLAG,
189                       p_item );
190             playlist_Unlock( m_pPlaylist );
191         }
192     }
193     tree_update descr;
194     descr.i_id = p_add->i_item;
195     descr.i_parent = p_add->i_node;
196     descr.b_visible = node->m_expanded;
197     descr.i_type = 2;
198     notify( &descr );
199 }
200
201 void Playtree::buildNode( playlist_item_t *pNode, VarTree &rTree )
202 {
203     for( int i = 0; i < pNode->i_children; i++ )
204     {
205         UString *pName = new UString( getIntf(),
206                                    pNode->pp_children[i]->p_input->psz_name );
207         rTree.add( pNode->pp_children[i]->i_id, UStringPtr( pName ),
208                      false,
209                      playlist_CurrentPlayingItem(m_pPlaylist) == pNode->pp_children[i],
210                      false, pNode->pp_children[i]->i_flags & PLAYLIST_RO_FLAG,
211                      pNode->pp_children[i] );
212         if( pNode->pp_children[i]->i_children )
213         {
214             buildNode( pNode->pp_children[i], rTree.back() );
215         }
216     }
217 }
218
219 void Playtree::buildTree()
220 {
221     clear();
222     playlist_Lock( m_pPlaylist );
223
224     i_items_to_append = 0;
225
226     clear();
227
228     /* TODO: Let user choose view - Stick with category ATM */
229
230     /* Set the root's name */
231     UString *pName = new UString( getIntf(),
232                              m_pPlaylist->p_root_category->p_input->psz_name );
233     m_cString = UStringPtr( pName );
234
235     buildNode( m_pPlaylist->p_root_category, *this );
236
237     playlist_Unlock( m_pPlaylist );
238 //  What is it ?
239 //    checkParents( NULL );
240 }
241