From 82c8d50f9a7f955bd2bc35b549c85f6a6c76bc53 Mon Sep 17 00:00:00 2001 From: Juraj Oravec Date: Tue, 17 Jul 2018 11:40:14 +0200 Subject: [PATCH] [Feature] Add python plugin MiddleClickLoader Summary: This plugin loads text from selection clipboard in tab after pressing mouse middle button (pressing mouse wheel). Url / text from selection clipboard can be loaded in: Current Tab, New Tab, New Window There is an option to use only valid url otherwise it will search using default search engine. FEATURE: 395688 Reviewers: #falkon, drosca Subscribers: falkon Tags: #falkon Differential Revision: https://phabricator.kde.org/D14155 --- src/scripts/CMakeLists.txt | 1 + src/scripts/middleclickloader/Messages.sh | 11 ++ src/scripts/middleclickloader/__init__.py | 51 +++++++++ src/scripts/middleclickloader/mcl_handler.py | 90 +++++++++++++++ src/scripts/middleclickloader/mcl_loadmode.py | 25 +++++ src/scripts/middleclickloader/mcl_settings.py | 69 ++++++++++++ src/scripts/middleclickloader/mcl_settings.ui | 105 ++++++++++++++++++ .../middleclickloader/metadata.desktop | 11 ++ 8 files changed, 363 insertions(+) create mode 100644 src/scripts/middleclickloader/Messages.sh create mode 100644 src/scripts/middleclickloader/__init__.py create mode 100644 src/scripts/middleclickloader/mcl_handler.py create mode 100644 src/scripts/middleclickloader/mcl_loadmode.py create mode 100644 src/scripts/middleclickloader/mcl_settings.py create mode 100644 src/scripts/middleclickloader/mcl_settings.ui create mode 100644 src/scripts/middleclickloader/metadata.desktop diff --git a/src/scripts/CMakeLists.txt b/src/scripts/CMakeLists.txt index 76e9f885d..8c143f0a3 100644 --- a/src/scripts/CMakeLists.txt +++ b/src/scripts/CMakeLists.txt @@ -11,3 +11,4 @@ endfunction() install_python_script(hellopython) install_python_script(runaction) +install_python_script(middleclickloader) diff --git a/src/scripts/middleclickloader/Messages.sh b/src/scripts/middleclickloader/Messages.sh new file mode 100644 index 000000000..6d3642cc4 --- /dev/null +++ b/src/scripts/middleclickloader/Messages.sh @@ -0,0 +1,11 @@ +#! /bin/sh + +XGETTEXT_FLAGS_PYTHON="\ +--copyright-holder=This_file_is_part_of_KDE \ +--msgid-bugs-address=http://bugs.kde.org \ +--from-code=UTF-8 \ +-L Python \ +-ki18n:1 -ki18np:1,2 \ +" + +$XGETTEXT_PROGRAM $XGETTEXT_FLAGS_PYTHON `find . -name '*.py'` -o $podir/falkon_middleclickloader.pot diff --git a/src/scripts/middleclickloader/__init__.py b/src/scripts/middleclickloader/__init__.py new file mode 100644 index 000000000..3c9c76b72 --- /dev/null +++ b/src/scripts/middleclickloader/__init__.py @@ -0,0 +1,51 @@ +# ============================================================ +# MiddleClickLoader - plugin for Falkon +# Copyright (C) 2018 Juraj Oravec +# +# 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 . +# ============================================================ + +import Falkon +from PySide2 import QtCore +from middleclickloader.mcl_handler import MCL_Handler + + +class MCL_Plugin(Falkon.PluginInterface, QtCore.QObject): + handler = None + + def init(self, state, settingsPath): + plugins = Falkon.MainApplication.instance().plugins() + plugins.registerAppEventHandler(Falkon.PluginProxy.MousePressHandler, self) + + self.handler = MCL_Handler(settingsPath) + + def unload(self): + del self.handler + self.handler = None + + def testPlugin(self): + return True + + def mousePress(self, type, obj, event): + if type == Falkon.Qz.ON_WebView: + return self.handler.mousePress(obj, event) + + return False + + def showSettings(self, parent): + pass + self.handler.showSettings(parent) + + +Falkon.registerPlugin(MCL_Plugin()) diff --git a/src/scripts/middleclickloader/mcl_handler.py b/src/scripts/middleclickloader/mcl_handler.py new file mode 100644 index 000000000..bea9a0ca4 --- /dev/null +++ b/src/scripts/middleclickloader/mcl_handler.py @@ -0,0 +1,90 @@ +# ============================================================ +# MiddleClickLoader - plugin for Falkon +# Copyright (C) 2018 Juraj Oravec +# +# 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 . +# ============================================================ + +import Falkon +from PySide2 import QtCore, QtGui, QtWidgets + +from middleclickloader.mcl_loadmode import MCL_LoadMode +from middleclickloader.mcl_settings import MCL_Settings + + +class MCL_Handler(QtCore.QObject): + settingsFile = "" + loaded = False + + onlyValidUrl = False + loadMode = 0 + + def __init__(self, settingsPath, parent=None): + super().__init__(parent) + + self.settingsFile = settingsPath + "/extensions.ini" + + def loadSettings(self): + settings = QtCore.QSettings(self.settingsFile, QtCore.QSettings.IniFormat) + settings.beginGroup("MiddleClickLoader") + self.loadMode = int(settings.value("LoadMode", MCL_LoadMode.NEW_TAB)) + self.onlyValidUrl = bool(settings.value("OnlyValidUrl", True)) + settings.endGroup() + + self.loaded = True + + def showSettings(self, parent=None): + self.settings = MCL_Settings(self.settingsFile, parent) + self.settings.accepted.connect(self.loadSettings) + + self.settings.exec_() + + def mousePress(self, view, event): + if not self.loaded: + self.loadSettings() + + if event.buttons() == QtCore.Qt.MiddleButton: + res = view.page().hitTestContent(event.pos()) + + if res.isContentEditable() or not res.linkUrl().isEmpty(): + return False + + selectionClipboard = QtWidgets.QApplication.clipboard().text(QtGui.QClipboard.Selection) + + if selectionClipboard: + guessedUrl = QtCore.QUrl.fromUserInput(selectionClipboard) + isValid = view.isUrlValid(guessedUrl) + + if self.onlyValidUrl and not isValid: + return False + + if not isValid: + searchManager = Falkon.MainApplication.instance().searchEnginesManager() + engine = searchManager.defaultEngine() + req = searchManager.searchResult(engine, selectionClipboard) + guessedUrl = req.url() + + return self.loadUrl(view, guessedUrl) + return False + + def loadUrl(self, view, url): + if self.loadMode == MCL_LoadMode.NEW_TAB: + view.openUrlInNewTab(url, Falkon.Qz.NT_NotSelectedTab) + elif self.loadMode == MCL_LoadMode.CURRENT_TAB: + view.load(url) + elif self.loadMode == MCL_LoadMode.NEW_WINDOW: + Falkon.MainApplication.instance().createWindow(Falkon.Qz.BW_NewWindow, url) + else: + return False + return True diff --git a/src/scripts/middleclickloader/mcl_loadmode.py b/src/scripts/middleclickloader/mcl_loadmode.py new file mode 100644 index 000000000..179ff5849 --- /dev/null +++ b/src/scripts/middleclickloader/mcl_loadmode.py @@ -0,0 +1,25 @@ +# ============================================================ +# MiddleClickLoader - plugin for Falkon +# Copyright (C) 2018 Juraj Oravec +# +# 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 . +# ============================================================ + +from enum import IntEnum + + +class MCL_LoadMode(IntEnum): + NEW_TAB = 0 + CURRENT_TAB = 1 + NEW_WINDOW = 2 diff --git a/src/scripts/middleclickloader/mcl_settings.py b/src/scripts/middleclickloader/mcl_settings.py new file mode 100644 index 000000000..adaac3cbc --- /dev/null +++ b/src/scripts/middleclickloader/mcl_settings.py @@ -0,0 +1,69 @@ +# ============================================================ +# MiddleClickLoader - plugin for Falkon +# Copyright (C) 2018 Juraj Oravec +# +# 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 . +# ============================================================ + +import os +from PySide2 import QtCore, QtWidgets, QtUiTools +from middleclickloader.i18n import i18n +from middleclickloader.mcl_loadmode import MCL_LoadMode + + +class MCL_Settings(QtWidgets.QDialog): + settingsFile = "" + ui = None + + def __init__(self, settingsFile, parent=None): + super().__init__(parent) + + self.settingsFile = settingsFile + + file = QtCore.QFile(os.path.join(os.path.dirname(__file__), "mcl_settings.ui")) + file.open(QtCore.QFile.ReadOnly) + self.ui = QtUiTools.QUiLoader().load(file, self) + file.close() + + layout = QtWidgets.QVBoxLayout(self) + layout.addWidget(self.ui) + self.setLayout(layout) + + self.setWindowTitle(i18n("MiddleClickLoader Setting")) + self.ui.label_header.setText("

