[kupfer: 2/51] grouping: Adapt plugins Claws Mail and Pidgin



commit 962de7f503259bf643a0350adaea71f0bc35085a
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Sun Jan 3 21:51:36 2010 +0100

    grouping: Adapt plugins Claws Mail and Pidgin

 kupfer/plugin/clawsmail.py |   61 +++++++++++++++++++++++--------------------
 kupfer/plugin/pidgin.py    |   62 +++++++++++++++++++++++++++++++------------
 2 files changed, 77 insertions(+), 46 deletions(-)
---
diff --git a/kupfer/plugin/clawsmail.py b/kupfer/plugin/clawsmail.py
index 1d8b7e4..fdffc25 100644
--- a/kupfer/plugin/clawsmail.py
+++ b/kupfer/plugin/clawsmail.py
@@ -3,9 +3,13 @@ import os
 import re
 from xml.dom import minidom
 
-from kupfer.objects import (Leaf, Action, Source, TextLeaf, UrlLeaf, RunnableLeaf, 
-		FilesystemWatchMixin, AppLeafContentMixin, FileLeaf)
+from kupfer.objects import Leaf, Action, Source
+from kupfer.objects import (TextLeaf, UrlLeaf, RunnableLeaf, FileLeaf,
+		AppLeafContentMixin )
 from kupfer import utils
+from kupfer.helplib import FilesystemWatchMixin
+from kupfer.obj.grouping import (EMAIL_KEY, NAME_KEY, ContactLeaf,
+		ToplevelGroupingSource)
 
 __kupfer_name__ = _("Claws Mail")
 __kupfer_sources__ = ("ClawsContactsSource", )
@@ -27,13 +31,17 @@ def _check_email(email):
 	return len(email) > 7 and _CHECK_EMAIL_RE.match(email.lower()) is not None
 
 
-class ClawsContact(Leaf):
+class ClawsContact(ContactLeaf):
 	''' Leaf represent single contact from Claws address book '''
-	def get_actions(self):
-		yield NewMailAction()
+	def __init__(self, obj, name):
+		slots = {EMAIL_KEY: obj, NAME_KEY: name}
+		super(ClawsContact, self).__init__(slots, name)
+
+	def repr_key(self):
+		return self.object[EMAIL_KEY]
 
 	def get_description(self):
-		return self.object
+		return self.object[EMAIL_KEY]
 
 	def get_icon_name(self):
 		return "stock_person"
@@ -69,40 +77,34 @@ class ReceiveMail(RunnableLeaf):
 		return "mail-send-receive"
 
 
+def _email_from_leaf(leaf):
+	if isinstance(leaf, UrlLeaf):
+		return _check_email(leaf.object) and _get_email_from_url(leaf.object)
+	if isinstance(leaf, TextLeaf):
+		return _check_email(leaf.object) and leaf.object
+	if isinstance(leaf, ContactLeaf):
+		return EMAIL_KEY in leaf and list(leaf[EMAIL_KEY])[0]
+
 class NewMailAction(Action):
 	''' Createn new mail to selected leaf (ClawsContact or TextLeaf)'''
 	def __init__(self):
 		Action.__init__(self, _('Compose New Mail To'))
 
 	def activate(self, leaf):
-		email = leaf.object
-		if isinstance(leaf, UrlLeaf):
-			email = _get_email_from_url(email)
-
+		email = _email_from_leaf(leaf)
 		utils.launch_commandline("claws-mail --compose '%s'" % email)
 
 	def get_icon_name(self):
 		return "mail-message-new"
 
 	def item_types(self):
-		yield ClawsContact
+		yield ContactLeaf
 		# we can enter email
 		yield TextLeaf
 		yield UrlLeaf
 
 	def valid_for_item(self, item):
-		if isinstance(item, ClawsContact):
-			return True
-
-		elif isinstance(item, TextLeaf):
-			return _check_email(item.object)
-
-		elif isinstance(item, UrlLeaf):
-			url = _get_email_from_url(item.object)
-			return _check_email(url)
-
-		return False
-
+		return bool(_email_from_leaf(item))
 
 class SendFileByMail(Action):
 	''' Createn new mail and attach selected file'''
