Commit 4f4624e0 authored by Philipp Hörist's avatar Philipp Hörist

Add dedicated connection state variable

parent adef78c5
......@@ -137,7 +137,7 @@ class StandardCommonCommands(CommandContainer):
if not app.config.get_per('accounts', connection.name,
'sync_with_global_status'):
continue
if not connection.is_connected:
if not connection.state.is_connected:
continue
connection.change_status(status, message)
......@@ -151,7 +151,7 @@ class StandardCommonCommands(CommandContainer):
if not app.config.get_per('accounts', connection.name,
'sync_with_global_status'):
continue
if not connection.is_connected:
if not connection.state.is_connected:
continue
connection.change_status('away', message)
......@@ -165,7 +165,7 @@ class StandardCommonCommands(CommandContainer):
if not app.config.get_per('accounts', connection.name,
'sync_with_global_status'):
continue
if not connection.is_connected:
if not connection.state.is_connected:
continue
connection.change_status('online', message)
......
......@@ -490,7 +490,7 @@ def account_is_connected(account):
if account not in connections:
return False
# 0 is offline, 1 is connecting
return connections[account].is_connected
return connections[account].state.is_connected
def is_invisible(account):
return SHOW_LIST[connections[account].connected] == 'invisible'
......
......@@ -59,6 +59,7 @@ from gajim.common import i18n
from gajim.common.i18n import _
from gajim.common.nec import NetworkEvent
from gajim.common.helpers import get_encryption_method
from gajim.common.const import ClientState
from gajim.common.connection_handlers import ConnectionHandlers
from gajim.common.connection_handlers_events import OurShowEvent
from gajim.common.connection_handlers_events import InformationEvent
......@@ -102,6 +103,8 @@ class CommonConnection:
self.time_to_reconnect = None
self._reconnect_timer_source = None
self._state = ClientState.DISCONNECTED
# If handlers have been registered
self.handlers_registered = False
......@@ -122,6 +125,14 @@ class CommonConnection:
self.get_config_values_or_default()
def _set_state(self, state):
log.info('State: %s', state)
self._state = state
@property
def state(self):
return self._state
@property
def is_connected(self):
return self.connected > 1
......@@ -204,7 +215,7 @@ class CommonConnection:
if show != 'invisible':
# We save it only when privacy list is accepted
self.status = msg
if show != 'offline' and self.connected < 1:
if show != 'offline' and self._state.is_disconnected:
# set old_show to requested 'show' in case we need to
# recconect before we auth to server
self.old_show = show
......@@ -223,10 +234,7 @@ class CommonConnection:
self.disconnect(reconnect=False)
return
if show != 'offline' and self.connected > 0:
# dont'try to connect, when we are in state 'connecting'
if self.connected == 1:
return
if show != 'offline' and self._state.is_connected:
if show == 'invisible':
self._change_to_invisible(msg)
return
......@@ -313,7 +321,7 @@ class Connection(CommonConnection, ConnectionHandlers):
def reconnect(self):
# Do not try to reco while we are already trying
self.time_to_reconnect = None
if not self.is_connected: # connection failed
if not self._state.is_connected: # connection failed
log.info('Reconnect')
self.connected = 1
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
......@@ -383,6 +391,7 @@ class Connection(CommonConnection, ConnectionHandlers):
log.info('Set state disconnected')
self.get_module('Ping').remove_timeout()
self.connected = 0
self._set_state(ClientState.DISCONNECTED)
self.disable_reconnect_timer()
app.interface.music_track_changed(None, None, self.name)
......@@ -612,6 +621,7 @@ class Connection(CommonConnection, ConnectionHandlers):
# create connection if it doesn't already exist
self.connected = 1
self._set_state(ClientState.CONNECTING)
h = hostname
p = 5222
......@@ -822,7 +832,7 @@ class Connection(CommonConnection, ConnectionHandlers):
def _connect_failure(self, con_type=None):
if not con_type:
# we are not retrying, and not conecting
if not self.retrycount and self.connected != 0:
if not self.retrycount and not self._state.is_disconnected:
self._disconnect()
if self._proxy:
pritxt = _('Could not connect to "%(host)s" via proxy "%(proxy)s"') %\
......@@ -1092,6 +1102,7 @@ class Connection(CommonConnection, ConnectionHandlers):
self._sm_resume_data = {}
self.connected = 2
self._set_state(ClientState.CONNECTED)
self.retrycount = 0
self.set_oldst()
self._set_send_timeouts()
......@@ -1117,6 +1128,7 @@ class Connection(CommonConnection, ConnectionHandlers):
new_jid=new_jid))
self.connected = 2
self._set_state(ClientState.CONNECTED)
self.retrycount = 0
self._discover_server()
self._set_send_timeouts()
......@@ -1141,7 +1153,7 @@ class Connection(CommonConnection, ConnectionHandlers):
self.connection.send(' ')
def send_invisible_presence(self, msg, initial=False):
if not app.account_is_connected(self.name):
if not self._state.is_connected:
return
if not self.get_module('PrivacyLists').supported:
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
......@@ -1151,7 +1163,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.is_connected and self.get_module('PrivacyLists').supported:
if self.get_module('PrivacyLists').supported:
self.get_module('Bytestream').remove_all_transfers()
self.get_module('Presence').send_presence(
typ='unavailable',
......@@ -1211,9 +1223,6 @@ class Connection(CommonConnection, ConnectionHandlers):
self.connect_and_auth()
def _discover_server(self):
if not app.account_is_connected(self.name):
return
self.get_module('Discovery').discover_server_info()
self.get_module('Discovery').discover_account_info()
self.get_module('Discovery').discover_server_items()
......@@ -1258,7 +1267,7 @@ class Connection(CommonConnection, ConnectionHandlers):
testit=testit)
def send_first_presence(self, signed=''):
if self.connected <= 1 or not self.continue_connect_info:
if not self._state.is_connected or not self.continue_connect_info:
return
show = self.continue_connect_info[0]
msg = self.continue_connect_info[1]
......@@ -1327,7 +1336,7 @@ class Connection(CommonConnection, ConnectionHandlers):
self.connection.send(stanza)
def send_message(self, message):
if not app.account_is_connected(self.name):
if not self._state.is_connected:
log.warning('Trying to send message while offline')
return
......@@ -1362,7 +1371,7 @@ class Connection(CommonConnection, ConnectionHandlers):
self.get_module('Message').log_message(message)
def send_messages(self, jids, message):
if not app.account_is_connected(self.name):
if not self._state.is_connected:
log.warning('Trying to send message while offline')
return
......@@ -1417,7 +1426,7 @@ class Connection(CommonConnection, ConnectionHandlers):
nbxmpp.features.getRegInfo(con, self._hostname)
def send_agent_status(self, agent, ptype):
if not app.account_is_connected(self.name):
if not self._state.is_connected:
return
show = helpers.get_xmpp_show(app.SHOW_LIST[self.connected])
......@@ -1428,7 +1437,7 @@ class Connection(CommonConnection, ConnectionHandlers):
caps=ptype != 'unavailable')
def unregister_account(self, callback):
if not app.account_is_connected(self.name):
if not self._state.is_connected:
return
self.removing_account = True
self.connection.get_module('Register').unregister(
......@@ -1439,7 +1448,7 @@ class Connection(CommonConnection, ConnectionHandlers):
# Account may have been disabled
return
if self.time_to_reconnect:
if not self.is_connected:
if not self._state.is_connected:
self.reconnect()
else:
self.time_to_reconnect = None
......@@ -215,6 +215,24 @@ class MUCJoinedState(Enum):
return self.name
class ClientState(IntEnum):
DISCONNECTED = 0
CONNECTING = 1
CONNECTED = 2
@property
def is_disconnected(self):
return self == ClientState.DISCONNECTED
@property
def is_connecting(self):
return self == ClientState.CONNECTING
@property
def is_connected(self):
return self == ClientState.CONNECTED
MUC_CREATION_EXAMPLES = [
(Q_('?Group chat name:Team'),
Q_('?Group chat description:Project discussion'),
......
......@@ -79,7 +79,7 @@ class LogindListener:
else:
self._inhibit_sleep(connection)
for conn in app.connections.values():
if conn.connected <= 0 and conn.time_to_reconnect:
if conn.state.is_disconnected and conn.time_to_reconnect:
conn.reconnect()
def _inhibit_sleep(self, connection):
......
......@@ -1053,8 +1053,11 @@ def update_optional_features(account=None):
[])
app.caps_hash[account_] = compute_caps_hash(disco_info, compare=False)
# re-send presence with new hash
if not app.account_is_connected(account_):
return
connected = app.connections[account_].connected
if connected > 1 and app.SHOW_LIST[connected] != 'invisible':
if app.SHOW_LIST[connected] != 'invisible':
app.connections[account_].change_status(
app.SHOW_LIST[connected], app.connections[account_].status)
......
......@@ -758,7 +758,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.is_connected:
if self.connection.connection and self.connection.state.is_connected:
self.connection.connection.send(stanza)
# TODO: Move to GUI?
reason, text = self.__reason_from_stanza(jingle)
......@@ -806,7 +806,7 @@ class JingleSession:
def __content_remove(self, content, reason=None):
assert self.state != JingleStates.ENDED
if self.connection.connection and self.connection.is_connected:
if self.connection.connection and self.connection.state.is_connected:
stanza, jingle = self.__make_jingle('content-remove', reason=reason)
self.__append_content(jingle, content)
self.connection.connection.send(stanza)
......
......@@ -198,7 +198,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.is_connected:
self._con.state.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]:
......
......@@ -39,6 +39,7 @@ from gajim.common import app
from gajim.common import modules
from gajim.common.nec import NetworkEvent
from gajim.common.i18n import _
from gajim.common.const import ClientState
from gajim.common.connection import CommonConnection
from gajim.common.zeroconf import client_zeroconf
from gajim.common.zeroconf import zeroconf
......@@ -116,7 +117,7 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
self.disconnect()
def _on_resolve_timeout(self):
if self.connected:
if self._state.is_connected:
if not self.connection.resolve_all():
self.disconnect()
return False
......@@ -214,7 +215,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.is_connected:
elif event.old_show > 1 and event.new_show == 0 and self._state.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]:
......@@ -303,6 +304,7 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
self.time_to_reconnect = None
self.connected = 0
self._set_state(ClientState.DISCONNECTED)
if self.connection:
self.connection.disconnect()
self.connection = None
......@@ -313,11 +315,12 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
def _on_disconnect(self):
self.connected = 0
self._set_state(ClientState.DISCONNECTED)
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
show='offline'))
def reannounce(self):
if self.connected:
if self._state.is_connected:
txt = {}
txt['1st'] = app.config.get_per('accounts', app.ZEROCONF_ACC_NAME,
'zeroconf_first_name')
......@@ -353,15 +356,17 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
check = self.connection.announce()
else:
self.connected = STATUS_LIST.index(show)
app.nec.push_incoming_event(NetworkEvent('signed-in', conn=self))
# stay offline when zeroconf does something wrong
if check:
self._set_state(ClientState.CONNECTED)
app.nec.push_incoming_event(NetworkEvent('signed-in', conn=self))
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
show=show))
else:
# show notification that avahi or system bus is down
self.connected = 0
self._set_state(ClientState.DISCONNECTED)
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
show='offline'))
self.status = 'offline'
......
......@@ -748,7 +748,7 @@ class ChangeStatusMessageDialog(TimeoutDialog):
class SynchroniseSelectAccountDialog:
def __init__(self, account):
# 'account' can be None if we are about to create our first one
if not account or 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 synchronise your contacts.'))
raise GajimGeneralException('You are not connected to the server')
......
......@@ -888,7 +888,7 @@ class GroupchatControl(ChatControlBase):
autopopup = app.config.get('autopopup')
autopopupaway = app.config.get('autopopupaway')
if not autopopup or (not autopopupaway and \
app.connections[self.account].connected > 2):
app.account_is_connected(self.account)):
self.roster.draw_contact(nick)
if self.parent_win:
self.parent_win.show_title()
......
......@@ -552,7 +552,6 @@ class AccountCreationWizard(EventHelper):
'gc_config': {}, 'search': {}, 'online_dialog': {},
'sub_request': {}}
app.interface.minimized_controls[self.account] = {}
app.connections[self.account].connected = 0
app.groups[self.account] = {}
app.contacts.add_account(self.account)
app.gc_connected[self.account] = {}
......
......@@ -104,7 +104,7 @@ class AccountsWindow(Gtk.ApplicationWindow):
break
def _relog(self, account):
if app.connections[account].connected == 0:
if not app.account_is_connected(account):
return
if account == app.ZEROCONF_ACC_NAME:
......@@ -528,7 +528,7 @@ class AccountRow(Gtk.ListBoxRow):
return Gdk.EVENT_PROPAGATE
if (account in app.connections and
app.connections[account].connected > 0):
not app.connections[account].state.is_disconnected):
# Connecting or connected
NewConfirmationDialog(
_('Disable Account'),
......
......@@ -642,5 +642,5 @@ class ChangePasswordSetting(DialogSetting):
activatable = False
if self.account in app.connections:
con = app.connections[self.account]
activatable = con.is_connected and con.register_supported
activatable = con.state.is_connected and con.register_supported
self.set_activatable(activatable)
......@@ -1748,7 +1748,6 @@ class Interface:
'infos': {}, 'disco': {}, 'gc_config': {}, 'search': {},
'online_dialog': {}, 'sub_request': {}}
self.minimized_controls[account] = {}
app.connections[account].connected = 0
app.groups[account] = {}
app.contacts.add_account(account)
app.gc_connected[account] = {}
......@@ -1992,12 +1991,12 @@ class Interface:
log.debug('NetworkMonitor state change: %s', connected)
if connected:
for connection in app.connections.values():
if connection.connected <= 0 and connection.time_to_reconnect:
if connection.state.is_disconnected and connection.time_to_reconnect:
log.info('Connect %s', connection.name)
GLib.timeout_add_seconds(2, connection.reconnect)
else:
for connection in app.connections.values():
if connection.is_connected:
if connection.state.is_connected:
log.info('Disconnect %s', connection.name)
connection.disconnect(immediately=True)
......
......@@ -2523,7 +2523,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.is_connected:
obj.conn.state.is_connected:
GLib.timeout_add_seconds(5, self.remove_to_be_removed,
jid, account)
......@@ -3450,7 +3450,7 @@ class RosterWindow:
# show and status
if not global_sync_connected_accounts > 0 or \
app.connections[account].connected > 0:
app.account_is_connected(account):
self.send_status(account, status, message)
self.send_pep(account, pep_dict)
self.update_status_combobox()
......@@ -4786,7 +4786,7 @@ class RosterWindow:
item = Gtk.MenuItem.new_with_mnemonic(uf_show)
sub_menu.append(item)
con = app.connections[account]
if show == 'invisible' and con.is_connected and \
if show == 'invisible' and con.state.is_connected and \
not con.get_module('PrivacyLists').supported:
item.set_sensitive(False)
else:
......
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