aboutsummaryrefslogtreecommitdiffstats
path: root/keydetailsdialog.cpp
blob: 64d8420439781638e7c24a3ca1a80a50c67d6681 (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
223
224
225
/*
 *      keydetailsdialog.cpp
 *
 *      Copyright 2008 gpg4usb-team <[email protected]>
 *
 *      This file is part of gpg4usb.
 *
 *      Gpg4usb 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 3 of the License, or
 *      (at your option) any later version.
 *
 *      Gpg4usb 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 gpg4usb.  If not, see <http://www.gnu.org/licenses/>
 */

#include "keydetailsdialog.h"

KeyDetailsDialog::KeyDetailsDialog(GpgME::GpgContext* ctx, KgpgCore::KgpgKey key, QWidget *parent)
    : QDialog(parent)
{
    mCtx = ctx;
    keyid = new QString(key.id());

    ownerBox = new QGroupBox(tr("Owner details"));
    keyBox = new QGroupBox(tr("Key details"));
    fingerprintBox = new QGroupBox(tr("Fingerprint"));
    additionalUidBox = new QGroupBox(tr("Additional Uids"));
    buttonBox = new QDialogButtonBox(QDialogButtonBox::Close);
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(close()));

    nameVarLabel = new QLabel(key.name());
    nameVarLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
    emailVarLabel = new QLabel(key.email());
    emailVarLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);

    commentVarLabel = new QLabel(key.comment());
    commentVarLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
    keyidVarLabel = new QLabel(key.id());
    keyidVarLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);

    QString keySizeVal, keyExpireVal, keyCreatedVal, keyAlgoVal;

    if (key.expirationDate().isNull()) {
        keyExpireVal = tr("Never");
    } else {
        keyExpireVal = key.expirationDate().toString("dd. MMM. yyyy");
    }

    keyAlgoVal = key.algorithm();
    keyCreatedVal = key.creationDate().toString("dd. MMM. yyyy");

    // have el-gamal key?
    /*if (key->subkeys->next) {
        keySizeVal.sprintf("%d / %d",  int(key->subkeys->length), int(key->subkeys->next->length));
        if (key->subkeys->next->expires == 0) {
            keyExpireVal += tr(" / Never");
        } else {
            keyExpireVal += " / " + QDateTime::fromTime_t(key->subkeys->next->expires).toString("dd. MMM. yyyy");
        }
        keyAlgoVal.append(" / ").append(gpgme_pubkey_algo_name(key->subkeys->next->pubkey_algo));
        keyCreatedVal += " / " + QDateTime::fromTime_t(key->subkeys->next->timestamp).toString("dd. MMM. yyyy");
    } else {
        keySizeVal.setNum(int(key->subkeys->length));
    }*/

    keySizeVarLabel = new QLabel(keySizeVal);
    expireVarLabel = new QLabel(keyExpireVal);
    createdVarLabel = new QLabel(keyCreatedVal);
    algorithmVarLabel = new QLabel(keyAlgoVal);

    QVBoxLayout *mvbox = new QVBoxLayout();
    QGridLayout *vboxKD = new QGridLayout();
    QGridLayout *vboxOD = new QGridLayout();

    vboxOD->addWidget(new QLabel(tr("Name:")), 0, 0);
    vboxOD->addWidget(new QLabel(tr("E-Mailaddress:")), 1, 0);
    vboxOD->addWidget(new QLabel(tr("Comment:")), 2, 0);
    vboxOD->addWidget(nameVarLabel, 0, 1);
    vboxOD->addWidget(emailVarLabel, 1, 1);
    vboxOD->addWidget(commentVarLabel, 2, 1);

    vboxKD->addWidget(new QLabel(tr("Key size:")), 0, 0);
    vboxKD->addWidget(new QLabel(tr("Expires on: ")), 1, 0);
    vboxKD->addWidget(new QLabel(tr("Algorithm: ")), 3, 0);
    vboxKD->addWidget(new QLabel(tr("Created on: ")), 4, 0);
    vboxKD->addWidget(new QLabel(tr("Key ID: ")), 5, 0);
    vboxKD->addWidget(keySizeVarLabel, 0, 1);
    vboxKD->addWidget(expireVarLabel, 1, 1);
    vboxKD->addWidget(algorithmVarLabel, 3, 1);
    vboxKD->addWidget(createdVarLabel, 4, 1);
    vboxKD->addWidget(keyidVarLabel, 5, 1);

    ownerBox->setLayout(vboxOD);
    mvbox->addWidget(ownerBox);

    keyBox->setLayout(vboxKD);
    mvbox->addWidget(keyBox);

    fingerPrintVarLabel = new QLabel(key.fingerprintBeautified());
    fingerPrintVarLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
    fingerPrintVarLabel->setStyleSheet("margin-left: 20; margin-right: 20;");
    QHBoxLayout *hboxFP = new QHBoxLayout();

    hboxFP->addWidget(fingerPrintVarLabel);
    QIcon ico(":button_copy.png");

    QPushButton copyFingerprintButton(QIcon(ico.pixmap(12, 12)), "");
    //copyFingerprintButton.setStyleSheet("QPushButton {border: 0px; } QPushButton:Pressed {}  ");
    copyFingerprintButton.setFlat(true);
    copyFingerprintButton.setToolTip(tr("copy fingerprint to clipboard"));
    connect(&copyFingerprintButton, SIGNAL(clicked()), this, SLOT(copyFingerprint()));

    hboxFP->addWidget(&copyFingerprintButton);

    fingerprintBox->setLayout(hboxFP);
    mvbox->addWidget(fingerprintBox);

    // If key has more than primary uid, also show the other uids
    /*gpgme_user_id_t addUserIds = key->uids->next;
    if (addUserIds !=NULL) {
        QVBoxLayout *vboxUID = new QVBoxLayout();
        while (addUserIds != NULL){
            addUserIdsVarLabel = new QLabel(addUserIds->name+ QString(" <")+addUserIds->email+">");
            addUserIdsVarLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
            vboxUID->addWidget(addUserIdsVarLabel);
            addUserIds=addUserIds->next;
        }
        additionalUidBox->setLayout(vboxUID);
        mvbox->addWidget(additionalUidBox);
    }*/

    if (key.secret()) {
        QGroupBox *privKeyBox = new QGroupBox(tr("Private Key"));
        QVBoxLayout *vboxPK = new QVBoxLayout();

        QPushButton *exportButton = new QPushButton(tr("Export Private Key"));
        vboxPK->addWidget(exportButton);
        connect(exportButton, SIGNAL(clicked()), this, SLOT(exportPrivateKey()));

        privKeyBox->setLayout(vboxPK);
        mvbox->addWidget(privKeyBox);
    }

    /*if((key->expired) || (key->revoked)) {
        QHBoxLayout *expBox = new QHBoxLayout();
        QIcon icon = QIcon::fromTheme("dialog-warning");
        QPixmap pixmap = icon.pixmap(QSize(32,32),QIcon::Normal,QIcon::On);

        QLabel *expLabel = new QLabel();
        QLabel *iconLabel = new QLabel();
        if (key->expired) {
            expLabel->setText(tr("Warning: Key expired"));
        }
        if (key->revoked) {
            expLabel->setText(tr("Warning: Key revoked"));
        }

        iconLabel->setPixmap(pixmap);
        QFont font = expLabel->font();
        font.setBold(true);
        expLabel->setFont(font);
        expBox->addWidget(iconLabel);
        expBox->addWidget(expLabel);
        mvbox->addLayout(expBox);
    }*/

    mvbox->addWidget(buttonBox);

    this->setLayout(mvbox);
    this->setWindowTitle(tr("Keydetails"));
    this->setModal(true);
    this->show();

    exec();
}

