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/vlcproc.hpp"
31 #include "../src/window_manager.hpp"
36 #ifdef HAVE_SYS_STAT_H
37 # include <sys/stat.h>
41 #elif defined( WIN32 ) && !defined( UNDER_CE )
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();
100 if( skin_last ) free( skin_last );
102 // The new theme cannot embed a video output yet
103 VlcProc::instance( getIntf() )->dropVout();
109 #if defined( HAVE_ZLIB_H )
110 bool ThemeLoader::extractTarGz( const string &tarFile, const string &rootDir )
113 #if defined( HAVE_LIBTAR_H )
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,
120 if( tar_open( &t, (char *)tarFile.c_str(), O_RDONLY ) == -1 )
126 if( tar_extract_all( t, (char *)rootDir.c_str() ) != 0 )
132 if( tar_close( t ) != 0 )
141 bool ThemeLoader::extract( const string &fileName )
143 char *tmpdir = tempnam( NULL, "vlt" );
144 string tempPath = tmpdir;
147 // Extract the file in a temporary directory
148 if( ! extractTarGz( fileName, tempPath ) )
151 // Find the XML file and parse it
153 if( ! findThemeFile( tempPath, xmlFile ) || ! parse( xmlFile ) )
155 msg_Err( getIntf(), "%s doesn't contain a " DEFAULT_XML_FILE " file",
157 deleteTempFiles( tempPath );
162 deleteTempFiles( tempPath );
167 void ThemeLoader::deleteTempFiles( const string &path )
169 OSFactory::instance( getIntf() )->rmDir( path );
171 #endif // HAVE_ZLIB_H
174 bool ThemeLoader::parse( const string &xmlFile )
177 msg_Dbg( getIntf(), "Using skin file: %s", xmlFile.c_str() );
179 // Extract the path of the XML file
181 const string &sep = OSFactory::instance( getIntf() )->getDirSeparator();
182 string::size_type p = xmlFile.rfind( sep, xmlFile.size() );
183 if( p != string::npos )
185 path = xmlFile.substr( 0, p + 1 );
193 SkinParser parser( getIntf(), xmlFile, path );
194 if( ! parser.parse() )
196 msg_Err( getIntf(), "Failed to parse %s", xmlFile.c_str() );
200 // Build and store the theme
201 Builder builder( getIntf(), parser.getData() );
202 getIntf()->p_sys->p_theme = builder.build();
208 bool ThemeLoader::findThemeFile( const string &rootDir, string &themeFilePath )
211 const string &sep = OSFactory::instance( getIntf() )->getDirSeparator();
214 struct dirent *pDirContent;
217 pCurrDir = opendir( rootDir.c_str() );
219 if( pCurrDir == NULL )
222 msg_Dbg( getIntf(), "Cannot open directory %s", rootDir.c_str() );
226 // Get the first directory entry
227 pDirContent = readdir( pCurrDir );
229 // While we still have entries in the directory
230 while( pDirContent != NULL )
232 string newURI = rootDir + sep + pDirContent->d_name;
235 if( string( pDirContent->d_name ) != "." &&
236 string( pDirContent->d_name ) != ".." )
238 #if defined( S_ISDIR )
239 struct stat stat_data;
240 stat( newURI.c_str(), &stat_data );
241 if( S_ISDIR(stat_data.st_mode) )
242 #elif defined( DT_DIR )
243 if( pDirContent->d_type & DT_DIR )
248 // Can we find the theme file in this subdirectory?
249 if( findThemeFile( newURI, themeFilePath ) )
251 closedir( pCurrDir );
257 // Found the theme file?
258 if( string( DEFAULT_XML_FILE ) ==
259 string( pDirContent->d_name ) )
261 themeFilePath = newURI;
262 closedir( pCurrDir );
268 pDirContent = readdir( pCurrDir );
271 closedir( pCurrDir );
276 #if !defined( HAVE_LIBTAR_H ) && defined( HAVE_ZLIB_H )
279 # define mkdir(dirname,mode) _mkdir(dirname)
282 /* Values used in typeflag field */
283 #define REGTYPE '0' /* regular file */
284 #define AREGTYPE '\0' /* regular file */
285 #define DIRTYPE '5' /* directory */
287 #define BLOCKSIZE 512
291 char name[100]; /* 0 */
292 char mode[8]; /* 100 */
293 char uid[8]; /* 108 */
294 char gid[8]; /* 116 */
295 char size[12]; /* 124 */
296 char mtime[12]; /* 136 */
297 char chksum[8]; /* 148 */
298 char typeflag; /* 156 */
299 char linkname[100]; /* 157 */
300 char magic[6]; /* 257 */
301 char version[2]; /* 263 */
302 char uname[32]; /* 265 */
303 char gname[32]; /* 297 */
304 char devmajor[8]; /* 329 */
305 char devminor[8]; /* 337 */
306 char prefix[155]; /* 345 */
312 char buffer[BLOCKSIZE];
313 struct tar_header header;
317 /* helper functions */
318 int getoct( char *p, int width );
319 int makedir( char *newdir );
321 int tar_open( TAR **t, char *pathname, int oflags )
323 gzFile f = gzopen( pathname, "rb" );
326 fprintf( stderr, "Couldn't gzopen %s\n", pathname );
330 *t = (gzFile *)malloc( sizeof(gzFile) );
336 int tar_extract_all( TAR *t, char *prefix )
338 union tar_buffer buffer;
339 int len, err, getheader = 1, remaining = 0;
340 FILE *outfile = NULL;
341 char fname[BLOCKSIZE + PATH_MAX];
345 len = gzread( *t, &buffer, BLOCKSIZE );
348 fprintf( stderr, "%s\n", gzerror(*t, &err) );
352 * Always expect complete blocks to process
353 * the tar information.
355 if( len != 0 && len != BLOCKSIZE )
357 fprintf( stderr, "gzread: incomplete block read\n" );
362 * If we have to get a tar header
367 * If we met the end of the tar
368 * or the end-of-tar block, we are done
370 if( (len == 0) || (buffer.header.name[0] == 0) )
375 sprintf( fname, "%s/%s", prefix, buffer.header.name );
377 /* Check magic value in header */
378 if( strncmp( buffer.header.magic, "GNUtar", 6 ) &&
379 strncmp( buffer.header.magic, "ustar", 5 ) )
381 //fprintf(stderr, "not a tar file\n");
385 switch( buffer.header.typeflag )
392 remaining = getoct( buffer.header.size, 12 );
395 outfile = fopen( fname, "wb" );
396 if( outfile == NULL )
398 /* try creating directory */
399 char *p = strrchr( fname, '/' );
405 outfile = fopen( fname, "wb" );
408 fprintf( stderr, "tar couldn't create %s\n",
417 * could have no contents
419 getheader = (remaining) ? 0 : 1;
427 unsigned int bytes = (remaining > BLOCKSIZE)?BLOCKSIZE:remaining;
429 if( outfile != NULL )
431 if( fwrite( &buffer, sizeof(char), bytes, outfile ) != bytes )
433 fprintf( stderr, "error writing %s skipping...\n", fname );
442 if( outfile != NULL )
455 int tar_close( TAR *t )
457 if( gzclose( *t ) != Z_OK ) fprintf( stderr, "failed gzclose\n" );
463 /* helper functions */
464 int getoct( char *p, int width )
476 result = result * 8 + (c - '0');
482 /* Recursive make directory
483 * Abort if you get an ENOENT errno somewhere in the middle
484 * e.g. ignore error "mkdir on existing directory"
486 * return 1 if OK, 0 on error
488 int makedir( char *newdir )
490 char *p, *buffer = strdup( newdir );
491 int len = strlen( buffer );
499 if( buffer[len-1] == '/' )
501 buffer[len-1] = '\0';
504 if( mkdir( buffer, 0775 ) == 0 )
515 while( *p && *p != '\\' && *p != '/' ) p++;
518 if( ( mkdir( buffer, 0775 ) == -1 ) && ( errno == ENOENT ) )
520 fprintf( stderr, "couldn't create directory %s\n", buffer );
524 if( hold == 0 ) break;
533 int gzopen_frontend( char *pathname, int oflags, int mode )
538 switch( oflags & O_ACCMODE )
552 gzf = gzopen( pathname, gzflags );