Write the config and MRU files to ?data in portable mode rather than ?user

Originally committed to SVN as r6187.
This commit is contained in:
Thomas Goyne 2011-12-30 01:21:03 +00:00
parent d01d7eb78b
commit 84d1315736
4 changed files with 33 additions and 10 deletions

View file

@ -184,8 +184,12 @@ void Options::Flush() {
} }
} }
io::Save file(config_file); json::Writer::Write(obj_out, io::Save(config_file).Get());
json::Writer::Write(obj_out, file.Get()); }
void Options::SetConfigPath(std::string const& new_path) {
config_file = new_path;
Flush();
} }
} // namespace agi } // namespace agi

View file

@ -68,7 +68,7 @@ private:
::json::Object CreateObject(std::string path); ::json::Object CreateObject(std::string path);
/// User config (file that will be written to disk) /// User config (file that will be written to disk)
const std::string config_file; std::string config_file;
/// Settings. /// Settings.
const OptionSetting setting; const OptionSetting setting;
@ -106,6 +106,10 @@ public:
/// Write the user configuration to disk, throws an exception if something goes wrong. /// Write the user configuration to disk, throws an exception if something goes wrong.
void Flush(); void Flush();
/// Change where the configuration file should be saved to
/// @param new_path New location of the configuration file
void SetConfigPath(std::string const& new_path);
}; };
} // namespace agi } // namespace agi

View file

@ -81,8 +81,7 @@
#include <libaegisub/access.h> #include <libaegisub/access.h>
#include <libaegisub/log.h> #include <libaegisub/log.h>
#include <libaegisub/hotkey.h> #include <libaegisub/hotkey.h>
#include <libaegisub/scoped_ptr.h>
namespace config { namespace config {
agi::Options *opt; agi::Options *opt;
@ -186,9 +185,6 @@ bool AegisubApp::OnInit() {
// Init icons. // Init icons.
icon::icon_init(); icon::icon_init();
const std::string conf_mru(StandardPaths::DecodePath("?user/mru.json"));
config::mru = new agi::MRUManager(conf_mru, GET_DEFAULT_CONFIG(default_mru));
// Set config file // Set config file
StartupLog("Load configuration"); StartupLog("Load configuration");
try { try {
@ -202,13 +198,13 @@ bool AegisubApp::OnInit() {
// Try loading configuration from the install dir if one exists there // Try loading configuration from the install dir if one exists there
try { try {
const std::string conf_local(StandardPaths::DecodePath("?data/config.json")); const std::string conf_local(StandardPaths::DecodePath("?data/config.json"));
std::ifstream* localConfig = agi::io::Open(conf_local); agi::scoped_ptr<std::istream> localConfig(agi::io::Open(conf_local));
config::opt->ConfigNext(*localConfig); config::opt->ConfigNext(*localConfig);
delete localConfig;
if (OPT_GET("App/Local Config")->GetBool()) { if (OPT_GET("App/Local Config")->GetBool()) {
// Local config, make ?user mean ?data so all user settings are placed in install dir // Local config, make ?user mean ?data so all user settings are placed in install dir
StandardPaths::SetPathValue("?user", StandardPaths::DecodePath("?data")); StandardPaths::SetPathValue("?user", StandardPaths::DecodePath("?data"));
config::opt->SetConfigPath(conf_local);
} }
} catch (agi::acs::AcsError const&) { } catch (agi::acs::AcsError const&) {
// File doesn't exist or we can't read it // File doesn't exist or we can't read it
@ -222,6 +218,9 @@ bool AegisubApp::OnInit() {
wxMessageBox("Configuration file is invalid. Error reported:\n" + lagi_wxString(err.GetMessage()), "Error"); wxMessageBox("Configuration file is invalid. Error reported:\n" + lagi_wxString(err.GetMessage()), "Error");
} }
StartupLog("Load MRU");
const std::string conf_mru(StandardPaths::DecodePath("?user/mru.json"));
config::mru = new agi::MRUManager(conf_mru, GET_DEFAULT_CONFIG(default_mru));
#ifdef __VISUALC__ #ifdef __VISUALC__
SetThreadName((DWORD) -1,"AegiMain"); SetThreadName((DWORD) -1,"AegiMain");

View file

@ -261,3 +261,19 @@ TEST_F(lagi_option, empty_array_decays_to_first_used_type) {
EXPECT_THROW(opt.Get("arr")->GetListInt(), agi::OptionValueErrorInvalidListType); EXPECT_THROW(opt.Get("arr")->GetListInt(), agi::OptionValueErrorInvalidListType);
} }
} }
TEST_F(lagi_option, change_out_path) {
util::remove("data/options/tmp");
util::remove("data/options/tmp2");
agi::Options opt("data/options/tmp", default_opt, agi::Options::FLUSH_SKIP);
ASSERT_NO_THROW(opt.SetConfigPath("data/options/tmp2"));
std::ifstream flushed_opts("data/options/tmp2");
ASSERT_TRUE(flushed_opts.good());
flushed_opts.seekg(0, std::ios::end);
EXPECT_GT(flushed_opts.tellg(), 0);
EXPECT_FALSE(std::ifstream("data/options/tmp").good());
}