1c51dc13303bbee7c22226ae676104bdd46ef08f
[vlc.git] / 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     vlc_object_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, true );
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     vlc_object_unlock( getIntf()->p_sys->p_playlist );
97 }
98
99 void Playtree::action( VarTree *pItem )
100 {
101     vlc_object_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, true, p_parent, p_item );
116     }
117     vlc_object_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 = m_pPlaylist->status.p_item == 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_item_t *p_item = playlist_ItemGetById(
178                                         m_pPlaylist, p_add->i_item, true );
179             if( !p_item ) return;
180             UString *pName = new UString( getIntf(),
181                                           p_item->p_input->psz_name );
182             node->add( p_add->i_item, UStringPtr( pName ),
183                       false,false, false, p_item->i_flags & PLAYLIST_RO_FLAG,
184                       p_item );
185         }
186     }
187     tree_update descr;
188     descr.i_id = p_add->i_item;
189     descr.i_parent = p_add->i_node;
190     descr.b_visible = node->m_expanded;
191     descr.i_type = 2;
192     notify( &descr );
193 }
194
195 void Playtree::buildNode( playlist_item_t *pNode, VarTree &rTree )
196 {
197     for( int i = 0; i < pNode->i_children; i++ )
198     {
199         UString *pName = new UString( getIntf(),
200                                    pNode->pp_children[i]->p_input->psz_name );
201         rTree.add( pNode->pp_children[i]->p_input->i_id, UStringPtr( pName ),
202                      false,
203                      m_pPlaylist->status.p_item == pNode->pp_children[i],
204                      false, pNode->pp_children[i]->i_flags & PLAYLIST_RO_FLAG,
205                      pNode->pp_children[i] );
206         if( pNode->pp_children[i]->i_children )
207         {
208             buildNode( pNode->pp_children[i], rTree.back() );
209         }
210     }
211 }
212
213 void Playtree::buildTree()
214 {
215     clear();
216     vlc_object_lock( m_pPlaylist );
217
218     i_items_to_append = 0;
219
220     clear();
221
222     /* TODO: Let user choose view - Stick with category ATM */
223
224     /* Set the root's name */
225     UString *pName = new UString( getIntf(),
226                              m_pPlaylist->p_root_category->p_input->psz_name );
227     m_cString = UStringPtr( pName );
228
229     buildNode( m_pPlaylist->p_root_category, *this );
230
231     vlc_object_unlock( m_pPlaylist );
232 //  What is it ?
233 //    checkParents( NULL );
234 }
235