1# Copyright 2017 Christoph Reiter
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2 of the License, or
6# (at your option) any later version.
7
8from gi.repository import Gtk
9
10from quodlibet import _
11from quodlibet import app
12from quodlibet.qltk.entry import UndoEntry
13
14
15def find_active_window():
16    """Try to get the active Window, default to the main one"""
17
18    for window in Gtk.Window.list_toplevels():
19        if window.is_active():
20            return window
21    else:
22        return app.window
23
24
25class TextExpander(Gtk.Expander):
26
27    def __init__(self, title, text):
28        super(TextExpander, self).__init__(label=title)
29        self.set_resize_toplevel(True)
30
31        buf = Gtk.TextBuffer()
32        buf.set_text(text)
33        tv = Gtk.TextView(buffer=buf, editable=False)
34        tv.set_left_margin(6)
35        if hasattr(tv, "set_top_margin"):
36            tv.set_top_margin(6)
37            tv.set_bottom_margin(6)
38
39        label = self.get_label_widget()
40        label.props.margin = 4
41
42        win = Gtk.ScrolledWindow()
43        win.add(tv)
44        win.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
45        win.set_shadow_type(Gtk.ShadowType.ETCHED_OUT)
46        win.set_size_request(-1, 175)
47        self.add(win)
48        win.show_all()
49
50
51class ErrorDialog(Gtk.MessageDialog):
52
53    RESPONSE_QUIT = 1
54    RESPONSE_SUBMIT = 2
55    RESPONSE_BUGREPORT = 3
56
57    def __init__(self, parent, error_text):
58        main_text = _("An Error Occurred")
59        secondary_text = _(
60            "You can ignore this error, but the application might be unstable "
61            "until it is restarted. Submitting an error report will only "
62            "take a few seconds and would help us a lot.")
63
64        super(ErrorDialog, self).__init__(
65            text=main_text, secondary_text=secondary_text)
66
67        self.set_transient_for(parent)
68        self.set_modal(True)
69        self.add_button(_("Submit Error Report"), self.RESPONSE_SUBMIT)
70        self.add_button(_("Quit Program"), self.RESPONSE_QUIT)
71        self.add_button(_("Ignore Error"), Gtk.ResponseType.CANCEL)
72        self.set_default_response(Gtk.ResponseType.CANCEL)
73
74        area = self.get_message_area()
75        expand = TextExpander(_("Error details:"), error_text)
76        area.pack_start(expand, False, True, 0)
77        area.show_all()
78
79
80class SubmitErrorDialog(Gtk.MessageDialog):
81
82    RESPONSE_SUBMIT = 1
83
84    def __init__(self, parent, error_text):
85        main_text = _("Submit Error Report")
86        secondary_text = _(
87            "Various details regarding the error and your system will be send "
88            "to a third party online service "
89            "(<a href='https://www.sentry.io'>www.sentry.io</a>). You can "
90            "review the data before sending it below.")
91
92        secondary_text += u"\n\n"
93
94        secondary_text += _(
95            "(optional) Please provide a short description of what happened "
96            "when the error occurred:")
97
98        super(SubmitErrorDialog, self).__init__(
99            modal=True, text=main_text, secondary_text=secondary_text,
100            secondary_use_markup=True)
101
102        self.set_transient_for(parent)
103        self.add_button(_("_Cancel"), Gtk.ResponseType.CANCEL)
104        self.add_button(_("_Send"), self.RESPONSE_SUBMIT)
105        self.set_default_response(Gtk.ResponseType.CANCEL)
106
107        area = self.get_message_area()
108
109        self._entry = UndoEntry()
110        self._entry.set_placeholder_text(_("Short description…"))
111        area.pack_start(self._entry, False, True, 0)
112
113        expand = TextExpander(_("Data to be sent:"), error_text)
114        area.pack_start(expand, False, True, 0)
115        area.show_all()
116
117        self.get_widget_for_response(Gtk.ResponseType.CANCEL).grab_focus()
118
119    def get_comment(self):
120        """Returns the user provided error description
121
122        Returns
123            text_Type
124        """
125
126        return self._entry.get_text()
127