You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

73 lines
3.1 KiB

# ============================================================
# RunAction plugin for Falkon
# Copyright (C) 2018 David Rosca <nowrep@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# ============================================================
import Falkon
import os, re, enum, shlex
from PySide2 import QtCore, QtGui
class Action():
class Type(enum.Enum):
Invalid, Url, Command = range(3)
class TypeCondition(enum.Enum):
Page, Link, Image, Media, Text = range(5)
id = ""
title = ""
menuTitle = ""
icon = QtGui.QIcon()
actionType = Type.Invalid
typeCondition = [ TypeCondition.Page, TypeCondition.Link, TypeCondition.Image, TypeCondition.Media ]
urlCondition = ".*"
submenu = ""
normalExec = ""
textExec = ""
supported = False
def __init__(self, fileName):
data = Falkon.DesktopFile(fileName)
self.id = os.path.splitext(os.path.basename(fileName))[0]
self.title = data.name()
self.menuTitle = data.comment()
self.icon = QtGui.QIcon.fromTheme(data.icon(), QtGui.QIcon(os.path.join(os.path.dirname(fileName), data.icon())))
self.actionType = Action.Type[data.value("X-RunAction-Type")]
self.typeCondition = list(map(lambda s: Action.TypeCondition[s], data.value("X-RunAction-TypeCondition").split(";")))
self.urlCondition = data.value("X-RunAction-UrlCondition") or self.urlCondition
self.submenu = data.value("X-RunAction-Submenu") or self.submenu
self.normalExec = data.value("X-RunAction-Exec") or self.normalExec
self.textExec = data.value("X-RunAction-TextExec") or self.normalExec
self.supported = data.tryExec()
def testAction(self, condition, url):
if not self.supported: return False
if not condition in self.typeCondition: return False
if not re.match(self.urlCondition, url.toString()): return False
return True
def execAction(self, url, text=""):
url = str(url.toEncoded())
if self.actionType == Action.Type.Command:
url = shlex.quote(url)
text = shlex.quote(text)
elif self.actionType == Action.Type.Url:
url = str(QtCore.QUrl.toPercentEncoding(url))
text = str(QtCore.QUrl.toPercentEncoding(text))
command = self.normalExec if text == "" else self.textExec
command = command.replace("{url}", url)
command = command.replace("{text}", text)
command = command.replace("{lang}", QtCore.QLocale.system().name()[:2])
return command