Daniel Brötzmann pushed to branch master at gajim / gajim
Commits: a4880100 by wurstsalat at 2022-01-22T19:25:15+01:00 AdHocCommands: Use window const, typing
- - - - -
6 changed files:
- gajim/gtk/add_contact.py - gajim/gtk/adhoc.py - gajim/gtk/const.py - gajim/gtk/controls/groupchat.py - gajim/gtk/discovery.py - gajim/gtk/main.py
Changes:
===================================== gajim/gtk/add_contact.py ===================================== @@ -36,7 +36,6 @@ from gajim.common.i18n import _ from gajim.common.modules.util import as_task
-from .adhoc import AdHocCommand from .assistant import Assistant from .assistant import Page from .assistant import ErrorPage @@ -475,7 +474,9 @@ def _on_register_clicked(self, _button: Gtk.Button) -> None: address=self._result.jid)
def _on_command_clicked(self, _button: Gtk.Button) -> None: - AdHocCommand(self._account, self._result.jid) + assert self._result is not None + open_window( + 'AdHocCommand', account=self._account, jid=str(self._result.jid))
class GroupChat(Page):
===================================== gajim/gtk/adhoc.py ===================================== @@ -12,6 +12,13 @@ # You should have received a copy of the GNU General Public License # along with Gajim. If not, see http://www.gnu.org/licenses/.
+from __future__ import annotations + +from typing import cast +from typing import Any +from typing import Optional +from typing import Union + import logging
from gi.repository import Gtk @@ -21,6 +28,12 @@ from nbxmpp.modules import dataforms from nbxmpp.errors import StanzaError from nbxmpp.errors import MalformedStanzaError +from nbxmpp.modules.dataforms import SimpleDataForm +from nbxmpp.modules.dataforms import MultipleDataForm +from nbxmpp.simplexml import Node +from nbxmpp.structs import AdHocCommand +from nbxmpp.structs import AdHocCommandNote +from nbxmpp.task import Task
from gajim.common import app from gajim.common.i18n import _ @@ -38,15 +51,15 @@ log = logging.getLogger('gajim.gui.adhoc')
-class AdHocCommand(Assistant): - def __init__(self, account, jid=None): +class AdHocCommands(Assistant): + def __init__(self, account: str, jid: Optional[str] = None) -> None: Assistant.__init__(self, width=600, height=500) + self.account = account + self.jid = jid
self._destroyed = False
self._client = app.get_client(account) - self._account = account - self._jid = jid
self.add_button('complete', _('Complete'), complete=True, css_class='suggested-action') @@ -78,9 +91,9 @@ def __init__(self, account, jid=None): self.show_all()
@ensure_not_destroyed - def _received_command_list(self, task): + def _received_command_list(self, task: Task) -> None: try: - commands = task.finish() + commands = cast(Optional[list[AdHocCommand]], task.finish()) except (StanzaError, MalformedStanzaError) as error: self._set_error(to_user_string(error), False) return @@ -89,13 +102,14 @@ def _received_command_list(self, task): self._set_error(_('No commands available'), False) return
- self.get_page('commands').add_commands(commands) + commands_page = cast(Commands, self.get_page('commands')) + commands_page.add_commands(commands) self.show_page('commands')
@ensure_not_destroyed - def _received_stage(self, task): + def _received_stage(self, task: Task) -> None: try: - stage = task.finish() + stage = cast(AdHocCommand, task.finish()) except (StanzaError, MalformedStanzaError) as error: self._set_error(to_user_string(error), True) return @@ -104,19 +118,23 @@ def _received_stage(self, task): if stage.is_completed: page_name = 'completed'
- page = self.get_page(page_name) + page = cast(Union[Stage, Completed], self.get_page(page_name)) page.process_stage(stage) self.show_page(page_name)
- def _set_error(self, text, show_command_button): - self.get_page('error').set_show_commands_button(show_command_button) - self.get_page('error').set_text(text) + def _set_error(self, text: str, show_command_button: bool) -> None: + error_page = cast(Error, self.get_page('error')) + error_page.set_show_commands_button(show_command_button) + error_page.set_text(text) self.show_page('error')
- def _on_destroy(self, *args): + def _on_destroy(self, _widget: Gtk.Widget) -> None: self._destroyed = True
- def _on_button_clicked(self, _assistant, button_name): + def _on_button_clicked(self, + _assistant: AdHocCommands, + button_name: str + ) -> None: if button_name == 'commands': self.show_page('commands')
@@ -132,8 +150,9 @@ def _on_button_clicked(self, _assistant, button_name): else: raise ValueError('Invalid button name: %s' % button_name)
- def _on_stage_action(self, action): - command, dataform = self.get_page('stage').stage_data + def _on_stage_action(self, action: AdHocAction) -> None: + stage_page = cast(Stage, self.get_page('stage')) + command, dataform = stage_page.stage_data if action == AdHocAction.PREV: dataform = None
@@ -144,10 +163,12 @@ def _on_stage_action(self, action): callback=self._received_stage)
self.show_page('executing') - self.get_page('stage').clear() + stage_page = cast(Stage, self.get_page('stage')) + stage_page.clear()
- def _on_execute(self, *args): - command = self.get_page('commands').get_selected_command() + def _on_execute(self, *args: Any) -> None: + commands_page = cast(Commands, self.get_page('commands')) + command = commands_page.get_selected_command() if command is None: return
@@ -158,8 +179,9 @@ def _on_execute(self, *args):
self.show_page('executing')
- def _on_cancel(self): - command, _ = self.get_page('stage').stage_data + def _on_cancel(self) -> None: + stage_page = cast(Stage, self.get_page('stage')) + command, _ = stage_page.stage_data self._client.get_module('AdHocCommands').execute_command( command, AdHocAction.CANCEL) self.show_page('commands') @@ -171,14 +193,14 @@ class Commands(Page): 'execute': (GObject.SignalFlags.RUN_LAST, None, ()), }
- def __init__(self): + def __init__(self) -> None: Page.__init__(self)
self.set_valign(Gtk.Align.FILL) self.complete = True self.title = _('Command List')
- self._commands = {} + self._commands: dict[str, AdHocCommand] = {} self._scrolled = Gtk.ScrolledWindow() self._scrolled.get_style_context().add_class('adhoc-scrolled') self._scrolled.set_max_content_height(400) @@ -204,33 +226,41 @@ def __init__(self): self.show_all()
@staticmethod - def _search_func(model, _column, search_text, iter_): + def _search_func(model: Gtk.TreeModel, + _column: int, + search_text: str, + iter_: Gtk.TreeIter + ) -> bool: return search_text.lower() not in model[iter_][0].lower()
- def _on_row_activate(self, _tree_view, _path, _column): + def _on_row_activate(self, + _tree_view: Gtk.TreeView, + _path: Gtk.TreePath, + _column: Gtk.TreeViewColumn + ) -> None: self.emit('execute')
- def add_commands(self, commands): + def add_commands(self, commands: list[AdHocCommand]) -> None: self._store.clear() self._commands = {} for command in commands: - key = '%s:%s' % (command.jid, command.node) + key = f'{command.jid}:{command.node}' self._commands[key] = command self._store.append((command.name, key))
- def get_selected_command(self): + def get_selected_command(self) -> Optional[AdHocCommand]: model, treeiter = self._treeview.get_selection().get_selected() if treeiter is None: return None key = model[treeiter][1] return self._commands[key]
- def get_visible_buttons(self): + def get_visible_buttons(self) -> list[str]: return ['execute']
class Stage(Page): - def __init__(self): + def __init__(self) -> None: Page.__init__(self)
self.set_valign(Gtk.Align.FILL) @@ -238,31 +268,35 @@ def __init__(self): self.title = _('Stage')
self._dataform_widget = None - self._notes = [] - self._last_stage_data = None + self._notes: list[Gtk.Label] = [] + self._last_stage_data: Optional[AdHocCommand] = None self.default = None self.show_all()
@property - def stage_data(self): + def stage_data(self) -> tuple[AdHocCommand, + Union[SimpleDataForm, MultipleDataForm]]: + assert self._last_stage_data is not None + assert self._dataform_widget is not None return self._last_stage_data, self._dataform_widget.get_submit_form()
@property - def actions(self): + def actions(self) -> Optional[set[AdHocAction]]: + assert self._last_stage_data is not None return self._last_stage_data.actions
- def clear(self): + def clear(self) -> None: self._show_form(None) self._show_notes(None) self._last_stage_data = None
- def process_stage(self, stage_data): + def process_stage(self, stage_data: AdHocCommand) -> None: self._last_stage_data = stage_data self._show_notes(stage_data.notes) self._show_form(stage_data.data) self.default = stage_data.default
- def _show_form(self, form): + def _show_form(self, form: Optional[Node]) -> None: if self._dataform_widget is not None: self.remove(self._dataform_widget) self._dataform_widget.destroy() @@ -276,7 +310,7 @@ def _show_form(self, form): self._dataform_widget.show_all() self.add(self._dataform_widget)
- def _show_notes(self, notes): + def _show_notes(self, notes: Optional[list[AdHocCommandNote]]): for note in self._notes: self.remove(note) self._notes = [] @@ -290,28 +324,28 @@ def _show_notes(self, notes): self._notes.append(label) self.add(label)
- def _on_is_valid(self, _widget, is_valid): + def _on_is_valid(self, _widget: DataFormWidget, is_valid: bool) -> None: self.complete = is_valid self.update_page_complete()
- def get_visible_buttons(self): + def get_visible_buttons(self) -> list[str]: actions = list(map(lambda action: action.value, self._last_stage_data.actions)) return actions
- def get_default_button(self): + def get_default_button(self) -> str: return self._last_stage_data.default.value
class Completed(Page): - def __init__(self): + def __init__(self) -> None: Page.__init__(self)
self.set_valign(Gtk.Align.FILL) self.complete = True self.title = _('Completed')
- self._notes = [] + self._notes: list[MultiLineLabel] = [] self._dataform_widget = None
icon = Gtk.Image.new_from_icon_name('object-select-symbolic', @@ -331,12 +365,12 @@ def __init__(self):
self.show_all()
- def process_stage(self, stage_data): + def process_stage(self, stage_data: AdHocCommand) -> None: self._show_notes(stage_data.notes) self._show_form(stage_data.data) self._show_icon_text(stage_data.data is None)
- def _show_icon_text(self, show): + def _show_icon_text(self, show: bool) -> None: if show: self.set_valign(Gtk.Align.CENTER) self._icon_text.show_all() @@ -344,7 +378,7 @@ def _show_icon_text(self, show): self.set_valign(Gtk.Align.FILL) self._icon_text.hide()
- def _show_form(self, form): + def _show_form(self, form: Optional[Node]) -> None: if self._dataform_widget is not None: self.remove(self._dataform_widget) self._dataform_widget.destroy() @@ -358,7 +392,7 @@ def _show_form(self, form): self._dataform_widget.show_all() self.add(self._dataform_widget)
- def _show_notes(self, notes): + def _show_notes(self, notes: list[AdHocCommandNote]): for note in self._notes: self.remove(note) self._notes = [] @@ -370,35 +404,35 @@ def _show_notes(self, notes): self._notes.append(label) self.add(label)
- def get_visible_buttons(self): + def get_visible_buttons(self) -> list[str]: return ['commands']
class Error(ErrorPage): - def __init__(self): + def __init__(self) -> None: ErrorPage.__init__(self)
self._show_commands_button = False self.set_heading(_('An error occurred'))
- def set_show_commands_button(self, value): + def set_show_commands_button(self, value: bool) -> None: self._show_commands_button = value
- def get_visible_buttons(self): + def get_visible_buttons(self) -> list[str]: if self._show_commands_button: return ['commands'] - return None + return []
class Executing(ProgressPage): - def __init__(self): + def __init__(self) -> None: ProgressPage.__init__(self) self.set_title(_('Executing…')) self.set_text(_('Executing…'))
class RequestCommandList(ProgressPage): - def __init__(self): + def __init__(self) -> None: ProgressPage.__init__(self) self.set_title(_('Requesting Command List')) self.set_text(_('Requesting Command List'))
===================================== gajim/gtk/const.py ===================================== @@ -134,6 +134,7 @@ def __str__(self): 'AccountsWindow': 'gajim.gui.accounts', 'AccountWizard': 'gajim.gui.account_wizard', 'AddContact': 'gajim.gui.add_contact', + 'AdHocCommands': 'gajim.gui.adhoc', 'AdhocMUC': 'gajim.gui.adhoc_muc', 'AdvancedConfig': 'gajim.gui.advanced_config', 'BlockingList': 'gajim.gui.blocking',
===================================== gajim/gtk/controls/groupchat.py ===================================== @@ -75,7 +75,6 @@ from gajim.gui.dialogs import ConfirmationDialog from gajim.gui.filechoosers import AvatarChooserDialog from gajim.gui.groupchat_config import GroupchatConfig -from gajim.gui.adhoc import AdHocCommand from gajim.gui.avatar_selector import AvatarSelector from gajim.gui.dataform import DataFormWidget from gajim.gui.groupchat_info import GroupChatInfoScrolled @@ -618,7 +617,7 @@ def _on_execute_command(self, nick = param.get_string() if nick: jid += '/' + nick - AdHocCommand(self.account, jid) + open_window('AdHocCommands', account=self.account, jid=jid)
def _on_change_avatar(self, _button: Gtk.Button) -> None: def _on_accept(path: str) -> None:
===================================== gajim/gtk/discovery.py ===================================== @@ -58,7 +58,6 @@ from gajim.common.i18n import _ from gajim.common.const import StyleAttr
-from .adhoc import AdHocCommand from .dialogs import ErrorDialog from .search import Search from .util import icon_exists @@ -1332,7 +1331,7 @@ def _on_execute_button_clicked(self, widget=None): if not iter_: return service = model[iter_][0] - AdHocCommand(self.account, service) + open_window('AdHocCommands', account=self.account, jid=service)
def _on_register_button_clicked(self, widget=None): """
===================================== gajim/gtk/main.py ===================================== @@ -43,7 +43,6 @@ from gajim.common.ged import EventHelper from gajim.common.modules.bytestream import is_transfer_active
-from .adhoc import AdHocCommand from .account_side_bar import AccountSideBar from .app_side_bar import AppSideBar from .workspace_side_bar import WorkspaceSideBar @@ -609,7 +608,7 @@ def contact_info(account: str, jid: str) -> None: @staticmethod def execute_command(account: str, jid: str) -> None: # TODO: Resource? - AdHocCommand(account, jid) + open_window('AdHocCommands', account=account, jid=jid)
def block_contact(self, account: str, jid: str) -> None: client = app.get_client(account)
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/commit/a4880100643e69ae2b037014e2d68383b...