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