1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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"
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
43 #elif defined( WIN32 ) && !defined( UNDER_CE )
52 #if defined( HAVE_ZLIB_H )
55 int gzopen_frontend ( const char *pathname, int oflags, int mode );
56 int gzclose_frontend( int );
57 int gzread_frontend ( int, void *, size_t );
58 int gzwrite_frontend( int, const void *, size_t );
59 #if defined( HAVE_LIBTAR_H )
63 int tar_open ( TAR **t, char *pathname, int oflags );
64 int tar_extract_all ( TAR *t, char *prefix );
65 int tar_close ( TAR *t );
66 int getoct( char *p, int width );
68 int makedir( const char *newdir );
71 #define DEFAULT_XML_FILE "theme.xml"
72 #define WINAMP2_XML_FILE "winamp2.xml"
73 #define ZIP_BUFFER_SIZE 4096
76 bool ThemeLoader::load( const string &fileName )
78 string path = getFilePath( fileName );
80 //Before all, let's see if the file is present
82 if( utf8_stat( path.c_str(), &p_stat ) )
85 // First, we try to un-targz the file, and if it fails we hope it's a XML
88 #if defined( HAVE_ZLIB_H )
89 if( ! extract( sToLocale( fileName ) ) && ! parse( path, fileName ) )
92 if( ! parse( path, fileName ) )
96 Theme *pNewTheme = getIntf()->p_sys->p_theme;
102 // Check if the skin to load is in the config file, to load its config
103 char *skin_last = config_GetPsz( getIntf(), "skins2-last" );
104 if( skin_last != NULL && fileName == (string)skin_last )
106 // Restore the theme configuration
107 getIntf()->p_sys->p_theme->loadConfig();
108 // Used to anchor the windows at the beginning
109 pNewTheme->getWindowManager().stopMove();
113 config_PutPsz( getIntf(), "skins2-last", fileName.c_str() );
115 pNewTheme->getWindowManager().showAll( true );
123 #if defined( HAVE_ZLIB_H )
124 bool ThemeLoader::extractTarGz( const string &tarFile, const string &rootDir )
127 #if defined( HAVE_LIBTAR_H )
128 tartype_t gztype = { (openfunc_t) gzopen_frontend,
129 (closefunc_t) gzclose_frontend,
130 (readfunc_t) gzread_frontend,
131 (writefunc_t) gzwrite_frontend };
133 if( tar_open( &t, (char *)tarFile.c_str(), &gztype, O_RDONLY, 0,
136 if( tar_open( &t, (char *)tarFile.c_str(), O_RDONLY ) == -1 )
142 if( tar_extract_all( t, (char *)rootDir.c_str() ) != 0 )
148 if( tar_close( t ) != 0 )
157 bool ThemeLoader::extractZip( const string &zipFile, const string &rootDir )
159 // Try to open the ZIP file
160 unzFile file = unzOpen( zipFile.c_str() );
161 unz_global_info info;
163 if( unzGetGlobalInfo( file, &info ) != UNZ_OK )
167 // Extract all the files in the archive
168 for( unsigned long i = 0; i < info.number_entry; i++ )
170 if( !extractFileInZip( file, rootDir ) )
172 msg_Warn( getIntf(), "error while unzipping %s",
178 if( i < info.number_entry - 1 )
180 // Go the next file in the archive
181 if( unzGoToNextFile( file ) !=UNZ_OK )
183 msg_Warn( getIntf(), "error while unzipping %s",
195 bool ThemeLoader::extractFileInZip( unzFile file, const string &rootDir )
197 // Read info for the current file
198 char filenameInZip[256];
199 unz_file_info fileInfo;
200 if( unzGetCurrentFileInfo( file, &fileInfo, filenameInZip,
201 sizeof( filenameInZip), NULL, 0, NULL, 0 )
207 // Convert the file name to lower case, because some winamp skins
208 // use the wrong case...
209 for( size_t i=0; i< strlen( filenameInZip ); i++)
211 filenameInZip[i] = tolower( filenameInZip[i] );
214 // Allocate the buffer
215 void *pBuffer = malloc( ZIP_BUFFER_SIZE );
219 // Get the path of the file
220 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
221 string fullPath = rootDir
222 + pOsFactory->getDirSeparator()
223 + fixDirSeparators( filenameInZip );
224 string basePath = getFilePath( fullPath );
226 // Extract the file if is not a directory
227 if( basePath != fullPath )
229 if( unzOpenCurrentFile( file ) )
234 makedir( basePath.c_str() );
235 FILE *fout = fopen( fullPath.c_str(), "wb" );
238 msg_Err( getIntf(), "error opening %s", fullPath.c_str() );
243 // Extract the current file
247 n = unzReadCurrentFile( file, pBuffer, ZIP_BUFFER_SIZE );
250 msg_Err( getIntf(), "error while reading zip file" );
256 if( fwrite( pBuffer, n , 1, fout) != 1 )
258 msg_Err( getIntf(), "error while writing %s",
268 if( unzCloseCurrentFile( file ) != UNZ_OK )
280 bool ThemeLoader::extract( const string &fileName )
283 char *tmpdir = tempnam( NULL, "vlt" );
284 string tempPath = sFromLocale( tmpdir );
287 // Extract the file in a temporary directory
288 if( ! extractTarGz( fileName, tempPath ) &&
289 ! extractZip( fileName, tempPath ) )
291 deleteTempFiles( tempPath );
297 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
298 // Find the XML file in the theme
299 if( findFile( tempPath, DEFAULT_XML_FILE, xmlFile ) )
301 path = getFilePath( xmlFile );
305 // No XML file, check if it is a winamp2 skin
307 if( findFile( tempPath, "main.bmp", mainBmp ) )
309 msg_Dbg( getIntf(), "trying to load a winamp2 skin" );
310 path = getFilePath( mainBmp );
312 // Look for winamp2.xml in the resource path
313 list<string> resPath = pOsFactory->getResourcePath();
314 list<string>::const_iterator it;
315 for( it = resPath.begin(); it != resPath.end(); it++ )
317 if( findFile( *it, WINAMP2_XML_FILE, xmlFile ) )
323 if( !xmlFile.empty() )
325 // Parse the XML file
326 if (! parse( path, xmlFile ) )
328 msg_Err( getIntf(), "error while parsing %s", xmlFile.c_str() );
334 msg_Err( getIntf(), "no XML found in theme %s", fileName.c_str() );
339 deleteTempFiles( tempPath );
344 void ThemeLoader::deleteTempFiles( const string &path )
346 OSFactory::instance( getIntf() )->rmDir( path );
348 #endif // HAVE_ZLIB_H
351 bool ThemeLoader::parse( const string &path, const string &xmlFile )
354 msg_Dbg( getIntf(), "using skin file: %s", xmlFile.c_str() );
357 SkinParser parser( getIntf(), xmlFile, path );
358 if( ! parser.parse() )
361 // Build and store the theme
362 Builder builder( getIntf(), parser.getData(), path );
363 getIntf()->p_sys->p_theme = builder.build();
369 string ThemeLoader::getFilePath( const string &rFullPath )
371 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
372 const string &sep = pOsFactory->getDirSeparator();
373 // Find the last separator ('/' or '\')
374 string::size_type p = rFullPath.rfind( sep, rFullPath.size() );
376 if( p != string::npos )
378 if( p < rFullPath.size() - 1)
380 basePath = rFullPath.substr( 0, p );
384 basePath = rFullPath;
391 string ThemeLoader::fixDirSeparators( const string &rPath )
393 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
394 const string &sep = pOsFactory->getDirSeparator();
395 string::size_type p = rPath.find( "/", 0 );
396 string newPath = rPath;
397 while( p != string::npos )
399 newPath = newPath.replace( p, 1, sep );
400 p = newPath.find( "/", p + 1 );
406 bool ThemeLoader::findFile( const string &rootDir, const string &rFileName,
407 string &themeFilePath )
410 const string &sep = OSFactory::instance( getIntf() )->getDirSeparator();
416 pCurrDir = utf8_opendir( rootDir.c_str() );
418 if( pCurrDir == NULL )
421 msg_Dbg( getIntf(), "cannot open directory %s", rootDir.c_str() );
425 // While we still have entries in the directory
426 while( ( pszDirContent = utf8_readdir( pCurrDir ) ) != NULL )
428 string newURI = rootDir + sep + pszDirContent;
431 if( string( pszDirContent ) != "." &&
432 string( pszDirContent ) != ".." )
434 #if defined( S_ISDIR )
435 struct stat stat_data;
437 if( ( utf8_stat( newURI.c_str(), &stat_data ) == 0 )
438 && S_ISDIR(stat_data.st_mode) )
439 #elif defined( DT_DIR )
440 if( pDirContent->d_type & DT_DIR )
445 // Can we find the file in this subdirectory?
446 if( findFile( newURI, rFileName, themeFilePath ) )
448 free( pszDirContent );
449 closedir( pCurrDir );
455 // Found the theme file?
456 if( rFileName == string( pszDirContent ) )
458 themeFilePath = newURI;
459 free( pszDirContent );
460 closedir( pCurrDir );
466 free( pszDirContent );
469 closedir( pCurrDir );
474 #if !defined( HAVE_LIBTAR_H ) && defined( HAVE_ZLIB_H )
476 /* Values used in typeflag field */
477 #define REGTYPE '0' /* regular file */
478 #define AREGTYPE '\0' /* regular file */
479 #define DIRTYPE '5' /* directory */
481 #define BLOCKSIZE 512
485 char name[100]; /* 0 */
486 char mode[8]; /* 100 */
487 char uid[8]; /* 108 */
488 char gid[8]; /* 116 */
489 char size[12]; /* 124 */
490 char mtime[12]; /* 136 */
491 char chksum[8]; /* 148 */
492 char typeflag; /* 156 */
493 char linkname[100]; /* 157 */
494 char magic[6]; /* 257 */
495 char version[2]; /* 263 */
496 char uname[32]; /* 265 */
497 char gname[32]; /* 297 */
498 char devmajor[8]; /* 329 */
499 char devminor[8]; /* 337 */
500 char prefix[155]; /* 345 */
506 char buffer[BLOCKSIZE];
507 struct tar_header header;
512 int tar_open( TAR **t, char *pathname, int oflags )
514 gzFile f = gzopen( pathname, "rb" );
517 fprintf( stderr, "Couldn't gzopen %s\n", pathname );
521 *t = (gzFile *)malloc( sizeof(gzFile) );
527 int tar_extract_all( TAR *t, char *prefix )
529 union tar_buffer buffer;
530 int len, err, getheader = 1, remaining = 0;
531 FILE *outfile = NULL;
532 char fname[BLOCKSIZE + PATH_MAX];
536 len = gzread( *t, &buffer, BLOCKSIZE );
539 fprintf( stderr, "%s\n", gzerror(*t, &err) );
543 * Always expect complete blocks to process
544 * the tar information.
546 if( len != 0 && len != BLOCKSIZE )
548 fprintf( stderr, "gzread: incomplete block read\n" );
553 * If we have to get a tar header
558 * If we met the end of the tar
559 * or the end-of-tar block, we are done
561 if( (len == 0) || (buffer.header.name[0] == 0) )
566 sprintf( fname, "%s/%s", prefix, buffer.header.name );
568 /* Check magic value in header */
569 if( strncmp( buffer.header.magic, "GNUtar", 6 ) &&
570 strncmp( buffer.header.magic, "ustar", 5 ) )
572 //fprintf(stderr, "not a tar file\n");
576 switch( buffer.header.typeflag )
583 remaining = getoct( buffer.header.size, 12 );
586 outfile = fopen( fname, "wb" );
587 if( outfile == NULL )
589 /* try creating directory */
590 char *p = strrchr( fname, '/' );
596 outfile = fopen( fname, "wb" );
599 fprintf( stderr, "tar couldn't create %s\n",
608 * could have no contents
610 getheader = (remaining) ? 0 : 1;
618 unsigned int bytes = (remaining > BLOCKSIZE)?BLOCKSIZE:remaining;
620 if( outfile != NULL )
622 if( fwrite( &buffer, sizeof(char), bytes, outfile ) != bytes )
624 fprintf( stderr, "error writing %s skipping...\n", fname );
634 if( outfile != NULL )
647 int tar_close( TAR *t )
649 if( gzclose( *t ) != Z_OK ) fprintf( stderr, "failed gzclose\n" );
655 /* helper functions */
656 int getoct( char *p, int width )
668 result = result * 8 + (c - '0');
676 # define mkdir(dirname,mode) _mkdir(dirname)
679 /* Recursive make directory
680 * Abort if you get an ENOENT errno somewhere in the middle
681 * e.g. ignore error "mkdir on existing directory"
683 * return 1 if OK, 0 on error
685 int makedir( const char *newdir )
687 char *p, *buffer = strdup( newdir );
688 int len = strlen( buffer );
696 if( buffer[len-1] == '/' )
698 buffer[len-1] = '\0';
701 if( mkdir( buffer, 0775 ) == 0 )
712 while( *p && *p != '\\' && *p != '/' ) p++;
715 if( ( mkdir( buffer, 0775 ) == -1 ) && ( errno == ENOENT ) )
717 fprintf( stderr, "couldn't create directory %s\n", buffer );
721 if( hold == 0 ) break;
730 static int currentGzFd = -1;
731 static void * currentGzVp = NULL;
733 int gzopen_frontend( const char *pathname, int oflags, int mode )
752 gzf = gzopen( pathname, gzflags );
766 int gzclose_frontend( int fd )
768 if( currentGzVp != NULL && fd != -1 )
770 void *toClose = currentGzVp;
771 currentGzVp = NULL; currentGzFd = -1;
772 return gzclose( toClose );
777 int gzread_frontend( int fd, void *p_buffer, size_t i_length )
779 if( currentGzVp != NULL && fd != -1 )
781 return gzread( currentGzVp, p_buffer, i_length );
786 int gzwrite_frontend( int fd, const void * p_buffer, size_t i_length )
788 if( currentGzVp != NULL && fd != -1 )
790 return gzwrite( currentGzVp, const_cast<void*>(p_buffer), i_length );