def __pull_passwords(self, bulk_keys): """ Pull from passwords @param bulk_keys as KeyBundle @raise StopIteration """ Logger.sync_debug("pull passwords") records = self.__firefox_sync.get_records("passwords", bulk_keys) for record in records: self.__check_worker() if record["modified"] < self.__mtimes["passwords"]: continue sleep(0.01) Logger.sync_debug("pulling %s", record) password = record["payload"] password_id = password["id"].strip("{}") if "formSubmitURL" in password.keys(): self.__helper.clear(password_id, self.__helper.store, password["usernameField"], password["username"], password["passwordField"], password["password"], password["hostname"], password["formSubmitURL"], password_id, None) elif "deleted" in password.keys(): # We assume True self.__helper.clear(password_id)
def __remove_from_passwords(self, uuid): """ Remove password from passwords collection @param uuid as str """ try: record = {} record["id"] = uuid record["deleted"] = True self.__pending_records["passwords"].append(record) self.__sync_pendings() except Exception as e: Logger.sync_debug("SyncWorker::__remove_from_passwords(): %s", e)
def __remove_from_bookmarks(self, guid): """ Remove from history @param guid as str """ try: record = {} record["id"] = guid record["type"] = "bookmark" record["deleted"] = True self.__pending_records["bookmarks"].append(record) self.__sync_pendings() except Exception as e: Logger.sync_debug("SyncWorker::__remove_from_bookmarks(): %s", e)
def __sync_pendings(self): """ Sync pendings record """ if Gio.NetworkMonitor.get_default().get_network_available() and\ self.__username and self.__password and not self.syncing: self.__syncing = True self.__check_worker() bulk_keys = self.__get_session_bulk_keys() for key in self.__pending_records.keys(): while self.__pending_records[key]: try: record = self.__pending_records[key].pop(0) Logger.sync_debug("syncing %s", record) self.__firefox_sync.add(record, key, bulk_keys) except: self.__pending_records[key].append(record) self.__syncing = False self.__update_state()
def __pull_history(self, bulk_keys): """ Pull from history @param bulk_keys as KeyBundle @raise StopIteration """ Logger.sync_debug("pull history") records = self.__firefox_sync.get_records("history", bulk_keys) for record in records: self.__check_worker() if record["modified"] < self.__mtimes["history"]: continue sleep(0.01) history = record["payload"] keys = history.keys() history_id = App().history.get_id_by_guid(history["id"]) # Check we have a valid history item if "histUri" in keys and\ "title" in keys and\ history["title"] and\ App().history.get_mtime(history_id) < record["modified"]: # Try to get visit date atimes = [] try: for visit in history["visits"]: atimes.append(round(int(visit["date"]) / 1000000, 2)) except: continue Logger.sync_debug("pulling %s", record) title = history["title"].rstrip().lstrip() history_id = App().history.add(title, history["histUri"], record["modified"], history["id"], atimes, True) elif "deleted" in keys: history_id = App().history.get_id_by_guid(history_id) App().history.remove(history_id)
def __pull_bookmarks(self, bulk_keys): """ Pull from bookmarks @param bulk_keys as KeyBundle @raise StopIteration """ Logger.sync_debug("pull bookmarks") SqlCursor.add(App().bookmarks) records = self.__firefox_sync.get_records("bookmarks", bulk_keys) children_array = [] for record in records: self.__check_worker() if record["modified"] < self.__mtimes["bookmarks"]: continue sleep(0.01) bookmark = record["payload"] bookmark_id = App().bookmarks.get_id_by_guid(bookmark["id"]) # Nothing to apply, continue if App().bookmarks.get_mtime(bookmark_id) >= record["modified"]: continue Logger.sync_debug("pulling %s", record) # Deleted bookmark if "deleted" in bookmark.keys(): App().bookmarks.remove(bookmark_id) # Keep folder only for firefox compatiblity elif "type" in bookmark.keys() and bookmark["type"] == "folder"\ and bookmark["id"] is not None\ and bookmark["title"]: if bookmark_id is None: bookmark_id = App().bookmarks.add(bookmark["title"], bookmark["id"], bookmark["id"], [], 0) # Will calculate position later if "children" in bookmark.keys(): children_array.append(bookmark["children"]) # We have a bookmark, add it elif "type" in bookmark.keys() and bookmark["type"] == "bookmark"\ and bookmark["id"] is not None\ and bookmark["title"]: # Add a new bookmark if bookmark_id is None: # Use parent name if no bookmarks tags if "tags" not in bookmark.keys() or\ not bookmark["tags"]: if "parentName" in bookmark.keys() and\ bookmark["parentName"]: bookmark["tags"] = [bookmark["parentName"]] else: bookmark["tags"] = [] bookmark_id = App().bookmarks.add(bookmark["title"], bookmark["bmkUri"], bookmark["id"], bookmark["tags"], 0) # Update bookmark else: App().bookmarks.set_title(bookmark_id, bookmark["title"]) App().bookmarks.set_uri(bookmark_id, bookmark["bmkUri"]) # Update tags current_tags = App().bookmarks.get_tags(bookmark_id) for tag in App().bookmarks.get_tags(bookmark_id): if "tags" in bookmark.keys() and\ tag not in bookmark["tags"]: tag_id = App().bookmarks.get_tag_id(tag) current_tags.remove(tag) App().bookmarks.del_tag_from(tag_id, bookmark_id) if "tags" in bookmark.keys(): for tag in bookmark["tags"]: # Tag already associated if tag in current_tags: continue tag_id = App().bookmarks.get_tag_id(tag) if tag_id is None: tag_id = App().bookmarks.add_tag(tag) App().bookmarks.add_tag_to(tag_id, bookmark_id) # Update parent name if available if bookmark_id is not None and "parentName" in bookmark.keys(): App().bookmarks.set_parent(bookmark_id, bookmark["parentid"], bookmark["parentName"]) App().bookmarks.set_mtime(bookmark_id, record["modified"]) # Update bookmark position for children in children_array: position = 0 for child in children: bid = App().bookmarks.get_id_by_guid(child) App().bookmarks.set_position(bid, position) position += 1 App().bookmarks.clean_tags() # Will commit SqlCursor.remove(App().bookmarks)
def __sync(self): """ Sync Eolie objects (bookmarks, history, ...) with Firefox Sync """ Logger.sync_debug("Start syncing") self.__syncing = True self.__sync_cancellable.reset() try: self.__mtimes = load( open(EOLIE_DATA_PATH + "/firefox_sync.bin", "rb")) except: self.__mtimes = { "bookmarks": 0.1, "history": 0.1, "passwords": 0.1 } try: self.__check_worker() bulk_keys = self.__get_session_bulk_keys() new_mtimes = self.__firefox_sync.client.info_collections() self.__check_worker() ######################## # Passwords Management # ######################## try: Logger.sync_debug("local passwords: %s, remote passwords: %s", self.__mtimes["passwords"], new_mtimes["passwords"]) # Only pull if something new available if self.__mtimes["passwords"] != new_mtimes["passwords"]: self.__pull_passwords(bulk_keys) except: pass # No passwords in sync self.__check_worker() ###################### # History Management # ###################### try: Logger.sync_debug("local history: %s, remote history: %s", self.__mtimes["history"], new_mtimes["history"]) # Only pull if something new available if self.__mtimes["history"] != new_mtimes["history"]: self.__pull_history(bulk_keys) except: pass self.__check_worker() ######################## # Bookmarks Management # ######################## try: Logger.sync_debug("local bookmarks: %s, remote bookmarks: %s", self.__mtimes["bookmarks"], new_mtimes["bookmarks"]) # Only pull if something new available if self.__mtimes["bookmarks"] != new_mtimes["bookmarks"]: self.__pull_bookmarks(bulk_keys) except: pass self.__update_state() Logger.sync_debug("Stop syncing") except Exception as e: Logger.error("SyncWorker::__sync(): %s", e) if str(e) == "The authentication token could not be found": self.set_credentials() self.__syncing = False