/* 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 { validateNextcloudConfig } = require('./cloud_sync_config_helper.js');
/**
* Manages the Nextcloud configuration form, validation and connection testing.
* It supplies provider-specific settings and first-sync behavior to the shared
* cloud-sync controller.
*/
class CloudSyncNextcloudAdapter {
bindEvents(onCredentialsChanged) {
$('#nextcloud-url, #nextcloud-username, #nextcloud-password').bind('input', () => {
this.clearMessages();
onCredentialsChanged();
});
$('#test-nextcloud-connection').bind('click', async () => {
await this.testConnection();
});
}
async load() {
this.url = await ipcSettings.get('nextcloudUrl', '');
this.username = await ipcSettings.get('nextcloudUsername', '');
this.password = await ipcSettings.get('nextcloudPassword', '');
}
getFormValues() {
return {
url: document.getElementById('nextcloud-url').value,
username: document.getElementById('nextcloud-username').value,
password: document.getElementById('nextcloud-password').value
};
}
setFormValues() {
document.getElementById('nextcloud-url').value = this.url || '';
document.getElementById('nextcloud-username').value = this.username || '';
document.getElementById('nextcloud-password').value = this.password || '';
}
isAvailable() {
return validateNextcloudConfig(this.getFormValues()).valid;
}
canEditSyncOptions() {
return true;
}
async isConfigured() {
const [url, username, password] = await Promise.all([
ipcSettings.get('nextcloudUrl', ''),
ipcSettings.get('nextcloudUsername', ''),
ipcSettings.get('nextcloudPassword', '')
]);
return !!url && !!username && !!password;
}
getDisplayName() {
return i18n.t('cloud-sync.provider-nextcloud');
}
getSettingsOverride() {
const values = this.getFormValues();
return {
cloudSyncProvider: 'nextcloud',
nextcloudUrl: values.url.trim(),
nextcloudUsername: values.username.trim(),
nextcloudPassword: values.password
};
}
getSaveFormData() {
return { nextcloud: this.getFormValues() };
}
clearMessages() {
['url', 'username', 'password'].forEach(field => {
document.getElementById(`nextcloud-${field}-error`).textContent = '';
});
const status = document.getElementById('nextcloud-connection-status');
status.textContent = '';
status.classList.remove('success', 'failed');
}
showValidation(validation) {
this.clearMessages();
Object.keys(validation.errors).forEach(field => {
document.getElementById(`nextcloud-${field}-error`).textContent = i18n.t(validation.errors[field]);
});
if (validation.warnings.length > 0) {
const status = document.getElementById('nextcloud-connection-status');
validation.warnings.forEach(warning => {
const element = document.createElement('div');
element.classList.add('nextcloud-connection-warning');
element.textContent = i18n.t(warning);
status.appendChild(element);
});
}
}
async testConnection() {
const values = this.getFormValues();
const validation = validateNextcloudConfig(values);
this.showValidation(validation);
if (!validation.valid) {
return;
}
const status = document.getElementById('nextcloud-connection-status');
const loading = document.getElementById('nextcloud-connection-loading');
const testButton = document.getElementById('test-nextcloud-connection');
loading.style.display = 'block';
testButton.disabled = true;
status.textContent = i18n.t('cloud-sync.nextcloud-testing-connection');
status.classList.remove('success', 'failed');
let result;
try {
result = await ipcGeneral.testNextcloudConnection(values.url.trim(), values.username.trim(), values.password);
} catch (error) {
console.error('[Nextcloud] Connection test failed:', error);
result = { success: false, errorKey: 'cloud-sync.nextcloud-connection-failed' };
}
loading.style.display = 'none';
testButton.disabled = false;
const resultMessage = document.createElement('div');
resultMessage.classList.add('nextcloud-connection-result');
resultMessage.textContent = (result.success ? '✓ ' : '✗ ') + i18n.t(result.success
? 'cloud-sync.nextcloud-connection-success'
: result.errorKey);
status.textContent = '';
status.appendChild(resultMessage);
status.classList.add(result.success ? 'success' : 'failed');
if (result.success && validation.warnings.length > 0) {
validation.warnings.forEach(warning => {
const element = document.createElement('div');
element.classList.add('nextcloud-connection-warning');
element.textContent = i18n.t(warning);
status.appendChild(element);
});
}
}
validate() {
const validation = validateNextcloudConfig(this.getFormValues());
this.showValidation(validation);
return validation;
}
async save(update) {
const values = this.getFormValues();
this.url = update.set.nextcloudUrl;
this.username = update.set.nextcloudUsername;
this.password = values.password;
}
async syncAfterSave(runFirstSyncIfNeeded) {
await runFirstSyncIfNeeded();
}
}
module.exports = CloudSyncNextcloudAdapter;
Source