1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@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, USA.
23 *****************************************************************************/
25 #include "theme_loader.hpp"
27 #include "../parser/builder.hpp"
28 #include "../parser/skin_parser.hpp"
29 #include "../src/os_factory.hpp"
30 #include "../src/window_manager.hpp"
35 #ifdef HAVE_SYS_STAT_H
36 # include <sys/stat.h>
40 #elif defined( WIN32 )
44 #if (!defined( WIN32 ) || defined(__MINGW32__))
45 /* Mingw has its own version of dirent */
50 #if defined( HAVE_ZLIB_H )
53 int gzopen_frontend( char *pathname, int oflags, int mode );
54 #if defined( HAVE_LIBTAR_H )
58 int tar_open ( TAR **t, char *pathname, int oflags );
59 int tar_extract_all ( TAR *t, char *prefix );
60 int tar_close ( TAR *t );
64 #define DEFAULT_XML_FILE "theme.xml"
67 bool ThemeLoader::load( const string &fileName )
69 // First, we try to un-targz the file, and if it fails we hope it's a XML
71 #if defined( HAVE_ZLIB_H )
72 if( ! extract( fileName ) && ! parse( fileName ) )
75 if( ! parse( fileName ) )
79 Theme *pNewTheme = getIntf()->p_sys->p_theme;
85 // Check if the skin to load is in the config file, to load its config
86 char *skin_last = config_GetPsz( getIntf(), "skins2-last" );
87 if( skin_last != NULL && fileName == (string)skin_last )
89 // Restore the theme configuration
90 getIntf()->p_sys->p_theme->loadConfig();
91 // Used to anchor the windows at the beginning
92 pNewTheme->getWindowManager().stopMove();
96 config_PutPsz( getIntf(), "skins2-last", fileName.c_str() );
98 pNewTheme->getWindowManager().showAll();
105 #if defined( HAVE_ZLIB_H )
106 bool ThemeLoader::extractTarGz( const string &tarFile, const string &rootDir )
109 #if defined( HAVE_LIBTAR_H )
110 tartype_t gztype = { (openfunc_t) gzopen_frontend, (closefunc_t) gzclose,
111 (readfunc_t) gzread, (writefunc_t) gzwrite };
113 if( tar_open( &t, (char *)tarFile.c_str(), &gztype, O_RDONLY, 0,
116 if( tar_open( &t, (char *)tarFile.c_str(), O_RDONLY ) == -1 )
122 if( tar_extract_all( t, (char *)rootDir.c_str() ) != 0 )
128 if( tar_close( t ) != 0 )
137 bool ThemeLoader::extract( const string &fileName )
139 char *tmpdir = tempnam( NULL, "vlt" );
140 string tempPath = tmpdir;
143 // Extract the file in a temporary directory
144 if( ! extractTarGz( fileName, tempPath ) )
147 // Find the XML file and parse it
149 if( ! findThemeFile( tempPath, xmlFile ) || ! parse( xmlFile ) )
151 msg_Err( getIntf(), "%s doesn't contain a " DEFAULT_XML_FILE " file",
153 deleteTempFiles( tempPath );
158 deleteTempFiles( tempPath );
163 void ThemeLoader::deleteTempFiles( const string &path )
165 OSFactory::instance( getIntf() )->rmDir( path );
167 #endif // HAVE_ZLIB_H
170 bool ThemeLoader::parse( const string &xmlFile )
173 msg_Dbg( getIntf(), "Using skin file: %s", xmlFile.c_str() );
175 // Extract the path of the XML file
177 const string &sep = OSFactory::instance( getIntf() )->getDirSeparator();
178 unsigned int p = xmlFile.rfind( sep, xmlFile.size() );
179 if( p != string::npos )
181 path = xmlFile.substr( 0, p + 1 );
189 SkinParser parser( getIntf(), xmlFile, path );
190 if( ! parser.parse() )
192 msg_Err( getIntf(), "Failed to parse %s", xmlFile.c_str() );
196 // Build and store the theme
197 Builder builder( getIntf(), parser.getData() );
198 getIntf()->p_sys->p_theme = builder.build();
204 bool ThemeLoader::findThemeFile( const string &rootDir, string &themeFilePath )
207 const string &sep = OSFactory::instance( getIntf() )->getDirSeparator();
210 struct dirent *pDirContent;
213 pCurrDir = opendir( rootDir.c_str() );
215 if( pCurrDir == NULL )
218 msg_Dbg( getIntf(), "Cannot open directory %s", rootDir.c_str() );
222 // Get the first directory entry
223 pDirContent = readdir( pCurrDir );
225 // While we still have entries in the directory
226 while( pDirContent != NULL )
228 string newURI = rootDir + sep + pDirContent->d_name;
231 if( string( pDirContent->d_name ) != "." &&
232 string( pDirContent->d_name ) != ".." )
234 #if defined( S_ISDIR )
235 struct stat stat_data;
236 stat( newURI.c_str(), &stat_data );
237 if( S_ISDIR(stat_data.st_mode) )
238 #elif defined( DT_DIR )
239 if( pDirContent->d_type & DT_DIR )
244 // Can we find the theme file in this subdirectory?
245 if( findThemeFile( newURI, themeFilePath ) )
252 // Found the theme file?
253 if( string( DEFAULT_XML_FILE ) ==
254 string( pDirContent->d_name ) )
256 themeFilePath = newURI;
262 pDirContent = readdir( pCurrDir );
269 #if !defined( HAVE_LIBTAR_H ) && defined( HAVE_ZLIB_H )
272 # define mkdir(dirname,mode) _mkdir(dirname)
275 /* Values used in typeflag field */
276 #define REGTYPE '0' /* regular file */
277 #define AREGTYPE '\0' /* regular file */
278 #define DIRTYPE '5' /* directory */
280 #define BLOCKSIZE 512
284 char name[100]; /* 0 */
285 char mode[8]; /* 100 */
286 char uid[8]; /* 108 */
287 char gid[8]; /* 116 */
288 char size[12]; /* 124 */
289 char mtime[12]; /* 136 */
290 char chksum[8]; /* 148 */
291 char typeflag; /* 156 */
292 char linkname[100]; /* 157 */
293 char magic[6]; /* 257 */
294 char version[2]; /* 263 */
295 char uname[32]; /* 265 */
296 char gname[32]; /* 297 */
297 char devmajor[8]; /* 329 */
298 char devminor[8]; /* 337 */
299 char prefix[155]; /* 345 */
305 char buffer[BLOCKSIZE];
306 struct tar_header header;
310 /* helper functions */
311 int getoct( char *p, int width );
312 int makedir( char *newdir );
314 int tar_open( TAR **t, char *pathname, int oflags )
316 gzFile f = gzopen( pathname, "rb" );
319 fprintf( stderr, "Couldn't gzopen %s\n", pathname );
323 *t = (gzFile *)malloc( sizeof(gzFile) );
329 int tar_extract_all( TAR *t, char *prefix )
331 union tar_buffer buffer;
332 int len, err, getheader = 1, remaining = 0;
333 FILE *outfile = NULL;
334 char fname[BLOCKSIZE + PATH_MAX];
338 len = gzread( *t, &buffer, BLOCKSIZE );
341 fprintf( stderr, "%s\n", gzerror(*t, &err) );
345 * Always expect complete blocks to process
346 * the tar information.
348 if( len != 0 && len != BLOCKSIZE )
350 fprintf( stderr, "gzread: incomplete block read\n" );
355 * If we have to get a tar header
360 * If we met the end of the tar
361 * or the end-of-tar block, we are done
363 if( (len == 0) || (buffer.header.name[0] == 0) )
368 sprintf( fname, "%s/%s", prefix, buffer.header.name );
370 /* Check magic value in header */
371 if( strncmp( buffer.header.magic, "GNUtar", 6 ) &&
372 strncmp( buffer.header.magic, "ustar", 5 ) )
374 //fprintf(stderr, "not a tar file\n");
378 switch( buffer.header.typeflag )
385 remaining = getoct( buffer.header.size, 12 );
388 outfile = fopen( fname, "wb" );
389 if( outfile == NULL )
391 /* try creating directory */
392 char *p = strrchr( fname, '/' );
398 outfile = fopen( fname, "wb" );
401 fprintf( stderr, "tar couldn't create %s\n",
410 * could have no contents
412 getheader = (remaining) ? 0 : 1;
420 unsigned int bytes = (remaining > BLOCKSIZE)?BLOCKSIZE:remaining;
422 if( outfile != NULL )
424 if( fwrite( &buffer, sizeof(char), bytes, outfile ) != bytes )
426 fprintf( stderr, "error writing %s skipping...\n", fname );
435 if( outfile != NULL )
448 int tar_close( TAR *t )
450 if( gzclose( *t ) != Z_OK ) fprintf( stderr, "failed gzclose\n" );
456 /* helper functions */
457 int getoct( char *p, int width )
469 result = result * 8 + (c - '0');
475 /* Recursive make directory
476 * Abort if you get an ENOENT errno somewhere in the middle
477 * e.g. ignore error "mkdir on existing directory"
479 * return 1 if OK, 0 on error
481 int makedir( char *newdir )
483 char *p, *buffer = strdup( newdir );
484 int len = strlen( buffer );
492 if( buffer[len-1] == '/' )
494 buffer[len-1] = '\0';
497 if( mkdir( buffer, 0775 ) == 0 )
508 while( *p && *p != '\\' && *p != '/' ) p++;
511 if( ( mkdir( buffer, 0775 ) == -1 ) && ( errno == ENOENT ) )
513 fprintf( stderr, "couldn't create directory %s\n", buffer );
517 if( hold == 0 ) break;
526 int gzopen_frontend( char *pathname, int oflags, int mode )
531 switch( oflags & O_ACCMODE )
545 gzf = gzopen( pathname, gzflags );