]> git.sesse.net Git - vlc/blobdiff - modules/gui/skins2/src/theme_loader.cpp
Add the compile information
[vlc] / modules / gui / skins2 / src / theme_loader.cpp
old mode 100755 (executable)
new mode 100644 (file)
index f3acd4b..9242c41
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * theme_loader.cpp
  *****************************************************************************
- * Copyright (C) 2003 VideoLAN
+ * Copyright (C) 2003 the VideoLAN team
  * $Id$
  *
  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
 #include "../parser/builder.hpp"
 #include "../parser/skin_parser.hpp"
 #include "../src/os_factory.hpp"
+#include "../src/vlcproc.hpp"
 #include "../src/window_manager.hpp"
 
-#include <fcntl.h>
-#if !defined( WIN32 )
+#ifdef HAVE_FCNTL_H
+#   include <fcntl.h>
+#endif
+#ifdef HAVE_SYS_STAT_H
+#   include <sys/stat.h>
+#endif
+#ifdef HAVE_UNISTD_H
 #   include <unistd.h>
-#else
+#elif defined( WIN32 ) && !defined( UNDER_CE )
 #   include <direct.h>
 #endif
 
+#ifdef HAVE_DIRENT_H
+#   include <dirent.h>
+#endif
+
 
 #if defined( HAVE_ZLIB_H )
 #   include <zlib.h>
 #   include <errno.h>
-int gzopen_frontend( char *pathname, int oflags, int mode );
+int gzopen_frontend ( char *pathname, int oflags, int mode );
+int gzclose_frontend( int );
+int gzread_frontend ( int, void *, size_t );
+int gzwrite_frontend( int, const void *, size_t );
 #if defined( HAVE_LIBTAR_H )
 #   include <libtar.h>
 #else
@@ -87,6 +100,10 @@ bool ThemeLoader::load( const string &fileName )
         // Show the windows
         pNewTheme->getWindowManager().showAll();
     }
+    if( skin_last ) free( skin_last );
+
+    // The new theme cannot embed a video output yet
+    VlcProc::instance( getIntf() )->dropVout();
 
     return true;
 }
