From 55cbcd38d25b37cd8100e5177d1b7cb455b9af55 Mon Sep 17 00:00:00 2001
From: Ming Rui Zhang <mingrui.zhang@savoirfairelinux.com>
Date: Wed, 30 Sep 2020 13:39:21 -0400
Subject: [PATCH] misc: using QRegExp to remove file url prefix

Remove file url prefix regardless of two or three slashs (file:// or file:///)

Gitlab: #113
Change-Id: Ia3600336fcc727a32646ee415243c1833e6c1e41
---
 src/commoncomponents/PreferenceItemDelegate.qml |  2 +-
 src/messagesadapter.cpp                         | 11 ++++++-----
 src/utilsadapter.cpp                            |  4 ++--
 3 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/src/commoncomponents/PreferenceItemDelegate.qml b/src/commoncomponents/PreferenceItemDelegate.qml
index 34a62d51a..8b83b08cd 100644
--- a/src/commoncomponents/PreferenceItemDelegate.qml
+++ b/src/commoncomponents/PreferenceItemDelegate.qml
@@ -78,7 +78,7 @@ ItemDelegate {
         id: preferenceFilePathDialog
 
         title: JamiStrings.selectFile
-        folder: "file://" + currentPath
+        folder: "file:///" + currentPath
 
         onAccepted: {
             var url = UtilsAdapter.getAbsPath(fileUrl.toString())
diff --git a/src/messagesadapter.cpp b/src/messagesadapter.cpp
index 8238f5cb8..810305dcf 100644
--- a/src/messagesadapter.cpp
+++ b/src/messagesadapter.cpp
@@ -262,11 +262,12 @@ MessagesAdapter::sendImage(const QString& message)
          * Img tag contains file paths.
          */
 
+        // TODO: put all QRegExp strings together
         QString msg(message);
 #ifdef Q_OS_WIN
-        msg = msg.replace("file://", "");
+        msg = msg.replace(QRegExp("^file:\\/{2,3}"), "");
 #else
-        msg = msg.replace("file://", "/");
+        msg = msg.replace(QRegExp("^file:\\/{2,3}"), "/");
 #endif
         QFileInfo fi(msg);
         QString fileName = fi.fileName();
@@ -337,7 +338,7 @@ MessagesAdapter::deleteInteraction(const QString& arg)
 void
 MessagesAdapter::openFile(const QString& arg)
 {
-    QUrl fileUrl("file://" + arg);
+    QUrl fileUrl("file:///" + arg);
     if (!QDesktopServices::openUrl(fileUrl)) {
         qDebug() << "Couldn't open file: " << fileUrl;
     }
@@ -399,9 +400,9 @@ MessagesAdapter::pasteKeyDetected()
          */
         for (int i = 0; i < urlList.size(); ++i) {
             /*
-             * Trim file:// from url.
+             * Trim file:// or file:/// from url.
              */
-            QString filePath = urlList.at(i).toString().remove("file://");
+            QString filePath = urlList.at(i).toString().remove(QRegExp("^file:\\/{2,3}"));
             QByteArray imageFormat = QImageReader::imageFormat(filePath);
 
             /*
diff --git a/src/utilsadapter.cpp b/src/utilsadapter.cpp
index 309f8f99b..d69d7049d 100644
--- a/src/utilsadapter.cpp
+++ b/src/utilsadapter.cpp
@@ -350,9 +350,9 @@ UtilsAdapter::getAbsPath(QString path)
     // contain 'file:///' for reasons we don't understand.
     // TODO: this logic can be refactored into the JamiFileDialog component.
 #ifdef Q_OS_WIN
-    return path.replace("file:///", "").replace("\n", "").replace("\r", "");
+    return path.replace(QRegExp("^file:\\/{2,3}"), "").replace("\n", "").replace("\r", "");
 #else
-    return path.replace("file:///", "/").replace("\n", "").replace("\r", "");
+    return path.replace(QRegExp("^file:\\/{2,3}"), "/").replace("\n", "").replace("\r", "");
 #endif
 }
 
-- 
GitLab