]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/theme_repository.cpp
04fc44e8c200e9952d9f494df8dcfb7c40c92c4f
[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 #ifdef HAVE_DIRENT_H
34 #   include <dirent.h>
35 #endif
36
37
38
39 ThemeRepository *ThemeRepository::instance( intf_thread_t *pIntf )
40 {
41     if( pIntf->p_sys->p_repository == NULL )
42     {
43         pIntf->p_sys->p_repository = new ThemeRepository( pIntf );
44     }
45
46     return pIntf->p_sys->p_repository;
47 }
48
49
50 void ThemeRepository::destroy( intf_thread_t *pIntf )
51 {
52     if( pIntf->p_sys->p_repository )
53     {
54         delete pIntf->p_sys->p_repository;
55         pIntf->p_sys->p_repository = NULL;
56     }
57 }
58
59
60 ThemeRepository::ThemeRepository( intf_thread_t *pIntf ): SkinObject( pIntf )
61 {
62     vlc_value_t val, text;
63
64     // Create a variable to add items in wxwindows popup menu
65     var_Create( pIntf, "intf-skins", VLC_VAR_STRING |
66                 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
67     text.psz_string = _("Select skin");
68     var_Change( pIntf, "intf-skins", VLC_VAR_SETTEXT, &text, NULL );
69
70     // Scan vlt files in the resource path
71     OSFactory *pOsFactory = OSFactory::instance( pIntf );
72     list<string> resPath = pOsFactory->getResourcePath();
73     list<string>::const_iterator it;
74     for( it = resPath.begin(); it != resPath.end(); it++ )
75     {
76         parseDirectory( *it );
77     }
78
79     // Set the callback
80     var_AddCallback( pIntf, "intf-skins", changeSkin, this );
81
82
83     // variable for opening a dialog box to change skins
84     var_Create( pIntf, "intf-skins-interactive", VLC_VAR_VOID |
85                 VLC_VAR_ISCOMMAND );
86     text.psz_string = _("Open skin ...");
87     var_Change( pIntf, "intf-skins-interactive", VLC_VAR_SETTEXT, &text, NULL );
88
89     // Set the callback
90     var_AddCallback( pIntf, "intf-skins-interactive", changeSkin, this );
91
92 }
93
94
95 ThemeRepository::~ThemeRepository()
96 {
97     var_Destroy( getIntf(), "intf-skins" );
98 }
99
100
101 void ThemeRepository::parseDirectory( const string &rDir_locale )
102 {
103     DIR *pDir;
104     char *pszDirContent;
105     vlc_value_t val, text;
106     // Path separator
107     const string &sep = OSFactory::instance( getIntf() )->getDirSeparator();
108
109     // Open the dir
110     // FIXME: parseDirectory should be invoked with UTF-8 input instead!!
111     string rDir = sFromLocale( rDir_locale );
112     pDir = utf8_opendir( rDir.c_str() );
113
114     if( pDir == NULL )
115     {
116         // An error occurred
117         msg_Dbg( getIntf(), "cannot open directory %s", rDir.c_str() );
118         return;
119     }
120
121     // While we still have entries in the directory
122     while( ( pszDirContent = utf8_readdir( pDir ) ) != NULL )
123     {
124         string name = pszDirContent;
125         string extension;
126         if( name.size() > 4 )
127         {
128             extension = name.substr( name.size() - 4, 4 );
129         }
130         if( extension == ".vlt" || extension == ".wsz" )
131         {
132             string path = rDir + sep + name;
133             msg_Dbg( getIntf(), "found skin %s", path.c_str() );
134
135             // Add the theme in the popup menu
136             string shortname = name.substr( 0, name.size() - 4 );
137             val.psz_string = strdup( path.c_str() );
138             text.psz_string = strdup( shortname.c_str() );
139             var_Change( getIntf(), "intf-skins", VLC_VAR_ADDCHOICE, &val,
140                         &text );
141             free( val.psz_string );
142             free( text.psz_string );
143         }
144
145         free( pszDirContent );
146     }
147
148     closedir( pDir );
149 }
150
151
152
153 int ThemeRepository::changeSkin( vlc_object_t *pIntf, char const *pVariable,
154                                  vlc_value_t oldval, vlc_value_t newval,
155                                  void *pData )
156 {
157     ThemeRepository *pThis = (ThemeRepository*)(pData);
158
159     if( !strcmp( pVariable, "intf-skins-interactive" ) )
160     {
161         CmdDlgChangeSkin cmd( pThis->getIntf() );
162         cmd.execute();
163     }
164     else if( !strcmp( pVariable, "intf-skins" ) )
165     {
166         // Try to load the new skin
167         CmdChangeSkin *pCmd = new CmdChangeSkin( pThis->getIntf(),
168                                                  newval.psz_string );
169         AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
170         pQueue->push( CmdGenericPtr( pCmd ) );
171     }
172
173     return VLC_SUCCESS;
174 }
175