]> git.sesse.net Git - vlc/blob - modules/gui/skins2/vars/playtree.cpp
Merge branch 1.0-bugfix
[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, 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         playlist_Lock( m_pPlaylist );
140         it->m_playing = playlist_CurrentPlayingItem( m_pPlaylist ) == pNode;
141         playlist_Unlock( m_pPlaylist );
142         if( it->m_playing ) descr.b_active_item = true;
143     }
144     else
145     {
146         msg_Warn(getIntf(), "cannot find node with id %d", id );
147     }
148     descr.i_type = 0;
149     notify( &descr );
150 }
151
152 /// \todo keep a list of "recently removed" to avoid looking up if we
153 //  already removed it
154 void Playtree::onDelete( int i_id )
155 {
156     tree_update descr;
157     descr.i_id = i_id;
158     descr.i_type = 3;
159     Iterator item = findById( i_id ) ;
160     if( item != end() )
161     {
162         if( item->parent() )
163             item->parent()->removeChild( item );
164         descr.b_visible = item->parent() ? true : item->parent()->m_expanded;
165         notify( &descr );
166     }
167 }
168
169 void Playtree::onAppend( playlist_add_t *p_add )
170 {
171     i_items_to_append --;
172
173     Iterator node = findById( p_add->i_node );
174     if( node != end() )
175     {
176         Iterator item =  findById( p_add->i_item );
177         if( item == end() )
178         {
179             playlist_Lock( m_pPlaylist );
180             playlist_item_t *p_item = playlist_ItemGetById(
181                                         m_pPlaylist, p_add->i_item );
182             if( !p_item )
183             {
184                 playlist_Unlock( m_pPlaylist );
185                 return;
186             }
187             UString *pName = new UString( getIntf(),
188                                           p_item->p_input->psz_name );
189             node->add( p_add->i_item, UStringPtr( pName ),
190                       false,false, false, p_item->i_flags & PLAYLIST_RO_FLAG,
191                       p_item );
192             playlist_Unlock( m_pPlaylist );
193         }
194     }
195     tree_update descr;
196     descr.i_id = p_add->i_item;
197     descr.i_parent = p_add->i_node;
198     descr.b_visible = node->m_expanded;
199     descr.i_type = 2;
200     notify( &descr );
201 }
202
203 void Playtree::buildNode( playlist_item_t *pNode, VarTree &rTree )
204 {
205     for( int i = 0; i < pNode->i_children; i++ )
206     {
207         UString *pName = new UString( getIntf(),
208                                    pNode->pp_children[i]->p_input->psz_name );
209         rTree.add( pNode->pp_children[i]->i_id, UStringPtr( pName ),
210                      false,
211                      playlist_CurrentPlayingItem(m_pPlaylist) == pNode->pp_children[i],
212                      false, pNode->pp_children[i]->i_flags & PLAYLIST_RO_FLAG,
213                      pNode->pp_children[i] );
214         if( pNode->pp_children[i]->i_children )
215         {
216             buildNode( pNode->pp_children[i], rTree.back() );
217         }
218     }
219 }
220
221 void Playtree::buildTree()
222 {
223     clear();
224     playlist_Lock( m_pPlaylist );
225
226     i_items_to_append = 0;
227
228     clear();
229
230     /* TODO: Let user choose view - Stick with category ATM */
231
232     /* Set the root's name */
233     UString *pName = new UString( getIntf(),
234                              m_pPlaylist->p_root_category->p_input->psz_name );
235     m_cString = UStringPtr( pName );
236
237     buildNode( m_pPlaylist->p_root_category, *this );
238
239     playlist_Unlock( m_pPlaylist );
240 //  What is it ?
241 //    checkParents( NULL );
242 }
243