~cszikszoy/do-plugins/pastebin

« back to all changes in this revision

Viewing changes to Pidgin/src/PidginContactItemSource.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
//  PidginContactItemSource.cs
 
2
//
 
3
//  GNOME Do is the legal property of its developers, whose names are too numerous
 
4
//  to list here.  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.Xml;
 
23
using System.Collections.Generic;
 
24
 
 
25
using Do.Addins;
 
26
 
 
27
namespace Do.Universe
 
28
{
 
29
        public class PidginContactItemSource : IItemSource
 
30
        {
 
31
 
 
32
                static readonly string kBuddyListFile;
 
33
                static readonly string kBuddyIconDirectory;
 
34
                
 
35
                static PidginContactItemSource () {
 
36
                        string home;
 
37
                        
 
38
                        home =  Environment.GetFolderPath (Environment.SpecialFolder.Personal);
 
39
                        kBuddyListFile = "~/.purple/blist.xml".Replace("~", home);
 
40
                        kBuddyIconDirectory = "~/.purple/icons".Replace("~", home);
 
41
                }
 
42
                
 
43
                List<IItem> buddies;
 
44
                
 
45
                public PidginContactItemSource ()
 
46
                {
 
47
                        buddies = new List<IItem> ();
 
48
                        UpdateItems ();
 
49
                }
 
50
                
 
51
                public Type[] SupportedItemTypes {
 
52
                        get {
 
53
                                return new Type[] {
 
54
                                        typeof (ContactItem),
 
55
                                };
 
56
                        }
 
57
                }
 
58
                
 
59
                public string Name { get { return "Pidgin Buddies"; } }
 
60
                public string Description { get { return "Buddies on your Pidgin buddy list."; } }
 
61
                public string Icon {get { return "pidgin"; } }
 
62
                
 
63
                public ICollection<IItem> Items {
 
64
                        get { return buddies; }
 
65
                }
 
66
                
 
67
                public ICollection<IItem> ChildrenOfItem (IItem item)
 
68
                {
 
69
                        return null;
 
70
                }
 
71
                
 
72
                public void UpdateItems ()
 
73
                {
 
74
                        XmlDocument blist;
 
75
                        // Add buddies as they are encountered to this hash so we don't create duplicates.
 
76
                        Dictionary<ContactItem, bool> buddies_seen;
 
77
                        
 
78
                        buddies.Clear ();
 
79
                        buddies_seen = new Dictionary<ContactItem, bool> ();
 
80
                        blist = new XmlDocument ();
 
81
                        try {
 
82
                                blist.Load (kBuddyListFile);
 
83
 
 
84
                                foreach (XmlNode contact_node in blist.GetElementsByTagName ("contact"))
 
85
                                foreach (XmlNode buddy_node in contact_node.ChildNodes) {
 
86
                                        ContactItem buddy;              
 
87
                                        
 
88
                                        buddy = ContactItemFromBuddyXmlNode (buddy_node);
 
89
                                        if (buddy == null) continue;
 
90
                                        ContactItemStore.SynchronizeContactWithStore (ref buddy);
 
91
                                        buddies_seen[buddy] = true;
 
92
                                }
 
93
                                
 
94
                        } catch (Exception e) {
 
95
                                Console.Error.WriteLine ("Could not read Pidgin buddy list file: " + e.Message);
 
96
                        }
 
97
                        foreach (ContactItem buddy in buddies_seen.Keys) {
 
98
                                buddies.Add (buddy);
 
99
                        }
 
100
                }
 
101
                
 
102
                ContactItem ContactItemFromBuddyXmlNode (XmlNode buddy_node)
 
103
                {
 
104
                        ContactItem buddy;
 
105
                        string proto, name, alias, icon;
 
106
                        
 
107
                        try {
 
108
                                name = alias = icon = null;
 
109
                                // The messaging protocol (e.g. "prpl-jabber" for Jabber).
 
110
                                proto = buddy_node.Attributes.GetNamedItem ("proto").Value;
 
111
                                foreach (XmlNode attr in buddy_node.ChildNodes) {
 
112
                                        switch (attr.Name) {
 
113
                                        // The screen name.
 
114
                                        case "name":
 
115
                                                name = attr.InnerText;
 
116
                                                break;
 
117
                                        // The alias, or real name.
 
118
                                        case "alias":
 
119
                                                alias = attr.InnerText;
 
120
                                                break;
 
121
                                        // Buddy icon image file.
 
122
                                        case "setting":
 
123
                                                if (attr.Attributes.GetNamedItem ("name").Value == "buddy_icon") {
 
124
                                                        icon = Path.Combine (kBuddyIconDirectory, attr.InnerText);
 
125
                                                }
 
126
                                                break;
 
127
                                        }
 
128
                                }
 
129
                        } catch {
 
130
                                // Bad buddy.
 
131
                                return null;
 
132
                        }
 
133
                        // If crucial details are missing, we can't make a buddy.
 
134
                        if (name == null || proto == null) return null;
 
135
                        
 
136
                        // Create a new buddy, add the details we have.
 
137
                        buddy = new ContactItem ();
 
138
                        if (alias != null)
 
139
                                        buddy.Name = alias;
 
140
                        if (icon != null)
 
141
                                        buddy.Photo = icon;
 
142
                        AddScreenNameToContact (buddy, proto, name);
 
143
                        return buddy;
 
144
                }
 
145
                
 
146
                void AddScreenNameToContact (ContactItem buddy, string proto, string name)
 
147
                {
 
148
                        if (buddy == null || proto == null || name == null)
 
149
                                return;
 
150
                        
 
151
                        switch (proto) {
 
152
                        case "prpl-aim":
 
153
                                if (!buddy.AIMs.Contains (name))
 
154
                                        buddy.AIMs.Add (name);
 
155
                                break;
 
156
                        case "prpl-jabber":
 
157
                                if (!buddy.Jabbers.Contains (name))
 
158
                                        buddy.Jabbers.Add (name);
 
159
                                break;
 
160
                        }
 
161
                }
 
162
        }
 
163
}