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 "openmsx.hh"
14 #include "memory.hh"
15 #include <cassert>
16 
17 using std::string;
18 using std::vector;
19 
20 namespace openmsx {
21 
23 {
24 public:
25  SaveSettingsCommand(CommandController& commandController,
26  SettingsConfig& settingsConfig);
27  virtual string execute(const vector<string>& tokens);
28  virtual string help (const vector<string>& tokens) const;
29  virtual void tabCompletion(vector<string>& tokens) const;
30 private:
31  SettingsConfig& settingsConfig;
32 };
33 
35 {
36 public:
37  LoadSettingsCommand(CommandController& commandController,
38  SettingsConfig& settingsConfig);
39  virtual string execute(const vector<string>& tokens);
40  virtual string help (const vector<string>& tokens) const;
41  virtual void tabCompletion(vector<string>& tokens) const;
42 private:
43  SettingsConfig& settingsConfig;
44 };
45 
46 
48  GlobalCommandController& globalCommandController,
49  HotKey& hotKey_)
50  : commandController(globalCommandController)
51  , saveSettingsCommand(make_unique<SaveSettingsCommand>(
52  commandController, *this))
53  , loadSettingsCommand(make_unique<LoadSettingsCommand>(
54  commandController, *this))
55  , settingsManager(make_unique<SettingsManager>(
56  globalCommandController))
57  , hotKey(hotKey_)
58  , mustSaveSettings(false)
59 {
60 }
61 
63 {
64  PRT_DEBUG("~SettingsConfig...");
65  if (mustSaveSettings) {
66  try {
67  PRT_DEBUG("Saving Settings...");
68  saveSetting();
69  PRT_DEBUG("Saving Settings... DONE");
70  } catch (FileException& e) {
71  commandController.getCliComm().printWarning(
72  "Auto-saving of settings failed: " + e.getMessage() );
73  }
74  }
75 }
76 
77 void SettingsConfig::loadSetting(const FileContext& context, const string& filename)
78 {
79  LocalFileReference file(context.resolve(filename));
80  xmlElement = XMLLoader::load(file.getFilename(), "settings.dtd");
81  getSettingsManager().loadSettings(xmlElement);
82  hotKey.loadBindings(xmlElement);
83 
84  // only set saveName after file was successfully parsed
85  setSaveFilename(context, filename);
86 }
87 
88 void SettingsConfig::setSaveFilename(const FileContext& context, const string& filename)
89 {
90  saveName = context.resolveCreate(filename);
91 }
92 
93 void SettingsConfig::saveSetting(const string& filename)
94 {
95  const string& name = filename.empty() ? saveName : filename;
96  if (name.empty()) return;
97 
98  getSettingsManager().saveSettings(xmlElement);
99  hotKey.saveBindings(xmlElement);
100 
101  File file(name, File::TRUNCATE);
102  string data = "<!DOCTYPE settings SYSTEM 'settings.dtd'>\n" +
103  xmlElement.dump();
104  file.write(data.data(), data.size());
105 }
106 
108 {
109  mustSaveSettings = save;
110 }
111 
113 {
114  return *settingsManager; // ***
115 }
116 
117 
118 // class SaveSettingsCommand
119 
121  CommandController& commandController,
122  SettingsConfig& settingsConfig_)
123  : Command(commandController, "save_settings")
124  , settingsConfig(settingsConfig_)
125 {
126 }
127 
128 string SaveSettingsCommand::execute(const vector<string>& tokens)
129 {
130  try {
131  switch (tokens.size()) {
132  case 1:
133  settingsConfig.saveSetting();
134  break;
135  case 2:
136  settingsConfig.saveSetting(tokens[1]);
137  break;
138  default:
139  throw SyntaxError();
140  }
141  } catch (FileException& e) {
142  throw CommandException(e.getMessage());
143  }
144  return "";
145 }
146 
147 string SaveSettingsCommand::help(const vector<string>& /*tokens*/) const
148 {
149  return "Save the current settings.";
150 }
151 
152 void SaveSettingsCommand::tabCompletion(vector<string>& tokens) const
153 {
154  if (tokens.size() == 2) {
156  }
157 }
158 
159 
160 // class LoadSettingsCommand
161 
163  CommandController& commandController,
164  SettingsConfig& settingsConfig_)
165  : Command(commandController, "load_settings")
166  , settingsConfig(settingsConfig_)
167 {
168 }
169 
170 string LoadSettingsCommand::execute(const vector<string>& tokens)
171 {
172  if (tokens.size() != 2) {
173  throw SyntaxError();
174  }
175  settingsConfig.loadSetting(SystemFileContext(), tokens[1]);
176  return "";
177 }
178 
179 string LoadSettingsCommand::help(const vector<string>& /*tokens*/) const
180 {
181  return "Load settings from given file.";
182 }
183 
184 void LoadSettingsCommand::tabCompletion(vector<string>& tokens) const
185 {
186  if (tokens.size() == 2) {
188  }
189 }
190 
191 } // namespace openmsx