]> git.sesse.net Git - vlc/blob - modules/gui/skins2/vars/playtree.cpp
skins2: playlist (process delete everywhere in the same way)
[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         if( it != end() )
153             it->m_playing = false;
154         m_currentItem = NULL;
155     }
156     else
157     {
158         playlist_Lock( m_pPlaylist );
159
160         playlist_item_t* current = playlist_CurrentPlayingItem( m_pPlaylist );
161         if( !current )
162         {
163             playlist_Unlock( m_pPlaylist );
164             return;
165         }
166
167         Iterator it = findById( current->i_id );
168         if( it != end() )
169             it->m_playing = true;
170         m_currentItem = current;
171
172         playlist_Unlock( m_pPlaylist );
173     }
174
175     tree_update descr;
176     descr.b_active_item = true;
177     descr.i_type = 0;
178     notify( &descr );
179 }
180
181
182 /// \todo keep a list of "recently removed" to avoid looking up if we
183 //  already removed it
184 void Playtree::onDelete( int i_id )
185 {
186     Iterator item = findById( i_id ) ;
187     if( item != end() )
188     {
189         VarTree* parent = item->parent();
190
191         item->m_deleted = true;
192
193         tree_update descr;
194         descr.i_id = i_id;
195         descr.i_type = 3;
196         descr.b_visible = parent ? parent->m_expanded : true;
197         notify( &descr );
198
199         if( parent )
200             parent->removeChild( item );
201     }
202
203 }
204
205 void Playtree::onAppend( playlist_add_t *p_add )
206 {
207     i_items_to_append --;
208
209     Iterator node = findById( p_add->i_node );
210     if( node != end() )
211     {
212         Iterator item =  findById( p_add->i_item );
213         if( item == end() )
214         {
215             playlist_Lock( m_pPlaylist );
216             playlist_item_t *p_item = playlist_ItemGetById(
217                                         m_pPlaylist, p_add->i_item );
218             if( !p_item )
219             {
220                 playlist_Unlock( m_pPlaylist );
221                 return;
222             }
223             UString *pName = new UString( getIntf(),
224                                           p_item->p_input->psz_name );
225             node->add( p_add->i_item, UStringPtr( pName ),
226                       false,false, false, p_item->i_flags & PLAYLIST_RO_FLAG,
227                       p_item );
228             playlist_Unlock( m_pPlaylist );
229         }
230     }
231     tree_update descr;
232     descr.i_id = p_add->i_item;
233     descr.i_parent = p_add->i_node;
234     descr.b_visible = node->m_expanded;
235     descr.i_type = 2;
236     notify( &descr );
237 }
238
239 void Playtree::buildNode( playlist_item_t *pNode, VarTree &rTree )
240 {
241     for( int i = 0; i < pNode->i_children; i++ )
242     {
243         UString *pName = new UString( getIntf(),
244                                    pNode->pp_children[i]->p_input->psz_name );
245         rTree.add( pNode->pp_children[i]->i_id, UStringPtr( pName ),
246                      false,
247                      playlist_CurrentPlayingItem(m_pPlaylist) == pNode->pp_children[i],
248                      false, pNode->pp_children[i]->i_flags & PLAYLIST_RO_FLAG,
249                      pNode->pp_children[i] );
250         if( pNode->pp_children[i]->i_children )
251         {
252             buildNode( pNode->pp_children[i], rTree.back() );
253         }
254     }
255 }
256
257 void Playtree::buildTree()
258 {
259     clear();
260     playlist_Lock( m_pPlaylist );
261
262     i_items_to_append = 0;
263
264     clear();
265
266     /* TODO: Let user choose view - Stick with category ATM */
267
268     /* Set the root's name */
269     UString *pName = new UString( getIntf(),
270                              m_pPlaylist->p_root_category->p_input->psz_name );
271     m_cString = UStringPtr( pName );
272
273     buildNode( m_pPlaylist->p_root_category, *this );
274
275     playlist_Unlock( m_pPlaylist );
276 //  What is it ?
277 //    checkParents( NULL );
278 }
279