]> git.sesse.net Git - vlc/blobdiff - modules/gui/skins2/src/theme_repository.cpp
skins2: fix playlist not displaying cuurent playing item correctly
[vlc] / modules / gui / skins2 / src / theme_repository.cpp
index c63a6261790bf9e7778fe4d26730ffd9205a1d2e..06199e4539b686c1974b820b24bad7122e187642 100644 (file)
@@ -18,7 +18,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 #include "theme_repository.hpp"
@@ -35,8 +35,6 @@
 #endif
 
 
-const char *ThemeRepository::kOpenDialog = "{openSkin}";
-
 
 ThemeRepository *ThemeRepository::instance( intf_thread_t *pIntf )
 {
@@ -51,11 +49,8 @@ ThemeRepository *ThemeRepository::instance( intf_thread_t *pIntf )
 
 void ThemeRepository::destroy( intf_thread_t *pIntf )
 {
-    if( pIntf->p_sys->p_repository )
-    {
-        delete pIntf->p_sys->p_repository;
-        pIntf->p_sys->p_repository = NULL;
-    }
+    delete pIntf->p_sys->p_repository;
+    pIntf->p_sys->p_repository = NULL;
 }
 
 
@@ -78,61 +73,103 @@ ThemeRepository::ThemeRepository( intf_thread_t *pIntf ): SkinObject( pIntf )
         parseDirectory( *it );
     }
 
-    // Add an entry for the "open skin" dialog
-    val.psz_string = (char*)kOpenDialog;
-    text.psz_string = _("Open skin...");
-    var_Change( getIntf(), "intf-skins", VLC_VAR_ADDCHOICE, &val,
-                &text );
+    // retrieve skins from skins directories and locate default skins
+    map<string,string>::const_iterator itmap, itdefault;
+    for( itmap = m_skinsMap.begin(); itmap != m_skinsMap.end(); itmap++ )
+    {
+        string path = itmap->first;
+        string name = itmap->second;
+        val.psz_string = (char*) path.c_str();
+        text.psz_string = (char*) name.c_str();
+        var_Change( getIntf(), "intf-skins", VLC_VAR_ADDCHOICE, &val,
+                    &text );
+
+        if( name == "default" )
+            itdefault = itmap;
+    }
+
+    // retrieve last skins stored or skins requested by user
+    char* psz_current = config_GetPsz( getIntf(), "skins2-last" );
+    string current = string( psz_current ? psz_current : "" );
+
+    // set the default skins if no skins provided
+    if( current.size() == 0 )
+    {
+        current = itdefault->first;
+        config_PutPsz( getIntf(), "skins2-last", current.c_str() );
+    }
+
+    free( psz_current );
+
+    // Update repository
+    updateRepository();
 
     // Set the callback
     var_AddCallback( pIntf, "intf-skins", changeSkin, this );
+
+    // variable for opening a dialog box to change skins
+    var_Create( pIntf, "intf-skins-interactive", VLC_VAR_VOID |
+                VLC_VAR_ISCOMMAND );
+    text.psz_string = _("Open skin ...");
+    var_Change( pIntf, "intf-skins-interactive", VLC_VAR_SETTEXT, &text, NULL );
+
+    // Set the callback
+    var_AddCallback( pIntf, "intf-skins-interactive", changeSkin, this );
+
 }
 
 
 ThemeRepository::~ThemeRepository()
 {
+    m_skinsMap.clear();
+
+    var_DelCallback( getIntf(), "intf-skins", changeSkin, this );
+    var_DelCallback( getIntf(), "intf-skins-interactive", changeSkin, this );
+
     var_Destroy( getIntf(), "intf-skins" );
+    var_Destroy( getIntf(), "intf-skins-interactive" );
 }
 
 
