Commit 0aa8856b authored by Yann Leboulanger's avatar Yann Leboulanger

new TimeoutDialog class: to be derivated for dialogs that need a timeout.

Resource conflict dialog has now a timeout. Fixes #4590
parent 7e137adf
......@@ -522,15 +522,50 @@ class ChangeMoodDialog:
def on_cancel_button_clicked(self, widget):
self.window.destroy()
class ChangeStatusMessageDialog:
class TimeoutDialog:
'''
Class designed to be derivated to create timeout'd dialogs (dialogs that
closes automatically after a timeout)
'''
def __init__(self, timeout, on_timeout):
self.countdown_left = timeout
self.countdown_enabled = True
self.title_text = ''
self.on_timeout = on_timeout
def run_timeout(self):
if self.countdown_left > 0:
self.countdown()
gobject.timeout_add_seconds(1, self.countdown)
def on_timeout():
'''To be implemented in derivated classes'''
pass
def countdown(self):
if self.countdown_enabled:
if self.countdown_left <= 0:
self.on_timeout()
return False
self.dialog.set_title('%s [%s]' % (self.title_text,
str(self.countdown_left)))
self.countdown_left -= 1
return True
else:
self.dialog.set_title(self.title_text)
return False
class ChangeStatusMessageDialog(TimeoutDialog):
def __init__(self, on_response, show=None, show_pep=True):
countdown_time = gajim.config.get('change_status_window_timeout')
TimeoutDialog.__init__(self, countdown_time, self.on_timeout)
self.show = show
self.pep_dict = {}
self.show_pep = show_pep
self.on_response = on_response
self.xml = gtkgui_helpers.get_glade('change_status_message_dialog.glade')
self.window = self.xml.get_widget('change_status_message_dialog')
self.window.set_transient_for(gajim.interface.roster.window)
self.dialog = self.xml.get_widget('change_status_message_dialog')
self.dialog.set_transient_for(gajim.interface.roster.window)
msg = None
if show:
uf_show = helpers.get_uf_show(show)
......@@ -549,7 +584,7 @@ class ChangeStatusMessageDialog:
'_last_' + self.show, 'mood_text')
else:
self.title_text = _('Status Message')
self.window.set_title(self.title_text)
self.dialog.set_title(self.title_text)
message_textview = self.xml.get_widget('message_textview')
self.message_buffer = message_textview.get_buffer()
......@@ -572,10 +607,6 @@ class ChangeStatusMessageDialog:
self.preset_messages_dict[msg_name] = opts
sorted_keys_list = helpers.get_sorted_keys(self.preset_messages_dict)
countdown_time = gajim.config.get('change_status_window_timeout')
self.countdown_left = countdown_time
self.countdown_enabled = True
self.message_liststore = gtk.ListStore(str) # msg_name
self.message_combobox = self.xml.get_widget('message_combobox')
self.message_combobox.set_model(self.message_liststore)
......@@ -600,12 +631,10 @@ class ChangeStatusMessageDialog:
self.xml.get_widget('mood_button').hide()
self.xml.signal_autoconnect(self)
if countdown_time > 0:
self.countdown()
gobject.timeout_add(1000, self.countdown)
self.window.connect('response', self.on_dialog_response)
self.window.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
self.window.show_all()
self.run_timeout()
self.dialog.connect('response', self.on_dialog_response)
self.dialog.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
self.dialog.show_all()
def draw_activity(self):
'''Set activity button'''
......@@ -645,26 +674,16 @@ class ChangeStatusMessageDialog:
img.set_from_pixbuf(None)
label.set_text('')
def countdown(self):
if self.countdown_enabled:
if self.countdown_left <= 0:
# Prevent GUI freeze when the combobox menu is opened on close
self.message_combobox.popdown()
self.window.response(gtk.RESPONSE_OK)
return False
self.window.set_title('%s [%s]' % (self.title_text,
str(self.countdown_left)))
self.countdown_left -= 1
return True
else:
self.window.set_title(self.title_text)
return False
def on_timeout(self):
# Prevent GUI freeze when the combobox menu is opened on close
self.message_combobox.popdown()
self.dialog.response(gtk.RESPONSE_OK)
def on_dialog_response(self, dialog, response):
if response == gtk.RESPONSE_OK:
beg, end = self.message_buffer.get_bounds()
message = self.message_buffer.get_text(beg, end).decode('utf-8')\
.strip()
.strip()
message = helpers.remove_invalid_xml_chars(message)
msg = helpers.to_one_line(message)
if self.show:
......@@ -683,7 +702,7 @@ class ChangeStatusMessageDialog:
'mood_text', self.pep_dict['mood_text'])
else:
message = None # user pressed Cancel button or X wm button
self.window.destroy()
self.dialog.destroy()
self.on_response(message, self.pep_dict)
def on_message_combobox_changed(self, widget):
......@@ -707,7 +726,7 @@ class ChangeStatusMessageDialog:
if event.keyval == gtk.keysyms.Return or \
event.keyval == gtk.keysyms.KP_Enter: # catch CTRL+ENTER
if (event.state & gtk.gdk.CONTROL_MASK):
self.window.response(gtk.RESPONSE_OK)
self.dialog.response(gtk.RESPONSE_OK)
# Stop the event
return True
......@@ -4495,4 +4514,15 @@ class GPGInfoWindow:
def on_close_button_clicked(self, widget):
self.window.destroy()
class ResourceConflictDialog(TimeoutDialog, InputDialog):
def __init__(self, title, text, resource, ok_handler):
TimeoutDialog.__init__(self, 15, self.on_timeout)
InputDialog.__init__(self, title, text, input_str=resource,
is_modal=False, ok_handler=ok_handler)
self.title_text = title
self.run_timeout()
def on_timeout(self):
self.on_okbutton_clicked(None)
# vim: se ts=3:
......@@ -2097,9 +2097,11 @@ class Interface:
gajim.config.set_per('accounts', account, 'resource', new_resource)
self.roster.send_status(account, gajim.connections[account].old_show,
gajim.connections[account].status)
dlg = dialogs.InputDialog(_('Resource Conflict'),
_('You are already connected to this account with the same resource. Please type a new one'), input_str = gajim.connections[account].server_resource,
is_modal = False, ok_handler = on_ok)
proposed_resource = gajim.connections[account].server_resource
proposed_resource += gajim.config.get('gc_proposed_nick_char')
dlg = dialogs.ResourceConflictDialog(_('Resource Conflict'),
_('You are already connected to this account with the same resource. '
'Please type a new one'), resource=proposed_resource, ok_handler=on_ok)
def handle_event_pep_config(self, account, data):
# ('PEP_CONFIG', account, (node, form))
......
......@@ -2156,7 +2156,7 @@ class RosterWindow:
return
dlg = dialogs.ChangeStatusMessageDialog(on_response, show, show_pep)
dlg.window.present() # show it on current workspace
dlg.dialog.present() # show it on current workspace
def change_status(self, widget, account, status):
def change(account, status):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment