openMSX
SettingsConfig.cc
Go to the documentation of this file.
1 #include "SettingsConfig.hh"
2 #include "XMLLoader.hh"
3 #include "File.hh"
4 #include "FileContext.hh"
5 #include "FileException.hh"
6 #include "CliComm.hh"
7 #include "HotKey.hh"
8 #include "CommandException.hh"
10 #include "TclObject.hh"
11 #include "outer.hh"
12 
13 using std::string;
14 using std::string_view;
15 using std::vector;
16 
17 namespace openmsx {
18 
20  GlobalCommandController& globalCommandController,
21  HotKey& hotKey_)
22  : commandController(globalCommandController)
23  , saveSettingsCommand(commandController)
24  , loadSettingsCommand(commandController)
25  , settingsManager(globalCommandController)
26  , hotKey(hotKey_)
27  , mustSaveSettings(false)
28 {
29 }
30 
32 {
33  if (mustSaveSettings) {
34  try {
35  saveSetting();
36  } catch (FileException& e) {
37  commandController.getCliComm().printWarning(
38  "Auto-saving of settings failed: ", e.getMessage());
39  }
40  }
41 }
42 
44 {
45  xmlElement = XMLLoader::load(context.resolve(filename), "settings.dtd");
46  getSettingsManager().loadSettings(xmlElement);
47  hotKey.loadBindings(xmlElement);
48 
49  // only set saveName after file was successfully parsed
51 }
52 
54 {
55  saveName = context.resolveCreate(filename);
56 }
57 
59 {
60  string_view name = filename.empty() ? saveName : filename;
61  if (name.empty()) return;
62 
63  // Normally the following isn't needed. Only when there was no
64  // settings.xml in either the user or the system directory (so an
65  // incomplete openMSX installation!!) the top level element will have
66  // an empty name. And we shouldn't write an invalid xml file.
67  xmlElement.setName("settings");
68 
69  // settings are kept up-to-date
70  hotKey.saveBindings(xmlElement);
71 
72  File file(name, File::TRUNCATE);
73  string data = "<!DOCTYPE settings SYSTEM 'settings.dtd'>\n" +
74  xmlElement.dump();
75  file.write(data.data(), data.size());
76 }
77 
78 
79 // class SaveSettingsCommand
80 
81 SettingsConfig::SaveSettingsCommand::SaveSettingsCommand(
82  CommandController& commandController_)
83  : Command(commandController_, "save_settings")
84 {
85 }
86 
87 void SettingsConfig::SaveSettingsCommand::execute(
88  span<const TclObject> tokens, TclObject& /*result*/)
89 {
90  checkNumArgs(tokens, Between{1, 2}, Prefix{1}, "?filename?");
91  auto& settingsConfig = OUTER(SettingsConfig, saveSettingsCommand);
92  try {
93  switch (tokens.size()) {
94  case 1:
95  settingsConfig.saveSetting();
96  break;
97  case 2:
98  settingsConfig.saveSetting(tokens[1].getString());
99  break;
100  }
101  } catch (FileException& e) {
102  throw CommandException(std::move(e).getMessage());
103  }
104 }
105 
106 string SettingsConfig::SaveSettingsCommand::help(const vector<string>& /*tokens*/) const
107 {
108  return "Save the current settings.";
109 }
110 
111 void SettingsConfig::SaveSettingsCommand::tabCompletion(vector<string>& tokens) const
112 {
113  if (tokens.size() == 2) {
114  completeFileName(tokens, systemFileContext());
115  }
116 }
117 
118 
119 // class LoadSettingsCommand
120 
121 SettingsConfig::LoadSettingsCommand::LoadSettingsCommand(
122  CommandController& commandController_)
123  : Command(commandController_, "load_settings")
124 {
125 }
126 
127 void SettingsConfig::LoadSettingsCommand::execute(
128  span<const TclObject> tokens, TclObject& /*result*/)
129 {
130  checkNumArgs(tokens, 2, "filename");
131  auto& settingsConfig = OUTER(SettingsConfig, loadSettingsCommand);
132  settingsConfig.loadSetting(systemFileContext(), tokens[1].getString());
133 }
134 
135 string SettingsConfig::LoadSettingsCommand::help(const vector<string>& /*tokens*/) const
136 {
137  return "Load settings from given file.";
138 }
139 
140 void SettingsConfig::LoadSettingsCommand::tabCompletion(vector<string>& tokens) const
141 {
142  if (tokens.size() == 2) {
143  completeFileName(tokens, systemFileContext());
144  }
145 }
146 
147 } // namespace openmsx
FileException.hh
openmsx::SettingsConfig::SettingsConfig
SettingsConfig(GlobalCommandController &globalCommandController, HotKey &hotKey)
Definition: SettingsConfig.cc:19
openmsx::SettingsConfig::loadSetting
void loadSetting(const FileContext &context, std::string_view filename)
Definition: SettingsConfig.cc:43
openmsx::SettingsConfig::setSaveFilename
void setSaveFilename(const FileContext &context, std::string_view filename)
Definition: SettingsConfig.cc:53
openmsx::CommandController
Definition: CommandController.hh:18
TclObject.hh
openmsx::HotKey::saveBindings
void saveBindings(XMLElement &config) const
Definition: HotKey.cc:216
openmsx::SettingsConfig::getSettingsManager
SettingsManager & getSettingsManager()
Definition: SettingsConfig.hh:29
openmsx::FileContext
Definition: FileContext.hh:10
openmsx::SettingsManager::loadSettings
void loadSettings(const XMLElement &config)
Definition: SettingsManager.cc:101
span
Definition: span.hh:126
OUTER
#define OUTER(type, member)
Definition: outer.hh:41
openmsx::CliComm::printWarning
void printWarning(std::string_view message)
Definition: CliComm.cc:10
openmsx::File::TRUNCATE
@ TRUNCATE
Definition: File.hh:20
openmsx::CommandController::getCliComm
virtual CliComm & getCliComm()=0
XMLLoader.hh
SettingsConfig.hh
File.hh
openmsx::File::write
void write(const void *buffer, size_t num)
Write to file.
Definition: File.cc:88
openmsx::filename
constexpr const char *const filename
Definition: FirmwareSwitch.cc:10
openmsx::FileException
Definition: FileException.hh:9
openmsx::HotKey
Definition: HotKey.hh:22
openmsx::SettingsConfig::~SettingsConfig
~SettingsConfig()
Definition: SettingsConfig.cc:31
FileContext.hh
openmsx::XMLElement::dump
std::string dump() const
Definition: XMLElement.cc:255
openmsx::XMLLoader::load
XMLElement load(string_view filename, string_view systemID)
Definition: XMLLoader.cc:34
outer.hh
span::size
constexpr index_type size() const noexcept
Definition: span.hh:296
HotKey.hh
gl::context
std::unique_ptr< Context > context
Definition: GLContext.cc:9
openmsx::File
Definition: File.hh:16
GlobalCommandController.hh
openmsx::HotKey::loadBindings
void loadBindings(const XMLElement &config)
Definition: HotKey.cc:176
openmsx::Command
Definition: Command.hh:41
CliComm.hh
openmsx::MSXException::getMessage
const std::string & getMessage() const &
Definition: MSXException.hh:23
openmsx::SettingsConfig::saveSetting
void saveSetting(std::string_view filename={})
Definition: SettingsConfig.cc:58
CommandException.hh
openmsx::GlobalCommandController
Definition: GlobalCommandController.hh:37
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5
openmsx::XMLElement::setName
void setName(std::string name_)
Definition: XMLElement.hh:32
openmsx::systemFileContext
FileContext systemFileContext()
Definition: FileContext.cc:152