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

0001 # ============================================================
0002 # RunAction plugin for Falkon
0003 # Copyright (C) 2018 David Rosca <nowrep@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 import Falkon
0019 import os
0020 import subprocess
0021 from PySide6 import QtCore
0022 from runaction.action import Action
0023 from runaction.settingsdialog import SettingsDialog
0024 
0025 
0026 class ActionManager(QtCore.QObject):
0027     actions = []
0028 
0029     def __init__(self, settingsPath, parent=None):
0030         super().__init__(parent)
0031 
0032         self.settingsPath = settingsPath
0033         settings = QtCore.QSettings(self.settingsPath + "/extensions.ini", QtCore.QSettings.IniFormat)
0034         self._disabledActions = settings.value("RunAction/disabledActions") or []
0035         self.loadActions()
0036 
0037     def getActions(self, webView, r=None):
0038         out = []
0039         menus = {}
0040 
0041         for action in list(filter(lambda a: a.id not in self.disabledActions, self.actions)):
0042             url = webView.url()
0043             text = ""
0044             if r and webView.selectedText():
0045                 cond = Action.TypeCondition.Text
0046                 text = webView.selectedText()
0047             elif r and not r.linkUrl().isEmpty():
0048                 cond = Action.TypeCondition.Link
0049                 url = r.linkUrl()
0050             elif r and not r.imageUrl().isEmpty():
0051                 cond = Action.TypeCondition.Image
0052                 url = r.imageUrl()
0053             elif r and not r.mediaUrl().isEmpty():
0054                 cond = Action.TypeCondition.Media
0055                 url = r.mediaUrl()
0056             else:
0057                 cond = Action.TypeCondition.Page
0058 
0059             if action.testAction(cond, url):
0060                 act = Falkon.Action(action.icon, action.title, self)
0061                 act.triggered.connect(lambda a=action, w=webView, u=url, t=text: self.execAction(a, w, u, t))
0062                 if action.submenu:
0063                     if action.submenu not in menus:
0064                         menu = Falkon.Menu(action.menuTitle, webView)
0065                         menus[action.submenu] = menu
0066                         out.append(menu)
0067                     menus[action.submenu].addAction(act)
0068                 else:
0069                     out.append(act)
0070 
0071         return out
0072 
0073     @property
0074     def disabledActions(self):
0075         return self._disabledActions
0076 
0077     @disabledActions.setter
0078     def disabledActions(self, value):
0079         settings = QtCore.QSettings(self.settingsPath + "/extensions.ini", QtCore.QSettings.IniFormat)
0080         settings.setValue("RunAction/disabledActions", value)
0081         self._disabledActions = value
0082 
0083     def showSettings(self, parent=None):
0084         dialog = SettingsDialog(self, parent)
0085         dialog.exec_()
0086 
0087     def execAction(self, action, webView, url, text=""):
0088         command = action.execAction(url, text)
0089         if action.actionType == Action.Type.Command:
0090             subprocess.Popen(command, shell=True)
0091         elif action.actionType == Action.Type.Url:
0092             webView.openUrlInNewTab(QtCore.QUrl(command), Falkon.Qz.NT_SelectedTab)
0093 
0094     def loadActions(self):
0095         self.actions = []
0096 
0097         paths = [
0098             os.path.join(os.path.dirname(__file__), "actions"),
0099             os.path.join(self.settingsPath, "runaction")
0100         ]
0101 
0102         for path in paths:
0103             if not os.path.exists(path):
0104                 continue
0105             for file in os.listdir(path):
0106                 if not file.endswith(".desktop"):
0107                     continue
0108                 fileName = os.path.join(path, file)
0109                 try:
0110                     action = Action(fileName)
0111                 except Exception as e:
0112                     print("Failed to parse {}: {}".format(fileName, e))
0113                 finally:
0114                     if action.supported:
0115                         self.actions.append(action)