Skip to content
Snippets Groups Projects
helpers.py 38.9 KiB
Newer Older
	if type_ and (not gajim.config.get(type_) or not is_first_message):
nkour's avatar
nkour committed
	if gajim.config.get('autopopupaway'): # always show notification
		return True
	if gajim.connections[account].connected in (2, 3): # we're online or chat
		return True
def allow_popup_window(account, advanced_notif_num = None):
	'''is it allowed to popup windows?'''
	if advanced_notif_num is not None:
		popup = gajim.config.get_per('notifications', str(advanced_notif_num),
			'auto_open')
		if popup == 'yes':
			return True
		if popup == 'no':
			return False
nkour's avatar
nkour committed
	autopopup = gajim.config.get('autopopup')
	autopopupaway = gajim.config.get('autopopupaway')
nkour's avatar
nkour committed
	if autopopup and (autopopupaway or \
	gajim.connections[account].connected in (2, 3)): # we're online or chat

def allow_sound_notification(sound_event, advanced_notif_num = None):
	if advanced_notif_num is not None:
		sound = gajim.config.get_per('notifications', str(advanced_notif_num),
			'sound')
		if sound == 'yes':
			return True
		if sound == 'no':
			return False
	if gajim.config.get_per('soundevents', sound_event, 'enabled'):
		return True
	return False

