~blueyed/apport/bug532944-lucid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/python

'''QT4 Apport user interface.

Copyright (C) 2007 Michael Hofmann <mh21@piware.de>

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 2 of the License, or (at your
option) any later version.  See http://www.gnu.org/copyleft/gpl.html for
the full text of the license.
'''

# FIXME: taskbar entry, header for problem details treewidget, binary data

import os.path, sys, subprocess
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import uic
from gettext import gettext as _
import apport.ui

class QT4Dialog(QDialog):
    '''QT4 dialog wrapper.'''

    def __init__(self, ui, title, heading, text):
        QDialog.__init__(self)

        dialog = uic.loadUi(os.path.join(os.path.dirname(sys.argv[0]), ui), self)

        self.setWindowTitle (title)
        self.findChild(QLabel, 'heading').setText('<h2>%s</h2>' % heading)
        self.findChild(QLabel, 'text').setText(text)

    def on_buttons_clicked(self, button):
        self.actionButton = button
        if self.sender().buttonRole(button) == QDialogButtonBox.ActionRole:
            button.window().done (2)

    def addbutton(self, button):
        return self.findChild(QDialogButtonBox, 'buttons').addButton(
                button, QDialogButtonBox.ActionRole)

class QT4ErrorDialog(QT4Dialog):
    '''QT4 Error dialog wrapper.'''

    def __init__(self, title, heading, text, checker = None):
        QT4Dialog.__init__(self, 'error.ui', title, heading, text)

        self.setMaximumSize(1, 1)
        self.findChild(QLabel, 'icon').setPixmap(QMessageBox.standardIcon
                (QMessageBox.Critical))

        self.checker = self.findChild(QCheckBox, 'checker')
        if checker:
            self.checker.setText(checker)
        else:
            self.checker.hide()

    def checked(self):
        return self.checker.isChecked()

class QT4ProgressDialog(QT4Dialog):
    '''QT4 Progress dialog wrapper.'''

    def __init__(self, title, heading, text):
        QT4Dialog.__init__(self, 'progress.ui', title, heading, text)

        self.setMaximumSize(1, 1)

    def on_buttons_clicked(self, button):
        QT4Dialog.on_buttons_clicked(self, button)
        if self.sender().buttonRole(button) == QDialogButtonBox.RejectRole:
            sys.exit(0)

    def set(self, value = None):
        progress = self.findChild(QProgressBar, 'progress')
        if not value:
            progress.setRange(0, 0)
            progress.setValue(0)
        else:
            progress.setRange(0, 1000)
            progress.setValue(value * 1000)

