~do-plugins/do-plugins/trunk

« back to all changes in this revision

Viewing changes to SimplePlugins/GmailContactItemSource.boo

  • Committer: Christopher James Halse Rogers
  • Date: 2008-04-03 22:32:04 UTC
  • mto: This revision was merged to the branch mainline in revision 87.
  • Revision ID: chalserogers@gmail.com-20080403223204-yh7zudzdzs3xwi7r
Revert deleted GNOME-Screenshot, GNOME-Terminal, GmailContactItemSource, and Tasque plugins

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// GmailContactItemSource.boo
 
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
namespace Google
 
21
 
 
22
import System
 
23
import System.IO
 
24
import System.Collections.Generic
 
25
 
 
26
import Do
 
27
import Do.Universe
 
28
 
 
29
class GmailContactItemSource (IItemSource):
 
30
 
 
31
  static final BeginName  = "FN:"
 
32
  static final BeginEmail = "EMAIL;TYPE=INTERNET:"
 
33
  static final EndVCard   = "END:VCARD"
 
34
  
 
35
  items as List[of IItem]
 
36
  
 
37
  def constructor ():
 
38
    items = List[of IItem] ()
 
39
  
 
40
  Name:
 
41
    get: return "Gmail Contacts"
 
42
 
 
43
  Description:
 
44
    get: return "Indexes your Gmail contacts."
 
45
 
 
46
  Icon:
 
47
    get: return "stock_person"
 
48
 
 
49
  SupportedItemTypes:
 
50
    get: return (typeof (ContactItem),)
 
51
  
 
52
  Items:
 
53
    get: return items
 
54
  
 
55
  def ChildrenOfItem (parent as IItem):
 
56
    return null
 
57
  
 
58
  def UpdateItems ():
 
59
    items.Clear ()
 
60
    for contact in LoadContacts ():
 
61
      items.Add (contact)
 
62
  
 
63
  def LoadContacts ():
 
64
    seen = {}
 
65
    file = Paths.Combine (Paths.ApplicationData, "contacts.vcf")
 
66
    try:
 
67
      using reader = File.OpenText (file):
 
68
        while line = reader.ReadLine ():
 
69
          # Read email and name fields in current vcard:
 
70
          while line and not line.StartsWith (EndVCard):
 
71
            if line.StartsWith (BeginName):
 
72
              name = line [BeginName.Length:]
 
73
            elif line.StartsWith (BeginEmail):
 
74
              email = line [BeginEmail.Length:]
 
75
            line = reader.ReadLine ()
 
76
          # Skip contact if no email found
 
77
          if not email: continue
 
78
          # Create and save the contact.
 
79
          if name:
 
80
            contact = ContactItem.Create (name)
 
81
            key = name
 
82
          else:
 
83
            contact = ContactItem.CreateWithEmail (email)
 
84
            key = email
 
85
          seen [key] = contact
 
86
          contact ["email.gmail"] = email
 
87
    except e:
 
88
      Console.Error.WriteLine (
 
89
        "Could not read Gmail contacts file ${file}: $ {e.Message}")
 
90
    return seen.Values