1
/* ClawsContactsItemSource.cs
3
* GNOME Do is the legal property of its developers. Please refer to the
4
* COPYRIGHT file distributed with this
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.
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.
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/>.
24
using System.Collections.Generic;
35
/// Items source for Claws Mail - indexes Claws contacts saved in ~/.claws/addrbook/addrbook-*.xml.
37
public class ClawsContactsItemSource: ItemSource {
39
readonly static string ClawsHome = ".claws-mail/addrbook";
40
readonly static string ClawsAddrBookIndex = "addrbook--index.xml";
42
readonly static string ClawsKeyPrefix = "email.claws.";
43
public readonly static string ClawsPrimaryEmailPrefix = ClawsKeyPrefix + "0.";
45
readonly static string ClawsHomePath;
46
readonly static string ClawsIndexFilename;
50
#region std properties
52
public override string Name {
53
get { return Catalog.GetString ("ClawsMail contacts"); }
56
public override string Description {
57
get { return Catalog.GetString ("Contacts in ClawsMail address book"); }
60
public override string Icon {
61
get { return "claws-mail"; }
64
public override IEnumerable<Type> SupportedItemTypes {
65
get { yield return typeof (ContactItem); }
68
public override IEnumerable<Item> Items {
75
static ClawsContactsItemSource ()
77
ClawsHomePath = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.Personal),
79
ClawsIndexFilename = Path.Combine (ClawsHomePath, ClawsAddrBookIndex);
83
public ClawsContactsItemSource ()
85
items = new List<Item> ();
89
public override IEnumerable<Item> ChildrenOfItem (Item item)
91
ContactItem contact = item as ContactItem;
92
foreach (string detail in contact.Details) {
93
if (detail.StartsWith (ClawsKeyPrefix)) {
94
yield return new ClawsContactDetailItem (detail, contact[detail]);
100
public override void UpdateItems ()
104
// iterate over address book files
105
foreach (string addressBook in AddressBookFiles) {
107
XmlDocument xmldoc = new XmlDocument ();
108
// read adress book by StreamReader. Without: error:Encoding name '...' not supported
109
using (StreamReader reader = new StreamReader (addressBook)) {
110
xmldoc.Load (reader);
113
XmlNodeList people = xmldoc.SelectNodes ("/address-book/person");
114
if (people == null) {
118
foreach (XmlNode person in people) {
119
// contact name from "cn" attribute
120
string personCn = person.Attributes ["cn"].InnerText;
121
if (string.IsNullOrEmpty (personCn)) {
126
XmlNodeList addresses = person.SelectNodes ("address-list/address");
127
if ((addresses == null) || (addresses.Count == 0)) { // no childs == no emails -> skip
131
ContactItem buddy = ContactItem.CreateWithName (personCn);
132
int emailCounter = 0;
134
foreach (XmlNode address in addresses) {
135
string email = address.Attributes ["email"].InnerText;
136
if (!string.IsNullOrEmpty (email)) {
137
string remarks = address.Attributes ["remarks"].InnerText;
138
string id = ClawsKeyPrefix + emailCounter + "." + remarks;
144
if (emailCounter > 0) {
149
} catch (Exception e) {
150
Log.Error ("ClawsContactsItemSource: file:{0} error:{1}", addressBook, e.Message);
151
Log.Debug ("ClawsContactsItemSource: file:{0}: {1}", addressBook, e.ToString ());
158
/// Get address book files from Claws config.
161
/// List of file names.<see cref="List`1"/>
163
private static List<string> AddressBookFiles {
165
List<string> result = new List<string> ();
167
if (!File.Exists (ClawsIndexFilename)) {
172
// open $HOME/.claws-mail/addrbook--index.xml
173
XmlDocument xmldoc = new XmlDocument ();
174
xmldoc.Load (ClawsIndexFilename);
175
XmlNodeList books = xmldoc.SelectNodes ("/addressbook/book_list/book");
180
foreach (XmlNode book in books) {
181
string fileName = book.Attributes ["file"].InnerText;
182
if (String.IsNullOrEmpty (fileName)) {
186
string filePath = Path.Combine (ClawsHomePath, fileName);
187
if (File.Exists (filePath)) {
188
result.Add (filePath);
191
} catch (Exception e) {
192
Log.Error("ClawsContactsItemSource.AddressBookFiles error: {0}", e.Message);
193
Log.Debug("ClawsContactsItemSource.AddressBookFiles: {0}", e.ToString ());