| 1 | # -*- coding:utf-8 -*- |
|---|
| 2 | ## src/gtkexcepthook.py |
|---|
| 3 | ## |
|---|
| 4 | ## Copyright (C) 2005-2006 Nikos Kouremenos <kourem AT gmail.com> |
|---|
| 5 | ## Copyright (C) 2005-2008 Yann Leboulanger <asterix AT lagaule.org> |
|---|
| 6 | ## Copyright (C) 2008 Stephan Erb <steve-e AT h3c.de> |
|---|
| 7 | ## |
|---|
| 8 | ## This file is part of Gajim. |
|---|
| 9 | ## |
|---|
| 10 | ## Gajim is free software; you can redistribute it and/or modify |
|---|
| 11 | ## it under the terms of the GNU General Public License as published |
|---|
| 12 | ## by the Free Software Foundation; version 3 only. |
|---|
| 13 | ## |
|---|
| 14 | ## Gajim is distributed in the hope that it will be useful, |
|---|
| 15 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | ## GNU General Public License for more details. |
|---|
| 18 | ## |
|---|
| 19 | ## You should have received a copy of the GNU General Public License |
|---|
| 20 | ## along with Gajim. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 21 | ## |
|---|
| 22 | |
|---|
| 23 | import sys |
|---|
| 24 | import os |
|---|
| 25 | import traceback |
|---|
| 26 | import threading |
|---|
| 27 | |
|---|
| 28 | import gtk |
|---|
| 29 | import pango |
|---|
| 30 | from common import i18n |
|---|
| 31 | import dialogs |
|---|
| 32 | |
|---|
| 33 | from cStringIO import StringIO |
|---|
| 34 | from common import helpers |
|---|
| 35 | |
|---|
| 36 | _exception_in_progress = threading.Lock() |
|---|
| 37 | |
|---|
| 38 | def _info(type_, value, tb): |
|---|
| 39 | if not _exception_in_progress.acquire(False): |
|---|
| 40 | # Exceptions have piled up, so we use the default exception |
|---|
| 41 | # handler for such exceptions |
|---|
| 42 | _excepthook_save(type_, value, tb) |
|---|
| 43 | return |
|---|
| 44 | |
|---|
| 45 | dialog = dialogs.HigDialog(None, gtk.MESSAGE_WARNING, gtk.BUTTONS_NONE, |
|---|
| 46 | _('A programming error has been detected'), |
|---|
| 47 | _('It probably is not fatal, but should be reported ' |
|---|
| 48 | 'to the developers nonetheless.')) |
|---|
| 49 | |
|---|
| 50 | dialog.set_modal(False) |
|---|
| 51 | #FIXME: add icon to this button |
|---|
| 52 | RESPONSE_REPORT_BUG = 42 |
|---|
| 53 | dialog.add_buttons(gtk.STOCK_CLOSE, gtk.BUTTONS_CLOSE, |
|---|
| 54 | _('_Report Bug'), RESPONSE_REPORT_BUG) |
|---|
| 55 | dialog.set_default_response(RESPONSE_REPORT_BUG) |
|---|
| 56 | report_button = dialog.action_area.get_children()[0] # right to left |
|---|
| 57 | report_button.grab_focus() |
|---|
| 58 | |
|---|
| 59 | # Details |
|---|
| 60 | textview = gtk.TextView() |
|---|
| 61 | textview.set_editable(False) |
|---|
| 62 | textview.modify_font(pango.FontDescription('Monospace')) |
|---|
| 63 | sw = gtk.ScrolledWindow() |
|---|
| 64 | sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) |
|---|
| 65 | sw.add(textview) |
|---|
| 66 | frame = gtk.Frame() |
|---|
| 67 | frame.set_shadow_type(gtk.SHADOW_IN) |
|---|
| 68 | frame.add(sw) |
|---|
| 69 | frame.set_border_width(6) |
|---|
| 70 | textbuffer = textview.get_buffer() |
|---|
| 71 | trace = StringIO() |
|---|
| 72 | traceback.print_exception(type_, value, tb, None, trace) |
|---|
| 73 | textbuffer.set_text(trace.getvalue()) |
|---|
| 74 | textview.set_size_request( |
|---|
| 75 | gtk.gdk.screen_width() / 3, |
|---|
| 76 | gtk.gdk.screen_height() / 4) |
|---|
| 77 | expander = gtk.Expander(_('Details')) |
|---|
| 78 | expander.add(frame) |
|---|
| 79 | dialog.vbox.add(expander) |
|---|
| 80 | |
|---|
| 81 | dialog.set_resizable(True) |
|---|
| 82 | # on expand the details the dialog remains centered on screen |
|---|
| 83 | dialog.set_position(gtk.WIN_POS_CENTER_ALWAYS) |
|---|
| 84 | |
|---|
| 85 | close_clicked = False |
|---|
| 86 | def on_dialog_response(dialog, response): |
|---|
| 87 | if response == RESPONSE_REPORT_BUG: |
|---|
| 88 | url = 'http://trac.gajim.org/wiki/HowToCreateATicket' |
|---|
| 89 | helpers.launch_browser_mailer('url', url) |
|---|
| 90 | else: |
|---|
| 91 | dialog.destroy() |
|---|
| 92 | dialog.connect('response', on_dialog_response) |
|---|
| 93 | dialog.show_all() |
|---|
| 94 | |
|---|
| 95 | _exception_in_progress.release() |
|---|
| 96 | |
|---|
| 97 | # gdb/kdm etc if we use startx this is not True |
|---|
| 98 | if os.name == 'nt' or not sys.stderr.isatty(): |
|---|
| 99 | #FIXME: maybe always show dialog? |
|---|
| 100 | _excepthook_save = sys.excepthook |
|---|
| 101 | sys.excepthook = _info |
|---|
| 102 | |
|---|
| 103 | # this is just to assist testing (python gtkexcepthook.py) |
|---|
| 104 | if __name__ == '__main__': |
|---|
| 105 | _excepthook_save = sys.excepthook |
|---|
| 106 | sys.excepthook = _info |
|---|
| 107 | print x # this always tracebacks |
|---|
| 108 | |
|---|
| 109 | # vim: se ts=3: |
|---|