{}

".format(i18n("MiddleClickLoader"))) + self.ui.label_loadMode.setText(i18n("Open url in:")) + self.ui.onlyValidUrl.setText(i18n("Use only valid url")) + + self.ui.loadMode.addItem(i18n("New Tab"), MCL_LoadMode.NEW_TAB) + self.ui.loadMode.addItem(i18n("Current Tab"), MCL_LoadMode.CURRENT_TAB) + self.ui.loadMode.addItem(i18n("New Window"), MCL_LoadMode.NEW_WINDOW) + + settings = QtCore.QSettings(self.settingsFile, QtCore.QSettings.IniFormat) + settings.beginGroup("MiddleClickLoader") + self.ui.loadMode.setCurrentIndex(int(settings.value("LoadMode", MCL_LoadMode.NEW_TAB))) + self.ui.onlyValidUrl.setChecked(bool(settings.value("OnlyValidUrl", True))) + settings.endGroup() + + self.ui.buttonBox.accepted.connect(self.accept) + self.ui.buttonBox.rejected.connect(self.reject) + + def accept(self): + settings = QtCore.QSettings(self.settingsFile, QtCore.QSettings.IniFormat) + settings.beginGroup("MiddleClickLoader") + settings.setValue("LoadMode", self.ui.loadMode.currentIndex()) + settings.setValue("OnlyValidUrl", self.ui.onlyValidUrl.isChecked()) + settings.endGroup() + + super().accept() + self.close() diff --git a/src/scripts/middleclickloader/mcl_settings.ui b/src/scripts/middleclickloader/mcl_settings.ui new file mode 100644 index 000000000..2ae85ee0d --- /dev/null +++ b/src/scripts/middleclickloader/mcl_settings.ui @@ -0,0 +1,105 @@ + + + MCL_Settings + + + + 0 + 0 + 379 + 194 + + + + MiddleClickLoader Settings + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + <h2>MiddleClickLoader</h2> + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + Open url in: + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + + Use only valid url + + + false + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + diff --git a/src/scripts/middleclickloader/metadata.desktop b/src/scripts/middleclickloader/metadata.desktop new file mode 100644 index 000000000..ca7de41f4 --- /dev/null +++ b/src/scripts/middleclickloader/metadata.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Name=MiddleClickLoader +Comment=Load text in selection clipboard as URL + +Icon= +Type=Service + +X-Falkon-Author=Juraj Oravec +X-Falkon-Email=sgd.orava@gmail.com +X-Falkon-Version=0.2.0 +X-Falkon-Settings=true