Ready for mentors
[cgmail.git] / cGmail / lib / keyringdicthelper.py
blobfe1f798d63dd112c80c882c7f3e694ba724883ab
2 # Copyright (C) 2007 Marco Ferragina <marco.ferragina@gmail.com>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 import gconf
20 import gnomekeyring
21 from ConfigParser import MissingSectionHeaderError
23 from dicthelper import DictHelper, CannotSaveError, CorruptedConfiguration
24 from storagehandler import StorageHandler
26 GCONF_AUTH_KEY = "/apps/cgmail/keyring_auth_token"
28 class KeyringHandler(StorageHandler):
29 def __init__(self):
30 self.__data = self.__read()
31 self.__lines = self.__data.split('\n')
32 self.__count = 0
33 self.__to_write = ""
35 def __read(self):
36 keyring = "default"
37 secret = ""
38 try:
39 gnomekeyring.create_sync(keyring, None)
40 except gnomekeyring.AlreadyExistsError:
41 pass
43 auth_token = gconf.client_get_default().get_int(GCONF_AUTH_KEY)
44 if auth_token > 0:
45 try:
46 secret = gnomekeyring.item_get_info_sync(keyring,
47 auth_token).get_secret()
48 except gnomekeyring.DeniedError:
49 auth_token = 0
50 return secret
52 def readline(self):
53 if self.__count < len(self.__lines):
54 line = self.__lines[self.__count]
55 self.__count += 1
56 if not line:
57 return "\n"
58 return line
59 else:
60 return None
62 def get_new_writer(self):
63 self.__to_write = ""
64 self.__store()
65 return self
67 def write(self, value):
68 self.__to_write += value
70 def __store(self):
71 keyring = "default"
72 try:
73 gnomekeyring.create_sync(keyring, None)
74 except gnomekeyring.AlreadyExistsError:
75 pass
77 try:
78 auth_token = gnomekeyring.item_create_sync(
79 keyring,
80 gnomekeyring.ITEM_GENERIC_SECRET,
81 "cgmail checker login informations",
82 dict(appname="cgmail, gmail checker"),
83 self.__to_write, True)
84 gconf.client_get_default().set_int(GCONF_AUTH_KEY, auth_token)
85 except:
86 raise CannotSaveError()
88 def close(self):
89 self.__store()
93 class KeyringDictHelper(DictHelper):
95 def __init__(self):
96 keyring_handler = KeyringHandler()
97 try:
98 DictHelper.__init__(self, keyring_handler)
99 except MissingSectionHeaderError:
100 keyring_handler = keyring_handler.get_new_writer()
101 raise CorruptedConfiguration
105 if __name__ == "__main__":
106 import gtk
107 k = KeyringDictHelper()
108 # k.add_account({"username": "tesiot", "type": "gmail"})
109 # k.remove_account(2)
110 print k.get_dicts()