1 /*****************************************************************************
2 * themeloader.cpp: ThemeLoader class
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: themeloader.cpp,v 1.4 2003/04/12 21:43:27 asmax Exp $
7 * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
8 * Emmanuel Puig <karibu@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111,
24 *****************************************************************************/
27 //--- GENERAL ---------------------------------------------------------------
34 //--- VLC -------------------------------------------------------------------
38 #if defined( HAVE_LIBTAR_H ) && defined( HAVE_ZLIB_H )
43 //--- SKIN ------------------------------------------------------------------
47 #include "themeloader.h"
48 #include "skin_common.h"
50 #define DEFAULT_XML_FILE "theme.xml"
52 //---------------------------------------------------------------------------
59 //---------------------------------------------------------------------------
60 ThemeLoader::ThemeLoader( intf_thread_t *_p_intf )
64 //---------------------------------------------------------------------------
65 ThemeLoader::~ThemeLoader()
68 //---------------------------------------------------------------------------
69 int gzopen_frontend( char *pathname, int oflags, int mode )
71 #if defined( HAVE_LIBTAR_H ) && defined( HAVE_ZLIB_H )
76 switch( oflags & O_ACCMODE )
90 fd = open( pathname, oflags, mode );
94 // if( ( oflags & O_CREAT ) && fchmod( fd, mode ) )
97 gzf = gzdopen( fd, gzflags );
109 //---------------------------------------------------------------------------
110 #if defined( HAVE_LIBTAR_H ) && defined( HAVE_ZLIB_H )
111 bool ThemeLoader::ExtractTarGz( const string tarfile, const string rootdir )
114 tartype_t gztype = { (openfunc_t) gzopen_frontend, (closefunc_t) gzclose,
115 (readfunc_t) gzread, (writefunc_t) gzwrite };
117 if( tar_open( &t, (char *)tarfile.c_str(), &gztype, O_RDONLY, 0,
123 if( tar_extract_all( t, (char *)rootdir.c_str() ) != 0 )
128 if( tar_close( t ) != 0 )
135 //---------------------------------------------------------------------------
136 bool ThemeLoader::Extract( const string FileName )
138 char *tmpdir = tempnam( NULL, "vlt" );
139 string TempPath = tmpdir;
142 if( ! ExtractTarGz( FileName, TempPath ) )
145 if( ! Parse( TempPath + DIRECTORY_SEPARATOR + string( DEFAULT_XML_FILE ) ) )
147 DeleteTempFiles( TempPath );
151 DeleteTempFiles( TempPath );
154 //---------------------------------------------------------------------------
155 void ThemeLoader::DeleteTempFiles( const string Path )
160 //---------------------------------------------------------------------------
161 void ThemeLoader::CleanTheme()
163 delete (OSTheme *)p_intf->p_sys->p_theme;
164 p_intf->p_sys->p_theme = (Theme *)new OSTheme( p_intf );
166 //---------------------------------------------------------------------------
167 bool ThemeLoader::Parse( const string XmlFile )
169 // Things to do before loading theme
170 p_intf->p_sys->p_theme->OnLoadTheme();
172 // Set the file to parse
173 yyin = fopen( XmlFile.c_str(), "r" );
176 // Skin cannot be opened
177 msg_Warn( p_intf, "Cannot open the specified skin file: %s",
183 msg_Dbg( p_intf, "Using skin file: %s", XmlFile.c_str() );
185 // Save current working directory
187 char *cwd = new char[MAX_PATH];
188 getcwd( cwd, MAX_PATH );
190 char *cwd = new char[PATH_MAX];
191 getcwd( cwd, PATH_MAX );
194 // Directory separator is different in each OS !
195 int p = XmlFile.rfind( DIRECTORY_SEPARATOR, XmlFile.size() );
197 // Change current working directory to xml file
200 path = XmlFile.substr( 0, p );
201 chdir( path.c_str() );
203 p_intf->p_sys->b_all_win_closed = false;
210 // Set old working directory to current
214 msg_Warn( p_intf, "yylex failed: %i", lex );
219 // Set old working directory to current
225 //---------------------------------------------------------------------------
226 bool ThemeLoader::Load( const string FileName )
228 // First, we try to un-targz the file, and if it fails we hope it's a XML
230 #if defined( HAVE_LIBTAR_H ) && defined( HAVE_ZLIB_H )
231 if( ! Extract( FileName ) && ! Parse( FileName ) )
234 if( ! Parse( FileName ) )
238 // Check if the skin to load is in the config file, to load its config
239 char *skin_last = config_GetPsz( p_intf, "skin_last" );
240 if( skin_last != NULL && FileName == (string)skin_last )
242 p_intf->p_sys->p_theme->LoadConfig();
246 config_PutPsz( p_intf, "skin_last", FileName.c_str() );
247 config_SaveConfigFile( p_intf, "skins" );
252 //---------------------------------------------------------------------------