4
#include<qmessagebox.h>
5
#include<qinputdialog.h>
6
#include<qapplication.h>
7
#include<qguardedptr.h>
9
static QByteArray stringToArray(const QString &s)
12
QCString cs = s.utf8();
13
a.resize(cs.length());
14
memcpy(a.data(), cs.data(), a.size());
18
QString plain2rich(const QString &plain, bool nobr=false);
20
QString plain2rich(const QString &plain, bool nobr)
25
for(int i = 0; i < (int)plain.length(); ++i) {
29
if(plain[i] == '\n') {
36
else if(plain[i] == '\t') {
43
else if(plain[i].isSpace()) {
44
if(i > 0 && plain[i-1] == ' ')
49
else if(plain[i] == '<')
51
else if(plain[i] == '>')
53
else if(plain[i] == '\"')
55
else if(plain[i] == '\'')
57
else if(plain[i] == '&')
70
//----------------------------------------------------------------------------
72
//----------------------------------------------------------------------------
73
View::View(OpenPGP::Engine *_pgp, QWidget *parent)
74
:QDialog(parent, 0, false, WDestructiveClose)
76
pix_key = new QPixmap("key.png");
78
connect(pgp, SIGNAL(initFinished(bool, const QString &)), SLOT(pgp_initFinished(bool, const QString &)));
80
QVBoxLayout *vb = new QVBoxLayout(this, 4);
81
lv = new QListView(this);
83
QHBoxLayout *hb = new QHBoxLayout(vb);
84
te = new QTextEdit(this);
86
te2 = new QTextEdit(this);
88
hb = new QHBoxLayout(vb);
91
pb_encrypt = new QPushButton("&Encrypt", this);
92
connect(pb_encrypt, SIGNAL(clicked()), SLOT(encrypt()));
93
hb->addWidget(pb_encrypt);
95
pb_decrypt = new QPushButton("&Decrypt", this);
96
connect(pb_decrypt, SIGNAL(clicked()), SLOT(decrypt()));
97
hb->addWidget(pb_decrypt);
99
pb_sign = new QPushButton("&Sign", this);
100
connect(pb_sign, SIGNAL(clicked()), SLOT(sign()));
101
hb->addWidget(pb_sign);
103
pb_verify = new QPushButton("&Verify", this);
104
connect(pb_verify, SIGNAL(clicked()), SLOT(verify()));
105
hb->addWidget(pb_verify);
107
QPushButton *pb = new QPushButton("&Close", this);
108
connect(pb, SIGNAL(clicked()), SLOT(reject()));
111
lv->addColumn("Key ID");
112
lv->addColumn("User ID");
113
lv->setAllColumnsShowFocus(true);
114
lv->setResizeMode(QListView::LastColumn);
117
lv->setSelectionMode(QListView::Extended);
129
void View::enableButtons()
131
pb_encrypt->setEnabled(true);
132
pb_decrypt->setEnabled(true);
133
pb_sign->setEnabled(true);
134
pb_verify->setEnabled(true);
137
void View::disableButtons()
139
pb_encrypt->setEnabled(false);
140
pb_decrypt->setEnabled(false);
141
pb_sign->setEnabled(false);
142
pb_verify->setEnabled(false);
145
void View::pgp_initFinished(bool ok, const QString &str)
148
QMessageBox::information(this, tr("Error"), tr("Unable to load %1.\nReason: %2").arg(pgp->name()).arg(str));
153
connect(pgp, SIGNAL(keysUpdated()), SLOT(pgp_keysUpdated()));
160
QStringList recipients;
161
for(QListViewItem *i = lv->firstChild(); i; i = i->nextSibling()) {
163
recipients += i->text(0);
165
if(recipients.isEmpty()) {
166
QMessageBox::information(this, "Encrypt", "You must select a public key to encrypt with.");
170
OpenPGP::Request *r = new OpenPGP::Request(pgp);
171
connect(r, SIGNAL(finished(bool)), SLOT(pgp_finished(bool)));
173
r->encrypt(stringToArray(te->text()), recipients);
178
OpenPGP::Request *r = new OpenPGP::Request(pgp);
179
connect(r, SIGNAL(finished(bool)), SLOT(pgp_finished(bool)));
180
connect(r, SIGNAL(needPassphrase()), SLOT(pgp_needPassphrase()));
182
r->decrypt(te->text());
187
OpenPGP::Request *r = new OpenPGP::Request(pgp);
188
connect(r, SIGNAL(finished(bool)), SLOT(pgp_finished(bool)));
189
connect(r, SIGNAL(needPassphrase()), SLOT(pgp_needPassphrase()));
191
QString defkey = pgp->secretKeys().first().keyID();
192
r->sign(stringToArray(te->text()), defkey);
197
OpenPGP::Request *r = new OpenPGP::Request(pgp);
198
connect(r, SIGNAL(finished(bool)), SLOT(pgp_finished(bool)));
200
r->verify(stringToArray(te->text()), te2->text());
203
void View::pgp_finished(bool ok)
205
OpenPGP::Request *r = (OpenPGP::Request *)sender();
206
if(r->type() == OpenPGP::Encrypt) {
208
QMessageBox::information(this, tr("Encrypt"), tr("Encountered an error while encrypting."));
210
te2->setText(r->encrypted());
212
else if(r->type() == OpenPGP::Decrypt) {
214
QMessageBox::information(this, tr("Encrypt"), tr("Encountered an error while decrypting."));
216
QByteArray dec = r->decrypted();
217
te2->setText(QString::fromUtf8(dec.data(), dec.size()));
220
else if(r->type() == OpenPGP::Sign) {
222
QMessageBox::information(this, tr("Sign"), tr("Encountered an error while signing."));
224
te2->setText(r->signature());
226
else if(r->type() == OpenPGP::Verify) {
227
int x = r->verifyResult();
228
QString keyID = r->keyID();
229
if(x == OpenPGP::VerifyGood)
230
QMessageBox::information(this, "Verify", tr("<nobr>GOOD signature from <b>%1</b>.</nobr>").arg(keyID));
231
else if(x == OpenPGP::VerifyBad)
232
QMessageBox::information(this, "Verify", tr("<nobr>BAD signature from <b>%1</b>.</nobr>").arg(keyID));
233
else if(x == OpenPGP::VerifyNoKey)
234
QMessageBox::information(this, "Verify", tr("<nobr>Cannot verify without key <b>%1</b>.</nobr>").arg(keyID));
236
QMessageBox::information(this, tr("Verify"), tr("Encountered an error while verifying."));
243
void View::pgp_needPassphrase()
245
QGuardedPtr<OpenPGP::Request> r = (OpenPGP::Request *)sender();
247
QString pp = QInputDialog::getText("Passphrase", "Please enter your passphrase:", QLineEdit::Password, QString::null, &ok, this);
250
r->submitPassphrase(pp);
258
void View::pgp_keysUpdated()
263
void View::loadKeys()
266
OpenPGP::KeyList pubkeys = pgp->publicKeys();
268
for(OpenPGP::KeyList::ConstIterator it = pubkeys.begin(); it != pubkeys.end(); ++it) {
269
const OpenPGP::Key &key = *it;
270
QListViewItem *lvi = new QListViewItem(lv);
271
lvi->setPixmap(0, *pix_key);
272
lvi->setText(0, key.keyID());
273
lvi->setText(1, key.userID());
278
//----------------------------------------------------------------------------
280
//----------------------------------------------------------------------------
281
int main(int argc, char **argv)
283
QApplication app(argc, argv);
285
OpenPGP::Engine *pgp = OpenPGP::createEngine("gpg");
286
if(!pgp->checkAvailability()) {
287
QMessageBox::information(0, QObject::tr("Error"), QObject::tr("%1 is not available.").arg(pgp->name()));
291
View *v = new View(pgp, 0);
292
app.setMainWidget(v);