searchbar.cpp 5.26 KB
Newer Older
1
/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; -*-
Marc Mutz's avatar
Marc Mutz committed
2
    view/searchbar.cpp
Frank Osterfeld's avatar
Frank Osterfeld committed
3
4

    This file is part of Kleopatra, the KDE keymanager
5
    SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB
Frank Osterfeld's avatar
Frank Osterfeld committed
6

7
    SPDX-License-Identifier: GPL-2.0-or-later
Frank Osterfeld's avatar
Frank Osterfeld committed
8
9
*/

Marc Mutz's avatar
Marc Mutz committed
10
11
#include <config-kleopatra.h>

Frank Osterfeld's avatar
Frank Osterfeld committed
12
13
#include "searchbar.h"

Laurent Montel's avatar
Laurent Montel committed
14
15
#include <Libkleo/KeyFilter>
#include <Libkleo/KeyFilterManager>
Frank Osterfeld's avatar
Frank Osterfeld committed
16

Laurent Montel's avatar
Laurent Montel committed
17
#include <KLocalizedString>
Andre Heinecke's avatar
Andre Heinecke committed
18
#include <QLineEdit>
Frank Osterfeld's avatar
Frank Osterfeld committed
19

20
#include <QComboBox>
Frank Osterfeld's avatar
Frank Osterfeld committed
21
#include <QHBoxLayout>
22
#include <QPushButton>
23
#include <QSortFilterProxyModel>
24

25
#include <Libkleo/GnuPG>
26
27
28
29
#include <qgpgme/keylistjob.h>
#include <qgpgme/protocol.h>
#include <gpgme++/keylistresult.h>

30
using namespace Kleo;
31

Laurent Montel's avatar
Laurent Montel committed
32
33
class SearchBar::Private
{
Marc Mutz's avatar
Marc Mutz committed
34
    friend class ::Kleo::SearchBar;
Laurent Montel's avatar
Laurent Montel committed
35
    SearchBar *const q;
Frank Osterfeld's avatar
Frank Osterfeld committed
36
public:
Laurent Montel's avatar
Laurent Montel committed
37
    explicit Private(SearchBar *qq);
Frank Osterfeld's avatar
Frank Osterfeld committed
38
39
    ~Private();

40
private:
Laurent Montel's avatar
Laurent Montel committed
41
42
    void slotKeyFilterChanged(int idx)
    {
Laurent Montel's avatar
Laurent Montel committed
43
        Q_EMIT q->keyFilterChanged(keyFilter(idx));
44
45
    }

46
    std::shared_ptr<KeyFilter> keyFilter(int idx) const
Laurent Montel's avatar
Laurent Montel committed
47
48
49
    {
        const QModelIndex mi = KeyFilterManager::instance()->model()->index(idx, 0);
        return KeyFilterManager::instance()->fromModelIndex(mi);
50
51
    }

52
    std::shared_ptr<KeyFilter> currentKeyFilter() const
Laurent Montel's avatar
Laurent Montel committed
53
54
    {
        return keyFilter(combo->currentIndex());
55
56
    }

Laurent Montel's avatar
Laurent Montel committed
57
58
    QString currentKeyFilterID() const
    {
59
        if (const std::shared_ptr<KeyFilter> f = currentKeyFilter()) {
60
            return f->id();
Laurent Montel's avatar
Laurent Montel committed
61
        } else {
62
            return QString();
Laurent Montel's avatar
Laurent Montel committed
63
        }
64
65
    }

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    void listNotCertifiedKeys() const
    {
        lineEdit->clear();
        combo->setCurrentIndex(combo->findData(QStringLiteral("not-validated-certificates")));
        Q_EMIT q->keyFilterChanged(keyFilter(combo->currentIndex()));
    }

    /* List all OpenPGP keys and see if we find one with a UID that is
     * not at least fully valid.  If we find one, show the certify
     * button.  */
    /* XXX: It would be nice to do this every time the user certifies
     * a key.  */
    void showOrHideCertifyButton() const
    {
        QGpgME::KeyListJob *job = QGpgME::openpgp()->keyListJob();
        connect(job, &QGpgME::KeyListJob::result, job,
Laurent Montel's avatar
Laurent Montel committed
82
                [this](const GpgME::KeyListResult&, const std::vector<GpgME::Key> &keys, const QString&, const GpgME::Error&)
83
84
85
86
87
88
89
90
91
92
93
94
                {
                    for (const auto &key: keys) {
                        if (Kleo::keyValidity(key) < GpgME::UserID::Validity::Full) {
                            certifyButton->show();
                            return;
                        }
                    }
                    certifyButton->hide();
                });
        job->start(QStringList());
    }

95
private:
96
    QSortFilterProxyModel *proxyModel;
Andre Heinecke's avatar
Andre Heinecke committed
97
    QLineEdit *lineEdit;
Laurent Montel's avatar
Laurent Montel committed
98
    QComboBox *combo;
99
    QPushButton *certifyButton;
Frank Osterfeld's avatar
Frank Osterfeld committed
100
101
};

