-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgrampswebsync.py
881 lines (754 loc) · 31.2 KB
/
grampswebsync.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2021-2022 David Straub
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""Gramps addon to synchronize with a Gramps Web server."""
import os
import threading
from datetime import datetime
try:
from typing import Callable, Optional
except ImportError:
from const import Type
Callable = Type
Optional = Type
from urllib.error import HTTPError, URLError
from urllib.parse import urlparse
from gi.repository import GLib, Gtk
from gramps.gen.config import config as configman
from gramps.gen.const import GRAMPS_LOCALE as glocale
from gramps.gen.db import DbTxn
from gramps.gen.db.utils import import_as_dict
from gramps.gen.errors import HandleError
from gramps.gen.utils.file import media_path_full
from gramps.gui.dialog import QuestionDialog2
from gramps.gui.managedwindow import ManagedWindow
from gramps.gui.plug.tool import BatchTool, ToolOptions
from const import (
A_ADD_LOC,
A_ADD_REM,
A_DEL_LOC,
A_DEL_REM,
A_MRG_REM,
A_UPD_LOC,
A_UPD_REM,
C_ADD_LOC,
C_ADD_REM,
C_DEL_LOC,
C_DEL_REM,
C_UPD_BOTH,
C_UPD_LOC,
C_UPD_REM,
MODE_BIDIRECTIONAL,
MODE_RESET_TO_LOCAL,
MODE_RESET_TO_REMOTE,
Actions,
)
from diffhandler import WebApiSyncDiffHandler
from webapihandler import WebApiHandler
try:
_trans = glocale.get_addon_translator(__file__)
except ValueError:
_trans = glocale.translation
_ = _trans.gettext
ngettext = _trans.ngettext
def get_password(service: str, username: str) -> Optional[str]:
"""If keyring is installed, return the user's password or None."""
try:
import keyring
except ImportError:
return None
return keyring.get_password(service, username)
def set_password(service: str, username: str, password: str) -> None:
"""If keyring is installed, store the user's password."""
try:
import keyring
except ImportError:
return None
keyring.set_password(service, username, password)
class GrampsWebSyncTool(BatchTool, ManagedWindow):
"""Main class for the Gramps Web Sync tool."""
def __init__(self, dbstate, user, options_class, name, *args, **kwargs) -> None:
"""Initialize GUI."""
BatchTool.__init__(self, dbstate, user, options_class, name)
ManagedWindow.__init__(self, user.uistate, [], self.__class__)
self.dbstate = dbstate
self.callback = self.uistate.pulse_progressbar
self.config = configman.register_manager("webapisync")
self.config.register("credentials.url", "")
self.config.register("credentials.username", "")
self.config.register("credentials.timestamp", 0)
self.config.load()
self.assistant = Gtk.Assistant()
self.set_window(self.assistant, None, _("Gramps Web Sync"))
self.setup_configs("interface.webapisync", 780, 600)
self.assistant.connect("close", self.do_close)
self.assistant.connect("cancel", self.do_close)
self.assistant.connect("apply", self.apply)
self.assistant.connect("prepare", self.prepare)
self.intro = IntroductionPage(self.assistant)
self.add_page(self.intro, Gtk.AssistantPageType.INTRO, _("Introduction"))
self.url = self.config.get("credentials.url")
self.username = self.config.get("credentials.username")
self.password = self.get_password()
self.loginpage = LoginPage(
self.assistant,
url=self.url,
username=self.username,
password=self.password,
)
self.add_page(self.loginpage, Gtk.AssistantPageType.CONTENT, _("Login"))
self.progress_page = ProgressPage(self.assistant)
self.add_page(
self.progress_page,
Gtk.AssistantPageType.PROGRESS,
_("Progress Information"),
)
self.confirmation = ConfirmationPage(self.assistant)
self.add_page(
self.confirmation, Gtk.AssistantPageType.CONFIRM, _("Final confirmation")
)
self.file_sync_page = FileSyncPage(self.assistant)
self.add_page(
self.file_sync_page,
Gtk.AssistantPageType.CONTENT,
_("Summary"),
)
self.file_confirmation = FileConfirmationPage(self.assistant)
self.add_page(
self.file_confirmation,
Gtk.AssistantPageType.CONFIRM,
_("Media Files"),
)
self.file_progress_page = FileProgressPage(self.assistant)
self.add_page(
self.file_progress_page,
Gtk.AssistantPageType.PROGRESS,
_("Progress Information"),
)
self.conclusion = ConclusionPage(self.assistant)
self.add_page(self.conclusion, Gtk.AssistantPageType.SUMMARY, _("Summary"))
self.show()
self.assistant.set_forward_page_func(self.forward_page, None)
self.api = None
self.db1 = dbstate.db
self.db2 = None
self._download_timestamp = 0
self.changes = None
self.sync = None
self.files_missing_local = []
self.files_missing_remote = []
self.uploaded = {}
self.downloaded = {}
def build_menu_names(self, obj):
"""Override :class:`.ManagedWindow` method."""
return (_("Gramps Web Sync"), None)
def do_close(self, assistant):
"""Close the assistant."""
position = self.window.get_position() # crock
self.assistant.hide()
self.window.move(position[0], position[1])
self.close()
def forward_page(self, page, data):
"""Specify the next page to be displayed."""
if self.conclusion.error:
return 7
if page == 2 and self.file_sync_page.unchanged:
return 4
if page == 5 and self.conclusion.unchanged:
return 7
return page + 1
def add_page(self, page, page_type, title=""):
"""Add a page to the assistant."""
page.show_all()
self.assistant.append_page(page)
self.assistant.set_page_title(page, title)
self.assistant.set_page_type(page, page_type)
def prepare(self, assistant, page):
"""Run page preparation code."""
page.update_complete()
if page == self.progress_page:
self.save_credentials()
url, username, password = self.get_credentials()
self.api = self.handle_server_errors(
WebApiHandler, url, username, password, None
)
if self.api is None:
return None
self.progress_page.label.set_text(_("Fetching remote data..."))
t = threading.Thread(target=self.async_compare_dbs)
t.start()
elif page == self.confirmation:
self.confirmation.prepare(self.changes)
elif page == self.file_sync_page:
self.assistant.commit()
if self.file_sync_page.unchanged:
self.file_sync_page.label.set_text(_("Both trees are the same."))
else:
self.file_sync_page.label.set_text(
_("Successfully synchronized %s objects.") % len(self.changes)
)
elif page == self.file_confirmation:
self.files_missing_local = self.get_missing_files_local()
self.files_missing_remote = self.get_missing_files_remote()
if not self.files_missing_local and not self.files_missing_remote:
self.handle_files_unchanged()
else:
self.file_confirmation.prepare(
self.files_missing_local, self.files_missing_remote
)
elif page == self.file_progress_page:
self.file_progress_page.prepare(
self.files_missing_local, self.files_missing_remote
)
t = threading.Thread(target=self.async_transfer_media)
t.start()
elif page == self.conclusion:
if self.conclusion.error:
pass
elif self.conclusion.unchanged:
text = _("Media files are in sync.")
self.conclusion.label.set_text(text)
else:
text = ""
if self.downloaded:
ok = sum([b for gid, b in self.downloaded.items()])
nok = sum([not b for gid, b in self.downloaded.items()])
if ok:
text += _("Successfully downloaded %s media files.") % ok
text += " "
if nok:
text += _("Encountered %s errors during download.") % nok
text += " "
if self.uploaded:
ok = sum([b for gid, b in self.uploaded.items()])
nok = sum([not b for gid, b in self.uploaded.items()])
if ok:
text += _("Successfully uploaded %s media files.") % ok
text += " "
if nok:
text += _("Encountered %s errors during upload.") % nok
self.conclusion.label.set_text(text)
self.conclusion.set_complete()
def handle_files_unchanged(self):
self.conclusion.unchanged = True
self.assistant.next_page()
def apply(self, assistant):
"""Apply the changes."""
page_number = assistant.get_current_page()
page = assistant.get_nth_page(page_number)
if page == self.confirmation:
try:
self.commit()
except:
self.handle_error(_("Unexpected error while applying changes."))
elif page == self.file_confirmation:
pass
def download_files(self):
"""Download media files missing locally."""
if not self.files_missing_local:
return
res = {}
for gramps_id, handle in self.files_missing_local:
self.downloaded[gramps_id] = self._download_file(handle)
self._update_file_progress()
return res
def _update_file_progress(self):
"""Update the file progress bars."""
self.file_progress_page.update_progress(
self.files_missing_local,
self.files_missing_remote,
self.downloaded,
self.uploaded,
)
def _download_file(self, handle):
"""Download a single media file."""
try:
obj = self.db1.get_media_from_handle(handle)
except HandleError:
self.handle_error(_("Error accessing media object."))
return
path = media_path_full(self.db1, obj.get_path())
return self.api.download_media_file(handle=handle, path=path)
def upload_files(self):
"""Upload media files missing remotely."""
if not self.files_missing_remote:
return
res = {}
for gramps_id, handle in self.files_missing_remote:
self.uploaded[gramps_id] = self._upload_file(handle)
self._update_file_progress()
return res
def _upload_file(self, handle):
"""Upload a single media file."""
try:
obj = self.db1.get_media_from_handle(handle)
except HandleError:
self.handle_error(_("Error accessing media object."))
return
path = media_path_full(self.db1, obj.get_path())
return self.api.upload_media_file(handle=handle, path=path)
def get_password(self):
"""Get a stored password."""
url = self.config.get("credentials.url")
username = self.config.get("credentials.username")
if not url or not username:
return None
return get_password(url, username)
def handle_error(self, message):
"""Handle an error message during sync."""
self.conclusion.error = True
self.assistant.next_page()
self.conclusion.label.set_text(message) #
self.conclusion.set_complete()
def handle_unchanged(self):
"""Return a message if nothing has changed."""
self.file_sync_page.unchanged = True
self.save_timestamp()
self.assistant.next_page()
def async_compare_dbs(self):
"""Download the remote data and import it to an in-memory database."""
# store timestamp just before downloading the XML
self._download_timestamp = datetime.now().timestamp()
GLib.idle_add(self.get_diff_actions)
def get_diff_actions(self):
"""Download the remote data, import it and compare it to local."""
path = self.handle_server_errors(self.api.download_xml)
if path is None:
return None
db2 = import_as_dict(str(path), self._user)
path.unlink() # delete temporary file
self.db2 = db2
self.progress_page.label.set_text(_("Comparing local and remote data..."))
timestamp = self.config.get("credentials.timestamp") or None
self.sync = WebApiSyncDiffHandler(
self.db1, self.db2, user=self._user, last_synced=timestamp
)
self.changes = self.sync.get_changes()
self.progress_page.label.set_text("")
self.progress_page.set_complete()
if len(self.changes) == 0:
self.handle_unchanged()
else:
self.assistant.next_page()
def async_transfer_media(self):
"""Upload/download media files."""
GLib.idle_add(self._async_transfer_media)
def _async_transfer_media(self):
"""Upload/download media files."""
self.handle_server_errors(self.download_files)
self.handle_server_errors(self.upload_files)
self.file_progress_page.set_complete()
self.assistant.next_page()
def handle_server_errors(self, callback: Callable, *args):
"""Handle server errors while executing a function."""
try:
return callback(*args)
except HTTPError as exc:
if exc.code == 401:
self.handle_error(_("Server authorization error."))
elif exc.code == 403:
self.handle_error(
_("Server authorization error: insufficient permissions.")
)
elif exc.code == 404:
self.handle_error(_("Error: URL not found."))
elif exc.code == 409:
self.handle_error(
_(
"Unable to synchronize changes to server: objects have been modified."
)
)
else:
self.handle_error(_("Error %s while connecting to server.") % exc.code)
return None
except URLError:
self.handle_error(_("Error connecting to server."))
return None
except ValueError:
self.handle_error(_("Error while parsing response from server."))
return None
def save_credentials(self):
"""Save the login credentials."""
url = self.loginpage.url.get_text()
url = self.sanitize_url(url)
username = self.loginpage.username.get_text()
password = self.loginpage.password.get_text()
if url != self.config.get("credentials.url"):
# if URL changed, clear last sync timestamp
self.config.set("credentials.timestamp", 0)
self.config.set("credentials.url", url)
self.config.set("credentials.username", username)
set_password(url, username, password)
self.config.save()
def sanitize_url(self, url: str) -> Optional[str]:
"""Warn if http and prepend https if missing."""
parsed_url = urlparse(url)
if parsed_url.scheme == "":
# if no httpX given, prepend https!
url = f"https://{url}"
elif parsed_url.scheme == "http":
question = QuestionDialog2(
_("Continue without transport encryption?"),
_(
"You have specified a URL with http scheme. "
"If you continue, your password will be sent "
"in clear text over the network. "
"Use only for local testing!"
),
_("Continue with HTTP"),
_("Use HTTPS"),
parent=self.window,
)
if not question.run():
return url.replace("http", "https")
return url
def get_credentials(self):
"""Get a tuple of URL, username, and password."""
return (
self.config.get("credentials.url"),
self.config.get("credentials.username"),
self.loginpage.password.get_text(),
)
def commit(self):
"""Commit all changes to the databases."""
msg = "Apply Gramps Web Sync changes"
with DbTxn(msg, self.sync.db1) as trans1:
with DbTxn(msg, self.sync.db2) as trans2:
actions = self.sync.changes_to_actions(self.changes, self.confirmation.sync_mode)
self.sync.commit_actions(actions, trans1, trans2)
# force the sync if mode is NOT bidirectional
force = self.confirmation.sync_mode != MODE_BIDIRECTIONAL
self.handle_server_errors(self.api.commit, trans2, force)
self.save_timestamp()
def save_timestamp(self):
"""Save last sync timestamp."""
# self.config.set("credentials.timestamp", self._download_timestamp)
self.config.set("credentials.timestamp", datetime.now().timestamp())
self.config.save()
def get_missing_files_local(self):
"""Get a list of media files missing locally."""
return [
(media.gramps_id, media.handle)
for media in self.db1.iter_media()
if not os.path.exists(media_path_full(self.db1, media.get_path()))
]
def get_missing_files_remote(self):
"""Get a list of media files missing remotely."""
missing_files = self.handle_server_errors(self.api.get_missing_files) or []
return [(media["gramps_id"], media["handle"]) for media in missing_files]
class Page(Gtk.Box):
"""Page base class."""
def __init__(self, assistant: Gtk.Assistant):
"""Initialize self."""
Gtk.Box.__init__(self, orientation=Gtk.Orientation.VERTICAL)
self.assistant = assistant
self._complete = False
def set_complete(self):
"""Set as complete."""
self._complete = True
self.update_complete()
@property
def complete(self):
return self._complete
def update_complete(self):
"""Set the current page's complete status."""
page_number = self.assistant.get_current_page()
current_page = self.assistant.get_nth_page(page_number)
self.assistant.set_page_complete(current_page, self.complete)
class IntroductionPage(Page):
"""A page containing introductory text."""
def __init__(self, assistant):
super().__init__(assistant)
label = Gtk.Label(label=self.__get_intro_text())
label.set_line_wrap(True)
label.set_use_markup(True)
label.set_max_width_chars(60)
self.pack_start(label, False, False, 0)
self._complete = True
def __get_intro_text(self):
"""Return the introductory text."""
return _(
"This tool allows to synchronize the currently opened "
"family tree with a remote family tree served by Gramps Web.\n\n"
"The tool assumes that the two trees are derivatives of each other, "
"i.e. one of the two was created from a Gramps XML (not GEDCOM!) "
"export of the other.\n\n"
"After successful synchronization, the two trees will be identical. "
"Modifications will be propagated based on timestamps. "
"You will be prompted for confirmation before any changes are made "
"to the local or remote trees.\n\n"
"If you instead want to merge two significantly different trees "
"with the option to make manual modifications, use the Import Merge "
"Tool instead."
)
class LoginPage(Page):
"""A page to log in."""
def __init__(self, assistant, url, username, password):
super().__init__(assistant)
self.set_spacing(12)
grid = Gtk.Grid()
grid.set_row_spacing(6)
grid.set_column_spacing(6)
self.add(grid)
label = Gtk.Label(label=_("Server URL: "))
grid.attach(label, 0, 0, 1, 1)
self.url = Gtk.Entry()
if url:
self.url.set_text(url)
self.url.set_hexpand(True)
self.url.set_input_purpose(Gtk.InputPurpose.URL)
grid.attach(self.url, 1, 0, 1, 1)
label = Gtk.Label(label=_("Username: "))
grid.attach(label, 0, 1, 1, 1)
self.username = Gtk.Entry()
if username:
self.username.set_text(username)
self.username.set_hexpand(True)
grid.attach(self.username, 1, 1, 1, 1)
label = Gtk.Label(label=_("Password: "))
grid.attach(label, 0, 2, 1, 1)
self.password = Gtk.Entry()
if password:
self.password.set_text(password)
self.password.set_hexpand(True)
self.password.set_visibility(False)
self.password.set_input_purpose(Gtk.InputPurpose.PASSWORD)
grid.attach(self.password, 1, 2, 1, 1)
self.url.connect("changed", self.on_entry_changed)
self.username.connect("changed", self.on_entry_changed)
self.password.connect("changed", self.on_entry_changed)
@property
def complete(self):
url = self.url.get_text()
username = self.username.get_text()
password = self.password.get_text()
if url and username and password:
return True
return False
def on_entry_changed(self, widget):
self.update_complete()
class ProgressPage(Page):
"""A progress 2page."""
def __init__(self, assistant):
super().__init__(assistant)
label = Gtk.Label(label="")
label.set_line_wrap(True)
label.set_use_markup(True)
label.set_max_width_chars(60)
self.label = label
self.pack_start(self.label, False, False, 0)
class FileProgressPage(Page):
"""A file progress page."""
def __init__(self, assistant):
"""Initialize page."""
super().__init__(assistant)
self.label1 = Gtk.Label(label="Media file download")
self.pack_start(self.label1, False, False, 20)
# self.progressbar1 = Gtk.ProgressBar()
# self.pack_start(self.progressbar1, False, False, 20)
self.label2 = Gtk.Label(label="Media file upload")
self.pack_start(self.label2, False, False, 20)
# self.progressbar2 = Gtk.ProgressBar()
# self.pack_start(self.progressbar2, False, False, 20)
def prepare(self, files_missing_local, files_missing_remote):
"""Prepare."""
n_down = len(files_missing_local)
if not n_down:
self.label1.hide()
# self.progressbar1.hide()
else:
self.label1.show()
# self.progressbar1.show()
self.label1.set_text(_("Downloading %s media file(s)") % n_down)
n_up = len(files_missing_remote)
if not n_up:
self.label2.hide()
# self.progressbar2.hide()
else:
self.label2.show()
# self.progressbar2.show()
self.label2.set_text(_("Uploading %s media file(s)") % n_up)
def update_progress(
self, files_missing_local, files_missing_remote, downloaded, uploaded
):
"""Update the progress bar."""
n_down = len(files_missing_local)
n_up = len(files_missing_remote)
i_down = len(downloaded)
i_up = len(uploaded)
# if n_down:
# self.progressbar1.set_fraction(i_down / n_down)
# if n_up:
# self.progressbar2.set_fraction(i_up / n_up)
class ConfirmationPage(Page):
"""Page showing the differences before applying them."""
# def diff_dialog(self) -> bool:
# """Edit the automatically generated actions via user interaction."""
# dialog = DiffDetailDialog(self._user.uistate, self.actions, on_ok=self.commit)
# dialog.show()
def __init__(self, assistant):
super().__init__(assistant)
self.sync_mode = MODE_BIDIRECTIONAL
self.store = Gtk.TreeStore(str, str)
# tree view
self.tree_view = Gtk.TreeView(model=self.store)
for i, col in enumerate(["ID", "Content"]):
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn(col, renderer, text=i)
self.tree_view.append_column(column)
# scrolled window
scrolled_window = Gtk.ScrolledWindow()
scrolled_window.add(self.tree_view)
self.sync_label = Gtk.Label()
self.sync_label.set_text("Sync mode")
# Box for radio buttons
self.radio_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=10)
# Radio buttons
option_name = _("Bidirectional Synchronization")
self.radio_button1 = Gtk.RadioButton.new_with_label_from_widget(
None, option_name
)
self.radio_button1.connect(
"toggled", self.on_radio_button_toggled, MODE_BIDIRECTIONAL
)
self.radio_box.pack_start(self.radio_button1, False, False, 0)
option_name = _("Reset remote to local")
self.radio_button2 = Gtk.RadioButton.new_from_widget(self.radio_button1)
self.radio_button2.set_label(option_name)
self.radio_button2.connect(
"toggled", self.on_radio_button_toggled, MODE_RESET_TO_LOCAL
)
self.radio_box.pack_start(self.radio_button2, False, False, 0)
option_name = _("Reset local to remote")
self.radio_button3 = Gtk.RadioButton.new_from_widget(self.radio_button1)
self.radio_button3.set_label(option_name)
self.radio_button3.connect(
"toggled", self.on_radio_button_toggled, MODE_RESET_TO_REMOTE
)
self.radio_box.pack_start(self.radio_button3, False, False, 0)
# Box to hold the label and radio buttons
self.label_radio_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=5)
self.label_radio_box.pack_start(self.sync_label, False, False, 0)
self.label_radio_box.pack_start(self.radio_box, False, False, 0)
self.outer_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
self.outer_box.pack_start(scrolled_window, True, True, 0)
self.outer_box.pack_start(self.label_radio_box, False, False, 10)
self.pack_start(self.outer_box, True, True, 0)
def on_radio_button_toggled(self, button, name):
"""Callback for radio buttons setting sync mode."""
if button.get_active():
self.sync_mode = int(name)
def prepare(self, changes: Actions):
"""Convert the changes list to a tree store."""
change_labels = {
_("Local changes"): {
_("Added"): C_ADD_LOC,
_("Deleted"): C_DEL_LOC,
_("Modified"): C_UPD_LOC,
},
_("Remote changes"): {
_("Added"): C_ADD_REM,
_("Deleted"): C_DEL_REM,
_("Modified"): C_UPD_REM,
},
_("Simultaneous changes"): {_("Modified"): C_UPD_BOTH},
}
for label1, v1 in change_labels.items():
iter1 = self.store.append(None, [label1, ""])
for label2, change_type in v1.items():
rows = []
for change in changes:
_type, handle, class_name, obj1, obj2 = change
if _type == change_type:
if obj1 is not None:
if class_name == "Tag":
gid = obj1.name
else:
gid = obj1.gramps_id
else:
if class_name == "Tag":
gid = obj2.name
else:
gid = obj2.gramps_id
obj_details = [class_name, gid]
rows.append(obj_details)
if rows:
label2 = f"{label2} ({len(rows)})"
iter2 = self.store.append(iter1, [label2, ""])
for row in rows:
self.store.append(iter2, row)
# expand first level
for i, row in enumerate(self.store):
self.tree_view.expand_row(Gtk.TreePath(i), False)
self.set_complete()
class FileSyncPage(Page):
"""Page to start media file sync."""
def __init__(self, assistant):
super().__init__(assistant)
label = Gtk.Label(label="")
label.set_line_wrap(True)
label.set_use_markup(True)
label.set_max_width_chars(60)
self.label = label
self.unchanged = False
self.pack_start(self.label, False, False, 0)
label = Gtk.Label(label=_("Click Next to synchronize media files."))
label.set_line_wrap(True)
label.set_use_markup(True)
label.set_max_width_chars(60)
self.pack_start(label, False, False, 0)
self.set_complete()
class FileConfirmationPage(Page):
"""File sync confirmation page."""
def __init__(self, assistant):
super().__init__(assistant)
self.store = Gtk.TreeStore(str)
# tree view
self.tree_view = Gtk.TreeView(model=self.store)
for i, col in enumerate(["ID"]):
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn(col, renderer, text=i)
self.tree_view.append_column(column)
# scrolled window
scrolled_window = Gtk.ScrolledWindow()
scrolled_window.add(self.tree_view)
self.pack_start(scrolled_window, True, True, 0)
def prepare(self, missing_local, missing_remote):
iter_local = self.store.append(None, [_("Missing locally")])
for gramps_id, handle in missing_local:
self.store.append(iter_local, [gramps_id])
iter_remote = self.store.append(None, [_("Missing remotely")])
for gramps_id, handle in missing_remote:
self.store.append(iter_remote, [gramps_id])
# expand first level
for i, row in enumerate(self.store):
self.tree_view.expand_row(Gtk.TreePath(i), False)
self.set_complete()
class ConclusionPage(Page):
"""The conclusion page."""
def __init__(self, assistant):
super().__init__(assistant)
self.error = False
self.unchanged = False
label = Gtk.Label(label="")
label.set_line_wrap(True)
label.set_use_markup(True)
label.set_max_width_chars(60)
self.label = label
self.pack_start(self.label, False, False, 0)
class GrampsWebSyncOptions(ToolOptions):
"""Options for Gramps Web Sync."""