Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

theme_repository.cpp

00001 /*****************************************************************************
00002  * theme_repository.cpp
00003  *****************************************************************************
00004  * Copyright (C) 2004 the VideoLAN team
00005  * $Id: theme_repository.cpp 12476 2005-09-04 21:50:18Z ipkiss $
00006  *
00007  * Authors: Cyril Deguet     <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 #include "theme_repository.hpp"
00025 #include "os_factory.hpp"
00026 #include "../commands/async_queue.hpp"
00027 #include "../commands/cmd_dialogs.hpp"
00028 #ifdef HAVE_UNISTD_H
00029 #   include <unistd.h>
00030 #elif defined( WIN32 ) && !defined( UNDER_CE )
00031 #   include <direct.h>
00032 #endif
00033 #ifdef HAVE_DIRENT_H
00034 #   include <dirent.h>
00035 #endif
00036 
00037 
00038 const char *ThemeRepository::kOpenDialog = "{openSkin}";
00039 
00040 
00041 ThemeRepository *ThemeRepository::instance( intf_thread_t *pIntf )
00042 {
00043     if( pIntf->p_sys->p_repository == NULL )
00044     {
00045         pIntf->p_sys->p_repository = new ThemeRepository( pIntf );
00046     }
00047 
00048     return pIntf->p_sys->p_repository;
00049 }
00050 
00051 
00052 void ThemeRepository::destroy( intf_thread_t *pIntf )
00053 {
00054     if( pIntf->p_sys->p_repository )
00055     {
00056         delete pIntf->p_sys->p_repository;
00057         pIntf->p_sys->p_repository = NULL;
00058     }
00059 }
00060 
00061 
00062 ThemeRepository::ThemeRepository( intf_thread_t *pIntf ): SkinObject( pIntf )
00063 {
00064     vlc_value_t val, text;
00065 
00066     // Create a variable to add items in wxwindows popup menu
00067     var_Create( pIntf, "intf-skins", VLC_VAR_STRING |
00068                 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
00069     text.psz_string = _("Select skin");
00070     var_Change( pIntf, "intf-skins", VLC_VAR_SETTEXT, &text, NULL );
00071 
00072     // Scan vlt files in the resource path
00073     OSFactory *pOsFactory = OSFactory::instance( pIntf );
00074     list<string> resPath = pOsFactory->getResourcePath();
00075     list<string>::const_iterator it;
00076     for( it = resPath.begin(); it != resPath.end(); it++ )
00077     {
00078         parseDirectory( *it );
00079     }
00080 
00081     // Add an entry for the "open skin" dialog
00082     val.psz_string = (char*)kOpenDialog;
00083     text.psz_string = _("Open skin...");
00084     var_Change( getIntf(), "intf-skins", VLC_VAR_ADDCHOICE, &val,
00085                 &text );
00086 
00087     // Set the callback
00088     var_AddCallback( pIntf, "intf-skins", changeSkin, this );
00089 }
00090 
00091 
00092 ThemeRepository::~ThemeRepository()
00093 {
00094     var_Destroy( getIntf(), "intf-skins" );
00095 }
00096 
00097 
00098 void ThemeRepository::parseDirectory( const string &rDir )
00099 {
00100     DIR *pDir;
00101     struct dirent *pDirContent;
00102     vlc_value_t val, text;
00103     // Path separator
00104     const string &sep = OSFactory::instance( getIntf() )->getDirSeparator();
00105 
00106     // Open the dir
00107     pDir = opendir( rDir.c_str() );
00108 
00109     if( pDir == NULL )
00110     {
00111         // An error occurred
00112         msg_Dbg( getIntf(), "Cannot open directory %s", rDir.c_str() );
00113         return;
00114     }
00115 
00116     // Get the first directory entry
00117     pDirContent = (dirent*)readdir( pDir );
00118 
00119     // While we still have entries in the directory
00120     while( pDirContent != NULL )
00121     {
00122         string name = pDirContent->d_name;
00123         if( name.size() > 4 && name.substr( name.size() - 4, 4 ) == ".vlt" )
00124         {
00125             string path = rDir + sep + name;
00126             msg_Dbg( getIntf(), "found skin %s", path.c_str() );
00127 
00128             // Add the theme in the popup menu
00129             string shortname = name.substr( 0, name.size() - 4 );
00130             val.psz_string = new char[path.size() + 1];
00131             text.psz_string = new char[shortname.size() + 1];
00132             strcpy( val.psz_string, path.c_str() );
00133             strcpy( text.psz_string, shortname.c_str() );
00134             var_Change( getIntf(), "intf-skins", VLC_VAR_ADDCHOICE, &val,
00135                         &text );
00136             delete[] val.psz_string;
00137             delete[] text.psz_string;
00138         }
00139 
00140         pDirContent = (dirent*)readdir( pDir );
00141     }
00142 
00143     closedir( pDir );
00144 }
00145 
00146 
00147 
00148 int ThemeRepository::changeSkin( vlc_object_t *pIntf, char const *pCmd,
00149                                  vlc_value_t oldval, vlc_value_t newval,
00150                                  void *pData )
00151 {
00152     ThemeRepository *pThis = (ThemeRepository*)(pData);
00153 
00154     // Special menu entry for the open skin dialog
00155     if( !strcmp( newval.psz_string, kOpenDialog ) )
00156     {
00157         CmdDlgChangeSkin cmd( pThis->getIntf() );
00158         cmd.execute();
00159     }
00160     else
00161     {
00162         // Try to load the new skin
00163         CmdChangeSkin *pCmd = new CmdChangeSkin( pThis->getIntf(),
00164                                                  newval.psz_string );
00165         AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
00166         pQueue->remove( "change skin" );
00167         pQueue->push( CmdGenericPtr( pCmd ) );
00168     }
00169 
00170     return VLC_SUCCESS;
00171 }
00172 

Generated on Tue Dec 20 10:14:42 2005 for vlc-0.8.4a by  doxygen 1.4.2