diff --git a/host/abstractbrowserplugin.cpp b/host/abstractbrowserplugin.cpp index c955203f..f36983c8 100644 --- a/host/abstractbrowserplugin.cpp +++ b/host/abstractbrowserplugin.cpp @@ -1,120 +1,125 @@ /* Copyright (C) 2017 by Kai Uwe Broulik Copyright (C) 2017 by David Edmundson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "abstractbrowserplugin.h" #include "connection.h" #include "settings.h" AbstractBrowserPlugin::AbstractBrowserPlugin::AbstractBrowserPlugin(const QString& subsystemId, int protocolVersion, QObject* parent): QObject(parent), m_subsystem(subsystemId), m_protocolVersion(protocolVersion) { } void AbstractBrowserPlugin::handleData(const QString& event, const QJsonObject& data) { Q_UNUSED(event); Q_UNUSED(data); } QJsonObject AbstractBrowserPlugin::handleData(int serial, const QString &event, const QJsonObject &data) { Q_UNUSED(serial); Q_UNUSED(event); Q_UNUSED(data); return QJsonObject(); } void AbstractBrowserPlugin::sendData(const QString &action, const QJsonObject &payload) { QJsonObject data; data[QStringLiteral("subsystem")] = m_subsystem; data[QStringLiteral("action")] = action; if (!payload.isEmpty()) { data[QStringLiteral("payload")] = payload; } Connection::self()->sendData(data); } void AbstractBrowserPlugin::sendReply(int requestSerial, const QJsonObject &payload) { QJsonObject data{ {QStringLiteral("replyToSerial"), requestSerial}, }; if (!payload.isEmpty()) { data.insert(QStringLiteral("payload"), payload); } Connection::self()->sendData(data); } bool AbstractBrowserPlugin::onLoad() { return true; } bool AbstractBrowserPlugin::onUnload() { return true; } void AbstractBrowserPlugin::onSettingsChanged(const QJsonObject &newSettings) { Q_UNUSED(newSettings); } +QJsonObject AbstractBrowserPlugin::status() const +{ + return {}; +} + QDebug AbstractBrowserPlugin::debug() const { auto d = qDebug(); QDebugStateSaver saver(d); d.nospace().noquote() << m_subsystem << ":"; return d; } QString AbstractBrowserPlugin::subsystem() const { return m_subsystem; } int AbstractBrowserPlugin::protocolVersion() const { return m_protocolVersion; } bool AbstractBrowserPlugin::isLoaded() const { return m_loaded; } void AbstractBrowserPlugin::setLoaded(bool loaded) { m_loaded = loaded; } QJsonObject AbstractBrowserPlugin::settings() const { return Settings::self().settingsForPlugin(m_subsystem); } diff --git a/host/abstractbrowserplugin.h b/host/abstractbrowserplugin.h index 2a23bb42..13f72dc1 100644 --- a/host/abstractbrowserplugin.h +++ b/host/abstractbrowserplugin.h @@ -1,76 +1,83 @@ /* Copyright (C) 2017 by Kai Uwe Broulik Copyright (C) 2017 by David Edmundson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #pragma once #include #include #include class AbstractBrowserPlugin : public QObject { Q_OBJECT public: ~AbstractBrowserPlugin() = default; QString subsystem() const; int protocolVersion() const; bool isLoaded() const; // FIXME this should not be public but we need to change it from main.cpp void setLoaded(bool loaded); + /** + * Lets the plugin add additional status information to the getSubsystemStatus request + * + * E.g. whether a library dependency or external binary is present. + */ + virtual QJsonObject status() const; + protected: /* * @arg subsystemId * The name of the plugin. This will be used for the "subsystem" parameter for all data sent * * @arg protocolVersion * As the browser extension will be shipped separately to the native plugin a user could have incompatiable setups * Here we inform the browser of the protocol used so if we do ever changed the native API we can at least detect it on the JS side and handle it */ AbstractBrowserPlugin(const QString &subsystemId, int protocolVersion, QObject *parent); virtual void handleData(const QString &event, const QJsonObject &data); virtual QJsonObject handleData(int serial, const QString &event, const QJsonObject &data); virtual bool onLoad(); virtual bool onUnload(); virtual void onSettingsChanged(const QJsonObject &newSettings); void sendData(const QString &action, const QJsonObject &payload = QJsonObject()); void sendReply(int requestSerial, const QJsonObject &payload = QJsonObject()); QDebug debug() const; QJsonObject settings() const; friend class PluginManager; private: QString m_subsystem; int m_protocolVersion; bool m_loaded = false; }; diff --git a/host/settings.cpp b/host/settings.cpp index 73f340bd..6abe7620 100644 --- a/host/settings.cpp +++ b/host/settings.cpp @@ -1,180 +1,180 @@ /* Copyright (C) 2017 by Kai Uwe Broulik Copyright (C) 2017 by David Edmundson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "settings.h" #include #include #include #include "pluginmanager.h" #include "settingsadaptor.h" const QMap Settings::environmentNames = { {Settings::Environment::Chrome, QStringLiteral("chrome")}, {Settings::Environment::Chromium, QStringLiteral("chromium")}, {Settings::Environment::Firefox, QStringLiteral("firefox")}, {Settings::Environment::Opera, QStringLiteral("opera")}, {Settings::Environment::Vivaldi, QStringLiteral("vivaldi")}, }; const QMap Settings::environmentDescriptions = { {Settings::Environment::Chrome, { QStringLiteral("google-chrome"), QStringLiteral("Google Chrome"), QStringLiteral("google-chrome"), QStringLiteral("google.com"), QStringLiteral("Google") } }, {Settings::Environment::Chromium, { QStringLiteral("chromium-browser"), QStringLiteral("Chromium"), QStringLiteral("chromium-browser"), QStringLiteral("google.com"), QStringLiteral("Google") } }, {Settings::Environment::Firefox, { QStringLiteral("firefox"), QStringLiteral("Mozilla Firefox"), QStringLiteral("firefox"), QStringLiteral("mozilla.org"), QStringLiteral("Mozilla") } }, {Settings::Environment::Opera, { QStringLiteral("opera"), QStringLiteral("Opera"), QStringLiteral("opera"), QStringLiteral("opera.com"), QStringLiteral("Opera") } }, {Settings::Environment::Vivaldi, { QStringLiteral("vivaldi"), QStringLiteral("Vivaldi"), // This is what the official package on their website uses QStringLiteral("vivaldi-stable"), QStringLiteral("vivaldi.com"), QStringLiteral("Vivaldi") } } }; Settings::Settings() : AbstractBrowserPlugin(QStringLiteral("settings"), 1, nullptr) { new SettingsAdaptor(this); QDBusConnection::sessionBus().registerObject(QStringLiteral("/Settings"), this); } Settings &Settings::self() { static Settings s_self; return s_self; } void Settings::handleData(const QString &event, const QJsonObject &data) { if (event == QLatin1String("changed")) { m_settings = data; for (auto it = data.begin(), end = data.end(); it != end; ++it) { const QString &subsystem = it.key(); const QJsonObject &settingsObject = it->toObject(); const QJsonValue enabledVariant = settingsObject.value(QStringLiteral("enabled")); // probably protocol overhead, not a plugin setting, skip. if (enabledVariant.type() == QJsonValue::Undefined) { continue; } auto *plugin = PluginManager::self().pluginForSubsystem(subsystem); if (!plugin) { continue; } if (enabledVariant.toBool()) { PluginManager::self().loadPlugin(plugin); } else { PluginManager::self().unloadPlugin(plugin); } PluginManager::self().settingsChanged(plugin, settingsObject); } emit changed(data); } else if (event == QLatin1String("openKRunnerSettings")) { QProcess::startDetached(QStringLiteral("kcmshell5"), {QStringLiteral("kcm_plasmasearch")}); } else if (event == QLatin1String("setEnvironment")) { QString name = data[QStringLiteral("browserName")].toString(); m_environment = Settings::environmentNames.key(name, Settings::Environment::Unknown); m_currentEnvironment = Settings::environmentDescriptions.value(m_environment); qApp->setApplicationName(m_currentEnvironment.applicationName); qApp->setApplicationDisplayName(m_currentEnvironment.applicationDisplayName); qApp->setDesktopFileName(m_currentEnvironment.desktopFileName); qApp->setOrganizationDomain(m_currentEnvironment.organizationDomain); qApp->setOrganizationName(m_currentEnvironment.organizationName); } } QJsonObject Settings::handleData(int serial, const QString &event, const QJsonObject &data) { Q_UNUSED(serial) Q_UNUSED(data) QJsonObject ret; if (event == QLatin1String("getSubsystemStatus")) { // should we add a PluginManager::knownSubsystems() that returns a QList? const QStringList subsystems = PluginManager::self().knownPluginSubsystems(); for (const QString &subsystem : subsystems) { const AbstractBrowserPlugin *plugin = PluginManager::self().pluginForSubsystem(subsystem); - QJsonObject details{ - {QStringLiteral("version"), plugin->protocolVersion()}, - {QStringLiteral("loaded"), plugin->isLoaded()} - }; + + QJsonObject details = plugin->status(); + details.insert(QStringLiteral("version"), plugin->protocolVersion()); + details.insert(QStringLiteral("loaded"), plugin->isLoaded()); ret.insert(subsystem, details); } } return ret; } Settings::Environment Settings::environment() const { return m_environment; } QString Settings::environmentString() const { return Settings::environmentNames.value(m_environment); } bool Settings::pluginEnabled(const QString &subsystem) const { return settingsForPlugin(subsystem).value(QStringLiteral("enabled")).toBool(); } QJsonObject Settings::settingsForPlugin(const QString &subsystem) const { return m_settings.value(subsystem).toObject(); }