diff --git a/extension/extension.js b/extension/extension.js index cf700ea0..b027a524 100644 --- a/extension/extension.js +++ b/extension/extension.js @@ -1,183 +1,187 @@ /* Copyright (C) 2017 Kai Uwe Broulik This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ function sendEnvironment() { var browser = ""; var ua = navigator.userAgent; // Try to match the most derived first if (ua.match(/vivaldi/i)) { browser = "vivaldi"; } else if(ua.match(/OPR/i)) { browser = "opera"; } else if(ua.match(/chrome/i)) { browser = "chromium"; // Apparently there is no better way to distinuish chromium from chrome for (i in window.navigator.plugins) { if (window.navigator.plugins[i].name === "Chrome PDF Viewer") { browser = "chrome"; break; } } } else if(ua.match(/firefox/i)) { browser = "firefox"; } sendPortMessage("settings", "setEnvironment", {browserName: browser}); } function sendSettings() { storage.get(DEFAULT_EXTENSION_SETTINGS, function (items) { if (chrome.runtime.lastError) { console.warn("Failed to load settings"); return; } sendPortMessage("settings", "changed", items); }); } // activates giveb tab and raises its window, used by tabs runner and mpris Raise command function raiseTab(tabId) { // first activate the tab, this means it's current in its window chrome.tabs.update(tabId, {active: true}, function (tab) { if (chrome.runtime.lastError || !tab) { // this "lastError" stuff feels so archaic // failed to update return; } // then raise the tab's window too chrome.windows.update(tab.windowId, {focused: true}); }); } // Debug // ------------------------------------------------------------------------ // addCallback("debug", "debug", function(payload) { console.log("From host:", payload.message); } ) addCallback("debug", "warning", function(payload) { console.warn("From host:", payload.message); } ) // System // ------------------------------------------------------------------------ // // When connecting to native host fails (e.g. not installed), we immediately get a disconnect // event immediately afterwards. Also avoid infinite restart loop then. var receivedMessageOnce = false; // Check for supported platform to avoid loading it on e.g. Windows and then failing // when the extension got synced to another device and then failing chrome.runtime.getPlatformInfo(function (info) { if (!SUPPORTED_PLATFORMS.includes(info.os)) { console.log("This extension is not supported on", info.os); return; } connectHost(); }); function connectHost() { port = chrome.runtime.connectNative("org.kde.plasma.browser_integration"); port.onMessage.addListener(function (message) { var subsystem = message.subsystem; var action = message.action; let isReply = message.hasOwnProperty("replyToSerial"); let replyToSerial = message.replyToSerial; if (!isReply && (!subsystem || !action)) { return; } receivedMessageOnce = true; if (isReply) { let replyResolver = pendingMessageReplyResolvers[replyToSerial]; if (replyResolver) { replyResolver(message.payload); delete pendingMessageReplyResolvers[replyToSerial]; } else { console.warn("There is no reply resolver for message with serial", replyToSerial); } return; } if (callbacks[subsystem] && callbacks[subsystem][action]) { callbacks[subsystem][action](message.payload, action); } else { console.warn("Don't know what to do with host message", subsystem, action); } }); port.onDisconnect.addListener(function() { var error = chrome.runtime.lastError; console.warn("Host disconnected", error); // Remove all kde connect menu entries since they won't work without a host for (let device of kdeConnectDevices) { chrome.contextMenus.remove(kdeConnectMenuIdPrefix + device); } kdeConnectDevices = []; var reason = chrome.i18n.getMessage("general_error_unknown"); if (error && error.message) { reason = error.message; } var message = receivedMessageOnce ? chrome.i18n.getMessage("general_error_port_disconnect", reason) : chrome.i18n.getMessage("general_error_port_startupfail"); chrome.notifications.create(null, { type: "basic", title: chrome.i18n.getMessage("general_error_title"), message: message, iconUrl: "icons/sad-face-128.png" }); if (receivedMessageOnce) { console.log("Auto-restarting it"); connectHost(); } else { console.warn("Not auto-restarting host as we haven't received any message from it before. Check that it's working/installed correctly"); } }); sendEnvironment(); sendSettings(); sendDownloads(); } addRuntimeCallback("settings", "changed", function () { // we could also just reload our extension :) // but this also causes the settings dialog to quit //chrome.runtime.reload(); sendSettings(); }); addRuntimeCallback("settings", "openKRunnerSettings", function () { sendPortMessage("settings", "openKRunnerSettings"); }); + +addRuntimeCallback("settings", "getSubsystemStatus", (message, sender, action) => { + return sendPortMessageWithReply("settings", "getSubsystemStatus"); +}); diff --git a/extension/options.css b/extension/options.css index de441fbb..f61cd8d6 100644 --- a/extension/options.css +++ b/extension/options.css @@ -1,81 +1,85 @@ body { min-width: 600px; /* prevent scroll bars*/ overflow: hidden; } -.not-supported-info { +.os-not-supported-info { display: none; } -body.not-supported .not-supported-info { +body.os-not-supported .os-not-supported-info { display: block; } -body.not-supported #extensions-selection { +body.os-not-supported #extensions-selection { display: none; } +.not-supported { + display: none !important; +} + .tabbar { display: block; padding: 0; /* color of separator line below heading */ background-color: #d3d3d3; /* undo body side margins*/ margin: 0 -17px 0px -17px; padding: 0px 17px; } .tabbar > li { display: inline-block; /* TODO draw a nice tab-like thingie */ margin-top: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; border: 1px solid #999; border-bottom: none; } .tabbar > li > a { display: block; text-decoration: none; color: #333; /* can we just reset it to default text color?*/ padding: 4px 8px; } .tabbar > li > a.active { font-weight: bold; background-color: #fff; } .tab { display: none; } .tab.active { display: block; } #extensions-selection { padding: 0; } #extensions-selection > li { display: block; padding: 0; } #extensions-selection p { margin-top: 0; opacity: 0.7; line-height: 1.3; /* checkbox default width is 13px, try to align the description somewhat*/ padding-left: 18px; } .dialog-button-box { float: right; padding-bottom: 14px; } img.konqi { float: right; margin-right: -14px; width: 144px; height: 240px; } diff --git a/extension/options.html b/extension/options.html index f59dfad4..170bed42 100644 --- a/extension/options.html +++ b/extension/options.html @@ -1,85 +1,85 @@
-

I18N

+

I18N

  • I18N

  • I18N

  • I18N

  • I18N

  • I18N

I18N
I18N
I18N

I18N

I18N

I18N

I18N

diff --git a/extension/options.js b/extension/options.js index 827f3852..07a44ef3 100644 --- a/extension/options.js +++ b/extension/options.js @@ -1,218 +1,253 @@ /* Copyright (C) 2017 Kai Uwe Broulik Copyright (C) 2018 David Edmundson This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ var storage = (IS_FIREFOX ? chrome.storage.local : chrome.storage.sync); function tabClicked(tabbar, tabbutton) { tabbar.buttons.forEach(function (button) { var tablink = button.dataset.tabLink var tabtarget = document.querySelector("[data-tab-id=" + tablink + "]"); if (tabbutton == button) { button.classList.add("active"); tabtarget.classList.add("active"); } else { button.classList.remove("active"); tabtarget.classList.remove("active"); } }); } function loadSettings(cb) { storage.get(DEFAULT_EXTENSION_SETTINGS, function (items) { if (chrome.runtime.lastError) { if (typeof cb === "function") { cb(false); } return; } for (var key in items) { if (!items.hasOwnProperty(key)) { continue; } let controls = document.querySelectorAll("[data-extension=" + key + "]"); for (let control of controls) { let settingsKey = control.dataset.settingsKey; if (!settingsKey) { console.warn("Invalid settings key in", control, "cannot load this"); continue; } let value = items[key][settingsKey] if (control.type === "checkbox") { control.checked = !!value; } else { if (value === true) { control.value = "TRUE"; } else if (value === false) { control.value = "FALSE"; } else { control.value = value; } } control.addEventListener("change", () => { let saveMessage = document.getElementById("save-message"); saveMessage.innerText = ""; saveSettings((error) => { if (error) { try { saveMessage.innerText = chrome.i18n.getMessage("options_save_failed"); } catch (e) { // When the extension is reloaded, any call to extension APIs throws, make sure we show at least some form of error saveMessage.innerText = "Saving settings failed (" + (error || e) + ")"; } return; } sendMessage("settings", "changed"); }); }); } } if (typeof cb === "function") { cb(true); } }); } function saveSettings(cb) { var settings = {}; let controls = document.querySelectorAll("[data-extension]"); for (let control of controls) { let extension = control.dataset.extension; if (!DEFAULT_EXTENSION_SETTINGS.hasOwnProperty(extension)) { console.warn("Cannot save settings for extension", extension, "which isn't in DEFAULT_EXTENSION_SETTINGS"); continue; } let settingsKey = control.dataset.settingsKey; if (!settingsKey) { console.warn("Invalid settings key in", control, "cannot save this"); continue; } if (!settings[extension]) { settings[extension] = {}; } if (!DEFAULT_EXTENSION_SETTINGS[extension].hasOwnProperty(settingsKey)) { console.warn("Cannot save settings key", settingsKey, "in extension", extension, "which isn't in DEFAULT_EXTENSION_SETTINGS"); continue; } if (control.type === "checkbox") { settings[extension][settingsKey] = control.checked; } else { let value = control.value; if (value === "TRUE") { value = true; } else if (value === "FALSE") { value = false; } settings[extension][settingsKey] = value; } } try { storage.set(settings, function () { return cb(chrome.runtime.lastError); }); // When the extension is reloaded, any call to extension APIs throws } catch (e) { cb(e); } } document.addEventListener("DOMContentLoaded", function () { // poor man's tab widget :) document.querySelectorAll(".tabbar").forEach(function (tabbar) { tabbar.buttons = []; tabbar.querySelectorAll("[data-tab-link]").forEach(function (button) { var tablink = button.dataset.tabLink var tabtarget = document.querySelector("[data-tab-id=" + tablink + "]"); button.addEventListener("click", function (event) { tabClicked(tabbar, button); event.preventDefault(); }); tabbar.buttons.push(button); // start with the one tab page that is active if (tabtarget.classList.contains("active")) { tabClicked(tabbar, button); } }); }); if (IS_FIREFOX) { document.querySelectorAll("[data-not-show-in=firefox]").forEach(function (item) { item.style.display = "none"; }); } // check whether the platform is supported before loading and activating settings chrome.runtime.getPlatformInfo(function (info) { if (!SUPPORTED_PLATFORMS.includes(info.os)) { - document.body.classList.add("not-supported"); + document.body.classList.add("os-not-supported"); return; } loadSettings(function () { var mpris = document.querySelector("[data-extension=mpris]"); var mprisEx = document.querySelector("[data-extension=mprisMediaSessions]"); mpris.addEventListener("change", function() { mprisEx.disabled = !mpris.checked; }); mprisEx.disabled = !mpris.checked; }); + + // When getSubsystemStatus fails we assume it's an old host without any of the new features + // for which we added the requires-extension attributes. Disable all of them initially + // and then have the supported ones enabled below. + document.querySelectorAll("[data-requires-extension]").forEach((item) => { + item.classList.add("not-supported", "by-host"); + }); + + sendMessage("settings", "getSubsystemStatus").then((status) => { + document.querySelectorAll("[data-requires-extension]").forEach((item) => { + let requiresExtension = item.dataset.requiresExtension; + + if (requiresExtension && !status.hasOwnProperty(requiresExtension)) { + console.log("Extension", requiresExtension, "is not supported by this version of the host"); + return; // continue + } + + let requiresMinimumVersion = Number(item.dataset.requiresExtensionVersionMinimum); + if (requiresMinimumVersion) { + let runningVersion = status[requiresExtension].version; + if (runningVersion < requiresMinimumVersion) { + console.log("Extension", requiresExtension, "of version", requiresMinimumVersion, "is required but only", runningVersion, "is present in the host"); + return; // continue + } + } + + item.classList.remove("not-supported", "by-host"); + }); + }).catch((e) => { + // The host is most likely not working correctly + // If we run this against an older host which doesn't support message replies + // this handler is never entered, so we really encountered an error just now! + console.warn("Failed to determine subsystem status", e); + // TODO show helpful error message in UI + }); }); document.getElementById("open-krunner-settings").addEventListener("click", function (event) { sendMessage("settings", "openKRunnerSettings"); event.preventDefault(); }); // Make translators credit behave like the one in KAboutData var translatorsAboutData = ""; var translators = chrome.i18n.getMessage("options_about_translators"); if (translators && translators !== "Your names") { translatorsAboutData = chrome.i18n.getMessage("options_about_translated_by", translators) } var translatorsAboutDataItem = document.getElementById("translators-aboutdata"); if (translatorsAboutData) { translatorsAboutDataItem.innerText = translatorsAboutData; } else { translatorsAboutDataItem.style.display = "none"; } }); diff --git a/host/settings.cpp b/host/settings.cpp index 3fb9b461..73f340bd 100644 --- a/host/settings.cpp +++ b/host/settings.cpp @@ -1,157 +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()} + }; + 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(); } diff --git a/host/settings.h b/host/settings.h index d5dbc200..0dd1ef92 100644 --- a/host/settings.h +++ b/host/settings.h @@ -1,86 +1,87 @@ /* 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 "abstractbrowserplugin.h" #include struct EnvironmentDescription { QString applicationName; QString applicationDisplayName; QString desktopFileName; QString organizationDomain; QString organizationName; }; /* * This class manages the extension's settings (so that settings in the browser * propagate to our extension) and also detects the environment the host is run * in (e.g. whether we're started by Firefox, Chrome, Chromium, or Opera) */ class Settings : public AbstractBrowserPlugin { Q_OBJECT Q_PROPERTY(QString Environment READ environmentString) public: static Settings &self(); enum class Environment { Unknown, Chrome, Chromium, Firefox, Opera, Vivaldi, }; Q_ENUM(Environment) void handleData(const QString &event, const QJsonObject &data) override; + QJsonObject handleData(int serial, const QString &event, const QJsonObject &data) override; Environment environment() const; QString environmentString() const; // dbus // TODO should we have additional getters like browserName(), browserDesktopEntry(), etc? bool pluginEnabled(const QString &subsystem) const; QJsonObject settingsForPlugin(const QString &subsystem) const; Q_SIGNALS: void changed(const QJsonObject &settings); private: Settings(); ~Settings() override = default; static const QMap environmentNames; static const QMap environmentDescriptions; Environment m_environment = Environment::Unknown; EnvironmentDescription m_currentEnvironment; QJsonObject m_settings; };