diff options
author | drebs <drebs@leap.se> | 2013-04-13 14:06:19 -0300 |
---|---|---|
committer | drebs <drebs@leap.se> | 2013-04-13 14:06:19 -0300 |
commit | ea7c0a8da1e823244700705ed5f175d56714c715 (patch) | |
tree | 70ceec9d951459f57667ea9dfe8ca317b24ded40 /src | |
parent | 7d63e687e4c3b6e4da4599a7e685dc5c44457a64 (diff) |
Add events signaling.
Diffstat (limited to 'src')
-rw-r--r-- | src/leap/soledad/__init__.py | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/src/leap/soledad/__init__.py b/src/leap/soledad/__init__.py index 38b1f772..9c7a728a 100644 --- a/src/leap/soledad/__init__.py +++ b/src/leap/soledad/__init__.py @@ -38,12 +38,14 @@ except ImportError: import json # noqa +from leap.common import events from leap.soledad.config import SoledadConfig from leap.soledad.util import GPGWrapper from leap.soledad.backends import sqlcipher from leap.soledad.backends.leap_backend import ( LeapDocument, DocumentNotEncrypted, + LeapSyncTarget, ) from leap.soledad.shared_db import SoledadSharedDatabase @@ -216,6 +218,7 @@ class Soledad(object): """ # TODO: write tests for methods below. # load/generate OpenPGP keypair + events.signal(events.SOLEDAD_CREATING_KEYS, self._user) if not self._has_privkey(): self._gen_privkey() self._load_privkey() @@ -223,6 +226,7 @@ class Soledad(object): if not self._has_symkey(): self._gen_symkey() self._load_symkey() + events.signal(events.SOLEDAD_DONE_CREATING_KEYS, self._user) def _init_db(self): """ @@ -468,10 +472,13 @@ class Soledad(object): @return: a document with encrypted key material in its contents @rtype: LeapDocument """ + events.signal(events.SOLEDAD_DOWNLOADING_KEYS, self._user) # TODO: change below to raise appropriate exceptions if not self._shared_db: return None - return self._shared_db.get_doc_unauth(self._user_hash()) + doc = self._shared_db.get_doc_unauth(self._user_hash()) + events.signal(events.SOLEDAD_DONE_DOWNLOADING_KEYS, self._user) + return doc def _assert_server_keys(self): """ @@ -491,6 +498,7 @@ class Soledad(object): assert result.fingerprints[0] == self._fingerprint assert remote_symkey == self._symkey else: + events.signal(events.SOLEDAD_UPLOADING_KEYS, self._user) privkey = self._gpg.export_keys(self._fingerprint, secret=True) content = { '_privkey': self.encrypt(privkey, @@ -502,6 +510,7 @@ class Soledad(object): doc = LeapDocument(doc_id=self._user_hash(), soledad=self) doc.content = content self._shared_db.put_doc(doc) + events.signal(events.SOLEDAD_DONE_UPLOADING_KEYS, self._user) def _assert_remote_keys(self): privkey, symkey = self._retrieve_keys() @@ -742,7 +751,29 @@ class Soledad(object): @rtype: str """ # TODO: create authentication scheme for sync with server. - return self._db.sync(url, creds=None, autocreate=True) + local_gen = self._db.sync(url, creds=None, autocreate=True) + events.signal(events.SOLEDAD_DONE_DATA_SYNC, self._user) + return local_gen + + def need_sync(self, url): + """ + Return if local db replica differs from remote url's replica. + + @param url: The remote replica to compare with local replica. + @type url: str + + @return: Whether remote replica and local replica differ. + @rtype: bool + """ + # TODO: create auth scheme for sync with server + target = LeapSyncTarget(url, creds=None, soledad=self) + info = target.get_sync_info(self._db._get_replica_uid()) + # compare source generation with target's last known source generation + if self._db._get_generation() != info[4]: + events.signal(events.SOLEDAD_NEW_DATA_TO_SYNC, self._user) + return True + return False + #------------------------------------------------------------------------- # Recovery document export and import |