keepassxc

Форк
0
/
DatabaseWidgetStateSync.cpp 
192 строки · 5.9 Кб
1
/*
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>
5
 *
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.
10
 *
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.
15
 *
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/>.
18
 */
19

20
#include "DatabaseWidgetStateSync.h"
21

22
#include <QCoreApplication>
23

24
DatabaseWidgetStateSync::DatabaseWidgetStateSync(QObject* parent)
25
    : QObject(parent)
26
    , m_activeDbWidget(nullptr)
27
    , m_blockUpdates(false)
28
{
29
    m_splitterSizes = {
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();
35

36
    connect(qApp, &QCoreApplication::aboutToQuit, this, &DatabaseWidgetStateSync::sync);
37
}
38

39
DatabaseWidgetStateSync::~DatabaseWidgetStateSync() = default;
40

41
/**
42
 * Sync state with persistent storage.
43
 */
44
void DatabaseWidgetStateSync::sync()
45
{
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);
53
    config()->sync();
54
}
55

56
void DatabaseWidgetStateSync::setActive(DatabaseWidget* dbWidget)
57
{
58
    if (m_activeDbWidget) {
59
        disconnect(m_activeDbWidget, nullptr, this, nullptr);
60
    }
61

62
    m_activeDbWidget = dbWidget;
63

64
    if (m_activeDbWidget) {
65
        m_blockUpdates = true;
66

67
        m_activeDbWidget->setSplitterSizes(m_splitterSizes);
68

69
        if (m_activeDbWidget->isSearchActive()) {
70
            restoreSearchView();
71
        } else {
72
            restoreListView();
73
        }
74

75
        m_blockUpdates = false;
76

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()));
83
    }
84
}
85

86
/**
87
 * Restore entry view list view state
88
 *
89
 * NOTE:
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
92
 *
93
 * NOTE:
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.
96
 */
97
void DatabaseWidgetStateSync::restoreListView()
98
{
99
    if (!m_listViewState.isEmpty()) {
100
        m_activeDbWidget->setEntryViewState(m_listViewState);
101
    }
102

103
    m_blockUpdates = false;
104
}
105

106
/**
107
 * Restore entry view search view state
108
 *
109
 * NOTE:
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
112
 *
113
 * NOTE:
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)
118
 */
119
void DatabaseWidgetStateSync::restoreSearchView()
120
{
121
    if (!m_searchViewState.isEmpty()) {
122
        m_activeDbWidget->setEntryViewState(m_searchViewState);
123
    } else {
124
        m_searchViewState = m_activeDbWidget->entryViewState();
125
    }
126

127
    m_blockUpdates = false;
128
}
129

130
void DatabaseWidgetStateSync::blockUpdates()
131
{
132
    m_blockUpdates = true;
133
}
134

135
void DatabaseWidgetStateSync::updateSplitterSizes()
136
{
137
    if (!m_blockUpdates) {
138
        m_splitterSizes = m_activeDbWidget->splitterSizes();
139
    }
140
}
141

142
/**
143
 * Update entry view list/search view state
144
 *
145
 * NOTE:
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
148
 */
149
void DatabaseWidgetStateSync::updateViewState()
150
{
151
    if (m_blockUpdates) {
152
        return;
153
    }
154

155
    if (m_activeDbWidget->isSearchActive()) {
156
        m_searchViewState = m_activeDbWidget->entryViewState();
157
    } else {
158
        m_listViewState = m_activeDbWidget->entryViewState();
159
    }
160

161
    sync();
162
}
163

164
QList<int> DatabaseWidgetStateSync::variantToIntList(const QVariant& variant)
165
{
166
    const QVariantList list = variant.toList();
167
    QList<int> result;
168

169
    for (const QVariant& var : list) {
170
        bool ok;
171
        int size = var.toInt(&ok);
172
        if (ok) {
173
            result.append(size);
174
        } else {
175
            result.clear();
176
            break;
177
        }
178
    }
179

180
    return result;
181
}
182

183
QVariant DatabaseWidgetStateSync::intListToVariant(const QList<int>& list)
184
{
185
    QVariantList result;
186

187
    for (int value : list) {
188
        result.append(value);
189
    }
190

191
    return result;
192
}
193

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.