1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: playlist.cpp,v 1.7 2004/02/27 13:24:12 gbazin Exp $
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
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.
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.
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 *****************************************************************************/
26 #if defined(HAVE_ICONV)
31 #include "playlist.hpp"
32 #include "../utils/ustring.hpp"
35 Playlist::Playlist( intf_thread_t *pIntf ): VarList( pIntf )
37 // Get the playlist VLC object
38 m_pPlaylist = pIntf->p_sys->p_playlist;
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 );
48 if( iconvHandle == (iconv_t)-1 )
50 msg_Warn( pIntf, "Unable to do requested conversion" );
53 msg_Dbg( pIntf, "No iconv support available" );
63 if( iconvHandle != (iconv_t)-1 )
65 iconv_close( iconvHandle );
71 void Playlist::delSelected()
73 // Remove the items from the VLC playlist
76 for( it = begin(); it != end(); it++ )
78 if( (*it).m_selected )
80 playlist_Delete( m_pPlaylist, index );
92 void Playlist::action( Elem_t *pItem )
94 // Find the index of the item
97 for( it = begin(); it != end(); it++ )
99 if( &*it == pItem ) break;
105 playlist_Goto( m_pPlaylist, index );
110 void Playlist::onChange()
117 void Playlist::buildList()
121 vlc_mutex_lock( &m_pPlaylist->object_lock );
122 for( int i = 0; i < m_pPlaylist->i_size; i++ )
124 // Get the name of the playlist item
125 UString *pName = convertName( m_pPlaylist->pp_items[i]->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 ) );
131 vlc_mutex_unlock( &m_pPlaylist->object_lock );
135 UString *Playlist::convertName( const char *pName )
138 if( iconvHandle == (iconv_t)-1 )
140 return new UString( getIntf(), pName );
143 char *pNewName, *pBufferOut;
144 const char *pBufferIn;
145 size_t ret, inbytesLeft, outbytesLeft;
147 // Try to convert the playlist item into UTF8
148 pNewName = (char*)malloc( 6 * strlen( pName ) );
149 pBufferOut = pNewName;
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 );
160 msg_Warn( getIntf(), "Failed to convert the playlist item into UTF8" );
162 return new UString( getIntf(), pName );
166 UString *pString = new UString( getIntf(), pNewName );
171 return new UString( getIntf(), pName );