2007-03-23 03:59:10 +03:00
# Error dialog with extensible "details" button.
#
# Copyright (C) 2007 Red Hat, Inc.
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
2007-11-20 19:12:20 +03:00
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.
2007-03-23 03:59:10 +03:00
import gtk
2008-09-06 21:24:47 +04:00
import logging
2011-04-06 19:22:03 +04:00
import traceback
2008-09-06 21:24:47 +04:00
2010-01-19 22:09:43 +03:00
import virtManager . util as util
2010-01-14 18:09:52 +03:00
def safe_set_text ( self , text ) :
2010-11-30 22:33:21 +03:00
# pygtk < 2.10 doesn't support text property
2010-01-19 22:09:43 +03:00
if not util . safe_set_prop ( self , " text " , text ) :
2010-01-14 18:09:52 +03:00
self . set_markup ( text )
2010-12-02 01:48:07 +03:00
def _launch_dialog ( dialog , primary_text , secondary_text , title ,
sync = True ) :
safe_set_text ( dialog , primary_text )
dialog . format_secondary_text ( secondary_text or None )
dialog . set_title ( title )
res = False
if sync :
res = dialog . run ( )
res = bool ( res in [ gtk . RESPONSE_YES , gtk . RESPONSE_OK ] )
dialog . destroy ( )
else :
def response_destroy ( src , ignore ) :
src . destroy ( )
dialog . connect ( " response " , response_destroy )
dialog . show ( )
2010-01-19 22:09:43 +03:00
2010-12-02 01:48:07 +03:00
return res
2009-01-22 00:28:54 +03:00
2010-12-02 01:48:07 +03:00
class vmmErrorDialog ( object ) :
2010-12-10 19:47:07 +03:00
def __init__ ( self , parent = None ) :
2010-12-02 01:48:07 +03:00
self . _parent = parent
2010-12-07 23:27:10 +03:00
self . _simple = None
2008-03-14 20:18:44 +03:00
2009-11-20 21:11:33 +03:00
def set_parent ( self , parent ) :
2010-12-02 01:48:07 +03:00
self . _parent = parent
def get_parent ( self ) :
return self . _parent
2011-04-06 19:22:03 +04:00
def show_err ( self , summary , details = None , title = " " ,
2010-12-02 01:48:07 +03:00
async = True , debug = True ,
dialog_type = gtk . MESSAGE_ERROR ,
buttons = gtk . BUTTONS_CLOSE ,
text2 = None ) :
2010-11-12 00:34:01 +03:00
if debug :
2010-12-08 21:05:38 +03:00
logging . debug ( " dialog message: %s : %s " % ( summary , details ) )
2009-01-22 00:28:54 +03:00
2011-04-06 19:22:03 +04:00
if details is None :
details = " " . join ( traceback . format_exc ( ) )
2010-12-02 01:48:07 +03:00
dialog = _errorDialog ( parent = self . get_parent ( ) ,
type = dialog_type , buttons = buttons )
return dialog . show_dialog ( primary_text = summary ,
secondary_text = text2 ,
details = details , title = title ,
sync = not async )
2008-03-14 20:18:44 +03:00
2010-11-30 23:37:54 +03:00
###################################
# Simple one shot message dialogs #
###################################
def _simple_dialog ( self , dialog_type , buttons , text1 ,
2010-12-02 01:48:07 +03:00
text2 , title , async = False ) :
2010-11-30 23:37:54 +03:00
2010-12-02 01:48:07 +03:00
dialog = gtk . MessageDialog ( self . get_parent ( ) ,
gtk . DIALOG_DESTROY_WITH_PARENT ,
type = dialog_type , buttons = buttons )
2010-12-07 23:27:10 +03:00
if self . _simple :
self . _simple . destroy ( )
self . _simple = dialog
2010-11-30 23:37:54 +03:00
2010-12-07 23:27:10 +03:00
return _launch_dialog ( self . _simple ,
2010-12-02 01:48:07 +03:00
text1 , text2 or " " , title or " " ,
sync = not async )
2008-03-14 20:18:44 +03:00
2010-11-30 23:37:54 +03:00
def val_err ( self , text1 , text2 = None , title = _ ( " Input Error " ) , async = True ) :
2009-07-02 20:45:48 +04:00
logging . debug ( " Validation Error: %s " % text1 )
2010-11-30 23:37:54 +03:00
dtype = gtk . MESSAGE_ERROR
buttons = gtk . BUTTONS_OK
self . _simple_dialog ( dtype , buttons , text1 , text2 , title , async )
return False
2009-07-02 20:45:48 +04:00
2010-11-30 23:37:54 +03:00
def show_info ( self , text1 , text2 = None , title = " " , async = True ) :
dtype = gtk . MESSAGE_INFO
buttons = gtk . BUTTONS_OK
self . _simple_dialog ( dtype , buttons , text1 , text2 , title , async )
return False
2009-02-16 05:20:14 +03:00
2010-11-30 23:37:54 +03:00
def yes_no ( self , text1 , text2 = None , title = None ) :
dtype = gtk . MESSAGE_WARNING
buttons = gtk . BUTTONS_YES_NO
return self . _simple_dialog ( dtype , buttons , text1 , text2 , title )
2008-03-14 20:18:44 +03:00
2010-11-30 23:37:54 +03:00
def ok_cancel ( self , text1 , text2 = None , title = None ) :
dtype = gtk . MESSAGE_WARNING
buttons = gtk . BUTTONS_OK_CANCEL
return self . _simple_dialog ( dtype , buttons , text1 , text2 , title )
2009-02-16 05:20:14 +03:00
2010-11-30 23:37:54 +03:00
def ok ( self , text1 , text2 = None , title = None ) :
dtype = gtk . MESSAGE_WARNING
buttons = gtk . BUTTONS_OK
return self . _simple_dialog ( dtype , buttons , text1 , text2 , title )
2009-02-16 05:20:14 +03:00
2010-11-30 23:37:54 +03:00
##########################################
# One shot dialog with a checkbox prompt #
##########################################
2010-03-04 00:58:50 +03:00
2009-11-19 00:11:17 +03:00
def warn_chkbox ( self , text1 , text2 = None , chktext = None , buttons = None ) :
2010-11-30 23:37:54 +03:00
dtype = gtk . MESSAGE_WARNING
buttons = buttons or gtk . BUTTONS_OK_CANCEL
2010-12-02 01:48:07 +03:00
chkbox = _errorDialog ( parent = self . get_parent ( ) ,
type = dtype ,
buttons = buttons )
return chkbox . show_dialog ( primary_text = text1 ,
secondary_text = text2 ,
chktext = chktext )
2009-09-24 00:42:56 +04:00
2009-11-19 00:11:17 +03:00
def err_chkbox ( self , text1 , text2 = None , chktext = None , buttons = None ) :
2010-11-30 23:37:54 +03:00
dtype = gtk . MESSAGE_ERROR
buttons = buttons or gtk . BUTTONS_OK
2010-12-02 01:48:07 +03:00
chkbox = _errorDialog ( parent = self . get_parent ( ) ,
type = dtype ,
buttons = buttons )
return chkbox . show_dialog ( primary_text = text1 ,
secondary_text = text2 ,
chktext = chktext )
class _errorDialog ( gtk . MessageDialog ) :
"""
Custom error dialog with optional check boxes or details drop down
"""
2010-12-10 19:47:07 +03:00
def __init__ ( self , * args , * * kwargs ) :
gtk . MessageDialog . __init__ ( self , * args , * * kwargs )
2010-12-02 01:48:07 +03:00
self . set_title ( " " )
2009-09-24 00:42:56 +04:00
2010-12-02 01:48:07 +03:00
self . chk_vbox = None
self . chk_align = None
self . init_chkbox ( )
2009-09-24 00:42:56 +04:00
2010-12-02 01:48:07 +03:00
self . buffer = None
self . buf_expander = None
self . init_details ( )
2009-09-24 00:42:56 +04:00
2010-12-02 01:48:07 +03:00
def init_chkbox ( self ) :
# Init check items
2009-09-24 00:42:56 +04:00
self . chk_vbox = gtk . VBox ( False , False )
self . chk_vbox . set_spacing ( 0 )
self . chk_align = gtk . Alignment ( )
self . chk_align . set_padding ( 0 , 0 , 62 , 0 )
self . chk_align . add ( self . chk_vbox )
self . chk_align . show_all ( )
self . vbox . pack_start ( self . chk_align )
2010-12-02 01:48:07 +03:00
def init_details ( self ) :
# Init details buffer
self . buffer = gtk . TextBuffer ( )
2010-12-10 19:47:07 +03:00
self . buf_expander = gtk . Expander ( _ ( " Details " ) )
sw = gtk . ScrolledWindow ( )
sw . set_shadow_type ( gtk . SHADOW_IN )
sw . set_size_request ( 400 , 240 )
sw . set_policy ( gtk . POLICY_AUTOMATIC , gtk . POLICY_AUTOMATIC )
details = gtk . TextView ( self . buffer )
details . set_editable ( False )
details . set_overwrite ( False )
details . set_cursor_visible ( False )
details . set_wrap_mode ( gtk . WRAP_WORD )
2010-12-02 01:48:07 +03:00
sw . add ( details )
self . buf_expander . add ( sw )
self . vbox . pack_start ( self . buf_expander )
self . buf_expander . show_all ( )
def show_dialog ( self , primary_text , secondary_text = " " ,
title = " " , details = " " , chktext = " " ,
sync = True ) :
2009-09-24 00:42:56 +04:00
chkbox = None
res = None
2010-12-02 01:48:07 +03:00
# Hide starting widgets
2009-09-24 00:42:56 +04:00
self . hide ( )
2010-12-02 01:48:07 +03:00
self . buf_expander . hide ( )
2009-09-24 00:42:56 +04:00
for c in self . chk_vbox . get_children ( ) :
self . chk_vbox . remove ( c )
2010-12-02 01:48:07 +03:00
if details :
self . buffer . set_text ( details )
2010-12-08 21:05:38 +03:00
title = title or " "
2010-12-02 01:48:07 +03:00
self . buf_expander . show ( )
2009-09-24 00:42:56 +04:00
if chktext :
chkbox = gtk . CheckButton ( chktext )
self . chk_vbox . add ( chkbox )
chkbox . show ( )
2010-12-02 01:48:07 +03:00
res = _launch_dialog ( self ,
primary_text , secondary_text or " " ,
title ,
sync = sync )
2009-09-24 00:42:56 +04:00
if chktext :
res = [ res , chkbox . get_active ( ) ]
return res