]> git.sesse.net Git - vlc/blob - modules/gui/skins2/vars/playlist.cpp
* src/input/control.c: added INPUT_ADD_INFO/INPUT_SET_NAME to input_Control().
[vlc] / modules / gui / skins2 / vars / playlist.cpp
1 /*****************************************************************************
2  * playlist.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include <vlc/vlc.h>
25
26 #if defined(HAVE_ICONV)
27 #include <iconv.h>
28 #include "charset.h"
29 #endif
30
31 #include "playlist.hpp"
32 #include "../utils/ustring.hpp"
33
34
35 Playlist::Playlist( intf_thread_t *pIntf ): VarList( pIntf )
36 {
37     // Get the playlist VLC object
38     m_pPlaylist = pIntf->p_sys->p_playlist;
39
40 #ifdef HAVE_ICONV
41     // Try to guess the current charset
42     char *pCharset = (char*)malloc( 100 );
43     vlc_current_charset( &pCharset );
44     iconvHandle = iconv_open( "UTF-8", pCharset );
45     msg_Dbg( pIntf, "Using character encoding: %s", pCharset );
46     free( pCharset );
47
48     if( iconvHandle == (iconv_t)-1 )
49     {
50         msg_Warn( pIntf, "Unable to do requested conversion" );
51     }
52 #else
53     msg_Dbg( pIntf, "No iconv support available" );
54 #endif
55
56     buildList();
57 }
58
59
60 Playlist::~Playlist()
61 {
62 #ifdef HAVE_ICONV
63     if( iconvHandle != (iconv_t)-1 )
64     {
65         iconv_close( iconvHandle );
66     }
67 #endif
68 }
69
70
71 void Playlist::delSelected()
72 {
73     // Remove the items from the VLC playlist
74     int index = 0;
75     ConstIterator it;
76     for( it = begin(); it != end(); it++ )
77     {
78         if( (*it).m_selected )
79         {
80             playlist_Delete( m_pPlaylist, index );
81         }
82         else
83         {
84             index++;
85         }
86     }
87
88     notify();
89 }
90
91
92 void Playlist::action( Elem_t *pItem )
93 {
94     // Find the index of the item
95     int index = 0;
96     ConstIterator it;
97     for( it = begin(); it != end(); it++ )
98     {
99         if( &*it == pItem ) break;
100         index++;
101     }
102     // Item found ?
103     if( index < size() )
104     {
105         playlist_Goto( m_pPlaylist, index );
106     }
107 }
108
109
110 void Playlist::onChange()
111 {
112     buildList();
113     notify();
114 }
115
116
117 void Playlist::buildList()
118 {
119     clear();
120
121     vlc_mutex_lock( &m_pPlaylist->object_lock );
122     for( int i = 0; i < m_pPlaylist->i_size; i++ )
123     {
124         // Get the name of the playlist item
125         UString *pName = convertName( m_pPlaylist->pp_items[i]->input.psz_name );
126         // Is it the played stream ?
127         bool playing = (i == m_pPlaylist->i_index );
128         // Add the item in the list
129         m_list.push_back( Elem_t( UStringPtr( pName ), false, playing ) );
130     }
131     vlc_mutex_unlock( &m_pPlaylist->object_lock );
132 }
133
134
135 UString *Playlist::convertName( const char *pName )
136 {
137 #ifdef HAVE_ICONV
138     if( iconvHandle == (iconv_t)-1 )
139     {
140         return new UString( getIntf(), pName );
141     }
142
143     char *pNewName, *pBufferOut;
144     const char *pBufferIn;
145     size_t ret, inbytesLeft, outbytesLeft;
146
147     // Try to convert the playlist item into UTF8
148     pNewName = (char*)malloc( 6 * strlen( pName ) );
149     pBufferOut = pNewName;
150     pBufferIn = pName;
151     inbytesLeft = strlen( pName );
152     outbytesLeft = 6 * inbytesLeft;
153     // ICONV_CONST is defined in config.h
154     ret = iconv( iconvHandle, (ICONV_CONST char **)&pBufferIn, &inbytesLeft,
155                  &pBufferOut, &outbytesLeft );
156     *pBufferOut = '\0';
157
158     if( inbytesLeft )
159     {
160         msg_Warn( getIntf(), "Failed to convert the playlist item into UTF8" );
161         free( pNewName );
162         return new UString( getIntf(), pName );
163     }
164     else
165     {
166         UString *pString = new UString( getIntf(), pNewName );
167         free( pNewName );
168         return pString;
169     }
170 #else
171     return new UString( getIntf(), pName );
172 #endif
173 }
174