diff --git a/gramps/gen/db/base.py b/gramps/gen/db/base.py index b0a75a7d5..5b4890d91 100644 --- a/gramps/gen/db/base.py +++ b/gramps/gen/db/base.py @@ -2329,10 +2329,10 @@ class QuerySet(object): self.database.add_tag(tag, trans) commit_func = self.database.get_table_func(self.table,"commit_func") for item in self.generator: - if tag.handle not in item.tag_list: - item.add_tag(tag.handle) - elif remove: + if remove and (tag.handle in item.tag_list): item.remove_tag(tag.handle) + elif (not remove) and (tag.handle not in item.tag_list): + item.add_tag(tag.handle) else: continue commit_func(item, trans) diff --git a/gramps/gen/db/generic.py b/gramps/gen/db/generic.py index 25a219c28..34e2ecdd1 100644 --- a/gramps/gen/db/generic.py +++ b/gramps/gen/db/generic.py @@ -2162,3 +2162,13 @@ class DbGeneric(DbWriteBase, DbReadBase, UpdateCallback, Callback): return ast.literal_eval(version) else: return (0, 0, 0) + + def support_remote_changes(self): + return True + + def get_updates_since(self, last_datetime): + from dateutil.parser import parse + ## get the last items from the table in a non-disruptive fashion + ## [(signal, args), ...] + return [] ## [("person-delete", ([person.handle],))] + diff --git a/gramps/gui/viewmanager.py b/gramps/gui/viewmanager.py index d2fb5fbda..e0b54ff4c 100644 --- a/gramps/gui/viewmanager.py +++ b/gramps/gui/viewmanager.py @@ -311,6 +311,32 @@ class ViewManager(CLIManager): # Need to call after plugins have been registered self.uistate.connect('update-available', self.process_updates) self.check_for_updates() + ## if sync flag: + self.start_update_remote_changes() + + def start_update_remote_changes(self): + """ + """ + global last_datetime + from gi.repository import GLib + from datetime import datetime, timezone + last_datetime = datetime.now(timezone.utc).astimezone() # local time, with timezone + + def update_remote_changes(): + """ + """ + global last_datetime + if self.dbstate.db.support_remote_changes() and self.dbstate.open: + updates = self.dbstate.db.get_updates_since(last_datetime) + last_datetime = datetime.now(timezone.utc).astimezone() # local time, with timezone + for update in updates: + signal, args = update + args = eval(args) # "person-delete", ([person.handle],) + GLib.idle_add(self.dbstate.db.emit, signal, args) + print("Emitting ", signal, args) + return True # True continues + + GLib.timeout_add_seconds(5, update_remote_changes) def check_for_updates(self): """ diff --git a/gramps/plugins/database/dbapi.py b/gramps/plugins/database/dbapi.py index ef62ee4f4..a8e2c291a 100644 --- a/gramps/plugins/database/dbapi.py +++ b/gramps/plugins/database/dbapi.py @@ -132,6 +132,7 @@ class DBAPI(DbGeneric): self.dbapi = default_settings["dbapi"] self.update_schema() + self.start_update_changes() def update_schema(self): """ @@ -2012,3 +2013,27 @@ class DBAPI(DbGeneric): summary = super().get_summary() summary.update(self.dbapi.__class__.get_summary()) return summary + + def start_update_changes(self): + """ + Listen to all changes, to emit them on remote databases. + """ + from functools import partial + signals = [] + for item in ["person", "family", "source", "citation", + "event", "media", "place", "repository", + "note", "tag"]: + signals.append('%s-update' % item) + signals.append('%s-delete' % item) + signals.append('%s-add' % item) + signals += ['person-rebuild', 'family-rebuild', 'place-rebuild', 'source-rebuild', + 'citation-rebuild', 'media-rebuild', 'event-rebuild', 'repository-rebuild', + 'note-rebuild', 'tag-rebuild', 'home-person-changed'] + for signal in signals: + self.connect(signal, lambda *args, signal=signal: self.record_update_change(signal, *args)) + + def record_update_change(self, signal, *args): + """ + Record the signal in the signal table. + """ + print(signal, args)