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"
41 #if (!defined( WIN32 ) || defined(__MINGW32__))
42 /* Mingw has its own version of dirent */
47 #if defined( HAVE_ZLIB_H )
50 int gzopen_frontend( char *pathname, int oflags, int mode );
51 #if defined( HAVE_LIBTAR_H )
55 int tar_open ( TAR **t, char *pathname, int oflags );
56 int tar_extract_all ( TAR *t, char *prefix );
57 int tar_close ( TAR *t );
61 #define DEFAULT_XML_FILE "theme.xml"
64 bool ThemeLoader::load( const string &fileName )
66 // First, we try to un-targz the file, and if it fails we hope it's a XML
68 #if defined( HAVE_ZLIB_H )
69 if( ! extract( fileName ) && ! parse( fileName ) )
72 if( ! parse( fileName ) )
76 Theme *pNewTheme = getIntf()->p_sys->p_theme;
82 // Check if the skin to load is in the config file, to load its config
83 char *skin_last = config_GetPsz( getIntf(), "skins2-last" );
84 if( skin_last != NULL && fileName == (string)skin_last )
86 // Restore the theme configuration
87 getIntf()->p_sys->p_theme->loadConfig();
88 // Used to anchor the windows at the beginning
89 pNewTheme->getWindowManager().stopMove();
93 config_PutPsz( getIntf(), "skins2-last", fileName.c_str() );
95 pNewTheme->getWindowManager().showAll();
102 #if defined( HAVE_ZLIB_H )
103 bool ThemeLoader::extractTarGz( const string &tarFile, const string &rootDir )
106 #if defined( HAVE_LIBTAR_H )
107 tartype_t gztype = { (openfunc_t) gzopen_frontend, (closefunc_t) gzclose,
108 (readfunc_t) gzread, (writefunc_t) gzwrite };
110 if( tar_open( &t, (char *)tarFile.c_str(), &gztype, O_RDONLY, 0,
113 if( tar_open( &t, (char *)tarFile.c_str(), O_RDONLY ) == -1 )
119 if( tar_extract_all( t, (char *)rootDir.c_str() ) != 0 )
125 if( tar_close( t ) != 0 )
134 bool ThemeLoader::extract( const string &fileName )
136 char *tmpdir = tempnam( NULL, "vlt" );
137 string tempPath = tmpdir;
140 // Extract the file in a temporary directory
141 if( ! extractTarGz( fileName, tempPath ) )
144 // Find the XML file and parse it
146 if( ! findThemeFile( tempPath, xmlFile ) || ! parse( xmlFile ) )
148 msg_Err( getIntf(), "%s doesn't contain a " DEFAULT_XML_FILE " file",
150 deleteTempFiles( tempPath );
155 deleteTempFiles( tempPath );
160 void ThemeLoader::deleteTempFiles( const string &path )
162 OSFactory::instance( getIntf() )->rmDir( path );
164 #endif // HAVE_ZLIB_H
167 bool ThemeLoader::parse( const string &xmlFile )
170 msg_Dbg( getIntf(), "Using skin file: %s", xmlFile.c_str() );
172 // Extract the path of the XML file
174 const string &sep = OSFactory::instance( getIntf() )->getDirSeparator();
175 unsigned int p = xmlFile.rfind( sep, xmlFile.size() );
176 if( p != string::npos )
178 path = xmlFile.substr( 0, p + 1 );
186 SkinParser parser( getIntf(), xmlFile, path );
187 if( ! parser.parse() )
189 msg_Err( getIntf(), "Failed to parse %s", xmlFile.c_str() );
193 // Build and store the theme
194 Builder builder( getIntf(), parser.getData() );
195 getIntf()->p_sys->p_theme = builder.build();
201 bool ThemeLoader::findThemeFile( const string &rootDir, string &themeFilePath )
204 const string &sep = OSFactory::instance( getIntf() )->getDirSeparator();
207 struct dirent *pDirContent;
210 pCurrDir = opendir( rootDir.c_str() );
212 if( pCurrDir == NULL )
215 msg_Dbg( getIntf(), "Cannot open directory %s", rootDir.c_str() );
219 // Get the first directory entry
220 pDirContent = readdir( pCurrDir );
222 // While we still have entries in the directory
223 while( pDirContent != NULL )
225 string newURI = rootDir + sep + pDirContent->d_name;
228 if( string( pDirContent->d_name ) != "." &&
229 string( pDirContent->d_name ) != ".." )
231 #if defined( S_ISDIR )
232 struct stat stat_data;
233 stat( newURI.c_str(), &stat_data );
234 if( S_ISDIR(stat_data.st_mode) )
235 #elif defined( DT_DIR )
236 if( pDirContent->d_type & DT_DIR )
241 // Can we find the theme file in this subdirectory?
242 if( findThemeFile( newURI, themeFilePath ) )
249 // Found the theme file?
250 if( string( DEFAULT_XML_FILE ) ==
251 string( pDirContent->d_name ) )
253 themeFilePath = newURI;
259 pDirContent = readdir( pCurrDir );
266 #if !defined( HAVE_LIBTAR_H ) && defined( HAVE_ZLIB_H )
269 # define mkdir(dirname,mode) _mkdir(dirname)
272 /* Values used in typeflag field */
273 #define REGTYPE '0' /* regular file */
274 #define AREGTYPE '\0' /* regular file */
275 #define DIRTYPE '5' /* directory */
277 #define BLOCKSIZE 512
281 char name[100]; /* 0 */
282 char mode[8]; /* 100 */
283 char uid[8]; /* 108 */
284 char gid[8]; /* 116 */
285 char size[12]; /* 124 */
286 char mtime[12]; /* 136 */
287 char chksum[8]; /* 148 */
288 char typeflag; /* 156 */
289 char linkname[100]; /* 157 */
290 char magic[6]; /* 257 */
291 char version[2]; /* 263 */
292 char uname[32]; /* 265 */
293 char gname[32]; /* 297 */
294 char devmajor[8]; /* 329 */
295 char devminor[8]; /* 337 */
296 char prefix[155]; /* 345 */
302 char buffer[BLOCKSIZE];
303 struct tar_header header;
307 /* helper functions */
308 int getoct( char *p, int width );
309 int makedir( char *newdir );
311 int tar_open( TAR **t, char *pathname, int oflags )
313 gzFile f = gzopen( pathname, "rb" );
316 fprintf( stderr, "Couldn't gzopen %s\n", pathname );
320 *t = (gzFile *)malloc( sizeof(gzFile) );
326 int tar_extract_all( TAR *t, char *prefix )
328 union tar_buffer buffer;
329 int len, err, getheader = 1, remaining = 0;
330 FILE *outfile = NULL;
331 char fname[BLOCKSIZE + PATH_MAX];
335 len = gzread( *t, &buffer, BLOCKSIZE );
338 fprintf( stderr, "%s\n", gzerror(*t, &err) );
342 * Always expect complete blocks to process
343 * the tar information.
345 if( len != 0 && len != BLOCKSIZE )
347 fprintf( stderr, "gzread: incomplete block read\n" );
352 * If we have to get a tar header
357 * If we met the end of the tar
358 * or the end-of-tar block, we are done
360 if( (len == 0) || (buffer.header.name[0] == 0) )
365 sprintf( fname, "%s/%s", prefix, buffer.header.name );
367 /* Check magic value in header */
368 if( strncmp( buffer.header.magic, "GNUtar", 6 ) &&
369 strncmp( buffer.header.magic, "ustar", 5 ) )
371 //fprintf(stderr, "not a tar file\n");
375 switch( buffer.header.typeflag )
382 remaining = getoct( buffer.header.size, 12 );
385 outfile = fopen( fname, "wb" );
386 if( outfile == NULL )
388 /* try creating directory */
389 char *p = strrchr( fname, '/' );
395 outfile = fopen( fname, "wb" );
398 fprintf( stderr, "tar couldn't create %s\n",
407 * could have no contents
409 getheader = (remaining) ? 0 : 1;
417 unsigned int bytes = (remaining > BLOCKSIZE)?BLOCKSIZE:remaining;
419 if( outfile != NULL )
421 if( fwrite( &buffer, sizeof(char), bytes, outfile ) != bytes )
423 fprintf( stderr, "error writing %s skipping...\n", fname );
432 if( outfile != NULL )
445 int tar_close( TAR *t )
447 if( gzclose( *t ) != Z_OK ) fprintf( stderr, "failed gzclose\n" );
453 /* helper functions */
454 int getoct( char *p, int width )
466 result = result * 8 + (c - '0');
472 /* Recursive make directory
473 * Abort if you get an ENOENT errno somewhere in the middle
474 * e.g. ignore error "mkdir on existing directory"
476 * return 1 if OK, 0 on error
478 int makedir( char *newdir )
480 char *p, *buffer = strdup( newdir );
481 int len = strlen( buffer );
489 if( buffer[len-1] == '/' )
491 buffer[len-1] = '\0';
494 if( mkdir( buffer, 0775 ) == 0 )
505 while( *p && *p != '\\' && *p != '/' ) p++;
508 if( ( mkdir( buffer, 0775 ) == -1 ) && ( errno == ENOENT ) )
510 fprintf( stderr, "couldn't create directory %s\n", buffer );
514 if( hold == 0 ) break;
523 int gzopen_frontend( char *pathname, int oflags, int mode )
528 switch( oflags & O_ACCMODE )
542 gzf = gzopen( pathname, gzflags );