1 /*****************************************************************************
2 * themeloader.cpp: ThemeLoader class
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: themeloader.cpp,v 1.5 2003/04/13 17:46:23 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"
53 #define DEFAULT_XML_FILE "theme.xml"
55 //---------------------------------------------------------------------------
62 //---------------------------------------------------------------------------
63 ThemeLoader::ThemeLoader( intf_thread_t *_p_intf )
67 //---------------------------------------------------------------------------
68 ThemeLoader::~ThemeLoader()
71 //---------------------------------------------------------------------------
72 int gzopen_frontend( char *pathname, int oflags, int mode )
74 #if defined( HAVE_LIBTAR_H ) && defined( HAVE_ZLIB_H )
79 switch( oflags & O_ACCMODE )
93 fd = open( pathname, oflags, mode );
97 // if( ( oflags & O_CREAT ) && fchmod( fd, mode ) )
100 gzf = gzdopen( fd, gzflags );
112 //---------------------------------------------------------------------------
113 #if defined( HAVE_LIBTAR_H ) && defined( HAVE_ZLIB_H )
114 bool ThemeLoader::ExtractTarGz( const string tarfile, const string rootdir )
117 tartype_t gztype = { (openfunc_t) gzopen_frontend, (closefunc_t) gzclose,
118 (readfunc_t) gzread, (writefunc_t) gzwrite };
120 if( tar_open( &t, (char *)tarfile.c_str(), &gztype, O_RDONLY, 0,
126 if( tar_extract_all( t, (char *)rootdir.c_str() ) != 0 )
131 if( tar_close( t ) != 0 )
138 //---------------------------------------------------------------------------
139 bool ThemeLoader::Extract( const string FileName )
141 char *tmpdir = tempnam( NULL, "vlt" );
142 string TempPath = tmpdir;
145 if( ! ExtractTarGz( FileName, TempPath ) )
148 if( ! Parse( TempPath + DIRECTORY_SEPARATOR + string( DEFAULT_XML_FILE ) ) )
150 DeleteTempFiles( TempPath );
154 DeleteTempFiles( TempPath );
157 //---------------------------------------------------------------------------
158 void ThemeLoader::DeleteTempFiles( const string Path )
163 //---------------------------------------------------------------------------
164 void ThemeLoader::CleanTheme()
166 delete (OSTheme *)p_intf->p_sys->p_theme;
167 p_intf->p_sys->p_theme = (Theme *)new OSTheme( p_intf );
169 //---------------------------------------------------------------------------
170 bool ThemeLoader::Parse( const string XmlFile )
172 // Things to do before loading theme
173 p_intf->p_sys->p_theme->OnLoadTheme();
175 // Set the file to parse
176 yyin = fopen( XmlFile.c_str(), "r" );
179 // Skin cannot be opened
180 msg_Warn( p_intf, "Cannot open the specified skin file: %s",
186 msg_Dbg( p_intf, "Using skin file: %s", XmlFile.c_str() );
188 // Save current working directory
190 char *cwd = new char[MAX_PATH];
191 getcwd( cwd, MAX_PATH );
193 char *cwd = new char[PATH_MAX];
194 getcwd( cwd, PATH_MAX );
197 // Directory separator is different in each OS !
198 int p = XmlFile.rfind( DIRECTORY_SEPARATOR, XmlFile.size() );
200 // Change current working directory to xml file
203 path = XmlFile.substr( 0, p );
204 chdir( path.c_str() );
206 p_intf->p_sys->b_all_win_closed = false;
214 // Set old working directory to current
218 msg_Warn( p_intf, "yylex failed: %i", lex );
223 // Set old working directory to current
229 //---------------------------------------------------------------------------
230 bool ThemeLoader::Load( const string FileName )
232 // First, we try to un-targz the file, and if it fails we hope it's a XML
234 #if defined( HAVE_LIBTAR_H ) && defined( HAVE_ZLIB_H )
235 if( ! Extract( FileName ) && ! Parse( FileName ) )
238 if( ! Parse( FileName ) )
242 // Check if the skin to load is in the config file, to load its config
243 char *skin_last = config_GetPsz( p_intf, "skin_last" );
244 if( skin_last != NULL && FileName == (string)skin_last )
246 p_intf->p_sys->p_theme->LoadConfig();
250 config_PutPsz( p_intf, "skin_last", FileName.c_str() );
251 config_SaveConfigFile( p_intf, "skins" );
256 //---------------------------------------------------------------------------