Refactor conditions to determine connected state

GUI code must not be aware of what integers map to connection states
This commit is contained in:
Philipp Hörist 2019-05-20 21:33:42 +02:00
parent 2485227701
commit 519d86444a
19 changed files with 55 additions and 57 deletions

View File

@ -739,7 +739,7 @@ class ChatControlBase(MessageControl, ChatCommandProcessor, CommandTools):
is_ctrl_enter = bool(event_state & Gdk.ModifierType.CONTROL_MASK)
send_message = is_ctrl_enter == app.config.get('send_on_ctrl_enter')
if send_message and app.connections[self.account].connected < 2:
if send_message and not app.account_is_connected(self.account):
# we are not connected
app.interface.raise_dialog('not-connected-while-sending')
elif send_message:

View File

@ -128,7 +128,7 @@ class StandardCommonCommands(CommandContainer):
if not app.config.get_per('accounts', connection.name,
'sync_with_global_status'):
continue
if connection.connected < 2:
if not connection.is_connected:
continue
connection.change_status(status, message)
@ -142,7 +142,7 @@ class StandardCommonCommands(CommandContainer):
if not app.config.get_per('accounts', connection.name,
'sync_with_global_status'):
continue
if connection.connected < 2:
if not connection.is_connected:
continue
connection.change_status('away', message)
@ -156,7 +156,7 @@ class StandardCommonCommands(CommandContainer):
if not app.config.get_per('accounts', connection.name,
'sync_with_global_status'):
continue
if connection.connected < 2:
if not connection.is_connected:
continue
connection.change_status('online', message)

View File

@ -453,7 +453,7 @@ def account_is_connected(account):
if account not in connections:
return False
# 0 is offline, 1 is connecting
return connections[account].connected > 1
return connections[account].is_connected
def is_invisible(account):
return SHOW_LIST[connections[account].connected] == 'invisible'

View File

