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