~jhonnyc/cgmail/jonathanc-branch

« back to all changes in this revision

Viewing changes to src/keyringaccounts.py

  • Committer: Marco Ferragina
  • Date: 2007-05-06 15:51:12 UTC
  • Revision ID: marco.ferragina@gmail.com-20070506155112-874uk2m8blrknyuf
Restructured package source dir. Now is much more organized. Implemented right click menu on account window treeview

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
import gconf
3
 
import gnomekeyring
4
 
 
5
 
from baseaccountmanager import BaseAccountManager, CannotSaveError
6
 
 
7
 
GCONF_AUTH_KEY = "/apps/cgmail/keyring_auth_token"
8
 
 
9
 
class KeyringAccounts(BaseAccountManager):
10
 
        
11
 
        def __init__(self):
12
 
                self.accounts_dicts = []
13
 
                try:
14
 
                        self.__retrieve_accounts_dicts()
15
 
                except ValueError:
16
 
                        # 
17
 
                        pass
18
 
 
19
 
        def get_accounts_dicts(self):
20
 
                return self.accounts_dicts
21
 
 
22
 
        def __retrieve_accounts_dicts(self): 
23
 
                keyring = "default"  
24
 
                try:  
25
 
                        gnomekeyring.create_sync(keyring, None)  
26
 
                except gnomekeyring.AlreadyExistsError:  
27
 
                        pass 
28
 
        
29
 
                auth_token = gconf.client_get_default().get_int(GCONF_AUTH_KEY)
30
 
                if auth_token > 0:
31
 
                        try:
32
 
                                secret = gnomekeyring.item_get_info_sync(keyring, 
33
 
                                                        auth_token).get_secret()
34
 
                                accounts = secret.split("\n")
35
 
                                for account in accounts:
36
 
                                        dict = {}
37
 
                                        params = account.split(";")
38
 
                                        for element in params:
39
 
                                                #param, value = element.split("=")
40
 
                                                couple = element.split()
41
 
                                                if len(couple) == 2:
42
 
                                                        param = couple[0]
43
 
                                                        value = couple[1]
44
 
                                                else:
45
 
                                                        param = couple[0]
46
 
                                                        value = ""
47
 
                                                dict[param] = value
48
 
                                        self.accounts_dicts.append(dict)
49
 
                        except gnomekeyring.DeniedError:
50
 
                                auth_token = 0
51
 
 
52
 
        def remove_account(self, id): 
53
 
                i = 0
54
 
                id = str(id)
55
 
                for a in self.accounts_dicts:
56
 
                        if a["id"] == id:
57
 
                                break
58
 
                        i += 1
59
 
                del self.accounts_dicts[i]
60
 
 
61
 
                self.__store()
62
 
 
63
 
        def add_account(self, dic, force_id = None):
64
 
                if force_id is None:
65
 
                        maxid = 0
66
 
                        for d in self.accounts_dicts:
67
 
                                if int(d["id"]) > maxid:
68
 
                                        maxid = int(d["id"])
69
 
 
70
 
                        dic["id"] = str(maxid + 1)
71
 
                else:
72
 
                        dic["id"] = force_id
73
 
 
74
 
                self.accounts_dicts.append(dic)
75
 
                self.__store()
76
 
 
77
 
                return dic["id"]
78
 
        
79
 
        def reset(self):
80
 
                self.accounts_dicts = []
81
 
                self.__store()
82
 
        
83
 
        def __store(self):
84
 
                keyring = "default"
85
 
                try:
86
 
                        gnomekeyring.create_sync(keyring, None)
87
 
                except gnomekeyring.AlreadyExistsError:
88
 
                        pass
89
 
                
90
 
                key = ""
91
 
 
92
 
                for dic in self.accounts_dicts:
93
 
                        for param, value in dic.iteritems():
94
 
                                if value.find(" ") != -1 or value.find(";") != -1 \
95
 
                                                or value.find("\n") != -1:
96
 
                                        print "Warning: reserved chars founded on ", value
97
 
                                        print "You cannot use '=' or ';' or '\\n'"
98
 
                                        print "trying to strip it"
99
 
                                        value.replace("=", "").replace(";", "").replace("\n", "")
100
 
                                key += param + " " + value + ";"
101
 
 
102
 
                        # remove last ";"
103
 
                        key = key[:len(key) - 1]
104
 
                        key += "\n"
105
 
                # remove last "\n"
106
 
                key = key[:len(key) - 1]
107
 
 
108
 
                try:
109
 
                        auth_token = gnomekeyring.item_create_sync(
110
 
                                keyring,
111
 
                                gnomekeyring.ITEM_GENERIC_SECRET,
112
 
                                "cgmail checker login informations",
113
 
                                dict(appname="cgmail, gmail checker"),
114
 
                                key, True)
115
 
                        gconf.client_get_default().set_int(GCONF_AUTH_KEY, auth_token)
116
 
                except:
117
 
                        raise CannotSaveError()
118
 
 
119
 
 
120
 
if __name__ == "__main__":
121
 
        import gtk
122
 
        k = KeyringAccounts()
123
 
#       k.add_account({"username": "tesiot", "type": "gmail"})
124
 
#       k.remove_account(2)
125
 
        print k.get_accounts_dicts()