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