Update of /cvsroot/mahogany/M/src/classes
In directory sc8-pr-cvs1:/tmp/cvs-serv13689/src/classes

Modified Files:
        ConfigSource.cpp 
Log Message:
added Copy(); fixed some bugs in creation/destruction of config sources; added support 
for using file config by default under Windows

Index: ConfigSource.cpp
===================================================================
RCS file: /cvsroot/mahogany/M/src/classes/ConfigSource.cpp,v
retrieving revision 1.7
retrieving revision 1.8
diff -b -u -2 -r1.7 -r1.8
--- ConfigSource.cpp    18 Sep 2003 16:30:48 -0000      1.7
+++ ConfigSource.cpp    27 Sep 2003 23:10:35 -0000      1.8
@@ -30,4 +30,6 @@
 
    #include <wx/utils.h>        // for wxGetHomeDir
+
+   #include "Profiles.h"               // for M_PROFILE_CONFIG_SECTION
 #endif // USE_PCH
 
@@ -78,4 +80,15 @@
 extern const MOption MP_CONFIG_SOURCE_TYPE;
 
+#ifdef OS_WIN
+extern const MOption MP_USE_CONFIG_FILE;
+#endif // OS_WIN
+
+// ----------------------------------------------------------------------------
+// globals
+// ----------------------------------------------------------------------------
+
+// the main config source
+static ConfigSource *gs_configSourceGlobal = NULL;
+
 // ============================================================================
 // ConfigSource implementation
@@ -89,5 +102,15 @@
 ConfigSource::CreateDefault(const String& filename)
 {
-   return new ConfigSourceLocal(filename);
+   // FIXME-MT
+   if ( !gs_configSourceGlobal )
+   {
+      gs_configSourceGlobal = ConfigSourceLocal::CreateDefault(filename);
+   }
+   else
+   {
+      gs_configSourceGlobal->IncRef();
+   }
+
+   return gs_configSourceGlobal;
 }
 
@@ -130,4 +153,48 @@
 }
 
+/* static */ bool
+ConfigSource::Copy(ConfigSource& configDst,
+                   const ConfigSource& configSrc,
+                   const String& pathDst,
+                   const String& pathSrc)
+{
+   ConfigSource::EnumData cookie;
+   String name;
+
+   const String pathSrcSlash(pathSrc + _T('/')),
+                pathDstSlash(pathDst + _T('/'));
+
+   // first copy all the entries
+   bool cont = configSrc.GetFirstEntry(pathSrc, name, cookie);
+   while ( cont )
+   {
+      // const_cast is ok because we don't copy to ourselves but to a different
+      // configDst
+      if ( !((ConfigSource&)configSrc).CopyEntry(pathSrcSlash + name,
+                                                 pathDstSlash + name,
+                                                 &configDst) )
+      {
+         return false;
+      }
+
+      cont = configSrc.GetNextEntry(name, cookie);
+   }
+
+   // and then (recursively) copy all subgroups
+   cont = configSrc.GetFirstGroup(pathSrc, name, cookie);
+   while ( cont )
+   {
+      if ( !Copy(configDst, configSrc,
+                 pathSrcSlash + name, pathDstSlash + name) )
+      {
+         return false;
+      }
+
+      cont = configSrc.GetNextGroup(name, cookie);
+   }
+
+   return true;
+}
+
 // ============================================================================
 // ConfigSourceFactory implementation
@@ -247,7 +314,17 @@
 // ----------------------------------------------------------------------------
 
