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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
/*
*
* keyserverimportdialog.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 "keyserverimportdialog.h"
KeyServerImportDialog::KeyServerImportDialog(GpgME::GpgContext *ctx, KeyList *keyList, QWidget *parent)
: QDialog(parent)
{
mCtx = ctx;
mKeyList = keyList;
// Buttons
closeButton = createButton(tr("&Close"), SLOT(close()));
importButton = createButton(tr("&Import"), SLOT(import()));
searchButton = createButton(tr("&Search"), SLOT(search()));
// Line edit for search string
searchLabel = new QLabel(tr("Search string:"));
searchLineEdit = new QLineEdit();
// combobox for keyserverlist
keyServerLabel = new QLabel(tr("Keyserver:"));
keyServerComboBox = createComboBox();
// table containing the keys found
createKeysTable();
message = new QLabel;
icon = new QLabel;
// Layout for messagebox
QHBoxLayout *messageLayout= new QHBoxLayout;
messageLayout->addWidget(icon);
messageLayout->addWidget(message);
messageLayout->addStretch();
// Layout for import and close button
QHBoxLayout *buttonsLayout = new QHBoxLayout;
buttonsLayout->addStretch();
buttonsLayout->addWidget(importButton);
buttonsLayout->addWidget(closeButton);
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(searchLabel, 1, 0);
mainLayout->addWidget(searchLineEdit, 1, 1);
mainLayout->addWidget(searchButton,1, 2);
mainLayout->addWidget(keyServerLabel, 2, 0);
mainLayout->addWidget(keyServerComboBox, 2, 1);
mainLayout->addWidget(keysTable, 3, 0, 1, 3);
mainLayout->addLayout(messageLayout, 4, 0, 1, 3);
mainLayout->addLayout(buttonsLayout, 5, 0, 1, 3);
this->setLayout(mainLayout);
this->setWindowTitle(tr("Import Keys from Keyserver"));
this->resize(700, 300);
this->setModal(true);
}
QPushButton *KeyServerImportDialog::createButton(const QString &text, const char *member)
{
QPushButton *button = new QPushButton(text);
connect(button, SIGNAL(clicked()), this, member);
return button;
}
QComboBox *KeyServerImportDialog::createComboBox()
{
QComboBox *comboBox = new QComboBox;
comboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
// Read keylist from ini-file and fill it into combobox
QSettings settings;
comboBox->addItems(settings.value("keyserver/keyServerList").toStringList());
// set default keyserver in combobox
QString keyserver = settings.value("keyserver/defaultKeyServer").toString();
comboBox->setCurrentIndex(comboBox->findText(keyserver));
return comboBox;
}
void KeyServerImportDialog::createKeysTable()
{
keysTable = new QTableWidget();
keysTable->setColumnCount(4);
// always a whole row is marked
keysTable->setSelectionBehavior(QAbstractItemView::SelectRows);
keysTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
// Make just one row selectable
keysTable->setSelectionMode(QAbstractItemView::SingleSelection);
QStringList labels;
labels << tr("UID") << tr("Creation date") << tr("KeyID") << tr("Tag");
keysTable->horizontalHeader()->setResizeMode(0, QHeaderView::ResizeToContents);
keysTable->setHorizontalHeaderLabels(labels);
keysTable->verticalHeader()->hide();
connect(keysTable, SIGNAL(cellActivated(int,int)),
this, SLOT(import()));
}
void KeyServerImportDialog::setMessage(const QString &text, bool error)
{
message->setText(text);
if (error) {
QIcon undoicon = QIcon::fromTheme("dialog-error");
QPixmap pixmap = undoicon.pixmap(QSize(32,32),QIcon::Normal,QIcon::On);
icon->setPixmap(pixmap);
} else {
QIcon undoicon = QIcon::fromTheme("dialog-information");
QPixmap pixmap = undoicon.pixmap(QSize(32,32),QIcon::Normal,QIcon::On);
icon->setPixmap(pixmap);
}
}
void KeyServerImportDialog::search()
{
QUrl url = keyServerComboBox->currentText()+":11371/pks/lookup?search="+searchLineEdit->text()+"&op=index&options=mr";
qnam = new QNetworkAccessManager(this);
QNetworkReply* reply = qnam->get(QNetworkRequest(url));
connect(reply, SIGNAL(finished()),
this, SLOT(searchFinished()));
}
void KeyServerImportDialog::searchFinished()
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
keysTable->clearContents();
keysTable->setRowCount(0);
QString firstLine = QString(reply->readLine(1024));
QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (reply->error()) {
setMessage(tr("Couldn't contact keyserver!"),true);
//setMessage(reply->error());
qDebug() << reply->error();
}
if (firstLine.contains("Error"))
{
QString text= QString(reply->readLine(1024));
if (text.contains("Too many responses")) {
setMessage(tr("Too many responses from keyserver!"),true);
} else if (text.contains("No keys found")) {
// if string looks like hex string, search again with 0x prepended
QRegExp rx("[0-9A-Fa-f]*");
QString query = searchLineEdit->text();
if (rx.exactMatch(query)) {
setMessage(tr("No keys found, input may be kexId, retrying search with 0x."),true);
searchLineEdit->setText(query.prepend("0x"));
this->search();
} else {
setMessage(tr("No keys found containing the search string!"),true);
}
} else if (text.contains("Insufficiently specific words")) {
setMessage(tr("Insufficiently specific search string!"),true);
} else {
setMessage(text, true);
}
} else {
int row = 0;
char buff[1024];
bool strikeout=false;
while (reply->readLine(buff,sizeof(buff)) !=-1) {
QString decoded = QString::fromUtf8(QByteArray::fromPercentEncoding(buff));
QStringList line = decoded.split(":");
//TODO: have a look at two following pub lines
if (line[0] == "pub") {
strikeout=false;
QString flags = line[line.size()-1];
keysTable->setRowCount(row+1);
// flags can be "d" for disabled, "r" for revoked
// or "e" for expired
if (flags.contains("r") or flags.contains("d") or flags.contains("e")) {
strikeout=true;
if (flags.contains("e")) {
keysTable->setItem(row, 3, new QTableWidgetItem( QString("expired")));
}
if (flags.contains("r")) {
keysTable->setItem(row, 3, new QTableWidgetItem( QString(tr("revoked"))));
}
if (flags.contains("d")) {
keysTable->setItem(row, 3, new QTableWidgetItem( QString(tr("disabled"))));
}
}
QStringList line2 = QString(reply->readLine()).split(":");
QTableWidgetItem *uid = new QTableWidgetItem();
if (line2.size() > 1) {
uid->setText(line2[1]);
keysTable->setItem(row, 0, uid);
}
QTableWidgetItem *creationdate = new QTableWidgetItem(QDateTime::fromTime_t(line[4].toInt()).toString("dd. MMM. yyyy"));
keysTable->setItem(row, 1, creationdate);
QTableWidgetItem *keyid = new QTableWidgetItem(line[1]);
keysTable->setItem(row, 2, keyid);
if (strikeout) {
QFont strike = uid->font();
strike.setStrikeOut(true);
uid->setFont(strike);
creationdate->setFont(strike);
keyid->setFont(strike);
}
row++;
} else {
if (line[0] == "uid") {
QStringList l;
int height=keysTable->rowHeight(row-1);
keysTable->setRowHeight(row-1,height+16);
QString tmp=keysTable->item(row-1,0)->text();
tmp.append(QString("\n")+line[1]);
QTableWidgetItem *tmp1 = new QTableWidgetItem(tmp);
keysTable->setItem(row-1,0,tmp1);
if (strikeout) {
QFont strike = tmp1->font();
strike.setStrikeOut(true);
tmp1->setFont(strike);
}
}
}
setMessage(tr("%1 keys found. Doubleclick a key to import it.").arg(row),false);
}
keysTable->resizeColumnsToContents();
}
reply->deleteLater();
reply = 0;
}
void KeyServerImportDialog::import()
{
if ( keysTable->currentRow() > -1 ) {
QString keyid = keysTable->item(keysTable->currentRow(),2)->text();
QUrl url = keyServerComboBox->currentText();
import(QStringList(keyid), url);
}
}
void KeyServerImportDialog::import(QStringList keyIds)
{
QSettings settings;
QString keyserver=settings.value("keyserver/defaultKeyServer").toString();
QUrl url(keyserver);
import(keyIds, url);
}
void KeyServerImportDialog::import(QStringList keyIds, QUrl keyServerUrl)
{
foreach(QString keyId, keyIds) {
QUrl reqUrl(keyServerUrl.scheme() + "://" + keyServerUrl.host() + ":11371/pks/lookup?op=get&search=0x"+keyId+"&options=mr");
//qDebug() << "req to " << reqUrl;
qnam = new QNetworkAccessManager(this);
QNetworkReply *reply = qnam->get(QNetworkRequest(reqUrl));
connect(reply, SIGNAL(finished()),
this, SLOT(importFinished()));
}
}
void KeyServerImportDialog::importFinished()
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
QByteArray key = reply->readAll();
QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (reply->error()) {
setMessage(tr("Error while contacting keyserver!"),true);
return;
}
this->importKeys(key.constData());
setMessage(tr("Key imported"),false);
// Add keyserver to list in config-file, if it isn't contained
QSettings settings;
QStringList keyServerList = settings.value("keyserver/keyServerList").toStringList();
if (!keyServerList.contains(keyServerComboBox->currentText()))
{
keyServerList.append(keyServerComboBox->currentText());
settings.setValue("keyserver/keyServerList", keyServerList);
}
reply->deleteLater();
reply = 0;
}
void KeyServerImportDialog::importKeys(QByteArray inBuffer)
{
GpgImportInformation result = mCtx->importKey(inBuffer);
new KeyImportDetailDialog(mCtx, result, this);
}
|