~vicamo/ubuntu/disco/apport/bug-1847967

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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#!/usr/bin/python3

'''Qt 5 Apport User Interface'''

# Copyright (C) 2015 Harald Sitter <sitter@kde.org>
# Copyright (C) 2007 - 2009 Canonical Ltd.
# Author: Richard A. Johnson <nixternal@ubuntu.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 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.

import os.path
import sys
import os

try:
    import apport
    from PyQt5.QtCore import (QByteArray,
                              QLibraryInfo,
                              QLocale,
                              Qt,
                              QTimer,
                              QTranslator)
    from PyQt5.QtGui import (QIcon,
                             QMovie,
                             QPainter)
    from PyQt5.QtWidgets import (QApplication,
                                 QCheckBox,
                                 QDialog,
                                 QDialogButtonBox,
                                 QLabel,
                                 QLineEdit,
                                 QMessageBox,
                                 QProgressBar,
                                 QPushButton,
                                 QRadioButton,
                                 QTreeWidget,
                                 QTreeWidgetItem,
                                 QFileDialog)
    from PyQt5 import uic
    import apport.ui
    from apport import unicode_gettext as _
    import sip
    # Work around for LP: 1282713
    try:
        sip.setdestroyonexit(False)
    except AttributeError:
        pass

except ImportError as e:
    # this can happen while upgrading python packages
    apport.fatal('Could not import module, is a package upgrade in progress?  Error: %s', str(e))


def translate(self, prop):
    '''Reimplement method from uic to change it to use gettext.'''

    if prop.get('notr', None) == 'true':
        return self._cstring(prop)
    else:
        if prop.text is None:
            return ''
        text = prop.text.encode('UTF-8')
        return _(text)


uic.properties.Properties._string = translate


class Dialog(QDialog):
    '''Main dialog wrapper'''

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

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

        self.setWindowTitle(title)
        if self.findChild(QLabel, 'heading'):
            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 ChoicesDialog(Dialog):
    '''Choices dialog wrapper'''

    def __init__(self, title, text):
        Dialog.__init__(self, 'choices.ui', title, None, text)

        self.setMaximumSize(1, 1)

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


