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., 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 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 // First, we try to un-targz the file, and if it fails we hope it's a XML
78 string path = getFilePath( fileName );
79 #if defined( HAVE_ZLIB_H )
80 if( ! extract( fileName ) && ! parse( path, fileName ) )
83 if( ! parse( path, fileName ) )
87 Theme *pNewTheme = getIntf()->p_sys->p_theme;
93 // Check if the skin to load is in the config file, to load its config
94 char *skin_last = config_GetPsz( getIntf(), "skins2-last" );
95 if( skin_last != NULL && fileName == (string)skin_last )
97 // Restore the theme configuration
98 getIntf()->p_sys->p_theme->loadConfig();
99 // Used to anchor the windows at the beginning
100 pNewTheme->getWindowManager().stopMove();
104 config_PutPsz( getIntf(), "skins2-last", fileName.c_str() );
106 pNewTheme->getWindowManager().showAll( true );
108 if( skin_last ) free( skin_last );
110 // The new theme cannot embed a video output yet
111 VlcProc::instance( getIntf() )->dropVout();
117 #if defined( HAVE_ZLIB_H )
118 bool ThemeLoader::extractTarGz( const string &tarFile, const string &rootDir )
121 #if defined( HAVE_LIBTAR_H )
122 tartype_t gztype = { (openfunc_t) gzopen_frontend,
123 (closefunc_t) gzclose_frontend,
124 (readfunc_t) gzread_frontend,
125 (writefunc_t) gzwrite_frontend };
127 if( tar_open( &t, (char *)tarFile.c_str(), &gztype, O_RDONLY, 0,
130 if( tar_open( &t, (char *)tarFile.c_str(), O_RDONLY ) == -1 )
136 if( tar_extract_all( t, (char *)rootDir.c_str() ) != 0 )
142 if( tar_close( t ) != 0 )
151 bool ThemeLoader::extractZip( const string &zipFile, const string &rootDir )
153 // Try to open the ZIP file
154 unzFile file = unzOpen( zipFile.c_str() );
155 unz_global_info info;
157 if( unzGetGlobalInfo( file, &info ) != UNZ_OK )
161 // Extract all the files in the archive
162 for( unsigned long i = 0; i < info.number_entry; i++ )
164 if( !extractFileInZip( file, rootDir ) )
166 msg_Warn( getIntf(), "Error while unzipping %s",
171 if( i < info.number_entry - 1 )
173 // Go the next file in the archive
174 if( unzGoToNextFile( file ) !=UNZ_OK )
176 msg_Warn( getIntf(), "Error while unzipping %s",
186 bool ThemeLoader::extractFileInZip( unzFile file, const string &rootDir )
188 // Read info for the current file
189 char filenameInZip[256];
190 unz_file_info fileInfo;
191 if( unzGetCurrentFileInfo( file, &fileInfo, filenameInZip,
192 sizeof( filenameInZip), NULL, 0, NULL, 0 )
198 // Convert the file name to lower case, because some winamp skins
199 // use the wrong case...
200 for( int i=0; i< strlen( filenameInZip ); i++)
202 filenameInZip[i] = tolower( filenameInZip[i] );
205 // Allocate the buffer
206 void *pBuffer = malloc( ZIP_BUFFER_SIZE );
209 msg_Err( getIntf(), "Failed to allocate memory" );
213 // Get the path of the file
214 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
215 string fullPath = rootDir
216 + pOsFactory->getDirSeparator()
217 + fixDirSeparators( filenameInZip );
218 string basePath = getFilePath( fullPath );
220 // Extract the file if is not a directory
221 if( basePath != fullPath )
223 if( unzOpenCurrentFile( file ) )
228 makedir( basePath.c_str() );
229 FILE *fout = fopen( fullPath.c_str(), "wb" );
232 msg_Err( getIntf(), "Error opening %s", fullPath.c_str() );
237 // Extract the current file
241 n = unzReadCurrentFile( file, pBuffer, ZIP_BUFFER_SIZE );
244 msg_Err( getIntf(), "Error while reading zip file" );
250 if( fwrite( pBuffer, n , 1, fout) != 1 )
252 msg_Err( getIntf(), "Error while writing %s",
262 if( unzCloseCurrentFile( file ) != UNZ_OK )
274 bool ThemeLoader::extract( const string &fileName )
277 char *tmpdir = tempnam( NULL, "vlt" );
278 string tempPath = tmpdir;
281 // Extract the file in a temporary directory
282 if( ! extractTarGz( fileName, tempPath ) &&
283 ! extractZip( fileName, tempPath ) )
288 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
289 // Find the XML file in the theme
290 if( findFile( tempPath, DEFAULT_XML_FILE, xmlFile ) )
292 path = getFilePath( xmlFile );
296 // No XML file, check if it is a winamp2 skin
298 if( findFile( tempPath, "main.bmp", mainBmp ) )
300 msg_Dbg( getIntf(), "Try to load a winamp2 skin" );
301 path = getFilePath( mainBmp );
303 // Look for winamp2.xml in the resource path
304 list<string> resPath = pOsFactory->getResourcePath();
305 list<string>::const_iterator it;
306 for( it = resPath.begin(); it != resPath.end(); it++ )
308 if( findFile( *it, WINAMP2_XML_FILE, xmlFile ) )
314 if( !xmlFile.empty() )
316 // Parse the XML file
317 if (! parse( path, xmlFile ) )
319 msg_Err( getIntf(), "Error while parsing %s", xmlFile.c_str() );
325 msg_Err( getIntf(), "No XML found in theme %s", fileName.c_str() );
330 deleteTempFiles( tempPath );
335 void ThemeLoader::deleteTempFiles( const string &path )
337 OSFactory::instance( getIntf() )->rmDir( path );
339 #endif // HAVE_ZLIB_H
342 bool ThemeLoader::parse( const string &path, const string &xmlFile )
345 msg_Dbg( getIntf(), "Using skin file: %s", xmlFile.c_str() );
348 SkinParser parser( getIntf(), xmlFile, path );
349 if( ! parser.parse() )
351 msg_Err( getIntf(), "Failed to parse %s", xmlFile.c_str() );
355 // Build and store the theme
356 Builder builder( getIntf(), parser.getData() );
357 getIntf()->p_sys->p_theme = builder.build();
363 string ThemeLoader::getFilePath( const string &rFullPath )
365 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
366 const string &sep = pOsFactory->getDirSeparator();
367 // Find the last separator ('/' or '\')
368 string::size_type p = rFullPath.rfind( sep, rFullPath.size() );
370 if( p != string::npos )
372 if( p < rFullPath.size() - 1)
374 basePath = rFullPath.substr( 0, p );
378 basePath = rFullPath;
385 string ThemeLoader::fixDirSeparators( const string &rPath )
387 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
388 const string &sep = pOsFactory->getDirSeparator();
389 string::size_type p = rPath.find( "/", 0 );
390 string newPath = rPath;
391 while( p != string::npos )
393 newPath = newPath.replace( p, 1, sep );
394 p = newPath.find( "/", p + 1 );
400 bool ThemeLoader::findFile( const string &rootDir, const string &rFileName,
401 string &themeFilePath )
404 const string &sep = OSFactory::instance( getIntf() )->getDirSeparator();
407 struct dirent *pDirContent;
410 pCurrDir = opendir( rootDir.c_str() );
412 if( pCurrDir == NULL )
415 msg_Dbg( getIntf(), "Cannot open directory %s", rootDir.c_str() );
419 // Get the first directory entry
420 pDirContent = (dirent*)readdir( pCurrDir );
422 // While we still have entries in the directory
423 while( pDirContent != NULL )
425 string newURI = rootDir + sep + pDirContent->d_name;
428 if( string( pDirContent->d_name ) != "." &&
429 string( pDirContent->d_name ) != ".." )
431 #if defined( S_ISDIR )
432 struct stat stat_data;
433 stat( newURI.c_str(), &stat_data );
434 if( S_ISDIR(stat_data.st_mode) )
435 #elif defined( DT_DIR )
436 if( pDirContent->d_type & DT_DIR )
441 // Can we find the file in this subdirectory?
442 if( findFile( newURI, rFileName, themeFilePath ) )
444 closedir( pCurrDir );
450 // Found the theme file?
451 if( rFileName == string( pDirContent->d_name ) )
453 themeFilePath = newURI;
454 closedir( pCurrDir );
460 pDirContent = (dirent*)readdir( pCurrDir );
463 closedir( pCurrDir );
468 #if !defined( HAVE_LIBTAR_H ) && defined( HAVE_ZLIB_H )
470 /* Values used in typeflag field */
471 #define REGTYPE '0' /* regular file */
472 #define AREGTYPE '\0' /* regular file */
473 #define DIRTYPE '5' /* directory */
475 #define BLOCKSIZE 512
479 char name[100]; /* 0 */
480 char mode[8]; /* 100 */
481 char uid[8]; /* 108 */
482 char gid[8]; /* 116 */
483 char size[12]; /* 124 */
484 char mtime[12]; /* 136 */
485 char chksum[8]; /* 148 */
486 char typeflag; /* 156 */
487 char linkname[100]; /* 157 */
488 char magic[6]; /* 257 */
489 char version[2]; /* 263 */
490 char uname[32]; /* 265 */
491 char gname[32]; /* 297 */
492 char devmajor[8]; /* 329 */
493 char devminor[8]; /* 337 */
494 char prefix[155]; /* 345 */
500 char buffer[BLOCKSIZE];
501 struct tar_header header;
506 int tar_open( TAR **t, char *pathname, int oflags )
508 gzFile f = gzopen( pathname, "rb" );
511 fprintf( stderr, "Couldn't gzopen %s\n", pathname );
515 *t = (gzFile *)malloc( sizeof(gzFile) );
521 int tar_extract_all( TAR *t, char *prefix )
523 union tar_buffer buffer;
524 int len, err, getheader = 1, remaining = 0;
525 FILE *outfile = NULL;
526 char fname[BLOCKSIZE + PATH_MAX];
530 len = gzread( *t, &buffer, BLOCKSIZE );
533 fprintf( stderr, "%s\n", gzerror(*t, &err) );
537 * Always expect complete blocks to process
538 * the tar information.
540 if( len != 0 && len != BLOCKSIZE )
542 fprintf( stderr, "gzread: incomplete block read\n" );
547 * If we have to get a tar header
552 * If we met the end of the tar
553 * or the end-of-tar block, we are done
555 if( (len == 0) || (buffer.header.name[0] == 0) )
560 sprintf( fname, "%s/%s", prefix, buffer.header.name );
562 /* Check magic value in header */
563 if( strncmp( buffer.header.magic, "GNUtar", 6 ) &&
564 strncmp( buffer.header.magic, "ustar", 5 ) )
566 //fprintf(stderr, "not a tar file\n");
570 switch( buffer.header.typeflag )
577 remaining = getoct( buffer.header.size, 12 );
580 outfile = fopen( fname, "wb" );
581 if( outfile == NULL )
583 /* try creating directory */
584 char *p = strrchr( fname, '/' );
590 outfile = fopen( fname, "wb" );
593 fprintf( stderr, "tar couldn't create %s\n",
602 * could have no contents
604 getheader = (remaining) ? 0 : 1;
612 unsigned int bytes = (remaining > BLOCKSIZE)?BLOCKSIZE:remaining;
614 if( outfile != NULL )
616 if( fwrite( &buffer, sizeof(char), bytes, outfile ) != bytes )
618 fprintf( stderr, "error writing %s skipping...\n", fname );
627 if( outfile != NULL )
640 int tar_close( TAR *t )
642 if( gzclose( *t ) != Z_OK ) fprintf( stderr, "failed gzclose\n" );
648 /* helper functions */
649 int getoct( char *p, int width )
661 result = result * 8 + (c - '0');
669 # define mkdir(dirname,mode) _mkdir(dirname)
672 /* Recursive make directory
673 * Abort if you get an ENOENT errno somewhere in the middle
674 * e.g. ignore error "mkdir on existing directory"
676 * return 1 if OK, 0 on error
678 int makedir( const char *newdir )
680 char *p, *buffer = strdup( newdir );
681 int len = strlen( buffer );
689 if( buffer[len-1] == '/' )
691 buffer[len-1] = '\0';
694 if( mkdir( buffer, 0775 ) == 0 )
705 while( *p && *p != '\\' && *p != '/' ) p++;
708 if( ( mkdir( buffer, 0775 ) == -1 ) && ( errno == ENOENT ) )
710 fprintf( stderr, "couldn't create directory %s\n", buffer );
714 if( hold == 0 ) break;
723 static int currentGzFd = -1;
724 static void * currentGzVp = NULL;
726 int gzopen_frontend( char *pathname, int oflags, int mode )
745 gzf = gzopen( pathname, gzflags );
759 int gzclose_frontend( int fd )
761 if( currentGzVp != NULL && fd != -1 )
763 void *toClose = currentGzVp;
764 currentGzVp = NULL; currentGzFd = -1;
765 return gzclose( toClose );
770 int gzread_frontend( int fd, void *p_buffer, size_t i_length )
772 if( currentGzVp != NULL && fd != -1 )
774 return gzread( currentGzVp, p_buffer, i_length );
779 int gzwrite_frontend( int fd, const void * p_buffer, size_t i_length )
781 if( currentGzVp != NULL && fd != -1 )
783 return gzwrite( currentGzVp, const_cast<void*>(p_buffer), i_length );