File indexing completed on 2024-05-12 04:58:40

0001 # ============================================================
0002 # MiddleClickLoader - plugin for Falkon
0003 # Copyright (C) 2018 Juraj Oravec <sgd.orava@gmail.com>
0004 #
0005 # This program is free software: you can redistribute it and/or modify
0006 # it under the terms of the GNU General Public License as published by
0007 # the Free Software Foundation, either version 3 of the License, or
0008 # (at your option) any later version.
0009 #
0010 # This program is distributed in the hope that it will be useful,
0011 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 # GNU General Public License for more details.
0014 #
0015 # You should have received a copy of the GNU General Public License
0016 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 # ============================================================
0018 
0019 import Falkon
0020 from PySide6 import QtCore, QtGui, QtWidgets
0021 
0022 from middleclickloader.mcl_loadmode import MCL_LoadMode
0023 from middleclickloader.mcl_settings import MCL_Settings
0024 
0025 
0026 class MCL_Handler(QtCore.QObject):
0027     settingsFile = ""
0028     loaded = False
0029 
0030     onlyValidUrl = False
0031     loadMode = 0
0032 
0033     def __init__(self, settingsPath, parent=None):
0034         super().__init__(parent)
0035 
0036         self.settingsFile = settingsPath + "/extensions.ini"
0037 
0038     def loadSettings(self):
0039         settings = QtCore.QSettings(self.settingsFile, QtCore.QSettings.IniFormat)
0040         settings.beginGroup("MiddleClickLoader")
0041         self.loadMode = int(settings.value("LoadMode", MCL_LoadMode.NEW_TAB))
0042         self.onlyValidUrl = bool(settings.value("OnlyValidUrl", True))
0043         settings.endGroup()
0044 
0045         self.loaded = True
0046 
0047     def showSettings(self, parent=None):
0048         self.settings = MCL_Settings(self.settingsFile, parent)
0049         self.settings.accepted.connect(self.loadSettings)
0050 
0051         self.settings.exec_()
0052 
0053     def mousePress(self, view, event):
0054         if not self.loaded:
0055             self.loadSettings()
0056 
0057         if event.buttons() == QtCore.Qt.MiddleButton:
0058             res = view.page().hitTestContent(event.pos())
0059 
0060             if res.isContentEditable() or not res.linkUrl().isEmpty():
0061                 return False
0062 
0063             selectionClipboard = QtWidgets.QApplication.clipboard().text(QtGui.QClipboard.Selection)
0064 
0065             if selectionClipboard:
0066                 guessedUrl = QtCore.QUrl.fromUserInput(selectionClipboard)
0067                 isValid = view.isUrlValid(guessedUrl)
0068 
0069                 if self.onlyValidUrl and not isValid:
0070                     return False
0071 
0072                 if not isValid:
0073                     searchManager = Falkon.MainApplication.instance().searchEnginesManager()
0074                     engine = searchManager.defaultEngine()
0075                     req = searchManager.searchResult(engine, selectionClipboard)
0076                     guessedUrl = req.url()
0077 
0078                 return self.loadUrl(view, guessedUrl)
0079         return False
0080 
0081     def loadUrl(self, view, url):
0082         if self.loadMode == MCL_LoadMode.NEW_TAB:
0083             view.openUrlInNewTab(url, Falkon.Qz.NT_NotSelectedTab)
0084         elif self.loadMode == MCL_LoadMode.CURRENT_TAB:
0085             view.load(url)
0086         elif self.loadMode == MCL_LoadMode.NEW_WINDOW:
0087             Falkon.MainApplication.instance().createWindow(Falkon.Qz.BW_NewWindow, url)
0088         else:
0089             return False
0090         return True