@@ -126,17 +128,20 @@ class SendFileByMail(Action):
 		return os.path.isfile(item.object)
 
 
-class ClawsContactsSource(AppLeafContentMixin, Source, FilesystemWatchMixin):
+class ClawsContactsSource(AppLeafContentMixin, ToplevelGroupingSource,
+		FilesystemWatchMixin):
 	appleaf_content_id = 'claws-mail'
 
 	def __init__(self, name=_("Claws Mail Address Book")):
-		Source.__init__(self, name)
+		super(ClawsContactsSource, self).__init__(name, "Contacts")
+		#Source.__init__(self, name)
 		self._claws_addrbook_dir = os.path.expanduser('~/.claws-mail/addrbook')
 		self._claws_addrbook_index = os.path.join(self._claws_addrbook_dir, \
 				"addrbook--index.xml")
-		self.unpickle_finish()
+		self._version = 4
 
-	def unpickle_finish(self):
+	def initialize(self):
+		ToplevelGroupingSource.initialize(self)
 		if not os.path.isdir(self._claws_addrbook_dir):
 			return
 
@@ -177,8 +182,8 @@ class ClawsContactsSource(AppLeafContentMixin, Source, FilesystemWatchMixin):
 		return "claws-mail"
 
 	def provides(self):
-		yield ClawsContact
 		yield RunnableLeaf
+		yield ContactLeaf
 
 	def _load_address_books(self):
 		''' load list of address-book files '''
diff --git a/kupfer/plugin/pidgin.py b/kupfer/plugin/pidgin.py
index 8432e33..d33f351 100644
--- a/kupfer/plugin/pidgin.py
+++ b/kupfer/plugin/pidgin.py
@@ -8,9 +8,15 @@ from kupfer import pretty, scheduler
 from kupfer import icons
 from kupfer import plugin_support
 from kupfer.helplib import dbus_signal_connect_weakly, PicklingHelperMixin
+from kupfer.obj.grouping import (EMAIL_KEY, NAME_KEY, ContactLeaf,
+		ToplevelGroupingSource)
 
 __kupfer_name__ = _("Pidgin")
 __kupfer_sources__ = ("ContactsSource", )
