/* This file is part of Ezra Bible App.
Copyright (C) 2019 - 2026 Ezra Bible App Development Team <contact@ezrabibleapp.net>
Ezra Bible App 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.
Ezra Bible App 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 Ezra Bible App. See the file LICENSE.
If not, see <http://www.gnu.org/licenses/>. */
const Dropbox = require('dropbox');
const DROPBOX_CLIENT_ID = '6m7e5ri5udcbkp3';
/**
* Manages Dropbox-specific cloud-sync configuration, OAuth linking and reset
* state. It provides dialog behavior, credential persistence and first-sync
* handling to the shared cloud-sync controller.
*/
class CloudSyncDropboxAdapter {
constructor(context) {
this.context = context;
this.resetConfiguration = false;
this.auth = new Dropbox.DropboxAuth({ clientId: DROPBOX_CLIENT_ID });
}
async load() {
this.resetConfiguration = false;
this.token = await ipcSettings.get('dropboxToken', '');
this.refreshToken = await ipcSettings.get('dropboxRefreshToken', '');
this.linkStatus = await ipcSettings.get('dropboxLinkStatus', null);
}
isAvailable() {
return this.linkStatus === 'LINKED' && !this.resetConfiguration;
}
canEditSyncOptions() {
return this.isAvailable();
}
async isConfigured() {
const linkStatus = await ipcSettings.get('dropboxLinkStatus', null);
return linkStatus === 'LINKED';
}
getDisplayName() {
return i18n.t('cloud-sync.provider-dropbox');
}
reset() {
this.resetConfiguration = true;
}
clearCredentials() {
this.token = null;
this.refreshToken = null;
this.linkStatus = null;
}
getSettingsOverride() {
return { cloudSyncProvider: 'dropbox' };
}
getSaveFormData() {
return {};
}
validate() {
return { valid: true };
}
onDeactivate() {
this.clearCredentials();
}
bindEvents(onReset) {
this.initAuthCallbacks();
$('#link-dropbox-account').bind('click', async () => {
await this.setupAuthentication();
});
$('#reset-dropbox-account-link').bind('click', () => {
this.reset();
this.updateLinkStatusLabel(true);
onReset();
});
}
updateLinkStatusLabel(resetLink=false) {
if (this.linkStatus === 'LINKED' && !resetLink) {
$('#dropbox-link-status').attr('i18n', 'dropbox.dropbox-link-status-linked');
$('#dropbox-link-status').text(i18n.t('dropbox.dropbox-link-status-linked'));
$('#dropbox-link-status').addClass('success');
$('#dropbox-link-status').removeClass('failed');
} else if (this.linkStatus === 'FAILED' && !resetLink) {
$('#dropbox-link-status').attr('i18n', 'dropbox.dropbox-link-status-linking-failed');
$('#dropbox-link-status').text(i18n.t('dropbox.dropbox-link-status-linking-failed'));
$('#dropbox-link-status').addClass('failed');
$('#dropbox-link-status').removeClass('success');
} else if (this.linkStatus === null || resetLink) {
$('#dropbox-link-status').attr('i18n', 'dropbox.dropbox-link-status-not-linked');
$('#dropbox-link-status').text(i18n.t('dropbox.dropbox-link-status-not-linked'));
$('#dropbox-link-status').removeClass('success');
$('#dropbox-link-status').removeClass('failed');
}
this.context.updateOptionsState();
}
initAuthCallbacks() {
if (this.context.platformHelper.isCordova()) {
// eslint-disable-next-line no-undef
universalLinks.subscribe('launchedAppFromLink', (eventData) => {
this.handleRedirect(eventData.url);
});
} else if (this.context.platformHelper.isElectron()) {
require('electron').ipcRenderer.on('dropbox-auth-callback', (event, url) => {
this.handleRedirect(url);
});
}
}
getRedirectUri() {
return 'ezrabible://app';
}
handleRedirect(url) {
const match = url.match(/code=([^&]+)/);
const codeMatch = match ? match[1] : '';
if (!codeMatch) {
return;
}
if (this.context.redirectHandled) {
return;
}
this.context.redirectHandled = true;
this.auth.setCodeVerifier(window.sessionStorage.getItem('codeVerifier'));
this.auth.getAccessTokenFromCode(this.getRedirectUri(), codeMatch)
.then(async (response) => {
this.token = response.result.access_token;
this.refreshToken = response.result.refresh_token;
this.linkStatus = 'LINKED';
this.resetConfiguration = false;
this.updateLinkStatusLabel();
await ipcSettings.set('dropboxToken', this.token);
await ipcSettings.set('dropboxRefreshToken', this.refreshToken);
await ipcSettings.set('dropboxLinkStatus', this.linkStatus);
}).catch((error) => {
this.linkStatus = 'FAILED';
this.updateLinkStatusLabel();
console.error('[Dropbox auth] Token exchange or configuration save failed:', error);
});
}
async setupAuthentication() {
const redirectUri = this.getRedirectUri();
try {
const authUrl = await this.auth.getAuthenticationUrl(redirectUri, undefined, 'code', 'offline', undefined, undefined, true);
window.sessionStorage.clear();
window.sessionStorage.setItem('codeVerifier', this.auth.codeVerifier);
this.context.redirectHandled = false;
uiHelper.openLinkInBrowser(authUrl);
} catch (error) {
console.error(error);
}
}
async save(providerUpdate) {
if (this.resetConfiguration) {
this.clearCredentials();
await ipcSettings.delete('lastCloudSyncResult');
await ipcSettings.delete('lastCloudSyncTime');
await ipcSettings.delete('lastCloudDownloadTime');
await ipcSettings.delete('lastCloudUploadTime');
await ipcSettings.set('firstCloudSyncDone', false);
}
if (this.linkStatus === 'LINKED' || this.resetConfiguration) {
await ipcSettings.set('dropboxToken', this.token);
await ipcSettings.set('dropboxRefreshToken', this.refreshToken);
}
await ipcSettings.set('dropboxLinkStatus', this.linkStatus);
if (providerUpdate.set.firstCloudSyncDone === false) {
this.context.firstSyncDone = false;
}
}
async syncAfterSave() {
if (this.linkStatus === 'LINKED' && !this.context.firstSyncDone) {
await ipcDb.syncCloudStorage();
await this.context.eventController.publishAsync('on-db-refresh');
await this.context.showSyncResultMessage();
}
this.resetConfiguration = false;
}
}
module.exports = CloudSyncDropboxAdapter;
Source