From 781133238af7c712d833d5fe617569d8756772ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Cie=C5=9Blak?= Date: Tue, 27 Aug 2024 15:19:22 +0200 Subject: [PATCH] ClipboardUtils: isValidImageUrl and isValidImageUrl excluded to UrlUtils --- ui/StatusQ/CMakeLists.txt | 2 ++ ui/StatusQ/include/StatusQ/clipboardutils.h | 2 -- ui/StatusQ/include/StatusQ/urlutils.h | 18 ++++++++++++ ui/StatusQ/src/clipboardutils.cpp | 16 ----------- ui/StatusQ/src/plugin.cpp | 3 ++ ui/StatusQ/src/urlutils.cpp | 31 +++++++++++++++++++++ ui/imports/utils/Utils.qml | 4 +-- 7 files changed, 56 insertions(+), 20 deletions(-) create mode 100644 ui/StatusQ/include/StatusQ/urlutils.h create mode 100644 ui/StatusQ/src/urlutils.cpp diff --git a/ui/StatusQ/CMakeLists.txt b/ui/StatusQ/CMakeLists.txt index ece1246d5e..8e964ec9e8 100644 --- a/ui/StatusQ/CMakeLists.txt +++ b/ui/StatusQ/CMakeLists.txt @@ -123,6 +123,7 @@ add_library(StatusQ SHARED include/StatusQ/sumaggregator.h include/StatusQ/systemutilsinternal.h include/StatusQ/undefinedfilter.h + include/StatusQ/urlutils.h include/StatusQ/writableproxymodel.h src/aggregator.cpp src/clipboardutils.cpp @@ -155,6 +156,7 @@ add_library(StatusQ SHARED src/sumaggregator.cpp src/systemutilsinternal.cpp src/undefinedfilter.cpp + src/urlutils.cpp src/writableproxymodel.cpp # wallet diff --git a/ui/StatusQ/include/StatusQ/clipboardutils.h b/ui/StatusQ/include/StatusQ/clipboardutils.h index 1a4644c2e1..864c21231c 100644 --- a/ui/StatusQ/include/StatusQ/clipboardutils.h +++ b/ui/StatusQ/include/StatusQ/clipboardutils.h @@ -52,8 +52,6 @@ public: return new ClipboardUtils; } - Q_INVOKABLE bool isValidImageUrl(const QUrl &url, const QStringList &acceptedExtensions) const; - Q_INVOKABLE qint64 getFileSize(const QUrl &url) const; Q_INVOKABLE void copyTextToClipboard(const QString& text); Q_INVOKABLE void clear(); diff --git a/ui/StatusQ/include/StatusQ/urlutils.h b/ui/StatusQ/include/StatusQ/urlutils.h new file mode 100644 index 0000000000..ae71d0226a --- /dev/null +++ b/ui/StatusQ/include/StatusQ/urlutils.h @@ -0,0 +1,18 @@ +#pragma once + +#include + +class QQmlEngine; +class QJSEngine; + +class UrlUtils : public QObject +{ + Q_OBJECT + +public: + static QObject* qmlInstance(QQmlEngine* engine, QJSEngine* scriptEngine); + + Q_INVOKABLE static bool isValidImageUrl(const QUrl &url, + const QStringList &acceptedExtensions); + Q_INVOKABLE static qint64 getFileSize(const QUrl &url); +}; diff --git a/ui/StatusQ/src/clipboardutils.cpp b/ui/StatusQ/src/clipboardutils.cpp index cabf9bcdd4..dc385e6144 100644 --- a/ui/StatusQ/src/clipboardutils.cpp +++ b/ui/StatusQ/src/clipboardutils.cpp @@ -72,22 +72,6 @@ QList ClipboardUtils::urls() const return m_clipboard->mimeData()->urls(); } -bool ClipboardUtils::isValidImageUrl(const QUrl& url, const QStringList& acceptedExtensions) const -{ - const auto strippedUrl = url.url(QUrl::RemoveAuthority | QUrl::RemoveFragment | QUrl::RemoveQuery); - return std::any_of(acceptedExtensions.constBegin(), acceptedExtensions.constEnd(), [strippedUrl](const auto & ext) { - return strippedUrl.endsWith(ext, Qt::CaseInsensitive); - }); -} - -qint64 ClipboardUtils::getFileSize(const QUrl& url) const -{ - if (url.isLocalFile()) { - return QFile(url.toLocalFile()).size(); - } - return 0; -} - void ClipboardUtils::copyTextToClipboard(const QString &text) { m_clipboard->setText(text); diff --git a/ui/StatusQ/src/plugin.cpp b/ui/StatusQ/src/plugin.cpp index 78d9b51e3d..e9b716d4d7 100644 --- a/ui/StatusQ/src/plugin.cpp +++ b/ui/StatusQ/src/plugin.cpp @@ -30,6 +30,7 @@ #include "StatusQ/sumaggregator.h" #include "StatusQ/systemutilsinternal.h" #include "StatusQ/undefinedfilter.h" +#include "StatusQ/urlutils.h" #include "StatusQ/writableproxymodel.h" #include "wallet/managetokenscontroller.h" @@ -78,6 +79,8 @@ public: qmlRegisterType("StatusQ", 0, 1, "FormattedDoubleProperty"); qmlRegisterSingletonType("StatusQ", 0, 1, "QClipboardProxy", &ClipboardUtils::qmlInstance); + qmlRegisterSingletonType("StatusQ", 0, 1, "UrlUtils", &UrlUtils::qmlInstance); + qmlRegisterType("StatusQ", 0, 1, "ModelEntry"); qmlRegisterType("StatusQ", 0, 1, "SnapshotObject"); diff --git a/ui/StatusQ/src/urlutils.cpp b/ui/StatusQ/src/urlutils.cpp new file mode 100644 index 0000000000..72f44df34e --- /dev/null +++ b/ui/StatusQ/src/urlutils.cpp @@ -0,0 +1,31 @@ +#include "StatusQ/urlutils.h" + +#include +#include + +#include + +QObject* UrlUtils::qmlInstance(QQmlEngine*, QJSEngine*) +{ + return new UrlUtils; +} + +bool UrlUtils::isValidImageUrl(const QUrl& url, const QStringList& acceptedExtensions) +{ + const auto strippedUrl = url.url( + QUrl::RemoveAuthority | QUrl::RemoveFragment | QUrl::RemoveQuery); + + return std::any_of(acceptedExtensions.constBegin(), + acceptedExtensions.constEnd(), + [strippedUrl](const auto & ext) { + return strippedUrl.endsWith(ext, Qt::CaseInsensitive); + }); +} + +qint64 UrlUtils::getFileSize(const QUrl& url) +{ + if (url.isLocalFile()) + return QFile(url.toLocalFile()).size(); + + return 0; +} diff --git a/ui/imports/utils/Utils.qml b/ui/imports/utils/Utils.qml index 45935f926e..9546004dfa 100644 --- a/ui/imports/utils/Utils.qml +++ b/ui/imports/utils/Utils.qml @@ -338,14 +338,14 @@ QtObject { function isValidDragNDropImage(url) { return url.startsWith(Constants.dataImagePrefix) || - QClipboardProxy.isValidImageUrl(url, Constants.acceptedDragNDropImageExtensions) + UrlUtils.isValidImageUrl(url, Constants.acceptedDragNDropImageExtensions) } function isFilesizeValid(img) { if (img.startsWith(Constants.dataImagePrefix)) { return img.length < maxImgSizeBytes } - const size = QClipboardProxy.getFileSize(img) + const size = UrlUtils.getFileSize(img) return size <= maxImgSizeBytes }