Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DatabaseSettingsWidgetMaintenance.cpp « dbsettings « gui « src - github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 12738a98759309ba4a5207cd8aabba95b35a979f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 *  Copyright (C) 2021 KeePassXC Team <team@keepassxc.org>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 or (at your option)
 *  version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "DatabaseSettingsWidgetMaintenance.h"
#include "ui_DatabaseSettingsWidgetMaintenance.h"

#include "core/Group.h"
#include "core/Metadata.h"
#include "gui/IconModels.h"
#include "gui/MessageBox.h"

DatabaseSettingsWidgetMaintenance::DatabaseSettingsWidgetMaintenance(QWidget* parent)
    : DatabaseSettingsWidget(parent)
    , m_ui(new Ui::DatabaseSettingsWidgetMaintenance())
    , m_customIconModel(new CustomIconModel(this))
    , m_deletionDecision(MessageBox::NoButton)
{
    m_ui->setupUi(this);

    m_ui->customIconsView->setModel(m_customIconModel);

    connect(m_ui->deleteButton, SIGNAL(clicked()), SLOT(removeCustomIcon()));
    connect(m_ui->purgeButton, SIGNAL(clicked()), SLOT(purgeUnusedCustomIcons()));
    connect(m_ui->customIconsView->selectionModel(),
            SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
            this,
            SLOT(selectionChanged()));
}

DatabaseSettingsWidgetMaintenance::~DatabaseSettingsWidgetMaintenance()
{
}

void DatabaseSettingsWidgetMaintenance::populateIcons(QSharedPointer<Database> db)
{
    m_customIconModel->setIcons(db->metadata()->customIconsPixmaps(IconSize::Default),
                                db->metadata()->customIconsOrder());
    m_ui->deleteButton->setEnabled(false);
}

void DatabaseSettingsWidgetMaintenance::initialize()
{
    auto database = DatabaseSettingsWidget::getDatabase();
    if (!database) {
        return;
    }
    populateIcons(database);
}

void DatabaseSettingsWidgetMaintenance::selectionChanged()
{
    QList<QModelIndex> indexes = m_ui->customIconsView->selectionModel()->selectedIndexes();
    if (indexes.isEmpty()) {
        m_ui->deleteButton->setEnabled(false);
    } else {
        m_ui->deleteButton->setEnabled(true);
    }
}

void DatabaseSettingsWidgetMaintenance::removeCustomIcon()
{
    auto database = DatabaseSettingsWidget::getDatabase();
    if (!database) {
        return;
    }

    m_deletionDecision = MessageBox::NoButton;

    QList<QModelIndex> indexes = m_ui->customIconsView->selectionModel()->selectedIndexes();
    for (auto index : indexes) {
        removeSingleCustomIcon(database, index);
    }

    populateIcons(database);
}

void DatabaseSettingsWidgetMaintenance::removeSingleCustomIcon(QSharedPointer<Database> database, QModelIndex index)
{
    QUuid iconUuid = m_customIconModel->uuidFromIndex(index);

    const QList<Entry*> allEntries = database->rootGroup()->entriesRecursive(true);
    QList<Entry*> entriesWithSelectedIcon;
    QList<Entry*> historicEntriesWithSelectedIcon;

    for (Entry* entry : allEntries) {
        if (iconUuid == entry->iconUuid()) {
            // Check if this is a history entry (no assigned group)
            if (!entry->group()) {
                historicEntriesWithSelectedIcon << entry;
            } else {
                entriesWithSelectedIcon << entry;
            }
        }
    }

    const QList<Group*> allGroups = database->rootGroup()->groupsRecursive(true);
    QList<Group*> groupsWithSameIcon;

    for (Group* group : allGroups) {
        if (iconUuid == group->iconUuid()) {
            groupsWithSameIcon << group;
        }
    }

    int iconUseCount = entriesWithSelectedIcon.size() + groupsWithSameIcon.size();
    if (iconUseCount > 0) {
        if (m_deletionDecision == MessageBox::NoButton) {
            m_deletionDecision = MessageBox::question(
                this,
                tr("Confirm Deletion"),
                tr("At least one of the selected icons is currently in use by at least one entry or group. "
                   "The icons of all affected entries and groups will be replaced by the default icon. "
                   "Are you sure you want to delete icons that are currently in use?"),
                MessageBox::Delete | MessageBox::Skip,
                MessageBox::Skip);
        }

        if (m_deletionDecision == MessageBox::Skip) {
            // Early out, nothing is changed
            return;
        } else {
            // Revert matched entries to the default entry icon
            for (Entry* entry : asConst(entriesWithSelectedIcon)) {
                entry->setIcon(Entry::DefaultIconNumber);
            }

            // Revert matched groups to the default group icon
            for (Group* group : asConst(groupsWithSameIcon)) {
                group->setIcon(Group::DefaultIconNumber);
            }
        }
    }

    // Remove the icon from history entries
    for (Entry* entry : asConst(historicEntriesWithSelectedIcon)) {
        entry->setUpdateTimeinfo(false);
        entry->setIcon(0);
        entry->setUpdateTimeinfo(true);
    }

    // Remove the icon from the database
    database->metadata()->removeCustomIcon(iconUuid);
}

void DatabaseSettingsWidgetMaintenance::purgeUnusedCustomIcons()
{
    auto database = DatabaseSettingsWidget::getDatabase();
    if (!database) {
        return;
    }

    QList<Entry*> historyEntries;
    QSet<QUuid> historicIcons;
    QSet<QUuid> iconsInUse;

    const QList<Entry*> allEntries = database->rootGroup()->entriesRecursive(true);
    for (Entry* entry : allEntries) {
        if (!entry->group()) {
            // Icons exclusively in use by historic entries (no
            // group assigned) are also purged from the database
            historyEntries << entry;
            historicIcons << entry->iconUuid();
        } else {
            iconsInUse << entry->iconUuid();
        }
    }

    const QList<Group*> allGroups = database->rootGroup()->groupsRecursive(true);
    for (Group* group : allGroups) {
        iconsInUse.insert(group->iconUuid());
    }

    int purgeCounter = 0;
    QList<QUuid> customIcons = database->metadata()->customIconsOrder();
    for (QUuid iconUuid : customIcons) {
        if (iconsInUse.contains(iconUuid)) {
            continue;
        }

        if (historicIcons.contains(iconUuid)) {
            // Remove the icon from history entries using this icon
            for (Entry* historicEntry : asConst(historyEntries)) {
                if (historicEntry->iconUuid() != iconUuid) {
                    continue;
                }
                historicEntry->setUpdateTimeinfo(false);
                historicEntry->setIcon(0);
                historicEntry->setUpdateTimeinfo(true);
            }
        }

        ++purgeCounter;
        database->metadata()->removeCustomIcon(iconUuid);
    }

    if (0 == purgeCounter) {
        MessageBox::information(this,
                                tr("Custom Icons Are In Use"),
                                tr("All custom icons are in use by at least one entry or group."),
                                MessageBox::Ok);
        return;
    }

    populateIcons(database);

    MessageBox::information(
        this, tr("Purged Unused Icons"), tr("Purged %n icon(s) from the database.", "", purgeCounter), MessageBox::Ok);
}