Commit bfa50806 authored by Philipp Hörist's avatar Philipp Hörist

Refactor connection status

- Remove connected variable
- Add status property instead
parent 4df48377
Pipeline #4970 passed with stages
in 3 minutes and 24 seconds
......@@ -437,12 +437,14 @@ class ChatControlBase(ChatCommandProcessor, CommandTools, EventHelper):
def _nec_our_status(self, obj):
if self.account != obj.conn.name:
return
if obj.show == 'connecting':
return
if obj.show == 'offline':
self.got_disconnected()
else:
# Other code rejoins all GCs, so we don't do it here
if not self._type.is_groupchat:
self.got_connected()
self.got_connected()
if self.parent_win:
self.parent_win.redraw_tab(self)
......
This diff is collapsed.
......@@ -53,6 +53,9 @@ class PresenceReceivedEvent(nec.NetworkIncomingEvent):
class OurShowEvent(nec.NetworkIncomingEvent):
name = 'our-show'
def init(self):
self.reconnect = False
class MessageSentEvent(nec.NetworkIncomingEvent):
name = 'message-sent'
......@@ -286,7 +289,7 @@ class NotificationEvent(nec.NetworkIncomingEvent):
if app.config.get('autopopupaway'):
# always show notification
self.do_popup = True
if app.connections[self.conn.name].connected in (2, 3):
if app.connections[self.conn.name].status in ('online', 'chat'):
# we're online or chat
self.do_popup = True
......@@ -352,7 +355,7 @@ class NotificationEvent(nec.NetworkIncomingEvent):
# always show notification
self.do_popup = True
elif app.connections[self.conn.name].connected in (2, 3):
elif app.connections[self.conn.name].status in ('online', 'chat'):
# we're online or chat
self.do_popup = True
......
......@@ -217,13 +217,18 @@ class MUCJoinedState(Enum):
class ClientState(IntEnum):
DISCONNECTED = 0
CONNECTING = 1
CONNECTED = 2
RECONNECT_SCHEDULED = 1
CONNECTING = 2
CONNECTED = 3
@property
def is_disconnected(self):
return self == ClientState.DISCONNECTED
@property
def is_reconnect_scheduled(self):
return self == ClientState.RECONNECT_SCHEDULED
@property
def is_connecting(self):
return self == ClientState.CONNECTING
......
......@@ -68,11 +68,11 @@ class LogindListener:
for name, conn in app.connections.items():
if app.account_is_connected(name):
conn.old_show = app.SHOW_LIST[conn.connected]
st = conn.status
st = conn.status_message
conn.change_status('offline',
_('Machine is going to sleep'))
conn.status = st
# TODO: Make this nicer
conn._status_message = st # pylint: disable=protected-access
conn.time_to_reconnect = 5
self._disinhibit_sleep()
......
......@@ -682,28 +682,42 @@ def play_sound_file(path_to_soundfile):
except GLib.Error as error:
log.error('Could not play sound: %s', error.message)
def get_connection_status(account):
con = app.connections[account]
if con.state.is_reconnect_scheduled:
return 'error'
if con.state.is_connecting:
return 'connecting'
if con.state.is_disconnected:
return 'offline'
return con.status
def get_global_show():
maxi = 0
for account in app.connections:
if not app.config.get_per('accounts', account,
'sync_with_global_status'):
'sync_with_global_status'):
continue
connected = app.connections[account].connected
if connected > maxi:
maxi = connected
status = get_connection_status(account)
index = app.SHOW_LIST.index(status)
if index > maxi:
maxi = index
return app.SHOW_LIST[maxi]
def get_global_status():
def get_global_status_message():
maxi = 0
for account in app.connections:
if not app.config.get_per('accounts', account,
'sync_with_global_status'):
'sync_with_global_status'):
continue
connected = app.connections[account].connected
if connected > maxi:
maxi = connected
status = app.connections[account].status
return status
status = app.connections[account].status
index = app.SHOW_LIST.index(status)
if index > maxi:
maxi = index
status_message = app.connections[account].status_message
return status_message
def statuses_unified():
......@@ -713,11 +727,11 @@ def statuses_unified():
reference = None
for account in app.connections:
if not app.config.get_per('accounts', account,
'sync_with_global_status'):
'sync_with_global_status'):
continue
if reference is None:
reference = app.connections[account].connected
elif reference != app.connections[account].connected:
reference = app.connections[account].status
elif reference != app.connections[account].status:
return False
return True
......@@ -818,7 +832,7 @@ is_first_message=True):
return False
if app.config.get('autopopupaway'): # always show notification
return True
if app.connections[account].connected in (2, 3): # we're online or chat
if app.connections[account].status in ('online', 'chat'): # we're online or chat
return True
return False
......@@ -829,14 +843,14 @@ def allow_popup_window(account):
autopopup = app.config.get('autopopup')
autopopupaway = app.config.get('autopopupaway')
if autopopup and (autopopupaway or \
app.connections[account].connected in (2, 3)): # we're online or chat
app.connections[account].status in ('online', 'chat')): # we're online or chat
return True
return False
def allow_sound_notification(account, sound_event):
if app.config.get('sounddnd') or app.connections[account].connected != \
app.SHOW_LIST.index('dnd') and app.config.get_per('soundevents',
sound_event, 'enabled'):
if (app.config.get('sounddnd') or
app.connections[account].status != 'dnd' and
app.config.get_per('soundevents', sound_event, 'enabled')):
return True
return False
......@@ -984,11 +998,8 @@ def get_accounts_info():
accounts = []
accounts_list = sorted(app.contacts.get_accounts())
for account in accounts_list:
status_idx = app.connections[account].connected
# uncomment the following to hide offline accounts
# if status_idx == 0: continue
status = app.SHOW_LIST[status_idx]
message = app.connections[account].status
status = app.connections[account].status
message = app.connections[account].status_message
single_line = get_uf_show(status)
if message is None:
message = ''
......@@ -1007,8 +1018,7 @@ def get_accounts_info():
def get_current_show(account):
if account not in app.connections:
return 'offline'
status = app.connections[account].connected
return app.SHOW_LIST[status]
return app.connections[account].status
def update_optional_features(account=None):
if account is not None:
......@@ -1051,9 +1061,9 @@ def update_optional_features(account=None):
if not app.account_is_connected(account_):
return
connected = app.connections[account_].connected
status = app.connections[account_].status
app.connections[account_].change_status(
app.SHOW_LIST[connected], app.connections[account_].status)
status, app.connections[account_].status_message)
def jid_is_blocked(account, jid):
con = app.connections[account]
......
......@@ -179,12 +179,10 @@ class MUC(BaseModule):
self._join(muc_data)
def _join(self, muc_data):
show = helpers.get_xmpp_show(app.SHOW_LIST[self._con.connected])
presence = self._con.get_module('Presence').get_presence(
muc_data.occupant_jid,
show=show,
status=self._con.status)
show=self._con.status,
status=self._con.status_message)
muc_x = presence.setTag(nbxmpp.NS_MUC + ' x')
self._add_history_query(muc_x, str(muc_data.jid))
......@@ -204,12 +202,10 @@ class MUC(BaseModule):
return True
def _create(self, muc_data):
show = helpers.get_xmpp_show(app.SHOW_LIST[self._con.connected])
presence = self._con.get_module('Presence').get_presence(
muc_data.occupant_jid,
show=show,
status=self._con.status)
show=self._con.status,
status=self._con.status_message)
presence.setTag(nbxmpp.NS_MUC + ' x')
......@@ -313,14 +309,13 @@ class MUC(BaseModule):
self._send_presence(muc_data, auto)
def _send_presence(self, muc_data, auto):
show = app.SHOW_LIST[self._con.connected]
if show == 'offline':
if self._con.status == 'offline':
# FIXME: Check if this
return
status = self._con.status
message = self._con.status_message
xmpp_show = helpers.get_xmpp_show(show)
xmpp_show = helpers.get_xmpp_show(self._con.status)
idle_time = None
if auto and app.is_installed('IDLE') and app.config.get('autoaway'):
......@@ -328,22 +323,22 @@ class MUC(BaseModule):
idle_time = time.strftime('%Y-%m-%dT%H:%M:%SZ',
time.gmtime(time.time() - idle_sec))
self._log.info('Send presence: %s, show: %s, status: %s, idle_time: %s',
muc_data.occupant_jid, xmpp_show, status, idle_time)
self._log.info('Send presence: %s, show: %s, '
'message: %s, idle_time: %s',
muc_data.occupant_jid, xmpp_show, message, idle_time)
self._con.get_module('Presence').send_presence(
muc_data.occupant_jid,
show=xmpp_show,
status=status,
status=self._con.status,
caps=True,
idle_time=idle_time)
def change_nick(self, room_jid, new_nick):
show = helpers.get_xmpp_show(app.SHOW_LIST[self._con.connected])
self._con.get_module('Presence').send_presence(
'%s/%s' % (room_jid, new_nick),
show=show,
status=self._con.status)
show=self._con.status,
status=self._con.status_message)
def _add_history_query(self, muc_x, room_jid):
disco_info = app.logger.get_last_disco_info(room_jid)
......
......@@ -343,13 +343,13 @@ class PrivacyLists(BaseModule):
else:
self.set_privacy_list(self.default_list, new_blocked_list)
if not app.interface.roster.regroup:
show = app.SHOW_LIST[self._con.connected]
show = self._con.status
else: # accounts merged
show = helpers.get_global_show()
for contact in contact_list:
self._con.get_module('Presence').send_presence(
contact.jid, show=show, status=self._con.status)
contact.jid, show=show, status=self._con.status_message)
self._presence_probe(contact.jid)
def block_group(self, group, contact_list, _message):
......@@ -404,13 +404,13 @@ class PrivacyLists(BaseModule):
else:
self.set_privacy_list(self.default_list, new_blocked_list)
if not app.interface.roster.regroup:
show = app.SHOW_LIST[self._con.connected]
show = self._con.status
else: # accounts merged
show = helpers.get_global_show()
for contact in contact_list:
self._con.get_module('Presence').send_presence(
contact.jid, show=show, status=self._con.status)
contact.jid, show=show, status=self._con.status_message)
def _presence_probe(self, jid):
self._log.info('Presence probe: %s', jid)
......
......@@ -21,7 +21,6 @@ from nbxmpp.structs import StanzaHandler
from nbxmpp.const import AvatarState
from gajim.common import app
from gajim.common import helpers
from gajim.common import configpaths
from gajim.common.const import RequestAvatar
from gajim.common.modules.base import BaseModule
......@@ -221,12 +220,10 @@ class VCardAvatars(BaseModule):
self._log.debug('Avatar already advertised')
return
show = helpers.get_xmpp_show(app.SHOW_LIST[self._con.connected])
self._con.get_module('Presence').send_presence(
priority=self._con.priority,
show=show,
status=self._con.status)
show=self._con.status,
status=self._con.status_message)
self.avatar_advertised = True
......
......@@ -35,8 +35,6 @@ from gajim.common.modules.misc import parse_xhtml
log = logging.getLogger('gajim.c.z.connection_handlers_zeroconf')
STATUS_LIST = ['offline', 'connecting', 'online', 'chat', 'away', 'xa', 'dnd']
class ConnectionHandlersZeroconf(connection_handlers.ConnectionHandlersBase):
def __init__(self):
......
......@@ -44,7 +44,6 @@ from gajim.common.connection import CommonConnection
from gajim.common.zeroconf import client_zeroconf
from gajim.common.zeroconf import zeroconf
from gajim.common.zeroconf.connection_handlers_zeroconf import ConnectionHandlersZeroconf
from gajim.common.zeroconf.connection_handlers_zeroconf import STATUS_LIST
from gajim.common.connection_handlers_events import OurShowEvent
from gajim.common.connection_handlers_events import InformationEvent
from gajim.common.connection_handlers_events import ConnectionLostEvent
......@@ -111,7 +110,7 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
log.debug('reconnect')
self.disconnect()
self.change_status(self.old_show, self.status)
self.change_status(self._status, self._status_message)
def disable_account(self):
self.disconnect()
......@@ -246,7 +245,7 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
if not zeroconf.test_zeroconf():
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
show='offline'))
self.status = 'offline'
self._status = 'offline'
app.nec.push_incoming_event(ConnectionLostEvent(None,
conn=self, title=_('Could not connect to "%s"') % self.name,
msg=_('Please check if Avahi or Bonjour is installed.')))
......@@ -256,7 +255,7 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
if not result:
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
show='offline'))
self.status = 'offline'
self._status = 'offline'
if result is False:
app.nec.push_incoming_event(ConnectionLostEvent(None,
conn=self, title=_('Could not start local service'),
......@@ -282,7 +281,7 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
avatar_sha=None))
self._on_presence(jid)
self.connected = STATUS_LIST.index(show)
self._status = show
# refresh all contacts data every five seconds
self.call_resolve_timeout = True
......@@ -292,18 +291,10 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
def disconnect(self, reconnect=True, immediately=True):
log.info('Start disconnecting zeroconf')
if reconnect:
if app.account_is_connected(self.name):
# we cannot change our status to offline or connecting
# after we auth to server
self.old_show = STATUS_LIST[self.connected]
# random number to show we wait network manager to send
# us a reconenct
self.time_to_reconnect = 5
else:
self.time_to_reconnect = None
self.connected = 0
self._set_state(ClientState.DISCONNECTED)
if self.connection:
self.connection.disconnect()
......@@ -314,7 +305,6 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
show='offline'))
def _on_disconnect(self):
self.connected = 0
self._set_state(ClientState.DISCONNECTED)
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
show='offline'))
......@@ -340,7 +330,7 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
self.port = port
last_msg = self.connection.last_msg
self.disconnect()
if not self.connect(self.status, last_msg):
if not self.connect(self._status, last_msg):
return
self.connection.announce()
else:
......@@ -362,11 +352,10 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
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'
self._status = 'offline'
app.nec.push_incoming_event(ConnectionLostEvent(None, conn=self,
title=_('Could not change status of account "%s"') % self.name,
msg=_('Please check if avahi-daemon is running.')))
......@@ -379,7 +368,7 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
# show notification that avahi or system bus is down
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
show='offline'))
self.status = 'offline'
self._status = 'offline'
app.nec.push_incoming_event(ConnectionLostEvent(None, conn=self,
title=_('Could not change status of account "%s"') % self.name,
msg=_('Please check if avahi-daemon is running.')))
......
......@@ -1127,6 +1127,17 @@ class GroupchatControl(ChatControlBase):
msg_log_id=event.msg_log_id,
displaymarking=event.displaymarking)
def _nec_our_status(self, event):
if self.account != event.conn.name:
return
if (event.show == 'offline' and
not event.conn.state.is_reconnect_scheduled):
self.got_disconnected()
if self.parent_win:
self.parent_win.redraw_tab(self)
def _nec_ping(self, event):
if self.contact.jid != event.contact.room_jid:
return
......
......@@ -122,8 +122,8 @@ class AccountsWindow(Gtk.ApplicationWindow):
account, show_before, status_before)
def relog(account):
show_before = app.SHOW_LIST[app.connections[account].connected]
status_before = app.connections[account].status
show_before = app.connections[account].status
status_before = app.connections[account].status_message
app.interface.roster.send_status(
account, 'offline', _('Be right back.'))
GLib.timeout_add(500, login, account, show_before, status_before)
......@@ -781,8 +781,8 @@ class PriorityDialog(SettingsDialog):
# Update priority
if self.account not in app.connections:
return
show = app.SHOW_LIST[app.connections[self.account].connected]
status = app.connections[self.account].status
show = app.connections[self.account].status
status = app.connections[self.account].status_message
app.connections[self.account].change_status(show, status)
......
......@@ -381,7 +381,7 @@ class StatusIcon:
acct,
'sync_with_global_status'):
continue
show = app.SHOW_LIST[app.connections[acct].connected]
show = app.connections[acct].status
app.interface.roster.send_status(acct, show, message)
app.interface.roster.send_pep(acct, pep_dict)
dlg = dialogs.ChangeStatusMessageDialog(on_response, status)
......
......@@ -325,8 +325,8 @@ class RosterTooltip(StatusTable):
jid=jid,
account=account,
name=account_name,
show=connection.get_status(),
status=connection.status,
show=connection.status,
status=connection.status_message,
resource=connection.get_own_jid().getResource(),
priority=connection.priority)
......
......@@ -806,7 +806,7 @@ class Interface:
if (app.config.get('notify_on_file_complete') and
(app.config.get('autopopupaway') or
app.connections[account].connected in (2, 3))):
app.connections[account].status in ('online', 'chat'))):
# We want to be notified and we are online/chat or we don't mind
# to be bugged when away/na/busy
app.notification.popup(
......@@ -829,15 +829,14 @@ class Interface:
app.logger.insert_jid(obj.conn.get_own_jid().getStripped())
account = obj.conn.name
app.block_signed_in_notifications[account] = True
connected = obj.conn.connected
pep_supported = obj.conn.get_module('PEP').supported
if not idle.Monitor.is_unknown() and connected in (2, 3):
if not idle.Monitor.is_unknown() and obj.conn.status in ('online', 'chat'):
# we go online or free for chat, so we activate auto status
app.sleeper_state[account] = 'online'
elif not ((idle.Monitor.is_away() and connected == 4) or \
(idle.Monitor.is_xa() and connected == 5)):
elif not ((idle.Monitor.is_away() and obj.conn.status == 'away') or \
(idle.Monitor.is_xa() and obj.conn.status == 'xa')):
# If we are autoaway/xa and come back after a disconnection, do
# nothing
# Else disable autoaway
......@@ -891,10 +890,8 @@ class Interface:
def handle_event_zc_name_conflict(self, obj):
def _on_ok(new_name):
app.config.set_per('accounts', obj.conn.name, 'name', new_name)
show = obj.conn.old_show
status = obj.conn.status
obj.conn.username = new_name
obj.conn.change_status(show, status)
obj.conn.change_status(obj.conn.status, obj.conn.status_message)
def _on_cancel(*args):
obj.conn.change_status('offline', '')
......@@ -916,11 +913,11 @@ class Interface:
# First we go offline, but we don't overwrite status message
account = obj.conn.name
conn = obj.conn
self.roster.send_status(account, 'offline', conn.status)
self.roster.send_status(account, 'offline', conn.status_message)
def _set_resource(new_resource):
app.config.set_per('accounts', account, 'resource', new_resource)
self.roster.send_status(account, conn.old_show, conn.status)
self.roster.send_status(account, conn.status, conn.status_message)
proposed_resource = conn.server_resource
if proposed_resource.startswith('gajim.'):
......@@ -1675,8 +1672,8 @@ class Interface:
app.sleeper_state[account] = 'online'
if app.sleeper_state[account] == 'idle':
# we go to the previous state
connected = app.connections[account].connected
self.roster.send_status(account, app.SHOW_LIST[connected],
status = app.connections[account].status
self.roster.send_status(account, status,
app.status_before_autoaway[account])
app.status_before_autoaway[account] = ''
app.sleeper_state[account] = 'off'
......@@ -1684,7 +1681,7 @@ class Interface:
if app.sleeper_state[account] == 'online':
# we save out online status
app.status_before_autoaway[account] = \
app.connections[account].status
app.connections[account].status_message
# we go away (no auto status) [we pass True to auto param]
auto_message = app.config.get('autoaway_message')
if not auto_message:
......@@ -1703,8 +1700,8 @@ class Interface:
# we save out online status
app.status_before_autoaway[account] = \
app.connections[account].status
connected = app.connections[account].connected
self.roster.send_status(account, app.SHOW_LIST[connected],
status = app.connections[account].status
self.roster.send_status(account, status,
app.status_before_autoaway[account], auto=True)
app.sleeper_state[account] = 'idle'
elif idle.Monitor.is_xa() and \
......@@ -1837,7 +1834,7 @@ class Interface:
app.config.get_per('accounts', a, 'last_status_msg')))
continue
show = app.config.get_per('accounts', a, 'autoconnect_as')
if show not in app.SHOW_LIST:
if show not in ['online', 'chat', 'away', 'xa', 'dnd']:
continue
if show not in shows:
shows[show] = [a]
......
......@@ -408,8 +408,7 @@ class GajimRemote(Server):
# If user did not ask for account, returns the global status
return helpers.get_global_show()
# return show for the given account
index = app.connections[account].connected
return app.SHOW_LIST[index]
return app.connections[account].status
def get_status_message(self, account):
"""
......@@ -417,7 +416,7 @@ class GajimRemote(Server):
"""
if not account:
# If user did not ask for account, returns the global status
return str(helpers.get_global_status())
return str(helpers.get_global_status_message())
# return show for the given account
status = app.connections[account].status
return status
......@@ -638,7 +637,7 @@ class GajimRemote(Server):
if not status:
if account not in app.connections:
return False
status = app.SHOW_LIST[app.connections[account].connected]
status = app.connections[account].status
GLib.idle_add(app.interface.roster.send_status, account, status,
message)
else:
......@@ -652,7 +651,7 @@ class GajimRemote(Server):
else:
if acc not in app.connections:
continue
status_ = app.SHOW_LIST[app.connections[acc].connected]
status_ = app.connections[acc].status
GLib.idle_add(app.interface.roster.send_status, acc, status_,
message)
return False
......@@ -664,8 +663,8 @@ class GajimRemote(Server):
"""
if account:
app.config.set_per('accounts', account, 'priority', prio)
show = app.SHOW_LIST[app.connections[account].connected]
status = app.connections[account].status
show = app.connections[account].status
status = app.connections[account].status_message
GLib.idle_add(app.connections[account].change_status, show, status)
else:
# account not specified, so change prio of all accounts
......@@ -676,8 +675,8 @@ class GajimRemote(Server):
'sync_with_global_status'):
continue
app.config.set_per('accounts', acc, 'priority', prio)
show = app.SHOW_LIST[app.connections[acc].connected]
status = app.connections[acc].status
show = app.connections[acc].status
status = app.connections[acc].status_message
GLib.idle_add(app.connections[acc].change_status, show, status)
def show_next_pending_event(self):
......@@ -709,11 +708,10 @@ class GajimRemote(Server):
if account in app.connections:
# account is valid
con = app.connections[account]
index = con.connected
result['status'] = app.SHOW_LIST[index]
result['status'] = con.status
result['name'] = con.name
result['jid'] = app.get_jid_from_account(con.name)
result['message'] = con.status
result['message'] = con.status_message
result['priority'] = str(con.priority)
result['resource'] = app.config.get_per('accounts', con.name,
'resource')
......
......@@ -291,7 +291,7 @@ class RosterWindow:
None, None, None, True] + [None] * self.nb_ext_renderers)
self._iters['MERGED']['account'] = it
else:
show = app.SHOW_LIST[app.connections[account].connected]
show = app.connections[account].status
our_jid = app.get_jid_from_account(account)
tls_pixbuf = None
......@@ -2247,7 +2247,7 @@ class RosterWindow:
def update_status_combobox(self):
# table to change index in connection.connected to index in combobox
table = {'offline':8, 'connecting':8, 'online':0, 'chat':1, 'away':2,