Otclient  14/8/2020
configmanager.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "configmanager.h"
24 
26 
28 {
29  m_settings = ConfigPtr(new Config());
30 }
31 
33 {
34  if(m_settings) {
35  // ensure settings are saved
36  m_settings->save();
37 
38  m_settings->unload();
39  m_settings = nullptr;
40  }
41 
42  for(ConfigPtr config : m_configs) {
43  config->unload();
44  config = nullptr;
45  }
46 
47  m_configs.clear();
48 }
49 
51 {
52  return m_settings;
53 }
54 
55 ConfigPtr ConfigManager::get(const std::string& file)
56 {
57  for(const ConfigPtr config : m_configs) {
58  if(config->getFileName() == file) {
59  return config;
60  }
61  }
62  return nullptr;
63 }
64 
65 ConfigPtr ConfigManager::loadSettings(const std::string file)
66 {
67  if(file.empty()) {
68  g_logger.error("Must provide a configuration file to load.");
69  }
70  else {
71  if(m_settings->load(file)) {
72  return m_settings;
73  }
74  }
75  return nullptr;
76 }
77 
78 ConfigPtr ConfigManager::create(const std::string& file)
79 {
80  ConfigPtr config = load(file);
81  if(!config) {
82  config = ConfigPtr(new Config());
83 
84  config->load(file);
85  config->save();
86 
87  m_configs.push_back(config);
88  }
89  return config;
90 }
91 
92 ConfigPtr ConfigManager::load(const std::string& file)
93 {
94  if(file.empty()) {
95  g_logger.error("Must provide a configuration file to load.");
96  return nullptr;
97  }
98  else {
99  ConfigPtr config = get(file);
100  if(!config) {
101  config = ConfigPtr(new Config());
102 
103  if(config->load(file)) {
104  m_configs.push_back(config);
105  }
106  else {
107  // cannot load config
108  config = nullptr;
109  }
110  }
111  return config;
112  }
113 }
114 
115 bool ConfigManager::unload(const std::string& file)
116 {
117  ConfigPtr config = get(file);
118  if(config) {
119  config->unload();
120  remove(config);
121  config = nullptr;
122  return true;
123  }
124  return false;
125 }
126 
128 {
129  m_configs.remove(config);
130 }
Config::load
bool load(const std::string &file)
Definition: config.cpp:35
configmanager.h
Config::save
bool save()
Definition: config.cpp:63
ConfigPtr
stdext::shared_object_ptr< Config > ConfigPtr
Definition: declarations.h:40
ConfigManager::remove
void remove(const ConfigPtr config)
Definition: configmanager.cpp:127
Logger::error
void error(const std::string &what)
Definition: logger.h:54
g_configs
ConfigManager g_configs
Definition: configmanager.cpp:25
Config
Definition: config.h:32
ConfigManager::getSettings
ConfigPtr getSettings()
Definition: configmanager.cpp:50
g_logger
Logger g_logger
Definition: logger.cpp:35
ConfigManager
Definition: configmanager.h:29
ConfigManager::m_settings
ConfigPtr m_settings
Definition: configmanager.h:46
ConfigManager::get
ConfigPtr get(const std::string &file)
Definition: configmanager.cpp:55
ConfigManager::terminate
void terminate()
Definition: configmanager.cpp:32
ConfigManager::init
void init()
Definition: configmanager.cpp:27
ConfigManager::create
ConfigPtr create(const std::string &file)
Definition: configmanager.cpp:78
ConfigManager::load
ConfigPtr load(const std::string &file)
Definition: configmanager.cpp:92
stdext::shared_object_ptr< Config >
Config::unload
bool unload()
Definition: config.cpp:53
ConfigManager::loadSettings
ConfigPtr loadSettings(const std::string file)
Definition: configmanager.cpp:65
ConfigManager::unload
bool unload(const std::string &file)
Definition: configmanager.cpp:115