class QT4UserInterface(apport.ui.UserInterface):
    '''QT4 UserInterface.'''

    def w(self, widget):
        '''Shortcut for getting a widget.'''

        return self.widgets.get_widget(widget)

    def __init__(self):
        apport.ui.UserInterface.__init__(self)

        self.app = QApplication([])

    #
    # ui_* implementation of abstract UserInterface classes
    #

    def ui_present_crash(self, desktop_entry):
        # adapt dialog heading and label appropriately
        if desktop_entry:
            name = desktop_entry.getName()
            heading = _('Sorry, %s closed unexpectedly.') % name
        elif self.report.has_key('ExecutablePath'):
            name = os.path.basename(self.report['ExecutablePath'])
            heading = _('Sorry, the program "%s" closed unexpectedly.') % name
        else:
            name = self.cur_package
            heading = _('Sorry, %s closed unexpectedly.') % name

        dialog = QT4ErrorDialog (name, heading,
                _('If you were not doing anything confidential (entering '
                'passwords or other private information), you can help to '
                'improve the application by reporting the problem.'), 
                _('&Ignore future crashes of this program version'))

        reportbutton = dialog.addbutton(_('&Report Problem...'))

        if desktop_entry and self.report.has_key('ExecutablePath') and \
            os.path.dirname(self.report['ExecutablePath']) in \
            os.environ['PATH'].split(':') and \
            subprocess.call(['pgrep', '-x',
                os.path.basename(self.report['ExecutablePath']),
                '-u', str(os.geteuid())], stdout=subprocess.PIPE) != 0:
            restartButton = dialog.addbutton(_('Restart &Program'))

        # show crash notification dialog
        response = dialog.exec_()
        blacklist = dialog.checked()

        if response == QDialog.Rejected:
            return {'action': 'cancel', 'blacklist': blacklist}
        if dialog.actionButton == reportbutton:
            return {'action': 'report', 'blacklist': blacklist}
        if dialog.actionButton == restartbutton:
            return {'action': 'restart', 'blacklist': blacklist}
        # Fallback
        return {'action': 'cancel', 'blacklist': blacklist}

    def ui_present_package_error(self):
        dialog = QT4ErrorDialog (self.report['Package'], 
                _('Sorry, the package "%s" failed to install or upgrade.') % 
                name,
                _('You can help the developers to fix the package by '
                    'reporting the problem.'))

        reportbutton = dialog.addbutton(_('&Report Problem...'))

        response = dialog.exec_()

        if response == QDialog.Rejected:
            return 'cancel'
        if dialog.actionButton == reportbutton:
            return 'report'
        # Fallback
        return 'cancel'

    def ui_present_kernel_error(self):
        dialog = QT4ErrorDialog (_('Kernel Error'),
                _('Sorry, the kernel encountered a serious problem'),
                _('Your system might become unstable now and might need to be '
                    'restarted.\n\nYou can help the developers to fix the '
                    'problem by reporting it.'))

        reportbutton = dialog.addbutton(_('&Report Problem...'))

        response = dialog.exec_()

        if response == QDialog.Rejected:
            return 'cancel'
        if dialog.actionButton == reportbutton:
            return 'report'
        # Fallback
        return 'cancel'

    def ui_present_report_details(self):
        # Let's get the dialog and modify it to our needs
        dialog = QT4Dialog ('bugreport.ui', 
                self.report.get('Package', 'Ubuntu').split()[0],
                _('Send problem report to the developers?'),
                _('After the problem report has been sent, please fill out '
                    'the form in the automatically opening web browser.'))

        sendbutton = dialog.addbutton(_('&Send'))

        # report contents in expander
        details = dialog.findChild (QTreeWidget, 'details')
        for key in self.report:
            keyitem = QTreeWidgetItem([key]);
            details.addTopLevelItem (keyitem)

            if self.report[key]:
                lines = self.report[key].splitlines()
                for line in lines:
                    QTreeWidgetItem(keyitem, [line])
                if len(lines) < 4:
                    keyitem.setExpanded(True)
            else:
                QTreeWidgetItem (keyitem, _('(binary data)'))

        # complete/reduced radio buttons
        if self.report.has_key('CoreDump'):
            dialog.findChild(QRadioButton, 'complete').setText(
                    _('Complete report (recommended; %s)') % 
                    self.format_filesize(self.get_complete_size()))
            dialog.findChild(QRadioButton, 'reduced').setText(
                    _('Reduced report (slow Internet connection; %s)') % 
                    self.format_filesize(self.get_reduced_size()))
        else:
            dialog.findChild(QFrame, 'options').hide()

        response = dialog.exec_()

        if response == QDialog.Rejected:
            return 'cancel'
        # Fallback
        if dialog.actionButton != sendbutton:
            return 'cancel'
        if dialog.findChild(QRadioButton, 'reduced').isChecked():
            return 'reduced'
        if dialog.findChild(QRadioButton, 'complete').isChecked():
            return 'full'
        # Fallback
        return 'undefined'

    def ui_info_message(self, title, text):
        QMessageBox.information (None, title, text, 
                QMessageBox.Close, QMessageBox.Close)

    def ui_error_message(self, title, text):
        QMessageBox.critical (None, title, text, 
                QMessageBox.Close, QMessageBox.Close)

    def ui_start_info_collection_progress(self):
        self.progress = QT4ProgressDialog (
                _('Collecting Problem Information'), 
                _('Collecting problem information'), 
                _('The collected information can be send to the developers to '
                    'improve the application. This might take some minutes.'))
        self.progress.set()
        self.progress.show()

    def ui_pulse_info_collection_progress(self):
        self.progress.set()
        QCoreApplication.processEvents()

    def ui_stop_info_collection_progress(self):
        self.progress.hide()

    def ui_start_upload_progress(self):
        '''Open a window with an definite progress bar, telling the user to
        wait while debug information is being uploaded.'''

        self.progress = QT4ProgressDialog (
                _('Uploading Problem Information'), 
                _('Uploading problem information'), 
                _('The collected information is sent to the bug tracking '
                    'system. This might take some minutes.'))
        self.progress.show()

    def ui_set_upload_progress(self, progress):
        '''Set the progress bar in the debug data upload progress
        window to the given ratio (between 0 and 1, or None for indefinite
        progress).

        This function is called every 100 ms.'''

        if progress:
            self.progress.set(progress)
        else:
            self.progress.set()
        QCoreApplication.processEvents()

    def ui_stop_upload_progress(self):
        '''Close debug data upload progress window.'''

        self.progress.hide()

if __name__ == '__main__':
    app = QT4UserInterface()
    app.run_argv()