FSF address change.
[vlc.git] / 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;
62     for (it = begin(); it != end() ; it = getNextVisibleItem( it ) )
63     {
64         if( (*it).m_selected )
65         {
66             playlist_item_t *p_item = (playlist_item_t *)(it->m_pData);
67             if( p_item->i_children == -1 )
68             {
69                 playlist_LockDelete( getIntf()->p_sys->p_playlist,
70                                      p_item->input.i_id );
71             }
72             else
73             {
74                 vlc_mutex_lock( &getIntf()->p_sys->p_playlist->object_lock );
75                 playlist_NodeDelete( getIntf()->p_sys->p_playlist, p_item,
76                                      VLC_TRUE, VLC_FALSE );
77                 vlc_mutex_unlock( &getIntf()->p_sys->p_playlist->object_lock );
78             }
79         }
80     }
81     /// \todo Do this better (handle item-deleted)
82     buildTree();
83     tree_update descr;
84     descr.i_type = 1;
85     notify( &descr );
86 }
87
88 void Playtree::action( VarTree *pItem )
89 {
90     vlc_mutex_lock( &m_pPlaylist->object_lock );
91     VarTree::Iterator it;
92     if( pItem->size() )
93     {
94         it = pItem->begin();
95         while( it->size() ) it = it->begin();
96     }
97     playlist_Control( m_pPlaylist,
98                       PLAYLIST_VIEWPLAY,
99                       m_pPlaylist->status.i_view,
100                       pItem->size()
101                           ? (playlist_item_t *)pItem->m_pData
102                           : (playlist_item_t *)pItem->parent()->m_pData,
103                       pItem->size()
104                           ? (playlist_item_t *)it->m_pData
105                           : (playlist_item_t *)pItem->m_pData
106                     );
107     vlc_mutex_unlock( &m_pPlaylist->object_lock );
108 }
109
110 void Playtree::onChange()
111 {
112     buildTree();
113     tree_update descr;
114     descr.i_type = 1;
115     notify( &descr );
116 }
117
118 void Playtree::onUpdateItem( int id )
119 {
120     Iterator it = findById( id );
121     if( it != end() )
122     {
123         // Update the item
124         playlist_item_t* pNode = (playlist_item_t*)(it->m_pData);
125         UString *pName = new UString( getIntf(), pNode->input.psz_name );
126         it->m_cString = UStringPtr( pName );
127         it->m_playing = m_pPlaylist->status.p_item == pNode;
128     }
129     else
130     {
131         msg_Warn(getIntf(), "Cannot find node with id %d", id );
132     }
133     tree_update descr;
134     descr.i_type = 0;
135     notify( &descr );
136 }
137
138 void Playtree::onAppend( playlist_add_t *p_add )
139 {
140     i_items_to_append --;
141
142     Iterator node = findById( p_add->i_node );
143     if( node != end() )
144     {
145         Iterator item =  findById( p_add->i_item );
146         if( item == end() )
147         {
148             playlist_item_t *p_item = playlist_ItemGetById(
149                                         m_pPlaylist, p_add->i_item );
150             if( !p_item ) return;
151             UString *pName = new UString( getIntf(), p_item->input.psz_name );
152             node->add( p_add->i_item, UStringPtr( pName ),
153                       false,false, false, p_item );
154         }
155     }
156     tree_update descr;
157     descr.i_id = p_add->i_item;
158     descr.i_parent = p_add->i_node;
159     descr.b_visible = node->m_expanded;
160     descr.i_type = 2;
161     notify( &descr );
162 }
163
164 void Playtree::buildNode( playlist_item_t *pNode, VarTree &rTree )
165 {
166     for( int i = 0; i < pNode->i_children; i++ )
167     {
168         UString *pName = new UString( getIntf(),
169                                       pNode->pp_children[i]->input.psz_name );
170         rTree.add( pNode->pp_children[i]->input.i_id, UStringPtr( pName ),
171                      false,
172                      m_pPlaylist->status.p_item == pNode->pp_children[i],
173                      false, pNode->pp_children[i] );
174         if( pNode->pp_children[i]->i_children )
175         {
176             buildNode( pNode->pp_children[i], rTree.back() );
177         }
178     }
179 }
180
181 void Playtree::buildTree()
182 {
183     clear();
184     vlc_mutex_lock( &m_pPlaylist->object_lock );
185
186     i_items_to_append = 0;
187
188     playlist_view_t *p_view;
189     p_view = playlist_ViewFind( m_pPlaylist, VIEW_CATEGORY );
190     /// \todo let the user chose the view
191
192     clear();
193
194     /* Set the root's name */
195     UString *pName = new UString( getIntf(), p_view->p_root->input.psz_name );
196     m_cString = UStringPtr( pName );
197
198     buildNode( p_view->p_root, *this );
199
200     vlc_mutex_unlock( &m_pPlaylist->object_lock );
201 //  What is it ?
202 //    checkParents( NULL );
203 }
204