apport/gtk/apport-gtk

651 lines
24 KiB
Plaintext
Raw Normal View History

2022-05-13 19:55:01 +08:00
#!/usr/bin/python3
2024-04-30 16:29:43 +08:00
"""GTK Apport user interface."""
2022-05-13 19:55:01 +08:00
# Copyright (C) 2007-2016 Canonical Ltd.
# Author: Martin Pitt <martin.pitt@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.
2024-04-30 16:29:43 +08:00
# pylint: disable=invalid-name
# pylint: enable=invalid-name
2022-05-13 19:55:01 +08:00
2024-04-30 16:29:43 +08:00
import os
import re
import subprocess
import sys
from gettext import gettext as _
2022-05-13 19:55:01 +08:00
import apport
import apport.ui
2024-04-30 16:29:43 +08:00
try:
import gi
gi.require_version("Gdk", "3.0") # noqa: E402, pylint: disable=C0413
gi.require_version("GdkX11", "3.0") # noqa: E402, pylint: disable=C0413
gi.require_version("Gtk", "3.0") # noqa: E402, pylint: disable=C0413
gi.require_version("Wnck", "3.0") # noqa: E402, pylint: disable=C0413
from gi.repository import GdkX11, GLib, Gtk, Wnck
except (AssertionError, ImportError, RuntimeError) as error:
# probably distribution upgrade or session just closing down?
sys.stderr.write(f"Cannot start: {str(error)}\n")
sys.exit(1)
2022-05-13 19:55:01 +08:00
2024-04-30 16:29:43 +08:00
have_display = bool(os.environ.get("DISPLAY") or os.environ.get("WAYLAND_DISPLAY"))
2022-05-13 19:55:01 +08:00
2024-04-30 16:29:43 +08:00
def find_xid_for_pid(pid: int) -> int | None:
"""Return the X11 Window (xid) for the supplied process ID."""
2022-05-13 19:55:01 +08:00
screen = Wnck.Screen.get_default()
screen.force_update()
for window in screen.get_windows():
if window.get_pid() == pid:
return window.get_xid()
return None
class GTKUserInterface(apport.ui.UserInterface):
2024-04-30 16:29:43 +08:00
# TODO: Address following pylint complaints
# pylint: disable=invalid-name,missing-function-docstring
"""GTK UserInterface."""
2022-05-13 19:55:01 +08:00
2024-04-30 16:29:43 +08:00
def w(self, widget: str) -> Gtk.Widget:
"""Shortcut for getting a widget."""
widget = self.widgets.get_object(widget)
assert widget is not None
return widget
2022-05-13 19:55:01 +08:00
2024-04-30 16:29:43 +08:00
def __init__(self, argv: list[str]):
apport.ui.UserInterface.__init__(self, argv)
2022-05-13 19:55:01 +08:00
# load UI
2024-04-30 16:29:43 +08:00
Gtk.Window.set_default_icon_name("apport")
2022-05-13 19:55:01 +08:00
self.widgets = Gtk.Builder()
self.widgets.set_translation_domain(self.gettext_domain)
2024-04-30 16:29:43 +08:00
self.widgets.add_from_file(
os.path.join(os.path.dirname(argv[0]), "apport-gtk.ui")
)
2022-05-13 19:55:01 +08:00
# connect signal handlers
assert self.widgets.connect_signals(self) is None
# initialize tree model and view
2024-04-30 16:29:43 +08:00
self.tree_model = self.w("details_treestore")
2022-05-13 19:55:01 +08:00
2024-04-30 16:29:43 +08:00
column = Gtk.TreeViewColumn("Report", Gtk.CellRendererText(), text=0)
self.w("details_treeview").append_column(column)
self.spinner = self.add_spinner_over_treeview(self.w("details_overlay"))
2022-05-13 19:55:01 +08:00
2024-04-30 16:29:43 +08:00
self.argv = argv
2022-05-13 19:55:01 +08:00
self.md = None
self.desktop_info = None
self.allowed_to_report = True
2024-04-30 16:29:43 +08:00
self.collect_called = False
2022-05-13 19:55:01 +08:00
#
# ui_* implementation of abstract UserInterface classes
#
2024-04-30 16:29:43 +08:00
@staticmethod
def add_spinner_over_treeview(overlay):
"""Reparents a treeview in a GtkOverlay, then layers a GtkSpinner
centered on top."""
2022-05-13 19:55:01 +08:00
# TODO handle the expose event of the spinner so that we can draw on
# the treeview's viewport's window instead.
spinner = Gtk.Spinner()
spinner.set_size_request(42, 42)
align = Gtk.Alignment()
align.set_valign(Gtk.Align.CENTER)
align.set_halign(Gtk.Align.CENTER)
align.add(spinner)
overlay.add_overlay(align)
overlay.show()
align.show()
spinner.hide()
return spinner
2024-04-30 16:29:43 +08:00
def ui_update_view(self, shown_keys: list[str] | None = None) -> None:
assert self.report
2022-05-13 19:55:01 +08:00
# do nothing if the dialog is already destroyed when the data
# collection finishes
2024-04-30 16:29:43 +08:00
if not self.w("details_treeview").get_property("visible"):
2022-05-13 19:55:01 +08:00
return
self.tree_model.clear()
2024-04-30 16:29:43 +08:00
for key, value in self.report.sorted_items(shown_keys):
2022-05-13 19:55:01 +08:00
keyiter = self.tree_model.insert_before(None, None)
self.tree_model.set_value(keyiter, 0, key)
valiter = self.tree_model.insert_before(keyiter, None)
2024-04-30 16:29:43 +08:00
if isinstance(value, str):
v = value
2022-05-13 19:55:01 +08:00
if len(v) > 4000:
2024-04-30 16:29:43 +08:00
v = v[:4000] + "\n[...]"
2022-05-13 19:55:01 +08:00
self.tree_model.set_value(valiter, 0, v)
# expand the row if the value has less than 5 lines
2024-04-30 16:29:43 +08:00
if len(list(filter(lambda c: c == "\n", value))) < 4:
self.w("details_treeview").expand_row(
self.tree_model.get_path(keyiter), False
)
2022-05-13 19:55:01 +08:00
else:
2024-04-30 16:29:43 +08:00
self.tree_model.set_value(valiter, 0, _("(binary data)"))
2022-05-13 19:55:01 +08:00
def get_system_application_title(self):
2024-04-30 16:29:43 +08:00
"""Get dialog title for a non-.desktop application.
2022-05-13 19:55:01 +08:00
If the system application was started from the console, assume a
developer who would appreciate the application name having a more
prominent placement. Otherwise, provide a simple explanation for
more novice users.
2024-04-30 16:29:43 +08:00
"""
assert self.report
env = self.report.get("ProcEnviron", "")
from_console = "TERM=" in env and "SHELL=" in env
2022-05-13 19:55:01 +08:00
if from_console:
2024-04-30 16:29:43 +08:00
if "ExecutablePath" in self.report:
t = _(
"Sorry, the application %s has stopped unexpectedly."
) % os.path.basename(self.report["ExecutablePath"])
2022-05-13 19:55:01 +08:00
else:
2024-04-30 16:29:43 +08:00
t = _("Sorry, %s has closed unexpectedly.") % self.cur_package
2022-05-13 19:55:01 +08:00
else:
2024-04-30 16:29:43 +08:00
if "DistroRelease" not in self.report:
2022-05-13 19:55:01 +08:00
self.report.add_os_info()
2024-04-30 16:29:43 +08:00
t = (
_("Sorry, %s has experienced an internal error.")
% self.report["DistroRelease"]
)
2022-05-13 19:55:01 +08:00
return t
def setup_bug_report(self):
# This is a bug generated through `apport-bug $package`, or
# `apport-collect $id`.
2024-04-30 16:29:43 +08:00
assert self.report
2022-05-13 19:55:01 +08:00
# avoid collecting information again, in this mode we already have it
2024-04-30 16:29:43 +08:00
if "DistroRelease" in self.report:
2022-05-13 19:55:01 +08:00
self.collect_called = True
self.ui_update_view()
2024-04-30 16:29:43 +08:00
self.w("title_label").set_label(
f"<big><b>{_('Send problem report to the developers?')}</b></big>"
)
self.w("title_label").show()
self.w("subtitle_label").hide()
self.w("ignore_future_problems").hide()
self.w("show_details").clicked()
self.w("show_details").hide()
self.w("dont_send_button").show()
self.w("continue_button").set_label(_("Send"))
2022-05-13 19:55:01 +08:00
def set_modal_for(self, xid):
2024-04-30 16:29:43 +08:00
gdk_window = self.w("dialog_crash_new")
2022-05-13 19:55:01 +08:00
gdk_window.realize()
gdk_window = gdk_window.get_window()
gdk_display = GdkX11.X11Display.get_default()
foreign = GdkX11.X11Window.foreign_new_for_display(gdk_display, xid)
gdk_window.set_transient_for(foreign)
gdk_window.set_modal_hint(True)
2024-04-30 16:29:43 +08:00
def ui_present_report_details(
self, allowed_to_report: bool = True, modal_for: int | None = None
) -> apport.ui.Action:
# TODO: Split into smaller functions/methods
# pylint: disable=too-many-branches,too-many-locals,too-many-statements
assert self.report
2022-05-13 19:55:01 +08:00
icon = None
self.collect_called = False
2024-04-30 16:29:43 +08:00
report_type = self.report.get("ProblemType")
self.w("details_scrolledwindow").hide()
self.w("show_details").set_label(_("Show Details"))
2022-05-13 19:55:01 +08:00
self.tree_model.clear()
self.allowed_to_report = allowed_to_report
if self.allowed_to_report:
2024-04-30 16:29:43 +08:00
self.w("remember_send_report_choice").show()
self.w("send_problem_notice_label").set_label(
f"<b>{self.w('send_problem_notice_label').get_label()}</b>"
)
self.w("send_problem_notice_label").show()
self.w("dont_send_button").grab_focus()
2022-05-13 19:55:01 +08:00
else:
2024-04-30 16:29:43 +08:00
self.w("dont_send_button").hide()
self.w("continue_button").set_label(_("Continue"))
self.w("continue_button").grab_focus()
2022-05-13 19:55:01 +08:00
2024-04-30 16:29:43 +08:00
self.w("examine").set_visible(self.can_examine_locally())
2022-05-13 19:55:01 +08:00
2024-04-30 16:29:43 +08:00
if modal_for is not None and "DISPLAY" in os.environ:
2022-05-13 19:55:01 +08:00
xid = find_xid_for_pid(modal_for)
if xid:
self.set_modal_for(xid)
2024-04-30 16:29:43 +08:00
if report_type == "Hang" and self.offer_restart:
self.w("ignore_future_problems").set_active(False)
self.w("ignore_future_problems").hide()
self.w("relaunch_app").set_active(True)
self.w("relaunch_app").show()
self.w("subtitle_label").show()
self.w("subtitle_label").set_label(
"You can wait to see if it wakes up, or close or relaunch it."
)
2022-05-13 19:55:01 +08:00
self.desktop_info = self.get_desktop_entry()
if self.desktop_info:
2024-04-30 16:29:43 +08:00
icon = self.desktop_info.get("icon")
name = self.desktop_info["name"]
2022-05-13 19:55:01 +08:00
name = GLib.markup_escape_text(name)
2024-04-30 16:29:43 +08:00
title = _("The application %s has stopped responding.") % name
2022-05-13 19:55:01 +08:00
else:
2024-04-30 16:29:43 +08:00
icon = "distributor-logo"
name = os.path.basename(self.report["ExecutablePath"])
2022-05-13 19:55:01 +08:00
title = _('The program "%s" has stopped responding.') % name
2024-04-30 16:29:43 +08:00
self.w("title_label").set_label(f"<big><b>{title}</b></big>")
elif not self.report_file or report_type == "Bug":
self.w("remember_send_report_choice").hide()
self.w("send_problem_notice_label").hide()
2022-05-13 19:55:01 +08:00
self.setup_bug_report()
2024-04-30 16:29:43 +08:00
elif report_type in {"KernelCrash", "KernelOops"}:
self.w("ignore_future_problems").set_active(False)
self.w("ignore_future_problems").hide()
self.w("title_label").set_label(
f"<big><b>{self.get_system_application_title()}</b></big>"
)
self.w("subtitle_label").hide()
icon = "distributor-logo"
elif report_type == "Package":
package = self.report.get("Package")
2022-05-13 19:55:01 +08:00
if package:
2024-04-30 16:29:43 +08:00
self.w("subtitle_label").set_label(_("Package: %s") % package)
self.w("subtitle_label").show()
2022-05-13 19:55:01 +08:00
else:
2024-04-30 16:29:43 +08:00
self.w("subtitle_label").hide()
self.w("ignore_future_problems").hide()
self.w("title_label").set_label(
_("Sorry, a problem occurred while installing software.")
)
2022-05-13 19:55:01 +08:00
else:
# Regular crash.
self.desktop_info = self.get_desktop_entry()
if self.desktop_info:
2024-04-30 16:29:43 +08:00
icon = self.desktop_info.get("icon")
n = self.desktop_info["name"]
2022-05-13 19:55:01 +08:00
n = GLib.markup_escape_text(n)
2024-04-30 16:29:43 +08:00
if report_type == "RecoverableProblem":
t = _("The application %s has experienced an internal error.") % n
2022-05-13 19:55:01 +08:00
else:
2024-04-30 16:29:43 +08:00
t = _("The application %s has closed unexpectedly.") % n
self.w("title_label").set_label(f"<big><b>{t}</b></big>")
self.w("subtitle_label").hide()
2022-05-13 19:55:01 +08:00
pid = apport.ui.get_pid(self.report)
still_running = pid and apport.ui.still_running(pid)
2024-04-30 16:29:43 +08:00
if (
"ProcCmdline" in self.report
and not still_running
and self.offer_restart
):
self.w("relaunch_app").set_active(True)
self.w("relaunch_app").show()
2022-05-13 19:55:01 +08:00
else:
2024-04-30 16:29:43 +08:00
icon = "distributor-logo"
if report_type == "RecoverableProblem":
title_text = (
_("The application %s has experienced an internal error.")
% self.cur_package
)
2022-05-13 19:55:01 +08:00
else:
title_text = self.get_system_application_title()
2024-04-30 16:29:43 +08:00
self.w("title_label").set_label(f"<big><b>{title_text}</b></big>")
self.w("subtitle_label").show()
self.w("subtitle_label").set_label(
_("If you notice further problems, try restarting the computer.")
)
self.w("ignore_future_problems").set_label(
_("Ignore future problems of this type")
)
if self.report.get("CrashCounter"):
self.w("ignore_future_problems").show()
2022-05-13 19:55:01 +08:00
else:
2024-04-30 16:29:43 +08:00
self.w("ignore_future_problems").hide()
2022-05-13 19:55:01 +08:00
2024-04-30 16:29:43 +08:00
if report_type == "RecoverableProblem":
body = self.report.get("DialogBody", "")
2022-05-13 19:55:01 +08:00
if body:
2024-04-30 16:29:43 +08:00
del self.report["DialogBody"]
self.w("subtitle_label").show()
2022-05-13 19:55:01 +08:00
# Set a maximum size for the dialog body, so developers do
# not try to shove entire log files into this dialog.
2024-04-30 16:29:43 +08:00
self.w("subtitle_label").set_label(body[:1024])
2022-05-13 19:55:01 +08:00
if icon:
2024-04-30 16:29:43 +08:00
# pylint: disable=import-outside-toplevel
2022-05-13 19:55:01 +08:00
from gi.repository import GdkPixbuf
2024-04-30 16:29:43 +08:00
2022-05-13 19:55:01 +08:00
builtin = Gtk.IconLookupFlags.USE_BUILTIN
2024-04-30 16:29:43 +08:00
app_icon = self.w("application_icon")
2022-05-13 19:55:01 +08:00
theme = Gtk.IconTheme.get_default()
try:
pb = theme.load_icon(icon, 42, builtin).copy()
2024-04-30 16:29:43 +08:00
overlay = theme.load_icon("dialog-error", 16, builtin)
2022-05-13 19:55:01 +08:00
overlay_w = overlay.get_width()
overlay_h = overlay.get_height()
off_x = pb.get_width() - overlay_w
off_y = pb.get_height() - overlay_h
2024-04-30 16:29:43 +08:00
overlay.composite(
pb,
off_x,
off_y,
overlay_w,
overlay_h,
off_x,
off_y,
1,
1,
GdkPixbuf.InterpType.BILINEAR,
255,
)
2022-05-13 19:55:01 +08:00
if app_icon.get_parent(): # work around LP#938090
app_icon.set_from_pixbuf(pb)
except GLib.GError:
2024-04-30 16:29:43 +08:00
self.w("application_icon").set_from_icon_name(
"dialog-error", Gtk.IconSize.DIALOG
)
2022-05-13 19:55:01 +08:00
else:
2024-04-30 16:29:43 +08:00
self.w("application_icon").set_from_icon_name(
"dialog-error", Gtk.IconSize.DIALOG
)
d = self.w("dialog_crash_new")
if "DistroRelease" in self.report:
d.set_title(self.report["DistroRelease"].split()[0])
d.set_resizable(self.w("details_scrolledwindow").get_property("visible"))
2022-05-13 19:55:01 +08:00
d.show()
# don't steal focus when being called without arguments (i. e.
# automatically launched)
2024-04-30 16:29:43 +08:00
if len(self.argv) == 1:
2022-05-13 19:55:01 +08:00
d.set_focus_on_map(False)
2024-04-30 16:29:43 +08:00
return_value = apport.ui.Action()
2022-05-13 19:55:01 +08:00
def dialog_crash_dismissed(widget):
2024-04-30 16:29:43 +08:00
self.w("dialog_crash_new").hide()
if widget is self.w("dialog_crash_new"):
2022-05-13 19:55:01 +08:00
Gtk.main_quit()
return
2024-04-30 16:29:43 +08:00
if widget is self.w("examine"):
return_value.examine = True
2022-05-13 19:55:01 +08:00
Gtk.main_quit()
return
2024-04-30 16:29:43 +08:00
# Force close or leave close app are the default actions
# with no specifier in case of hangs or crash
if (
self.w("relaunch_app").get_active()
and self.desktop_info
and self.offer_restart
):
return_value.restart = True
2022-05-13 19:55:01 +08:00
2024-04-30 16:29:43 +08:00
if self.w("ignore_future_problems").get_active():
return_value.ignore = True
2022-05-13 19:55:01 +08:00
2024-04-30 16:29:43 +08:00
return_value.remember = self.w("remember_send_report_choice").get_active()
2022-05-13 19:55:01 +08:00
2024-04-30 16:29:43 +08:00
if widget == self.w("continue_button"):
return_value.report = self.allowed_to_report
2022-05-13 19:55:01 +08:00
Gtk.main_quit()
2024-04-30 16:29:43 +08:00
self.w("dialog_crash_new").connect("destroy", dialog_crash_dismissed)
self.w("continue_button").connect("clicked", dialog_crash_dismissed)
self.w("dont_send_button").connect("clicked", dialog_crash_dismissed)
self.w("examine").connect("clicked", dialog_crash_dismissed)
2022-05-13 19:55:01 +08:00
Gtk.main()
return return_value
def _ui_message_dialog(self, title, text, _type, buttons=Gtk.ButtonsType.CLOSE):
self.md = Gtk.MessageDialog(message_type=_type, buttons=buttons)
2024-04-30 16:29:43 +08:00
if "http://" in text or "https://" in text:
self.md.set_markup(text_to_markup(text))
2022-05-13 19:55:01 +08:00
else:
# work around gnome #620579
2024-04-30 16:29:43 +08:00
self.md.set_property("text", text)
2022-05-13 19:55:01 +08:00
self.md.set_title(title)
result = self.md.run()
self.md.hide()
while Gtk.events_pending():
Gtk.main_iteration_do(False)
self.md = None
return result
def ui_info_message(self, title, text):
self._ui_message_dialog(title, text, Gtk.MessageType.INFO)
def ui_error_message(self, title, text):
self._ui_message_dialog(title, text, Gtk.MessageType.ERROR)
def ui_shutdown(self):
Gtk.main_quit()
def ui_start_upload_progress(self):
2024-04-30 16:29:43 +08:00
"""Open a window with an definite progress bar, telling the user to
wait while debug information is being uploaded."""
2022-05-13 19:55:01 +08:00
2024-04-30 16:29:43 +08:00
self.w("progressbar_upload").set_fraction(0)
self.w("window_report_upload").show()
2022-05-13 19:55:01 +08:00
while Gtk.events_pending():
Gtk.main_iteration_do(False)
2024-04-30 16:29:43 +08:00
def ui_set_upload_progress(self, progress: float | None) -> None:
"""Set the progress bar in the debug data upload progress
2022-05-13 19:55:01 +08:00
window to the given ratio (between 0 and 1, or None for indefinite
progress).
2024-04-30 16:29:43 +08:00
This function is called every 100 ms."""
2022-05-13 19:55:01 +08:00
if progress:
2024-04-30 16:29:43 +08:00
self.w("progressbar_upload").set_fraction(progress)
2022-05-13 19:55:01 +08:00
else:
2024-04-30 16:29:43 +08:00
self.w("progressbar_upload").set_pulse_step(0.1)
self.w("progressbar_upload").pulse()
2022-05-13 19:55:01 +08:00
while Gtk.events_pending():
Gtk.main_iteration_do(False)
def ui_stop_upload_progress(self):
2024-04-30 16:29:43 +08:00
"""Close debug data upload progress window."""
2022-05-13 19:55:01 +08:00
2024-04-30 16:29:43 +08:00
self.w("window_report_upload").hide()
2022-05-13 19:55:01 +08:00
while Gtk.events_pending():
Gtk.main_iteration_do(False)
def ui_start_info_collection_progress(self):
# show a spinner if we already have the main window
2024-04-30 16:29:43 +08:00
if self.w("dialog_crash_new").get_property("visible"):
2022-05-13 19:55:01 +08:00
self.spinner.show()
self.spinner.start()
elif self.crashdb.accepts(self.report):
# show a progress dialog if our DB accepts the crash
2024-04-30 16:29:43 +08:00
self.w("progressbar_information_collection").set_fraction(0)
self.w("window_information_collection").show()
2022-05-13 19:55:01 +08:00
while Gtk.events_pending():
Gtk.main_iteration_do(False)
def ui_pulse_info_collection_progress(self):
2024-04-30 16:29:43 +08:00
if self.w("window_information_collection").get_property("visible"):
self.w("progressbar_information_collection").pulse()
2022-05-13 19:55:01 +08:00
# for a spinner we just need to handle events
while Gtk.events_pending():
Gtk.main_iteration_do(False)
def ui_stop_info_collection_progress(self):
2024-04-30 16:29:43 +08:00
if self.w("window_information_collection").get_property("visible"):
self.w("window_information_collection").hide()
2022-05-13 19:55:01 +08:00
else:
self.spinner.hide()
self.spinner.stop()
while Gtk.events_pending():
Gtk.main_iteration_do(False)
def ui_question_yesno(self, text):
2024-04-30 16:29:43 +08:00
"""Show a yes/no question.
2022-05-13 19:55:01 +08:00
Return True if the user selected "Yes", False if selected "No" or
"None" on cancel/dialog closing.
2024-04-30 16:29:43 +08:00
"""
result = self._ui_message_dialog(
"", text, Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO
)
2022-05-13 19:55:01 +08:00
if result == Gtk.ResponseType.YES:
return True
if result == Gtk.ResponseType.NO:
return False
return None
def ui_question_choice(self, text, options, multiple):
2024-04-30 16:29:43 +08:00
"""Show an question with predefined choices.
2022-05-13 19:55:01 +08:00
options is a list of strings to present. If multiple is True, they
should be check boxes, if multiple is False they should be radio
buttons.
Return list of selected option indexes, or None if the user cancelled.
If multiple == False, the list will always have one element.
2024-04-30 16:29:43 +08:00
"""
d = self.w("dialog_choice")
2022-05-13 19:55:01 +08:00
d.set_default_size(400, -1)
2024-04-30 16:29:43 +08:00
self.w("label_choice_text").set_label(text)
2022-05-13 19:55:01 +08:00
# remove previous choices
2024-04-30 16:29:43 +08:00
for child in self.w("vbox_choices").get_children():
2022-05-13 19:55:01 +08:00
child.destroy()
b = None
for option in options:
if multiple:
b = Gtk.CheckButton.new_with_label(option)
2024-04-30 16:29:43 +08:00
# use previous radio button as group; work around GNOME#635253
elif b:
b = Gtk.RadioButton.new_with_label_from_widget(b, option)
2022-05-13 19:55:01 +08:00
else:
2024-04-30 16:29:43 +08:00
b = Gtk.RadioButton.new_with_label([], option)
self.w("vbox_choices").pack_start(b, True, True, 0)
self.w("vbox_choices").show_all()
2022-05-13 19:55:01 +08:00
result = d.run()
d.hide()
if result != Gtk.ResponseType.OK:
return None
index = 0
result = []
2024-04-30 16:29:43 +08:00
for c in self.w("vbox_choices").get_children():
2022-05-13 19:55:01 +08:00
if c.get_active():
result.append(index)
index += 1
return result
def ui_question_file(self, text):
2024-04-30 16:29:43 +08:00
"""Show a file selector dialog.
2022-05-13 19:55:01 +08:00
Return path if the user selected a file, or None if cancelled.
2024-04-30 16:29:43 +08:00
"""
2022-05-13 19:55:01 +08:00
md = Gtk.FileChooserDialog(
2024-04-30 16:29:43 +08:00
text,
parent=self.w("window_information_collection"),
buttons=(
Gtk.STOCK_CANCEL,
Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN,
Gtk.ResponseType.OK,
),
)
2022-05-13 19:55:01 +08:00
result = md.run()
md.hide()
while Gtk.events_pending():
Gtk.main_iteration_do(False)
if result == Gtk.ResponseType.OK:
return md.get_filenames()[0]
2024-04-30 16:29:43 +08:00
return None
2022-05-13 19:55:01 +08:00
2024-04-30 16:29:43 +08:00
@staticmethod
def _get_terminal():
terminals = [
"x-terminal-emulator",
"gnome-terminal",
"terminator",
"xfce4-terminal",
"xterm",
]
2022-05-13 19:55:01 +08:00
for t in terminals:
program = GLib.find_program_in_path(t)
if program:
2024-04-30 16:29:43 +08:00
return program
return None
2022-05-13 19:55:01 +08:00
2024-04-30 16:29:43 +08:00
def ui_has_terminal(self):
return have_display and self._get_terminal() is not None
2022-05-13 19:55:01 +08:00
2024-04-30 16:29:43 +08:00
def ui_run_terminal(self, command):
program = self._get_terminal()
assert program is not None
subprocess.call([program, "-e", command])
2022-05-13 19:55:01 +08:00
#
# Event handlers
#
def on_show_details_clicked(self, widget):
2024-04-30 16:29:43 +08:00
sw = self.w("details_scrolledwindow")
if sw.get_property("visible"):
self.w("dialog_crash_new").set_resizable(False)
2022-05-13 19:55:01 +08:00
sw.hide()
2024-04-30 16:29:43 +08:00
widget.set_label(_("Show Details"))
2022-05-13 19:55:01 +08:00
else:
2024-04-30 16:29:43 +08:00
self.w("dialog_crash_new").set_resizable(True)
2022-05-13 19:55:01 +08:00
sw.show()
2024-04-30 16:29:43 +08:00
widget.set_label(_("Hide Details"))
2022-05-13 19:55:01 +08:00
if not self.collect_called:
self.collect_called = True
2024-04-30 16:29:43 +08:00
self.ui_update_view(["ExecutablePath"])
GLib.idle_add(
lambda: self.collect_info(on_finished=self.ui_update_view)
)
2022-05-13 19:55:01 +08:00
return True
def on_progress_window_close_event(self, widget, event=None):
2024-04-30 16:29:43 +08:00
# pylint: disable=unused-argument
self.w("window_information_collection").hide()
self.w("window_report_upload").hide()
2022-05-13 19:55:01 +08:00
sys.exit(0)
2024-04-30 16:29:43 +08:00
def text_to_markup(text: str) -> str:
"""Turn URLs into links"""
escaped_text = GLib.markup_escape_text(text)
return re.sub(
"(https?://[a-zA-Z0-9._-]+[a-zA-Z0-9_#?%+=./-]*[a-zA-Z0-9_#?%+=/-])",
r'<a href="\1">\1</a>',
escaped_text,
)
if __name__ == "__main__":
2022-05-13 19:55:01 +08:00
if not have_display:
2024-04-30 16:29:43 +08:00
apport.fatal(
"This program needs a running X session. Please see"
' "man apport-cli" for a command line version of Apport.'
)
app = GTKUserInterface(sys.argv)
2022-05-13 19:55:01 +08:00
app.run_argv()