[kupfer: 4/12] gmail: loading additional contacts data



commit 0c1e7402d3c184222278217b97c05677d3d00de6
Author: Karol BÄdkowski <karol bedkowski gmail com>
Date:   Sat Dec 17 17:47:48 2011 +0100

    gmail: loading additional contacts data
    
    Gmail plugin:
    * Update to use new classes.
    * Simplify plugin
    * fix problem with grouping (missing initializing ToplevelGroupinSource)
    * fix problem with pickling contacts list
    Other:
    * update authors
    * pep8
    
    gmail: loading additional contacts data may be disabled in preferences
    
    Add new configuration option for plugin "Load additional information".

 kupfer/plugin/gmail/__init__.py |  161 ++++++++++++++++++--------------------
 1 files changed, 76 insertions(+), 85 deletions(-)
---
diff --git a/kupfer/plugin/gmail/__init__.py b/kupfer/plugin/gmail/__init__.py
index a3fb9b9..416fb39 100644
--- a/kupfer/plugin/gmail/__init__.py
+++ b/kupfer/plugin/gmail/__init__.py
@@ -3,8 +3,9 @@ __kupfer_name__ = _("Gmail")
 __kupfer_sources__ = ("GoogleContactsSource", )
 __kupfer_actions__ = ('NewMailAction', )
 __description__ = _("Load contacts and compose new email in Gmail")
-__version__ = "2010-04-06"
-__author__ = "Karol BÄdkowski <karol bedkowski gmail com>"
+__version__ = "2011-03-06"
+__author__ = ("Karol BÄdkowski <karol bedkowski gmail com>, "
+              "Adi Sieker <adi sieker info>")
 
 import urllib
 import time
@@ -15,11 +16,8 @@ import gdata.contacts.service
 from kupfer.objects import Action, TextLeaf, UrlLeaf
 from kupfer.obj.special import PleaseConfigureLeaf, InvalidCredentialsLeaf
 from kupfer.obj.grouping import ToplevelGroupingSource
-from kupfer.obj.contacts import ContactLeaf, email_from_leaf
-from kupfer.obj.contacts import JabberContact, AddressContact, PhoneContact
-from kupfer.obj.contacts import IMContact, EmailContact
+from kupfer.obj import contacts
 from kupfer import plugin_support, pretty, utils, icons, kupferstring
-from kupfer.plugin.skype import Contact as SkypeContact
 
 plugin_support.check_keyring_support()
 
@@ -35,7 +33,13 @@ __kupfer_settings__ = plugin_support.PluginSettings(
 		'label': _("Load contacts' pictures"),
 		'type': bool,
 		'value': True,
-	}
+	},
+	{
+		'key': 'loadadditional',
+		'label': _("Load additional information"),
+		'type': bool,
+		'value': True,
+	},
 )
 
 GMAIL_NEW_MAIL_URL = \
@@ -43,45 +47,40 @@ GMAIL_NEW_MAIL_URL = \
 
 GMAIL_EDIT_CONTACT_URL = "https://mail.google.com/mail/#contact/%(contact)s"
 
