Falkon Develop
Cross-platform Qt-based web browser
mcl_handler.py
Go to the documentation of this file.
1# ============================================================
2# MiddleClickLoader - plugin for Falkon
3# Copyright (C) 2018 Juraj Oravec <sgd.orava@gmail.com>
4#
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program. If not, see <http://www.gnu.org/licenses/>.
17# ============================================================
18
19import Falkon
20from PySide6 import QtCore, QtGui, QtWidgets
21
22from middleclickloader.mcl_loadmode import MCL_LoadMode
23from middleclickloader.mcl_settings import MCL_Settings
24
25
26class MCL_Handler(QtCore.QObject):
27 settingsFile = ""
28 loaded = False
29
30 onlyValidUrl = False
31 loadMode = 0
32
33 def __init__(self, settingsPath, parent=None):
34 super().__init__(parent)
35
36 self.settingsFilesettingsFile = settingsPath + "/extensions.ini"
37
38 def loadSettings(self):
39 settings = QtCore.QSettings(self.settingsFilesettingsFile, QtCore.QSettings.IniFormat)
40 settings.beginGroup("MiddleClickLoader")
41 self.loadModeloadMode = int(settings.value("LoadMode", MCL_LoadMode.NEW_TAB))
42 self.onlyValidUrlonlyValidUrl = bool(settings.value("OnlyValidUrl", True))
43 settings.endGroup()
44
45 self.loadedloaded = True
46
47 def showSettings(self, parent=None):
49 self.settings.accepted.connect(self.loadSettings)
50
51 self.settings.exec_()
52
53 def mousePress(self, view, event):
54 if not self.loadedloaded:
55 self.loadSettings()
56
57 if event.buttons() == QtCore.Qt.MiddleButton:
58 res = view.page().hitTestContent(event.pos())
59
60 if res.isContentEditable() or not res.linkUrl().isEmpty():
61 return False
62
63 selectionClipboard = QtWidgets.QApplication.clipboard().text(QtGui.QClipboard.Selection)
64
65 if selectionClipboard:
66 guessedUrl = QtCore.QUrl.fromUserInput(selectionClipboard)
67 isValid = view.isUrlValid(guessedUrl)
68
69 if self.onlyValidUrlonlyValidUrl and not isValid:
70 return False
71
72 if not isValid:
73 searchManager = Falkon.MainApplication.instance().searchEnginesManager()
74 engine = searchManager.defaultEngine()
75 req = searchManager.searchResult(engine, selectionClipboard)
76 guessedUrl = req.url()
77
78 return self.loadUrl(view, guessedUrl)
79 return False
80
81 def loadUrl(self, view, url):
82 if self.loadModeloadMode == MCL_LoadMode.NEW_TAB:
83 view.openUrlInNewTab(url, Falkon.Qz.NT_NotSelectedTab)
84 elif self.loadModeloadMode == MCL_LoadMode.CURRENT_TAB:
85 view.load(url)
86 elif self.loadModeloadMode == MCL_LoadMode.NEW_WINDOW:
87 Falkon.MainApplication.instance().createWindow(Falkon.Qz.BW_NewWindow, url)
88 else:
89 return False
90 return True
def __init__(self, settingsPath, parent=None)
Definition: mcl_handler.py:33