| 1 |
/* |
| 2 |
* Copyright (C) 2019 Aetf <[email protected]> |
| 3 |
* |
| 4 |
* This program is free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation, either version 2 or (at your option) |
| 7 |
* version 3 of the License. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include "DatabaseSettingsWidgetFdoSecrets.h" |
| 19 |
#include "ui_DatabaseSettingsWidgetFdoSecrets.h" |
| 20 |
|
| 21 |
#include "fdosecrets/FdoSecretsSettings.h" |
| 22 |
|
| 23 |
#include "core/Group.h" |
| 24 |
#include "core/Metadata.h" |
| 25 |
#include "gui/group/GroupModel.h" |
| 26 |
|
| 27 |
#include <QSortFilterProxyModel> |
| 28 |
|
| 29 |
namespace |
| 30 |
{ |
| 31 |
enum class ExposedGroup |
| 32 |
{ |
| 33 |
None, |
| 34 |
Expose |
| 35 |
}; |
| 36 |
} // namespace |
| 37 |
|
| 38 |
class DatabaseSettingsWidgetFdoSecrets::GroupModelNoRecycle : public QSortFilterProxyModel |
| 39 |
{ |
| 40 |
Q_OBJECT |
| 41 |
|
| 42 |
Database* m_db; |
| 43 |
|
| 44 |
public: |
| 45 |
explicit GroupModelNoRecycle(Database* db) |
| 46 |
: m_db(db) |
| 47 |
{ |
| 48 |
Q_ASSERT(db); |
| 49 |
setSourceModel(new GroupModel(m_db, this)); |
| 50 |
} |
| 51 |
|
| 52 |
Group* groupFromIndex(const QModelIndex& index) const |
| 53 |
{ |
| 54 |
return groupFromSourceIndex(mapToSource(index)); |
| 55 |
} |
| 56 |
|
| 57 |
Group* groupFromSourceIndex(const QModelIndex& index) const |
| 58 |
{ |
| 59 |
auto groupModel = qobject_cast<GroupModel*>(sourceModel()); |
| 60 |
Q_ASSERT(groupModel); |
| 61 |
return groupModel->groupFromIndex(index); |
| 62 |
} |
| 63 |
|
| 64 |
QModelIndex indexFromGroup(Group* group) const |
| 65 |
{ |
| 66 |
auto groupModel = qobject_cast<GroupModel*>(sourceModel()); |
| 67 |
Q_ASSERT(groupModel); |
| 68 |
return mapFromSource(groupModel->index(group)); |
| 69 |
} |
| 70 |
|
| 71 |
protected: |
| 72 |
bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override |
| 73 |
{ |
| 74 |
auto source_idx = sourceModel()->index(source_row, 0, source_parent); |
| 75 |
if (!source_idx.isValid()) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
auto recycleBin = m_db->metadata()->recycleBin(); |
| 80 |
if (!recycleBin) { |
| 81 |
return true; |
| 82 |
} |
| 83 |
|
| 84 |
// can not call mapFromSource, which internally calls filterAcceptsRow |
| 85 |
auto group = groupFromSourceIndex(source_idx); |
| 86 |
|
| 87 |
return group && !group->isRecycled() && group->uuid() != recycleBin->uuid(); |
| 88 |
} |
| 89 |
}; |
| 90 |
|
| 91 |
DatabaseSettingsWidgetFdoSecrets::DatabaseSettingsWidgetFdoSecrets(QWidget* parent) |
| 92 |
: QWidget(parent) |
| 93 |
, m_ui(new Ui::DatabaseSettingsWidgetFdoSecrets) |
| 94 |
{ |
| 95 |
m_ui->setupUi(this); |
| 96 |
m_ui->buttonGroup->setId(m_ui->radioDonotExpose, static_cast<int>(ExposedGroup::None)); |
| 97 |
m_ui->buttonGroup->setId(m_ui->radioExpose, static_cast<int>(ExposedGroup::Expose)); |
| 98 |
|
| 99 |
// make sure there is at least a selection |
| 100 |
connect(m_ui->radioExpose, &QRadioButton::toggled, this, [this](bool checked) { |
| 101 |
if (checked && !m_ui->selectGroup->selectionModel()->hasSelection()) { |
| 102 |
auto model = m_ui->selectGroup->model(); |
| 103 |
if (model) { |
| 104 |
auto idx = model->index(0, 0); |
| 105 |
m_ui->selectGroup->selectionModel()->select(idx, QItemSelectionModel::SelectCurrent); |
| 106 |
} |
| 107 |
} |
| 108 |
}); |
| 109 |
} |
| 110 |
|
| 111 |
DatabaseSettingsWidgetFdoSecrets::~DatabaseSettingsWidgetFdoSecrets() = default; |
| 112 |
|
| 113 |
void DatabaseSettingsWidgetFdoSecrets::loadSettings(QSharedPointer<Database> db) |
| 114 |
{ |
| 115 |
m_db = std::move(db); |
| 116 |
|
| 117 |
m_model.reset(new GroupModelNoRecycle(m_db.data())); |
| 118 |
m_ui->selectGroup->setModel(m_model.data()); |
| 119 |
|
| 120 |
Group* recycleBin = nullptr; |
| 121 |
if (m_db->metadata() && m_db->metadata()->recycleBin()) { |
| 122 |
recycleBin = m_db->metadata()->recycleBin(); |
| 123 |
} |
| 124 |
|
| 125 |
auto group = m_db->rootGroup()->findGroupByUuid(FdoSecrets::settings()->exposedGroup(m_db)); |
| 126 |
if (!group || group->isRecycled() || (recycleBin && group->uuid() == recycleBin->uuid())) { |
| 127 |
m_ui->radioDonotExpose->setChecked(true); |
| 128 |
} else { |
| 129 |
auto idx = m_model->indexFromGroup(group); |
| 130 |
m_ui->selectGroup->selectionModel()->select(idx, QItemSelectionModel::SelectCurrent); |
| 131 |
// expand all its parents |
| 132 |
idx = idx.parent(); |
| 133 |
while (idx.isValid()) { |
| 134 |
m_ui->selectGroup->expand(idx); |
| 135 |
idx = idx.parent(); |
| 136 |
} |
| 137 |
m_ui->radioExpose->setChecked(true); |
| 138 |
} |
| 139 |
|
| 140 |
settingsWarning(); |
| 141 |
} |
| 142 |
|
| 143 |
void DatabaseSettingsWidgetFdoSecrets::saveSettings() |
| 144 |
{ |
| 145 |
Q_ASSERT(m_db); |
| 146 |
Q_ASSERT(m_model); |
| 147 |
|
| 148 |
QUuid exposedGroup; |
| 149 |
switch (static_cast<ExposedGroup>(m_ui->buttonGroup->checkedId())) { |
| 150 |
case ExposedGroup::None: |
| 151 |
break; |
| 152 |
case ExposedGroup::Expose: { |
| 153 |
auto idx = m_ui->selectGroup->selectionModel()->selectedIndexes().takeFirst(); |
| 154 |
Q_ASSERT(idx.isValid()); |
| 155 |
exposedGroup = m_model->groupFromIndex(idx)->uuid(); |
| 156 |
break; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
FdoSecrets::settings()->setExposedGroup(m_db, exposedGroup); |
| 161 |
} |
| 162 |
|
| 163 |
void DatabaseSettingsWidgetFdoSecrets::settingsWarning() |
| 164 |
{ |
| 165 |
if (FdoSecrets::settings()->isEnabled()) { |
| 166 |
m_ui->groupBox->setEnabled(true); |
| 167 |
m_ui->warningWidget->hideMessage(); |
| 168 |
} else { |
| 169 |
m_ui->groupBox->setEnabled(false); |
| 170 |
m_ui->warningWidget->showMessage(tr("Enable Secret Service to access these settings."), MessageWidget::Warning); |
| 171 |
m_ui->warningWidget->setCloseButtonVisible(false); |
| 172 |
m_ui->warningWidget->setAutoHideTimeout(-1); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
#include "DatabaseSettingsWidgetFdoSecrets.moc" |