2
# -*- Mode: Python; indent-tabs-mode: nil; tab-width: 4; coding: utf-8 -*-
4
# Copyright (c) 2014-2018 Harald Sitter <apachelogger@kubuntu.org>
6
# This program is free software; you can redistribute it and/or
7
# modify it under the terms of the GNU General Public License as
8
# published by the Free Software Foundation; either version 2 of the
9
# License, or (at your option) any later version.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
# 14.04 has a broken pyqt5, so don't even try to import it and require
22
# In 14.04 various signals in pyqt5 can not be connected because it thinks
23
# the signal does not exist or has an incompatible signature. Since this
24
# potentially renders the GUI entirely broken and pyqt5 was not actively
25
# used back then it is fair to simply require qt4 on trusty systems.
26
from .utils import get_dist
27
if get_dist() == 'trusty':
30
from PyQt5.QtCore import QObject, QCoreApplication, pyqtSlot, QUrl
31
from PyQt5.QtGui import QDesktopServices
33
from PyQt4.QtCore import QObject, QCoreApplication, pyqtSlot, QUrl
34
from PyQt4.QtGui import QDesktopServices
40
def singleton(class_):
43
def instance(*args, **kwargs):
44
if class_ not in instances:
45
instances[class_] = class_(*args, **kwargs)
46
return instances[class_]
51
class QUrlOpener(QObject):
53
QObject.__init__(self)
54
self.setParent(QCoreApplication.instance())
56
def setupUrlHandles(self):
57
# Make sure we don't run a root browser.
58
# NOTE: Qt native API can set an openUrl handler from a QObject
59
# function, pyqt in theory also allows an arbitrary callable. Latter
60
# has been observed to be non-functional so rely on the native handling
61
QDesktopServices.setUrlHandler('http', self, 'openUrl')
62
QDesktopServices.setUrlHandler('https', self, 'openUrl')
64
# NOTE: largely code copy from ReleaseNotesViewer which imports GTK.
66
def openUrl(self, url):
68
"""Open the specified URL in a browser"""
69
# Find an appropiate browser
70
if os.path.exists("/usr/bin/xdg-open"):
71
command = ["xdg-open", url]
72
elif os.path.exists("/usr/bin/kde-open"):
73
command = ["kde-open", url]
74
elif os.path.exists("/usr/bin/exo-open"):
75
command = ["exo-open", url]
76
elif os.path.exists('/usr/bin/gnome-open'):
77
command = ['gnome-open', url]
79
command = ['x-www-browser', url]
80
# Avoid to run the browser as user root
81
if os.getuid() == 0 and 'SUDO_USER' in os.environ:
84
'-u', os.environ['SUDO_USER']] + command
85
subprocess.Popen(command)