openMSX
SettingsConfig.cc
Go to the documentation of this file.
1 #include "SettingsConfig.hh"
2 #include "SettingsManager.hh"
3 #include "XMLLoader.hh"
4 #include "LocalFileReference.hh"
5 #include "File.hh"
6 #include "FileContext.hh"
7 #include "FileException.hh"
8 #include "CliComm.hh"
9 #include "HotKey.hh"
10 #include "CommandException.hh"
12 #include "Command.hh"
13 #include "memory.hh"
14 
15 using std::string;
16 using std::vector;
17 
18 namespace openmsx {
19 
21 {
22 public:
23  SaveSettingsCommand(CommandController& commandController,
24  SettingsConfig& settingsConfig);
25  virtual string execute(const vector<string>& tokens);
26  virtual string help (const vector<string>& tokens) const;
27  virtual void tabCompletion(vector<string>& tokens) const;
28 private:
29  SettingsConfig& settingsConfig;
30 };
31 
33 {
34 public:
35  LoadSettingsCommand(CommandController& commandController,
36  SettingsConfig& settingsConfig);
37  virtual string execute(const vector<string>& tokens);
38  virtual string help (const vector<string>& tokens) const;
39  virtual void tabCompletion(vector<string>& tokens) const;
40 private:
41  SettingsConfig& settingsConfig;
42 };
43 
44 
46  GlobalCommandController& globalCommandController,
47  HotKey& hotKey_)
48  : commandController(globalCommandController)
49  , saveSettingsCommand(make_unique<SaveSettingsCommand>(
50  commandController, *this))
51  , loadSettingsCommand(make_unique<LoadSettingsCommand>(
52  commandController, *this))
53  , settingsManager(make_unique<SettingsManager>(
54  globalCommandController))
55  , hotKey(hotKey_)
56  , mustSaveSettings(false)
57 {
58 }
59 
61 {
62  if (mustSaveSettings) {
63  try {
64  saveSetting();
65  } catch (FileException& e) {
66  commandController.getCliComm().printWarning(
67  "Auto-saving of settings failed: " + e.getMessage() );
68  }
69  }
70 }
71 
72 void SettingsConfig::loadSetting(const FileContext& context, const string& filename)
73 {
74  LocalFileReference file(context.resolve(filename));
75  xmlElement = XMLLoader::load(file.getFilename(), "settings.dtd");
76  getSettingsManager().loadSettings(xmlElement);
77  hotKey.loadBindings(xmlElement);
78 
79  // only set saveName after file was successfully parsed
80  setSaveFilename(context, filename);
81 }
82 
83 void SettingsConfig::setSaveFilename(const FileContext& context, const string& filename)
84 {
85  saveName = context.resolveCreate(filename);
86 }
87 
88 void SettingsConfig::saveSetting(const string& filename)
89 {
90  const string& name = filename.empty() ? saveName : filename;
91  if (name.empty()) return;
92 
93  // Normally the following isn't needed. Only when there was no
94  // settings.xml in either the user or the system directory (so an
95  // incomplete openMSX installation!!) the top level element will have
96  // an empty name. And we shouldn't write an invalid xml file.
97  xmlElement.setName("settings");
98 
99  // settings are kept up-to-date
100  hotKey.saveBindings(xmlElement);
101 
102  File file(name, File::TRUNCATE);
103  string data = "<!DOCTYPE settings SYSTEM 'settings.dtd'>\n" +
104  xmlElement.dump();
105  file.write(data.data(), data.size());
106 }
107 
109 {
110  mustSaveSettings = save;
111 }
112 
114 {
115  return *settingsManager; // ***
116 }
117 
118 
119 // class SaveSettingsCommand
120 
122  CommandController& commandController,
123  SettingsConfig& settingsConfig_)
124  : Command(commandController, "save_settings")
125  , settingsConfig(settingsConfig_)
126 {
127 }
128 
129 string SaveSettingsCommand::execute(const vector<string>& tokens)
130 {
131  try {
132  switch (tokens.size()) {
133  case 1:
134  settingsConfig.saveSetting();
135  break;
136  case 2:
137  settingsConfig.saveSetting(tokens[1]);
138  break;
139  default:
140  throw SyntaxError();
141  }
142  } catch (FileException& e) {
143  throw CommandException(e.getMessage());
144  }
145  return "";
146 }
147 
148 string SaveSettingsCommand::help(const vector<string>& /*tokens*/) const
149 {
150  return "Save the current settings.";
151 }
152 
153 void SaveSettingsCommand::tabCompletion(vector<string>& tokens) const
154 {
155  if (tokens.size() == 2) {
157  }
158 }
159 
160 
161 // class LoadSettingsCommand
162 
164  CommandController& commandController,
165  SettingsConfig& settingsConfig_)
166  : Command(commandController, "load_settings")
167  , settingsConfig(settingsConfig_)
168 {
169 }
170 
171 string LoadSettingsCommand::execute(const vector<string>& tokens)
172 {
173  if (tokens.size() != 2) {
174  throw SyntaxError();
175  }
176  settingsConfig.loadSetting(SystemFileContext(), tokens[1]);
177  return "";
178 }
179 
180 string LoadSettingsCommand::help(const vector<string>& /*tokens*/) const
181 {
182  return "Load settings from given file.";
183 }
184 
185 void LoadSettingsCommand::tabCompletion(vector<string>& tokens) const
186 {
187  if (tokens.size() == 2) {
189  }
190 }
191 
192 } // namespace openmsx
virtual void tabCompletion(vector< string > &tokens) const
Attempt tab completion for this command.
const std::string resolveCreate(string_ref filename) const
Definition: FileContext.cc:81
virtual void tabCompletion(vector< string > &tokens) const
Attempt tab completion for this command.
virtual string execute(const vector< string > &tokens)
Alternative for the execute() method above.
void printWarning(string_ref message)
Definition: CliComm.cc:28
void saveSetting(const std::string &filename="")
SettingsManager & getSettingsManager()
void setName(string_ref name)
Definition: XMLElement.cc:99
virtual string help(const vector< string > &tokens) const
Print help for this command.
void loadSettings(const XMLElement &config)
void setSaveSettings(bool save)
std::string dump() const
Definition: XMLElement.cc:299
XMLElement load(string_ref filename, string_ref systemID)
Definition: XMLLoader.cc:31
static void completeFileName(std::vector< std::string > &tokens, const FileContext &context, const RANGE &extra)
Definition: Completer.hh:102
void save(SDL_Surface *surface, const std::string &filename)
Definition: PNG.cc:364
Manages all settings.
const std::string resolve(string_ref filename) const
Definition: FileContext.cc:73
void loadBindings(const XMLElement &config)
Definition: HotKey.cc:224
const std::string & getMessage() const
Definition: MSXException.hh:14
void saveBindings(XMLElement &config) const
Definition: HotKey.cc:252
SaveSettingsCommand(CommandController &commandController, SettingsConfig &settingsConfig)
SettingsConfig(GlobalCommandController &globalCommandController, HotKey &hotKey)
virtual string execute(const vector< string > &tokens)
Alternative for the execute() method above.
virtual CliComm & getCliComm()=0
uint8_t * data()
void write(const void *buffer, size_t num)
Write to file.
Definition: File.cc:73
Helper class to use files is APIs other than openmsx::File.
void setSaveFilename(const FileContext &context, const std::string &filename)
LoadSettingsCommand(CommandController &commandController, SettingsConfig &settingsConfig)
std::unique_ptr< T > make_unique()
Definition: memory.hh:27
virtual string help(const vector< string > &tokens) const
Print help for this command.
void loadSetting(const FileContext &context, const std::string &filename)