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
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
):
30 self
.__data
= self
.__read
()
31 self
.__lines
= self
.__data
.split('\n')
39 gnomekeyring
.create_sync(keyring
, None)
40 except gnomekeyring
.AlreadyExistsError
:
43 auth_token
= gconf
.client_get_default().get_int(GCONF_AUTH_KEY
)
46 secret
= gnomekeyring
.item_get_info_sync(keyring
,
47 auth_token
).get_secret()
48 except gnomekeyring
.DeniedError
:
53 if self
.__count
< len(self
.__lines
):
54 line
= self
.__lines
[self
.__count
]
62 def get_new_writer(self
):
67 def write(self
, value
):
68 self
.__to
_write
+= value
73 gnomekeyring
.create_sync(keyring
, None)
74 except gnomekeyring
.AlreadyExistsError
:
78 auth_token
= gnomekeyring
.item_create_sync(
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
)
86 raise CannotSaveError()
93 class KeyringDictHelper(DictHelper
):
96 keyring_handler
= KeyringHandler()
98 DictHelper
.__init
__(self
, keyring_handler
)
99 except MissingSectionHeaderError
:
100 keyring_handler
= keyring_handler
.get_new_writer()
101 raise CorruptedConfiguration
105 if __name__
== "__main__":
107 k
= KeyringDictHelper()
108 # k.add_account({"username": "tesiot", "type": "gmail"})
109 # k.remove_account(2)