From 16b93c2d7dad73c33a745dce2a2eb7eac0e9826e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michael=20Br=C3=BCning?= <michael.bruning@theqtcompany.com>
Date: Mon, 30 Mar 2015 12:41:03 +0200
Subject: [PATCH] Add a fallback for discovering plugins and translations.

This should fix problems with deployed applications and remove
the need to use a qt.conf file to configure the PluginsPath and
TranslationsPath in order to get for example ffmpegsumo loaded by
default.

It should also fix the creation of empty directories from the Chromium
layer.

Task-number: QTBUG-44331
Change-Id: I08bb5ac1b308820bed2c758b30ce43f1209038e1
Reviewed-by: Andras Becsi <andras.becsi@theqtcompany.com>
Reviewed-by: Peter Varga <pvarga@inf.u-szeged.hu>
---
 src/core/web_engine_library_info.cpp | 44 +++++++++++++++++++++++-----
 1 file changed, 37 insertions(+), 7 deletions(-)

diff --git a/src/core/web_engine_library_info.cpp b/src/core/web_engine_library_info.cpp
index ce862aae6..835fe8bba 100644
--- a/src/core/web_engine_library_info.cpp
+++ b/src/core/web_engine_library_info.cpp
@@ -60,6 +60,11 @@ using namespace QtWebEngineCore;
 
 namespace {
 
+QString fallbackDir() {
+    static QString directory = QDir::homePath() % QLatin1String("/.") % QCoreApplication::applicationName();
+    return directory;
+}
+
 QString location(QLibraryInfo::LibraryLocation path)
 {
 #if defined(Q_OS_BLACKBERRY)
@@ -173,7 +178,22 @@ QString pluginsPath()
 #if defined(OS_MACOSX) && defined(QT_MAC_FRAMEWORK_BUILD)
     return getPath(frameworkBundle()) % QLatin1String("/Libraries");
 #else
-    return location(QLibraryInfo::PluginsPath) % QLatin1String("/qtwebengine");
+    static bool initialized = false;
+    static QString potentialPluginsPath = location(QLibraryInfo::PluginsPath) % QDir::separator() % QLatin1String("qtwebengine");
+
+    if (!initialized) {
+        initialized = true;
+        if (!QFileInfo::exists(potentialPluginsPath)) {
+            qWarning("Installed Qt plugins directory not found at location %s. Trying application directory...", qPrintable(potentialPluginsPath));
+            potentialPluginsPath = QCoreApplication::applicationDirPath() % QDir::separator() % QLatin1String("qtwebengine");
+        }
+        if (!QFileInfo::exists(potentialPluginsPath)) {
+            qWarning("Qt WebEngine Plugins directory not found at location %s. Trying fallback directory. Plugins as for example video codecs MAY NOT work.", qPrintable(potentialPluginsPath));
+            potentialPluginsPath = fallbackDir();
+        }
+    }
+
+    return potentialPluginsPath;
 #endif
 }
 
@@ -182,13 +202,23 @@ QString localesPath()
 #if defined(OS_MACOSX) && defined(QT_MAC_FRAMEWORK_BUILD)
     return getResourcesPath(frameworkBundle()) % QLatin1String("/qtwebengine_locales");
 #else
-    return location(QLibraryInfo::TranslationsPath) % QDir::separator() % QLatin1String("qtwebengine_locales");
-#endif
-}
+    static bool initialized = false;
+    static QString potentialLocalesPath = location(QLibraryInfo::TranslationsPath) % QDir::separator() % QLatin1String("qtwebengine_locales");
 
-QString fallbackDir() {
-    static QString directory = QDir::homePath() % QLatin1String("/.") % QCoreApplication::applicationName();
-    return directory;
+    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");
+        }
+        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();
+        }
+    }
+
+    return potentialLocalesPath;
+#endif
 }
 
 } // namespace
-- 
GitLab