@@ -97,8 +114,10 @@ bool ThemeLoader::extractTarGz( const string &tarFile, const string &rootDir )
 {
     TAR *t;
 #if defined( HAVE_LIBTAR_H )
-    tartype_t gztype = { (openfunc_t) gzopen_frontend, (closefunc_t) gzclose,
-        (readfunc_t) gzread, (writefunc_t) gzwrite };
+    tartype_t gztype = { (openfunc_t) gzopen_frontend,
+                         (closefunc_t) gzclose_frontend,
+                         (readfunc_t) gzread_frontend,
+                         (writefunc_t) gzwrite_frontend };
 
     if( tar_open( &t, (char *)tarFile.c_str(), &gztype, O_RDONLY, 0,
                   TAR_GNU ) == -1 )
@@ -134,9 +153,9 @@ bool ThemeLoader::extract( const string &fileName )
     if( ! extractTarGz( fileName, tempPath ) )
         return false;
 
-    // Parse the extracted XML file
-    const string &sep = OSFactory::instance( getIntf() )->getDirSeparator();
-    if( ! parse( tempPath + sep + string( DEFAULT_XML_FILE ) ) )
+    // Find the XML file and parse it
+    string xmlFile;
+    if( ! findThemeFile( tempPath, xmlFile ) || ! parse( xmlFile ) )
     {
         msg_Err( getIntf(), "%s doesn't contain a " DEFAULT_XML_FILE " file",
                  fileName.c_str() );
@@ -162,28 +181,24 @@ bool ThemeLoader::parse( const string &xmlFile )
     // File loaded
     msg_Dbg( getIntf(), "Using skin file: %s", xmlFile.c_str() );
 
-    // Save current working directory
-    char *cwd = new char[PATH_MAX];
-    getcwd( cwd, PATH_MAX );
-
-    // Change current working directory to xml file
+    // Extract the path of the XML file
+    string path;
     const string &sep = OSFactory::instance( getIntf() )->getDirSeparator();
-    unsigned int p = xmlFile.rfind( sep, xmlFile.size() );
+    string::size_type p = xmlFile.rfind( sep, xmlFile.size() );
     if( p != string::npos )
     {
-        string path = xmlFile.substr( 0, p );
-        chdir( path.c_str() );
+        path = xmlFile.substr( 0, p + 1 );
+    }
+    else
+    {
+        path = "";
     }
 
     // Start the parser
-    SkinParser parser( getIntf(), xmlFile );
-    bool ret = parser.parse();
-    if( !ret )
+    SkinParser parser( getIntf(), xmlFile, path );
+    if( ! parser.parse() )
     {
         msg_Err( getIntf(), "Failed to parse %s", xmlFile.c_str() );
-        // Set old working directory to current
-        chdir( cwd );
-        delete[] cwd;
         return false;
     }
 
@@ -191,16 +206,78 @@ bool ThemeLoader::parse( const string &xmlFile )
     Builder builder( getIntf(), parser.getData() );
     getIntf()->p_sys->p_theme = builder.build();
 
-    // Set old working directory to current.
-    // We need to do that _after_ calling Builder:build(), otherwise the
-    // opening of the files will fail
-    chdir( cwd );
-    delete[] cwd;
-
     return true;
 }
 
 
+bool ThemeLoader::findThemeFile( const string &rootDir, string &themeFilePath )
+{
+    // Path separator
+    const string &sep = OSFactory::instance( getIntf() )->getDirSeparator();
+
+    DIR *pCurrDir;
+    struct dirent *pDirContent;
+
+    // Open the dir
+    pCurrDir = opendir( rootDir.c_str() );
+
+    if( pCurrDir == NULL )
+    {
+        // An error occurred
+        msg_Dbg( getIntf(), "Cannot open directory %s", rootDir.c_str() );
+        return false;
+    }
+
+    // Get the first directory entry
+    pDirContent = (dirent*)readdir( pCurrDir );
+
+    // While we still have entries in the directory
+    while( pDirContent != NULL )
+    {
+        string newURI = rootDir + sep + pDirContent->d_name;
+
+        // Skip . and ..
+        if( string( pDirContent->d_name ) != "." &&
+            string( pDirContent->d_name ) != ".." )
+        {
+#if defined( S_ISDIR )
+            struct stat stat_data;
+            stat( newURI.c_str(), &stat_data );
+            if( S_ISDIR(stat_data.st_mode) )
+#elif defined( DT_DIR )
+            if( pDirContent->d_type & DT_DIR )
+#else
+            if( 0 )
+#endif
+            {
+                // Can we find the theme file in this subdirectory?
+                if( findThemeFile( newURI, themeFilePath ) )
+                {
+                    closedir( pCurrDir );
+                    return true;
+                }
+            }
+            else
+            {
+                // Found the theme file?
+                if( string( DEFAULT_XML_FILE ) ==
+                    string( pDirContent->d_name ) )
+                {
+                    themeFilePath = newURI;
+                    closedir( pCurrDir );
+                    return true;
+                }
+            }
+        }
+
+        pDirContent = (dirent*)readdir( pCurrDir );
+    }
+
+    closedir( pCurrDir );
+    return false;
+}
+
+
 #if !defined( HAVE_LIBTAR_H ) && defined( HAVE_ZLIB_H )
 
 #ifdef WIN32
@@ -458,12 +535,16 @@ int makedir( char *newdir )
 #endif
 
 #ifdef HAVE_ZLIB_H
+
+static int currentGzFd = -1;
+static void * currentGzVp = NULL;
+
 int gzopen_frontend( char *pathname, int oflags, int mode )
 {
     char *gzflags;
     gzFile gzf;
 
-    switch( oflags & O_ACCMODE )
+    switch( oflags )
     {
         case O_WRONLY:
             gzflags = "wb";
@@ -484,6 +565,40 @@ int gzopen_frontend( char *pathname, int oflags, int mode )
         return -1;
     }
 
-    return (int)gzf;
+    /** Hum ... */
+    currentGzFd = 42;
+    currentGzVp = gzf;
+
+    return currentGzFd;
+}
+
+int gzclose_frontend( int fd )
+{
+    if( currentGzVp != NULL && fd != -1 )
+    {
+        void *toClose = currentGzVp;
+        currentGzVp = NULL;  currentGzFd = -1;
+        return gzclose( toClose );
+    }
+    return -1;
+}
+
+int gzread_frontend( int fd, void *p_buffer, size_t i_length )
+{
+    if( currentGzVp != NULL && fd != -1 )
+    {
+        return gzread( currentGzVp, p_buffer, i_length );
+    }
+    return -1;
 }
+
+int gzwrite_frontend( int fd, const void * p_buffer, size_t i_length )
+{
+    if( currentGzVp != NULL && fd != -1 )
+    {
+        return gzwrite( currentGzVp, const_cast<void*>(p_buffer), i_length );
+    }
+    return -1;
+}
+
 #endif