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"
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 ( const char *pathname, int oflags, int mode );
54 int gzclose_frontend( int );
55 int gzread_frontend ( int, void *, size_t );
56 int gzwrite_frontend( int, const void *, size_t );
57 #if defined( HAVE_LIBTAR_H )
61 int tar_open ( TAR **t, char *pathname, int oflags );
62 int tar_extract_all ( TAR *t, char *prefix );
63 int tar_close ( TAR *t );
64 int getoct( char *p, int width );
66 int makedir( const char *newdir );
69 #define DEFAULT_XML_FILE "theme.xml"
70 #define WINAMP2_XML_FILE "winamp2.xml"
71 #define ZIP_BUFFER_SIZE 4096
74 bool ThemeLoader::load( const string &fileName )
76 string path = getFilePath( fileName );
78 //Before all, let's see if the file is present
80 if( vlc_stat( fileName.c_str(), &p_stat ) )
83 // First, we try to un-targz the file, and if it fails we hope it's a XML
86 #if defined( HAVE_ZLIB_H )
87 if( ! extract( sToLocale( fileName ) ) && ! parse( path, fileName ) )
90 if( ! parse( path, fileName ) )
94 Theme *pNewTheme = getIntf()->p_sys->p_theme;
98 // Restore the theme configuration
99 getIntf()->p_sys->p_theme->loadConfig();
101 // Retain new loaded skins in config
102 config_PutPsz( getIntf(), "skins2-last", fileName.c_str() );
108 #if defined( HAVE_ZLIB_H )
109 bool ThemeLoader::extractTarGz( const string &tarFile, const string &rootDir )
112 #if defined( HAVE_LIBTAR_H )
113 tartype_t gztype = { (openfunc_t) gzopen_frontend,
114 (closefunc_t) gzclose_frontend,
115 (readfunc_t) gzread_frontend,
116 (writefunc_t) gzwrite_frontend };
118 if( tar_open( &t, (char *)tarFile.c_str(), &gztype, O_RDONLY, 0,
121 if( tar_open( &t, (char *)tarFile.c_str(), O_RDONLY ) == -1 )
127 if( tar_extract_all( t, (char *)rootDir.c_str() ) != 0 )
133 if( tar_close( t ) != 0 )
142 bool ThemeLoader::extractZip( const string &zipFile, const string &rootDir )
144 // Try to open the ZIP file
145 unzFile file = unzOpen( zipFile.c_str() );
146 unz_global_info info;
148 if( unzGetGlobalInfo( file, &info ) != UNZ_OK )
152 // Extract all the files in the archive
153 for( unsigned long i = 0; i < info.number_entry; i++ )
155 if( !extractFileInZip( file, rootDir ) )
157 msg_Warn( getIntf(), "error while unzipping %s",
163 if( i < info.number_entry - 1 )
165 // Go the next file in the archive
166 if( unzGoToNextFile( file ) !=UNZ_OK )
168 msg_Warn( getIntf(), "error while unzipping %s",
180 bool ThemeLoader::extractFileInZip( unzFile file, const string &rootDir )
182 // Read info for the current file
183 char filenameInZip[256];
184 unz_file_info fileInfo;
185 if( unzGetCurrentFileInfo( file, &fileInfo, filenameInZip,
186 sizeof( filenameInZip), NULL, 0, NULL, 0 )
194 // Convert the file name to lower case, because some winamp skins
195 // use the wrong case...
196 for( size_t i=0; i< strlen( filenameInZip ); i++)
198 filenameInZip[i] = tolower( filenameInZip[i] );
203 // Allocate the buffer
204 void *pBuffer = malloc( ZIP_BUFFER_SIZE );
208 // Get the path of the file
209 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
210 string fullPath = rootDir
211 + pOsFactory->getDirSeparator()
212 + fixDirSeparators( filenameInZip );
213 string basePath = getFilePath( fullPath );
215 // Extract the file if is not a directory
216 if( basePath != fullPath )
218 if( unzOpenCurrentFile( file ) )
223 makedir( basePath.c_str() );
224 FILE *fout = fopen( fullPath.c_str(), "wb" );
227 msg_Err( getIntf(), "error opening %s", fullPath.c_str() );
232 // Extract the current file
236 n = unzReadCurrentFile( file, pBuffer, ZIP_BUFFER_SIZE );
239 msg_Err( getIntf(), "error while reading zip file" );
245 if( fwrite( pBuffer, n , 1, fout) != 1 )
247 msg_Err( getIntf(), "error while writing %s",
257 if( unzCloseCurrentFile( file ) != UNZ_OK )
269 bool ThemeLoader::extract( const string &fileName )
272 char *tmpdir = tempnam( NULL, "vlt" );
273 string tempPath = sFromLocale( tmpdir );
276 // Extract the file in a temporary directory
277 if( ! extractTarGz( fileName, tempPath ) &&
278 ! extractZip( fileName, tempPath ) )
280 deleteTempFiles( tempPath );
286 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
287 // Find the XML file in the theme
288 if( findFile( tempPath, DEFAULT_XML_FILE, xmlFile ) )
290 path = getFilePath( xmlFile );
294 // No XML file, check if it is a winamp2 skin
296 if( findFile( tempPath, "main.bmp", mainBmp ) )
298 msg_Dbg( getIntf(), "trying to load a winamp2 skin" );
299 path = getFilePath( mainBmp );
301 // Look for winamp2.xml in the resource path
302 list<string> resPath = pOsFactory->getResourcePath();
303 list<string>::const_iterator it;
304 for( it = resPath.begin(); it != resPath.end(); it++ )
306 if( findFile( *it, WINAMP2_XML_FILE, xmlFile ) )
312 if( !xmlFile.empty() )
314 // Parse the XML file
315 if (! parse( path, xmlFile ) )
317 msg_Err( getIntf(), "error while parsing %s", xmlFile.c_str() );
323 msg_Err( getIntf(), "no XML found in theme %s", fileName.c_str() );
328 deleteTempFiles( tempPath );
333 void ThemeLoader::deleteTempFiles( const string &path )
335 OSFactory::instance( getIntf() )->rmDir( path );
337 #endif // HAVE_ZLIB_H
340 bool ThemeLoader::parse( const string &path, const string &xmlFile )
343 msg_Dbg( getIntf(), "using skin file: %s", xmlFile.c_str() );
346 SkinParser parser( getIntf(), xmlFile, path );
347 if( ! parser.parse() )
350 // Build and store the theme
351 Builder builder( getIntf(), parser.getData(), path );
352 getIntf()->p_sys->p_theme = builder.build();
358 string ThemeLoader::getFilePath( const string &rFullPath )
360 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
361 const string &sep = pOsFactory->getDirSeparator();
362 // Find the last separator ('/' or '\')
363 string::size_type p = rFullPath.rfind( sep, rFullPath.size() );
365 if( p != string::npos )
367 if( p < rFullPath.size() - 1)
369 basePath = rFullPath.substr( 0, p );
373 basePath = rFullPath;
380 string ThemeLoader::fixDirSeparators( const string &rPath )
382 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
383 const string &sep = pOsFactory->getDirSeparator();
384 string::size_type p = rPath.find( "/", 0 );
385 string newPath = rPath;
386 while( p != string::npos )
388 newPath = newPath.replace( p, 1, sep );
389 p = newPath.find( "/", p + 1 );
395 bool ThemeLoader::findFile( const string &rootDir, const string &rFileName,
396 string &themeFilePath )
399 const string &sep = OSFactory::instance( getIntf() )->getDirSeparator();
405 pCurrDir = vlc_opendir( rootDir.c_str() );
407 if( pCurrDir == NULL )
410 msg_Dbg( getIntf(), "cannot open directory %s", rootDir.c_str() );
414 // While we still have entries in the directory
415 while( ( pszDirContent = vlc_readdir( pCurrDir ) ) != NULL )
417 string newURI = rootDir + sep + pszDirContent;
420 if( string( pszDirContent ) != "." &&
421 string( pszDirContent ) != ".." )
423 #if defined( S_ISDIR )
424 struct stat stat_data;
426 if( ( vlc_stat( newURI.c_str(), &stat_data ) == 0 )
427 && S_ISDIR(stat_data.st_mode) )
428 #elif defined( DT_DIR )
429 if( pDirContent->d_type & DT_DIR )
434 // Can we find the file in this subdirectory?
435 if( findFile( newURI, rFileName, themeFilePath ) )
437 free( pszDirContent );
438 closedir( pCurrDir );
444 // Found the theme file?
445 if( rFileName == string( pszDirContent ) )
447 themeFilePath = newURI;
448 free( pszDirContent );
449 closedir( pCurrDir );
455 free( pszDirContent );
458 closedir( pCurrDir );
463 #if !defined( HAVE_LIBTAR_H ) && defined( HAVE_ZLIB_H )
465 /* Values used in typeflag field */
466 #define REGTYPE '0' /* regular file */
467 #define AREGTYPE '\0' /* regular file */
468 #define DIRTYPE '5' /* directory */
470 #define BLOCKSIZE 512
474 char name[100]; /* 0 */
475 char mode[8]; /* 100 */
476 char uid[8]; /* 108 */
477 char gid[8]; /* 116 */
478 char size[12]; /* 124 */
479 char mtime[12]; /* 136 */
480 char chksum[8]; /* 148 */
481 char typeflag; /* 156 */
482 char linkname[100]; /* 157 */
483 char magic[6]; /* 257 */
484 char version[2]; /* 263 */
485 char uname[32]; /* 265 */
486 char gname[32]; /* 297 */
487 char devmajor[8]; /* 329 */
488 char devminor[8]; /* 337 */
489 char prefix[155]; /* 345 */
495 char buffer[BLOCKSIZE];
496 struct tar_header header;
501 int tar_open( TAR **t, char *pathname, int oflags )
503 gzFile f = gzopen( pathname, "rb" );
506 fprintf( stderr, "Couldn't gzopen %s\n", pathname );
510 *t = (gzFile *)malloc( sizeof(gzFile) );
516 int tar_extract_all( TAR *t, char *prefix )
518 union tar_buffer buffer;
519 int len, err, getheader = 1, remaining = 0;
520 FILE *outfile = NULL;
521 char fname[BLOCKSIZE + PATH_MAX];
525 len = gzread( *t, &buffer, BLOCKSIZE );
528 fprintf( stderr, "%s\n", gzerror(*t, &err) );
532 * Always expect complete blocks to process
533 * the tar information.
535 if( len != 0 && len != BLOCKSIZE )
537 fprintf( stderr, "gzread: incomplete block read\n" );
542 * If we have to get a tar header
547 * If we met the end of the tar
548 * or the end-of-tar block, we are done
550 if( (len == 0) || (buffer.header.name[0] == 0) )
555 sprintf( fname, "%s/%s", prefix, buffer.header.name );
557 /* Check magic value in header */
558 if( strncmp( buffer.header.magic, "GNUtar", 6 ) &&
559 strncmp( buffer.header.magic, "ustar", 5 ) )
561 //fprintf(stderr, "not a tar file\n");
565 switch( buffer.header.typeflag )
572 remaining = getoct( buffer.header.size, 12 );
573 if( !remaining ) outfile = NULL; else
575 outfile = fopen( fname, "wb" );
576 if( outfile == NULL )
578 /* try creating directory */
579 char *p = strrchr( fname, '/' );
585 outfile = fopen( fname, "wb" );
588 fprintf( stderr, "tar couldn't create %s\n",
596 * could have no contents
598 getheader = (remaining) ? 0 : 1;
606 unsigned int bytes = (remaining > BLOCKSIZE)?BLOCKSIZE:remaining;
608 if( outfile != NULL )
610 if( fwrite( &buffer, sizeof(char), bytes, outfile ) != bytes )
612 fprintf( stderr, "error writing %s skipping...\n", fname );
622 if( outfile != NULL )
635 int tar_close( TAR *t )
637 if( gzclose( *t ) != Z_OK ) fprintf( stderr, "failed gzclose\n" );
643 /* helper functions */
644 int getoct( char *p, int width )
656 result = result * 8 + (c - '0');
664 # define mkdir(dirname,mode) _mkdir(dirname)
667 /* Recursive make directory
668 * Abort if you get an ENOENT errno somewhere in the middle
669 * e.g. ignore error "mkdir on existing directory"
671 * return 1 if OK, 0 on error
673 int makedir( const char *newdir )
675 char *p, *buffer = strdup( newdir );
676 int len = strlen( buffer );
684 if( buffer[len-1] == '/' )
686 buffer[len-1] = '\0';
689 if( mkdir( buffer, 0775 ) == 0 )
700 while( *p && *p != '\\' && *p != '/' ) p++;
703 if( ( mkdir( buffer, 0775 ) == -1 ) && ( errno == ENOENT ) )
705 fprintf( stderr, "couldn't create directory %s\n", buffer );
709 if( hold == 0 ) break;
718 static int currentGzFd = -1;
719 static void * currentGzVp = NULL;
721 int gzopen_frontend( const char *pathname, int oflags, int mode )
740 gzf = gzopen( pathname, gzflags );
754 int gzclose_frontend( int fd )
756 if( currentGzVp != NULL && fd != -1 )
758 void *toClose = currentGzVp;
759 currentGzVp = NULL; currentGzFd = -1;
760 return gzclose( toClose );
765 int gzread_frontend( int fd, void *p_buffer, size_t i_length )
767 if( currentGzVp != NULL && fd != -1 )
769 return gzread( currentGzVp, p_buffer, i_length );
774 int gzwrite_frontend( int fd, const void * p_buffer, size_t i_length )
776 if( currentGzVp != NULL && fd != -1 )
778 return gzwrite( currentGzVp, const_cast<void*>(p_buffer), i_length );