1 /*****************************************************************************
2 * themeloader.cpp: ThemeLoader class
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: themeloader.cpp,v 1.2 2003/03/18 04:56:58 ipkiss 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 ---------------------------------------------------------------
31 //--- VLC -------------------------------------------------------------------
35 #if defined( HAVE_LIBTAR_H ) && defined( HAVE_ZLIB_H )
40 //--- SKIN ------------------------------------------------------------------
44 #include "themeloader.h"
45 #include "skin_common.h"
47 #define DEFAULT_XML_FILE "theme.xml"
49 //---------------------------------------------------------------------------
56 //---------------------------------------------------------------------------
57 ThemeLoader::ThemeLoader( intf_thread_t *_p_intf )
61 //---------------------------------------------------------------------------
62 ThemeLoader::~ThemeLoader()
65 //---------------------------------------------------------------------------
66 int gzopen_frontend( char *pathname, int oflags, int mode )
68 #if defined( HAVE_LIBTAR_H ) && defined( HAVE_ZLIB_H )
73 switch( oflags & O_ACCMODE )
87 fd = open( pathname, oflags, mode );
91 // if( ( oflags & O_CREAT ) && fchmod( fd, mode ) )
94 gzf = gzdopen( fd, gzflags );
106 //---------------------------------------------------------------------------
107 #if defined( HAVE_LIBTAR_H ) && defined( HAVE_ZLIB_H )
108 bool ThemeLoader::ExtractTarGz( const string tarfile, const string rootdir )
111 tartype_t gztype = { (openfunc_t) gzopen_frontend, (closefunc_t) gzclose,
112 (readfunc_t) gzread, (writefunc_t) gzwrite };
114 if( tar_open( &t, (char *)tarfile.c_str(), &gztype, O_RDONLY, 0,
120 if( tar_extract_all( t, (char *)rootdir.c_str() ) != 0 )
125 if( tar_close( t ) != 0 )
132 //---------------------------------------------------------------------------
133 bool ThemeLoader::Extract( const string FileName )
135 char *tmpdir = tempnam( NULL, "vlt" );
136 string TempPath = tmpdir;
139 if( ! ExtractTarGz( FileName, TempPath ) )
142 if( ! Parse( TempPath + DIRECTORY_SEPARATOR + string( DEFAULT_XML_FILE ) ) )
144 DeleteTempFiles( TempPath );
148 DeleteTempFiles( TempPath );
151 //---------------------------------------------------------------------------
152 void ThemeLoader::DeleteTempFiles( const string Path )
157 //---------------------------------------------------------------------------
158 void ThemeLoader::CleanTheme()
160 delete (OSTheme *)p_intf->p_sys->p_theme;
161 p_intf->p_sys->p_theme = (Theme *)new OSTheme( p_intf );
163 //---------------------------------------------------------------------------
164 bool ThemeLoader::Parse( const string XmlFile )
166 // Things to do before loading theme
167 p_intf->p_sys->p_theme->OnLoadTheme();
169 // Set the file to parse
170 yyin = fopen( XmlFile.c_str(), "r" );
173 // Skin cannot be opened
174 msg_Warn( p_intf, "Cannot open the specified skin file: %s",
180 msg_Dbg( p_intf, "Using skin file: %s", XmlFile.c_str() );
182 // Save current working directory
183 char *cwd = new char[MAX_PATH];
184 getcwd( cwd, MAX_PATH );
186 // Directory separator is different in each OS !
187 int p = XmlFile.rfind( DIRECTORY_SEPARATOR, XmlFile.size() );
189 // Change current working directory to xml file
192 path = XmlFile.substr( 0, p );
193 chdir( path.c_str() );
195 p_intf->p_sys->b_all_win_closed = false;
202 // Set old working directory to current
206 msg_Warn( p_intf, "yylex failed: %i", lex );
211 // Set old working directory to current
217 //---------------------------------------------------------------------------
218 bool ThemeLoader::Load( const string FileName )
220 // First, we try to un-targz the file, and if it fails we hope it's a XML
222 #if defined( HAVE_LIBTAR_H ) && defined( HAVE_ZLIB_H )
223 if( ! Extract( FileName ) && ! Parse( FileName ) )
226 if( ! Parse( FileName ) )
230 // Check if the skin to load is in the config file, to load its config
231 char *skin_last = config_GetPsz( p_intf, "skin_last" );
232 if( skin_last != NULL && FileName == (string)skin_last )
234 p_intf->p_sys->p_theme->LoadConfig();
238 config_PutPsz( p_intf, "skin_last", FileName.c_str() );
239 config_SaveConfigFile( p_intf, "skin" );
244 //---------------------------------------------------------------------------