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 "TclObject.hh"
14 #include "memory.hh"
15 
16 using std::string;
17 using std::vector;
18 
19 namespace openmsx {
20 
22 {
23 public:
24  SaveSettingsCommand(CommandController& commandController,
25  SettingsConfig& settingsConfig);
26  virtual void execute(array_ref<TclObject> tokens, TclObject& result);
27  virtual string help(const vector<string>& tokens) const;
28  virtual void tabCompletion(vector<string>& tokens) const;
29 private:
30  SettingsConfig& settingsConfig;
31 };
32 
34 {
35 public:
36  LoadSettingsCommand(CommandController& commandController,
37  SettingsConfig& settingsConfig);
38  virtual void execute(array_ref<TclObject> tokens, TclObject& result);
39  virtual string help(const vector<string>& tokens) const;
40  virtual void tabCompletion(vector<string>& tokens) const;
41 private:
42  SettingsConfig& settingsConfig;
43 };
44 
45 
47  GlobalCommandController& globalCommandController,
48  HotKey& hotKey_)
49  : commandController(globalCommandController)
50  , saveSettingsCommand(make_unique<SaveSettingsCommand>(
51  commandController, *this))
52  , loadSettingsCommand(make_unique<LoadSettingsCommand>(
53  commandController, *this))
54  , settingsManager(make_unique<SettingsManager>(
55  globalCommandController))
56  , hotKey(hotKey_)
57  , mustSaveSettings(false)
58 {
59 }
60 
62 {
63  if (mustSaveSettings) {
64  try {
65  saveSetting();
66  } catch (FileException& e) {
67  commandController.getCliComm().printWarning(
68  "Auto-saving of settings failed: " + e.getMessage() );
69  }
70  }
71 }
72 
74 {
75  LocalFileReference file(context.resolve(filename));
76  xmlElement = XMLLoader::load(file.getFilename(), "settings.dtd");
77  getSettingsManager().loadSettings(xmlElement);
78  hotKey.loadBindings(xmlElement);
79 
80  // only set saveName after file was successfully parsed
81  setSaveFilename(context, filename);
82 }
83 
85 {
86  saveName = context.resolveCreate(filename);
87 }
88 
90 {
91  string_ref name = filename.empty() ? saveName : filename;
92  if (name.empty()) return;
93 
94  // Normally the following isn't needed. Only when there was no
95  // settings.xml in either the user or the system directory (so an
96  // incomplete openMSX installation!!) the top level element will have
97  // an empty name. And we shouldn't write an invalid xml file.
98  xmlElement.setName("settings");
99 
100  // settings are kept up-to-date
101  hotKey.saveBindings(xmlElement);
102 
103  File file(name, File::TRUNCATE);
104  string data = "<!DOCTYPE settings SYSTEM 'settings.dtd'>\n" +
105  xmlElement.dump();
106  file.write(data.data(), data.size());
107 }
108 
110 {
111  mustSaveSettings = save;
112 }
113 
115 {
116  return *settingsManager; // ***
117 }
118 
119 
120 // class SaveSettingsCommand
121 
123  CommandController& commandController,
124  SettingsConfig& settingsConfig_)
125  : Command(commandController, "save_settings")
126  , settingsConfig(settingsConfig_)
127 {
128 }
129 
131 {
132  try {
133  switch (tokens.size()) {
134  case 1:
135  settingsConfig.saveSetting();
136  break;
137  case 2:
138  settingsConfig.saveSetting(tokens[1].getString());
139  break;
140  default:
141  throw SyntaxError();
142  }
143  } catch (FileException& e) {
144  throw CommandException(e.getMessage());
145  }
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 
172  TclObject& /*result*/)
173 {
174  if (tokens.size() != 2) {
175  throw SyntaxError();
176  }
177  settingsConfig.loadSetting(SystemFileContext(), tokens[1].getString());
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:82
virtual void tabCompletion(vector< string > &tokens) const
Attempt tab completion for this command.
size_type size() const
Definition: array_ref.hh:61
void printWarning(string_ref message)
Definition: CliComm.cc:28
SettingsManager & getSettingsManager()
void setName(string_ref name)
Definition: XMLElement.cc:96
virtual string help(const vector< string > &tokens) const
Print help for this command.
This class implements a subset of the proposal for std::string_ref (proposed for the next c++ standar...
Definition: string_ref.hh:18
void loadSettings(const XMLElement &config)
void setSaveSettings(bool save)
std::string dump() const
Definition: XMLElement.cc:296
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:369
Manages all settings.
void saveSetting(string_ref filename="")
const std::string resolve(string_ref filename) const
Definition: FileContext.cc:74
void loadBindings(const XMLElement &config)
Definition: HotKey.cc:224
This class implements a subset of the proposal for std::array_ref (proposed for the next c++ standard...
Definition: array_ref.hh:19
std::unique_ptr< Context > context
Definition: GLContext.cc:9
const std::string & getMessage() const
Definition: MSXException.hh:14
void saveBindings(XMLElement &config) const
Definition: HotKey.cc:251
SaveSettingsCommand(CommandController &commandController, SettingsConfig &settingsConfig)
void loadSetting(const FileContext &context, string_ref filename)
SettingsConfig(GlobalCommandController &globalCommandController, HotKey &hotKey)
virtual void execute(array_ref< TclObject > tokens, TclObject &result)
Execute this command.
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, string_ref 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.
bool empty() const
Definition: string_ref.hh:56
virtual void execute(array_ref< TclObject > tokens, TclObject &result)
Execute this command.