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