Commit 519d8644 authored by Philipp Hörist's avatar Philipp Hörist

Refactor conditions to determine connected state

GUI code must not be aware of what integers map to connection states
parent 24852277
......@@ -739,7 +739,7 @@ def _on_message_textview_key_press_event(self, widget, event):
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:
......
......@@ -128,7 +128,7 @@ def status(self, status, message):
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 @@ def away(self, message):
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 @@ def online(self, message):
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)
......
......@@ -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'
......
......@@ -127,6 +127,10 @@ def __init__(self, name):
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 @@ def check_jid(self, jid):
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 @@ def get_own_jid(self, warn=False):
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 @@ def send_invisible_presence(self, msg, initial=False):
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 @@ def _reconnect_alarm(self):
# 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
......@@ -766,7 +766,7 @@ def _JingleFileTransfer__session_info(self, payload):
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 @@ def __content_modify(self):
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)
......
......@@ -135,7 +135,7 @@ def send_file_approval(self, file_props):
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 @@ def send_file_rejection(self, file_props, code='403', typ=None):
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 @@ def send_success_connect_reply(self, streamhost):
"""
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 @@ def _send_socks5_info(self, file_props):
"""
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 @@ def _connect_error(self, sid, error, error_type, msg=None):
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 @@ def _proxy_auth_ok(self, proxy):
"""
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')
......
......@@ -41,7 +41,7 @@ def request_entity_time(self, jid, resource):
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:
......
......@@ -189,7 +189,7 @@ def _update_contact(self, event, properties):
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]:
......
......@@ -224,7 +224,7 @@ def _update_contact(self, event):
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]:
......
......@@ -783,7 +783,7 @@ def on_ok_button_clicked(self, widget):
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
......
......@@ -488,7 +488,7 @@ def __init__(self, account, jid='', node='', address_entry=False,
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')
......
......@@ -187,8 +187,7 @@ def _on_join_clicked(self, *args):
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
......
......@@ -316,7 +316,7 @@ def on_ok_button_clicked(self, widget):
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 '
......
......@@ -651,5 +651,5 @@ def update_activatable(self, name, value):
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)
......@@ -269,7 +269,7 @@ def update_char_counter(self, widget):
_('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)
......
......@@ -148,7 +148,7 @@ def on_row_activated(self, listbox, row):
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'),
......
......@@ -933,9 +933,8 @@ def handle_event_signed_in(self, obj):
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 @@ def join_gc_room(self, account, room_jid, nick, password, minimize=False,
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 @@ def network_status_changed(self, monitor, connected):
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)
......
......@@ -437,7 +437,7 @@ def _get_account_and_contact(self, account, jid):
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 @@ def _get_account_and_contact(self, account, jid):
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 @@ def _get_account_for_groupchat(self, account, room_jid):
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 @@ def open_chat(self, jid, account, message):
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 @@ def prefs_put(self, key):
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:
......
......@@ -2063,7 +2063,7 @@ def send_status(self, account, status, txt, auto=False, to=None):
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 @@ def send_status_continue(self, account, status, txt, auto, to):
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 @@ def on_quit_request(self, widget=None):
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 @@ def on_continue3(message, pep_dict):
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 @@ def _nec_presence_received(self, obj):
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 @@ def on_rename(self, widget, row_type, jid, account):
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 @@ def on_roster_treeview_button_press_event(self, widget, event):
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 @@ def drag_data_received_data(self, treeview, context, x, y, selection, info,
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 @@ def build_account_menu(self, account):
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 @@ def build_account_menu(self, account):
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 @@ def build_account_menu(self, account):
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 @@ def build_account_menu(self, account):
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 @@ def make_group_menu(self, event, titer):
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 @@ def make_group_menu(self, event, titer):
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 @@ def make_multiple_contact_menu(self, event, iters):
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
......
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