~haakan/do-plugins/sshfix

« back to all changes in this revision

Viewing changes to Thunderbird/src/ThunderbirdContactItemSource.cs

  • Committer: Jason Smith
  • Date: 2008-12-24 04:37:17 UTC
  • mto: This revision was merged to the branch mainline in revision 337.
  • Revision ID: jassmith@gmail.com-20081224043717-9yq3uhajlmnyyg5k
Merge community into official

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