gtkexcepthook.py 3.21 KB
Newer Older
1
##	gtkexcepthook.py
2
##
Yann Leboulanger's avatar
Yann Leboulanger committed
3
## Copyright (C) 2005-2006 Yann Leboulanger <asterix@lagaule.org>
nkour's avatar
clean  
nkour committed
4
## Copyright (C) 2005-2006 Nikos Kouremenos <kourem@gmail.com>
5 6 7
##
## Initially written and submitted by Gustavo J. A. M. Carneiro
##
8 9 10
## This file is part of Gajim.
##
## 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 16 17 18
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
19 20 21
## You should have received a copy of the GNU General Public License
## along with Gajim.  If not, see <http://www.gnu.org/licenses/>.
##
22

23
import sys
24
import os
25
import traceback
nkour's avatar
nkour committed
26
import threading
27 28 29

import gtk
import pango
30
from common import i18n
31
import dialogs
32 33

from cStringIO import StringIO
34
from common import helpers
35

nkour's avatar
nkour committed
36
_exception_in_progress = threading.Lock()
37 38

def _info(type, value, tb):
Yann Leboulanger's avatar
Yann Leboulanger committed
39
	if not _exception_in_progress.acquire(False):
40 41 42 43 44
		# Exceptions have piled up, so we use the default exception
		# handler for such exceptions
		_excepthook_save(type, value, tb)
		return

45 46 47 48 49 50 51
	dialog = dialogs.HigDialog(None, gtk.MESSAGE_WARNING, gtk.BUTTONS_NONE, 
				_('A programming error has been detected'),
				_('It probably is not fatal, but should be reported '
				'to the developers nonetheless.'))
	
	#FIXME: add icon to this button
	RESPONSE_REPORT_BUG = 42
nkour's avatar
nkour committed
52 53
	dialog.add_buttons(gtk.STOCK_CLOSE, gtk.BUTTONS_CLOSE,
		_('_Report Bug'), RESPONSE_REPORT_BUG)
54
	dialog.set_default_response(RESPONSE_REPORT_BUG)
nkour's avatar
nkour committed
55 56
	report_button = dialog.action_area.get_children()[0] # right to left
	report_button.grab_focus()
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

	# Details
	textview = gtk.TextView()
	textview.set_editable(False)
	textview.modify_font(pango.FontDescription('Monospace'))
	sw = gtk.ScrolledWindow()
	sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
	sw.add(textview)
	frame = gtk.Frame()
	frame.set_shadow_type(gtk.SHADOW_IN)
	frame.add(sw)
	frame.set_border_width(6)
	textbuffer = textview.get_buffer()
	trace = StringIO()
	traceback.print_exception(type, value, tb, None, trace)
	textbuffer.set_text(trace.getvalue())
	textview.set_size_request(
		gtk.gdk.screen_width() / 3,
		gtk.gdk.screen_height() / 4)
	expander = gtk.Expander(_('Details'))
	expander.add(frame)
	dialog.vbox.add(expander)

80
	dialog.set_resizable(True)
nkour's avatar
nkour committed
81
	# on expand the details the dialog remains centered on screen
nkour's avatar
nkour committed
82
	dialog.set_position(gtk.WIN_POS_CENTER_ALWAYS)
83 84

	dialog.show_all()
85

86 87 88 89 90 91 92 93 94 95
	close_clicked = False
	while not close_clicked:
		resp = dialog.run()
		if resp == RESPONSE_REPORT_BUG:
			url = 'http://trac.gajim.org/wiki/WikiStart#howto_report_ticket'
			helpers.launch_browser_mailer('url', url)
		else:
			close_clicked = True
	
	dialog.destroy()
96

nkour's avatar
nkour committed
97
	_exception_in_progress.release()
98
	
99 100
# gdb/kdm etc if we use startx this is not True
if os.name == 'nt' or not sys.stderr.isatty():
101 102 103 104 105 106 107 108 109
	#FIXME: maybe always show dialog?
	_excepthook_save = sys.excepthook
	sys.excepthook = _info

# this is just to assist testing (python gtkexcepthook.py)
if __name__ == '__main__':
	_excepthook_save = sys.excepthook
	sys.excepthook = _info
	print x # this always tracebacks