From e57817addc344e7c06446e394abe5fd68fe24301 Mon Sep 17 00:00:00 2001 From: Yann Leboulanger Date: Thu, 24 Aug 2017 15:41:39 +0200 Subject: [PATCH] Nicer code --- gajim/adhoc_commands.py | 116 ++++++++++++++++++-------------------- gajim/dataforms_widget.py | 12 ++-- 2 files changed, 63 insertions(+), 65 deletions(-) diff --git a/gajim/adhoc_commands.py b/gajim/adhoc_commands.py index a8ffd9bb2..03f23b56f 100644 --- a/gajim/adhoc_commands.py +++ b/gajim/adhoc_commands.py @@ -59,6 +59,13 @@ class CommandWindow: self.jid = jid self.commandnode = commandnode self.data_form_widget = None + self.stage_finish_cb = None + self.stage_back_button_cb = None + self.stage_forward_button_cb = None + self.stage_execute_button_cb = None + self.stage_close_button_cb = None + self.stage_restart_button_cb = None + self.stage_window_delete_cb = None # retrieving widgets from xml self.xml = gtkgui_helpers.get_gtk_builder('adhoc_commands_window.ui') @@ -109,54 +116,48 @@ class CommandWindow: # These functions are set up by appropriate stageX methods. def stage_finish(self, *anything): - pass - - def stage_back_button_clicked(self, *anything): - assert False - - def stage_forward_button_clicked(self, *anything): - assert False - - def stage_execute_button_clicked(self, *anything): - assert False - - def stage_close_button_clicked(self, *anything): - assert False - - def stage_restart_button_clicked(self, *anything): - assert False - - def stage_adhoc_commands_window_delete_event(self, *anything): - assert False - - def do_nothing(self, *anything): - return False + if self.stage_finish_cb: + self.stage_finish_cb(*anything) # Widget callbacks... def on_back_button_clicked(self, *anything): - return self.stage_back_button_clicked(*anything) + if self.stage_back_button_cb: + return self.stage_back_button_cb(*anything) + return False def on_forward_button_clicked(self, *anything): - return self.stage_forward_button_clicked(*anything) + if self.stage_forward_button_cb: + return self.stage_forward_button_cb(*anything) + return False def on_execute_button_clicked(self, *anything): - return self.stage_execute_button_clicked(*anything) + if self.stage_execute_button_cb: + return self.stage_execute_button_cb(*anything) + return False def on_finish_button_clicked(self, *anything): - return self.stage_finish_button_clicked(*anything) + if self.stage_finish_button_cb: + return self.stage_finish_button_cb(*anything) + return False def on_close_button_clicked(self, *anything): - return self.stage_close_button_clicked(*anything) + if self.stage_close_button_cb: + return self.stage_close_button_cb(*anything) + return False def on_restart_button_clicked(self, *anything): - return self.stage_restart_button_clicked(*anything) + if self.stage_restart_button_cb: + return self.stage_restart_button_cb(*anything) + return False def on_adhoc_commands_window_destroy(self, *anything): # TODO: do all actions that are needed to remove this object from memory self.remove_pulsing() def on_adhoc_commands_window_delete_event(self, *anything): - return self.stage_adhoc_commands_window_delete_event(self.window) + if self.stage_window_delete_cb: + return self.stage_window_delete_cb(self.window) + return False def __del__(self): print('Object has been deleted.') @@ -188,10 +189,10 @@ class CommandWindow: self.xml.get_object('retrieving_commands_progressbar')) # setup the callbacks - self.stage_finish = self.stage1_finish - self.stage_close_button_clicked = self.stage1_close_button_clicked - self.stage_restart_button_clicked = self.stage1_restart_button_clicked - self.stage_adhoc_commands_window_delete_event = \ + self.stage_finish_cb = self.stage1_finish + self.stage_close_button_cb = self.stage1_close_button_clicked + self.stage_restart_button_cb = self.stage1_restart_button_clicked + self.stage_window_delete_cb = \ self.stage1_adhoc_commands_window_delete_event def stage1_finish(self): @@ -245,11 +246,11 @@ class CommandWindow: self.command_list_vbox.pack_start(radio, False, True, 0) self.command_list_vbox.show_all() - self.stage_finish = self.stage2_finish - self.stage_close_button_clicked = self.stage2_close_button_clicked - self.stage_restart_button_clicked = self.stage2_restart_button_clicked - self.stage_forward_button_clicked = self.stage2_forward_button_clicked - self.stage_adhoc_commands_window_delete_event = self.do_nothing + self.stage_finish_cb = self.stage2_finish + self.stage_close_button_cb = self.stage2_close_button_clicked + self.stage_restart_button_cb = self.stage2_restart_button_clicked + self.stage_forward_button_cb = self.stage2_forward_button_clicked + self.stage_window_delete_cb = None def stage2_finish(self): """ @@ -296,18 +297,14 @@ class CommandWindow: self.stage3_submit_form() - self.stage_finish = self.stage3_finish - self.stage_back_button_clicked = self.stage3_back_button_clicked - self.stage_forward_button_clicked = self.stage3_forward_button_clicked - self.stage_execute_button_clicked = self.stage3_execute_button_clicked - self.stage_finish_button_clicked = self.stage3_finish_button_clicked - self.stage_close_button_clicked = self.stage3_close_button_clicked - self.stage_restart_button_clicked = self.stage3_restart_button_clicked - self.stage_adhoc_commands_window_delete_event = \ - self.stage3_close_button_clicked - - def stage3_finish(self): - pass + self.stage_finish_cb = None + self.stage_back_button_cb = self.stage3_back_button_clicked + self.stage_forward_button_cb = self.stage3_forward_button_clicked + self.stage_execute_button_cb = self.stage3_execute_button_clicked + self.stage_finish_button_cb = self.stage3_finish_button_clicked + self.stage_close_button_cb = self.stage3_close_button_clicked + self.stage_restart_button_cb = self.stage3_restart_button_clicked + self.stage_window_delete_cb = self.stage3_close_button_clicked def stage3_can_close(self, cb): if self.form_status == 'completed': @@ -447,8 +444,7 @@ class CommandWindow: self.execute_button.hide() self.finish_button.hide() self.close_button.show() - self.stage_adhoc_commands_window_delete_event = \ - self.stage3_close_button_clicked + self.stage_window_delete_cb = self.stage3_close_button_clicked note = command.getTag('note') if note: @@ -480,10 +476,10 @@ class CommandWindow: self.execute_button.set_sensitive(False) self.finish_button.set_sensitive(False) - self.stage_finish = self.do_nothing - self.stage_close_button_clicked = self.stage4_close_button_clicked - self.stage_restart_button_clicked = self.stage4_restart_button_clicked - self.stage_adhoc_commands_window_delete_event = self.do_nothing + self.stage_finish_cb = None + self.stage_close_button_cb = self.stage4_close_button_clicked + self.stage_restart_button_cb = self.stage4_restart_button_clicked + self.stage_window_delete_cb = None def stage4_close_button_clicked(self, widget): self.window.destroy() @@ -532,10 +528,10 @@ class CommandWindow: self.error_description_label.set_text(error) - self.stage_finish = self.do_nothing - self.stage_close_button_clicked = self.stage5_close_button_clicked - self.stage_restart_button_clicked = self.stage5_restart_button_clicked - self.stage_adhoc_commands_window_delete_event = self.do_nothing + self.stage_finish_cb = None + self.stage_close_button_cb = self.stage5_close_button_clicked + self.stage_restart_button_cb = self.stage5_restart_button_clicked + self.stage_window_delete_cb = None def stage5_close_button_clicked(self, widget): self.window.destroy() diff --git a/gajim/dataforms_widget.py b/gajim/dataforms_widget.py index bb9eb56a6..3f8d7b8ea 100644 --- a/gajim/dataforms_widget.py +++ b/gajim/dataforms_widget.py @@ -56,6 +56,7 @@ class DataFormWidget(Gtk.Alignment, object): self._data_form = None self.selectable = False + self.clean_cb = None self.xml = gtkgui_helpers.get_gtk_builder('data_form_window.ui', 'data_form_vbox') @@ -143,7 +144,8 @@ class DataFormWidget(Gtk.Alignment, object): rewritten by build_*_data_form, according to type of form which is actually displayed """ - pass + if self.clean_cb: + self.clean_cb() def build_single_data_form(self): '''Invoked when new single form is to be created.''' @@ -162,7 +164,7 @@ class DataFormWidget(Gtk.Alignment, object): self.data_form_types_notebook.page_num( self.single_form_scrolledwindow)) - self.clean_data_form = self.clean_single_data_form + self.clean_cb = self.clean_single_data_form def clean_single_data_form(self): """ @@ -170,7 +172,7 @@ class DataFormWidget(Gtk.Alignment, object): form from widget """ self.singleform.destroy() - self.clean_data_form = self.empty_method # we won't call it twice + self.clean_cb = None # we won't call it twice del self.singleform def build_multiple_data_form(self): @@ -216,7 +218,7 @@ class DataFormWidget(Gtk.Alignment, object): self.data_form_types_notebook.page_num( self.multiple_form_hbox)) - self.clean_data_form = self.clean_multiple_data_form + self.clean_cb = self.clean_multiple_data_form readwrite = self._data_form.type_ != 'result' if not readwrite: @@ -232,7 +234,7 @@ class DataFormWidget(Gtk.Alignment, object): Called as clean_data_form, read the docs of clean_data_form(). Remove form from widget """ - self.clean_data_form = self.empty_method # we won't call it twice + self.clean_cb = None # we won't call it twice del self.multiplemodel def refresh_multiple_buttons(self):