From 0405981b156d64e9eaf9a42ab0bbff780048bc06 Mon Sep 17 00:00:00 2001
From: js <js-gajim@webkeks.org>
Date: Wed, 2 Jul 2008 09:20:49 +0000
Subject: [PATCH] Added a function to check for specific features. This makes
 implementing a later coming fallback to disco rather easy.

---
 src/chat_control.py  | 12 ++++--------
 src/common/caps.py   | 15 +++++++++++++++
 src/roster_window.py | 16 ++++++----------
 3 files changed, 25 insertions(+), 18 deletions(-)

diff --git a/src/chat_control.py b/src/chat_control.py
index 7cb6a17a8d..9a4b454472 100644
--- a/src/chat_control.py
+++ b/src/chat_control.py
@@ -1074,15 +1074,13 @@ class ChatControl(ChatControlBase):
 
 		# If we don't have resource, we can't do file transfer
 		# in transports, contact holds our info we need to disable it too
-		if NS_FILE in gajim.capscache[(contact.caps_hash_method,
-		contact.caps_hash)].features:
+		if gajim.capscache.is_supported(contact, NS_FILE):
 			send_file_button.set_sensitive(True)
 		else:
 			send_file_button.set_sensitive(False)
 
 		# check if it's possible to convert to groupchat
-		if NS_MUC in gajim.capscache[(contact.caps_hash_method,
-		contact.caps_hash)].features:
+		if gajim.capscache.is_supported(contact, NS_MUC):
 			convert_to_gc_button.set_sensitive(True)
 		else:
 			convert_to_gc_button.set_sensitive(False)
@@ -1821,15 +1819,13 @@ class ChatControl(ChatControlBase):
 
 		# If we don't have resource, we can't do file transfer
 		# in transports, contact holds our info we need to disable it too
-		if NS_FILE in gajim.capscache[(contact.caps_hash_method,
-		contact.caps_hash)].features:
+		if gajim.capscache.is_supported(contact, NS_FILE):
 			send_file_menuitem.set_sensitive(True)
 		else:
 			send_file_menuitem.set_sensitive(False)
 
 		# check if it's possible to convert to groupchat
-		if NS_MUC in gajim.capscache[(contact.caps_hash_method,
-		contact.caps_hash)].features:
+		if gajim.capscache.is_supported(contact, NS_MUC):
 			convert_to_gc_menuitem.set_sensitive(True)
 		else:
 			convert_to_gc_menuitem.set_sensitive(False)
diff --git a/src/common/caps.py b/src/common/caps.py
index aac45d571e..df73baec1c 100644
--- a/src/common/caps.py
+++ b/src/common/caps.py
@@ -178,6 +178,21 @@ class CapsCache(object):
 			# this will create proper object
 			q.queried=1
 			con.discoverInfo(jid, '%s#%s' % (node, hash))
+	
+	def is_supported(self, contact, feature):
+		if not contact.resource:
+			return False
+
+		# FIXME: We assume everything is supported if we got no caps.
+		#	 This is the "Asterix way", after 0.12 release, I will
+		#	 likely implement a fallback to disco (could be disabled
+		#	 for mobile users who pay for traffic)
+		features = self[(contact.caps_hash_method,
+			contact.caps_hash)].features
+		if feature in features or features == []:
+			return True
+
+		return False
 
 gajim.capscache = CapsCache(gajim.logger)
 
diff --git a/src/roster_window.py b/src/roster_window.py
index 770aaf8ddd..73d9e1470e 100644
--- a/src/roster_window.py
+++ b/src/roster_window.py
@@ -4884,8 +4884,7 @@ class RosterWindow:
 				start_chat_menuitem.connect('activate',
 					self.on_roster_treeview_row_activated, tree_path)
 
-			if contact.resource and NS_FILE in gajim.capscache[
-			(contact.caps_hash_method, contact.caps_hash)].features:
+			if gajim.capscache.is_supported(contact, NS_FILE):
 				send_file_menuitem.set_sensitive(True)
 				send_file_menuitem.connect('activate',
 					self.on_send_file_menuitem_activate, contact, account)
@@ -5048,8 +5047,7 @@ class RosterWindow:
 		else: # one resource
 			start_chat_menuitem.connect('activate',
 				gajim.interface.on_open_chat_window, contact, account)
-			if contact.resource and NS_COMMANDS in gajim.capscache[
-			(contact.caps_hash_method, contact.caps_hash)].features:
+			if gajim.capscache.is_supported(contact, NS_COMMANDS):
 				execute_command_menuitem.set_sensitive(True)
 				execute_command_menuitem.connect('activate', self.on_execute_command,
 					contact, account, contact.resource)
@@ -5062,8 +5060,7 @@ class RosterWindow:
 				our_jid_other_resource = contact.resource
 			# Else this var is useless but harmless in next connect calls
 
-			if contact.resource and NS_FILE in gajim.capscache[
-			(contact.caps_hash_method, contact.caps_hash)].features:
+			if gajim.capscache.is_supported(contact, NS_FILE):
 				send_file_menuitem.set_sensitive(True)
 				send_file_menuitem.connect('activate',
 					self.on_send_file_menuitem_activate, contact, account)
@@ -5479,8 +5476,8 @@ class RosterWindow:
 				item.connect('activate', action, [(c, account)], c.resource)
 			else: # start_chat, execute_command, send_file
 				item.connect('activate', action, c, account, c.resource)
-			if cap and cap not in gajim.capscache[
-			(c.caps_hash_method, c.caps_hash)].features:
+			if cap and \
+			not gajim.capscache.is_supported(c, cap):
 				item.set_sensitive(False)
 		return sub_menu
 
@@ -5516,8 +5513,7 @@ class RosterWindow:
 			invite_to_new_room_menuitem.set_submenu(self.build_resources_submenu(
 				contact_list, account, self.on_invite_to_new_room, cap=NS_MUC))
 		else:
-			if NS_MUC in gajim.capscache[(contact.caps_hash_method,
-			contact.caps_hash)].features:
+			if gajim.capscache.is_supported(contact, NS_MUC):
 				invite_menuitem.set_sensitive(True)
 				invite_to_new_room_menuitem.connect('activate',
 					self.on_invite_to_new_room, list_)
-- 
GitLab