+__kupfer_actions__ = (
+	"OpenChat",
+	"SendMessage",
+)
 __description__ = _("Access to Pidgin Contacts")
 __version__ = "0.1"
 __author__ = ("Chmouel Boudjnah <chmouel chmouel com>, "
@@ -18,11 +24,15 @@ __author__ = ("Chmouel Boudjnah <chmouel chmouel com>, "
 
 plugin_support.check_dbus_connection()
 
+# Contact data contstants
+PIDGIN_ACCOUNT = "_PIDGIN_ACCOUNT"
+PIDGIN_JID = "_PIDGIN_JID"
+
+# D-Bus "addresses"
 SERVICE_NAME = "im.pidgin.purple.PurpleService"
 OBJECT_NAME = "/im/pidgin/purple/PurpleObject"
 IFACE_NAME = "im.pidgin.purple.PurpleInterface"
 
-
 def _create_dbus_connection(activate=False):
 	''' Create dbus connection to Pidgin
 	@activate: true=starts pidgin if not running
@@ -52,14 +62,23 @@ def _send_message_to_contact(pcontact, message, present=False):
 	interface = _create_dbus_connection()
 	if not interface:
 		return
-	account, jid = pcontact.account, pcontact.jid
+	account = pcontact[PIDGIN_ACCOUNT][0]
+	jid = pcontact[PIDGIN_JID][0]
 	conversation = interface.PurpleConversationNew(1, account, jid)
 	im = interface.PurpleConvIm(conversation)
 	interface.PurpleConvImSend(im, message)
 	if present:
 		interface.PurpleConversationPresent(conversation)
 
-class OpenChat(Action):
+class ContactAction (Action):
+	def get_required_slots(self):
+		return ()
+	def item_types(self):
+		yield ContactLeaf
+	def valid_for_item(self, leaf):
+		return all(slot in leaf for slot in self.get_required_slots())
+
+class OpenChat(ContactAction):
 	""" Open Chat Conversation Window with jid """
 
 	def __init__(self):
@@ -68,6 +87,9 @@ class OpenChat(Action):
 	def activate(self, leaf):
 		_send_message_to_contact(leaf, u"", present=True)
 
+	def get_required_slots(self):
+		return [PIDGIN_ACCOUNT, PIDGIN_JID]
+
 class ChatTextSource (TextSource):
 	def get_rank(self):
 		return 100
@@ -80,7 +102,7 @@ class ChatTextSource (TextSource):
 	def get_items(self, text):
 		return self.get_text_items(text)
 
-class SendMessage (Action):
+class SendMessage (ContactAction):
 	""" Send chat message directly from Kupfer """
 	def __init__(self):
 		Action.__init__(self, _("Send Message..."))
@@ -88,8 +110,9 @@ class SendMessage (Action):
 	def activate(self, leaf, iobj):
 		_send_message_to_contact(leaf, iobj.object)
 
-	def item_types(self):
-		yield PidginContact
+	def get_required_slots(self):
+		return [PIDGIN_ACCOUNT, PIDGIN_JID]
+
 	def requires_object(self):
 		return True
 	def object_types(self):
@@ -97,16 +120,19 @@ class SendMessage (Action):
 	def object_source(self, for_item=None):
 		return ChatTextSource()
 
-class PidginContact(Leaf):
+class PidginContact(ContactLeaf):
 	""" Leaf represent single contact from Pidgin """
-
 	def __init__(self, jid, name, account, icon, protocol, available,
 		status_message):
-		# @obj should be unique for each contact
-		# we use @jid as an alias for this contact
-		obj = (account, jid)
-		Leaf.__init__(self, obj, name or jid)
+		slots = {
+			EMAIL_KEY: jid,
+			NAME_KEY: name or jid,
+			PIDGIN_ACCOUNT: account,
+			PIDGIN_JID: jid,
+		}
+		ContactLeaf.__init__(self, slots, name or jid)
 
+		# we use @jid as an alias for this contact
 		if unicode(self) != jid:
 			self.name_aliases.add(jid)
 
@@ -124,9 +150,8 @@ class PidginContact(Leaf):
 		self.jid = jid
 		self.icon = icon
 
-	def get_actions(self):
-		yield OpenChat()
-		yield SendMessage()
+	def repr_key(self):
+		return "%s, %s" % (self.object[PIDGIN_ACCOUNT], self.object[PIDGIN_JID])
 
 	def get_description(self):
 		return self._description
@@ -140,13 +165,13 @@ class PidginContact(Leaf):
 		return "stock_person"
 
 
-class ContactsSource(AppLeafContentMixin, Source, PicklingHelperMixin):
+class ContactsSource(AppLeafContentMixin, ToplevelGroupingSource, PicklingHelperMixin):
 	''' Get contacts from all on-line accounts in Pidgin via DBus '''
 	appleaf_content_id = 'pidgin'
 
 	def __init__(self):
-		Source.__init__(self, _('Pidgin Contacts'))
-		self._version = 2
+		ToplevelGroupingSource.__init__(self, _('Pidgin Contacts'), "Contacts")
+		self._version = 5
 		self.unpickle_finish()
 
 	def unpickle_finish(self):
@@ -154,6 +179,7 @@ class ContactsSource(AppLeafContentMixin, Source, PicklingHelperMixin):
 		self.pickle_prepare()
 
 	def initialize(self):
+		ToplevelGroupingSource.initialize(self)
 		self._install_dbus_signal()
 		self._buddy_update_timer = scheduler.Timer()
 		self._buddy_update_queue = set()



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