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:
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:
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 
109 
110 // class SaveSettingsCommand
111 
114  SettingsConfig& settingsConfig_)
115  : Command(commandController, "save_settings")
116  , settingsConfig(settingsConfig_)
117 {
118 }
119 
121 {
122  try {
123  switch (tokens.size()) {
124  case 1:
125  settingsConfig.saveSetting();
126  break;
127  case 2:
128  settingsConfig.saveSetting(tokens[1].getString());
129  break;
130  default:
131  throw SyntaxError();
132  }
133  } catch (FileException& e) {
134  throw CommandException(e.getMessage());
135  }
136 }
137 
138 string SaveSettingsCommand::help(const vector<string>& /*tokens*/) const
139 {
140  return "Save the current settings.";
141 }
142 
143 void SaveSettingsCommand::tabCompletion(vector<string>& tokens) const
144 {
145  if (tokens.size() == 2) {
147  }
148 }
149 
150 
151 // class LoadSettingsCommand
152 
155  SettingsConfig& settingsConfig_)
156  : Command(commandController, "load_settings")
157  , settingsConfig(settingsConfig_)
158 {
159 }
160 
162  TclObject& /*result*/)
163 {
164  if (tokens.size() != 2) {
165  throw SyntaxError();
166  }
167  settingsConfig.loadSetting(SystemFileContext(), tokens[1].getString());
168 }
169 
170 string LoadSettingsCommand::help(const vector<string>& /*tokens*/) const
171 {
172  return "Load settings from given file.";
173 }
174 
175 void LoadSettingsCommand::tabCompletion(vector<string>& tokens) const
176 {
177  if (tokens.size() == 2) {
179  }
180 }
181 
182 } // 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
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)
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
Manages all settings.
void saveSetting(string_ref filename="")
SettingsManager & getSettingsManager()
const std::string resolve(string_ref filename) const
Definition: FileContext.cc:74
void loadBindings(const XMLElement &config)
Definition: HotKey.cc:225
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:252
SaveSettingsCommand(CommandController &commandController, SettingsConfig &settingsConfig)
void loadSetting(const FileContext &context, string_ref filename)
SettingsConfig(GlobalCommandController &globalCommandController, HotKey &hotKey)
commandController(commandController_)
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.