~cszikszoy/do-plugins/pastebin

1 by djsiegel at gmail
Initial files.
1
//  Evolution.cs (requires libevolution-cil)
2
//
3
//  GNOME Do is the legal property of its developers.
4
//  Please refer to the COPYRIGHT file distributed with this
5
//  source distribution.
6
//
7
//  This program is free software: you can redistribute it and/or modify
8
//  it under the terms of the GNU General Public License as published by
9
//  the Free Software Foundation, either version 3 of the License, or
10
//  (at your option) any later version.
11
//
12
//  This program is distributed in the hope that it will be useful,
13
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
//  GNU General Public License for more details.
16
//
17
//  You should have received a copy of the GNU General Public License
18
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
using System;
21
using System.IO;
22
using System.Collections.Generic;
23
24
using Evolution;
25
26
using Do.Addins;
27
28
namespace Do.Universe
29
{
30
31
	public class EvolutionContactItemSource : IItemSource
32
	{
33
		
34
		List<IItem> contacts;
35
		
36
		public EvolutionContactItemSource ()
37
		{
38
			contacts = new List<IItem> ();
39
			UpdateItems ();
40
		}
41
		
42
		public Type[] SupportedItemTypes {
43
			get {
44
				return new Type[] {
45
					typeof (ContactItem),
46
				};
47
			}
48
		}
49
		
50
		public string Name { get { return "Evolution Contacts"; } }
51
		public string Description { get { return "Evolution Contacts"; } }
52
		public string Icon { get { return "evolution"; } }
53
		
54
		public void UpdateItems ()
55
		{
56
			try {
57
				_UpdateItems ();
58
			} catch (Exception e) {
59
				Console.WriteLine ("Cannot index evolution contacts because a {0} was thrown: {1}", e.GetType (), e.Message);
60
				return;
61
			}
62
		}
63
		
64
		public ICollection<IItem> Items {
65
			get { return contacts; }
66
		}
67
		
68
		public ICollection<IItem> ChildrenOfItem (IItem item)
69
		{
70
			return null;
71
		}
72
		
73
		void _UpdateItems ()
74
		{
75
			SourceList sources;
76
		
77
			contacts.Clear ();
78
			sources = new SourceList ("/apps/evolution/addressbook/sources");
79
			foreach (SourceGroup group in sources.Groups)
80
			foreach (Source source in group.Sources) {
81
				Book address_book;
82
				Contact[] e_contacts;
83
				ContactItem contact;
84
					
85
				// Only index local address books
86
				if (!source.IsLocal ()) continue;
87
				address_book = new Book (source);
88
				address_book.Open (true);
89
				e_contacts = address_book.GetContacts (BookQuery.AnyFieldContains (""));
90
				foreach (Contact e_contact in e_contacts) {
91
					try {
92
						contact = CreateEvolutionContactItem (e_contact);
93
					} catch {
94
						// bad contact
95
						continue;
96
					}
97
					contacts.Add (contact);
98
				}
99
			}
100
		}
101
	
102
		ContactItem CreateEvolutionContactItem (Contact e_contact) {
103
			ContactItem contact;
104
						
105
			contact = new ContactItem (e_contact.FullName);
106
			
107
			if (e_contact.Email1 != null && e_contact.Email1 != "")
108
				contact.Emails.Add (e_contact.Email1);
109
			if (e_contact.Email2 != null && e_contact.Email2 != "")
110
				contact.Emails.Add (e_contact.Email2);
111
			if (e_contact.Email3 != null && e_contact.Email3 != "")
112
				contact.Emails.Add (e_contact.Email3);
113
			
114
			contact.AIMs.AddRange (e_contact.ImAim);
115
			contact.Jabbers.AddRange (e_contact.ImJabber);
116
			
117
			switch (e_contact.Photo.PhotoType) {
118
			case ContactPhotoType.Inlined:
119
				contact.Photo = Path.GetTempFileName () + ".jpg";
120
				try {
121
					File.WriteAllBytes (contact.Photo, e_contact.Photo.Data);
122
				} catch {
123
					contact.Photo = null;
124
				}
125
				break;
126
			case ContactPhotoType.Uri:
127
				if (File.Exists (e_contact.Photo.Uri)) {
128
					contact.Photo = e_contact.Photo.Uri;
129
				}
130
				break;
131
			}
132
			ContactItemStore.SynchronizeContactWithStore (ref contact);
133
			return contact;
134
		}
135
		
136
	}
137
}