mirror of
https://invent.kde.org/network/falkon.git
synced 2024-11-11 01:22:10 +01:00
[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
This commit is contained in:
parent
1cc053c7f1
commit
82c8d50f9a
|
@ -11,3 +11,4 @@ endfunction()
|
|||
|
||||
install_python_script(hellopython)
|
||||
install_python_script(runaction)
|
||||
install_python_script(middleclickloader)
|
||||
|
|
11
src/scripts/middleclickloader/Messages.sh
Normal file
11
src/scripts/middleclickloader/Messages.sh
Normal file
|
@ -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
|
51
src/scripts/middleclickloader/__init__.py
Normal file
51
src/scripts/middleclickloader/__init__.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
# ============================================================
|
||||
# MiddleClickLoader - plugin for Falkon
|
||||
# Copyright (C) 2018 Juraj Oravec <sgd.orava@gmail.com>
|
||||
#
|
||||
# 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 <http://www.gnu.org/licenses/>.
|
||||
# ============================================================
|
||||
|
||||
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())
|
90
src/scripts/middleclickloader/mcl_handler.py
Normal file
90
src/scripts/middleclickloader/mcl_handler.py
Normal file
|
@ -0,0 +1,90 @@
|
|||
# ============================================================
|
||||
# MiddleClickLoader - plugin for Falkon
|
||||
# Copyright (C) 2018 Juraj Oravec <sgd.orava@gmail.com>
|
||||
#
|
||||
# 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 <http://www.gnu.org/licenses/>.
|
||||
# ============================================================
|
||||
|
||||
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
|
25
src/scripts/middleclickloader/mcl_loadmode.py
Normal file
25
src/scripts/middleclickloader/mcl_loadmode.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
# ============================================================
|
||||
# MiddleClickLoader - plugin for Falkon
|
||||
# Copyright (C) 2018 Juraj Oravec <sgd.orava@gmail.com>
|
||||
#
|
||||
# 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 <http://www.gnu.org/licenses/>.
|
||||
# ============================================================
|
||||
|
||||
from enum import IntEnum
|
||||
|
||||
|
||||
class MCL_LoadMode(IntEnum):
|
||||
NEW_TAB = 0
|
||||
CURRENT_TAB = 1
|
||||
NEW_WINDOW = 2
|
69
src/scripts/middleclickloader/mcl_settings.py
Normal file
69
src/scripts/middleclickloader/mcl_settings.py
Normal file
|
@ -0,0 +1,69 @@
|
|||
# ============================================================
|
||||
# MiddleClickLoader - plugin for Falkon
|
||||
# Copyright (C) 2018 Juraj Oravec <sgd.orava@gmail.com>
|
||||
#
|
||||
# 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 <http://www.gnu.org/licenses/>.
|
||||
# ============================================================
|
||||
|
||||
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("<h2>{}</h2>".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()
|
105
src/scripts/middleclickloader/mcl_settings.ui
Normal file
105
src/scripts/middleclickloader/mcl_settings.ui
Normal file
|
@ -0,0 +1,105 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>MCL_Settings</class>
|
||||
<widget class="QWidget" name="MiddleClickLoaderSettings">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>379</width>
|
||||
<height>194</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>MiddleClickLoader Settings</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||
<item>
|
||||
<spacer name="horizontalSpacer_2">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="label_header">
|
||||
<property name="text">
|
||||
<string><h2>MiddleClickLoader</h2></string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="horizontalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<widget class="QLabel" name="label_loadMode">
|
||||
<property name="text">
|
||||
<string>Open url in:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="horizontalSpacer_6">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QComboBox" name="loadMode"/>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QCheckBox" name="onlyValidUrl">
|
||||
<property name="text">
|
||||
<string>Use only valid url</string>
|
||||
</property>
|
||||
<property name="tristate">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QDialogButtonBox" name="buttonBox">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="standardButtons">
|
||||
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
11
src/scripts/middleclickloader/metadata.desktop
Normal file
11
src/scripts/middleclickloader/metadata.desktop
Normal file
|
@ -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
|
Loading…
Reference in New Issue
Block a user