]> git.sesse.net Git - vlc/blob - modules/gui/skins2/vars/playtree.cpp
A bit of headers cleanup
[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 #include <vlc/vlc.h>
26
27 #include "playtree.hpp"
28 #include <vlc_playlist.h>
29 #include "../utils/ustring.hpp"
30
31 #include "vlc_charset.h"
32
33
34 Playtree::Playtree( intf_thread_t *pIntf ): VarTree( pIntf )
35 {
36     // Get the VLC playlist object
37     m_pPlaylist = pIntf->p_sys->p_playlist;
38
39     i_items_to_append = 0;
40
41     buildTree();
42 }
43
44 Playtree::~Playtree()
45 {
46 }
47
48 void Playtree::delSelected()
49 {
50     Iterator it = begin();
51     vlc_mutex_lock( &getIntf()->p_sys->p_playlist->object_lock );
52     for( it = begin(); it != end(); it = getNextVisibleItem( it ) )
53     {
54         if( (*it).m_selected && !(*it).isReadonly() )
55         {
56             (*it).m_deleted = true;
57         }
58     }
59     /// \todo Do this better (handle item-deleted)
60     tree_update descr;
61     descr.i_type = 3;
62     notify( &descr );
63     it = begin();
64     while( it != end() )
65     {
66         if( (*it).m_deleted )
67         {
68             VarTree::Iterator it2;
69             playlist_item_t *p_item = (playlist_item_t *)(it->m_pData);
70             if( p_item->i_children == -1 )
71             {
72                 playlist_DeleteFromInput( getIntf()->p_sys->p_playlist,
73                                           p_item->p_input->i_id, VLC_TRUE );
74                 it2 = getNextVisibleItem( it ) ;
75                 it->parent()->removeChild( it );
76                 it = it2;
77             }
78             else
79             {
80                 playlist_NodeDelete( getIntf()->p_sys->p_playlist, p_item,
81                                      VLC_TRUE, VLC_FALSE );
82                 it2 = getNextSibling( it );
83                 it->parent()->removeChild( it );
84                 it = it2;
85             }
86         }
87         else
88         {
89             it = getNextVisibleItem( it );
90         }
91     }
92     vlc_mutex_unlock( &getIntf()->p_sys->p_playlist->object_lock );
93 }
94
95 void Playtree::action( VarTree *pItem )
96 {
97     vlc_mutex_lock( &m_pPlaylist->object_lock );
98     VarTree::Iterator it;
99
100     playlist_item_t *p_item = (playlist_item_t *)pItem->m_pData;
101     playlist_item_t *p_parent = p_item;
102     while( p_parent )
103     {
104         if( p_parent == m_pPlaylist->p_root_category )
105             break;
106         p_parent = p_parent->p_parent;
107     }
108
109     if( p_parent )
110     {
111         playlist_Control( m_pPlaylist, PLAYLIST_VIEWPLAY, VLC_TRUE, p_parent, p_item );
112     }
113     vlc_mutex_unlock( &m_pPlaylist->object_lock );
114 }
115
116 void Playtree::onChange()
117 {
118     buildTree();
119     tree_update descr;
120     descr.i_type = 1;
121     notify( &descr );
122 }
123
124 void Playtree::onUpdateItem( int id )
125 {
126     Iterator it = findById( id );
127     tree_update descr;
128     descr.b_active_item = false;
129     if( it != end() )
130     {
131         // Update the item
132         playlist_item_t* pNode = (playlist_item_t*)(it->m_pData);
133         UString *pName = new UString( getIntf(), pNode->p_input->psz_name );
134         it->m_cString = UStringPtr( pName );
135         it->m_playing = m_pPlaylist->status.p_item == pNode;
136         if( it->m_playing ) descr.b_active_item = true;
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 /// \todo keep a list of "recently removed" to avoid looking up if we
147 //  already removed it
148 void Playtree::onDelete( int i_id )
149 {
150     tree_update descr;
151     descr.i_id = i_id;
152     descr.i_type = 3;
153     Iterator item = findById( i_id ) ;
154     if( item != end() )
155     {
156         if( item->parent() )
157             item->parent()->removeChild( item );
158         descr.b_visible = item->parent() ? true : item->parent()->m_expanded;
159         notify( &descr );
160     }
161 }
162
163 void Playtree::onAppend( playlist_add_t *p_add )
164 {
165     i_items_to_append --;
166
167     Iterator node = findById( p_add->i_node );
168     if( node != end() )
169     {
170         Iterator item =  findById( p_add->i_item );
171         if( item == end() )
172         {
173             playlist_item_t *p_item = playlist_ItemGetById(
174                                         m_pPlaylist, p_add->i_item, VLC_TRUE );
175             if( !p_item ) return;
176             UString *pName = new UString( getIntf(),
177                                           p_item->p_input->psz_name );
178             node->add( p_add->i_item, UStringPtr( pName ),
179                       false,false, false, p_item->i_flags & PLAYLIST_RO_FLAG,
180                       p_item );
181         }
182     }
183     tree_update descr;
184     descr.i_id = p_add->i_item;
185     descr.i_parent = p_add->i_node;
186     descr.b_visible = node->m_expanded;
187     descr.i_type = 2;
188     notify( &descr );
189 }
190
191 void Playtree::buildNode( playlist_item_t *pNode, VarTree &rTree )
192 {
193     for( int i = 0; i < pNode->i_children; i++ )
194     {
195         UString *pName = new UString( getIntf(),
196                                    pNode->pp_children[i]->p_input->psz_name );
197         rTree.add( pNode->pp_children[i]->p_input->i_id, UStringPtr( pName ),
198                      false,
199                      m_pPlaylist->status.p_item == pNode->pp_children[i],
200                      false, pNode->pp_children[i]->i_flags & PLAYLIST_RO_FLAG,
201                      pNode->pp_children[i] );
202         if( pNode->pp_children[i]->i_children )
203         {
204             buildNode( pNode->pp_children[i], rTree.back() );
205         }
206     }
207 }
208
209 void Playtree::buildTree()
210 {
211     clear();
212     vlc_mutex_lock( &m_pPlaylist->object_lock );
213
214     i_items_to_append = 0;
215
216     clear();
217
218     /* TODO: Let user choose view - Stick with category ATM */
219
220     /* Set the root's name */
221     UString *pName = new UString( getIntf(),
222                              m_pPlaylist->p_root_category->p_input->psz_name );
223     m_cString = UStringPtr( pName );
224
225     buildNode( m_pPlaylist->p_root_category, *this );
226
227     vlc_mutex_unlock( &m_pPlaylist->object_lock );
228 //  What is it ?
229 //    checkParents( NULL );
230 }
231