From bf684cb7ffc59e4fc73742ef4c1130d8b984b1a5 Mon Sep 17 00:00:00 2001 From: Anmol Gautam Date: Wed, 18 Jul 2018 18:48:28 +0530 Subject: [PATCH] Restricted icons to plugin directory The icons will be searched in the following order: - Theme icons - Falkon resource - Files present in the plugin directory --- .../qml/api/browseraction/qmlbrowseraction.cpp | 10 +++++++++- src/lib/plugins/qml/api/menus/qmlaction.cpp | 12 +++++++++++- src/lib/plugins/qml/api/menus/qmlaction.h | 2 ++ src/lib/plugins/qml/api/menus/qmlmenu.cpp | 14 +++++++++++++- src/lib/plugins/qml/api/menus/qmlmenu.h | 2 ++ .../qml/api/notifications/qmlnotifications.cpp | 17 +++++++++++++++-- .../qml/api/notifications/qmlnotifications.h | 3 +++ src/lib/plugins/qml/api/sidebar/qmlsidebar.cpp | 9 ++++++++- src/lib/plugins/qml/qmlplugininterface.cpp | 3 ++- src/lib/plugins/qml/qmlplugins.cpp | 3 +++ src/scripts/helloqml/helloqml.qml | 14 +++++++------- 11 files changed, 75 insertions(+), 14 deletions(-) diff --git a/src/lib/plugins/qml/api/browseraction/qmlbrowseraction.cpp b/src/lib/plugins/qml/api/browseraction/qmlbrowseraction.cpp index 2cfdc6416..2aed74c7e 100644 --- a/src/lib/plugins/qml/api/browseraction/qmlbrowseraction.cpp +++ b/src/lib/plugins/qml/api/browseraction/qmlbrowseraction.cpp @@ -20,7 +20,9 @@ #include "navigationbar.h" #include "statusbar.h" #include "pluginproxy.h" +#include "qml/api/fileutils/qmlfileutils.h" #include +#include QmlBrowserAction::QmlBrowserAction(QObject *parent) : QObject(parent) @@ -212,8 +214,14 @@ void QmlBrowserActionButton::setIcon(const QString &icon) m_iconUrl = icon; if (QIcon::hasThemeIcon(m_iconUrl)) { AbstractButtonInterface::setIcon(QIcon::fromTheme(m_iconUrl)); + } else if (m_iconUrl.startsWith(QSL(":"))) { + // Icon is loaded from falkon resource + AbstractButtonInterface::setIcon(QIcon(m_iconUrl)); } else { - AbstractButtonInterface::setIcon(QIcon(QzTools::getPathFromUrl(QUrl(m_iconUrl)))); + const QString pluginPath = m_popup->creationContext()->contextProperty("__path__").toString(); + QmlFileUtils fileUtils(pluginPath); + m_iconUrl = fileUtils.resolve(icon); + AbstractButtonInterface::setIcon(QIcon(m_iconUrl)); } } diff --git a/src/lib/plugins/qml/api/menus/qmlaction.cpp b/src/lib/plugins/qml/api/menus/qmlaction.cpp index 5f5d2518f..c6e709db6 100644 --- a/src/lib/plugins/qml/api/menus/qmlaction.cpp +++ b/src/lib/plugins/qml/api/menus/qmlaction.cpp @@ -17,6 +17,7 @@ * ============================================================ */ #include "qmlaction.h" #include "qztools.h" +#include "qml/api/fileutils/qmlfileutils.h" QmlAction::QmlAction(QAction *action, QObject *parent) : QObject(parent) @@ -37,8 +38,12 @@ void QmlAction::setProperties(const QVariantMap &map) QIcon icon; if (QIcon::hasThemeIcon(iconPath)) { icon = QIcon::fromTheme(iconPath); + } else if (iconPath.startsWith(QSL(":"))) { + // Icon is loaded from falkon resource + icon = QIcon(iconPath); } else { - icon = QIcon(QzTools::getPathFromUrl(QUrl::fromEncoded(iconPath.toUtf8()))); + QmlFileUtils fileUtils(m_pluginPath); + icon = QIcon(fileUtils.resolve(iconPath)); } m_action->setIcon(icon); } else if (key == QSL("shortcut")) { @@ -57,3 +62,8 @@ void QmlAction::update(const QVariantMap &map) { setProperties(map); } + +void QmlAction::setPluginPath(const QString &path) +{ + m_pluginPath = path; +} diff --git a/src/lib/plugins/qml/api/menus/qmlaction.h b/src/lib/plugins/qml/api/menus/qmlaction.h index e8b7f4309..093108b31 100644 --- a/src/lib/plugins/qml/api/menus/qmlaction.h +++ b/src/lib/plugins/qml/api/menus/qmlaction.h @@ -31,6 +31,7 @@ public: explicit QmlAction(QAction *action, QObject *parent = nullptr); void setProperties(const QVariantMap &map); Q_INVOKABLE void update(const QVariantMap &map); + void setPluginPath(const QString &path); Q_SIGNALS: /** @@ -40,4 +41,5 @@ Q_SIGNALS: private: QAction *m_action; + QString m_pluginPath; }; diff --git a/src/lib/plugins/qml/api/menus/qmlmenu.cpp b/src/lib/plugins/qml/api/menus/qmlmenu.cpp index defbe689b..85ba04fb4 100644 --- a/src/lib/plugins/qml/api/menus/qmlmenu.cpp +++ b/src/lib/plugins/qml/api/menus/qmlmenu.cpp @@ -17,6 +17,7 @@ * ============================================================ */ #include "qmlmenu.h" #include "qztools.h" +#include "qml/api/fileutils/qmlfileutils.h" #include QmlMenu::QmlMenu(QMenu *menu, QObject *parent) @@ -43,6 +44,7 @@ QmlAction *QmlMenu::addAction(const QVariantMap &map) QAction *action = new QAction(); QmlAction *qmlAction = new QmlAction(action, this); + qmlAction->setPluginPath(m_pluginPath); qmlAction->setProperties(map); m_menu->addAction(action); @@ -68,8 +70,12 @@ QmlMenu *QmlMenu::addMenu(const QVariantMap &map) QIcon icon; if (QIcon::hasThemeIcon(iconPath)) { icon = QIcon::fromTheme(iconPath); + } else if (iconPath.startsWith(QSL(":"))) { + // Icon is loaded from falkon resource + icon = QIcon(iconPath); } else { - icon = QIcon(QzTools::getPathFromUrl(QUrl::fromEncoded(iconPath.toUtf8()))); + QmlFileUtils fileUtils(m_pluginPath); + icon = QIcon(fileUtils.resolve(iconPath)); } newMenu->setIcon(icon); continue; @@ -78,6 +84,7 @@ QmlMenu *QmlMenu::addMenu(const QVariantMap &map) } m_menu->addMenu(newMenu); QmlMenu *newQmlMenu = new QmlMenu(newMenu, this); + newQmlMenu->setPluginPath(m_pluginPath); return newQmlMenu; } @@ -92,3 +99,8 @@ void QmlMenu::addSeparator() m_menu->addSeparator(); } + +void QmlMenu::setPluginPath(const QString &path) +{ + m_pluginPath = path; +} diff --git a/src/lib/plugins/qml/api/menus/qmlmenu.h b/src/lib/plugins/qml/api/menus/qmlmenu.h index 61c1e9505..fa1334683 100644 --- a/src/lib/plugins/qml/api/menus/qmlmenu.h +++ b/src/lib/plugins/qml/api/menus/qmlmenu.h @@ -31,6 +31,7 @@ public: Q_INVOKABLE QmlAction *addAction(const QVariantMap &map); Q_INVOKABLE QmlMenu *addMenu(const QVariantMap &map); Q_INVOKABLE void addSeparator(); + void setPluginPath(const QString &path); Q_SIGNALS: /** @@ -40,4 +41,5 @@ Q_SIGNALS: private: QMenu *m_menu; + QString m_pluginPath; }; diff --git a/src/lib/plugins/qml/api/notifications/qmlnotifications.cpp b/src/lib/plugins/qml/api/notifications/qmlnotifications.cpp index 0de5fdece..21feb895f 100644 --- a/src/lib/plugins/qml/api/notifications/qmlnotifications.cpp +++ b/src/lib/plugins/qml/api/notifications/qmlnotifications.cpp @@ -19,6 +19,7 @@ #include "mainapplication.h" #include "desktopnotificationsfactory.h" #include "qztools.h" +#include "qml/api/fileutils/qmlfileutils.h" QmlNotifications::QmlNotifications(QObject *parent) : QObject(parent) @@ -38,9 +39,21 @@ QmlNotifications::QmlNotifications(QObject *parent) void QmlNotifications::create(const QVariantMap &map) { const QString iconUrl = map.value(QSL("icon")).toString(); - const QString iconPath = QzTools::getPathFromUrl(QUrl(iconUrl)); - const QPixmap icon(iconPath); + QPixmap icon; + if (iconUrl.startsWith(QSL(":"))) { + // Icon is loaded from falkon resource + icon = QPixmap(iconUrl); + } else { + QmlFileUtils fileUtils(m_pluginPath); + const QString iconPath = fileUtils.resolve(iconUrl); + icon = QPixmap(iconPath); + } const QString heading = map.value(QSL("heading")).toString(); const QString message = map.value(QSL("message")).toString(); mApp->desktopNotifications()->showNotification(icon, heading, message); } + +void QmlNotifications::setPluginPath(const QString &path) +{ + m_pluginPath = path; +} diff --git a/src/lib/plugins/qml/api/notifications/qmlnotifications.h b/src/lib/plugins/qml/api/notifications/qmlnotifications.h index 246d8fda5..0bf3f32f1 100644 --- a/src/lib/plugins/qml/api/notifications/qmlnotifications.h +++ b/src/lib/plugins/qml/api/notifications/qmlnotifications.h @@ -28,4 +28,7 @@ class QmlNotifications : public QObject public: explicit QmlNotifications(QObject *parent = nullptr); Q_INVOKABLE void create(const QVariantMap &map); + void setPluginPath(const QString &path); +private: + QString m_pluginPath; }; diff --git a/src/lib/plugins/qml/api/sidebar/qmlsidebar.cpp b/src/lib/plugins/qml/api/sidebar/qmlsidebar.cpp index aef011bde..34a45a38f 100644 --- a/src/lib/plugins/qml/api/sidebar/qmlsidebar.cpp +++ b/src/lib/plugins/qml/api/sidebar/qmlsidebar.cpp @@ -19,8 +19,10 @@ #include "mainapplication.h" #include "qztools.h" #include "sidebar.h" +#include "qml/api/fileutils/qmlfileutils.h" #include #include +#include QmlSideBar::QmlSideBar(QObject *parent) : QObject(parent) @@ -133,8 +135,13 @@ QAction *QmlSideBarHelper::createMenuAction() action->setShortcut(QKeySequence(m_shortcut)); if (QIcon::hasThemeIcon(m_iconUrl)) { action->setIcon(QIcon::fromTheme(m_iconUrl)); + } else if (m_iconUrl.startsWith(QSL(":"))) { + // Icon is loaded from falkon resource + action->setIcon(QIcon(m_iconUrl)); } else { - action->setIcon(QIcon(QzTools::getPathFromUrl(QUrl(m_iconUrl)))); + const QString pluginPath = m_item->creationContext()->contextProperty("__path__").toString(); + QmlFileUtils fileUtils(pluginPath); + action->setIcon(QIcon(fileUtils.resolve(m_iconUrl))); } action->setCheckable(m_checkable); return action; diff --git a/src/lib/plugins/qml/qmlplugininterface.cpp b/src/lib/plugins/qml/qmlplugininterface.cpp index 061d4ccc4..dc1bfb565 100644 --- a/src/lib/plugins/qml/qmlplugininterface.cpp +++ b/src/lib/plugins/qml/qmlplugininterface.cpp @@ -34,6 +34,7 @@ #include #include #include +#include QmlPluginInterface::QmlPluginInterface() : m_settingsWindow(nullptr) @@ -94,8 +95,8 @@ void QmlPluginInterface::populateWebViewMenu(QMenu *menu, WebView *webview, cons } QmlMenu *qmlMenu = new QmlMenu(menu); + qmlMenu->setPluginPath(m_engine->rootContext()->contextProperty("__path__").toString()); QmlWebHitTestResult *qmlWebHitTestResult = new QmlWebHitTestResult(webHitTestResult); - QJSValueList args; args.append(m_engine->newQObject(qmlMenu)); args.append(m_engine->newQObject(qmlWebHitTestResult)); diff --git a/src/lib/plugins/qml/qmlplugins.cpp b/src/lib/plugins/qml/qmlplugins.cpp index b7841548d..f627fb778 100644 --- a/src/lib/plugins/qml/qmlplugins.cpp +++ b/src/lib/plugins/qml/qmlplugins.cpp @@ -122,7 +122,10 @@ void QmlPlugins::registerQmlTypes() Q_UNUSED(engine) Q_UNUSED(scriptEngine) + QString filePath = engine->rootContext()->contextProperty("__path__").toString(); + auto *object = new QmlNotifications(); + object->setPluginPath(filePath); return object; }); diff --git a/src/scripts/helloqml/helloqml.qml b/src/scripts/helloqml/helloqml.qml index 4db4c7fe8..929ed8371 100644 --- a/src/scripts/helloqml/helloqml.qml +++ b/src/scripts/helloqml/helloqml.qml @@ -21,7 +21,7 @@ Falkon.PluginInterface { identity: 'helloqml-id' title: 'Testing QML Title' toolTip: 'Testing QML Tooltip' - icon: Qt.resolvedUrl('qrc:/icons/preferences/extensions.svg') + icon: ':/icons/preferences/extensions.svg' location: Falkon.BrowserAction.NavigationToolBar | Falkon.BrowserAction.StatusBar popup: Window { property var borderMargin: 1 @@ -41,7 +41,7 @@ Falkon.PluginInterface { color: 'white' Image { id: image - source: Qt.resolvedUrl('qrc:/icons/other/startpage.svg') + source: 'qrc:/icons/other/startpage.svg' anchors.left: parent.left anchors.right: parent.right anchors.top: parent.top @@ -61,11 +61,11 @@ Falkon.PluginInterface { Falkon.SideBar { name: 'helloqml-sidebar' title: 'Testing QML SideBar' - icon: Qt.resolvedUrl('qrc:/icons/preferences/extensions.svg') + icon: ':/icons/preferences/extensions.svg' checkable: true Window { Image { - source: Qt.resolvedUrl('qrc:/icons/other/startpage.svg') + source: 'qrc:/icons/other/startpage.svg' anchors.verticalCenter: parent.verticalCenter anchors.left: parent.left anchors.right: parent.right @@ -84,7 +84,7 @@ Falkon.PluginInterface { var text = 'My first qml plugin action' var action = menu.addAction({ text: text, - icon: Qt.resolvedUrl('qrc:/icons/preferences/extensions.svg') + icon: ':/icons/preferences/extensions.svg' }) if (webHitTestResult.isImage()) { @@ -105,7 +105,7 @@ Falkon.PluginInterface { Falkon.Notifications.create({ heading: 'Hello QML', message: 'First qml plugin action works :-)', - icon: Qt.resolvedUrl('qrc:/icons/preferences/extensions.svg') + icon: ':/icons/preferences/extensions.svg' }) }) } @@ -121,7 +121,7 @@ Falkon.PluginInterface { height: 200 Image { id: image - source: Qt.resolvedUrl('qrc:/icons/other/about.svg') + source: 'qrc:/icons/other/about.svg' anchors.top: parent.top anchors.left: parent.left anchors.right: parent.right