~roadmr/ubuntu/precise/checkbox/0.13.1

« back to all changes in this revision

Viewing changes to checkbox/user_interface.py

  • Committer: Bazaar Package Importer
  • Author(s): Marc Tardif
  • Date: 2009-01-20 18:55:20 UTC
  • Revision ID: james.westby@ubuntu.com-20090120185520-s18m2hninrt53fki
Tags: 0.5
* New upstream version:
  * Added concept of hyper text view to display clickable links.
  * Added concept of properties to components.
  * Added pci information to launchpad report.
  * Added dmi information to launchpad report.
  * Added text area to keyboard test.
  * Removed sourcing of base postrm script.
  * Updated translations from Launchpad.
* Fixed handling of interrupt signal (LP: #327810)
* Fixed display of text in graphical interface (LP: #240374)
* Fixed support for regexes in blacklist and whitelist (LP: #327177)
* Fixed opening of subunit log file (LP: #325737)
* Fixed internet test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#
2
 
# Copyright (c) 2008 Canonical
3
 
#
4
 
# Written by Marc Tardif <marc@interunion.ca>
5
 
#
6
2
# This file is part of Checkbox.
7
3
#
 
4
# Copyright 2008 Canonical Ltd.
 
5
#
8
6
# Checkbox is free software: you can redistribute it and/or modify
9
7
# it under the terms of the GNU General Public License as published by
10
8
# the Free Software Foundation, either version 3 of the License, or
18
16
# You should have received a copy of the GNU General Public License
19
17
# along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
20
18
#
 
19
import re
 
20
import os
 
21
import pwd
21
22
import sys
 
23
import logging
 
24
import subprocess
 
25
import webbrowser
 
26
 
22
27
import gettext
23
 
import logging
 
28
from gettext import gettext as _
24
29
 
25
30
from checkbox.contrib.REThread import REThread
26
31
 
 
32
from checkbox.lib.environ import add_variable, get_variable, remove_variable
27
33
from checkbox.lib.iterator import NEXT
28
34
 
29
35
 
33
39
 
34
40
       A concrete subclass must implement all the abstract show_* methods."""
35
41
 
36
 
    def __init__(self, config):
37
 
        self._config = config
 
42
    def __init__(self, title, data_path=None):
 
43
        self.title = title
 
44
        self.data_path = data_path
38
45
 
39
46
        self.direction = NEXT
40
47
        self.gettext_domain = "checkbox"
47
54
 
48
55
        while thread.isAlive():
49
56
            self.show_pulse()
50
 
            try:
51
 
                thread.join(0.1)
52
 
            except KeyboardInterrupt:
53
 
                sys.exit(1)
 
57
            thread.join(0.1)
54
58
        thread.exc_raise()
55
59
 
56
60
        return thread.return_value()
76
80
        raise NotImplementedError, \
77
81
            "this function must be overridden by subclasses"
78
82
 
79
 
    def show_exchange(self, authentication, reports=[], message=None,
80
 
                      error=None):
 
83
    def show_exchange(self, authentication, reports=[], message=None):
81
84
        raise NotImplementedError, \
82
85
            "this function must be overridden by subclasses"
83
86
 
84
87
    def show_final(self, message):
85
88
        raise NotImplementedError, \
86
89
            "this function must be overridden by subclasses"
 
90
 
 
91
    def show_url(self, url):
 
92
        """Open the given URL in a new browser window.
 
93
 
 
94
        Display an error dialog if everything fails."""
 
95
 
 
96
        (r, w) = os.pipe()
 
97
        if os.fork() > 0:
 
98
            os.close(w)
 
99
            (pid, status) = os.wait()
 
100
            if status:
 
101
                title = _("Unable to start web browser")
 
102
                error = _("Unable to start web browser to open %s." % url)
 
103
                message = os.fdopen(r).readline()
 
104
                if message:
 
105
                    error += "\n" + message
 
106
                self.show_error(title, error)
 
107
            try:
 
108
                os.close(r)
 
109
            except OSError:
 
110
                pass
 
111
            return
 
112
 
 
113
        os.setsid()
 
114
        os.close(r)
 
115
 
 
116
        # If we are called through sudo, determine the real user id and run the
 
117
        # browser with it to get the user's web browser settings.
 
118
        try:
 
119
            uid = int(get_variable("SUDO_UID"))
 
120
            gid = int(get_variable("SUDO_GID"))
 
121
            sudo_prefix = ["sudo", "-H", "-u", "#%s" % uid]
 
122
        except (TypeError):
 
123
            uid = os.getuid()
 
124
            gid = None
 
125
            sudo_prefix = []
 
126
 
 
127
        # figure out appropriate web browser
 
128
        try:
 
129
            # if ksmserver is running, try kfmclient
 
130
            try:
 
131
                if os.getenv("DISPLAY") and \
 
132
                        subprocess.call(["pgrep", "-x", "-u", str(uid), "ksmserver"],
 
133
                                stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0:
 
134
                    subprocess.call(sudo_prefix + ["kfmclient", "openURL", url])
 
135
                    sys.exit(0)
 
136
            except OSError:
 
137
                pass
 
138
 
 
139
            # if gnome-session is running, try gnome-open; special-case firefox
 
140
            # (and more generally, mozilla browsers) and epiphany to open a new window
 
141
            # with respectively -new-window and --new-window
 
142
            try:
 
143
                if os.getenv("DISPLAY") and \
 
144
                        subprocess.call(["pgrep", "-x", "-u", str(uid), "gnome-panel"],
 
145
                                stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0:
 
146
                    gct = subprocess.Popen(sudo_prefix + ["gconftool", "--get",
 
147
                        "/desktop/gnome/url-handlers/http/command"],
 
148
                        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
149
                    if gct.returncode == 0:
 
150
                        preferred_browser = gct.communicate()[0]
 
151
                        browser = re.match("((firefox|seamonkey|flock)[^\s]*)", preferred_browser)
 
152
                        if browser:
 
153
                            subprocess.call(sudo_prefix + [browser.group(0), "-new-window", url])
 
154
                            sys.exit(0)
 
155
                        browser = re.match("(epiphany[^\s]*)", preferred_browser)
 
156
                        if browser:
 
157
                            subprocess.call(sudo_prefix + [browser.group(0), "--new-window", url])
 
158
                            sys.exit(0)
 
159
                    if subprocess.call(sudo_prefix + ["gnome-open", url]) == 0:
 
160
                        sys.exit(0)
 
161
            except OSError:
 
162
                pass
 
163
 
 
164
            # fall back to webbrowser
 
165
            if uid and gid:
 
166
                os.setgroups([gid])
 
167
                os.setgid(gid)
 
168
                os.setuid(uid)
 
169
                remove_variable("SUDO_USER")
 
170
                add_variable("HOME", pwd.getpwuid(uid).pw_dir)
 
171
 
 
172
            webbrowser.open(url, new=True, autoraise=True)
 
173
            sys.exit(0)
 
174
 
 
175
        except Exception, e:
 
176
            os.write(w, str(e))
 
177
            sys.exit(1)