Debian Codegraph @

src/fdosecrets/widgets/SettingsWidgetFdoSecrets.cpp

raw
1 /*
2 * Copyright (C) 2018 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 "SettingsWidgetFdoSecrets.h"
19 #include "ui_SettingsWidgetFdoSecrets.h"
20
21 #include "fdosecrets/FdoSecretsPlugin.h"
22 #include "fdosecrets/FdoSecretsSettings.h"
23 #include "fdosecrets/widgets/RowButtonHelper.h"
24 #include "fdosecrets/widgets/SettingsModels.h"
25 #include "objects/Service.h"
26
27 #include "gui/DatabaseWidget.h"
28
29 #include <QAction>
30 #include <QToolButton>
31
32 using FdoSecrets::DBusClientPtr;
33 using FdoSecrets::SettingsClientModel;
34 using FdoSecrets::SettingsDatabaseModel;
35
36 namespace
37 {
38 class ManageDatabase : public QWidget
39 {
40 Q_OBJECT
41
42 Q_PROPERTY(DatabaseWidget* dbWidget READ dbWidget WRITE setDbWidget USER true)
43
44 public:
45 explicit ManageDatabase(FdoSecretsPlugin* plugin, QWidget* parent = nullptr)
46 : QWidget(parent)
47 , m_plugin(plugin)
48 {
49 // db settings
50 m_dbSettingsAct = new QAction(tr("Database settings"), this);
51 m_dbSettingsAct->setIcon(icons()->icon(QStringLiteral("document-edit")));
52 m_dbSettingsAct->setToolTip(tr("Edit database settings"));
53 m_dbSettingsAct->setEnabled(false);
54 connect(m_dbSettingsAct, &QAction::triggered, this, [this]() {
55 if (!m_dbWidget) {
56 return;
57 }
58 m_plugin->serviceInstance()->doSwitchToDatabaseSettings(m_dbWidget);
59 });
60
61 // unlock/lock
62 m_lockAct = new QAction(tr("Unlock database"), this);
63 m_lockAct->setIcon(icons()->icon(QStringLiteral("object-unlocked")));
64 m_lockAct->setToolTip(tr("Unlock database to show more information"));
65 connect(m_lockAct, &QAction::triggered, this, [this]() {
66 if (!m_dbWidget) {
67 return;
68 }
69 if (m_dbWidget->isLocked()) {
70 m_plugin->serviceInstance()->doUnlockDatabaseInDialog(m_dbWidget);
71 } else {
72 m_dbWidget->lock();
73 }
74 });
75
76 // layout
77 auto dbSettingsBtn = new QToolButton(this);
78 dbSettingsBtn->setAutoRaise(true);
79 dbSettingsBtn->setDefaultAction(m_dbSettingsAct);
80
81 auto lockBtn = new QToolButton(this);
82 lockBtn->setAutoRaise(true);
83 lockBtn->setDefaultAction(m_lockAct);
84
85 auto layout = new QHBoxLayout(this);
86 layout->setContentsMargins(1, 1, 1, 1);
87 layout->setSpacing(1);
88
89 layout->addStretch();
90 layout->addWidget(dbSettingsBtn);
91 layout->addWidget(lockBtn);
92 layout->addStretch();
93 }
94
95 DatabaseWidget* dbWidget() const
96 {
97 return m_dbWidget;
98 }
99
100 void setDbWidget(DatabaseWidget* dbWidget)
101 {
102 if (m_dbWidget == dbWidget) {
103 return;
104 }
105
106 if (m_dbWidget) {
107 disconnect();
108 }
109
110 m_dbWidget = dbWidget;
111
112 reconnect();
113 }
114
115 private:
116 void disconnect()
117 {
118 if (!m_dbWidget) {
119 return;
120 }
121 m_dbWidget->disconnect(this);
122 }
123
124 void reconnect()
125 {
126 if (!m_dbWidget) {
127 return;
128 }
129 connect(m_dbWidget, &DatabaseWidget::databaseLocked, this, [this]() {
130 if (!m_lockAct || !m_dbSettingsAct) {
131 return;
132 }
133 m_lockAct->setText(tr("Unlock database"));
134 m_lockAct->setIcon(icons()->icon(QStringLiteral("object-unlocked")));
135 m_lockAct->setToolTip(tr("Unlock database to show more information"));
136 m_dbSettingsAct->setEnabled(false);
137 });
138 connect(m_dbWidget, &DatabaseWidget::databaseUnlocked, this, [this]() {
139 if (!m_lockAct || !m_dbSettingsAct) {
140 return;
141 }
142 m_lockAct->setText(tr("Lock database"));
143 m_lockAct->setIcon(icons()->icon(QStringLiteral("object-locked")));
144 m_lockAct->setToolTip(tr("Lock database"));
145 m_dbSettingsAct->setEnabled(true);
146 });
147 }
148
149 private:
150 FdoSecretsPlugin* m_plugin = nullptr;
151 QPointer<DatabaseWidget> m_dbWidget = nullptr;
152 QAction* m_dbSettingsAct = nullptr;
153 QAction* m_lockAct = nullptr;
154 };
155
156 class ManageSession : public QWidget
157 {
158 Q_OBJECT
159
160 Q_PROPERTY(const DBusClientPtr& client READ client WRITE setClient USER true)
161
162 public:
163 explicit ManageSession(QWidget* parent = nullptr)
164 : QWidget(parent)
165 {
166 auto disconnectAct = new QAction(tr("Disconnect"), this);
167 disconnectAct->setIcon(icons()->icon(QStringLiteral("dialog-close")));
168 disconnectAct->setToolTip(tr("Disconnect this application"));
169 connect(disconnectAct, &QAction::triggered, this, [this]() {
170 if (m_client) {
171 m_client->disconnectDBus();
172 }
173 });
174
175 auto resetAct = new QAction(tr("Reset"), this);
176 resetAct->setIcon(icons()->icon(QStringLiteral("refresh")));
177 resetAct->setToolTip(tr("Reset any remembered decisions for this application"));
178 connect(resetAct, &QAction::triggered, this, [this]() {
179 if (m_client) {
180 m_client->clearAuthorization();
181 }
182 });
183
184 // layout
185 auto disconnectBtn = new QToolButton(this);
186 disconnectBtn->setAutoRaise(true);
187 disconnectBtn->setDefaultAction(disconnectAct);
188
189 auto resetBtn = new QToolButton(this);
190 resetBtn->setAutoRaise(true);
191 resetBtn->setDefaultAction(resetAct);
192
193 auto layout = new QHBoxLayout(this);
194 layout->setContentsMargins(1, 1, 1, 1);
195 layout->setSpacing(1);
196
197 layout->addStretch();
198 layout->addWidget(resetBtn);
199 layout->addWidget(disconnectBtn);
200 layout->addStretch();
201 }
202
203 const DBusClientPtr& client() const
204 {
205 return m_client;
206 }
207
208 void setClient(DBusClientPtr client)
209 {
210 m_client = std::move(client);
211 }
212
213 private:
214 DBusClientPtr m_client{};
215 };
216 } // namespace
217
218 SettingsWidgetFdoSecrets::SettingsWidgetFdoSecrets(FdoSecretsPlugin* plugin, QWidget* parent)
219 : QWidget(parent)
220 , m_ui(new Ui::SettingsWidgetFdoSecrets())
221 , m_plugin(plugin)
222 {
223 m_ui->setupUi(this);
224 m_ui->warningMsg->setHidden(true);
225 m_ui->warningMsg->setCloseButtonVisible(false);
226
227 auto clientModel = new SettingsClientModel(*plugin->dbus(), this);
228 m_ui->tableClients->setModel(clientModel);
229 installWidgetItemDelegate<ManageSession>(m_ui->tableClients,
230 SettingsClientModel::ColumnManage,
231 [](QWidget* p, const QModelIndex&) { return new ManageSession(p); });
232
233 // config header after setting model, otherwise the header doesn't have enough sections
234 auto clientViewHeader = m_ui->tableClients->horizontalHeader();
235 clientViewHeader->setSelectionMode(QAbstractItemView::NoSelection);
236 clientViewHeader->setSectionsClickable(false);
237 clientViewHeader->setSectionResizeMode(QHeaderView::ResizeToContents);
238 clientViewHeader->setSectionResizeMode(SettingsClientModel::ColumnApplication, QHeaderView::Stretch);
239
240 auto dbModel = new SettingsDatabaseModel(plugin->dbTabs(), this);
241 m_ui->tableDatabases->setModel(dbModel);
242 installWidgetItemDelegate<ManageDatabase>(
243 m_ui->tableDatabases, SettingsDatabaseModel::ColumnManage, [plugin](QWidget* p, const QModelIndex&) {
244 return new ManageDatabase(plugin, p);
245 });
246
247 // config header after setting model, otherwise the header doesn't have enough sections
248 auto dbViewHeader = m_ui->tableDatabases->horizontalHeader();
249 dbViewHeader->setSelectionMode(QAbstractItemView::NoSelection);
250 dbViewHeader->setSectionsClickable(false);
251 dbViewHeader->setSectionResizeMode(QHeaderView::Stretch);
252 dbViewHeader->setSectionResizeMode(SettingsDatabaseModel::ColumnManage, QHeaderView::ResizeToContents);
253
254 // prompt the user to save settings before the sections are enabled
255 connect(m_plugin, &FdoSecretsPlugin::secretServiceStarted, this, &SettingsWidgetFdoSecrets::updateServiceState);
256 connect(m_plugin, &FdoSecretsPlugin::secretServiceStopped, this, &SettingsWidgetFdoSecrets::updateServiceState);
257 connect(m_ui->enableFdoSecretService, &QCheckBox::toggled, this, &SettingsWidgetFdoSecrets::updateServiceState);
258 updateServiceState();
259
260 // background checking
261 m_checkTimer.setInterval(2000);
262 connect(&m_checkTimer, &QTimer::timeout, this, &SettingsWidgetFdoSecrets::checkDBusName);
263 connect(m_plugin, &FdoSecretsPlugin::secretServiceStarted, &m_checkTimer, &QTimer::stop);
264 connect(m_plugin, SIGNAL(secretServiceStopped()), &m_checkTimer, SLOT(start()));
265 }
266
267 SettingsWidgetFdoSecrets::~SettingsWidgetFdoSecrets() = default;
268
269 void SettingsWidgetFdoSecrets::loadSettings()
270 {
271 m_ui->enableFdoSecretService->setChecked(FdoSecrets::settings()->isEnabled());
272 m_ui->showNotification->setChecked(FdoSecrets::settings()->showNotification());
273 m_ui->confirmDeleteItem->setChecked(FdoSecrets::settings()->confirmDeleteItem());
274 m_ui->confirmAccessItem->setChecked(FdoSecrets::settings()->confirmAccessItem());
275 m_ui->unlockBeforeSearch->setChecked(FdoSecrets::settings()->unlockBeforeSearch());
276 }
277
278 void SettingsWidgetFdoSecrets::saveSettings()
279 {
280 FdoSecrets::settings()->setEnabled(m_ui->enableFdoSecretService->isChecked());
281 FdoSecrets::settings()->setShowNotification(m_ui->showNotification->isChecked());
282 FdoSecrets::settings()->setConfirmDeleteItem(m_ui->confirmDeleteItem->isChecked());
283 FdoSecrets::settings()->setConfirmAccessItem(m_ui->confirmAccessItem->isChecked());
284 FdoSecrets::settings()->setUnlockBeforeSearch(m_ui->unlockBeforeSearch->isChecked());
285 }
286
287 void SettingsWidgetFdoSecrets::showEvent(QShowEvent* event)
288 {
289 QWidget::showEvent(event);
290 QTimer::singleShot(0, this, &SettingsWidgetFdoSecrets::checkDBusName);
291 m_checkTimer.start();
292 }
293
294 void SettingsWidgetFdoSecrets::hideEvent(QHideEvent* event)
295 {
296 QWidget::hideEvent(event);
297 m_checkTimer.stop();
298 }
299
300 void SettingsWidgetFdoSecrets::checkDBusName()
301 {
302 if (m_plugin->serviceInstance()) {
303 // only need checking if the service is not started or failed to start.
304 return;
305 }
306
307 if (m_plugin->dbus()->serviceOccupied()) {
308 m_ui->warningMsg->showMessage(
309 tr("<b>Warning:</b> ") + m_plugin->dbus()->reportExistingService(), MessageWidget::Warning, -1);
310 m_ui->enableFdoSecretService->setChecked(false);
311 m_ui->enableFdoSecretService->setEnabled(false);
312 return;
313 }
314 m_ui->warningMsg->hideMessage();
315 m_ui->enableFdoSecretService->setEnabled(true);
316 }
317
318 void SettingsWidgetFdoSecrets::updateServiceState()
319 {
320 m_ui->tabWidget->setEnabled(m_plugin->serviceInstance() != nullptr);
321 if (m_ui->enableFdoSecretService->isChecked() && !m_plugin->serviceInstance()) {
322 m_ui->tabWidget->setToolTip(
323 tr("Save current changes to activate the plugin and enable editing of this section."));
324 } else {
325 m_ui->tabWidget->setToolTip("");
326 }
327 }
328
329 #include "SettingsWidgetFdoSecrets.moc"