void KeyDetailsDialog::exportPrivateKey()
{
    // Show a information box with explanation about private key
    int ret = QMessageBox::information(this, tr("Exporting private Key"),
                                       tr("You are about to export your private key.\n"
                                          "This is NOT your public key, so don't give it away.\n"
                                          "Make sure you keep it save."
                                          "Do you really want to export your private key?"),
                                       QMessageBox::Cancel | QMessageBox::Ok);

    // export key, if ok was clicked
    if (ret == QMessageBox::Ok) {
        QByteArray *keyArray = new QByteArray();
        mCtx->exportSecretKey(*keyid, keyArray);
        KgpgCore::KgpgKey key = mCtx->getKeyDetails(*keyid);
        QString fileString = key.name() + " " + key.email() + "(" + key.id()+ ")_pub_sec.asc";
        QString fileName = QFileDialog::getSaveFileName(this, tr("Export Key To File"), fileString, tr("Key Files") + " (*.asc *.txt);;All Files (*)");
        QFile file(fileName);
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
            QMessageBox::critical(0,tr("Export error"),tr("Couldn't open %1 for writing").arg(fileName));
            return;
        }
        QTextStream stream(&file);
        stream << *keyArray;
        file.close();
        delete keyArray;
    }
}

QString KeyDetailsDialog::beautifyFingerprint(QString fingerprint)
{
    uint len = fingerprint.length();
    if ((len > 0) && (len % 4 == 0))
        for (uint n = 0; 4 *(n + 1) < len; ++n)
            fingerprint.insert(5 * n + 4, ' ');
    return fingerprint;
}

void KeyDetailsDialog::copyFingerprint() {
    QString fpr = fingerPrintVarLabel->text().trimmed().replace(" ", "");
    QClipboard *cb = QApplication::clipboard();
    cb->setText(fpr);
}