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