From 84d13157360a09a1011826c45fef4ebd3b756f00 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Fri, 30 Dec 2011 01:21:03 +0000 Subject: [PATCH] Write the config and MRU files to ?data in portable mode rather than ?user Originally committed to SVN as r6187. --- aegisub/libaegisub/common/option.cpp | 8 ++++++-- aegisub/libaegisub/include/libaegisub/option.h | 6 +++++- aegisub/src/main.cpp | 13 ++++++------- aegisub/tests/libaegisub_option.cpp | 16 ++++++++++++++++ 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/aegisub/libaegisub/common/option.cpp b/aegisub/libaegisub/common/option.cpp index 4e9c76151..5371ffa02 100644 --- a/aegisub/libaegisub/common/option.cpp +++ b/aegisub/libaegisub/common/option.cpp @@ -184,8 +184,12 @@ void Options::Flush() { } } - io::Save file(config_file); - json::Writer::Write(obj_out, file.Get()); + json::Writer::Write(obj_out, io::Save(config_file).Get()); +} + +void Options::SetConfigPath(std::string const& new_path) { + config_file = new_path; + Flush(); } } // namespace agi diff --git a/aegisub/libaegisub/include/libaegisub/option.h b/aegisub/libaegisub/include/libaegisub/option.h index 4d774de49..091f86414 100644 --- a/aegisub/libaegisub/include/libaegisub/option.h +++ b/aegisub/libaegisub/include/libaegisub/option.h @@ -68,7 +68,7 @@ private: ::json::Object CreateObject(std::string path); /// User config (file that will be written to disk) - const std::string config_file; + std::string config_file; /// Settings. const OptionSetting setting; @@ -106,6 +106,10 @@ public: /// Write the user configuration to disk, throws an exception if something goes wrong. 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 diff --git a/aegisub/src/main.cpp b/aegisub/src/main.cpp index 2936b7de7..a117c04b5 100644 --- a/aegisub/src/main.cpp +++ b/aegisub/src/main.cpp @@ -81,8 +81,7 @@ #include #include #include - - +#include namespace config { agi::Options *opt; @@ -186,9 +185,6 @@ bool AegisubApp::OnInit() { // Init icons. 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 StartupLog("Load configuration"); try { @@ -202,13 +198,13 @@ bool AegisubApp::OnInit() { // Try loading configuration from the install dir if one exists there try { const std::string conf_local(StandardPaths::DecodePath("?data/config.json")); - std::ifstream* localConfig = agi::io::Open(conf_local); + agi::scoped_ptr localConfig(agi::io::Open(conf_local)); config::opt->ConfigNext(*localConfig); - delete localConfig; if (OPT_GET("App/Local Config")->GetBool()) { // Local config, make ?user mean ?data so all user settings are placed in install dir StandardPaths::SetPathValue("?user", StandardPaths::DecodePath("?data")); + config::opt->SetConfigPath(conf_local); } } catch (agi::acs::AcsError const&) { // 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"); } + 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__ SetThreadName((DWORD) -1,"AegiMain"); diff --git a/aegisub/tests/libaegisub_option.cpp b/aegisub/tests/libaegisub_option.cpp index 6207e989b..e642881ed 100644 --- a/aegisub/tests/libaegisub_option.cpp +++ b/aegisub/tests/libaegisub_option.cpp @@ -261,3 +261,19 @@ TEST_F(lagi_option, empty_array_decays_to_first_used_type) { 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()); +}