-ConfigSourceLocal::ConfigSourceLocal(const String& filename, const String& name)
-                 : ConfigSource(name), m_config(NULL)
+ConfigSourceLocal::ConfigSourceLocal(wxConfigBase *config, const String& name)
+                 : ConfigSource(name)
 {
+   ASSERT_MSG( config, _T("NULL config in ConfigSourceLocal?") );
+
+   m_config = config;
+}
+
+
+ConfigSourceLocal *ConfigSourceLocal::CreateDefault(const String& filename)
+{
+   wxConfigBase *config = NULL;
+
    String localFilePath, globalFilePath;
 
@@ -412,19 +489,20 @@
 #elif defined(OS_WIN)
    // under Windows we just use the registry if the filename is not specified
-   if ( filename.empty() )
+   localFilePath = filename;
+   if ( localFilePath.empty() )
    {
-      // don't give explicit name, but rather use the default logic (it's
-      // perfectly ok, for the registry case our keys are under vendor\appname)
-      m_config = new wxRegConfig
-                     (
-                        M_APPLICATIONNAME,
-                        M_VENDORNAME,
-                        _T(""),
-                        _T(""),
-                        wxCONFIG_USE_LOCAL_FILE | wxCONFIG_USE_GLOBAL_FILE
-                     );
+      config = CreateRegConfig();
 
-      // skip wxFileConfig creation below
-      return;
+      // one extra complication: the user wants to always want to use config
+      // file instead of the registry, this is indicated by the presence of
+      String key(M_PROFILE_CONFIG_SECTION);
+      key << _T('/') << GetOptionName(MP_USE_CONFIG_FILE);
+      if ( config->Read(key, &localFilePath) )
+      {
+         // we want to use wxFileConfig finally...
+         delete config;
+         config = NULL; // not needed now, but safer if code is changed later
+      }
+      //else: do use wxRegConfig created above
    }
 #else  // !Windows, !Unix
@@ -432,4 +510,20 @@
 #endif // OS
 
+   if ( !config )
+   {
+      config = CreateFileConfig(localFilePath, globalFilePath);
+   }
+
+   return new ConfigSourceLocal(config, _T(""));
+}
+
+/* static */
+wxConfigBase *
+ConfigSourceLocal::CreateFileConfig(const String& localFilePath,
+                                    const String& globalFilePath)
+{
+   ASSERT_MSG( !localFilePath.empty(),
+               _T("invalid file path in CreateFileConfig") );
+
    wxFileConfig *fileconf = new wxFileConfig
                                 (
@@ -447,11 +541,38 @@
    fileconf->SetUmask(0077);
 
-   m_config = fileconf;
+   return fileconf;
+}
+
+
+#ifdef OS_WIN
+
+/* static */
+wxConfigBase *
+ConfigSourceLocal::CreateRegConfig()
+{
+   // don't give explicit name, but rather use the default logic (it's
+   // perfectly ok, for the registry case our keys are under vendor\appname)
+   return new wxRegConfig
+              (
+                  M_APPLICATIONNAME,
+                  M_VENDORNAME,
+                  _T(""),
+                  _T(""),
+                  wxCONFIG_USE_LOCAL_FILE | wxCONFIG_USE_GLOBAL_FILE
+              );
 }
 
+#endif // OS_WIN
+
 ConfigSourceLocal::~ConfigSourceLocal()
 {
-   // don't try to recreate us
+   if ( this == gs_configSourceGlobal )
+   {
+      gs_configSourceGlobal = NULL;
+
+      // we must have been set as global wxConfig object too, don't try to
+      // recreate it any more now
    wxConfig::Set(NULL);
+   }
 
    delete m_config;
@@ -559,6 +680,11 @@
 }
 
-bool ConfigSourceLocal::CopyEntry(const String& nameSrc, const String& nameDst)
+bool
+ConfigSourceLocal::CopyEntry(const String& nameSrc,
+                             const String& nameDst,
+                             ConfigSource *configDstOrig)
 {
+   ConfigSource *configDst = configDstOrig ? configDstOrig : this;
+
    bool rc = true;
 
@@ -582,7 +708,7 @@
                   long l;
                   if ( val.ToLong(&l) )
-                     rc = m_config->Write(nameDst, l);
+                     rc = configDst->Write(nameDst, l);
                   else
-                     rc = m_config->Write(nameDst, val);
+                     rc = configDst->Write(nameDst, val);
                }
             }
@@ -592,5 +718,5 @@
             {
                long l;
-               rc = m_config->Read(nameSrc, &l) && m_config->Write(nameDst, l);
+               rc = m_config->Read(nameSrc, &l) && configDst->Write(nameDst, l);
             }
             break;
@@ -622,5 +748,5 @@
    }
 
-   ConfigSource *configNew = new ConfigSourceLocal(filename, name);
+   ConfigSource *configNew = ConfigSourceLocal::CreateFile(filename, name);
    if ( !configNew->IsOk() )
    {



-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
Mahogany-cvsupdates mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/mahogany-cvsupdates

Reply via email to