// static bool OTPaths::Set(OTSettings& config, const String& strSection, const String& strKey, const String& strValue, const bool& bIsRelative, bool& out_bIsNewOrUpdated, const String& strComment) { if (!strSection.Exists()) { otErr << __FUNCTION__ << ": Null: " << "strSection" << " passed in!\n"; OT_FAIL; } if (!strKey.Exists()) { otErr << __FUNCTION__ << ": Null: " << "strKey" << " passed in!\n"; OT_FAIL; } out_bIsNewOrUpdated = false; const bool bPreLoaded(config.IsLoaded()); if (!bPreLoaded) // we only need to load, if not already loaded. { config.Reset(); if (!config.Load()) { OT_FAIL; } } bool bBoolIsNew(false); String strRelativeKey(""); strRelativeKey.Format("%s%s", strKey.Get(), OT_CONFIG_ISRELATIVE); if (config.Set_bool(strSection, strRelativeKey, bIsRelative, bBoolIsNew, strComment)) { bool bStringIsNew = false; if (config.Set_str(strSection, strKey, strValue, bStringIsNew)) { if (bBoolIsNew && bStringIsNew) // using existing key { out_bIsNewOrUpdated = true; } if (!bPreLoaded) { if (!config.Save()) { OT_FAIL; } config.Reset(); } return true; } } // if we get here, there has been a error! OT_FAIL; }
bool OTFolders::GetSetAll() { OTSettings config = OTSettings(OTPaths::GlobalConfigFile()); config.Reset(); if(!config.Load()) return false; if(!GetSetFolderName(config,KEY_ACCOUNT, DEFAULT_ACCOUNT, s_strAccount )) return false; if(!GetSetFolderName(config,KEY_CERT, DEFAULT_CERT, s_strCert )) return false; if(!GetSetFolderName(config,KEY_CONTRACT, DEFAULT_CONTRACT, s_strContract )) return false; if(!GetSetFolderName(config,KEY_CREDENTIAL, DEFAULT_CREDENTIAL, s_strCredential )) return false; if(!GetSetFolderName(config,KEY_CRON, DEFAULT_CRON, s_strCron )) return false; if(!GetSetFolderName(config,KEY_INBOX, DEFAULT_INBOX, s_strInbox )) return false; if(!GetSetFolderName(config,KEY_MARKET, DEFAULT_MARKET, s_strMarket )) return false; if(!GetSetFolderName(config,KEY_MINT, DEFAULT_MINT, s_strMint )) return false; if(!GetSetFolderName(config,KEY_NYM, DEFAULT_NYM, s_strNym )) return false; if(!GetSetFolderName(config,KEY_NYMBOX, DEFAULT_NYMBOX, s_strNymbox )) return false; if(!GetSetFolderName(config,KEY_OUTBOX, DEFAULT_OUTBOX, s_strOutbox )) return false; if(!GetSetFolderName(config,KEY_PAYMENTINBOX, DEFAULT_PAYMENTINBOX, s_strPaymentInbox )) return false; if(!GetSetFolderName(config,KEY_PUBCRED, DEFAULT_PUBCRED, s_strPubcred )) return false; if(!GetSetFolderName(config,KEY_PUBKEY, DEFAULT_PUBKEY, s_strPubkey )) return false; if(!GetSetFolderName(config,KEY_PURSE, DEFAULT_PURSE, s_strPurse )) return false; if(!GetSetFolderName(config,KEY_RECEIPT, DEFAULT_RECEIPT, s_strReceipt )) return false; if(!GetSetFolderName(config,KEY_RECORDBOX, DEFAULT_RECORDBOX, s_strRecordBox )) return false; if(!GetSetFolderName(config,KEY_EXPIREDBOX, DEFAULT_EXPIREDBOX, s_strExpiredBox )) return false; if(!GetSetFolderName(config,KEY_SCRIPT, DEFAULT_SCRIPT, s_strScript )) return false; if(!GetSetFolderName(config,KEY_SMARTCONTRACTS, DEFAULT_SMARTCONTRACTS, s_strSmartContracts )) return false; if(!GetSetFolderName(config,KEY_SPENT, DEFAULT_SPENT, s_strSpent )) return false; if(!GetSetFolderName(config,KEY_USERACCT, DEFAULT_USERACCT, s_strUserAcct )) return false; if(!config.Save()) return false; config.Reset(); return true; }
// static bool OTPaths::Get(OTSettings& config, const String& strSection, const String& strKey, String& out_strVar, bool& out_bIsRelative, bool& out_bKeyExist) { if (!strSection.Exists()) { otErr << __FUNCTION__ << ": Null: " << "strSection" << " passed in!\n"; OT_FAIL; } if (!strKey.Exists()) { otErr << __FUNCTION__ << ": Null: " << "strKey" << " passed in!\n"; OT_FAIL; } out_strVar = ""; out_bIsRelative = false; out_bKeyExist = false; const bool bPreLoaded(config.IsLoaded()); if (!bPreLoaded) { config.Reset(); if (!config.Load()) { OT_FAIL; } } bool bBoolExists(false), bIsRelative(false); String strRelativeKey(""), strOutFolder(""); strRelativeKey.Format("%s%s", strKey.Get(), OT_CONFIG_ISRELATIVE); if (config.Check_bool(strSection, strRelativeKey, bIsRelative, bBoolExists)) { bool bStringExists = false; if (config.Check_str(strSection, strKey, strOutFolder, bStringExists)) { if (bBoolExists && bStringExists) { if (!bIsRelative) // lets fix the path, so it dosn't matter how // people write it in the config. { if (!ToReal(strOutFolder, strOutFolder)) { OT_FAIL; } if (!FixPath(strOutFolder, strOutFolder, true)) { OT_FAIL; } } out_strVar = strOutFolder; out_bIsRelative = bIsRelative; out_bKeyExist = true; } else { out_strVar = ""; out_bIsRelative = false; out_bKeyExist = false; } if (!bPreLoaded) { config.Reset(); } return true; } } // if we get here, there has been a error! OT_FAIL; }
// static bool OTPaths::LoadSetScriptsFolder // ie. PrefixFolder() + [ if (NOT Android) // "lib/opentxs/" ] (OTSettings& config, // optional const String& strScriptsFolder, // optional const bool& bIsRelative // optional ) { if (&config == &s_settings) ConfigureDefaultSettings(); const bool bPreLoaded(config.IsLoaded()); if (!bPreLoaded) { config.Reset(); if (!config.Load()) { OT_FAIL; } } String strRelativeKey = ""; strRelativeKey.Format("%s%s", "scripts", OT_CONFIG_ISRELATIVE); // local vairables. bool bConfigIsRelative = false; String strConfigFolder = ""; // lets first check what we have in the configuration: { bool bKeyIsNew = false; if (!config.CheckSet_bool("paths", strRelativeKey, true, bConfigIsRelative, bKeyIsNew)) { return false; } if (!config.CheckSet_str("paths", "scripts", OT_SCRIPTS_DIR, strConfigFolder, bKeyIsNew)) { return false; } } // lets first test if there was a folder passed in if ((strScriptsFolder.Exists()) && (3 < strScriptsFolder.GetLength())) { // we have a folder passed in, lets now check if we need to update // anything: if (bConfigIsRelative != bIsRelative) { bConfigIsRelative = bIsRelative; bool bNewOrUpdated = false; if (!config.Set_bool("paths", strRelativeKey, bConfigIsRelative, bNewOrUpdated)) { return false; } } if (!strConfigFolder.Compare(strScriptsFolder)) { strConfigFolder = strScriptsFolder; // update folder bool bNewOrUpdated = false; if (!config.Set_str("paths", "scripts", strConfigFolder, bNewOrUpdated)) { return false; } } } if (bConfigIsRelative) { if (!FixPath(strConfigFolder, strConfigFolder, true)) { OT_FAIL; } String strPrefixScriptPath = ""; AppendFolder(strPrefixScriptPath, PrefixFolder(), strConfigFolder); String strAppBinaryScriptPath = ""; // if the AppBinaryFolder is set, we will attempt to use this script // path instead. // however if the directory dosn't exist, we will default back to // appending to the prefix. // TODO: Make the prefix path set to AppBinaryFolder. (da2ce7) if (AppBinaryFolder().Exists()) { AppendFolder(strAppBinaryScriptPath, AppBinaryFolder(), strConfigFolder); if (!OTPaths::FolderExists(strAppBinaryScriptPath)) { otOut << __FUNCTION__ << ": Warning: Cannot Find: " << strAppBinaryScriptPath << ", using default!"; strAppBinaryScriptPath = ""; // don't have anything here. } } s_strScriptsFolder = strAppBinaryScriptPath.Exists() ? strAppBinaryScriptPath : strPrefixScriptPath; if (!s_strScriptsFolder.Exists()) OT_FAIL; } else { if (!ToReal(strConfigFolder, strConfigFolder)) { OT_FAIL; } if (!FixPath(strConfigFolder, strConfigFolder, true)) { OT_FAIL; } s_strScriptsFolder = strConfigFolder; // set } if (!bPreLoaded) { if (!config.Save()) { OT_FAIL; } config.Reset(); } return true; // success }
// The LoadSet Functions will update the static values. // static bool OTPaths::LoadSetPrefixFolder // eg. /usr/local/ (OTSettings& config, // optional const String& strPrefixFolder // optional // const bool& bIsRelative (cannot be relative); ) { /* The prefix path is special. This path is tested if it is different to the one that would be automatically selected by this program (aka either compiled into, or from the registry, or the default user data directory). If the set path is different to what would be supplied and the override path value is set. Then we will use that path. Otherwise, we will update the path in the configuration to link against the updated path. Users will need to set the override path flag in the configuration, if they want to manually set the prefix path. */ if (&config == &s_settings) ConfigureDefaultSettings(); const bool bPreLoaded(config.IsLoaded()); if (!bPreLoaded) { config.Reset(); if (!config.Load()) { OT_FAIL; } } { // get default path String strDefaultPrefixPath(OT_PREFIX_PATH); { if (!strDefaultPrefixPath.Exists()) { otErr << __FUNCTION__ << ": Error: OT_PREFIX_PATH is not set!"; OT_FAIL; } #ifdef _WIN32 String strTemp; if (OTPaths::Win_GetInstallFolderFromRegistry(strTemp)) { strDefaultPrefixPath = strTemp; } #endif if (!ToReal(strDefaultPrefixPath, strDefaultPrefixPath)) { OT_FAIL; } if (!FixPath(strDefaultPrefixPath, strDefaultPrefixPath, true)) { OT_FAIL; } } String strLocalPrefixPath = ""; bool bPrefixPathOverride = false; { // now check the configuration to see what values we have: String strConfigPath = ""; bool bIsNew = false; String strPrefixPathOverride("prefix_path_override"); if (!config.CheckSet_str("paths", "prefix_path", strDefaultPrefixPath, strConfigPath, bIsNew)) { return false; } if (!config.CheckSet_bool( "paths", strPrefixPathOverride, false, bPrefixPathOverride, bIsNew, "; This will force the prefix not to change")) { return false; } // if the config dosn't have a prefix path set. Lets set the // default. // if a prefix path was passed in, we will override with that later. if (!strConfigPath.Exists() || (3 > strConfigPath.GetLength())) { otErr << __FUNCTION__ << ": Error: Bad " << "prefix_path" << " in config, will reset!"; strConfigPath = strDefaultPrefixPath; // set bPrefixPathOverride = false; // lets set the default path, and reset override bool bNewOrUpdate = false; if (!config.Set_str("paths", "prefix_path", strDefaultPrefixPath, bNewOrUpdate)) { return false; } if (!config.Set_bool("paths", strPrefixPathOverride, false, bNewOrUpdate)) { return false; } } strLocalPrefixPath = strConfigPath; } { if (!bPrefixPathOverride) { bool bUpdate = false; // default if (!strLocalPrefixPath.Compare(strDefaultPrefixPath)) { strLocalPrefixPath = strDefaultPrefixPath; bUpdate = true; } // passed in if (strPrefixFolder.Exists() && (3 < strPrefixFolder.GetLength())) { // a prefix folder was passed in... lets use it, and update // the config if the override isn't set String strTmp = strPrefixFolder; if (!ToReal(strTmp, strTmp)) { OT_FAIL; } if (!FixPath(strTmp, strTmp, true)) { OT_FAIL; } if (!strLocalPrefixPath.Compare(strTmp)) { strLocalPrefixPath = strTmp; bUpdate = true; } } // we need to update the path in the config if (bUpdate) { bool bNewOrUpdate = false; if (!config.Set_str("paths", "prefix_path", strLocalPrefixPath, bNewOrUpdate)) { return false; } } } } { if (!strLocalPrefixPath.Exists()) { OT_FAIL; } if (!ToReal(strLocalPrefixPath, strLocalPrefixPath)) { OT_FAIL; } if (!FixPath(strLocalPrefixPath, strLocalPrefixPath, true)) { OT_FAIL; } s_strPrefixFolder = strLocalPrefixPath; } } if (!bPreLoaded) { if (!config.Save()) { OT_FAIL; } config.Reset(); } return true; }