-void ThemeRepository::parseDirectory( const string &rDir )
+void ThemeRepository::parseDirectory( const string &rDir_locale )
 {
     DIR *pDir;
-    struct dirent *pDirContent;
+    char *pszDirContent;
     vlc_value_t val, text;
     // Path separator
     const string &sep = OSFactory::instance( getIntf() )->getDirSeparator();
 
     // Open the dir
-    pDir = opendir( rDir.c_str() );
+    // FIXME: parseDirectory should be invoked with UTF-8 input instead!!
+    string rDir = sFromLocale( rDir_locale );
+    pDir = vlc_opendir( rDir.c_str() );
 
     if( pDir == NULL )
     {
         // An error occurred
-        msg_Dbg( getIntf(), "Cannot open directory %s", rDir.c_str() );
+        msg_Dbg( getIntf(), "cannot open directory %s", rDir.c_str() );
         return;
     }
 
-    // Get the first directory entry
-    pDirContent = readdir( pDir );
-
     // While we still have entries in the directory
-    while( pDirContent != NULL )
+    while( ( pszDirContent = vlc_readdir( pDir ) ) != NULL )
     {
-        string name = pDirContent->d_name;
-        if( name.size() > 4 && name.substr( name.size() - 4, 4 ) == ".vlt" )
+        string name = pszDirContent;
+        string extension;
+        if( name.size() > 4 )
+        {
+            extension = name.substr( name.size() - 4, 4 );
+        }
+        if( extension == ".vlt" || extension == ".wsz" )
         {
             string path = rDir + sep + name;
-            msg_Dbg( getIntf(), "found skin %s", path.c_str() );
+            string shortname = name.substr( 0, name.size() - 4 );
+            m_skinsMap[path] = shortname;
 
-            // Add the theme in the popup menu
-            val.psz_string = (char*)path.c_str();
-            text.psz_string = (char*)name.substr(0, name.size() - 4).c_str();
-            var_Change( getIntf(), "intf-skins", VLC_VAR_ADDCHOICE, &val,
-                        &text );
+            msg_Dbg( getIntf(), "found skin %s", path.c_str() );
         }
 
-        pDirContent = readdir( pDir );
+        free( pszDirContent );
     }
 
     closedir( pDir );
@@ -140,28 +177,53 @@ void ThemeRepository::parseDirectory( const string &rDir )
 
 
 
-int ThemeRepository::changeSkin( vlc_object_t *pIntf, char const *pCmd,
+int ThemeRepository::changeSkin( vlc_object_t *pIntf, char const *pVariable,
                                  vlc_value_t oldval, vlc_value_t newval,
                                  void *pData )
 {
     ThemeRepository *pThis = (ThemeRepository*)(pData);
 
-    // Special menu entry for the open skin dialog
-    if( !strcmp( newval.psz_string, kOpenDialog ) )
+    if( !strcmp( pVariable, "intf-skins-interactive" ) )
     {
         CmdDlgChangeSkin cmd( pThis->getIntf() );
         cmd.execute();
     }
-    else
+    else if( !strcmp( pVariable, "intf-skins" ) )
     {
         // Try to load the new skin
         CmdChangeSkin *pCmd = new CmdChangeSkin( pThis->getIntf(),
                                                  newval.psz_string );
         AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
-        pQueue->remove( "change skin" );
         pQueue->push( CmdGenericPtr( pCmd ) );
     }
 
     return VLC_SUCCESS;
 }
 
+
+void ThemeRepository::updateRepository()
+{
+    vlc_value_t val, text;
+
+    // retrieve the current skin
+    char* psz_current = config_GetPsz( getIntf(), "skins2-last" );
+    if( !psz_current )
+        return;
+
+    val.psz_string = psz_current;
+    text.psz_string = psz_current;
+
+    // add this new skins if not yet present in repository
+    string current( psz_current );
+    if( m_skinsMap.find( current ) == m_skinsMap.end() )
+    {
+        var_Change( getIntf(), "intf-skins", VLC_VAR_ADDCHOICE, &val,
+                    &text );
+    }
+
+    // mark this current skins as 'checked' in list
+    var_Change( getIntf(), "intf-skins", VLC_VAR_SETVALUE, &val, NULL );
+
+    free( psz_current );
+}
+