Changeset View
Changeset View
Standalone View
Standalone View
src/scripts/runaction/action.py
Show All 10 Lines | |||||
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | # GNU General Public License for more details. | 13 | # GNU General Public License for more details. | ||
14 | # | 14 | # | ||
15 | # You should have received a copy of the GNU General Public License | 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/>. | 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | # ============================================================ | 17 | # ============================================================ | ||
18 | import Falkon | 18 | import Falkon | ||
19 | import os, re, enum, shlex | 19 | import os | ||
20 | import re | ||||
21 | import enum | ||||
22 | import shlex | ||||
20 | from PySide2 import QtCore, QtGui | 23 | from PySide2 import QtCore, QtGui | ||
21 | 24 | | |||
25 | | ||||
22 | class Action(): | 26 | class Action(): | ||
23 | class Type(enum.Enum): | 27 | class Type(enum.Enum): | ||
24 | Invalid, Url, Command = range(3) | 28 | Invalid, Url, Command = range(3) | ||
25 | 29 | | |||
26 | class TypeCondition(enum.Enum): | 30 | class TypeCondition(enum.Enum): | ||
27 | Page, Link, Image, Media, Text = range(5) | 31 | Page, Link, Image, Media, Text = range(5) | ||
28 | 32 | | |||
29 | id = "" | 33 | id = "" | ||
Show All 18 Lines | 45 | def __init__(self, fileName): | |||
48 | self.typeCondition = list(map(lambda s: Action.TypeCondition[s], data.value("X-RunAction-TypeCondition").split(";"))) | 52 | self.typeCondition = list(map(lambda s: Action.TypeCondition[s], data.value("X-RunAction-TypeCondition").split(";"))) | ||
49 | self.urlCondition = data.value("X-RunAction-UrlCondition") or self.urlCondition | 53 | self.urlCondition = data.value("X-RunAction-UrlCondition") or self.urlCondition | ||
50 | self.submenu = data.value("X-RunAction-Submenu") or self.submenu | 54 | self.submenu = data.value("X-RunAction-Submenu") or self.submenu | ||
51 | self.normalExec = data.value("X-RunAction-Exec") or self.normalExec | 55 | self.normalExec = data.value("X-RunAction-Exec") or self.normalExec | ||
52 | self.textExec = data.value("X-RunAction-TextExec") or self.normalExec | 56 | self.textExec = data.value("X-RunAction-TextExec") or self.normalExec | ||
53 | self.supported = data.tryExec() | 57 | self.supported = data.tryExec() | ||
54 | 58 | | |||
55 | def testAction(self, condition, url): | 59 | def testAction(self, condition, url): | ||
56 | if not self.supported: return False | 60 | if not self.supported: | ||
57 | if not condition in self.typeCondition: return False | 61 | return False | ||
58 | if not re.match(self.urlCondition, url.toString()): return False | 62 | if condition not in self.typeCondition: | ||
63 | return False | ||||
64 | if not re.match(self.urlCondition, url.toString()): | ||||
65 | return False | ||||
59 | return True | 66 | return True | ||
60 | 67 | | |||
61 | def execAction(self, url, text=""): | 68 | def execAction(self, url, text=""): | ||
62 | url = str(url.toEncoded()) | 69 | url = str(url.toEncoded(), "utf-8") | ||
63 | if self.actionType == Action.Type.Command: | 70 | if self.actionType == Action.Type.Command: | ||
64 | url = shlex.quote(url) | 71 | url = shlex.quote(url) | ||
65 | text = shlex.quote(text) | 72 | text = shlex.quote(text) | ||
66 | elif self.actionType == Action.Type.Url: | 73 | elif self.actionType == Action.Type.Url: | ||
67 | url = str(QtCore.QUrl.toPercentEncoding(url)) | 74 | url = str(QtCore.QUrl.toPercentEncoding(url), "utf-8") | ||
68 | text = str(QtCore.QUrl.toPercentEncoding(text)) | 75 | text = str(QtCore.QUrl.toPercentEncoding(text), "utf-8") | ||
69 | command = self.normalExec if text == "" else self.textExec | 76 | command = self.normalExec if text == "" else self.textExec | ||
70 | command = command.replace("{url}", url) | 77 | command = command.replace("{url}", url) | ||
71 | command = command.replace("{text}", text) | 78 | command = command.replace("{text}", text) | ||
72 | command = command.replace("{lang}", QtCore.QLocale.system().name()[:2]) | 79 | command = command.replace("{lang}", QtCore.QLocale.system().name()[:2]) | ||
73 | return command | 80 | return command |