@ -127,6 +127,10 @@ class CommonConnection:
self.get_config_values_or_default()
@property
def is_connected(self):
return self.connected > 1
def _register_new_handlers(self, con):
for handler in modules.get_handlers(self):
if len(handler) == 5:
@ -176,7 +180,7 @@ class CommonConnection:
def _prepare_message(self, obj):
if not self.connection or self.connected < 2:
if not self.connection or not self.is_connected:
return 1
if isinstance(obj.jid, list):
@ -511,7 +515,7 @@ class Connection(CommonConnection, ConnectionHandlers):
def reconnect(self):
# Do not try to reco while we are already trying
self.time_to_reconnect = None
if self.connected < 2: # connection failed
if not self.is_connected: # connection failed
log.info('Reconnect')
self.connected = 1
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
@ -1347,7 +1351,7 @@ class Connection(CommonConnection, ConnectionHandlers):
return
# If we are already connected, and privacy rules are supported, send
# offline presence first as it's required by XEP-0126
if self.connected > 1 and self.get_module('PrivacyLists').supported:
if self.is_connected and self.get_module('PrivacyLists').supported:
self.get_module('Bytestream').remove_all_transfers()
self.get_module('Presence').send_presence(
typ='unavailable',
@ -1780,7 +1784,7 @@ class Connection(CommonConnection, ConnectionHandlers):
# Account may have been disabled
return
if self.time_to_reconnect:
if self.connected < 2:
if not self.is_connected:
self.reconnect()
else:
self.time_to_reconnect = None

View File

@ -766,7 +766,7 @@ class JingleSession:
def _session_terminate(self, reason=None):
stanza, jingle = self.__make_jingle('session-terminate', reason=reason)
self.__broadcast_all(stanza, jingle, None, 'session-terminate-sent')
if self.connection.connection and self.connection.connected >= 2:
if self.connection.connection and self.connection.is_connected:
self.connection.connection.send(stanza)
# TODO: Move to GUI?
reason, text = self.__reason_from_stanza(jingle)
@ -818,7 +818,7 @@ class JingleSession:
def __content_remove(self, content, reason=None):
assert self.state != JingleStates.ENDED
if self.connection.connection and self.connection.connected > 1:
if self.connection.connection and self.connection.is_connected:
stanza, jingle = self.__make_jingle('content-remove', reason=reason)
self.__append_content(jingle, content)
self.connection.connection.send(stanza)

View File

@ -135,7 +135,7 @@ class Bytestream(BaseModule):
Send iq, confirming that we want to download the file
"""
# user response to ConfirmationDialog may come after we've disconneted
if not self._con.connection or self._con.connected < 2:
if not app.account_is_connected(self._account):
return
# file transfer initiated by a jingle session
@ -176,7 +176,7 @@ class Bytestream(BaseModule):
invalid stream or 'profile' for invalid profile
"""
# user response to ConfirmationDialog may come after we've disconnected
if not self._con.connection or self._con.connected < 2:
if not app.account_is_connected(self._account):
return
if file_props.sid in self._sessions:
@ -188,7 +188,7 @@ class Bytestream(BaseModule):
"""
Send reply to the initiator of FT that we made a connection
"""
if not self._con.connection or self._con.connected < 2:
if not app.account_is_connected(self._account):
return
if streamhost is None:
return None
@ -249,7 +249,7 @@ class Bytestream(BaseModule):
"""
Send iq for the present streamhosts and proxies
"""
if not self._con.connection or self._con.connected < 2:
if not app.account_is_connected(self._account):
return
receiver = file_props.receiver
sender = file_props.sender
@ -463,7 +463,7 @@ class Bytestream(BaseModule):
Called when there is an error establishing BS connection, or when
connection is rejected
"""
if not self._con.connection or self._con.connected < 2:
if not app.account_is_connected(self._account):
return
file_props = FilesProp.getFileProp(self._account, sid)
if file_props is None:
@ -491,7 +491,7 @@ class Bytestream(BaseModule):
"""
Called after authentication to proxy server
"""
if not self._con.connection or self._con.connected < 2:
if not app.account_is_connected(self._account):
return
file_props = FilesProp.getFileProp(self._account, proxy['sid'])
iq = nbxmpp.Iq(to=proxy['initiator'], typ='set')

View File

@ -41,7 +41,7 @@ class EntityTime(BaseModule):
if not app.account_is_connected(self._account):
return
# If we are invisible, do not request
if self._con.connected == app.SHOW_LIST.index('invisible'):
if app.is_invisible(self._account):
return
if resource:

View File

@ -189,7 +189,7 @@ class Presence(BaseModule):
if jid in app.to_be_removed[self._account]:
app.to_be_removed[self._account].remove(jid)
elif event.old_show > 1 and event.new_show == 0 and \
self._con.connected > 1:
self._con.is_connected:
if not jid in app.to_be_removed[self._account]:
app.to_be_removed[self._account].append(jid)
if jid in app.newly_added[self._account]:

View File

@ -224,7 +224,7 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
app.newly_added[self.name].append(jid)
if jid in app.to_be_removed[self.name]:
app.to_be_removed[self.name].remove(jid)
elif event.old_show > 1 and event.new_show == 0 and self.connected > 1:
elif event.old_show > 1 and event.new_show == 0 and self.is_connected:
if not jid in app.to_be_removed[self.name]:
app.to_be_removed[self.name].append(jid)
if jid in app.newly_added[self.name]:

View File

@ -783,7 +783,7 @@ class SynchroniseSelectAccountDialog:
return
remote_account = model.get_value(iter_, 0)
if app.connections[remote_account].connected < 2:
if not app.account_is_connected(remote_account):
ErrorDialog(_('This account is not connected to the server'),
_('You cannot synchronize with an account unless it is connected.'))
return

View File

@ -488,7 +488,7 @@ class ServiceDiscoveryWindow:
self.reloading = False
# Check connection
if app.connections[account].connected < 2:
if not app.account_is_connected(account):
ErrorDialog(_('You are not connected to the server'),
_('Without a connection, you can not browse available services'))
raise RuntimeError('You must be connected to browse services')

View File

@ -187,8 +187,7 @@ class JoinGroupchatWindow(Gtk.ApplicationWindow):
account = self.account_combo.get_active_id()
nickname = self.nick_entry.get_text()
invisible_show = app.SHOW_LIST.index('invisible')
if app.connections[account].connected == invisible_show:
if app.is_invisible(account):
app.interface.raise_dialog('join-while-invisible')
return

View File

@ -316,7 +316,7 @@ class ProfileWindow(Gtk.ApplicationWindow):
if self.update_progressbar_timeout_id:
# Operation in progress
return
if app.connections[self.account].connected < 2:
if not app.account_is_connected(self.account):
ErrorDialog(
_('You are not connected to the server'),
_('Without a connection, you can not publish your contact '

View File

@ -651,5 +651,5 @@ class ChangePasswordSetting(DialogSetting):
activatable = False
if self.account in app.connections:
con = app.connections[self.account]
activatable = con.connected >= 2 and con.register_supported
activatable = con.is_connected and con.register_supported
self.set_activatable(activatable)

View File

@ -269,7 +269,7 @@ class SingleMessageWindow(Gtk.ApplicationWindow):
_('Characters typed: %s') % str(characters_no))
def send_single_message(self):
if app.connections[self.account].connected <= 1:
if not app.account_is_connected(self.account):
# if offline or connecting
ErrorDialog(_('Connection not available'),
_('Please make sure you are connected with "%s".') % self.account)

View File

@ -148,7 +148,7 @@ class XMLConsoleWindow(Gtk.Window):
self._ui.input_entry.grab_focus()
def on_send(self, *args):
if app.connections[self.account].connected <= 1:
if not app.account_is_connected(self.account):
# if offline or connecting
ErrorDialog(
_('Connection not available'),

View File

@ -933,9 +933,8 @@ class Interface:
if obj.conn.get_module('MAM').available:
obj.conn.get_module('MAM').request_archive_on_signin()
invisible_show = app.SHOW_LIST.index('invisible')
# We cannot join rooms if we are invisible
if connected == invisible_show:
if app.is_invisible(account):
return
# send currently played music
if (pep_supported and sys.platform not in ('win32', 'darwin') and
@ -1626,8 +1625,7 @@ class Interface:
self.roster.on_groupchat_maximized(None, room_jid, account)
return
invisible_show = app.SHOW_LIST.index('invisible')
if app.connections[account].connected == invisible_show:
if app.is_invisible(account):
ErrorDialog(
_('You cannot join a group chat while you are invisible'))
return
@ -2179,7 +2177,7 @@ class Interface:
GLib.timeout_add_seconds(2, connection.reconnect)
else:
for connection in app.connections.values():
if connection.connected > 1:
if connection.is_connected:
log.info('Disconnect %s', connection.name)
connection.disconnect(immediately=True)

View File

@ -437,7 +437,7 @@ class GajimRemote(Server):
if not account and len(accounts) == 1:
account = accounts[0]
if account:
if app.connections[account].connected > 1: # account is connected
if app.account_is_connected(account): # account is connected
connected_account = account
contact = app.contacts.get_contact_with_highest_priority(
account, jid)
@ -445,7 +445,7 @@ class GajimRemote(Server):
for account_ in accounts:
contact = app.contacts.get_contact_with_highest_priority(
account, jid)
if contact and app.connections[account_].connected > 1:
if contact and app.account_is_connected(account_):
# account is connected
connected_account = account_
break
@ -466,14 +466,14 @@ class GajimRemote(Server):
if not account and len(accounts) == 1:
account = accounts[0]
if account:
if app.connections[account].connected > 1 and \
if app.account_is_connected(account) and \
room_jid in app.gc_connected[account] and \
app.gc_connected[account][room_jid]:
# account and groupchat are connected
connected_account = account
else:
for account_ in accounts:
if app.connections[account_].connected > 1 and \
if app.account_is_connected(account_) and \
room_jid in app.gc_connected[account_] and \
app.gc_connected[account_][room_jid]:
# account and groupchat are connected
@ -590,7 +590,7 @@ class GajimRemote(Server):
connected_account = None
first_connected_acct = None
for acct in accounts:
if app.connections[acct].connected > 1: # account is online
if app.account_is_connected(acct): # account is online
contact = app.contacts.get_first_contact_from_jid(acct, jid)
if app.interface.msg_win_mgr.has_window(jid, acct):
connected_account = acct
@ -790,8 +790,7 @@ class GajimRemote(Server):
def add_contact(self, jid, account):
if account:
if account in app.connections and \
app.connections[account].connected > 1:
if app.account_is_connected(account):
# if given account is active, use it
AddNewContactWindow(account=account, jid=jid)
else:

View File

@ -2063,7 +2063,7 @@ class RosterWindow:
app.config.set_per('accounts', account, 'last_status', status)
app.config.set_per('accounts', account, 'last_status_msg',
helpers.to_one_line(txt))
if app.connections[account].connected < 2:
if not app.account_is_connected(account):
self.set_connecting_state(account)
self.send_status_continue(account, status, txt, auto, to)
@ -2112,8 +2112,7 @@ class RosterWindow:
else:
if status in ('invisible', 'offline'):
self.delete_pep(app.get_jid_from_account(account), account)
was_invisible = app.connections[account].connected == \
app.SHOW_LIST.index('invisible')
was_invisible = app.is_invisible(account)
app.connections[account].change_status(status, txt, auto)
for gc_control in app.interface.msg_win_mgr.get_controls(
@ -2409,7 +2408,7 @@ class RosterWindow:
accounts = list(app.connections.keys())
get_msg = False
for acct in accounts:
if app.connections[acct].connected:
if app.account_is_connected(acct):
get_msg = True
break
@ -2417,7 +2416,7 @@ class RosterWindow:
self.quit_on_next_offline = 0
accounts_to_disconnect = []
for acct in accounts:
if app.connections[acct].connected > 1:
if app.account_is_connected(acct):
self.quit_on_next_offline += 1
accounts_to_disconnect.append(acct)
@ -2505,7 +2504,7 @@ class RosterWindow:
GLib.timeout_add_seconds(5, self.remove_newly_added, jid,
account)
elif obj.old_show > 1 and obj.new_show == 0 and \
obj.conn.connected > 1:
obj.conn.is_connected:
GLib.timeout_add_seconds(5, self.remove_to_be_removed,
jid, account)
@ -2868,7 +2867,7 @@ class RosterWindow:
return
# account is offline, don't allow to rename
if app.connections[account].connected < 2:
if not app.account_is_connected(account):
return
if row_type in ('contact', 'agent'):
# it's jid
@ -3225,8 +3224,7 @@ class RosterWindow:
elif type_ == 'account':
account = model[path][Column.ACCOUNT]
if account != 'all':
show = app.connections[account].connected
if show > 1: # We are connected
if app.account_is_connected(account):
self.on_change_status_message_activate(widget, account)
return True
show = helpers.get_global_show()
@ -4202,7 +4200,7 @@ class RosterWindow:
if account_dest == 'all':
return
# nothing can be done, if destination account is offline
if app.connections[account_dest].connected < 2:
if not app.account_is_connected(account_dest):
return
# A file got dropped on the roster
@ -4748,7 +4746,7 @@ class RosterWindow:
item = Gtk.MenuItem.new_with_mnemonic(uf_show)
sub_menu.append(item)
con = app.connections[account]
if show == 'invisible' and con.connected > 1 and \
if show == 'invisible' and con.is_connected and \
not con.get_module('PrivacyLists').supported:
item.set_sensitive(False)
else:
@ -4761,7 +4759,7 @@ class RosterWindow:
sub_menu.append(item)
item.connect('activate', self.on_change_status_message_activate,
account)
if app.connections[account].connected < 2:
if not app.account_is_connected(account):
item.set_sensitive(False)
item = Gtk.SeparatorMenuItem.new()
@ -4831,7 +4829,7 @@ class RosterWindow:
self.add_bookmarks_list(gc_sub_menu, account)
# make some items insensitive if account is offline
if app.connections[account].connected < 2:
if not app.account_is_connected(account):
for widget in (add_contact_menuitem, service_discovery_menuitem,
join_group_chat_menuitem, execute_command_menuitem,
pep_menuitem):
@ -4859,7 +4857,7 @@ class RosterWindow:
sub_menu.append(item)
item.connect('activate', self.on_change_status_message_activate,
account)
if app.connections[account].connected < 2:
if not app.account_is_connected(account):
item.set_sensitive(False)
uf_show = helpers.get_uf_show('offline', use_mnemonic=True)
@ -4969,7 +4967,7 @@ class RosterWindow:
if app.config.get_per('accounts', account, 'is_zeroconf'):
send_group_message_item.set_sensitive(False)
if app.connections[account].connected < 2:
if not app.account_is_connected(account):
send_group_message_item.set_sensitive(False)
invite_menuitem.set_sensitive(False)
@ -5012,7 +5010,7 @@ class RosterWindow:
group, account)
# unsensitive if account is not connected
if app.connections[account].connected < 2:
if not app.account_is_connected(account):
rename_item.set_sensitive(False)
# General group cannot be changed
@ -5052,7 +5050,7 @@ class RosterWindow:
for titer in iters:
jid = model[titer][Column.JID]
account = model[titer][Column.ACCOUNT]
if app.connections[account].connected < 2:
if not app.account_is_connected(account):
one_account_offline = True
if not app.connections[account].get_module('PrivacyLists').supported:
privacy_rules_supported = False