14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21
from gi.repository import Accounts, GLib, Signon
24
logger = logging.getLogger(__name__)
23
27
class CredentialsException(Exception):
25
29
"""Exception for credentials problems."""
66
71
self._join_main_loop()
73
# XXX Sometimes, the account fails to be enabled. This sleep seems to
74
# fix it but we haven't yet found the reason. --elopio - 2014-06-25
68
76
self._enable_evernote_service(account)
78
logger.info('Created the account with id: {}.'.format(account.id))
79
self._log_accounts_info()
72
82
def _create_account(self):
83
logger.debug('Creating the Evernote account.')
73
84
account = self._manager.create_account('evernote')
74
85
account.set_enabled(True)
75
86
account.store(self._on_account_created, None)
78
89
def _on_account_created(self, account, error, _):
81
self._main_loop.quit()
91
self._quit_main_loop_on_error(error, 'storing account')
93
def _quit_main_loop_on_error(self, error, step):
94
logger.error('Error {}.'.format(step))
96
self._main_loop.quit()
83
98
def _get_identity_info(self, user_name, password):
84
99
info = Signon.IdentityInfo.new()
90
105
def _set_credentials_id_to_account(
91
106
self, identity, id_, error, account_dict):
94
self._main_loop.quit()
108
self._quit_main_loop_on_error(
109
error, 'storing credentials with info')
111
logger.debug('Setting credentials to account.')
96
112
account = account_dict.get('account')
97
113
oauth_token = account_dict.get('oauth_token')
98
114
account.set_variant('CredentialsId', GLib.Variant('u', id_))
101
117
def _process_session(self, account, error, oauth_token):
104
self._main_loop.quit()
119
self._quit_main_loop_on_error(
120
error, 'setting credentials id to account')
122
logger.debug('Processing session.')
106
123
account_service = Accounts.AccountService.new(account, None)
107
124
auth_data = account_service.get_auth_data()
108
125
identity = auth_data.get_credentials_id()
120
137
def _on_login_processed(self, session, reply, error, userdata):
139
self._quit_main_loop_on_error(error, 'processing session')
124
self._main_loop.quit()
142
self._main_loop.quit()
126
144
def _enable_evernote_service(self, account):
145
logger.debug('Enabling evernote service.')
127
146
service = self._manager.get_service('evernote')
128
147
account.select_service(service)
129
148
account.set_enabled(True)
130
account.store(self._on_account_created, None)
149
account.store(self._on_service_enabled, None)
151
def _on_service_enabled(self, account, error, _):
153
self._quit_main_loop_on_error(error, 'enabling service')
155
def _log_accounts_info(self):
156
account_ids = self._manager.list()
157
logger.debug('Existing accounts: {}.'.format(account_ids))
158
for id_ in account_ids:
159
account = self._manager.get_account(id_)
160
self._log_account_info(account)
162
def _log_account_info(self, account):
163
logger.debug('Account info:')
164
logger.debug('id: {}'.format(account.id))
165
logger.debug('provider: {}'.format(account.get_provider_name()))
166
logger.debug('enabled: {}'.format(account.get_enabled()))
167
logger.debug('Account services:')
168
for service in account.list_services():
169
logger.debug('name: {}'.format(service.get_name()))
170
account_service = Accounts.AccountService.new(account, service)
171
logger.debug('enabled: {}'.format(account_service.get_enabled()))
132
173
def delete_account(self, account):
133
174
"""Delete an account.
135
176
:param account: The account to delete.
179
logger.info('Deleting the account with id {}.'.format(account.id))
138
180
self._start_main_loop()
140
182
account.store(self._on_account_deleted, None)
141
183
self._join_main_loop()
143
def _on_account_deleted(self, account, error, userdata):
185
def _on_account_deleted(self, account, error, _):
147
self._main_loop.quit()
187
self._quit_main_loop_on_error(error, 'deleting account')
189
self._main_loop.quit()