From f8b997bcf4755bbc847f25d440a9db5bddb9e364 Mon Sep 17 00:00:00 2001 From: Weslly Date: Fri, 21 Sep 2018 00:49:56 -0300 Subject: [PATCH] Add support for opening file:// urls (#2311) * Add support for opening file:// urls * Open file urls without file:// scheme --- src/core/Entry.cpp | 19 +++++++++++++------ tests/TestEntry.cpp | 7 +++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/core/Entry.cpp b/src/core/Entry.cpp index 4233d89e3..bc394b227 100644 --- a/src/core/Entry.cpp +++ b/src/core/Entry.cpp @@ -26,6 +26,7 @@ #include "totp/totp.h" #include +#include #include const int Entry::DefaultIconNumber = 0; @@ -986,12 +987,13 @@ Entry::PlaceholderType Entry::placeholderType(const QString& placeholder) const QString Entry::resolveUrl(const QString& url) const { QString newUrl = url; - if (!url.isEmpty() && !url.contains("://")) { - // URL doesn't have a protocol, add https by default - newUrl.prepend("https://"); - } - if (newUrl.startsWith("cmd://")) { + QRegExp fileRegEx("^([a-z]:)?[\\\\/]", Qt::CaseInsensitive, QRegExp::RegExp2); + if (fileRegEx.indexIn(newUrl) != -1) { + // Match possible file paths without the scheme and convert it to a file URL + newUrl = QDir::fromNativeSeparators(newUrl); + newUrl = QUrl::fromLocalFile(newUrl).toString(); + } else if (newUrl.startsWith("cmd://")) { QStringList cmdList = newUrl.split(" "); for (int i = 1; i < cmdList.size(); ++i) { // Don't pass arguments to the resolveUrl function (they look like URL's) @@ -1004,9 +1006,14 @@ QString Entry::resolveUrl(const QString& url) const return QString(""); } + if (!newUrl.isEmpty() && !newUrl.contains("://")) { + // URL doesn't have a protocol, add https by default + newUrl.prepend("https://"); + } + // Validate the URL QUrl tempUrl = QUrl(newUrl); - if (tempUrl.isValid() && (tempUrl.scheme() == "http" || tempUrl.scheme() == "https")) { + if (tempUrl.isValid() && (tempUrl.scheme() == "http" || tempUrl.scheme() == "https" || tempUrl.scheme() == "file")) { return tempUrl.url(); } diff --git a/tests/TestEntry.cpp b/tests/TestEntry.cpp index 5e5d7bc3c..5c3cde618 100644 --- a/tests/TestEntry.cpp +++ b/tests/TestEntry.cpp @@ -148,6 +148,8 @@ void TestEntry::testResolveUrl() QScopedPointer entry(new Entry()); QString testUrl("www.google.com"); QString testCmd("cmd://firefox " + testUrl); + QString testFileUnix("/home/example/test.txt"); + QString testFileWindows("c:/WINDOWS/test.txt"); QString testComplexCmd("cmd://firefox --start-now --url 'http://" + testUrl + "' --quit"); QString nonHttpUrl("ftp://google.com"); QString noUrl("random text inserted here"); @@ -156,6 +158,11 @@ void TestEntry::testResolveUrl() QCOMPARE(entry->resolveUrl(""), QString("")); QCOMPARE(entry->resolveUrl(testUrl), "https://" + testUrl); QCOMPARE(entry->resolveUrl("http://" + testUrl), "http://" + testUrl); + // Test file:// URL's + QCOMPARE(entry->resolveUrl("file://" + testFileUnix), "file://" + testFileUnix); + QCOMPARE(entry->resolveUrl(testFileUnix), "file://" + testFileUnix); + QCOMPARE(entry->resolveUrl("file:///" + testFileWindows), "file:///" + testFileWindows); + QCOMPARE(entry->resolveUrl(testFileWindows), "file:///" + testFileWindows); // Test cmd:// with no URL QCOMPARE(entry->resolveUrl("cmd://firefox"), QString("")); QCOMPARE(entry->resolveUrl("cmd://firefox --no-url"), QString(""));