]> git.sesse.net Git - vlc/blob - modules/gui/skins2/vars/playtree.cpp
Fix playlist crasher and simplify a few things
[vlc] / modules / gui / skins2 / vars / playtree.cpp
1 /*****************************************************************************
2  * playtree.cpp
3  *****************************************************************************
4  * Copyright (C) 2005 VideoLAN
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include <vlc/vlc.h>
25
26 #include "playtree.hpp"
27 #include "../utils/ustring.hpp"
28
29 #include "charset.h"
30
31
32 Playtree::Playtree( intf_thread_t *pIntf ): VarTree( pIntf )
33 {
34     // Get the VLC playlist object
35     m_pPlaylist = pIntf->p_sys->p_playlist;
36
37     i_items_to_append = 0;
38
39     // Try to guess the current charset
40     char *pCharset;
41     vlc_current_charset( &pCharset );
42     iconvHandle = vlc_iconv_open( "UTF-8", pCharset );
43     msg_Dbg( pIntf, "using character encoding: %s", pCharset );
44     free( pCharset );
45
46     if( iconvHandle == (vlc_iconv_t) - 1 )
47     {
48         msg_Warn( pIntf, "unable to do requested conversion" );
49     }
50
51     buildTree();
52 }
53
54 Playtree::~Playtree()
55 {
56     if( iconvHandle != (vlc_iconv_t) - 1 ) vlc_iconv_close( iconvHandle );
57 }
58
59 void Playtree::delSelected()
60 {
61     Iterator it = begin();
62     vlc_mutex_lock( &getIntf()->p_sys->p_playlist->object_lock );
63     for( it = begin(); it != end(); it = getNextVisibleItem( it ) )
64     {
65         if( (*it).m_selected && !(*it).isReadonly() )
66         {
67             (*it).m_deleted = true;
68         }
69     }
70     /// \todo Do this better (handle item-deleted)
71     tree_update descr;
72     descr.i_type = 3;
73     notify( &descr );
74     it = begin();
75     while( it != end() )
76     {
77         if( (*it).m_deleted )
78         {
79             VarTree::Iterator it2;
80             playlist_item_t *p_item = (playlist_item_t *)(it->m_pData);
81             if( p_item->i_children == -1 )
82             {
83                 playlist_DeleteFromItemId( getIntf()->p_sys->p_playlist,
84                                            p_item->i_id );
85                 it2 = getNextVisibleItem( it ) ;
86                 it->parent()->removeChild( it );
87                 it = it2;
88             }
89             else
90             {
91                 playlist_NodeDelete( getIntf()->p_sys->p_playlist, p_item,
92                                      VLC_TRUE, VLC_FALSE );
93                 it2 = getNextSibling( it );
94                 it->parent()->removeChild( it );
95                 it = it2;
96             }
97         }
98         else
99         {
100             it = getNextVisibleItem( it );
101         }
102     }
103     vlc_mutex_unlock( &getIntf()->p_sys->p_playlist->object_lock );
104 }
105
106 void Playtree::action( VarTree *pItem )
107 {
108     vlc_mutex_lock( &m_pPlaylist->object_lock );
109     VarTree::Iterator it;
110
111     playlist_item_t *p_item = (playlist_item_t *)pItem->m_pData;
112     playlist_item_t *p_parent = p_item;
113     while( p_parent )
114     {
115         if( p_parent == m_pPlaylist->p_root_category )
116             break;
117         p_parent = p_parent->p_parent;
118     }
119
120     if( p_parent )
121     {
122         playlist_Control( m_pPlaylist, PLAYLIST_VIEWPLAY, p_parent, p_item );
123     }
124     vlc_mutex_unlock( &m_pPlaylist->object_lock );
125 }
126
127 void Playtree::onChange()
128 {
129     buildTree();
130     tree_update descr;
131     descr.i_type = 1;
132     notify( &descr );
133 }
134
135 void Playtree::onUpdateItem( int id )
136 {
137     Iterator it = findById( id );
138     tree_update descr;
139     descr.b_active_item = false;
140     if( it != end() )
141     {
142         // Update the item
143         playlist_item_t* pNode = (playlist_item_t*)(it->m_pData);
144         UString *pName = new UString( getIntf(), pNode->p_input->psz_name );
145         it->m_cString = UStringPtr( pName );
146         it->m_playing = m_pPlaylist->status.p_item == pNode;
147         if( it->m_playing ) descr.b_active_item = true;
148     }
149     else
150     {
151         msg_Warn(getIntf(), "cannot find node with id %d", id );
152     }
153     descr.i_type = 0;
154     notify( &descr );
155 }
156
157 /// \todo keep a list of "recently removed" to avoid looking up if we
158 //  already removed it
159 void Playtree::onDelete( int i_id )
160 {
161     tree_update descr;
162     descr.i_id = i_id;
163     descr.i_type = 3;
164     Iterator item = findById( i_id ) ;
165     if( item != end() )
166     {
167         if( item->parent() )
168             item->parent()->removeChild( item );
169         descr.b_visible = item->parent() ? true : item->parent()->m_expanded;
170         notify( &descr );
171     }
172 }
173
174 void Playtree::onAppend( playlist_add_t *p_add )
175 {
176     i_items_to_append --;
177
178     Iterator node = findById( p_add->i_node );
179     if( node != end() )
180     {
181         Iterator item =  findById( p_add->i_item );
182         if( item == end() )
183         {
184             playlist_item_t *p_item = playlist_ItemGetById(
185                                         m_pPlaylist, p_add->i_item );
186             if( !p_item ) return;
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         }
193     }
194     tree_update descr;
195     descr.i_id = p_add->i_item;
196     descr.i_parent = p_add->i_node;
197     descr.b_visible = node->m_expanded;
198     descr.i_type = 2;
199     notify( &descr );
200 }
201
202 void Playtree::buildNode( playlist_item_t *pNode, VarTree &rTree )
203 {
204     for( int i = 0; i < pNode->i_children; i++ )
205     {
206         UString *pName = new UString( getIntf(),
207                                    pNode->pp_children[i]->p_input->psz_name );
208         rTree.add( pNode->pp_children[i]->p_input->i_id, UStringPtr( pName ),
209                      false,
210                      m_pPlaylist->status.p_item == pNode->pp_children[i],
211                      false, pNode->pp_children[i]->i_flags & PLAYLIST_RO_FLAG,
212                      pNode->pp_children[i] );
213         if( pNode->pp_children[i]->i_children )
214         {
215             buildNode( pNode->pp_children[i], rTree.back() );
216         }
217     }
218 }
219
220 void Playtree::buildTree()
221 {
222     clear();
223     vlc_mutex_lock( &m_pPlaylist->object_lock );
224
225     i_items_to_append = 0;
226
227     clear();
228
229     /* TODO: Let user choose view - Stick with category ATM */
230
231     /* Set the root's name */
232     UString *pName = new UString( getIntf(),
233                              m_pPlaylist->p_root_category->p_input->psz_name );
234     m_cString = UStringPtr( pName );
235
236     buildNode( m_pPlaylist->p_root_category, *this );
237
238     vlc_mutex_unlock( &m_pPlaylist->object_lock );
239 //  What is it ?
240 //    checkParents( NULL );
241 }
242