diff options
author | Peter Varga <pvarga@inf.u-szeged.hu> | 2022-07-20 12:32:39 +0200 |
---|---|---|
committer | Peter Varga <pvarga@inf.u-szeged.hu> | 2022-08-10 16:54:50 +0200 |
commit | 0af49e22a5364bb898308d9c931df711711bcb98 (patch) | |
tree | c00a10288b5693e6226c1f810acae55a2e3a063e | |
parent | d1cb5bab9c64ff5321aa2f7a7b33aefc8959c569 (diff) |
Add environment variables to specify resources and locales locations
- QTWEBENGINE_RESOURCES_PATH for resources
- QTWEBENGINE_LOCALES_PATH for locales (translations)
Fixes: QTBUG-88595
Fixes: QTBUG-78646
Task-number: QTBUG-103760
Change-Id: I9208ab8cc154d89eae6053eb3c50f007be5ffbb1
Reviewed-by: Jörg Bornemann <joerg.bornemann@qt.io>
Reviewed-by: Michal Klocek <michal.klocek@qt.io>
(cherry picked from commit f63f7903c9400b772f9566bc4b8c7d8e527314a9)
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
-rw-r--r-- | src/core/doc/src/qtwebengine-deploying.qdoc | 7 | ||||
-rw-r--r-- | src/core/web_engine_library_info.cpp | 160 |
2 files changed, 122 insertions, 45 deletions
diff --git a/src/core/doc/src/qtwebengine-deploying.qdoc b/src/core/doc/src/qtwebengine-deploying.qdoc index 383f6828b..3fb46a672 100644 --- a/src/core/doc/src/qtwebengine-deploying.qdoc +++ b/src/core/doc/src/qtwebengine-deploying.qdoc @@ -105,8 +105,12 @@ \li On Linux and Windows: the \c resources directory in the directory specified by QLibraryInfo::location(QLibraryInfo::DataPath) \li On \macos: \c .app/Content/Resources + \li The application directory specified by QCoreApplication::applicationDirPath() \endlist + Alternatively, a resources directory path can be set as a value of the + \c QTWEBENGINE_RESOURCES_PATH environment variable. + \section2 Translations Locale data (such as \c en-US.pak) is searched form the following locations: @@ -118,6 +122,9 @@ QLibraryInfo::location(QLibraryInfo::TranslationsPath) \endlist + Alternatively, a locales directory path can be set as a value of the + \c QTWEBENGINE_LOCALES_PATH environment variable. + \section2 JavaScript Files in Qt Resource Files If your WebEngine application is built using the Qt Quick Compiler, and the application ships diff --git a/src/core/web_engine_library_info.cpp b/src/core/web_engine_library_info.cpp index 5dea8d2aa..839ecadf9 100644 --- a/src/core/web_engine_library_info.cpp +++ b/src/core/web_engine_library_info.cpp @@ -23,6 +23,7 @@ #include <QFileInfo> #include <QLibraryInfo> #include <QLocale> +#include <QLoggingCategory> #include <QStandardPaths> #if defined(Q_OS_WIN) @@ -35,6 +36,8 @@ using namespace QtWebEngineCore; +Q_LOGGING_CATEGORY(webEngineLibraryInfoLog, "qt.webengine.libraryinfo") + namespace { QString fallbackDir() { @@ -145,11 +148,25 @@ QString subProcessPath() for (const QString &candidate : qAsConst(candidatePaths)) { if (QFileInfo::exists(candidate)) { processPath = candidate; + qCDebug(webEngineLibraryInfoLog, "Qt WebEngine process path: %s", + qPrintable(candidate)); break; } } - if (processPath.isEmpty()) - qFatal("Could not find %s", processBinary.toUtf8().constData()); + if (processPath.isEmpty()) { + QStringList errorMessage; + errorMessage.append( + QStringLiteral("The following paths were searched for Qt WebEngine Process:")); + for (const QString &candidate : qAsConst(candidatePaths)) + errorMessage.append(QStringLiteral(" ") % candidate); + errorMessage.append(QStringLiteral("but could not find it.")); + if (fromEnv.isEmpty()) { + errorMessage.append( + QStringLiteral("You may override the default search path by using " + "QTWEBENGINEPROCESS_PATH environment variable.")); + } + qFatal("%s", qPrintable(errorMessage.join('\n'))); + } #if defined(Q_OS_WIN) base::CommandLine *parsedCommandLine = base::CommandLine::ForCurrentProcess(); @@ -167,23 +184,51 @@ QString subProcessPath() QString localesPath() { - static bool initialized = false; - static QString potentialLocalesPath = + static QString potentialLocalesPath; + if (potentialLocalesPath.isEmpty()) { + QStringList candidatePaths; + const QString translationPakFilename = + QLatin1String(WebEngineLibraryInfo::getResolvedLocale() + ".pak"); + const QString fromEnv = qEnvironmentVariable("QTWEBENGINE_LOCALES_PATH"); + if (!fromEnv.isEmpty()) { + // Only search in QTWEBENGINE_LOCALES_PATH if set + candidatePaths << fromEnv; + } else { #if defined(Q_OS_DARWIN) && defined(QT_MAC_FRAMEWORK_BUILD) - getResourcesPath(frameworkBundle()) % QLatin1String("/qtwebengine_locales"); -#else - QLibraryInfo::path(QLibraryInfo::TranslationsPath) % QDir::separator() % QLatin1String("qtwebengine_locales"); + candidatePaths << getResourcesPath(frameworkBundle()) % QDir::separator() + % QLatin1String("qtwebengine_locales"); #endif + candidatePaths << QLibraryInfo::path(QLibraryInfo::TranslationsPath) % QDir::separator() + % QLatin1String("qtwebengine_locales"); + candidatePaths << fallbackDir(); + } - if (!initialized) { - initialized = true; - if (!QFileInfo::exists(potentialLocalesPath)) { - qWarning("Installed Qt WebEngine locales directory not found at location %s. Trying application directory...", qPrintable(potentialLocalesPath)); - potentialLocalesPath = QCoreApplication::applicationDirPath() % QDir::separator() % QLatin1String("qtwebengine_locales"); + for (const QString &candidate : qAsConst(candidatePaths)) { + if (QFileInfo::exists(candidate % QDir::separator() % translationPakFilename)) { + potentialLocalesPath = candidate; + qCDebug(webEngineLibraryInfoLog, "Qt WebEngine locales path: %s", + qPrintable(candidate)); + break; + } } - if (!QFileInfo::exists(potentialLocalesPath)) { - qWarning("Qt WebEngine locales directory not found at location %s. Trying fallback directory... Translations MAY NOT not be correct.", qPrintable(potentialLocalesPath)); - potentialLocalesPath = fallbackDir(); + + if (potentialLocalesPath.isEmpty()) { + QStringList warningMessage; + warningMessage.append( + QStringLiteral("The following paths were searched for Qt WebEngine locales:")); + for (const QString &candidate : qAsConst(candidatePaths)) + warningMessage.append(QStringLiteral(" ") % candidate); + warningMessage.append( + QStringLiteral( + "but could not find the translation file for the current locale: ") + % translationPakFilename); + if (fromEnv.isEmpty()) { + warningMessage.append( + QStringLiteral("You may override the default search paths by using " + "QTWEBENGINE_LOCALES_PATH environment variable.")); + } + warningMessage.append(QStringLiteral("Translations WILL NOT be correct.")); + qWarning("%s", qPrintable(warningMessage.join('\n'))); } } @@ -229,6 +274,8 @@ QString dictionariesPath() for (const QString &candidate : qAsConst(candidatePaths)) { if (QFileInfo::exists(candidate)) { potentialDictionariesPath = candidate; + qCDebug(webEngineLibraryInfoLog, "Qt WebEngine dictionaries path: %s", + qPrintable(candidate)); break; } } @@ -238,28 +285,49 @@ QString dictionariesPath() } #endif // QT_CONFIG(webengine_spellchecker) -QString resourcesDataPath() +QString resourcesPath() { - static bool initialized = false; - static QString potentialResourcesPath = + static QString potentialResourcesPath; + if (potentialResourcesPath.isEmpty()) { + QStringList candidatePaths; + const QString resourcesPakFilename = QLatin1String("qtwebengine_resources.pak"); + const QString fromEnv = qEnvironmentVariable("QTWEBENGINE_RESOURCES_PATH"); + if (!fromEnv.isEmpty()) { + // Only search in QTWEBENGINE_RESOURCES_PATH if set + candidatePaths << fromEnv; + } else { #if defined(Q_OS_DARWIN) && defined(QT_MAC_FRAMEWORK_BUILD) - getResourcesPath(frameworkBundle()); -#else - QLibraryInfo::path(QLibraryInfo::DataPath) % QLatin1String("/resources"); + candidatePaths << getResourcesPath(frameworkBundle()); #endif - if (!initialized) { - initialized = true; - if (!QFileInfo::exists(potentialResourcesPath % QLatin1String("/qtwebengine_resources.pak"))) { - qWarning("Qt WebEngine resources not found at %s. Trying parent directory...", qPrintable(potentialResourcesPath)); - potentialResourcesPath = QLibraryInfo::path(QLibraryInfo::DataPath); + candidatePaths << QLibraryInfo::path(QLibraryInfo::DataPath) % QDir::separator() + % QLatin1String("resources"); + candidatePaths << QLibraryInfo::path(QLibraryInfo::DataPath); + candidatePaths << QCoreApplication::applicationDirPath(); + candidatePaths << fallbackDir(); } - if (!QFileInfo::exists(potentialResourcesPath % QLatin1String("/qtwebengine_resources.pak"))) { - qWarning("Qt WebEngine resources not found at %s. Trying application directory...", qPrintable(potentialResourcesPath)); - potentialResourcesPath = QCoreApplication::applicationDirPath(); + + for (const QString &candidate : qAsConst(candidatePaths)) { + if (QFileInfo::exists(candidate % QDir::separator() % resourcesPakFilename)) { + potentialResourcesPath = candidate; + qCDebug(webEngineLibraryInfoLog, "Qt WebEngine resources path: %s", + qPrintable(candidate)); + break; + } } - if (!QFileInfo::exists(potentialResourcesPath % QLatin1String("/qtwebengine_resources.pak"))) { - qWarning("Qt WebEngine resources not found at %s. Trying fallback directory... The application MAY NOT work.", qPrintable(potentialResourcesPath)); - potentialResourcesPath = fallbackDir(); + + if (potentialResourcesPath.isEmpty()) { + QStringList errorMessage; + errorMessage.append(QStringLiteral( + "The following paths were searched for Qt WebEngine resources:")); + for (const QString &candidate : qAsConst(candidatePaths)) + errorMessage.append(QStringLiteral(" ") % candidate); + errorMessage.append(QStringLiteral("but could not find any.")); + if (fromEnv.isEmpty()) { + errorMessage.append( + QStringLiteral("You may override the default search paths by using " + "QTWEBENGINE_RESOURCES_PATH environment variable.")); + } + qFatal("%s", qPrintable(errorMessage.join('\n'))); } } @@ -272,13 +340,13 @@ base::FilePath WebEngineLibraryInfo::getPath(int key) QString directory; switch (key) { case QT_RESOURCES_PAK: - return toFilePath(resourcesDataPath() % QLatin1String("/qtwebengine_resources.pak")); + return toFilePath(resourcesPath() % QLatin1String("/qtwebengine_resources.pak")); case QT_RESOURCES_100P_PAK: - return toFilePath(resourcesDataPath() % QLatin1String("/qtwebengine_resources_100p.pak")); + return toFilePath(resourcesPath() % QLatin1String("/qtwebengine_resources_100p.pak")); case QT_RESOURCES_200P_PAK: - return toFilePath(resourcesDataPath() % QLatin1String("/qtwebengine_resources_200p.pak")); + return toFilePath(resourcesPath() % QLatin1String("/qtwebengine_resources_200p.pak")); case QT_RESOURCES_DEVTOOLS_PAK: - return toFilePath(resourcesDataPath() % QLatin1String("/qtwebengine_devtools_resources.pak")); + return toFilePath(resourcesPath() % QLatin1String("/qtwebengine_devtools_resources.pak")); #if defined(Q_OS_DARWIN) && defined(QT_MAC_FRAMEWORK_BUILD) case QT_FRAMEWORK_BUNDLE: return toFilePath(getBundlePath(frameworkBundle())); @@ -298,7 +366,7 @@ base::FilePath WebEngineLibraryInfo::getPath(int key) directory = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); break; case base::DIR_QT_LIBRARY_DATA: - return toFilePath(resourcesDataPath()); + return toFilePath(resourcesPath()); case ui::DIR_LOCALES: return toFilePath(localesPath()); #if QT_CONFIG(webengine_spellchecker) @@ -323,14 +391,16 @@ std::u16string WebEngineLibraryInfo::getApplicationName() std::string WebEngineLibraryInfo::getResolvedLocale() { base::CommandLine *parsedCommandLine = base::CommandLine::ForCurrentProcess(); - if (parsedCommandLine->HasSwitch(switches::kLang)) { - return parsedCommandLine->GetSwitchValueASCII(switches::kLang); - } else { - const QString &locale = QLocale().bcp47Name(); - std::string resolvedLocale; - if (l10n_util::CheckAndResolveLocale(locale.toStdString(), &resolvedLocale)) - return resolvedLocale; - } + std::string locale; + if (parsedCommandLine->HasSwitch(switches::kLang)) + locale = parsedCommandLine->GetSwitchValueASCII(switches::kLang); + else + locale = QLocale().bcp47Name().toStdString(); + + std::string resolvedLocale; + if (l10n_util::CheckAndResolveLocale(locale, &resolvedLocale)) + return resolvedLocale; + return "en-US"; } |