From b145e59b419756f0b8c494166b9ac891513b44fb Mon Sep 17 00:00:00 2001
From: Dimitur Kirov <dkirov@gmail.com>
Date: Mon, 8 May 2006 22:44:47 +0000
Subject: [PATCH] moved completion dict creation in helpers.py

---
 src/common/helpers.py | 19 +++++++++++++++++++
 src/dialogs.py        | 32 +++++---------------------------
 2 files changed, 24 insertions(+), 27 deletions(-)

diff --git a/src/common/helpers.py b/src/common/helpers.py
index a3fd5f5a51..a5574cc96d 100644
--- a/src/common/helpers.py
+++ b/src/common/helpers.py
@@ -179,6 +179,25 @@ def convert_bytes(string):
 		suffix = _('%s B')
 	return suffix % unicode(bytes)
 
+
+def get_contact_dict_for_account(account):
+	''' create a dict of jid, nick -> contact with all contacts of account.
+	Can be used for completion lists'''
+	contacts_dict = {}
+	for jid in gajim.contacts.get_jid_list(account):
+		contact = gajim.contacts.get_contact_with_highest_priority(account,
+				jid)
+		contacts_dict[jid] = contact
+		name = contact.name
+		if contacts_dict.has_key(name):
+			contact1 = contacts_dict[name]
+			del contacts_dict[name]
+			contacts_dict['%s (%s)' % (name, contact1.jid)] = contact1
+			contacts_dict['%s (%s)' % (name, jid)] = contact
+		else:
+			contacts_dict[name] = contact
+	return contacts_dict
+
 def get_uf_show(show, use_mnemonic = False):
 	'''returns a userfriendly string for dnd/xa/chat
 	and makes all strings translatable
diff --git a/src/dialogs.py b/src/dialogs.py
index e33c0c43ab..0a5f56f396 100644
--- a/src/dialogs.py
+++ b/src/dialogs.py
@@ -1000,18 +1000,8 @@ class NewChatDialog(InputDialog):
 		
 		self.completion_dict = {}
 		liststore = gtkgui_helpers.get_completion_liststore(self.input_entry)
+		self.completion_dict = helpers.get_contact_dict_for_account(account)
 		# add all contacts to the model
-		for jid in gajim.contacts.get_jid_list(account):
-			contact = gajim.contacts.get_contact_with_highest_priority(account, jid)
-			self.completion_dict[jid] = contact
-			name = contact.name
-			if self.completion_dict.has_key(name):
-				contact1 = self.completion_dict[name]
-				del self.completion_dict[name]
-				self.completion_dict['%s (%s)' % (name, contact1.jid)] = contact1
-				self.completion_dict['%s (%s)' % (name, jid)] = contact
-			else:
-				self.completion_dict[name] = contact
 		for jid in self.completion_dict.keys():
 			contact = self.completion_dict[jid]
 			img =  gajim.interface.roster.jabber_state_images['16'][contact.show]
@@ -1252,29 +1242,17 @@ class SingleMessageWindow:
 			self.subject = ''
 		self.subject_entry.set_text(self.subject)
 
-		self.completion_dict = {}
+
 		if to == '':
 			liststore = gtkgui_helpers.get_completion_liststore(self.to_entry)
-			# add all contacts to the model
-			for jid in gajim.contacts.get_jid_list(account):
-				contact = gajim.contacts.get_contact_with_highest_priority(
-							account, jid)
-				self.completion_dict[jid] = contact
-				name = contact.name
-				if self.completion_dict.has_key(name):
-					contact1 = self.completion_dict[name]
-					del self.completion_dict[name]
-					self.completion_dict['%s (%s)' % (name, contact1.jid)] = \
-						contact1
-					self.completion_dict['%s (%s)' % (name, jid)] = contact
-				else:
-					self.completion_dict[name] = contact
+			self.completion_dict = helpers.get_contact_dict_for_account(account)
 			for jid in self.completion_dict.keys():
 				contact = self.completion_dict[jid]
 				img = gajim.interface.roster.jabber_state_images['16'][
 						contact.show]
 				liststore.append((img.get_pixbuf(), jid))
-
+		else:
+			self.completion_dict = {}
 		self.xml.signal_autoconnect(self)
 
 		if gajim.config.get('saveposition'):
-- 
GitLab