Daniel Brötzmann pushed to branch master at gajim / gajim
Commits: e348faf6 by wurstsalat at 2021-12-22T19:27:38+01:00 AddContact: Typing
- - - - - 211e49ee by wurstsalat at 2021-12-22T19:28:32+01:00 AddContact: Make sure Contact can be added under any circumstances
- - - - - 1ad8eddc by wurstsalat at 2021-12-22T19:46:46+01:00 Assistant: Typing
- - - - - 0f1e4c61 by wurstsalat at 2021-12-22T19:57:54+01:00 AdvancedConfig: Typing
- - - - -
4 changed files:
- gajim/common/app.py - gajim/gtk/add_contact.py - gajim/gtk/advanced_config.py - gajim/gtk/assistant.py
Changes:
===================================== gajim/common/app.py ===================================== @@ -399,12 +399,12 @@ def get_accounts_sorted() -> list[str]: def get_enabled_accounts_with_labels( exclude_local: bool = True, connected_only: bool = False, - private_storage_only: bool = False) -> list[str]: + private_storage_only: bool = False) -> list[list[str]]: """ Returns a list with [account, account_label] entries. Order by account_label """ - accounts: list[str] = [] + accounts = [] for acc in connections: if exclude_local and account_is_zeroconf(acc): continue
===================================== gajim/gtk/add_contact.py ===================================== @@ -12,9 +12,12 @@ # You should have received a copy of the GNU General Public License # along with Gajim. If not, see http://www.gnu.org/licenses/.
-from typing import Tuple -from typing import Optional from typing import Any +from typing import Dict +from typing import List +from typing import Optional +from typing import Tuple +from typing import Union
import logging
@@ -23,8 +26,11 @@ from nbxmpp import JID from nbxmpp import Namespace from nbxmpp.errors import is_error +from nbxmpp.errors import StanzaError +from nbxmpp.structs import DiscoInfo
from gajim.common import app +from gajim.common import types from gajim.common.helpers import get_subscription_request_msg from gajim.common.helpers import validate_jid from gajim.common.i18n import _ @@ -45,13 +51,13 @@ class AddContact(Assistant): def __init__(self, account: Optional[str] = None, jid: Optional[JID] = None, - nick: str = None): + nick: Optional[str] = None): Assistant.__init__(self) self.account = account self.jid = jid self._nick = nick
- self._result = None + self._result: Union[DiscoInfo, StanzaError, None] = None
self.add_button('next', _('Next'), complete=True, css_class='suggested-action') @@ -74,14 +80,17 @@ def __init__(self, progress.set_text(_('Trying to gather information on this address…'))
self.connect('button-clicked', self._on_button_clicked) + self.connect('destroy', self._on_destroy)
self.show_all() - self.connect('destroy', self._on_destroy)
- def _on_destroy(self, *args): + def _on_destroy(self, *args: Any) -> None: app.check_finalize(self)
- def _on_button_clicked(self, _assistant, button_name): + def _on_button_clicked(self, + _assistant: Assistant, + button_name: str + ) -> None: page = self.get_current_page() account, _ = self.get_page('address').get_account_and_jid()
@@ -126,7 +135,7 @@ def _start_disco(self) -> None: self._disco_info(account, jid)
@as_task - def _disco_info(self, account, address): + def _disco_info(self, account: str, address: str) -> None: _task = yield
client = app.get_client(account) @@ -139,7 +148,7 @@ def _disco_info(self, account, address): log.info('Disco Info received: %s', address) self._process_info(account, result)
- def _process_error(self, account, result): + def _process_error(self, account: str, result: StanzaError) -> None: log.debug('Error received: %s', result) self._result = result
@@ -155,7 +164,7 @@ def _process_error(self, account, result): self.get_page('error').set_text(result.get_text()) self.show_page('error', Gtk.StackTransitionType.SLIDE_LEFT)
- def _process_info(self, account, result): + def _process_info(self, account: str, result: DiscoInfo) -> None: log.debug('Info received: %s', result) self._result = result
@@ -186,14 +195,8 @@ def _process_info(self, account, result): self.show_page('gateway', Gtk.StackTransitionType.SLIDE_LEFT) return
- if result.has_category('client'): - self.get_page('contact').prepare(account, result) - self.show_page('contact', Gtk.StackTransitionType.SLIDE_LEFT) - return - - self.get_page('error').set_text( - _('This address does not seem to offer any gateway service.')) - self.show_page('error') + self.get_page('contact').prepare(account, result) + self.show_page('contact', Gtk.StackTransitionType.SLIDE_LEFT)
class Address(Page): @@ -218,12 +221,12 @@ def __init__(self, account: Optional[str], jid: Optional[JID]) -> None: if len(accounts) > 1: self._ui.account_box.show()
- if self._account is not None: + if account is not None: self._ui.account_combo.set_active_id(account) else: self._ui.account_combo.set_active(0) else: - # Set to first (and only) item; sets self._account as well + # Set to first (and only) item; sets self._account automatically self._ui.account_combo.set_active(0)
if jid is not None: @@ -233,29 +236,29 @@ def __init__(self, account: Optional[str], jid: Optional[JID]) -> None:
self.show_all()
- def get_visible_buttons(self): + def get_visible_buttons(self) -> List[str]: return ['next']
- def get_default_button(self): + def get_default_button(self) -> str: return 'next'
- def get_account_and_jid(self) -> Tuple[Optional[str], Any]: + def get_account_and_jid(self) -> Tuple[str, str]: return self._account, self._ui.address_entry.get_text()
def focus(self) -> None: self._ui.address_entry.grab_focus()
- def _on_account_changed(self, combobox): + def _on_account_changed(self, combobox: Gtk.ComboBox) -> None: account = combobox.get_active_id() self._account = account self._set_complete()
- def _show_icon(self, show): + def _show_icon(self, show: bool) -> None: icon = 'dialog-warning-symbolic' if show else None self._ui.address_entry.set_icon_from_icon_name( Gtk.EntryIconPosition.SECONDARY, icon)
- def _set_complete(self, *args): + def _set_complete(self, *args: Any) -> None: if self._account is None: self.complete = False self.update_page_complete() @@ -306,26 +309,26 @@ def _validate_address(self, address: str) -> bool:
class Error(ErrorPage): - def __init__(self): + def __init__(self) -> None: ErrorPage.__init__(self) self.set_title(_('Add Contact')) self.set_heading(_('An Error Occurred'))
- def get_visible_buttons(self): + def get_visible_buttons(self) -> List[str]: return ['back']
- def get_default_button(self): + def get_default_button(self) -> str: return 'back'
class Contact(Page): - def __init__(self): + def __init__(self) -> None: Page.__init__(self) self.title = _('Add Contact')
- self._result = None - self._account = None - self._contact = None + self._result: Union[DiscoInfo, StanzaError, None] = None + self._account: Optional[str] = None + self._contact: Optional[types.BareContact] = None
self._ui = get_builder('add_contact.ui') self.add(self._ui.contact_grid) @@ -334,13 +337,13 @@ def __init__(self):
self.show_all()
- def get_visible_buttons(self): + def get_visible_buttons(self) -> List[str]: return ['back', 'add']
- def get_default_button(self): + def get_default_button(self) -> str: return 'add'
- def prepare(self, account, result): + def prepare(self, account: str, result: Union[DiscoInfo, StanzaError]): self._result = result self._account = account
@@ -357,11 +360,11 @@ def _update_groups(self, account: str) -> None: for group in client.get_module('Roster').get_groups(): self._ui.group_combo.append_text(group)
- def _on_info_clicked(self, _button): + def _on_info_clicked(self, _button: Gtk.Button) -> None: open_window( 'ContactInfo', account=self._account, contact=self._contact)
- def get_subscription_data(self): + def get_subscription_data(self) -> Dict[str, Union[str, List[str], bool]]: group = self._ui.group_combo.get_child().get_text() groups = [group] if group else [] return { @@ -372,12 +375,12 @@ def get_subscription_data(self):
class Gateway(Page): - def __init__(self): + def __init__(self) -> None: Page.__init__(self) self.title = _('Service Gateway')
- self._account = None - self._result = None + self._account: Optional[str] = None + self._result: Optional[DiscoInfo] = None
self._ui = get_builder('add_contact.ui') self.add(self._ui.gateway_box) @@ -387,13 +390,13 @@ def __init__(self):
self.show_all()
- def get_visible_buttons(self): + def get_visible_buttons(self) -> List[str]: return ['back', 'add']
- def get_default_button(self): + def get_default_button(self) -> str: return 'add'
- def prepare(self, account, result): + def prepare(self, account: str, result: DiscoInfo) -> None: self._account = account self._result = result
@@ -447,22 +450,22 @@ def prepare(self, account, result): self._ui.commands_button.set_tooltip_text( _('This gateway does not support Ad-Hoc Commands.'))
- def _on_register_clicked(self, _button): + def _on_register_clicked(self, _button: Gtk.Button) -> None: open_window( 'ServiceRegistration', account=self._account, address=self._result.jid)
- def _on_command_clicked(self, _button): + def _on_command_clicked(self, _button: Gtk.Button) -> None: AdHocCommand(self._account, self._result.jid)
class GroupChat(Page): - def __init__(self): + def __init__(self) -> None: Page.__init__(self) self.title = _('Join Group Chat?')
- self._result = None + self._result: Optional[DiscoInfo] = None
heading = Gtk.Label(label=_('Join Group Chat?')) heading.get_style_context().add_class('large-header') @@ -477,13 +480,13 @@ def __init__(self):
self.show_all()
- def get_visible_buttons(self): + def get_visible_buttons(self) -> List[str]: return ['back', 'join']
- def get_default_button(self): + def get_default_button(self) -> str: return 'join'
- def prepare(self, account, result): + def prepare(self, account: str, result: DiscoInfo) -> None: self._result = result
self._info_box.set_account(account)
===================================== gajim/gtk/advanced_config.py ===================================== @@ -19,6 +19,8 @@ # You should have received a copy of the GNU General Public License # along with Gajim. If not, see http://www.gnu.org/licenses/.
+from typing import Optional + from enum import IntEnum from enum import unique
@@ -32,6 +34,7 @@
from gajim.common.setting_values import ADVANCED_SETTINGS from gajim.common.setting_values import APP_SETTINGS + from .builder import get_builder
@@ -56,7 +59,7 @@ class Column(IntEnum):
class AdvancedConfig(Gtk.ApplicationWindow): - def __init__(self): + def __init__(self) -> None: Gtk.ApplicationWindow.__init__(self) self.set_application(app.app) self.set_position(Gtk.WindowPosition.CENTER) @@ -107,7 +110,7 @@ def __init__(self): self._ui.connect_signals(self) self.show_all()
- def _on_key_press(self, _widget, event): + def _on_key_press(self, _widget: Gtk.Widget, event: Gdk.EventKey) -> None: if event.keyval != Gdk.KEY_Escape: return
@@ -117,11 +120,18 @@ def _on_key_press(self, _widget, event):
self.destroy()
- def _value_column_data_callback(self, _col, cell, model, iter_, _data): + def _value_column_data_callback(self, + _col: Gtk.TreeViewColumn, + cell: Gtk.CellRenderer, + model: Gtk.TreeModel, + iter_: Gtk.TreeIter, + _data: Optional[object]): opttype = model[iter_][Column.TYPE] cell.set_property('editable', opttype != SETTING_TYPES[bool])
- def _on_treeview_selection_changed(self, treeselection): + def _on_treeview_selection_changed(self, + treeselection: Gtk.TreeSelection + ) -> None: model, iter_ = treeselection.get_selected() if not iter_: self._ui.reset_button.set_sensitive(False) @@ -133,7 +143,11 @@ def _on_treeview_selection_changed(self, treeselection): self._ui.description.set_text(desc or Q_('?config description:None')) self._ui.reset_button.set_sensitive(True)
- def _on_treeview_row_activated(self, _treeview, path, _column): + def _on_treeview_row_activated(self, + _treeview: Gtk.TreeView, + path: Gtk.TreePath, + _column: Gtk.TreeViewColumn + ) -> None: modelpath = self.modelfilter.convert_path_to_child_path(path) modelrow = self.model[modelpath] setting = modelrow[Column.NAME] @@ -147,7 +161,11 @@ def _on_treeview_row_activated(self, _treeview, path, _column): app.settings.set(setting, setting_value) modelrow[Column.VALUE] = column_value
- def _on_config_edited(self, _cell, path, text): + def _on_config_edited(self, + _cell: Gtk.CellRendererText, + path: str, + text: str + ) -> None: path = Gtk.TreePath.new_from_string(path) modelpath = self.modelfilter.convert_path_to_child_path(path) modelrow = self.model[modelpath] @@ -160,7 +178,7 @@ def _on_config_edited(self, _cell, path, text): app.settings.set(setting, value) modelrow[Column.VALUE] = text
- def _on_reset_button_clicked(self, button): + def _on_reset_button_clicked(self, button: Gtk.Button) -> None: model, iter_ = self.treeview.get_selection().get_selected() if not iter_: return @@ -176,7 +194,7 @@ def _on_reset_button_clicked(self, button): app.settings.set(setting, default) button.set_sensitive(False)
- def _fill_model(self, node=None, parent=None): + def _fill_model(self, parent=None) -> None: for category, settings in ADVANCED_SETTINGS.items(): if category != 'app': continue @@ -199,7 +217,11 @@ def _fill_model(self, node=None, parent=None):
self.model.append(parent, [setting, value, type_])
- def _visible_func(self, model, treeiter, _data): + def _visible_func(self, + model: Gtk.TreeModel, + treeiter: Gtk.TreeIter, + _data: Optional[object] + ) -> bool: search_string = self._ui.search_entry.get_text().lower() if not search_string: return True @@ -211,5 +233,5 @@ def _visible_func(self, model, treeiter, _data): return True return False
- def _on_search_entry_changed(self, _widget): + def _on_search_entry_changed(self, _entry: Gtk.SearchEntry) -> None: self.modelfilter.refilter()
===================================== gajim/gtk/assistant.py ===================================== @@ -12,6 +12,12 @@ # 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 Any +from typing import Dict +from typing import Optional +from typing import Tuple + from gi.repository import Gdk from gi.repository import Gtk from gi.repository import Gio @@ -36,10 +42,10 @@ class Assistant(Gtk.ApplicationWindow, EventHelper): ))
def __init__(self, - transient_for=None, - width=550, - height=400, - transition_duration=200): + transient_for: Optional[Gtk.Window] = None, + width: int = 550, + height: int = 400, + transition_duration: int = 200) -> None: Gtk.ApplicationWindow.__init__(self) EventHelper.__init__(self) self.set_application(Gio.Application.get_default()) @@ -51,8 +57,8 @@ def __init__(self, self.set_transient_for(transient_for) self.set_type_hint(Gdk.WindowTypeHint.DIALOG)
- self._pages = {} - self._buttons = {} + self._pages: Dict[str, Page] = {} + self._buttons: Dict[str, Tuple[Gtk.Button, bool]] = {} self._button_visible_func = None
self._ui = get_builder('assistant.ui') @@ -64,29 +70,32 @@ def __init__(self, self.connect('destroy', self.__on_destroy) self._ui.connect_signals(self)
- def show_all(self): + def show_all(self) -> None: page_name = self._ui.stack.get_visible_child_name() self.emit('page-changed', page_name) Gtk.ApplicationWindow.show_all(self)
- def _on_key_press_event(self, _widget, event): + def _on_key_press_event(self, + _widget: Gtk.Widget, + event: Gdk.EventKey + ) -> None: if event.keyval == Gdk.KEY_Escape: self.destroy()
- def _update_page_complete(self, *args): + def _update_page_complete(self, *args: Any) -> None: page_widget = self._ui.stack.get_visible_child() for button, complete in self._buttons.values(): if complete: button.set_sensitive(page_widget.complete)
- def update_title(self): + def update_title(self) -> None: self.set_title(self._ui.stack.get_visible_child().title)
- def _hide_buttons(self): + def _hide_buttons(self) -> None: for button, _ in self._buttons.values(): button.hide()
- def _set_buttons_visible(self): + def _set_buttons_visible(self) -> None: page_name = self._ui.stack.get_visible_child_name() if self._button_visible_func is None: buttons = self.get_page(page_name).get_visible_buttons() @@ -112,11 +121,16 @@ def _set_buttons_visible(self): def set_button_visible_func(self, func): self._button_visible_func = func
- def set_default_button(self, button_name): + def set_default_button(self, button_name: str) -> None: button, _ = self._buttons[button_name] button.grab_default()
- def add_button(self, name, label, css_class=None, complete=False): + def add_button(self, + name: str, + label: str, + css_class: Optional[str] = None, + complete: bool = False + ) -> None: button = Gtk.Button(label=label, can_default=True, no_show_all=True) @@ -126,13 +140,13 @@ def add_button(self, name, label, css_class=None, complete=False): self._buttons[name] = (button, complete) self._ui.action_area.pack_end(button, False, False, 0)
- def add_pages(self, pages): + def add_pages(self, pages: Dict[str, Page]): self._pages = pages for name, widget in pages.items(): widget.connect('update-page-complete', self._update_page_complete) self._ui.stack.add_named(widget, name)
- def add_default_page(self, name): + def add_default_page(self, name: str) -> Page: if name == 'success': page = SuccessPage() elif name == 'error': @@ -146,19 +160,22 @@ def add_default_page(self, name): self._ui.stack.add_named(page, name) return page
- def get_current_page(self): + def get_current_page(self) -> str: return self._ui.stack.get_visible_child_name()
- def show_page(self, name, transition=Gtk.StackTransitionType.NONE): + def show_page(self, + name: str, + transition: Gtk.StackTransitionType = Gtk.StackTransitionType.NONE + ) -> None: if self._ui.stack.get_visible_child_name() == name: return self._hide_buttons() self._ui.stack.set_visible_child_full(name, transition)
- def get_page(self, name): + def get_page(self, name: str) -> Page: return self._pages[name]
- def _on_visible_child_name(self, stack, _param): + def _on_visible_child_name(self, stack: Gtk.Stack, _param: str) -> None: if stack.get_visible_child_name() is None: # Happens for some reason when adding the first page return @@ -166,14 +183,14 @@ def _on_visible_child_name(self, stack, _param): self._set_buttons_visible() self.emit('page-changed', stack.get_visible_child_name())
- def __on_button_clicked(self, button): + def __on_button_clicked(self, button: Gtk.Button) -> None: for button_name, button_data in self._buttons.items(): button_ = button_data[0] if button_ == button: self.emit('button-clicked', button_name) return
- def __on_destroy(self, *args): + def __on_destroy(self, *args: Any) -> None: self._pages.clear() self._buttons.clear()
@@ -184,26 +201,26 @@ class Page(Gtk.Box): 'update-page-complete': (GObject.SignalFlags.RUN_LAST, None, ()), }
- def __init__(self): + def __init__(self) -> None: Gtk.Box.__init__(self, orientation=Gtk.Orientation.VERTICAL) self.set_spacing(18) self.set_valign(Gtk.Align.CENTER)
- self.title = '' - self.complete = True + self.title: str = '' + self.complete: bool = True
- def get_visible_buttons(self): + def get_visible_buttons(self) -> None: return None
- def get_default_button(self): + def get_default_button(self) -> None: return None
- def update_page_complete(self): + def update_page_complete(self) -> None: self.emit('update-page-complete')
class DefaultPage(Page): - def __init__(self, icon_name, icon_css_class): + def __init__(self, icon_name: str, icon_css_class: str) -> None: Page.__init__(self)
self._heading = Gtk.Label() @@ -227,32 +244,32 @@ def __init__(self, icon_name, icon_css_class): self.pack_start(self._label, False, True, 0) self.show_all()
- def set_heading(self, heading): + def set_heading(self, heading: str) -> None: self._heading.set_text(heading)
- def set_text(self, text): + def set_text(self, text: str) -> None: self._label.set_text(text)
- def set_title(self, title): + def set_title(self, title: str) -> None: self.title = title
class ErrorPage(DefaultPage): - def __init__(self): + def __init__(self) -> None: DefaultPage.__init__(self, icon_name='dialog-error-symbolic', icon_css_class='error-color')
class SuccessPage(DefaultPage): - def __init__(self): + def __init__(self) -> None: DefaultPage.__init__(self, icon_name='object-select-symbolic', icon_css_class='success-color')
class ProgressPage(Page): - def __init__(self): + def __init__(self) -> None: Page.__init__(self)
self._label = Gtk.Label() @@ -269,8 +286,8 @@ def __init__(self):
self.show_all()
- def set_text(self, text): + def set_text(self, text: str) -> None: self._label.set_text(text)
- def set_title(self, title): + def set_title(self, title: str) -> None: self.title = title
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/compare/36d4071a8ade991b562b6832da20b1f4...