New addressbook backend.



Hi guys,

I wrote a backend for the Evolution 2.0 addressbook.  It's a bit buggy
right now, and you need a couple patches for evolution-sharp to compile
it.

When this is working, I'm ready to do a release.  Post 0.1, I want to
spend some serious time cleaning up the architecture.  Some of the new
resultset stuff is confusing and makes writing backends harder, and the
fact that match rendering is now uniform no matter the match type is
somewhat suboptimal for the user; different types of data shouldn't look
the same.

Best,
Nat

//
// backend-edsbook.cs: Evolution 2.0 addressbook backend.
//
// Author:
//   Nat Friedman <nat novell com>
//
//

using System;
using System.Collections;
using GLib;
using Evolution;

namespace Dashboard {
	public class Addressbook : Backend {

		Evolution.Book addressbook;

		public override bool Startup ()
		{
			Name = "Evolution Addressbook";

			addressbook = new Evolution.Book ();
			addressbook.LoadLocalAddressbook ();

			this.SubscribeToClues ("full_name", 
					       "email", 
					       "org", 
					       "url", 
					       "phone", 
					       "address", 
					       "rss", 
					       "aim_name", 
					       "yahoo_name", 
					       "jabber_name", 
					       "msn_name", 
					       "icq_name", 
					       "keyword");

			this.Initialized = true;

			return true;
			
		}

		public override BackendResultSet ProcessCluePacket (CluePacket cp)
		{
			BackendResultSet resultset = new BackendResultSet (this, cp);

			foreach (Clue c in cp.Clues)
				this.ProcessClue (resultset, c);

			return resultset;
		}

		private void ProcessClue (BackendResultSet resultset, Clue c)
		{
			ArrayList contacts;

			contacts = FindContacts (c);

			GenerateMatches (resultset, contacts, c);
			GenerateClues (resultset, contacts, c);
		}

		private ArrayList FindContacts (Clue c)
		{

			BookQuery query = BuildQuery (c);

			return addressbook.GetContacts (query);
		}

		private void GenerateMatches (BackendResultSet resultset, ArrayList contacts, Clue c)
		{
			foreach (Contact contact in contacts)
				resultset.AddResult (GenerateResultForContact (contact), c);
		}

		private void GenerateClues (BackendResultSet resultset, ArrayList contacts, Clue c)
		{
			ArrayList clues = new ArrayList ();

			foreach (Contact contact in contacts)
				GenerateCluesForContact (clues, contact, c);
		}

		private void GenerateCluesForContact (ArrayList clues, Contact contact, Clue c)
		{
			AddClue (clues, c, contact.FullName,     "full_name");
			AddClue (clues, c, contact.Email1,       "email");
			AddClue (clues, c, contact.ImAim,        "aim_name");
			AddClue (clues, c, contact.ImMsn,        "msn_name");
			AddClue (clues, c, contact.ImIcq,        "icq_name");
			AddClue (clues, c, contact.ImJabber,     "jabber_name");
			AddClue (clues, c, contact.ImYahoo,      "yahoo_name");
			AddClue (clues, c, contact.HomepageUrl,  "url");
			AddClue (clues, c, contact.BlogUrl,      "rss");
		}

		private void AddClue (ArrayList clues, Clue c, string field, string field_name)
		{
			if (field != null && field != "")
				clues.Add (new Clue (field_name, field, 10, c));
		}

		private BackendResult GenerateResultForContact (Contact contact)
		{
			BackendResult result = new BackendResult ();

			if (contact.FullName != null && contact.FullName != "")
				result.AddItem (new BackendResultItem ("Name", contact.FullName, "text", "xml"));

			if (contact.Email1 != null && contact.Email1 != "")
				result.AddItem (new BackendResultItem ("EMail", contact.Email1, "text", "xml"));

			if (contact.PrimaryPhone != null && contact.PrimaryPhone != "")
				result.AddItem (new BackendResultItem ("Phone", contact.PrimaryPhone, "text", "xml"));

			return result;
		}

		private BookQuery BuildQuery (Clue c)
		{
			if (c.Type == "*" || c.Type == "keyword")
				return BookQuery.AnyFieldContains (c.Text);

//			if (c.Type == "phone")
//				return BookQuery.Andv (BookQuery.FieldTest (ContactField.PhonePrimary, BookQueryTest.Is, Clue.Text),
//						       BookQuery.FieldTest (ContactField.PhoneBusiness, BookQueryTest.Is, Clue.Text),
//						       BookQuery.FieldTest (ContactField.PhoneHome, BookQueryTest.Is, Clue.Text),
//						       BookQuery.FieldTest (ContactField.PhoneHome2, BookQueryTest.Is, Clue.Text),
//						       BookQuery.FieldTest (ContactField.PhoneHomeFax, BookQueryTest.Is, Clue.Text),
//						       BookQuery.FieldTest (ContactField.PhoneMobile, BookQueryTest.Is, Clue.Text),
//						       BookQuery.FieldTest (ContactField.PhonePager, BookQueryTest.Is, Clue.Text));

			ContactField field = MapClueTypeToField (c);

			return BookQuery.FieldTest (MapClueTypeToField (c), BookQueryTest.Is, c.Text);
		}

