예제 #1
0
        public static AppConfigEx Load()
        {
            AppConfigSerializer.GetConfigPaths();

            AppConfigEx cfgEnf = LoadConfigFileEx(m_strEnforcedConfigFile);

            if (cfgEnf != null)
            {
                cfgEnf.Meta.IsEnforcedConfiguration = true;
                return(cfgEnf);
            }

            AppConfigEx cfgGlobal = LoadConfigFileEx(m_strGlobalConfigFile);
            AppConfigEx cfgUser   = LoadConfigFileEx(m_strUserConfigFile);

            if ((cfgGlobal == null) && (cfgUser == null))
            {
                return(new AppConfigEx());
            }
            else if ((cfgGlobal != null) && (cfgUser == null))
            {
                return(cfgGlobal);
            }
            else if ((cfgGlobal == null) && (cfgUser != null))
            {
                return(cfgUser);
            }

            cfgUser.Meta.PreferUserConfiguration = cfgGlobal.Meta.PreferUserConfiguration;
            return(cfgGlobal.Meta.PreferUserConfiguration ? cfgUser : cfgGlobal);
        }
예제 #2
0
        public static bool Save(AppConfigEx tConfig)
        {
            Debug.Assert(tConfig != null);
            if (tConfig == null)
            {
                throw new ArgumentNullException("tConfig");
            }

            AppConfigSerializer.GetConfigPaths();

            XmlDocument xdEnforced = LoadEnforcedConfigFile();
            AppConfigEx cfgGlobal  = LoadConfigFileEx(m_strGlobalConfigFile, xdEnforced);

            bool bPreferUser = (xdEnforced != null);

            if (cfgGlobal != null)
            {
                bPreferUser = cfgGlobal.Meta.PreferUserConfiguration;
            }
            else if (xdEnforced != null)
            {
                GetConfigPref(xdEnforced, ref bPreferUser, null);
            }

            if (bPreferUser)
            {
                EnsureAppDataDirAvailable(m_strUserConfigFile);
                if (SaveConfigFileEx(tConfig, m_strUserConfigFile, true))
                {
                    return(true);
                }

                if (SaveConfigFileEx(tConfig, m_strGlobalConfigFile, false))
                {
                    return(true);
                }
            }
            else             // Don't prefer user -- use global first
            {
                if (SaveConfigFileEx(tConfig, m_strGlobalConfigFile, false))
                {
                    return(true);
                }

                EnsureAppDataDirAvailable(m_strUserConfigFile);
                if (SaveConfigFileEx(tConfig, m_strUserConfigFile, true))
                {
                    return(true);
                }
            }

#if !KeePassLibSD
            if (Program.MainForm != null)
            {
                Program.MainForm.SetStatusEx(KPRes.ConfigSaveFailed);
            }
#endif

            return(false);
        }
예제 #3
0
        public static AppConfigEx Load()
        {
            AppConfigSerializer.GetConfigPaths();

            // AppConfigEx cfgEnf = LoadConfigFileEx(m_strEnforcedConfigFile);
            // if(cfgEnf != null)
            // {
            //	cfgEnf.Meta.IsEnforcedConfiguration = true;
            //	return cfgEnf;
            // }
            XmlDocument xdEnforced = LoadEnforcedConfigFile();

            AppConfigEx cfgGlobal = LoadConfigFileEx(m_strGlobalConfigFile, xdEnforced);
            AppConfigEx cfgUser   = LoadConfigFileEx(m_strUserConfigFile, xdEnforced);

            if ((cfgGlobal == null) && (cfgUser == null))
            {
                if (xdEnforced != null)
                {
                    XmlSerializerEx xmlSerial = new XmlSerializerEx(typeof(AppConfigEx));
                    try
                    {
                        using (MemoryStream msEnf = new MemoryStream())
                        {
                            xdEnforced.Save(msEnf);

                            using (MemoryStream msRead = new MemoryStream(
                                       msEnf.ToArray(), false))
                            {
                                AppConfigEx cfgEnf = (AppConfigEx)xmlSerial.Deserialize(msRead);
                                cfgEnf.OnLoad();

                                return(cfgEnf);
                            }
                        }
                    }
                    catch (Exception) { Debug.Assert(false); }
                }

                AppConfigEx cfgNew = new AppConfigEx();
                cfgNew.OnLoad();                 // Create defaults
                return(cfgNew);
            }
            else if ((cfgGlobal != null) && (cfgUser == null))
            {
                return(cfgGlobal);
            }
            else if ((cfgGlobal == null) && (cfgUser != null))
            {
                return(cfgUser);
            }

            cfgUser.Meta.PreferUserConfiguration = cfgGlobal.Meta.PreferUserConfiguration;
            return(cfgGlobal.Meta.PreferUserConfiguration ? cfgUser : cfgGlobal);
        }
예제 #4
0
        public static AppConfigEx Load()
        {
            AppConfigSerializer.GetConfigPaths();

            // AppConfigEx cfgEnf = LoadConfigFileEx(g_strEnforcedConfigFile);
            // if(cfgEnf != null)
            // {
            //	cfgEnf.Meta.IsEnforcedConfiguration = true;
            //	return cfgEnf;
            // }
            XmlDocument xdEnforced = LoadEnforcedConfigFile();

            AppConfigEx cfgGlobal = LoadConfigFileEx(g_strGlobalConfigFile, xdEnforced);
            AppConfigEx cfgUser   = LoadConfigFileEx(g_strUserConfigFile, xdEnforced);

            if ((cfgGlobal == null) && (cfgUser == null))
            {
                if (xdEnforced != null)
                {
                    // Create an empty configuration file and merge it with
                    // the enforced configuration; this ensures that merge
                    // behaviors (like the node mode 'Remove') work as intended
                    try
                    {
                        string strFile = Program.TempFilesPool.GetTempFileName("xml");
                        File.WriteAllText(strFile, AppConfigEx.GetEmptyConfigXml(),
                                          StrUtil.Utf8);

                        AppConfigEx cfg = LoadConfigFileEx(strFile, xdEnforced);
                        if (cfg != null)
                        {
                            return(cfg);
                        }
                        Debug.Assert(false);
                    }
                    catch (Exception) { Debug.Assert(false); }
                }

                AppConfigEx cfgNew = new AppConfigEx();
                cfgNew.OnLoad();                 // Create defaults
                return(cfgNew);
            }
            else if ((cfgGlobal != null) && (cfgUser == null))
            {
                return(cfgGlobal);
            }
            else if ((cfgGlobal == null) && (cfgUser != null))
            {
                return(cfgUser);
            }

            cfgUser.Meta.PreferUserConfiguration = cfgGlobal.Meta.PreferUserConfiguration;
            return(cfgGlobal.Meta.PreferUserConfiguration ? cfgUser : cfgGlobal);
        }