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

IconDownloaderDialog.cpp « gui « src - github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 271a916f3e6f5e15824e21f0b3ce1aca354dda41 (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
/*
 *  Copyright (C) 2019 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 "IconDownloaderDialog.h"
#include "ui_IconDownloaderDialog.h"

#include "core/Config.h"
#include "core/Database.h"
#include "core/Entry.h"
#include "core/Metadata.h"
#include "core/Tools.h"
#include "gui/IconDownloader.h"
#include "gui/IconModels.h"
#include "gui/Icons.h"
#include "osutils/OSUtils.h"
#ifdef Q_OS_MACOS
#include "gui/osutils/macutils/MacUtils.h"
#endif

#include <QStandardItemModel>

IconDownloaderDialog::IconDownloaderDialog(QWidget* parent)
    : QDialog(parent)
    , m_ui(new Ui::IconDownloaderDialog())
    , m_dataModel(new QStandardItemModel(this))
{
    setWindowFlags(Qt::Window);
    setAttribute(Qt::WA_DeleteOnClose);

    m_ui->setupUi(this);
    showFallbackMessage(false);

    m_dataModel->clear();
    m_dataModel->setHorizontalHeaderLabels({tr("URL"), tr("Status")});

    m_ui->tableView->setModel(m_dataModel);
    m_ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);

    connect(m_ui->cancelButton, SIGNAL(clicked()), SLOT(abortDownloads()));
    connect(m_ui->closeButton, SIGNAL(clicked()), SLOT(close()));
}

IconDownloaderDialog::~IconDownloaderDialog()
{
    abortDownloads();
}

void IconDownloaderDialog::downloadFavicons(const QSharedPointer<Database>& database,
                                            const QList<Entry*>& entries,
                                            bool force)
{
    m_db = database;
    m_urlToEntries.clear();
    abortDownloads();
    for (const auto& e : entries) {
        // Only consider entries with a valid URL and without a custom icon
        auto webUrl = e->webUrl();
        if (!webUrl.isEmpty() && (force || e->iconUuid().isNull())) {
            m_urlToEntries.insert(webUrl, e);
        }
    }

    if (m_urlToEntries.count() > 0) {
#ifdef Q_OS_MACOS
        macUtils()->raiseOwnWindow();
        Tools::wait(100);
#endif
        showFallbackMessage(false);
        m_ui->progressLabel->setText(tr("Please wait, processing entry list…"));
        open();
        QApplication::processEvents();

        for (const auto& url : m_urlToEntries.uniqueKeys()) {
            m_dataModel->appendRow(QList<QStandardItem*>()
                                   << new QStandardItem(url) << new QStandardItem(tr("Downloading…")));
            m_activeDownloaders.append(createDownloader(url));
        }

        // Setup the dialog
        updateProgressBar();
        updateCancelButton();
        QApplication::processEvents();

        // Start the downloads
        for (auto downloader : m_activeDownloaders) {
            downloader->download();
        }
    }
}

IconDownloader* IconDownloaderDialog::createDownloader(const QString& url)
{
    auto downloader = new IconDownloader();
    connect(downloader,
            SIGNAL(finished(const QString&, const QImage&)),
            this,
            SLOT(downloadFinished(const QString&, const QImage&)));

    downloader->setUrl(url);
    return downloader;
}

void IconDownloaderDialog::downloadFinished(const QString& url, const QImage& icon)
{
    // Prevent re-entrance from multiple calls finishing at the same time
    QMutexLocker locker(&m_mutex);

    // Cleanup the icon downloader that sent this signal
    auto downloader = qobject_cast<IconDownloader*>(sender());
    if (downloader) {
        downloader->deleteLater();
        m_activeDownloaders.removeAll(downloader);
    }

    updateProgressBar();
    updateCancelButton();

    if (m_db && !icon.isNull()) {
        // Don't add an icon larger than 128x128, but retain original size if smaller
        auto scaledIcon = icon;
        if (icon.width() > 128 || icon.height() > 128) {
            scaledIcon = icon.scaled(128, 128);
        }

        QByteArray serializedIcon = Icons::saveToBytes(scaledIcon);
        QUuid uuid = m_db->metadata()->findCustomIcon(serializedIcon);
        if (uuid.isNull()) {
            uuid = QUuid::createUuid();
            m_db->metadata()->addCustomIcon(uuid, serializedIcon);
            updateTable(url, tr("Ok"));
        } else {
            updateTable(url, tr("Already Exists"));
        }

        // Set the icon on all the entries associated with this url
        for (const auto entry : m_urlToEntries.values(url)) {
            entry->setIcon(uuid);
        }
    } else {
        showFallbackMessage(true);
        updateTable(url, tr("Download Failed"));
        return;
    }
}

void IconDownloaderDialog::showFallbackMessage(bool state)
{
    // Show fallback message if the option is not active
    bool show = state && !config()->get(Config::Security_IconDownloadFallback).toBool();
    m_ui->fallbackLabel->setVisible(show);
}

void IconDownloaderDialog::updateProgressBar()
{
    int total = m_urlToEntries.uniqueKeys().count();
    int value = total - m_activeDownloaders.count();
    m_ui->progressBar->setValue(value);
    m_ui->progressBar->setMaximum(total);
    m_ui->progressLabel->setText(
        tr("Downloading favicons (%1/%2)…").arg(QString::number(value), QString::number(total)));
}

void IconDownloaderDialog::updateCancelButton()
{
    m_ui->cancelButton->setEnabled(!m_activeDownloaders.isEmpty());
}

void IconDownloaderDialog::updateTable(const QString& url, const QString& message)
{
    for (int i = 0; i < m_dataModel->rowCount(); ++i) {
        if (m_dataModel->item(i, 0)->text() == url) {
            m_dataModel->item(i, 1)->setText(message);
        }
    }
}

void IconDownloaderDialog::abortDownloads()
{
    for (auto* downloader : m_activeDownloaders) {
        delete downloader;
    }
    m_activeDownloaders.clear();
    updateProgressBar();
    updateCancelButton();
}