Falkon Develop
Cross-platform Qt-based web browser
actionmanager.py
Go to the documentation of this file.
1# ============================================================
2# RunAction plugin for Falkon
3# Copyright (C) 2018 David Rosca <nowrep@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# ============================================================
18import Falkon
19import os
20import subprocess
21from PySide6 import QtCore
22from runaction.action import Action
23from runaction.settingsdialog import SettingsDialog
24
25
26class ActionManager(QtCore.QObject):
27 actions = []
28
29 def __init__(self, settingsPath, parent=None):
30 super().__init__(parent)
31
32 self.settingsPath = settingsPath
33 settings = QtCore.QSettings(self.settingsPath + "/extensions.ini", QtCore.QSettings.IniFormat)
34 self._disabledActions = settings.value("RunAction/disabledActions") or []
35 self.loadActions()
36
37 def getActions(self, webView, r=None):
38 out = []
39 menus = {}
40
41 for action in list(filter(lambda a: a.id not in self.disabledActionsdisabledActions, self.actionsactions)):
42 url = webView.url()
43 text = ""
44 if r and webView.selectedText():
45 cond = Action.TypeCondition.Text
46 text = webView.selectedText()
47 elif r and not r.linkUrl().isEmpty():
48 cond = Action.TypeCondition.Link
49 url = r.linkUrl()
50 elif r and not r.imageUrl().isEmpty():
51 cond = Action.TypeCondition.Image
52 url = r.imageUrl()
53 elif r and not r.mediaUrl().isEmpty():
54 cond = Action.TypeCondition.Media
55 url = r.mediaUrl()
56 else:
57 cond = Action.TypeCondition.Page
58
59 if action.testAction(cond, url):
60 act = Falkon.Action(action.icon, action.title, self)
61 act.triggered.connect(lambda a=action, w=webView, u=url, t=text: self.execAction(a, w, u, t))
62 if action.submenu:
63 if action.submenu not in menus:
64 menu = Falkon.Menu(action.menuTitle, webView)
65 menus[action.submenu] = menu
66 out.append(menu)
67 menus[action.submenu].addAction(act)
68 else:
69 out.append(act)
70
71 return out
72
73 @property
74 def disabledActions(self):
75 return self._disabledActions
76
77 @disabledActions.setter
78 def disabledActions(self, value):
79 settings = QtCore.QSettings(self.settingsPath + "/extensions.ini", QtCore.QSettings.IniFormat)
80 settings.setValue("RunAction/disabledActions", value)
81 self._disabledActions = value
82
83 def showSettings(self, parent=None):
84 dialog = SettingsDialog(self, parent)
85 dialog.exec_()
86
87 def execAction(self, action, webView, url, text=""):
88 command = action.execAction(url, text)
89 if action.actionType == Action.Type.Command:
90 subprocess.Popen(command, shell=True)
91 elif action.actionType == Action.Type.Url:
92 webView.openUrlInNewTab(QtCore.QUrl(command), Falkon.Qz.NT_SelectedTab)
93
94 def loadActions(self):
96
97 paths = [
98 os.path.join(os.path.dirname(__file__), "actions"),
99 os.path.join(self.settingsPath, "runaction")
100 ]
101
102 for path in paths:
103 if not os.path.exists(path):
104 continue
105 for file in os.listdir(path):
106 if not file.endswith(".desktop"):
107 continue
108 fileName = os.path.join(path, file)
109 try:
110 action = Action(fileName)
111 except Exception as e:
112 print("Failed to parse {}: {}".format(fileName, e))
113 finally:
114 if action.supported:
115 self.actionsactions.append(action)
def execAction(self, action, webView, url, text="")
def showSettings(self, parent=None)
def getActions(self, webView, r=None)
def __init__(self, settingsPath, parent=None)