-REL_TYPE_EMAIL = "email"
-REL_TYPE_ADDRESS = "address"
-REL_TYPE_PHONE = "phone"
-REL_TYPE_IM = "im"
-REL_LIST = {}
-
-REL_LIST[REL_TYPE_EMAIL] = {gdata.contacts.REL_WORK: _("Work email"),
-                            gdata.contacts.REL_HOME: _("Home email"),
-                            gdata.contacts.REL_OTHER: _("Other email")
+REL_LIST_EMAIL = {gdata.contacts.REL_WORK: _("Work email"),
+                  gdata.contacts.REL_HOME: _("Home email"),
+                  gdata.contacts.REL_OTHER: _("Other email"),
 }
-
-REL_LIST[REL_TYPE_ADDRESS] = {gdata.contacts.REL_WORK: _("Work address"),
-                              gdata.contacts.REL_HOME: _("Home address"),
-                              gdata.contacts.REL_OTHER: _("Other address")
+REL_LIST_ADDRESS = {gdata.contacts.REL_WORK: _("Work address"),
+                    gdata.contacts.REL_HOME: _("Home address"),
+                    gdata.contacts.REL_OTHER: _("Other address"),
 }
-
-REL_LIST[REL_TYPE_PHONE] = {gdata.contacts.PHONE_CAR: _("Car phone"),
-                            gdata.contacts.PHONE_FAX: _("Fax"),
-                            gdata.contacts.PHONE_GENERAL: _("General"),
-                            gdata.contacts.PHONE_HOME: _("Home phone"),
-                            gdata.contacts.PHONE_HOME_FAX: _("Home fax"),
-                            gdata.contacts.PHONE_INTERNAL: _("Internal phone"),
-                            gdata.contacts.PHONE_MOBILE: _("Mobile"),
-                            gdata.contacts.PHONE_OTHER: _("Other"),
-                            gdata.contacts.PHONE_VOIP: _("VOIP"),
-                            gdata.contacts.PHONE_WORK: _("Work phone"),
-                            gdata.contacts.PHONE_WORK_FAX: _("Work fax")
+REL_LIST_PHONE = {gdata.contacts.PHONE_CAR: _("Car phone"),
+                  gdata.contacts.PHONE_FAX: _("Fax"),
+                  gdata.contacts.PHONE_GENERAL: _("General"),
+                  gdata.contacts.PHONE_HOME: _("Home phone"),
+                  gdata.contacts.PHONE_HOME_FAX: _("Home fax"),
+                  gdata.contacts.PHONE_INTERNAL: _("Internal phone"),
+                  gdata.contacts.PHONE_MOBILE: _("Mobile"),
+                  gdata.contacts.PHONE_OTHER: _("Other"),
+                  gdata.contacts.PHONE_VOIP: _("VOIP"),
+                  gdata.contacts.PHONE_WORK: _("Work phone"),
+                  gdata.contacts.PHONE_WORK_FAX: _("Work fax"),
 }
-
-REL_LIST[REL_TYPE_IM] = {gdata.contacts.IM_AIM: _("Aim"),
-                         gdata.contacts.IM_GOOGLE_TALK: _("Google Talk"),
-                         gdata.contacts.IM_ICQ: _("ICQ"),
-                         gdata.contacts.IM_JABBER: _("Jabber"),
-                         gdata.contacts.IM_MSN: _("MSN"),
-                         gdata.contacts.IM_QQ: _("QQ"),
-                         gdata.contacts.IM_SKYPE: _("Skype"),
-                         gdata.contacts.IM_YAHOO: _("Yahoo")
+REL_LIST_IM = {gdata.contacts.IM_AIM: contacts.AIMContact,
+               gdata.contacts.IM_GOOGLE_TALK: contacts.GoogleTalkContact,
+               gdata.contacts.IM_ICQ: contacts.ICQContact,
+               gdata.contacts.IM_JABBER: contacts.JabberContact,
+               gdata.contacts.IM_MSN: contacts.MSNContact,
+               gdata.contacts.IM_QQ: contacts.QQContact,
+               gdata.contacts.IM_YAHOO: contacts.YahooContact,
 }
 
+# older version of gdata don't have IM_SKYPE
+if hasattr(gdata.contacts, 'IM_SKYPE'):
+	REL_LIST_IM[gdata.contacts.IM_SKYPE] = contacts.SkypeContact
+
+
 def is_plugin_configured():
 	''' Check if plugin is configured (user name and password is configured) '''
 	upass = __kupfer_settings__['userpass']
@@ -97,7 +96,8 @@ class NewMailAction(Action):
 		self.activate_multiple((obj, ))
 
 	def activate_multiple(self, objects):
-		recipients = ",".join(urllib.quote(email_from_leaf(L)) for L in objects)
+		recipients = ",".join(urllib.quote(contacts.email_from_leaf(L))
+				for L in objects)
 		url = GMAIL_NEW_MAIL_URL % dict(emails=recipients)
 		utils.show_url(url)
 
@@ -105,13 +105,13 @@ class NewMailAction(Action):
 		return icons.ComposedIcon("mail-message-new", "gmail")
 
 	def item_types(self):
-		yield ContactLeaf
+		yield contacts.ContactLeaf
 		# we can enter email
 		yield TextLeaf
 		yield UrlLeaf
 
 	def valid_for_item(self, item):
-		return bool(email_from_leaf(item))
+		return bool(contacts.email_from_leaf(item))
 
 	def get_description(self):
 		return _("Open web browser and compose new email in Gmail")
@@ -146,11 +146,6 @@ def get_gclient():
 	gd_client.ProgrammaticLogin()
 	return gd_client
 
-def get_label(rel_type, key):
-	try:
-		return REL_LIST[rel_type][key]
-	except KeyError:
-		return u''
 
 def get_contacts():
 	''' load all contacts '''
@@ -159,15 +154,17 @@ def get_contacts():
 	num_contacts = 0
 	try:
 		gd_client = get_gclient()
-
 		if gd_client is None:
 			return
-
 		query = gdata.contacts.service.ContactsQuery()
-		query.max_results = 9999 # load all contacts
+		query.max_results = 9999  # load all contacts
 		for entry in gd_client.GetContactsFeed(query.ToUri()).entry:
+			if not entry.email:
+				# skip contacts without email
+				continue
 			num_contacts += 1
 			common_name = kupferstring.tounicode(entry.title.text)
+			primary_mail_key = {contacts.EMAIL_KEY: entry.email[0].address}
 			contact_id = None
 			try:
 				contact_id = entry.id.text.split('/')[-1]
@@ -182,53 +179,52 @@ def get_contacts():
 							image = gd_client.GetPhoto(entry)
 						except:
 							pass
-					email = email.address
-					yield GoogleContact(email, common_name or email, image, contact_id)
-
+					email_str = email.address
+					yield GoogleContact(email_str, common_name or email_str,
+							image, contact_id, REL_LIST_EMAIL.get(email.rel))
+			if not __kupfer_settings__['loadadditional']:
+				continue
 			for phone in entry.phone_number:
-				if not phone.text:
-					continue
-				yield PhoneContact(phone.text, common_name, get_label(REL_TYPE_PHONE, phone.rel))
-
+				if phone.text:
+					yield contacts.PhoneContact(phone.text, common_name,
+							REL_LIST_PHONE.get(phone.rel), slots=primary_mail_key)
 			for address in entry.postal_address:
-				if not address.text:
-					continue
-				yield AddressContact(address.text, common_name, get_label(REL_TYPE_ADDRESS, address.rel))
-
+				if address.text:
+					yield contacts.AddressContact(address.text, common_name,
+							REL_LIST_PHONE.get(address.rel), slots=primary_mail_key)
 			for im in entry.im:
-				if not im.text:
-					continue
-				if im.rel == gdata.contacts.IM_SKYPE:
-					yield SkypeContact(common_name, im.text, "Unknown")
-				elif im.rel == gdata.contacts.IM_JABBER:
-					yield JabberContact(im.text, common_name, "Unknown", None)
-				else:
-					yield IMContact(im.text, common_name, get_label(REL_TYPE_IM, im.rel))
-
+				im_id = im.text or im.address
+				protocol = im.protocol or im.rel
+				if im_id and protocol in REL_LIST_IM:
+					yield REL_LIST_IM[protocol](im_id, common_name, slots=primary_mail_key)
 	except (gdata.service.BadAuthentication, gdata.service.CaptchaRequired), err:
 		pretty.print_error(__name__, 'get_contacts error',
 				'authentication error', err)
 		yield InvalidCredentialsLeaf(__name__, __kupfer_name__)
-
 	except gdata.service.Error, err:
 		pretty.print_error(__name__, 'get_contacts error', err)
-
 	else:
 		pretty.print_debug(__name__, 'get_contacts finished; load contacts:',
-				num_contacts, 'in:', time.time()-start_time, 'load_icons:',
+				num_contacts, 'in:', time.time() - start_time, 'load_icons:',
 				__kupfer_settings__['loadicons'])
 
 
-class GoogleContact(EmailContact):
-	def __init__(self, email, name, image, contact_id):
-		EmailContact.__init__(self, email, name)
+class GoogleContact(contacts.EmailContact):
+	def __init__(self, email, name, image, contact_id, email_type):
+		contacts.EmailContact.__init__(self, email, name)
 		self.image = image
+		self.email_type = email_type
 		self.google_contact_id = contact_id
 
 	def get_thumbnail(self, width, height):
 		if self.image:
 			return icons.get_pixbuf_from_data(self.image, width, height)
-		return EmailContact.get_thumbnail(self, width, height)
+		return contacts.EmailContact.get_thumbnail(self, width, height)
+
+	def get_description(self):
+		if self.email_type:
+			return '%s: %s' % (self.email_type, self.object[contacts.EMAIL_KEY])
+		return contacts.EmailContact.get_description(self)
 
 	def get_actions(self):
 		if self.google_contact_id:
@@ -240,7 +236,7 @@ class GoogleContactsSource(ToplevelGroupingSource):
 
 	def __init__(self, name=_("Gmail")):
 		super(GoogleContactsSource, self).__init__(name, "Contacts")
-		self._version = 4
+		self._version = 5
 		self._contacts = []
 
 	def initialize(self):
@@ -266,13 +262,8 @@ class GoogleContactsSource(ToplevelGroupingSource):
 	def should_sort_lexically(self):
 		return True
 
-	def provides(self):
-		yield ContactLeaf
-		yield PleaseConfigureLeaf
-
 	def get_description(self):
 		return _("Contacts from Google services (Gmail)")
 
 	def get_icon_name(self):
 		return "gmail"
-



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]