class ProgressDialog(Dialog):
    '''Progress dialog wrapper'''

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

        self.setMaximumSize(1, 1)

    def on_buttons_clicked(self, button):
        Dialog.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 ReportDialog(Dialog):
    '''Report dialog wrapper'''

    def __init__(self, report, allowed_to_report, ui, desktop_info):
        if 'DistroRelease' not in report:
            report.add_os_info()
        distro = report['DistroRelease']
        Dialog.__init__(self, 'bugreport.ui', distro.split()[0], '', '')
        self.details = self.findChild(QPushButton, 'show_details')
        self.details.clicked.connect(self.on_show_details_clicked)
        self.continue_button = self.findChild(QPushButton, 'continue_button')
        self.continue_button.clicked.connect(self.on_continue_clicked)
        self.closed_button = self.findChild(QPushButton, 'closed_button')
        self.closed_button.clicked.connect(self.on_closed_clicked)
        self.examine_button = self.findChild(QPushButton, 'examine_button')
        self.examine_button.clicked.connect(self.on_examine_clicked)
        self.cancel_button = self.findChild(QPushButton, 'cancel_button')
        self.cancel_button.clicked.connect(self.on_cancel_button_clicked)
        self.treeview = self.findChild(QTreeWidget, 'details')
        self.send_error_report = self.findChild(QCheckBox, 'send_error_report')
        self.ignore_future_problems = self.findChild(QCheckBox, 'ignore_future_problems')
        self.heading = self.findChild(QLabel, 'heading')
        self.text = self.findChild(QLabel, 'text')
        self.ui = ui
        self.collect_called = False
        icon = None
        report_type = report.get('ProblemType')

        self.spinner = QLabel('', parent=self.treeview)
        self.spinner.setGeometry(0, 0, 32, 32)
        self.movie = QMovie(
            os.path.join(os.path.dirname(sys.argv[0]), 'spinner.gif'),
            QByteArray(), self.spinner)
        self.spinner.setMovie(self.movie)
        self.spinner.setVisible(False)

        if allowed_to_report:
            self.send_error_report.setChecked(True)
            self.send_error_report.show()
        else:
            self.send_error_report.setChecked(False)
            self.send_error_report.hide()

        self.examine_button.setVisible(self.ui.can_examine_locally())

        self.cancel_button.hide()
        if not self.ui.report_file:
            # This is a bug generated through `apport-bug $package`, or
            # `apport-collect $id`.

            # avoid collecting information again, in this mode we already have it
            if 'Uname' in report:
                self.collect_called = True
                self.ui.ui_update_view(self)
            self.heading.setText(_('Send problem report to the developers?'))
            self.text.hide()
            self.closed_button.hide()
            self.ignore_future_problems.hide()
            self.show_details.hide()
            self.cancel_button.show()
            self.send_error_report.setChecked(True)
            self.send_error_report.hide()
            self.continue_button.setText(_('Send'))
            self.showtree(True)

        elif report_type == 'KernelCrash' or report_type == 'KernelOops':
            self.ignore_future_problems.setChecked(False)
            self.ignore_future_problems.hide()
            self.heading.setText(_('Sorry, %s has experienced an '
                                   'internal error.') % distro)
            self.closed_button.hide()
            self.text.hide()
            icon = 'distributor-logo'
        elif report_type == 'Package':
            package = report.get('Package')
            if package:
                self.text.setText(_('Package: %s') % package)
                self.text.show()
            else:
                self.text.hide()
            self.closed_button.hide()
            self.ignore_future_problems.hide()
            self.heading.setText(_('Sorry, a problem occurred while installing software.'))
        else:
            # Regular crash.
            if desktop_info:
                icon = desktop_info.get('icon')
                if report_type == 'RecoverableProblem':
                    self.heading.setText(_('The application %s has experienced an internal error.') %
                                         desktop_info['name'])
                else:
                    self.heading.setText(_('The application %s has closed unexpectedly.') %
                                         desktop_info['name'])
                self.text.hide()

                pid = apport.ui.get_pid(report)
                still_running = pid and apport.ui.still_running(pid)
                if 'ProcCmdline' not in report or still_running or not self.ui.offer_restart:
                    self.closed_button.hide()
                    self.continue_button.setText(_('Continue'))
                else:
                    self.closed_button.show()
                    self.closed_button.setText(_('Leave Closed'))
                    self.continue_button.setText(_('Relaunch'))
            else:
                icon = 'distributor-logo'
                self.heading.setText(_('Sorry, %s has experienced an '
                                       'internal error.') % distro)
                self.text.show()
                self.text.setText(_('If you notice further problems, '
                                    'try restarting the computer.'))
                self.closed_button.hide()
                self.continue_button.setText(_('Continue'))
                self.ignore_future_problems.setText(_('Ignore future problems of this type'))
            if report.get('CrashCounter'):
                self.ignore_future_problems.show()
            else:
                self.ignore_future_problems.hide()

            if report_type == 'RecoverableProblem':
                body = report.get('DialogBody', '')
                if body:
                    del report['DialogBody']
                    # Set a maximum size for the dialog body, so developers do
                    # not try to shove entire log files into this dialog.
                    self.text.setText(body[:1024])
                    self.text.show()

        if icon:
            base = QIcon.fromTheme(icon).pixmap(42, 42)
            overlay = QIcon.fromTheme('dialog-error').pixmap(16, 16)
            p = QPainter(base)
            p.drawPixmap(base.width() - overlay.width(),
                         base.height() - overlay.height(), overlay)
            p.end()
            self.application_icon.setPixmap(base)
        else:
            self.application_icon.setPixmap(
                QIcon.fromTheme('dialog-error').pixmap(42, 42))

        if self.ui.report_file:
            self.showtree(False)

    def on_continue_clicked(self):
        self.done(1)

    def on_closed_clicked(self):
        self.done(2)

    def on_examine_clicked(self):
        self.done(3)

    def on_cancel_button_clicked(self):
        self.done(QDialog.Rejected)

    def on_show_details_clicked(self):
        if not self.treeview.isVisible():
            self.details.setText(_('Hide Details'))
            self.showtree(True)
        else:
            self.details.setText(_('Show Details'))
            self.showtree(False)

    def collect_done(self):
        self.ui.ui_update_view(self)

    def showtree(self, visible):
        self.treeview.setVisible(visible)
        if visible and not self.collect_called:
            self.ui.ui_update_view(self, ['ExecutablePath'])
            QTimer.singleShot(0, lambda: self.ui.collect_info(on_finished=self.collect_done))
            self.collect_called = True
        if visible:
            self.setMaximumSize(16777215, 16777215)
        else:
            self.setMaximumSize(1, 1)


