]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/theme_repository.cpp
Remove useless <dirent.h> check
[vlc] / modules / gui / skins2 / src / theme_repository.cpp
1 /*****************************************************************************
2  * theme_repository.cpp
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include "theme_repository.hpp"
25 #include "os_factory.hpp"
26 #include "../commands/async_queue.hpp"
27 #include "../commands/cmd_dialogs.hpp"
28 #ifdef HAVE_UNISTD_H
29 #   include <unistd.h>
30 #elif defined( WIN32 ) && !defined( UNDER_CE )
31 #   include <direct.h>
32 #endif
33
34 #include <fstream>
35
36
37 ThemeRepository *ThemeRepository::instance( intf_thread_t *pIntf )
38 {
39     if( pIntf->p_sys->p_repository == NULL )
40     {
41         pIntf->p_sys->p_repository = new ThemeRepository( pIntf );
42     }
43
44     return pIntf->p_sys->p_repository;
45 }
46
47
48 void ThemeRepository::destroy( intf_thread_t *pIntf )
49 {
50     delete pIntf->p_sys->p_repository;
51     pIntf->p_sys->p_repository = NULL;
52 }
53
54
55 ThemeRepository::ThemeRepository( intf_thread_t *pIntf ): SkinObject( pIntf )
56 {
57     vlc_value_t val, text;
58
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 );
64
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++ )
70     {
71         parseDirectory( *it );
72     }
73
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++ )
77     {
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,
83                     &text );
84
85         if( name == "Default" )
86             itdefault = itmap;
87     }
88
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 : "" );
92     free( psz_current );
93
94     // check if skins exists and is readable
95     bool b_readable = !ifstream( current.c_str() ).fail();
96
97     msg_Dbg( getIntf(), "requested skins %s is %s accessible",
98                          current.c_str(), b_readable ? "" : "NOT" );
99
100     // set the default skins if given skins not accessible
101     if( !b_readable )
102         current = itdefault->second;
103
104     // save this valid skins for reuse
105     config_PutPsz( getIntf(), "skins2-last", current.c_str() );
106
107     // Update repository
108     updateRepository();
109
110     // Set the callback
111     var_AddCallback( pIntf, "intf-skins", changeSkin, this );
112
113     // variable for opening a dialog box to change skins
114     var_Create( pIntf, "intf-skins-interactive", VLC_VAR_VOID |
115                 VLC_VAR_ISCOMMAND );
116     text.psz_string = _("Open skin ...");
117     var_Change( pIntf, "intf-skins-interactive", VLC_VAR_SETTEXT, &text, NULL );
118
119     // Set the callback
120     var_AddCallback( pIntf, "intf-skins-interactive", changeSkin, this );
121
122 }
123
124
125 ThemeRepository::~ThemeRepository()
126 {
127     m_skinsMap.clear();
128
129     var_DelCallback( getIntf(), "intf-skins", changeSkin, this );
130     var_DelCallback( getIntf(), "intf-skins-interactive", changeSkin, this );
131
132     var_Destroy( getIntf(), "intf-skins" );
133     var_Destroy( getIntf(), "intf-skins-interactive" );
134 }
135
136
137 void ThemeRepository::parseDirectory( const string &rDir_locale )
138 {
139     DIR *pDir;
140     char *pszDirContent;
141     vlc_value_t val, text;
142     // Path separator
143     const string &sep = OSFactory::instance( getIntf() )->getDirSeparator();
144
145     // Open the dir
146     // FIXME: parseDirectory should be invoked with UTF-8 input instead!!
147     string rDir = sFromLocale( rDir_locale );
148     pDir = vlc_opendir( rDir.c_str() );
149
150     if( pDir == NULL )
151     {
152         // An error occurred
153         msg_Dbg( getIntf(), "cannot open directory %s", rDir.c_str() );
154         return;
155     }
156
157     // While we still have entries in the directory
158     while( ( pszDirContent = vlc_readdir( pDir ) ) != NULL )
159     {
160         string name = pszDirContent;
161         string extension;
162         if( name.size() > 4 )
163         {
164             extension = name.substr( name.size() - 4, 4 );
165         }
166         if( extension == ".vlt" || extension == ".wsz" )
167         {
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;
175
176             msg_Dbg( getIntf(), "found skin %s", path.c_str() );
177         }
178
179         free( pszDirContent );
180     }
181
182     closedir( pDir );
183 }
184
185
186
187 int ThemeRepository::changeSkin( vlc_object_t *pIntf, char const *pVariable,
188                                  vlc_value_t oldval, vlc_value_t newval,
189                                  void *pData )
190 {
191     ThemeRepository *pThis = (ThemeRepository*)(pData);
192
193     if( !strcmp( pVariable, "intf-skins-interactive" ) )
194     {
195         CmdDlgChangeSkin cmd( pThis->getIntf() );
196         cmd.execute();
197     }
198     else if( !strcmp( pVariable, "intf-skins" ) )
199     {
200         // Try to load the new skin
201         CmdChangeSkin *pCmd = new CmdChangeSkin( pThis->getIntf(),
202                                                  newval.psz_string );
203         AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
204         pQueue->push( CmdGenericPtr( pCmd ) );
205     }
206
207     return VLC_SUCCESS;
208 }
209
210
211 void ThemeRepository::updateRepository()
212 {
213     vlc_value_t val, text;
214
215     // retrieve the current skin
216     char* psz_current = config_GetPsz( getIntf(), "skins2-last" );
217     if( !psz_current )
218         return;
219
220     val.psz_string = psz_current;
221     text.psz_string = psz_current;
222
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++ )
227     {
228         if( it->second == current )
229             break;
230     }
231     if( it == m_skinsMap.end() )
232     {
233         var_Change( getIntf(), "intf-skins", VLC_VAR_ADDCHOICE, &val,
234                     &text );
235         string name = psz_current;
236         m_skinsMap[name] = name;
237     }
238
239     // mark this current skins as 'checked' in list
240     var_Change( getIntf(), "intf-skins", VLC_VAR_SETVALUE, &val, NULL );
241
242     free( psz_current );
243 }
244