Falkon Develop
Cross-platform Qt-based web browser
action.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 re
21import enum
22import shlex
23from PySide6 import QtCore, QtGui
24
25
26class Action():
27 class Type(enum.Enum):
28 Invalid, Url, Command = range(3)
29
30 class TypeCondition(enum.Enum):
31 Page, Link, Image, Media, Text = range(5)
32
33 id = ""
34 title = ""
35 menuTitle = ""
36 icon = QtGui.QIcon()
37 actionType = Type.Invalid
38 typeCondition = [ TypeCondition.Page, TypeCondition.Link, TypeCondition.Image, TypeCondition.Media ]
39 urlCondition = ".*"
40 submenu = ""
41 normalExec = ""
42 textExec = ""
43 supported = False
44
45 def __init__(self, fileName):
46 data = Falkon.DesktopFile(fileName)
47 self.id = os.path.splitext(os.path.basename(fileName))[0]
48 self.titletitle = data.name()
49 self.menuTitlemenuTitle = data.comment()
50 self.icon = QtGui.QIcon.fromTheme(data.icon(), QtGui.QIcon(os.path.join(os.path.dirname(fileName), data.icon())))
51 self.actionType = Action.Type[data.value("X-RunAction-Type")]
52 self.typeConditiontypeCondition = list(map(lambda s: Action.TypeCondition[s], data.value("X-RunAction-TypeCondition").split(";")))
53 self.urlConditionurlCondition = data.value("X-RunAction-UrlCondition") or self.urlConditionurlCondition
54 self.submenusubmenu = data.value("X-RunAction-Submenu") or self.submenusubmenu
55 self.normalExecnormalExec = data.value("X-RunAction-Exec") or self.normalExecnormalExec
56 self.textExectextExec = data.value("X-RunAction-TextExec") or self.normalExecnormalExec
57 self.supportedsupported = data.tryExec()
58
59 def testAction(self, condition, url):
60 if not self.supportedsupported:
61 return False
62 if condition not in self.typeConditiontypeCondition:
63 return False
64 if not re.match(self.urlConditionurlCondition, url.toString()):
65 return False
66 return True
67
68 def execAction(self, url, text=""):
69 url = str(url.toEncoded(), "utf-8")
70 if self.actionType == Action.Type.Command:
71 url = shlex.quote(url)
72 text = shlex.quote(text)
73 elif self.actionType == Action.Type.Url:
74 url = str(QtCore.QUrl.toPercentEncoding(url), "utf-8")
75 text = str(QtCore.QUrl.toPercentEncoding(text), "utf-8")
76 command = self.normalExecnormalExec if text == "" else self.textExectextExec
77 command = command.replace("{url}", url)
78 command = command.replace("{text}", text)
79 command = command.replace("{lang}", QtCore.QLocale.system().name()[:2])
80 return command
def execAction(self, url, text="")
Definition: action.py:68
def __init__(self, fileName)
Definition: action.py:45
def testAction(self, condition, url)
Definition: action.py:59