def get_chat_control(account, contact):
	full_jid_with_resource = contact.jid
	if contact.resource:
		full_jid_with_resource += '/' + contact.resource
	highest_contact = gajim.contacts.get_contact_with_highest_priority(
		account, contact.jid)
	# Look for a chat control that has the given resource, or default to
	# one without resource
	ctrl = gajim.interface.msg_win_mgr.get_control(full_jid_with_resource,
	if ctrl:
		return ctrl
	elif highest_contact and highest_contact.resource and \
	contact.resource != highest_contact.resource:
	else:
		# unknown contact or offline message
		return gajim.interface.msg_win_mgr.get_control(contact.jid, account)
def get_notification_icon_tooltip_dict():
	'''returns a dict of the form {acct: {'show': show, 'message': message,
	'event_lines': [list of text lines to show in tooltip]}'''
	# How many events must there be before they're shown summarized, not per-user
	max_ungrouped_events = 10

	accounts = get_accounts_info()

	# Gather events. (With accounts, when there are more.)
	for account in accounts:
		account_name = account['name']
		account['event_lines'] = []
		# Gather events per-account
		pending_events = gajim.events.get_events(account = account_name)
		messages, non_messages, total_messages, total_non_messages = {}, {}, 0, 0
		for jid in pending_events:
			for event in pending_events[jid]:
				if event.type_.count('file') > 0:
					# This is a non-messagee event.
					messages[jid] = non_messages.get(jid, 0) + 1
					total_non_messages = total_non_messages + 1
				else:
					# This is a message.
					messages[jid] = messages.get(jid, 0) + 1
					total_messages = total_messages + 1
		# Display unread messages numbers, if any
		if total_messages > 0:
			if total_messages > max_ungrouped_events:
				text = ngettext(
					'%d message pending',
					'%d messages pending',
					total_messages, total_messages, total_messages)
				account['event_lines'].append(text)
			else:
				for jid in messages.keys():
					text = ngettext(
						'%d message pending',
						'%d messages pending',
						messages[jid], messages[jid], messages[jid])
					contact = gajim.contacts.get_first_contact_from_jid(
						account['name'], jid)
					if jid in gajim.gc_connected[account['name']]:
						text += _(' from room %s') % (jid)
					elif contact:
						name = contact.get_shown_name()
						text += _(' from user %s') % (name)
					else:
						text += _(' from %s') % (jid)
					account['event_lines'].append(text)
		# Display unseen events numbers, if any
		if total_non_messages > 0:
			if total_non_messages > max_ungrouped_events:
				text = ngettext(
					'%d event pending',
					'%d events pending',
					total_non_messages, total_non_messages, total_non_messages)
				accounts[account]['event_lines'].append(text)
			else:
				for jid in non_messages.keys():
					text = ngettext(
						'%d event pending',
						'%d events pending',
						non_messages[jid], non_messages[jid], non_messages[jid])
					text += _(' from user %s') % (jid)
					accounts[account]['event_lines'].append(text)

	return accounts

def get_notification_icon_tooltip_text():
	text = None
	# How many events must there be before they're shown summarized, not per-user
	# max_ungrouped_events = 10
	# Character which should be used to indent in the tooltip.
	indent_with = ' '
	accounts = get_notification_icon_tooltip_dict()
	if len(accounts) == 0:
		# No configured account
		return _('Gajim')

	# at least one account present

	# Is there more that one account?
	if len(accounts) == 1:
		show_more_accounts = False
		show_more_accounts = True

	# If there is only one account, its status is shown on the first line.
	if show_more_accounts:
		text = _('Gajim')
		text = _('Gajim - %s') % (get_account_status(accounts[0]))

	# Gather and display events. (With accounts, when there are more.)
	for account in accounts:
		account_name = account['name']
		# Set account status, if not set above
		if (show_more_accounts):
			message = '\n' + indent_with + ' %s - %s'
			text += message % (account_name, get_account_status(account))
			# Account list shown, messages need to be indented more
			indent_how = 2
		else:
			# If no account list is shown, messages could have default indenting.
			indent_how = 1
		for line in account['event_lines']:
			text += '\n' + indent_with * indent_how + ' '
			text += line
	return text

def get_accounts_info():
	'''helper for notification icon tooltip'''
	accounts = []
	accounts_list = sorted(gajim.contacts.get_accounts())
	for account in accounts_list:
		status_idx = gajim.connections[account].connected
		# uncomment the following to hide offline accounts
		# if status_idx == 0: continue
		status = gajim.SHOW_LIST[status_idx]
		message = gajim.connections[account].status
		single_line = get_uf_show(status)
		if message is None:
			message = ''
		else:
			message = message.strip()
		if message != '':
			single_line += ': ' + message
		accounts.append({'name': account, 'status_line': single_line,
				'show': status, 'message': message})
	return accounts

def get_iconset_path(iconset):
	if os.path.isdir(os.path.join(gajim.DATA_DIR, 'iconsets', iconset)):
		return os.path.join(gajim.DATA_DIR, 'iconsets', iconset)
	elif os.path.isdir(os.path.join(gajim.MY_ICONSETS_PATH, iconset)):
		return os.path.join(gajim.MY_ICONSETS_PATH, iconset)
js's avatar
js committed
def get_mood_iconset_path(iconset):
	if os.path.isdir(os.path.join(gajim.DATA_DIR, 'moods', iconset)):
		return os.path.join(gajim.DATA_DIR, 'moods', iconset)
	elif os.path.isdir(os.path.join(gajim.MY_MOOD_ICONSETS_PATH, iconset)):
		return os.path.join(gajim.MY_MOOD_ICONSETS_PATH, iconset)

js's avatar
js committed
def get_activity_iconset_path(iconset):
	if os.path.isdir(os.path.join(gajim.DATA_DIR, 'activities', iconset)):
		return os.path.join(gajim.DATA_DIR, 'activities', iconset)
	elif os.path.isdir(os.path.join(gajim.MY_ACTIVITY_ICONSETS_PATH,
	iconset)):
		return os.path.join(gajim.MY_ACTIVITY_ICONSETS_PATH, iconset)

def get_transport_path(transport):
	if os.path.isdir(os.path.join(gajim.DATA_DIR, 'iconsets', 'transports',
	transport)):
		return os.path.join(gajim.DATA_DIR, 'iconsets', 'transports', transport)
	elif os.path.isdir(os.path.join(gajim.MY_ICONSETS_PATH, 'transports',
	transport)):
		return os.path.join(gajim.MY_ICONSETS_PATH, 'transports', transport)
	# No transport folder found, use default jabber one
	return get_iconset_path(gajim.config.get('iconset'))
steve-e's avatar
steve-e committed

def prepare_and_validate_gpg_keyID(account, jid, keyID):
	'''Returns an eight char long keyID that can be used with for GPG encryption with this contact.
	If the given keyID is None, return UNKNOWN; if the key does not match the assigned key
	XXXXXXXXMISMATCH is returned. If the key is trusted and not yet assigned, assign it'''
	if gajim.connections[account].USE_GPG:
steve-e's avatar
steve-e committed
			keyID = keyID[8:]
steve-e's avatar
steve-e committed
		attached_keys = gajim.config.get_per('accounts', account,
			'attached_gpg_keys').split()
steve-e's avatar
steve-e committed
		if jid in attached_keys and keyID:
			attachedkeyID = attached_keys[attached_keys.index(jid) + 1]
			if attachedkeyID != keyID:
				# Mismatch! Another gpg key was expected
				keyID += 'MISMATCH'
		elif jid in attached_keys:
			# An unsigned presence, just use the assigned key
			keyID = attached_keys[attached_keys.index(jid) + 1]
steve-e's avatar
steve-e committed
			public_keys = gajim.connections[account].ask_gpg_keys()
			# Assign the corresponding key, if we have it in our keyring
steve-e's avatar
steve-e committed
				for u in gajim.contacts.get_contacts(account, jid):
					u.keyID = keyID
				keys_str = gajim.config.get_per('accounts', account, 'attached_gpg_keys')
				keys_str += jid + ' ' + keyID + ' '
				gajim.config.set_per('accounts', account, 'attached_gpg_keys', keys_str)
		elif keyID is None:
			keyID = 'UNKNOWN'
	return keyID

def update_optional_features(account = None):
	if account:
		accounts = [account]
	else:
		accounts = [a for a in gajim.connections]
	for a in accounts:
		gajim.gajim_optional_features[a] = []
		if gajim.config.get_per('accounts', a, 'subscribe_mood'):
			gajim.gajim_optional_features[a].append(xmpp.NS_MOOD + '+notify')
		if gajim.config.get_per('accounts', a, 'subscribe_activity'):
			gajim.gajim_optional_features[a].append(xmpp.NS_ACTIVITY + '+notify')
		if gajim.config.get_per('accounts', a, 'publish_tune'):
			gajim.gajim_optional_features[a].append(xmpp.NS_TUNE)
		if gajim.config.get_per('accounts', a, 'subscribe_tune'):
			gajim.gajim_optional_features[a].append(xmpp.NS_TUNE + '+notify')
		if gajim.config.get_per('accounts', a, 'subscribe_nick'):
			gajim.gajim_optional_features[a].append(xmpp.NS_NICK + '+notify')
		if gajim.config.get('outgoing_chat_state_notifactions') != 'disabled':
			gajim.gajim_optional_features[a].append(xmpp.NS_CHATSTATES)
		if not gajim.config.get('ignore_incoming_xhtml'):
			gajim.gajim_optional_features[a].append(xmpp.NS_XHTML_IM)
		if gajim.HAVE_PYCRYPTO \
		and gajim.config.get_per('accounts', a, 'enable_esessions'):
			gajim.gajim_optional_features[a].append(xmpp.NS_ESESSION)
js's avatar
js committed
		if gajim.config.get_per('accounts', a, 'answer_receipts'):
			gajim.gajim_optional_features[a].append(xmpp.NS_RECEIPTS)
		gajim.caps_hash[a] = compute_caps_hash([gajim.gajim_identity],
			gajim.gajim_common_features + gajim.gajim_optional_features[a])
		# re-send presence with new hash
		connected = gajim.connections[a].connected
		if connected > 1 and gajim.SHOW_LIST[connected] != 'invisible':
			gajim.connections[a].change_status(gajim.SHOW_LIST[connected],
				gajim.connections[a].status)