~ubuntu-core-dev/ubuntu-release-upgrader/trunk

« back to all changes in this revision

Viewing changes to DistUpgrade/QUrlOpener.py

  • Committer: Balint Reczey
  • Date: 2019-12-17 20:29:55 UTC
  • Revision ID: balint.reczey@canonical.com-20191217202955-nqe4xz2c54s60y59
Moved to git at https://git.launchpad.net/ubuntu-release-upgrader

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# QUrlOpener.py
2
 
# -*- Mode: Python; indent-tabs-mode: nil; tab-width: 4; coding: utf-8 -*-
3
 
#
4
 
#  Copyright (c) 2014-2018 Harald Sitter <apachelogger@kubuntu.org>
5
 
#
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.
10
 
#
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.
15
 
#
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/>.
18
 
 
19
 
try:
20
 
    # 14.04 has a broken pyqt5, so don't even try to import it and require
21
 
    # pyqt4.
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':
28
 
        raise ImportError
29
 
 
30
 
    from PyQt5.QtCore import QObject, QCoreApplication, pyqtSlot, QUrl
31
 
    from PyQt5.QtGui import QDesktopServices
32
 
except ImportError:
33
 
    from PyQt4.QtCore import QObject, QCoreApplication, pyqtSlot, QUrl
34
 
    from PyQt4.QtGui import QDesktopServices
35
 
 
36
 
import os
37
 
import subprocess
38
 
 
39
 
 
40
 
def singleton(class_):
41
 
    instances = {}
42
 
 
43
 
    def instance(*args, **kwargs):
44
 
        if class_ not in instances:
45
 
            instances[class_] = class_(*args, **kwargs)
46
 
        return instances[class_]
47
 
    return instance
48
 
 
49
 
 
50
 
@singleton
51
 
class QUrlOpener(QObject):
52
 
    def __init__(self):
53
 
        QObject.__init__(self)
54
 
        self.setParent(QCoreApplication.instance())
55
 
 
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')
63
 
 
64
 
    # NOTE: largely code copy from ReleaseNotesViewer which imports GTK.
65
 
    @pyqtSlot(QUrl)
66
 
    def openUrl(self, url):
67
 
        url = url.toString()
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]
78
 
        else:
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:
82
 
            command = ['sudo',
83
 
                       '--set-home',
84
 
                       '-u', os.environ['SUDO_USER']] + command
85
 
        subprocess.Popen(command)