2
* Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
3
* Copyright (C) 2014 Felix Geyer <debfx@fobos.de>
4
* Copyright (C) 2014 Florian Geyer <blueice@fobos.de>
6
* This program is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 2 or (at your option)
9
* version 3 of the License.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20
#include "DatabaseWidgetStateSync.h"
22
#include <QCoreApplication>
24
DatabaseWidgetStateSync::DatabaseWidgetStateSync(QObject* parent)
26
, m_activeDbWidget(nullptr)
27
, m_blockUpdates(false)
30
{Config::GUI_SplitterState, variantToIntList(config()->get(Config::GUI_SplitterState))},
31
{Config::GUI_PreviewSplitterState, variantToIntList(config()->get(Config::GUI_PreviewSplitterState))},
32
{Config::GUI_GroupSplitterState, variantToIntList(config()->get(Config::GUI_GroupSplitterState))}};
33
m_listViewState = config()->get(Config::GUI_ListViewState).toByteArray();
34
m_searchViewState = config()->get(Config::GUI_SearchViewState).toByteArray();
36
connect(qApp, &QCoreApplication::aboutToQuit, this, &DatabaseWidgetStateSync::sync);
39
DatabaseWidgetStateSync::~DatabaseWidgetStateSync() = default;
42
* Sync state with persistent storage.
44
void DatabaseWidgetStateSync::sync()
46
config()->set(Config::GUI_SplitterState, intListToVariant(m_splitterSizes.value(Config::GUI_SplitterState)));
47
config()->set(Config::GUI_PreviewSplitterState,
48
intListToVariant(m_splitterSizes.value(Config::GUI_PreviewSplitterState)));
49
config()->set(Config::GUI_GroupSplitterState,
50
intListToVariant(m_splitterSizes.value(Config::GUI_GroupSplitterState)));
51
config()->set(Config::GUI_ListViewState, m_listViewState);
52
config()->set(Config::GUI_SearchViewState, m_searchViewState);
56
void DatabaseWidgetStateSync::setActive(DatabaseWidget* dbWidget)
58
if (m_activeDbWidget) {
59
disconnect(m_activeDbWidget, nullptr, this, nullptr);
62
m_activeDbWidget = dbWidget;
64
if (m_activeDbWidget) {
65
m_blockUpdates = true;
67
m_activeDbWidget->setSplitterSizes(m_splitterSizes);
69
if (m_activeDbWidget->isSearchActive()) {
75
m_blockUpdates = false;
77
connect(m_activeDbWidget, SIGNAL(splitterSizesChanged()), SLOT(updateSplitterSizes()));
78
connect(m_activeDbWidget, SIGNAL(entryViewStateChanged()), SLOT(updateViewState()));
79
connect(m_activeDbWidget, SIGNAL(listModeActivated()), SLOT(restoreListView()));
80
connect(m_activeDbWidget, SIGNAL(searchModeActivated()), SLOT(restoreSearchView()));
81
connect(m_activeDbWidget, SIGNAL(listModeAboutToActivate()), SLOT(blockUpdates()));
82
connect(m_activeDbWidget, SIGNAL(searchModeAboutToActivate()), SLOT(blockUpdates()));
87
* Restore entry view list view state
90
* States of entry view 'Hide Usernames'/'Hide Passwords' settings are global,
91
* i.e. they are the same for both list and search mode
94
* If m_listViewState is empty, the list view has been activated for the first
95
* time after starting with a clean (or invalid) config.
97
void DatabaseWidgetStateSync::restoreListView()
99
if (!m_listViewState.isEmpty()) {
100
m_activeDbWidget->setEntryViewState(m_listViewState);
103
m_blockUpdates = false;
107
* Restore entry view search view state
110
* States of entry view 'Hide Usernames'/'Hide Passwords' settings are global,
111
* i.e. they are the same for both list and search mode
114
* If m_searchViewState is empty, the search view has been activated for the
115
* first time after starting with a clean (or invalid) config. Thus, save the
116
* current state. Without this, m_searchViewState would remain empty until
117
* there is an actual view state change (e.g. column is resized)
119
void DatabaseWidgetStateSync::restoreSearchView()
121
if (!m_searchViewState.isEmpty()) {
122
m_activeDbWidget->setEntryViewState(m_searchViewState);
124
m_searchViewState = m_activeDbWidget->entryViewState();
127
m_blockUpdates = false;
130
void DatabaseWidgetStateSync::blockUpdates()
132
m_blockUpdates = true;
135
void DatabaseWidgetStateSync::updateSplitterSizes()
137
if (!m_blockUpdates) {
138
m_splitterSizes = m_activeDbWidget->splitterSizes();
143
* Update entry view list/search view state
146
* States of entry view 'Hide Usernames'/'Hide Passwords' settings are global,
147
* i.e. they are the same for both list and search mode
149
void DatabaseWidgetStateSync::updateViewState()
151
if (m_blockUpdates) {
155
if (m_activeDbWidget->isSearchActive()) {
156
m_searchViewState = m_activeDbWidget->entryViewState();
158
m_listViewState = m_activeDbWidget->entryViewState();
164
QList<int> DatabaseWidgetStateSync::variantToIntList(const QVariant& variant)
166
const QVariantList list = variant.toList();
169
for (const QVariant& var : list) {
171
int size = var.toInt(&ok);
183
QVariant DatabaseWidgetStateSync::intListToVariant(const QList<int>& list)
187
for (int value : list) {
188
result.append(value);