~cszikszoy/do-plugins/pastebin

« back to all changes in this revision

Viewing changes to Thunderbird/src/ThunderbirdContactItemSource.cs

  • Committer: djsiegel at gmail
  • Date: 2007-11-07 17:13:21 UTC
  • Revision ID: djsiegel@gmail.com-20071107171321-zgu46ukqlpdx1ypa
Initial files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  ThunderbirdContactItemSource.cs
 
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
// 
 
21
 
 
22
using System;
 
23
using System.IO;
 
24
using System.Collections;
 
25
using System.Collections.Generic;
 
26
 
 
27
using Do.Addins;
 
28
using Beagle.Util;
 
29
 
 
30
namespace Do.Universe
 
31
{
 
32
 
 
33
        public class ThunderbirdContactItemSource : IItemSource
 
34
        {
 
35
                
 
36
                const string BeginProfileName = "Path=";
 
37
                const string BeginDefaultProfile = "Name=default";
 
38
                
 
39
                List<IItem> contacts;
 
40
                
 
41
                public ThunderbirdContactItemSource ()
 
42
                {
 
43
                        contacts = new List<IItem> ();
 
44
                        UpdateItems ();
 
45
                }
 
46
                
 
47
                public Type[] SupportedItemTypes {
 
48
                        get {
 
49
                                return new Type[] {
 
50
                                        typeof (ContactItem),
 
51
                                };
 
52
                        }
 
53
                }
 
54
                
 
55
                public string Name { get { return "Thunderbird Contacts"; } }
 
56
                public string Description { get { return "Thunderbird Contacts"; } }
 
57
                public string Icon { get { return "thunderbird"; } }
 
58
                
 
59
                public void UpdateItems ()
 
60
                {
 
61
                        try {
 
62
                                _UpdateItems ();
 
63
                        } catch (Exception e) {
 
64
                                Console.WriteLine ("Cannot index Thunderbird contacts because a {0} was thrown: {1}", e.GetType (), e.Message);
 
65
                                return;
 
66
                        }
 
67
                }
 
68
                
 
69
                public ICollection<IItem> Items {
 
70
                        get { return contacts; }
 
71
                }
 
72
                
 
73
                public ICollection<IItem> ChildrenOfItem (IItem item)
 
74
                {
 
75
                        return null;
 
76
                }
 
77
                
 
78
                void _UpdateItems ()
 
79
                {
 
80
                        MorkDatabase database;
 
81
                
 
82
                        contacts.Clear ();
 
83
                        database = new MorkDatabase (GetThunderbirdAddressBookFilePath ());
 
84
                        database.Read ();
 
85
                        database.EnumNamespace = "ns:addrbk:db:row:scope:card:all";
 
86
 
 
87
                        foreach (string id in database) {
 
88
                                Hashtable contact_row;
 
89
                                ContactItem contact;
 
90
                                
 
91
                                contact_row = database.Compile (id, database.EnumNamespace);
 
92
                                contact = CreateThunderbirdContactItem (contact_row);
 
93
                                if (contact != null)
 
94
                                        contacts.Add (contact);
 
95
                        }
 
96
                }
 
97
        
 
98
                ContactItem CreateThunderbirdContactItem (Hashtable row) {
 
99
                        ContactItem contact;
 
100
                        string name, email;
 
101
                        
 
102
                        contact = new ContactItem ();
 
103
                        
 
104
//                      foreach (object o in row.Keys)
 
105
//                              Console.WriteLine ("\t{0} --> {1}", o, row[o]);
 
106
                        
 
107
                        // I think this will detect deleted contacts... Hmm...
 
108
                        if (row["table"] == null || row["table"] as string == "C6")
 
109
                                return null;
 
110
                        
 
111
                        // Name
 
112
                        name = row["DisplayName"] as string;
 
113
                        if (name == null || name == string.Empty)
 
114
                                name = string.Format ("{0} {1}", row["FirstName"], row["LastName"]);
 
115
                        contact.Name = name;
 
116
                        
 
117
                        // Email
 
118
                        email = row["PrimaryEmail"] as string;
 
119
                        if (email != null && email != string.Empty)
 
120
                                contact.Emails.Add (email);
 
121
                        
 
122
                        ContactItemStore.SynchronizeContactWithStore (ref contact);
 
123
                        return contact;
 
124
                }
 
125
                
 
126
                string GetThunderbirdAddressBookFilePath ()
 
127
                {
 
128
                        string home, path, profile;
 
129
                        StreamReader reader;
 
130
 
 
131
                        profile = null;
 
132
                        home = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);
 
133
                        path = System.IO.Path.Combine (home, ".mozilla-thunderbird/profiles.ini");
 
134
                        try {
 
135
                                reader = System.IO.File.OpenText (path);
 
136
                        } catch {
 
137
                                return null;
 
138
                        }
 
139
                        
 
140
                        bool got_default = false;
 
141
                        for (string line = reader.ReadLine (); line != null; line = reader.ReadLine ()) {
 
142
                                if (got_default && line.StartsWith (BeginProfileName)) {
 
143
                                        line = line.Trim ();
 
144
                                        line = line.Substring (BeginProfileName.Length);
 
145
                                        profile = line;
 
146
                                        break;
 
147
                                }
 
148
                                else if (line.StartsWith (BeginDefaultProfile)) {
 
149
                                        got_default = true;
 
150
                                }
 
151
                        }
 
152
                        reader.Close ();
 
153
                        
 
154
                        if (profile == null) {
 
155
                                return null;
 
156
                        }
 
157
                        path = System.IO.Path.Combine (home, ".mozilla-thunderbird");
 
158
                        path = System.IO.Path.Combine (path, profile);
 
159
                        path = System.IO.Path.Combine (path, "abook.mab");
 
160
                        return path;
 
161
                        
 
162
                }
 
163
                
 
164
        }
 
165
}