| 1 | #include "./resources.h" |
| 2 | |
| 3 | #include "resources/config.h" |
| 4 | |
| 5 | #include <QDir> |
| 6 | #include <QFile> |
| 7 | #include <QFont> |
| 8 | #include <QIcon> |
| 9 | #include <QLibraryInfo> |
| 10 | #include <QLocale> |
| 11 | #include <QSettings> |
| 12 | #include <QString> |
| 13 | #include <QStringBuilder> |
| 14 | #include <QTranslator> |
| 15 | #if defined(QT_UTILITIES_GUI_QTWIDGETS) |
| 16 | #include <QApplication> |
| 17 | #elif defined(QT_UTILITIES_GUI_QTQUICK) |
| 18 | #include <QGuiApplication> |
| 19 | #else |
| 20 | #include <QCoreApplication> |
| 21 | #endif |
| 22 | |
| 23 | #ifdef Q_OS_ANDROID |
| 24 | #include <QDebug> |
| 25 | #include <QStandardPaths> |
| 26 | #endif |
| 27 | |
| 28 | #ifdef Q_OS_ANDROID |
| 29 | #include <c++utilities/conversion/stringbuilder.h> |
| 30 | #endif |
| 31 | |
| 32 | #include <iostream> |
| 33 | |
| 34 | using namespace std; |
| 35 | |
| 36 | ///! \cond |
| 37 | inline void initResources() |
| 38 | { |
| 39 | Q_INIT_RESOURCE(qtutilsicons); |
| 40 | } |
| 41 | |
| 42 | inline void cleanupResources() |
| 43 | { |
| 44 | Q_CLEANUP_RESOURCE(qtutilsicons); |
| 45 | } |
| 46 | ///! \endcond |
| 47 | |
| 48 | namespace QtUtilities { |
| 49 | |
| 50 | /*! |
| 51 | * \brief Functions for using the resources provided by this library. |
| 52 | * \deprecated Replaced by ENABLE_QT_RESOURCES_OF_STATIC_DEPENDENCIES macro. |
| 53 | */ |
| 54 | namespace QtUtilitiesResources { |
| 55 | |
| 56 | /*! |
| 57 | * \brief Initiates the resources used and provided by this library. |
| 58 | * \deprecated Replaced by ENABLE_QT_RESOURCES_OF_STATIC_DEPENDENCIES macro. |
| 59 | */ |
| 60 | void init() |
| 61 | { |
| 62 | initResources(); |
| 63 | } |
| 64 | |
| 65 | /*! |
| 66 | * \brief Frees the resources used and provided by this library. |
| 67 | * \deprecated Replaced by ENABLE_QT_RESOURCES_OF_STATIC_DEPENDENCIES macro. |
| 68 | */ |
| 69 | void cleanup() |
| 70 | { |
| 71 | cleanupResources(); |
| 72 | } |
| 73 | } // namespace QtUtilitiesResources |
| 74 | |
| 75 | /*! |
| 76 | * \brief Convenience functions to load translations for Qt and the application. |
| 77 | */ |
| 78 | namespace TranslationFiles { |
| 79 | |
| 80 | /*! |
| 81 | * \brief The translators installed via the load-functions in this namespace. |
| 82 | */ |
| 83 | static QList<QTranslator *> translators; |
| 84 | |
| 85 | /*! |
| 86 | * \brief Allows to set an additional search path for translation files. |
| 87 | * \remarks This path is considered *before* the default directories. |
| 88 | */ |
| 89 | QString &additionalTranslationFilePath() |
| 90 | { |
| 91 | static QString path; |
| 92 | return path; |
| 93 | } |
| 94 | |
| 95 | /// \cond |
| 96 | static QString relativeBase() |
| 97 | { |
| 98 | static const auto relativeBase = [] { |
| 99 | auto appDir = QCoreApplication::applicationDirPath(); |
| 100 | if (appDir.isEmpty()) { |
| 101 | appDir = QStringLiteral("."); |
| 102 | } |
| 103 | return appDir; |
| 104 | }(); |
| 105 | return relativeBase; |
| 106 | } |
| 107 | /// \endcond |
| 108 | |
| 109 | /*! |
| 110 | * \brief Loads and installs the appropriate Qt translation file for the current |
| 111 | * locale. |
| 112 | * \param repositoryNames Specifies the names of the Qt repositories to load |
| 113 | * translations for (e.g. "qtbase", "qtdeclarative", ...). |
| 114 | * \remarks |
| 115 | * - Translation files have to be placed in one of the following locations: |
| 116 | * * QLibraryInfo::location(QLibraryInfo::TranslationsPath) (used in UNIX) |
| 117 | * * ../share/qt/translations (used in Windows) |
| 118 | * - Translation files can also be built-in using by setting the CMake variable |
| 119 | * BUILTIN_TRANSLATIONS. |
| 120 | * In this case it is also necessary to load the translations using this |
| 121 | * function. |
| 122 | */ |
| 123 | void loadQtTranslationFile(std::initializer_list<QString> repositoryNames) |
| 124 | { |
| 125 | loadQtTranslationFile(repositoryNames, QLocale().name()); |
| 126 | } |
| 127 | |
| 128 | /*! |
| 129 | * \brief Loads and installs the appropriate Qt translation file for the |
| 130 | * specified locale. |
| 131 | * \param repositoryNames Specifies the names of the Qt repositories to load |
| 132 | * translations for (e.g. "qtbase", "qtdeclarative", ...). |
| 133 | * \param localeName Specifies the name of the locale. |
| 134 | * \remarks |
| 135 | * - Translation files have to be placed in one of the following locations: |
| 136 | * * QLibraryInfo::location(QLibraryInfo::TranslationsPath) (used in UNIX) |
| 137 | * * ../share/qt/translations (used in Windows) |
| 138 | * - Translation files can also be built-in using by setting the CMake variable |
| 139 | * BUILTIN_TRANSLATIONS. |
| 140 | * In this case it is also necessary to load the translations using this |
| 141 | * function. |
| 142 | */ |
| 143 | void loadQtTranslationFile(initializer_list<QString> repositoryNames, const QString &localeName) |
| 144 | { |
| 145 | const auto debugTranslations = qEnvironmentVariableIntValue("QT_DEBUG_TRANSLATIONS"); |
| 146 | const auto relBase = relativeBase(); |
| 147 | for (const auto &repoName : repositoryNames) { |
| 148 | auto *const qtTranslator = new QTranslator(QCoreApplication::instance()); |
| 149 | const auto fileName = QString(repoName % QChar('_') % localeName); |
| 150 | |
| 151 | QString path; |
| 152 | if ( |
| 153 | // allow putting translations into configurable directory |
| 154 | (!additionalTranslationFilePath().isEmpty() && qtTranslator->load(fileName, path = additionalTranslationFilePath())) |
| 155 | // allow putting translations next to the Qt libraries |
| 156 | || qtTranslator->load(fileName, |
| 157 | path = |
| 158 | #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) |
| 159 | QLibraryInfo::location(QLibraryInfo::TranslationsPath) |
| 160 | #else |
| 161 | QLibraryInfo::path(QLibraryInfo::TranslationsPath) |
| 162 | #endif |
| 163 | ) |
| 164 | // load translations from the "share" directory to support installations following the usual UNIX file system hierarchy |
| 165 | || qtTranslator->load(fileName, path = relBase + QStringLiteral("/../share/qt/translations")) |
| 166 | // load built-in translations |
| 167 | || qtTranslator->load(fileName, path = QStringLiteral(":/translations"))) { |
| 168 | QCoreApplication::installTranslator(qtTranslator); |
| 169 | translators.append(qtTranslator); |
| 170 | if (debugTranslations) { |
| 171 | cerr << "Loading translation file for Qt repository \"" << repoName.toLocal8Bit().data() << "\" and the locale \"" |
| 172 | << localeName.toLocal8Bit().data() << "\" from \"" << path.toLocal8Bit().data() << "\"." << endl; |
| 173 | } |
| 174 | } else { |
| 175 | delete qtTranslator; |
| 176 | if (localeName.startsWith(QLatin1String("en"))) { |
| 177 | // the translation file is probably just empty (English is built-in and usually only used for plural forms) |
| 178 | continue; |
| 179 | } |
| 180 | #if defined(Q_OS_ANDROID) |
| 181 | qDebug() << "Unable to load translation file for Qt repository: " << repoName << localeName; |
| 182 | #else |
| 183 | cerr << "Unable to load translation file for Qt repository \"" << repoName.toLocal8Bit().data() << "\" and locale " |
| 184 | << localeName.toLocal8Bit().data() << "." << endl; |
| 185 | #endif |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /*! |
| 191 | * \brief Loads and installs the appropriate application translation file for |
| 192 | * the current locale. |
| 193 | * \param configName Specifies the name of the build configuration, e.g. an empty string or "debug-qt6". |
| 194 | * \param applicationName Specifies the name of the application or additional library, e.g. "tageditor" or "qtutilities". |
| 195 | * \remarks |
| 196 | * - Translation files have to be placed in one of the following locations: |
| 197 | * * ./ |
| 198 | * * ../$application |
| 199 | * * ../../$application |
| 200 | * * ./translations |
| 201 | * * ../share/$application/translations |
| 202 | * * $install_prefix/share/$application/translations |
| 203 | * - Translation files must be named using the following scheme: |
| 204 | * * $application_$language.qm |
| 205 | * - Translation files can also be built-in using by setting the CMake variable |
| 206 | * BUILTIN_TRANSLATIONS. |
| 207 | * In this case it is also necessary to load the translations using this |
| 208 | * function. |
| 209 | */ |
| 210 | void loadApplicationTranslationFile(const QString &configName, const QString &applicationName) |
| 211 | { |
| 212 | // load English translation files as fallback |
| 213 | loadApplicationTranslationFile(configName, applicationName, QStringLiteral("en_US")); |
| 214 | // load translation files for current locale |
| 215 | const auto defaultLocale(QLocale().name()); |
| 216 | if (defaultLocale != QLatin1String("en_US")) { |
| 217 | loadApplicationTranslationFile(configName, applicationName, defaultLocale); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /// \cond |
| 222 | static void logTranslationEvent( |
| 223 | const char *event, const QString &configName, const QString &applicationName, const QString &localeName, const QString &path = QString()) |
| 224 | { |
| 225 | #if defined(Q_OS_ANDROID) |
| 226 | qDebug() << CppUtilities::argsToString(event, " translation file for: ").data() << applicationName << localeName; |
| 227 | if (!configName.isEmpty()) { |
| 228 | qDebug() << "config: " << configName; |
| 229 | } |
| 230 | if (!path.isEmpty()) { |
| 231 | qDebug() << "path: " << path; |
| 232 | } |
| 233 | #else |
| 234 | cerr << event << " translation file for \"" << applicationName.toLocal8Bit().data() << "\""; |
| 235 | if (!configName.isEmpty()) { |
| 236 | cerr << " (config \"" << configName.toLocal8Bit().data() << "\")"; |
| 237 | } |
| 238 | cerr << " and locale \"" << localeName.toLocal8Bit().data() << '\"'; |
| 239 | if (!path.isEmpty()) { |
| 240 | cerr << " from \"" << path.toLocal8Bit().data() << '\"'; |
| 241 | } |
| 242 | cerr << '.' << endl; |
| 243 | #endif |
| 244 | } |
| 245 | /// \endcond |
| 246 | |
| 247 | /*! |
| 248 | * \brief Loads and installs the appropriate application translation file for |
| 249 | * the specified locale. |
| 250 | * \param configName Specifies the name of the build configuration, e.g. an empty string or "debug-qt6". |
| 251 | * \param applicationName Specifies the name of the application or additional library, e.g. "tageditor" or "qtutilities". |
| 252 | * \param localeName Specifies the name of the locale, e.g. "de_DE". |
| 253 | * \remarks |
| 254 | * - Translation files have to be placed in one of the following locations: |
| 255 | * * ./ |
| 256 | * * ../$application |
| 257 | * * ../../$application |
| 258 | * * ./translations |
| 259 | * * ../share/$application/translations |
| 260 | * * $install_prefix/share/$application/translations |
| 261 | * - Translation files must be named using the following scheme: |
| 262 | * * $application_$language.qm |
| 263 | * - Translation files can also be built-in using by setting the CMake variable |
| 264 | * BUILTIN_TRANSLATIONS. |
| 265 | * In this case it is also necessary to load the translations using this |
| 266 | * function. |
| 267 | */ |
| 268 | void loadApplicationTranslationFile(const QString &configName, const QString &applicationName, const QString &localeName) |
| 269 | { |
| 270 | auto *const appTranslator = new QTranslator(QCoreApplication::instance()); |
| 271 | const auto fileName = QString(applicationName % QChar('_') % localeName); |
| 272 | const auto directoryName = configName.isEmpty() ? applicationName : QString(applicationName % QChar('-') % configName); |
| 273 | const auto relBase = relativeBase(); |
| 274 | |
| 275 | if (auto path = QString(); |
| 276 | // allow putting translations into configurable directory |
| 277 | (!additionalTranslationFilePath().isEmpty() && appTranslator->load(fileName, path = additionalTranslationFilePath())) |
| 278 | // allow putting translations next to the executable (useful during development and for easily supplying additional |
| 279 | // translations, e.g. under Windows) |
| 280 | || appTranslator->load(fileName, path = relBase) |
| 281 | // load translations from sibling directories (useful during development to load translations of e.g. qtutilities |
| 282 | // from its build directory) |
| 283 | || appTranslator->load(fileName, path = relBase % QStringLiteral("/../") % applicationName) |
| 284 | || appTranslator->load(fileName, path = relBase % QStringLiteral("/../../") % applicationName) |
| 285 | // allow putting translations next to the executable into "translations" subdirectory (useful for easily supplying |
| 286 | // additional translations, e.g. under Windows) |
| 287 | || appTranslator->load(fileName, path = relBase % QStringLiteral("/translations")) |
| 288 | // load translations from the "share" directory to support installations following the usual UNIX file system hierarchy |
| 289 | || appTranslator->load(fileName, path = relBase % QStringLiteral("/../share/") % directoryName % QStringLiteral("/translations")) |
| 290 | || appTranslator->load(fileName, path = QStringLiteral(APP_INSTALL_PREFIX "/share/") % directoryName % QStringLiteral("/translations")) |
| 291 | // load built-in translations |
| 292 | || appTranslator->load(fileName, path = QStringLiteral(":/translations"))) { |
| 293 | QCoreApplication::installTranslator(appTranslator); |
| 294 | translators.append(appTranslator); |
| 295 | if (qEnvironmentVariableIntValue("QT_DEBUG_TRANSLATIONS")) { |
| 296 | logTranslationEvent("Loading", configName, applicationName, localeName, path); |
| 297 | } |
| 298 | } else { |
| 299 | delete appTranslator; |
| 300 | if (localeName.startsWith(QLatin1String("en"))) { |
| 301 | // the translation file is probably just empty (English is built-in and usually only used for plural forms) |
| 302 | return; |
| 303 | } |
| 304 | logTranslationEvent("Unable to load", configName, applicationName, localeName); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /*! |
| 309 | * \brief Loads and installs the appropriate application translation file for |
| 310 | * the current locale. |
| 311 | * \param configName Specifies the name of the build configuration, e.g. an empty string or "debug-qt6". |
| 312 | * \param applicationNames Specifies the names of the applications and libraries, e.g. `{"qtutilities", "tageditor"}`. |
| 313 | */ |
| 314 | void loadApplicationTranslationFile(const QString &configName, const std::initializer_list<QString> &applicationNames) |
| 315 | { |
| 316 | for (const QString &applicationName : applicationNames) { |
| 317 | loadApplicationTranslationFile(configName, applicationName); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | /*! |
| 322 | * \brief Loads and installs the appropriate application translation file for |
| 323 | * the specified locale. |
| 324 | * \param configName Specifies the name of the build configuration, e.g. an empty string or "debug-qt6". |
| 325 | * \param applicationNames Specifies the names of the applications and libraries, e.g. `{"qtutilities", "tageditor"}`. |
| 326 | * \param localeName Specifies the name of the locale. |
| 327 | */ |
| 328 | void loadApplicationTranslationFile(const QString &configName, const std::initializer_list<QString> &applicationNames, const QString &localeName) |
| 329 | { |
| 330 | for (const QString &applicationName : applicationNames) { |
| 331 | loadApplicationTranslationFile(configName, applicationName, localeName); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | /*! |
| 336 | * \brief Clears all translation files previously loaded via the load-functions in this namespace. |
| 337 | */ |
| 338 | void clearTranslationFiles() |
| 339 | { |
| 340 | for (auto *const translator : translators) { |
| 341 | QCoreApplication::removeTranslator(translator); |
| 342 | } |
| 343 | translators.clear(); |
| 344 | } |
| 345 | |
| 346 | } // namespace TranslationFiles |
| 347 | |
| 348 | /*! |
| 349 | * \brief Convenience functions to check whether a |
| 350 | * QCoreApplication/QGuiApplication/QApplication singleton has been instantiated |
| 351 | * yet. |
| 352 | */ |
| 353 | namespace ApplicationInstances { |
| 354 | |
| 355 | #if defined(QT_UTILITIES_GUI_QTWIDGETS) |
| 356 | /*! |
| 357 | * \brief Returns whether a QApplication has been instantiated yet. |
| 358 | */ |
| 359 | bool hasWidgetsApp() |
| 360 | { |
| 361 | return qobject_cast<QApplication *>(QCoreApplication::instance()) != nullptr; |
| 362 | } |
| 363 | #endif |
| 364 | |
| 365 | #if defined(QT_UTILITIES_GUI_QTWIDGETS) || defined(QT_UTILITIES_GUI_QTQUICK) |
| 366 | /*! |
| 367 | * \brief Returns whether a QGuiApplication has been instantiated yet. |
| 368 | */ |
| 369 | bool hasGuiApp() |
| 370 | { |
| 371 | return qobject_cast<QGuiApplication *>(QCoreApplication::instance()) != nullptr; |
| 372 | } |
| 373 | #endif |
| 374 | |
| 375 | /*! |
| 376 | * \brief Returns whether a QCoreApplication has been instantiated yet. |
| 377 | */ |
| 378 | bool hasCoreApp() |
| 379 | { |
| 380 | return qobject_cast<QCoreApplication *>(QCoreApplication::instance()) != nullptr; |
| 381 | } |
| 382 | } // namespace ApplicationInstances |
| 383 | |
| 384 | /*! |
| 385 | * \brief Sets Qt application attributes which are commonly used within my Qt applications. |
| 386 | * \remarks |
| 387 | * - So far this enables High-DPI support. |
| 388 | * - The exact attributes are unspecified and might change to whatever makes sense in the future. |
| 389 | */ |
| 390 | void setupCommonQtApplicationAttributes() |
| 391 | { |
| 392 | // enable dark window frame on Windows if the configured color palette is dark |
| 393 | // - supported as of Qt 6.4; no longer required as of Qt 6.5 |
| 394 | // - see https://bugreports.qt.io/browse/QTBUG-72028?focusedCommentId=677819#comment-677819 |
| 395 | // and https://www.qt.io/blog/dark-mode-on-windows-11-with-qt-6.5 |
| 396 | #if defined(Q_OS_WINDOWS) && (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) && (QT_VERSION < QT_VERSION_CHECK(6, 5, 0)) |
| 397 | if (const auto qtVersion = QLibraryInfo::version(); |
| 398 | qtVersion >= QVersionNumber(6, 4, 0) && qtVersion < QVersionNumber(6, 5, 0) && !qEnvironmentVariableIsSet("QT_QPA_PLATFORM")) { |
| 399 | qputenv("QT_QPA_PLATFORM", "windows:darkmode=1"); |
| 400 | } |
| 401 | #endif |
| 402 | |
| 403 | // ensure FONTCONFIG_PATH is set (mainly required for static GNU/Linux builds) |
| 404 | #ifdef QT_FEATURE_fontdialog |
| 405 | if (!qEnvironmentVariableIsSet("FONTCONFIG_PATH") && QDir(QStringLiteral("/etc/fonts")).exists()) { |
| 406 | qputenv("FONTCONFIG_PATH", "/etc/fonts"); |
| 407 | } |
| 408 | #endif |
| 409 | |
| 410 | // enable settings for High-DPI scaling |
| 411 | #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) |
| 412 | if (!QCoreApplication::instance()) { |
| 413 | QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true); |
| 414 | } |
| 415 | QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, true); |
| 416 | #endif |
| 417 | } |
| 418 | |
| 419 | /*! |
| 420 | * \brief Returns the settings object for the specified \a organization and \a application. |
| 421 | * \remarks |
| 422 | * - This function always uses INI as that's what I'd like to use in all of my applications consistently, regardless of the platform. |
| 423 | * - The parameter \a application might be empty. In fact, most of my applications use just `getSettings(QStringLiteral(PROJECT_NAME))`. |
| 424 | * - This function first checks whether a file called `$organization/$application.ini` exists in the current working directory or next |
| 425 | * to the executable (or just `$organization.ini` if \a application is empty) and uses that if it exists. That allows having a portable |
| 426 | * installation. |
| 427 | * - Some of my apps where using values from QCoreApplication for \a organization and \a application in the beginning. This function |
| 428 | * moves those old config files to their new location if needed. This extra handling will likely removed at some point. Note that |
| 429 | * I moved away from using values from QCoreApplication to avoid having spaces and additional config suffixes in the file name. |
| 430 | */ |
| 431 | std::unique_ptr<QSettings> getSettings(const QString &organization, const QString &application) |
| 432 | { |
| 433 | auto settings = std::unique_ptr<QSettings>(); |
| 434 | const auto portableFileName |
| 435 | = application.isEmpty() ? organization + QStringLiteral(".ini") : organization % QChar('/') % application % QStringLiteral(".ini"); |
| 436 | if (const auto portableFileWorkingDir = QFile(portableFileName); portableFileWorkingDir.exists()) { |
| 437 | settings = std::make_unique<QSettings>(portableFileWorkingDir.fileName(), QSettings::IniFormat); |
| 438 | } else if (const auto portableFileNextToApp = QFile(QCoreApplication::applicationDirPath() % QChar('/') % portableFileName); |
| 439 | portableFileNextToApp.exists()) { |
| 440 | settings = std::make_unique<QSettings>(portableFileNextToApp.fileName(), QSettings::IniFormat); |
| 441 | } else { |
| 442 | settings = std::make_unique<QSettings>(QSettings::IniFormat, QSettings::UserScope, organization, application); |
| 443 | // move config created by older versions to new location |
| 444 | if (organization != QCoreApplication::organizationName() || application != QCoreApplication::applicationName()) { |
| 445 | const auto oldConfig |
| 446 | = QSettings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName()) |
| 447 | .fileName(); |
| 448 | QFile::rename(oldConfig, settings->fileName()) || QFile::remove(oldConfig); |
| 449 | } |
| 450 | } |
| 451 | loadSettingsWithLogging(*settings); |
| 452 | return settings; |
| 453 | } |
| 454 | |
| 455 | /*! |
| 456 | * \brief Loads settings and logs a corresponding message if the env variable QT_DEBUG_SETTINGS is set. |
| 457 | */ |
| 458 | void loadSettingsWithLogging(QSettings &settings) |
| 459 | { |
| 460 | settings.sync(); |
| 461 | if (!qEnvironmentVariableIntValue("QT_DEBUG_SETTINGS")) { |
| 462 | return; |
| 463 | } |
| 464 | const auto error = errorMessageForSettings(settings); |
| 465 | if (error.isEmpty()) { |
| 466 | std::cerr << "Loaded/synced settings from " << settings.fileName().toStdString() << '\n'; |
| 467 | } else { |
| 468 | std::cerr << "Unable to load settings: " << error.toStdString() << '\n'; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | /*! |
| 473 | * \brief Saves settings and logs a corresponding message if the env variable QT_DEBUG_SETTINGS is set. |
| 474 | */ |
| 475 | void saveSettingsWithLogging(QSettings &settings) |
| 476 | { |
| 477 | settings.sync(); |
| 478 | if (!qEnvironmentVariableIntValue("QT_DEBUG_SETTINGS")) { |
| 479 | return; |
| 480 | } |
| 481 | const auto error = errorMessageForSettings(settings); |
| 482 | if (error.isEmpty()) { |
| 483 | std::cerr << "Saved/synced settings to " << settings.fileName().toStdString() << '\n'; |
| 484 | } else { |
| 485 | std::cerr << "Unable to save settings: " << error.toStdString() << '\n'; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | /*! |
| 490 | * \brief Returns an error message for the specified \a settings or an empty string if there's no error. |
| 491 | */ |
| 492 | QString errorMessageForSettings(const QSettings &settings) |
| 493 | { |
| 494 | auto errorMessage = QString(); |
| 495 | switch (settings.status()) { |
| 496 | case QSettings::NoError: |
| 497 | return QString(); |
| 498 | case QSettings::AccessError: |
| 499 | errorMessage = QCoreApplication::translate("QtUtilities", "unable to access file"); |
| 500 | break; |
| 501 | case QSettings::FormatError: |
| 502 | errorMessage = QCoreApplication::translate("QtUtilities", "file has invalid format"); |
| 503 | break; |
| 504 | default: |
| 505 | errorMessage = QCoreApplication::translate("QtUtilities", "unknown error"); |
| 506 | } |
| 507 | return QCoreApplication::translate("QtUtilities", "Unable to sync settings from \"%1\": %2").arg(settings.fileName(), errorMessage); |
| 508 | } |
| 509 | |
| 510 | /*! |
| 511 | * \brief Deletes the Qt Quick pipeline cache on platforms where this is needed to workaround issues with the cache. |
| 512 | */ |
| 513 | void deletePipelineCacheIfNeeded() |
| 514 | { |
| 515 | #ifdef Q_OS_ANDROID |
| 516 | // delete OpenGL pipeline cache under Android as it seems to break loading the app in certain cases |
| 517 | const auto cachePaths = QStandardPaths::standardLocations(QStandardPaths::CacheLocation); |
| 518 | for (const auto &cachePath : cachePaths) { |
| 519 | const auto cacheDir = QDir(cachePath); |
| 520 | const auto subdirs = cacheDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot); |
| 521 | for (const auto &subdir : subdirs) { |
| 522 | if (subdir.startsWith(QLatin1String("qtpipelinecache"))) { |
| 523 | QFile::remove(cachePath % QChar('/') % subdir % QStringLiteral("/qqpc_opengl")); |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | #endif |
| 528 | } |
| 529 | |
| 530 | } // namespace QtUtilities |