]> git.sesse.net Git - vlc/blob - modules/gui/skins2/vars/playtree.cpp
Copyright stuff
[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 "../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     // Try to guess the current charset
41     char *pCharset;
42     vlc_current_charset( &pCharset );
43     iconvHandle = vlc_iconv_open( "UTF-8", pCharset );
44     msg_Dbg( pIntf, "using character encoding: %s", pCharset );
45     free( pCharset );
46
47     if( iconvHandle == (vlc_iconv_t) - 1 )
48     {
49         msg_Warn( pIntf, "unable to do requested conversion" );
50     }
51
52     buildTree();
53 }
54
55 Playtree::~Playtree()
56 {
57     if( iconvHandle != (vlc_iconv_t) - 1 ) vlc_iconv_close( iconvHandle );
58 }
59
60 void Playtree::delSelected()
61 {
62     Iterator it = begin();
63     vlc_mutex_lock( &getIntf()->p_sys->p_playlist->object_lock );
64     for( it = begin(); it != end(); it = getNextVisibleItem( it ) )
65     {
66         if( (*it).m_selected && !(*it).isReadonly() )
67         {
68             (*it).m_deleted = true;
69         }
70     }
71     /// \todo Do this better (handle item-deleted)
72     tree_update descr;
73     descr.i_type = 3;
74     notify( &descr );
75     it = begin();
76     while( it != end() )
77     {
78         if( (*it).m_deleted )
79         {
80             VarTree::Iterator it2;
81             playlist_item_t *p_item = (playlist_item_t *)(it->m_pData);
82             if( p_item->i_children == -1 )
83             {
84                 playlist_DeleteFromItemId( getIntf()->p_sys->p_playlist,
85                                            p_item->i_id );
86                 it2 = getNextVisibleItem( it ) ;
87                 it->parent()->removeChild( it );
88                 it = it2;
89             }
90             else
91             {
92                 playlist_NodeDelete( getIntf()->p_sys->p_playlist, p_item,
93                                      VLC_TRUE, VLC_FALSE );
94                 it2 = getNextSibling( it );
95                 it->parent()->removeChild( it );
96                 it = it2;
97             }
98         }
99         else
100         {
101             it = getNextVisibleItem( it );
102         }
103     }
104     vlc_mutex_unlock( &getIntf()->p_sys->p_playlist->object_lock );
105 }
106
107 void Playtree::action( VarTree *pItem )
108 {
109     vlc_mutex_lock( &m_pPlaylist->object_lock );
110     VarTree::Iterator it;
111
112     playlist_item_t *p_item = (playlist_item_t *)pItem->m_pData;
113     playlist_item_t *p_parent = p_item;
114     while( p_parent )
115     {
116         if( p_parent == m_pPlaylist->p_root_category )
117             break;
118         p_parent = p_parent->p_parent;
119     }
120
121     if( p_parent )
122     {
123         playlist_Control( m_pPlaylist, PLAYLIST_VIEWPLAY, p_parent, p_item );
124     }
125     vlc_mutex_unlock( &m_pPlaylist->object_lock );
126 }
127
128 void Playtree::onChange()
129 {
130     buildTree();
131     tree_update descr;
132     descr.i_type = 1;
133     notify( &descr );
134 }
135
136 void Playtree::onUpdateItem( int id )
137 {
138     Iterator it = findById( id );
139     tree_update descr;
140     descr.b_active_item = false;
141     if( it != end() )
142     {
143         // Update the item
144         playlist_item_t* pNode = (playlist_item_t*)(it->m_pData);
145         UString *pName = new UString( getIntf(), pNode->p_input->psz_name );
146         it->m_cString = UStringPtr( pName );
147         it->m_playing = m_pPlaylist->status.p_item == pNode;
148         if( it->m_playing ) descr.b_active_item = true;
149     }
150     else
151     {
152         msg_Warn(getIntf(), "cannot find node with id %d", id );
153     }
154     descr.i_type = 0;
155     notify( &descr );
156 }
157
158 /// \todo keep a list of "recently removed" to avoid looking up if we
159 //  already removed it
160 void Playtree::onDelete( int i_id )
161 {
162     tree_update descr;
163     descr.i_id = i_id;
164     descr.i_type = 3;
165     Iterator item = findById( i_id ) ;
166     if( item != end() )
167     {
168         if( item->parent() )
169             item->parent()->removeChild( item );
170         descr.b_visible = item->parent() ? true : item->parent()->m_expanded;
171         notify( &descr );
172     }
173 }
174
175 void Playtree::onAppend( playlist_add_t *p_add )
176 {
177     i_items_to_append --;
178
179     Iterator node = findById( p_add->i_node );
180     if( node != end() )
181     {
182         Iterator item =  findById( p_add->i_item );
183         if( item == end() )
184         {
185             playlist_item_t *p_item = playlist_ItemGetById(
186                                         m_pPlaylist, p_add->i_item );
187             if( !p_item ) return;
188             UString *pName = new UString( getIntf(),
189                                           p_item->p_input->psz_name );
190             node->add( p_add->i_item, UStringPtr( pName ),
191                       false,false, false, p_item->i_flags & PLAYLIST_RO_FLAG,
192                       p_item );
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]->p_input->i_id, UStringPtr( pName ),
210                      false,
211                      m_pPlaylist->status.p_item == 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     vlc_mutex_lock( &m_pPlaylist->object_lock );
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     vlc_mutex_unlock( &m_pPlaylist->object_lock );
240 //  What is it ?
241 //    checkParents( NULL );
242 }
243