1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #include "theme_repository.hpp"
25 #include "os_factory.hpp"
26 #include "../commands/async_queue.hpp"
27 #include "../commands/cmd_dialogs.hpp"
30 #elif defined( WIN32 ) && !defined( UNDER_CE )
37 ThemeRepository *ThemeRepository::instance( intf_thread_t *pIntf )
39 if( pIntf->p_sys->p_repository == NULL )
41 pIntf->p_sys->p_repository = new ThemeRepository( pIntf );
44 return pIntf->p_sys->p_repository;
48 void ThemeRepository::destroy( intf_thread_t *pIntf )
50 delete pIntf->p_sys->p_repository;
51 pIntf->p_sys->p_repository = NULL;
55 ThemeRepository::ThemeRepository( intf_thread_t *pIntf ): SkinObject( pIntf )
57 vlc_value_t val, text;
59 // Create a variable to add items in wxwindows popup menu
60 var_Create( pIntf, "intf-skins", VLC_VAR_STRING |
61 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
62 text.psz_string = _("Select skin");
63 var_Change( pIntf, "intf-skins", VLC_VAR_SETTEXT, &text, NULL );
65 // Scan vlt files in the resource path
66 OSFactory *pOsFactory = OSFactory::instance( pIntf );
67 list<string> resPath = pOsFactory->getResourcePath();
68 list<string>::const_iterator it;
69 for( it = resPath.begin(); it != resPath.end(); it++ )
71 parseDirectory( *it );
74 // retrieve skins from skins directories and locate default skins
75 map<string,string>::const_iterator itmap, itdefault;
76 for( itmap = m_skinsMap.begin(); itmap != m_skinsMap.end(); itmap++ )
78 string name = itmap->first;
79 string path = itmap->second;
80 val.psz_string = (char*) path.c_str();
81 text.psz_string = (char*) name.c_str();
82 var_Change( getIntf(), "intf-skins", VLC_VAR_ADDCHOICE, &val,
85 if( name == "Default" )
89 // retrieve last skins stored or skins requested by user
90 char* psz_current = var_InheritString( getIntf(), "skins2-last" );
91 string current = string( psz_current ? psz_current : "" );
94 // check if skins exists and is readable
95 bool b_readable = !ifstream( current.c_str() ).fail();
97 msg_Dbg( getIntf(), "requested skins %s is %s accessible",
98 current.c_str(), b_readable ? "" : "NOT" );
100 // set the default skins if given skins not accessible
102 current = itdefault->second;
104 // save this valid skins for reuse
105 config_PutPsz( getIntf(), "skins2-last", current.c_str() );
111 var_AddCallback( pIntf, "intf-skins", changeSkin, this );
113 // variable for opening a dialog box to change skins
114 var_Create( pIntf, "intf-skins-interactive", VLC_VAR_VOID |
116 text.psz_string = _("Open skin ...");
117 var_Change( pIntf, "intf-skins-interactive", VLC_VAR_SETTEXT, &text, NULL );
120 var_AddCallback( pIntf, "intf-skins-interactive", changeSkin, this );
125 ThemeRepository::~ThemeRepository()
129 var_DelCallback( getIntf(), "intf-skins", changeSkin, this );
130 var_DelCallback( getIntf(), "intf-skins-interactive", changeSkin, this );
132 var_Destroy( getIntf(), "intf-skins" );
133 var_Destroy( getIntf(), "intf-skins-interactive" );
137 void ThemeRepository::parseDirectory( const string &rDir_locale )
141 vlc_value_t val, text;
143 const string &sep = OSFactory::instance( getIntf() )->getDirSeparator();
146 // FIXME: parseDirectory should be invoked with UTF-8 input instead!!
147 string rDir = sFromLocale( rDir_locale );
148 pDir = vlc_opendir( rDir.c_str() );
153 msg_Dbg( getIntf(), "cannot open directory %s", rDir.c_str() );
157 // While we still have entries in the directory
158 while( ( pszDirContent = vlc_readdir( pDir ) ) != NULL )
160 string name = pszDirContent;
162 if( name.size() > 4 )
164 extension = name.substr( name.size() - 4, 4 );
166 if( extension == ".vlt" || extension == ".wsz" )
168 string path = rDir + sep + name;
169 string shortname = name.substr( 0, name.size() - 4 );
170 for( int i = 0; i < shortname.size(); i++ )
171 shortname[i] = ( i == 0 ) ?
172 toupper( shortname[i] ) :
173 tolower( shortname[i] );
174 m_skinsMap[shortname] = path;
176 msg_Dbg( getIntf(), "found skin %s", path.c_str() );
179 free( pszDirContent );
187 int ThemeRepository::changeSkin( vlc_object_t *pIntf, char const *pVariable,
188 vlc_value_t oldval, vlc_value_t newval,
191 ThemeRepository *pThis = (ThemeRepository*)(pData);
193 if( !strcmp( pVariable, "intf-skins-interactive" ) )
195 CmdDlgChangeSkin cmd( pThis->getIntf() );
198 else if( !strcmp( pVariable, "intf-skins" ) )
200 // Try to load the new skin
201 CmdChangeSkin *pCmd = new CmdChangeSkin( pThis->getIntf(),
203 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
204 pQueue->push( CmdGenericPtr( pCmd ) );
211 void ThemeRepository::updateRepository()
213 vlc_value_t val, text;
215 // retrieve the current skin
216 char* psz_current = config_GetPsz( getIntf(), "skins2-last" );
220 val.psz_string = psz_current;
221 text.psz_string = psz_current;
223 // add this new skins if not yet present in repository
224 string current( psz_current );
225 map<string,string>::const_iterator it;
226 for( it = m_skinsMap.begin(); it != m_skinsMap.end(); it++ )
228 if( it->second == current )
231 if( it == m_skinsMap.end() )
233 var_Change( getIntf(), "intf-skins", VLC_VAR_ADDCHOICE, &val,
235 string name = psz_current;
236 m_skinsMap[name] = name;
239 // mark this current skins as 'checked' in list
240 var_Change( getIntf(), "intf-skins", VLC_VAR_SETVALUE, &val, NULL );