~cszikszoy/do-plugins/fix-confluence

« back to all changes in this revision

Viewing changes to ClawsMail/src/ClawsContactsItemSource.cs

  • Committer: David Siegel
  • Date: 2009-01-07 19:40:53 UTC
  • mfrom: (407.1.4 trunk)
  • Revision ID: david@x-20090107194053-wseaq4rkw4y12bka
Merge.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ClawsContactsItemSource.cs 
 
2
 *
 
3
 * GNOME Do is the legal property of its developers. Please refer to the
 
4
 * 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
 
 
21
 
 
22
using System;
 
23
using System.IO;
 
24
using System.Collections.Generic;
 
25
using System.Xml;
 
26
using Mono.Unix;
 
27
using Do.Universe;
 
28
using Do.Platform;
 
29
 
 
30
 
 
31
namespace Claws
 
32
{
 
33
 
 
34
        /// <summary>
 
35
        /// Items source for Claws Mail - indexes Claws contacts saved in ~/.claws/addrbook/addrbook-*.xml. 
 
36
        /// </summary>
 
37
        public class ClawsContactsItemSource: ItemSource {
 
38
 
 
39
                readonly static string ClawsHome = ".claws-mail/addrbook";
 
40
                readonly static string ClawsAddrBookIndex = "addrbook--index.xml";
 
41
                
 
42
                readonly static string ClawsKeyPrefix = "email.claws.";
 
43
                public readonly static string ClawsPrimaryEmailPrefix = ClawsKeyPrefix + "0.";
 
44
 
 
45
                readonly static string ClawsHomePath;
 
46
                readonly static string ClawsIndexFilename;
 
47
                
 
48
                List<Item> items;
 
49
 
 
50
                #region std properties
 
51
                
 
52
                public override string Name {
 
53
                        get { return Catalog.GetString ("ClawsMail contacts"); }
 
54
                }
 
55
 
 
56
                public override string Description {
 
57
                        get { return Catalog.GetString ("Contacts in ClawsMail address book"); }
 
58
                }
 
59
 
 
60
                public override string Icon {
 
61
                        get { return "claws-mail"; }
 
62
                }
 
63
 
 
64
                public override IEnumerable<Type> SupportedItemTypes {
 
65
                        get { yield return typeof (ContactItem); }
 
66
                }
 
67
 
 
68
                public override IEnumerable<Item> Items {
 
69
                        get { return items; }
 
70
                }
 
71
 
 
72
                #endregion
 
73
 
 
74
 
 
75
                static ClawsContactsItemSource ()
 
76
                {
 
77
                        ClawsHomePath =  Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.Personal), 
 
78
                                                                 ClawsHome);
 
79
                        ClawsIndexFilename = Path.Combine (ClawsHomePath, ClawsAddrBookIndex);
 
80
                }
 
81
                
 
82
                
 
83
                public ClawsContactsItemSource () 
 
84
                {
 
85
                        items = new List<Item> ();
 
86
                }
 
87
                
 
88
                
 
89
                public override IEnumerable<Item> ChildrenOfItem (Item item)
 
90
                {
 
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]);
 
95
                                }
 
96
                        }
 
97
                }
 
98
 
 
99
                
 
100
                public override void UpdateItems () 
 
101
                {
 
102
                        items.Clear ();
 
103
 
 
104
                        // iterate over address book files 
 
105
                        foreach (string addressBook in AddressBookFiles) {                              
 
106
                                try {
 
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);
 
111
                                        }
 
112
                                        
 
113
                                        XmlNodeList people = xmldoc.SelectNodes ("/address-book/person");
 
114
                                        if (people == null) {
 
115
                                                continue;
 
116
                                        }
 
117
                                        
 
118
                                        foreach (XmlNode person in people) {                                            
 
119
                                                // contact name from "cn" attribute
 
120
                                                string personCn = person.Attributes ["cn"].InnerText;
 
121
                                                if (string.IsNullOrEmpty (personCn)) {
 
122
                                                        continue;
 
123
                                                }
 
124
                                                
 
125
                                                // load emails
 
126
                                                XmlNodeList addresses = person.SelectNodes ("address-list/address");                                            
 
127
                                                if ((addresses == null) || (addresses.Count == 0)) { // no childs == no emails -> skip
 
128
                                                        continue;
 
129
                                                }
 
130
 
 
131
                                                ContactItem buddy = ContactItem.CreateWithName (personCn);
 
132
                                                int emailCounter = 0;                                           
 
133
                                                
 
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;
 
139
                                                                buddy [id] = email;
 
140
                                                                emailCounter++;
 
141
                                                        }
 
142
                                                }
 
143
                                                
 
144
                                                if (emailCounter > 0) {
 
145
                                                        items.Add (buddy);
 
146
                                                }
 
147
                                        }                       
 
148
 
 
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 ());
 
152
                                }
 
153
                        }
 
154
                }
 
155
 
 
156
 
 
157
                /// <summary>
 
158
                /// Get address book files from Claws config.
 
159
                /// </summary>
 
160
                /// <returns>
 
161
                /// List of file names.<see cref="List`1"/>
 
162
                /// </returns>
 
163
                private static List<string> AddressBookFiles {
 
164
                        get {
 
165
                                List<string> result = new List<string> ();
 
166
        
 
167
                                if (!File.Exists (ClawsIndexFilename)) {
 
168
                                        return result;
 
169
                                }
 
170
                                
 
171
                                try {
 
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");
 
176
                                        if (books == null) {
 
177
                                                return result;
 
178
                                        }
 
179
                                        
 
180
                                        foreach (XmlNode book in books) {
 
181
                                                string fileName = book.Attributes ["file"].InnerText;
 
182
                                                if (String.IsNullOrEmpty (fileName)) {
 
183
                                                        continue;
 
184
                                                }
 
185
        
 
186
                                                string filePath = Path.Combine (ClawsHomePath, fileName);
 
187
                                                if (File.Exists (filePath)) {
 
188
                                                        result.Add (filePath);
 
189
                                                }
 
190
                                        }                               
 
191
                                } catch (Exception e) {                                 
 
192
                                        Log.Error("ClawsContactsItemSource.AddressBookFiles error: {0}", e.Message);
 
193
                                        Log.Debug("ClawsContactsItemSource.AddressBookFiles: {0}", e.ToString ());
 
194
                                }
 
195
                                
 
196
                                return result;
 
197
                        }
 
198
                }
 
199
 
 
200
        }
 
201
}