		private ContactField MapClueTypeToField (Clue c)
		{
			switch (c.Type) {
			case "email":
				return ContactField.Email;
			case "url":
				return ContactField.HomepageUrl;
			case "aim_name":
				return ContactField.ImAim;
			case "icq_name":
				return ContactField.ImIcq;
			case "msn_name":
				return ContactField.ImMsn;
			case "yahoo_name":
				return ContactField.ImYahoo;
			case "jabber_name":
				return ContactField.ImJabber;
			// FIXME: this is the main blog URL, not the RSS URL
			case "rss":
				return ContactField.BlogUrl;
			}

			return ContactField.FileAs;
		}

	}
}

Index: Contact.custom
===================================================================
RCS file: /cvs/gnome/evolution-sharp/evolution/Contact.custom,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 Contact.custom
--- Contact.custom	11 Nov 2003 21:01:27 -0000	1.1.1.1
+++ Contact.custom	1 Feb 2004 01:56:44 -0000
@@ -30,6 +30,18 @@
 			}
 		}
 
+		public string FullName {
+			get {
+				GLib.Value val = new GLib.Value (Handle, "full_name");
+				GetProperty("full_name", val);
+				string ret = (string) val;
+				return ret;
+			}
+			set {
+				SetProperty("full_name", new GLib.Value(value));
+			}
+		}
+
 		public string GivenName {
 			get {
 				GLib.Value val = new GLib.Value (Handle, "given_name");
@@ -546,6 +558,66 @@
 			}
 		}
 
+		public string ImAim {
+			get {
+				GLib.Value val = new GLib.Value (Handle, "im_aim");
+				GetProperty("im_aim", val);
+				string ret = (string) val;
+				return ret;
+			}
+			set {
+				SetProperty("im_aim", new GLib.Value(value));
+			}
+		}
+
+		public string ImMsn {
+			get {
+				GLib.Value val = new GLib.Value (Handle, "im_msn");
+				GetProperty("im_msn", val);
+				string ret = (string) val;
+				return ret;
+			}
+			set {
+				SetProperty("im_msn", new GLib.Value(value));
+			}
+		}
+
+		public string ImIcq {
+			get {
+				GLib.Value val = new GLib.Value (Handle, "im_icq");
+				GetProperty("im_icq", val);
+				string ret = (string) val;
+				return ret;
+			}
+			set {
+				SetProperty("im_icq", new GLib.Value(value));
+			}
+		}
+
+		public string ImJabber {
+			get {
+				GLib.Value val = new GLib.Value (Handle, "im_jabber");
+				GetProperty("im_jabber", val);
+				string ret = (string) val;
+				return ret;
+			}
+			set {
+				SetProperty("im_jabber", new GLib.Value(value));
+			}
+		}
+
+		public string ImYahoo {
+			get {
+				GLib.Value val = new GLib.Value (Handle, "im_yahoo");
+				GetProperty("im_yahoo", val);
+				string ret = (string) val;
+				return ret;
+			}
+			set {
+				SetProperty("im_yahoo", new GLib.Value(value));
+			}
+		}
+
 		public string Spouse {
 			get {
 				GLib.Value val = new GLib.Value (Handle, "spouse");
Index: Book.custom
===================================================================
RCS file: /cvs/gnome/evolution-sharp/evolution/Book.custom,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 Book.custom
--- Book.custom	11 Nov 2003 21:01:27 -0000	1.1.1.1
+++ Book.custom	1 Feb 2004 00:07:23 -0000
@@ -1,14 +1,14 @@
 		[DllImport("ebook")]
 		static extern unsafe bool e_book_get_contacts(IntPtr raw, IntPtr query, out IntPtr contacts, out IntPtr error);
 
-		public unsafe GLib.List GetContacts(Evolution.BookQuery query) {
+		public unsafe ArrayList GetContacts (Evolution.BookQuery query) {
 			IntPtr contacts_handle;
 			IntPtr error = IntPtr.Zero;
-			bool raw_ret = e_book_get_contacts(Handle, query.Handle, out contacts_handle, out error);
+			bool raw_ret = e_book_get_contacts (Handle, query.Handle, out contacts_handle, out error);
 			if (error != IntPtr.Zero) throw new GLib.GException (error);
 			if (raw_ret == false)
 				return null;
 			else
-				return new GLib.List(contacts_handle, typeof (Contact));
+				return new ArrayList (new GLib.List (contacts_handle, typeof (Contact)));
 		}
 


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