class UserPassDialog(Dialog):
    '''Username/Password dialog wrapper'''

    def __init__(self, title, text):
        Dialog.__init__(self, 'userpass.ui', title, None, text)
        self.findChild(QLabel, 'l_username').setText(_('Username:'))
        self.findChild(QLabel, 'l_password').setText(_('Password:'))

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


class MainUserInterface(apport.ui.UserInterface):
    '''The main user interface presented to the user'''

    def __init__(self):
        apport.ui.UserInterface.__init__(self)
        # Help unit tests get at the dialog.
        self.dialog = None
        self.progress = None

    #
    # ui_* implementation of abstract UserInterface classes
    #

    def ui_update_view(self, dialog, shown_keys=None):
        # report contents
        details = dialog.findChild(QTreeWidget, 'details')
        if shown_keys:
            keys = set(self.report.keys()) & set(shown_keys)
        else:
            keys = self.report.keys()
        details.clear()
        for key in sorted(keys):
            # ignore internal keys
            if key.startswith('_'):
                continue

            keyitem = QTreeWidgetItem([key])
            details.addTopLevelItem(keyitem)

            # string value
            if not hasattr(self.report[key], 'gzipvalue') and \
               hasattr(self.report[key], 'isspace') and \
               not self.report._is_binary(self.report[key]):
                lines = self.report[key].splitlines()
                for line in lines:
                    QTreeWidgetItem(keyitem, [str(line)])
                if len(lines) < 4:
                    keyitem.setExpanded(True)
            else:
                QTreeWidgetItem(keyitem, [_('(binary data)')])

    def ui_present_report_details(self, allowed_to_report=True, modal_for=None):
        desktop_info = self.get_desktop_entry()
        self.dialog = ReportDialog(self.report, allowed_to_report, self,
                                   desktop_info)

        response = self.dialog.exec_()

        return_value = {'report': False, 'blacklist': False, 'remember': False,
                        'restart': False, 'examine': False}
        if response == QDialog.Rejected:
            return return_value
        elif response == 3:
            return_value['examine'] = True
            return return_value

        text = self.dialog.continue_button.text().replace('&', '')
        if response == 1 and text == _('Relaunch') and self.offer_restart:
            return_value['restart'] = True
        if self.dialog.send_error_report.isChecked():
            return_value['report'] = True
        if self.dialog.ignore_future_problems.isChecked():
            return_value['blacklist'] = True
        return return_value

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

    def ui_error_message(self, title, text):
        QMessageBox.information(None, _(title), _(text))

    def ui_start_info_collection_progress(self):
        # show a spinner if we already have the main window
        if self.dialog and self.dialog.isVisible():
            rect = self.dialog.spinner.parent().rect()
            self.dialog.spinner.setGeometry(rect.width() / 2 - self.dialog.spinner.width() / 2,
                                            rect.height() / 2 - self.dialog.spinner.height() / 2,
                                            self.dialog.spinner.width(), self.dialog.spinner.height())
            self.dialog.movie.start()
        elif self.crashdb.accepts(self.report):
            # show a progress dialog if our DB accepts the crash
            self.progress = ProgressDialog(
                _('Collecting Problem Information'),
                _('Collecting problem information'),
                _('The collected information can be sent to the developers '
                  'to improve the application. This might take a few '
                  'minutes.'))
            self.progress.set()
            self.progress.show()

        QApplication.processEvents()

    def ui_pulse_info_collection_progress(self):
        if self.progress:
            self.progress.set()
        # for a spinner we just need to handle events
        QApplication.processEvents()

    def ui_stop_info_collection_progress(self):
        if self.progress:
            self.progress.hide()
            self.progress = None
        else:
            self.dialog.movie.stop()
            self.dialog.spinner.hide()

        QApplication.processEvents()

    def ui_start_upload_progress(self):
        self.progress = ProgressDialog(
            _('Uploading Problem Information'),
            _('Uploading problem information'),
            _('The collected information is being sent to the bug '
              'tracking system. This might take a few minutes.'))
        self.progress.show()

    def ui_set_upload_progress(self, progress):
        if progress:
            self.progress.set(progress)
        else:
            self.progress.set()
        QApplication.processEvents()

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

    def ui_question_yesno(self, text):
        response = QMessageBox.question(None, '', 'text',
                                        QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel)
        if response == QMessageBox.Yes:
            return True
        if response == QMessageBox.No:
            return False
        return None

    def ui_question_choice(self, text, options, multiple):
        ''' Show a question with predefined choices.

        @options is a list of strings to present.
        @multiple - if True, choices should be QCheckBoxes, if False then
        should be QRadioButtons.

        Return list of selected option indexes, or None if the user cancelled.
        If multiple is False, the list will always have one element.
        '''
        dialog = ChoicesDialog(_('Apport'), text)

        b = None
        for option in options:
            if multiple:
                b = QCheckBox(option)
            else:
                b = QRadioButton(option)
            dialog.vbox_choices.insertWidget(0, b)

        response = dialog.exec_()

        if response == QDialog.Rejected:
            return 'cancel'

        response = [c for c in range(0, dialog.vbox_choices.count())
                    if dialog.vbox_choices.itemAt(c).widget().isChecked()]

        return response

    def ui_question_file(self, text):
        ''' Show a file selector dialog.

        Return path if the user selected a file, or None if cancelled.
        '''
        response = QFileDialog.getOpenFileName(None, unicode(text, 'UTF-8'))
        if response.length() == 0:
            return None
        return str(response)

    def ui_question_userpass(self, text):
        '''Show a Username/Password dialog.

        Return a tuple (user, pass) or None if cancelled.
        '''
        dialog = UserPassDialog(_('Apport'), text)
        response = dialog.exec_()

        if response == QDialog.Rejected:
            return None

        username = str(dialog.findChild(QLineEdit, 'e_username').text())
        password = str(dialog.findChild(QLineEdit, 'e_password').text())

        if len(username) == 0 or len(password) == 0:
            return None
        return (username, password)


if __name__ == '__main__':
    if not os.environ.get('DISPLAY'):
        apport.fatal('This program needs a running X session. Please see "man apport-cli" for a command line version of Apport.')

    app = QApplication(sys.argv)
    app.setApplicationName('apport-kde')
    app.setApplicationDisplayName(_('Apport'))
    app.setWindowIcon(QIcon.fromTheme('apport'))
    translator = QTranslator()
    translator.load("qtbase_" + QLocale.system().name(),
                    QLibraryInfo.location(QLibraryInfo.TranslationsPath))
    app.installTranslator(translator)

    UserInterface = MainUserInterface()
    sys.exit(UserInterface.run_argv())