1 | #include"pgptest.h"
|
---|
2 |
|
---|
3 | #include<qlayout.h>
|
---|
4 | #include<qmessagebox.h>
|
---|
5 | #include<qinputdialog.h>
|
---|
6 | #include<qapplication.h>
|
---|
7 | #include<qguardedptr.h>
|
---|
8 |
|
---|
9 | static QByteArray stringToArray(const QString &s)
|
---|
10 | {
|
---|
11 | QByteArray a;
|
---|
12 | QCString cs = s.utf8();
|
---|
13 | a.resize(cs.length());
|
---|
14 | memcpy(a.data(), cs.data(), a.size());
|
---|
15 | return a;
|
---|
16 | }
|
---|
17 |
|
---|
18 | QString plain2rich(const QString &plain, bool nobr=false);
|
---|
19 |
|
---|
20 | QString plain2rich(const QString &plain, bool nobr)
|
---|
21 | {
|
---|
22 | QString rich;
|
---|
23 | int col = 0;
|
---|
24 |
|
---|
25 | for(int i = 0; i < (int)plain.length(); ++i) {
|
---|
26 | if(nobr && col == 0)
|
---|
27 | rich += "<nobr>";
|
---|
28 |
|
---|
29 | if(plain[i] == '\n') {
|
---|
30 | if(nobr)
|
---|
31 | rich += "</nobr>";
|
---|
32 | rich += "<br>";
|
---|
33 | col = 0;
|
---|
34 | continue;
|
---|
35 | }
|
---|
36 | else if(plain[i] == '\t') {
|
---|
37 | rich += QChar::nbsp;
|
---|
38 | while(col % 4) {
|
---|
39 | rich += QChar::nbsp;
|
---|
40 | ++col;
|
---|
41 | }
|
---|
42 | }
|
---|
43 | else if(plain[i].isSpace()) {
|
---|
44 | if(i > 0 && plain[i-1] == ' ')
|
---|
45 | rich += QChar::nbsp;
|
---|
46 | else
|
---|
47 | rich += ' ';
|
---|
48 | }
|
---|
49 | else if(plain[i] == '<')
|
---|
50 | rich += "<";
|
---|
51 | else if(plain[i] == '>')
|
---|
52 | rich += ">";
|
---|
53 | else if(plain[i] == '\"')
|
---|
54 | rich += """;
|
---|
55 | else if(plain[i] == '\'')
|
---|
56 | rich += "'";
|
---|
57 | else if(plain[i] == '&')
|
---|
58 | rich += "&";
|
---|
59 | else
|
---|
60 | rich += plain[i];
|
---|
61 | ++col;
|
---|
62 | }
|
---|
63 |
|
---|
64 | if(col > 0 && nobr)
|
---|
65 | rich += "</nobr>";
|
---|
66 |
|
---|
67 | return rich;
|
---|
68 | }
|
---|
69 |
|
---|
70 | //----------------------------------------------------------------------------
|
---|
71 | // View
|
---|
72 | //----------------------------------------------------------------------------
|
---|
73 | View::View(OpenPGP::Engine *_pgp, QWidget *parent)
|
---|
74 | :QDialog(parent, 0, false, WDestructiveClose)
|
---|
75 | {
|
---|
76 | pix_key = new QPixmap("key.png");
|
---|
77 | pgp = _pgp;
|
---|
78 | connect(pgp, SIGNAL(initFinished(bool, const QString &)), SLOT(pgp_initFinished(bool, const QString &)));
|
---|
79 |
|
---|
80 | QVBoxLayout *vb = new QVBoxLayout(this, 4);
|
---|
81 | lv = new QListView(this);
|
---|
82 | vb->addWidget(lv);
|
---|
83 | QHBoxLayout *hb = new QHBoxLayout(vb);
|
---|
84 | te = new QTextEdit(this);
|
---|
85 | hb->addWidget(te);
|
---|
86 | te2 = new QTextEdit(this);
|
---|
87 | hb->addWidget(te2);
|
---|
88 | hb = new QHBoxLayout(vb);
|
---|
89 | hb->addStretch(1);
|
---|
90 |
|
---|
91 | pb_encrypt = new QPushButton("&Encrypt", this);
|
---|
92 | connect(pb_encrypt, SIGNAL(clicked()), SLOT(encrypt()));
|
---|
93 | hb->addWidget(pb_encrypt);
|
---|
94 |
|
---|
95 | pb_decrypt = new QPushButton("&Decrypt", this);
|
---|
96 | connect(pb_decrypt, SIGNAL(clicked()), SLOT(decrypt()));
|
---|
97 | hb->addWidget(pb_decrypt);
|
---|
98 |
|
---|
99 | pb_sign = new QPushButton("&Sign", this);
|
---|
100 | connect(pb_sign, SIGNAL(clicked()), SLOT(sign()));
|
---|
101 | hb->addWidget(pb_sign);
|
---|
102 |
|
---|
103 | pb_verify = new QPushButton("&Verify", this);
|
---|
104 | connect(pb_verify, SIGNAL(clicked()), SLOT(verify()));
|
---|
105 | hb->addWidget(pb_verify);
|
---|
106 |
|
---|
107 | QPushButton *pb = new QPushButton("&Close", this);
|
---|
108 | connect(pb, SIGNAL(clicked()), SLOT(reject()));
|
---|
109 | hb->addWidget(pb);
|
---|
110 |
|
---|
111 | lv->addColumn("Key ID");
|
---|
112 | lv->addColumn("User ID");
|
---|
113 | lv->setAllColumnsShowFocus(true);
|
---|
114 | lv->setResizeMode(QListView::LastColumn);
|
---|
115 | resize(600,400);
|
---|
116 |
|
---|
117 | lv->setSelectionMode(QListView::Extended);
|
---|
118 |
|
---|
119 | disableButtons();
|
---|
120 | pgp->init();
|
---|
121 | }
|
---|
122 |
|
---|
123 | View::~View()
|
---|
124 | {
|
---|
125 | delete pgp;
|
---|
126 | delete pix_key;
|
---|
127 | }
|
---|
128 |
|
---|
129 | void View::enableButtons()
|
---|
130 | {
|
---|
131 | pb_encrypt->setEnabled(true);
|
---|
132 | pb_decrypt->setEnabled(true);
|
---|
133 | pb_sign->setEnabled(true);
|
---|
134 | pb_verify->setEnabled(true);
|
---|
135 | }
|
---|
136 |
|
---|
137 | void View::disableButtons()
|
---|
138 | {
|
---|
139 | pb_encrypt->setEnabled(false);
|
---|
140 | pb_decrypt->setEnabled(false);
|
---|
141 | pb_sign->setEnabled(false);
|
---|
142 | pb_verify->setEnabled(false);
|
---|
143 | }
|
---|
144 |
|
---|
145 | void View::pgp_initFinished(bool ok, const QString &str)
|
---|
146 | {
|
---|
147 | if(!ok) {
|
---|
148 | QMessageBox::information(this, tr("Error"), tr("Unable to load %1.\nReason: %2").arg(pgp->name()).arg(str));
|
---|
149 | close();
|
---|
150 | }
|
---|
151 | else {
|
---|
152 | enableButtons();
|
---|
153 | connect(pgp, SIGNAL(keysUpdated()), SLOT(pgp_keysUpdated()));
|
---|
154 | loadKeys();
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | void View::encrypt()
|
---|
159 | {
|
---|
160 | QStringList recipients;
|
---|
161 | for(QListViewItem *i = lv->firstChild(); i; i = i->nextSibling()) {
|
---|
162 | if(i->isSelected())
|
---|
163 | recipients += i->text(0);
|
---|
164 | }
|
---|
165 | if(recipients.isEmpty()) {
|
---|
166 | QMessageBox::information(this, "Encrypt", "You must select a public key to encrypt with.");
|
---|
167 | return;
|
---|
168 | }
|
---|
169 |
|
---|
170 | OpenPGP::Request *r = new OpenPGP::Request(pgp);
|
---|
171 | connect(r, SIGNAL(finished(bool)), SLOT(pgp_finished(bool)));
|
---|
172 | disableButtons();
|
---|
173 | r->encrypt(stringToArray(te->text()), recipients);
|
---|
174 | }
|
---|
175 |
|
---|
176 | void View::decrypt()
|
---|
177 | {
|
---|
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()));
|
---|
181 | disableButtons();
|
---|
182 | r->decrypt(te->text());
|
---|
183 | }
|
---|
184 |
|
---|
185 | void View::sign()
|
---|
186 | {
|
---|
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()));
|
---|
190 | disableButtons();
|
---|
191 | QString defkey = pgp->secretKeys().first().keyID();
|
---|
192 | r->sign(stringToArray(te->text()), defkey);
|
---|
193 | }
|
---|
194 |
|
---|
195 | void View::verify()
|
---|
196 | {
|
---|
197 | OpenPGP::Request *r = new OpenPGP::Request(pgp);
|
---|
198 | connect(r, SIGNAL(finished(bool)), SLOT(pgp_finished(bool)));
|
---|
199 | disableButtons();
|
---|
200 | r->verify(stringToArray(te->text()), te2->text());
|
---|
201 | }
|
---|
202 |
|
---|
203 | void View::pgp_finished(bool ok)
|
---|
204 | {
|
---|
205 | OpenPGP::Request *r = (OpenPGP::Request *)sender();
|
---|
206 | if(r->type() == OpenPGP::Encrypt) {
|
---|
207 | if(!ok)
|
---|
208 | QMessageBox::information(this, tr("Encrypt"), tr("Encountered an error while encrypting."));
|
---|
209 | else
|
---|
210 | te2->setText(r->encrypted());
|
---|
211 | }
|
---|
212 | else if(r->type() == OpenPGP::Decrypt) {
|
---|
213 | if(!ok)
|
---|
214 | QMessageBox::information(this, tr("Encrypt"), tr("Encountered an error while decrypting."));
|
---|
215 | else {
|
---|
216 | QByteArray dec = r->decrypted();
|
---|
217 | te2->setText(QString::fromUtf8(dec.data(), dec.size()));
|
---|
218 | }
|
---|
219 | }
|
---|
220 | else if(r->type() == OpenPGP::Sign) {
|
---|
221 | if(!ok)
|
---|
222 | QMessageBox::information(this, tr("Sign"), tr("Encountered an error while signing."));
|
---|
223 | else
|
---|
224 | te2->setText(r->signature());
|
---|
225 | }
|
---|
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));
|
---|
235 | else
|
---|
236 | QMessageBox::information(this, tr("Verify"), tr("Encountered an error while verifying."));
|
---|
237 | }
|
---|
238 |
|
---|
239 | r->deleteLater();
|
---|
240 | enableButtons();
|
---|
241 | }
|
---|
242 |
|
---|
243 | void View::pgp_needPassphrase()
|
---|
244 | {
|
---|
245 | QGuardedPtr<OpenPGP::Request> r = (OpenPGP::Request *)sender();
|
---|
246 | bool ok;
|
---|
247 | QString pp = QInputDialog::getText("Passphrase", "Please enter your passphrase:", QLineEdit::Password, QString::null, &ok, this);
|
---|
248 | if(r) {
|
---|
249 | if(ok)
|
---|
250 | r->submitPassphrase(pp);
|
---|
251 | else {
|
---|
252 | r->deleteLater();
|
---|
253 | enableButtons();
|
---|
254 | }
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | void View::pgp_keysUpdated()
|
---|
259 | {
|
---|
260 | loadKeys();
|
---|
261 | }
|
---|
262 |
|
---|
263 | void View::loadKeys()
|
---|
264 | {
|
---|
265 | // display the keys
|
---|
266 | OpenPGP::KeyList pubkeys = pgp->publicKeys();
|
---|
267 | lv->clear();
|
---|
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());
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 |
|
---|
278 | //----------------------------------------------------------------------------
|
---|
279 | // main
|
---|
280 | //----------------------------------------------------------------------------
|
---|
281 | int main(int argc, char **argv)
|
---|
282 | {
|
---|
283 | QApplication app(argc, argv);
|
---|
284 |
|
---|
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()));
|
---|
288 | return 0;
|
---|
289 | }
|
---|
290 |
|
---|
291 | View *v = new View(pgp, 0);
|
---|
292 | app.setMainWidget(v);
|
---|
293 | v->show();
|
---|
294 | app.exec();
|
---|
295 | return 0;
|
---|
296 | }
|
---|