Source

frontend/controllers/cloud_sync/cloud_sync_settings_service.js

/* 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/>. */

/**
 * Loads and updates shared cloud-sync settings, reads dialog options and
 * coordinates a first sync when needed. Settings IPC is resolved lazily.
 */
class CloudSyncSettingsService {
  constructor(getIpcSettings) {
    this.getIpcSettings = getIpcSettings;
  }

  get ipcSettings() {
    return this.getIpcSettings();
  }

  async loadShared(settingsKeys) {
    const [onlyWifi, enableBackgroundSync, useCustomModuleRepo,
           customModuleRepo, firstSyncDone, syncAfterChanges] = await Promise.all([
      this.ipcSettings.get(settingsKeys.onlyWifi, false),
      this.ipcSettings.get(settingsKeys.enableBackgroundSync, true),
      this.ipcSettings.get(settingsKeys.useCustomModuleRepo, false),
      this.ipcSettings.get(settingsKeys.customModuleRepo, 'custom_module_repo'),
      this.ipcSettings.get(settingsKeys.firstSyncDone, false),
      this.ipcSettings.get(settingsKeys.syncAfterChanges, false)
    ]);

    return {
      onlyWifi,
      enableBackgroundSync,
      useCustomModuleRepo,
      customModuleRepo,
      firstSyncDone,
      syncAfterChanges
    };
  }

  async applyUpdate(update) {
    for (const key of update.remove) {
      await this.ipcSettings.delete(key);
    }

    for (const key of Object.keys(update.set)) {
      await this.ipcSettings.set(key, update.set[key]);
    }
  }

  getSharedOptions() {
    return {
      onlyWifi: document.getElementById('only-sync-on-wifi').checked,
      enableBackgroundSync: document.getElementById('database-sync').checked,
      syncAfterChanges: document.getElementById('sync-cloud-after-changes').checked
    };
  }

  async runFirstSyncIfNeeded(ipcDb, eventController, showSyncResultMessage) {
    const firstSyncDone = await this.ipcSettings.get('firstCloudSyncDone', false);

    if (!firstSyncDone) {
      await ipcDb.syncCloudStorage();
      await eventController.publishAsync('on-db-refresh');
      await showSyncResultMessage();
    }
  }

  getCurrentSettings(activeProvider, nextcloudUrl, nextcloudUsername) {
    return {
      cloudSyncProvider: activeProvider,
      nextcloudUrl,
      nextcloudUsername
    };
  }
}

module.exports = CloudSyncSettingsService;