diff --git a/src/rime/config/config_component.cc b/src/rime/config/config_component.cc index f5ff15dc6..69ec4e833 100644 --- a/src/rime/config/config_component.cc +++ b/src/rime/config/config_component.cc @@ -143,15 +143,30 @@ void Config::SetItem(an item) { set_modified(); } -const ResourceType ConfigComponentBase::kConfigResourceType = { +const ResourceType ConfigResourceProvider::kDefaultResourceType = { "config", "", ".yaml", }; -ConfigComponentBase::ConfigComponentBase(const ResourceType& resource_type) - : resource_resolver_( - Service::instance().CreateResourceResolver(resource_type)) { +ResourceResolver* ConfigResourceProvider::CreateResourceResolver( + const ResourceType& resource_type) { + return Service::instance().CreateResourceResolver(resource_type); +} + +const ResourceType UserConfigResourceProvider::kDefaultResourceType = { + "user_config", + "", + ".yaml", +}; + +ResourceResolver* UserConfigResourceProvider::CreateResourceResolver( + const ResourceType& resource_type) { + return Service::instance().CreateUserSpecificResourceResolver(resource_type); +} + +ConfigComponentBase::ConfigComponentBase(ResourceResolver* resource_resolver) + : resource_resolver_(resource_resolver) { } ConfigComponentBase::~ConfigComponentBase() { diff --git a/src/rime/config/config_component.h b/src/rime/config/config_component.h index 39895c28d..08c36b7ab 100644 --- a/src/rime/config/config_component.h +++ b/src/rime/config/config_component.h @@ -68,11 +68,22 @@ class ConfigCompiler; class ConfigCompilerPlugin; struct ConfigResource; +struct ConfigResourceProvider { + RIME_API static const ResourceType kDefaultResourceType; + RIME_API static ResourceResolver* + CreateResourceResolver(const ResourceType& resource_type); +}; + +struct UserConfigResourceProvider { + RIME_API static const ResourceType kDefaultResourceType; + RIME_API static ResourceResolver* + CreateResourceResolver(const ResourceType& resource_type); +}; + class ConfigComponentBase : public Config::Component { public: - RIME_API static const ResourceType kConfigResourceType; - RIME_API ConfigComponentBase(const ResourceType& resource_type); - RIME_API ~ConfigComponentBase(); + RIME_API ConfigComponentBase(ResourceResolver* resource_resolver); + RIME_API virtual ~ConfigComponentBase(); RIME_API Config* Create(const string& file_name); protected: @@ -84,13 +95,17 @@ class ConfigComponentBase : public Config::Component { map> cache_; }; -template +template class ConfigComponent : public ConfigComponentBase { public: - ConfigComponent(const ResourceType& resource_type = kConfigResourceType) - : ConfigComponentBase(resource_type) {} + ConfigComponent(const ResourceType& resource_type = + ResourceProvider::kDefaultResourceType) + : ConfigComponentBase( + ResourceProvider::CreateResourceResolver(resource_type)) {} ConfigComponent(function setup) - : ConfigComponentBase(kConfigResourceType) { + : ConfigComponentBase( + ResourceProvider::CreateResourceResolver( + ResourceProvider::kDefaultResourceType)) { setup(&loader_); } private: diff --git a/src/rime/core_module.cc b/src/rime/core_module.cc index 175d23ea7..95ff7dac7 100644 --- a/src/rime/core_module.cc +++ b/src/rime/core_module.cc @@ -36,10 +36,11 @@ static void rime_core_initialize() { r.Register("config", config_loader); r.Register("schema", new SchemaComponent(config_loader)); - auto user_config = new ConfigComponent( - [](ConfigLoader* loader) { - loader->set_auto_save(true); - }); + auto user_config = + new ConfigComponent( + [](ConfigLoader* loader) { + loader->set_auto_save(true); + }); r.Register("user_config", user_config); } diff --git a/src/rime/service.cc b/src/rime/service.cc index fddcdbb39..1d6af56ec 100644 --- a/src/rime/service.cc +++ b/src/rime/service.cc @@ -176,6 +176,13 @@ ResourceResolver* Service::CreateResourceResolver(const ResourceType& type) { return resolver.release(); } +ResourceResolver* Service::CreateUserSpecificResourceResolver( + const ResourceType& type) { + the resolver(new ResourceResolver(type)); + resolver->set_root_path(deployer().user_data_dir); + return resolver.release(); +} + Service& Service::instance() { static the s_instance; if (!s_instance) { diff --git a/src/rime/service.h b/src/rime/service.h index 6e6183ed7..f2e6a83fa 100644 --- a/src/rime/service.h +++ b/src/rime/service.h @@ -76,6 +76,7 @@ class Service { const string& message_value); ResourceResolver* CreateResourceResolver(const ResourceType& type); + ResourceResolver* CreateUserSpecificResourceResolver(const ResourceType& type); Deployer& deployer() { return deployer_; } bool disabled() { return !started_ || deployer_.IsMaintenanceMode(); } diff --git a/test/config_test.cc b/test/config_test.cc index 71980201b..434457323 100644 --- a/test/config_test.cc +++ b/test/config_test.cc @@ -30,10 +30,12 @@ class RimeConfigTest : public ::testing::Test { TEST(RimeConfigComponentTest, RoundTrip) { // registration Registry& r = Registry::instance(); - r.Register("test_config", new ConfigComponent( - [](ConfigLoader* loader) { - loader->set_auto_save(true); - })); + r.Register( + "test_config", + new ConfigComponent( + [](ConfigLoader* loader) { + loader->set_auto_save(true); + })); // find component Config::Component* cc = Config::Require("test_config"); ASSERT_TRUE(cc != NULL);