Laurent Montel's avatar
Laurent Montel committed
102
103
SearchBar::Private::Private(SearchBar *qq)
    : q(qq)
Frank Osterfeld's avatar
Frank Osterfeld committed
104
{
Laurent Montel's avatar
Laurent Montel committed
105
    QHBoxLayout *layout = new QHBoxLayout(q);
Laurent Montel's avatar
Laurent Montel committed
106
    layout->setContentsMargins(0, 0, 0, 0);
Andre Heinecke's avatar
Andre Heinecke committed
107
108
    lineEdit = new QLineEdit(q);
    lineEdit->setClearButtonEnabled(true);
Laurent Montel's avatar
Laurent Montel committed
109
    lineEdit->setPlaceholderText(i18n("Search..."));
Laurent Montel's avatar
Laurent Montel committed
110
111
112
    layout->addWidget(lineEdit, /*stretch=*/1);
    combo = new QComboBox(q);
    layout->addWidget(combo);
113
    certifyButton = new QPushButton(q);
Laurent Montel's avatar
Laurent Montel committed
114
    certifyButton->setIcon(QIcon::fromTheme(QStringLiteral("security-medium")));
115
116
117
118
119
120
121
122
    certifyButton->setToolTip(i18n("Some certificates are not yet certified. "
                                   "Click here to see a list of these certificates."
                                   "<br/><br/>"
                                   "Certification is required to make sure that the certificates "
                                   "actually belong to the identity they claim to belong to."));
    certifyButton->hide();
    layout->addWidget(certifyButton);
    showOrHideCertifyButton();
Frank Osterfeld's avatar
Frank Osterfeld committed
123

124
125
126
127
    proxyModel = new QSortFilterProxyModel{q};
    proxyModel->setSourceModel(KeyFilterManager::instance()->model());
    proxyModel->sort(0, Qt::AscendingOrder);
    combo->setModel(proxyModel);
128

Laurent Montel's avatar
Laurent Montel committed
129
130
131
    KDAB_SET_OBJECT_NAME(layout);
    KDAB_SET_OBJECT_NAME(lineEdit);
    KDAB_SET_OBJECT_NAME(combo);
132
    KDAB_SET_OBJECT_NAME(certifyButton);
Frank Osterfeld's avatar
Frank Osterfeld committed
133

Andre Heinecke's avatar
Andre Heinecke committed
134
    connect(lineEdit, &QLineEdit::textChanged, q, &SearchBar::stringFilterChanged);
Laurent Montel's avatar
Laurent Montel committed
135
    connect(combo, SIGNAL(currentIndexChanged(int)), q, SLOT(slotKeyFilterChanged(int)));
136
    connect(certifyButton, SIGNAL(clicked()), q, SLOT(listNotCertifiedKeys()));
137
}
138

139
SearchBar::Private::~Private() {}
Frank Osterfeld's avatar
Frank Osterfeld committed
140

Laurent Montel's avatar
Laurent Montel committed
141
142
SearchBar::SearchBar(QWidget *parent, Qt::WindowFlags f)
    : QWidget(parent, f), d(new Private(this))
Frank Osterfeld's avatar
Frank Osterfeld committed
143
{
Laurent Montel's avatar
Laurent Montel committed
144

Frank Osterfeld's avatar
Frank Osterfeld committed
145
146
147
148
}

SearchBar::~SearchBar() {}

149
150
void SearchBar::updateClickMessage(const QString &shortcutStr)
{
Laurent Montel's avatar
Laurent Montel committed
151
    d->lineEdit->setPlaceholderText(i18n("Search...<%1>", shortcutStr));
152
153
}

154
// slot
Laurent Montel's avatar
Laurent Montel committed
155
156
157
void SearchBar::setStringFilter(const QString &filter)
{
    d->lineEdit->setText(filter);
Frank Osterfeld's avatar
Frank Osterfeld committed
158
}
159
160

// slot
161
void SearchBar::setKeyFilter(const std::shared_ptr<KeyFilter> &kf)
Laurent Montel's avatar
Laurent Montel committed
162
163
164
165
166
167
168
{
    const QModelIndex idx = KeyFilterManager::instance()->toModelIndex(kf);
    if (idx.isValid()) {
        d->combo->setCurrentIndex(idx.row());
    } else {
        d->combo->setCurrentIndex(0);
    }
169
170
}

171
// slot
Laurent Montel's avatar
Laurent Montel committed
172
173
174
void SearchBar::setChangeStringFilterEnabled(bool on)
{
    d->lineEdit->setEnabled(on);
175
176
177
}

// slot
Laurent Montel's avatar
Laurent Montel committed
178
179
180
void SearchBar::setChangeKeyFilterEnabled(bool on)
{
    d->combo->setEnabled(on);
181
182
}

Andre Heinecke's avatar
Andre Heinecke committed
183
QLineEdit *SearchBar::lineEdit() const
Laurent Montel's avatar
Laurent Montel committed
184
{
185
186
187
    return d->lineEdit;
}

188
189
#include "moc_searchbar.cpp"