File indexing completed on 2024-05-19 05:37:27
0001 /* 0002 * SPDX-FileCopyrightText: 2017 Ivan Cukic <ivan.cukic (at) kde.org> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include "backendchooserwidget.h" 0008 0009 #include "ui_backendchooserwidget.h" 0010 0011 #include "engine/backend_p.h" 0012 #include "vault.h" 0013 0014 class BackendChooserWidget::Private 0015 { 0016 public: 0017 Private(BackendChooserWidget *parent) 0018 : q(parent) 0019 { 0020 } 0021 0022 Ui::BackendChooserWidget ui; 0023 0024 bool vaultNameValid = false; 0025 bool backendValid = false; 0026 QByteArray bestsBackend; 0027 int bestBackendPrio = -1; 0028 0029 void setVaultNameValid(bool valid) 0030 { 0031 vaultNameValid = valid; 0032 q->setIsValid(vaultNameValid && backendValid); 0033 } 0034 0035 void setBackendValid(bool valid) 0036 { 0037 backendValid = valid; 0038 q->setIsValid(vaultNameValid && backendValid); 0039 } 0040 0041 BackendChooserWidget *const q; 0042 }; 0043 0044 BackendChooserWidget::BackendChooserWidget() 0045 : DialogDsl::DialogModule(false) 0046 , d(new Private(this)) 0047 { 0048 d->ui.setupUi(this); 0049 d->ui.textStatus->hide(); 0050 d->ui.page2Layout->setRowStretch(1, 10); 0051 0052 connect(d->ui.editVaultName, &QLineEdit::textChanged, this, [&](const QString &vaultName) { 0053 d->setVaultNameValid(!vaultName.isEmpty()); 0054 }); 0055 0056 connect(d->ui.comboBackend, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), this, &BackendChooserWidget::checkCurrentBackend); 0057 0058 connect(d->ui.pickBackendButton, &QPushButton::clicked, this, &BackendChooserWidget::showBackendSelector); 0059 } 0060 0061 BackendChooserWidget::~BackendChooserWidget() 0062 { 0063 } 0064 0065 void BackendChooserWidget::checkCurrentBackend() 0066 { 0067 const auto backendId = d->ui.comboBackend->currentData().toString(); 0068 const auto backend = PlasmaVault::Backend::instance(backendId); 0069 bool backendValid = false; 0070 0071 d->setBackendValid(false); 0072 0073 if (!backend) { 0074 d->ui.textStatus->setHtml(i18n("The specified backend is not available")); 0075 d->ui.textStatus->show(); 0076 0077 } else { 0078 d->ui.textStatus->hide(); 0079 0080 const auto result = AsynQt::await(backend->validateBackend()); 0081 0082 if (!result) { 0083 d->ui.textStatus->setHtml(result.error().message()); 0084 d->ui.textStatus->show(); 0085 0086 } else { 0087 backendValid = true; 0088 } 0089 } 0090 0091 d->setBackendValid(backendValid); 0092 } 0093 0094 void BackendChooserWidget::showBackendSelector() 0095 { 0096 d->ui.vaultEncryptionConfig->setCurrentWidget(d->ui.page2); 0097 0098 checkCurrentBackend(); 0099 } 0100 0101 void BackendChooserWidget::addItem(const QByteArray &id, const QString &title, int priority) 0102 { 0103 d->ui.comboBackend->addItem(title, id); 0104 0105 if (priority > d->bestBackendPrio) { 0106 const auto backend = PlasmaVault::Backend::instance(id); 0107 Q_ASSERT(backend); // backend and UI out of sync. Its an assert since they both are part of the same .so 0108 if (backend && AsynQt::await(backend->validateBackend())) { 0109 d->bestBackendPrio = priority; 0110 d->bestsBackend = id; 0111 d->ui.backendName->setText(title); 0112 d->setBackendValid(true); 0113 } 0114 } 0115 } 0116 0117 PlasmaVault::Vault::Payload BackendChooserWidget::fields() const 0118 { 0119 QByteArray backend = d->bestsBackend; 0120 if (d->ui.vaultEncryptionConfig->currentWidget() == d->ui.page2) 0121 backend = d->ui.comboBackend->currentData().toByteArray(); 0122 Q_ASSERT(!backend.isEmpty()); 0123 return {{KEY_BACKEND, backend}, {KEY_NAME, d->ui.editVaultName->text()}}; 0124 } 0125 0126 void BackendChooserWidget::checkBackendAvailable() 0127 { 0128 if (d->bestsBackend.isEmpty()) { // in case there are no backends found at all 0129 showBackendSelector(); // show the more helpful selector 0130 } 0131 } 0132 0133 #include "moc_backendchooserwidget.cpp"