| 1 | /* |
| 2 | * SPDX-FileCopyrightText: Copyright (C) 2024 James Turner - [email protected] |
| 3 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | */ |
| 5 | |
| 6 | #include "LaunchConfig.hxx" |
| 7 | |
| 8 | #include <set> |
| 9 | |
| 10 | #include <Main/options.hxx> |
| 11 | #include <simgear/misc/sg_path.hxx> |
| 12 | |
| 13 | #include <QSettings> |
| 14 | #include <QDebug> |
| 15 | #include <QIODevice> |
| 16 | #include <QDataStream> |
| 17 | #include <QClipboard> |
| 18 | #include <QGuiApplication> |
| 19 | |
| 20 | #include "SettingsWrapper.hxx" |
| 21 | |
| 22 | static bool static_enableDownloadDirUI = true; |
| 23 | static QSettings::Format static_binaryFormat = QSettings::InvalidFormat; |
| 24 | |
| 25 | static bool binaryReadFunc(QIODevice &device, QSettings::SettingsMap &map) |
| 26 | { |
| 27 | QDataStream ds(&device); |
| 28 | int count; |
| 29 | ds >> count; |
| 30 | for (int i=0; i < count; ++i) { |
| 31 | QString k; |
| 32 | QVariant v; |
| 33 | ds >> k >> v; |
| 34 | map.insert(k, v); |
| 35 | } |
| 36 | |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | static bool binaryWriteFunc(QIODevice &device, const QSettings::SettingsMap &map) |
| 41 | { |
| 42 | QDataStream ds(&device); |
| 43 | |
| 44 | ds << map.size(); |
| 45 | Q_FOREACH(QString key, map.keys()) { |
| 46 | ds << key << map.value(key); |
| 47 | } |
| 48 | |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | LaunchConfig::LaunchConfig(QObject* parent) : |
| 53 | QObject(parent) |
| 54 | { |
| 55 | if (static_binaryFormat == QSettings::InvalidFormat) { |
| 56 | static_binaryFormat = QSettings::registerFormat("fglaunch", |
| 57 | &binaryReadFunc, |
| 58 | &binaryWriteFunc); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | LaunchConfig::~LaunchConfig() |
| 63 | { |
| 64 | |
| 65 | } |
| 66 | |
| 67 | void LaunchConfig::reset() |
| 68 | { |
| 69 | m_values.clear(); |
| 70 | } |
| 71 | |
| 72 | void LaunchConfig::applyToOptions() const |
| 73 | { |
| 74 | const auto extraArgs = extraArgNames(); |
| 75 | flightgear::Options* options = flightgear::Options::sharedInstance(); |
| 76 | std::for_each(m_values.begin(), m_values.end(), |
| 77 | [options, &extraArgs](const Arg& arg) |
| 78 | { |
| 79 | const auto name = arg.arg.toStdString(); |
| 80 | if (arg.origin == Launcher) { |
| 81 | auto it = extraArgs.find(name); |
| 82 | if (it != extraArgs.end()) { |
| 83 | return; |
| 84 | } |
| 85 | } |
| 86 | options->addOption(name, arg.value.toStdString()); |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | std::set<std::string> LaunchConfig::extraArgNames() const |
| 91 | { |
| 92 | // build a set of all the extra args we have defined |
| 93 | std::set<std::string> r; |
| 94 | for (auto arg : m_values) { |
| 95 | // don't override prop: arguments |
| 96 | if (arg.arg == "prop") continue; |
| 97 | |
| 98 | // allow some multi-valued arguments |
| 99 | if (arg.arg == "fg-scenery") |
| 100 | { |
| 101 | continue; |
| 102 | } |
| 103 | |
| 104 | if (arg.origin == ExtraArgs) |
| 105 | r.insert(arg.arg.toStdString()); |
| 106 | } |
| 107 | return r; |
| 108 | } |
| 109 | |
| 110 | void LaunchConfig::setArg(QString name, QString value, Origin origin) |
| 111 | { |
| 112 | m_values.push_back(Arg(name, value, origin)); |
| 113 | } |
| 114 | |
| 115 | void LaunchConfig::setArg(const std::string &name, const std::string &value) |
| 116 | { |
| 117 | setArg(QString::fromStdString(name), QString::fromStdString(value), Launcher); |
| 118 | } |
| 119 | |
| 120 | void LaunchConfig::setProperty(QString path, QVariant value, Origin origin) |
| 121 | { |
| 122 | m_values.push_back(Arg("prop", path + "=" + value.toString(), origin)); |
| 123 | } |
| 124 | |
| 125 | void LaunchConfig::setEnableDisableOption(QString name, bool value) |
| 126 | { |
| 127 | m_values.push_back(Arg((value ? "enable-" : "disable-") + name, "", Launcher)); |
| 128 | } |
| 129 | |
| 130 | QString LaunchConfig::htmlForCommandLine() |
| 131 | { |
| 132 | QString html; |
| 133 | string_list commandLineOpts = flightgear::Options::sharedInstance()->extractOptions(); |
| 134 | if (!commandLineOpts.empty()) { |
| 135 | html += tr("<p>Options passed on the command line:</p>\n"); |
| 136 | html += "<ul>\n"; |
| 137 | for (auto opt : commandLineOpts) { |
| 138 | html += QString("<li>--") + QString::fromStdString(opt) + "</li>\n"; |
| 139 | } |
| 140 | html += "</ul>\n"; |
| 141 | } |
| 142 | |
| 143 | reset(); |
| 144 | collect(); |
| 145 | |
| 146 | const auto extraArgs = extraArgNames(); |
| 147 | |
| 148 | html += tr("<p>Options set in the launcher:</p>\n"); |
| 149 | html += "<ul>\n"; |
| 150 | for (auto arg : valuesFromLauncher()) { |
| 151 | auto it = extraArgs.find(arg.arg.toStdString()); |
| 152 | html += "<li>"; |
| 153 | bool strikeThrough = (it != extraArgs.end()); |
| 154 | if (strikeThrough) { |
| 155 | html += "<i>"; |
| 156 | } |
| 157 | if (arg.value.isEmpty()) { |
| 158 | html += QString("--") + arg.arg; |
| 159 | } else if (arg.arg == "prop") { |
| 160 | html += QString("--") + arg.arg + ":" + arg.value; |
| 161 | } else { |
| 162 | html += QString("--") + arg.arg + "=" + arg.value; |
| 163 | } |
| 164 | if (strikeThrough) { |
| 165 | html += tr(" (will be skipped due to being specified as an additional argument)") + "</i> "; |
| 166 | } |
| 167 | html += "</li>\n"; |
| 168 | } |
| 169 | html += "</ul>\n"; |
| 170 | |
| 171 | html += tr("<p>Options set as additional arguments:</p>\n"); |
| 172 | html += "<ul>\n"; |
| 173 | for (auto arg : valuesFromExtraArgs()) { |
| 174 | if (arg.value.isEmpty()) { |
| 175 | html += QString("<li>--") + arg.arg + "</li>\n"; |
| 176 | } else if (arg.arg == "prop") { |
| 177 | html += QString("<li>--") + arg.arg + ":" + arg.value + "</li>\n"; |
| 178 | } else { |
| 179 | html += QString("<li>--") + arg.arg + "=" + arg.value + "</li>\n"; |
| 180 | } |
| 181 | } |
| 182 | html += "</ul>\n"; |
| 183 | |
| 184 | return html; |
| 185 | } |
| 186 | |
| 187 | static QString formatArgForClipboard(const LaunchConfig::Arg& a) |
| 188 | { |
| 189 | if (a.value.isEmpty()) { |
| 190 | return "--" + a.arg + " "; |
| 191 | } else if (a.arg == "prop") { |
| 192 | return "--" + a.arg + ":" + a.value + " "; |
| 193 | } else { |
| 194 | return "--" + a.arg + "=" + a.value + " "; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | void LaunchConfig::copyCommandLine() |
| 199 | { |
| 200 | QString r; |
| 201 | |
| 202 | for (auto opt : flightgear::Options::sharedInstance()->extractOptions()) { |
| 203 | r += "--" + QString::fromStdString(opt) + " "; |
| 204 | } |
| 205 | |
| 206 | reset(); |
| 207 | collect(); |
| 208 | const auto extraArgs = extraArgNames(); |
| 209 | |
| 210 | for (auto arg : valuesFromLauncher()) { |
| 211 | auto it = extraArgs.find(arg.arg.toStdString()); |
| 212 | if (it != extraArgs.end()) { |
| 213 | continue; // skipped due to extra arg overriding |
| 214 | } |
| 215 | |
| 216 | r += formatArgForClipboard(arg); |
| 217 | } |
| 218 | |
| 219 | for (auto arg : valuesFromExtraArgs()) { |
| 220 | r += formatArgForClipboard(arg); |
| 221 | } |
| 222 | |
| 223 | QClipboard *clipboard = QGuiApplication::clipboard(); |
| 224 | clipboard->setText(r); |
| 225 | } |
| 226 | |
| 227 | bool LaunchConfig::saveConfigToINI() |
| 228 | { |
| 229 | // create settings using default type (INI) and path (inside FG_HOME), |
| 230 | // as setup in initQSettings() |
| 231 | m_loadSaveSettings = flightgear::createQSettings(); |
| 232 | emit save(); |
| 233 | m_loadSaveSettings->sync(); |
| 234 | m_loadSaveSettings.reset(); |
| 235 | |
| 236 | return true; |
| 237 | } |
| 238 | |
| 239 | bool LaunchConfig::loadConfigFromINI() |
| 240 | { |
| 241 | // create settings using default type (INI) and path (inside FG_HOME), |
| 242 | // as setup in initQSettings() |
| 243 | m_loadSaveSettings = flightgear::createQSettings(); |
| 244 | emit restore(); |
| 245 | emit postRestore(); |
| 246 | m_loadSaveSettings.reset(); |
| 247 | return true; |
| 248 | } |
| 249 | |
| 250 | bool LaunchConfig::saveConfigToFile(QString path) |
| 251 | { |
| 252 | m_loadSaveSettings.reset(new QSettings(path, static_binaryFormat)); |
| 253 | emit save(); |
| 254 | m_loadSaveSettings.reset(); |
| 255 | return true; |
| 256 | } |
| 257 | |
| 258 | bool LaunchConfig::loadConfigFromFile(QString path) |
| 259 | { |
| 260 | m_loadSaveSettings.reset(new QSettings(path, static_binaryFormat)); |
| 261 | emit restore(); |
| 262 | // some things have an ordering dependency, give them a chance to run |
| 263 | // after other settings have been restored (eg, location or aircraft) |
| 264 | emit postRestore(); |
| 265 | m_loadSaveSettings.reset(); |
| 266 | return true; |
| 267 | } |
| 268 | |
| 269 | QVariant LaunchConfig::getValueForKey(QString group, QString key, QVariant defaultValue) const |
| 270 | { |
| 271 | if (!m_loadSaveSettings) { |
| 272 | // becuase we load settings on component completion, we need |
| 273 | // to create the default implementation (using the INI file) |
| 274 | // on demand |
| 275 | m_loadSaveSettings = flightgear::createQSettings(); |
| 276 | } |
| 277 | |
| 278 | m_loadSaveSettings->beginGroup(group); |
| 279 | auto v = m_loadSaveSettings->value(key, defaultValue); |
| 280 | bool convertedOk = v.convert(static_cast<int>(defaultValue.type())); |
| 281 | if (!convertedOk) { |
| 282 | qWarning() << "type forcing on loaded value failed:" << key << v << v.typeName() << defaultValue; |
| 283 | } |
| 284 | // qInfo() << Q_FUNC_INFO << key << "value" << v << v.typeName() << convertedOk; |
| 285 | return v; |
| 286 | } |
| 287 | |
| 288 | void LaunchConfig::setValueForKey(QString group, QString key, QVariant var) |
| 289 | { |
| 290 | Q_ASSERT(m_loadSaveSettings); |
| 291 | m_loadSaveSettings->beginGroup(group); |
| 292 | // qInfo() << "saving" << key << "with value" << var << var.typeName(); |
| 293 | m_loadSaveSettings->setValue(key, var); |
| 294 | m_loadSaveSettings->endGroup(); |
| 295 | } |
| 296 | |
| 297 | QString LaunchConfig::defaultDownloadDir() const |
| 298 | { |
| 299 | return QString::fromStdString(flightgear::defaultDownloadDir().utf8Str()); |
| 300 | } |
| 301 | |
| 302 | bool LaunchConfig::enableDownloadDirUI() const |
| 303 | { |
| 304 | return static_enableDownloadDirUI; |
| 305 | } |
| 306 | |
| 307 | void LaunchConfig::setEnableDownloadDirUI(bool enableDownloadDirUI) |
| 308 | { |
| 309 | static_enableDownloadDirUI = enableDownloadDirUI; |
| 310 | } |
| 311 | |
| 312 | auto LaunchConfig::values() const -> std::vector<Arg> |
| 313 | { |
| 314 | return m_values; |
| 315 | } |
| 316 | |
| 317 | auto LaunchConfig::valuesFromLauncher() const -> std::vector<Arg> |
| 318 | { |
| 319 | std::vector<Arg> result; |
| 320 | std::copy_if(m_values.begin(), m_values.end(), std::back_inserter(result), [](const Arg& a) |
| 321 | { return a.origin == Launcher; }); |
| 322 | return result; |
| 323 | } |
| 324 | |
| 325 | auto LaunchConfig::valuesFromExtraArgs() const -> std::vector<Arg> |
| 326 | { |
| 327 | std::vector<Arg> result; |
| 328 | std::copy_if(m_values.begin(), m_values.end(), std::back_inserter(result), [](const Arg& a) |
| 329 | { return a.origin == ExtraArgs; }); |
| 330 | return result; |
| 331 | } |