From b0f3a234d0a99e7d05fdabcf3497dd119aa6dd22 Mon Sep 17 00:00:00 2001 From: Adomas Ven Date: Mon, 27 Jun 2016 19:47:11 +0300 Subject: [PATCH] Allow choosing of synced libraries in sync preferences (#1033) Affects Sync button and auto-sync. Manual library sync will be possible later via library context menu. --- .../zotero/preferences/librariesToSync.xul | 67 +++++++++ .../zotero/preferences/preferences_sync.js | 131 +++++++++++++++++- .../zotero/preferences/preferences_sync.xul | 73 +++++----- .../zotero/xpcom/sync/syncAPIClient.js | 16 ++- .../content/zotero/xpcom/sync/syncRunner.js | 2 +- chrome/locale/en-US/zotero/preferences.dtd | 4 + chrome/locale/en-US/zotero/zotero.properties | 2 + 7 files changed, 255 insertions(+), 40 deletions(-) create mode 100644 chrome/content/zotero/preferences/librariesToSync.xul diff --git a/chrome/content/zotero/preferences/librariesToSync.xul b/chrome/content/zotero/preferences/librariesToSync.xul new file mode 100644 index 000000000..e11d842c2 --- /dev/null +++ b/chrome/content/zotero/preferences/librariesToSync.xul @@ -0,0 +1,67 @@ + + + + + + + + + %prefWindow; + + %common; +]> + + + + + \ No newline at end of file diff --git a/chrome/content/zotero/preferences/preferences_sync.js b/chrome/content/zotero/preferences/preferences_sync.js index a5d2ee621..7f83bd289 100644 --- a/chrome/content/zotero/preferences/preferences_sync.js +++ b/chrome/content/zotero/preferences/preferences_sync.js @@ -148,7 +148,6 @@ Zotero_Preferences.Sync = { Zotero.Sync.Runner.deleteAPIKey(); return; } - this.displayFields(json.username); }), @@ -195,8 +194,138 @@ Zotero_Preferences.Sync = { } this.displayFields(); + Zotero.Prefs.clear('sync.librariesToSync'); yield Zotero.Sync.Runner.deleteAPIKey(); }), + + + showLibrariesToSyncDialog: function() { + var io = {}; + window.openDialog('chrome://zotero/content/preferences/librariesToSync.xul', + "zotero-preferences-librariesToSyncDialog", "chrome,modal,centerscreen", io); + }, + + + dblClickLibraryToSync: function (event) { + var tree = document.getElementById("libraries-to-sync-tree"); + var row = {}, col = {}, child = {}; + tree.treeBoxObject.getCellAt(event.clientX, event.clientY, row, col, child); + + if (col.value.element.id == 'libraries-to-sync-checked') { + return; + } + // if dblclicked anywhere but the checkbox update pref + return this.toggleLibraryToSync(row.value); + }, + + + clickLibraryToSync: function (event) { + var tree = document.getElementById("libraries-to-sync-tree"); + var row = {}, col = {}, child = {}; + tree.treeBoxObject.getCellAt(event.clientX, event.clientY, row, col, child); + + if (col.value.element.id != 'libraries-to-sync-checked') { + return; + } + // if clicked on checkbox update pref + return this.toggleLibraryToSync(row.value); + }, + + + toggleLibraryToSync: function (index) { + var treechildren = document.getElementById('libraries-to-sync-rows'); + if (index >= treechildren.childNodes.length) { + return; + } + var row = treechildren.childNodes[index]; + var val = row.firstChild.childNodes[1].getAttribute('value'); + if (!val) { + return + } + + var librariesToSkip = JSON.parse(Zotero.Prefs.get('sync.librariesToSkip') || '[]'); + var indexOfId = librariesToSkip.indexOf(val); + if (indexOfId == -1) { + librariesToSkip.push(val); + } else { + librariesToSkip.splice(indexOfId, 1); + } + Zotero.Prefs.set('sync.librariesToSkip', JSON.stringify(librariesToSkip)); + + var cell = row.firstChild.firstChild; + cell.setAttribute('value', indexOfId != -1); + }, + + + initLibrariesToSync: Zotero.Promise.coroutine(function* () { + var tree = document.getElementById("libraries-to-sync-tree"); + var treechildren = document.getElementById('libraries-to-sync-rows'); + while (treechildren.hasChildNodes()) { + treechildren.removeChild(treechildren.firstChild); + } + + function addRow(libraryName, id, checked=false, editable=true) { + var treeitem = document.createElement('treeitem'); + var treerow = document.createElement('treerow'); + var checkboxCell = document.createElement('treecell'); + var nameCell = document.createElement('treecell'); + + nameCell.setAttribute('label', libraryName); + nameCell.setAttribute('value', id); + nameCell.setAttribute('editable', false); + checkboxCell.setAttribute('value', checked); + checkboxCell.setAttribute('editable', editable); + + treerow.appendChild(checkboxCell); + treerow.appendChild(nameCell); + treeitem.appendChild(treerow); + treechildren.appendChild(treeitem); + } + + // Add loading row while we're loading a group list + var loadingLabel = Zotero.getString("zotero.preferences.sync.librariesToSync.loadingLibraries"); + addRow(loadingLabel, "loading", false, false); + + var apiKey = Zotero.Sync.Data.Local.getAPIKey(); + var client = Zotero.Sync.Runner.getAPIClient({apiKey}); + var groups = []; + try { + // Load up remote groups + var keyInfo = yield Zotero.Sync.Runner.checkAccess(client, {timeout: 5000}); + groups = yield client.getGroups(keyInfo.userID); + } + catch (e) { + // Connection problems + if ((e instanceof Zotero.HTTP.UnexpectedStatusException) + || (e instanceof Zotero.HTTP.TimeoutException) + || (e instanceof Zotero.HTTP.BrowserOfflineException)) { + Zotero.alert( + window, + Zotero.getString('general.error'), + Zotero.getString('sync.error.checkConnection', Zotero.clientName) + ); + } + else { + throw e; + } + document.getElementsByTagName('dialog')[0].acceptDialog(); + } + + // Remove the loading row + treechildren.removeChild(treechildren.firstChild); + + var librariesToSkip = JSON.parse(Zotero.Prefs.get('sync.librariesToSkip') || '[]'); + // Add default rows + addRow(Zotero.getString("pane.collections.libraryAndFeeds"), "L" + Zotero.Libraries.userLibraryID, + librariesToSkip.indexOf("L" + Zotero.Libraries.userLibraryID) == -1); + addRow(Zotero.getString("pane.collections.publications"), "L" + Zotero.Libraries.publicationsLibraryID, + librariesToSkip.indexOf("L" + Zotero.Libraries.publicationsLibraryID) == -1); + + // Add group rows + for (let group of groups) { + addRow(group.data.name, "G" + group.id, librariesToSkip.indexOf("G" + group.id) == -1); + } + }), updateStorageSettingsUI: Zotero.Promise.coroutine(function* () { diff --git a/chrome/content/zotero/preferences/preferences_sync.xul b/chrome/content/zotero/preferences/preferences_sync.xul index 266546723..0155d0560 100644 --- a/chrome/content/zotero/preferences/preferences_sync.xul +++ b/chrome/content/zotero/preferences/preferences_sync.xul @@ -103,46 +103,45 @@ - - - - - - + + + + + - - -