File indexing completed on 2024-05-19 05:06:55

0001 /*
0002     SPDX-FileCopyrightText: 2021 Dawid Wróbel <me@dawidwrobel.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 
0008 // ----------------------------------------------------------------------------
0009 // QT Includes
0010 
0011 #include <QRegularExpression>
0012 
0013 // ----------------------------------------------------------------------------
0014 // Project Includes
0015 
0016 #include "kmmurl.h"
0017 
0018 #undef QUrl
0019 
0020 KMMUrl::KMMUrl()
0021     : QUrl()
0022 {}
0023 
0024 KMMUrl::KMMUrl(const KMMUrl &copy)
0025     : QUrl(copy)
0026 {}
0027 
0028 KMMUrl::KMMUrl(const QUrl &copy)
0029     : QUrl(copy)
0030 {}
0031 
0032 KMMUrl KMMUrl::fromUserInput(const QString &userInput)
0033 {
0034     auto properUrl = normalizeUrlString(userInput);
0035 
0036     if (userInput.at(0) == ':') {
0037         auto properQUrl = QUrl::fromLocalFile(properUrl.remove(0, 1));
0038         properQUrl.setScheme(QStringLiteral("qrc"));
0039 
0040         return properQUrl;
0041     }
0042     else {
0043         return QUrl::fromUserInput(properUrl);
0044     }
0045 }
0046 
0047 QString KMMUrl::toLocalFile() const
0048 {
0049     if (this->scheme() == QStringLiteral("qrc") || this->url().startsWith(QStringLiteral("qrc"))) {
0050         return normalizeUrlString(this->url());
0051     }
0052     else {
0053         return QUrl::toLocalFile();
0054     }
0055 }
0056 
0057 QString KMMUrl::normalizeUrlString(const QString &url)
0058 {
0059     static const QRegularExpression regex("^(file:|qrc)", QRegularExpression::CaseInsensitiveOption);
0060     return QString(url).remove(regex);
0061 }
0062