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