• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

kgpg

  • sources
  • kde-4.14
  • kdeutils
  • kgpg
  • editor
kgpgtextedit.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2002 Jean-Baptiste Mardelle <bj@altern.org>
3  * Copyright (C) 2009,2010,2011 Rolf Eike Beer <kde@opensource.sf-tec.de>
4  */
5 
6 /***************************************************************************
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  ***************************************************************************/
14 
15 #include "kgpgtextedit.h"
16 
17 #include "selectsecretkey.h"
18 #include "kgpgsettings.h"
19 #include "keyservers.h"
20 #include "selectpublickeydialog.h"
21 #include "detailedconsole.h"
22 #include "keysmanager.h"
23 #include "editor/kgpgeditor.h"
24 #include "transactions/kgpgdecrypt.h"
25 #include "transactions/kgpgencrypt.h"
26 #include "transactions/kgpgimport.h"
27 #include "transactions/kgpgsigntext.h"
28 #include "transactions/kgpgverify.h"
29 
30 #include <KLocale>
31 #include <KMessageBox>
32 #include <QDragEnterEvent>
33 #include <QDropEvent>
34 #include <QFile>
35 #include <QTextCodec>
36 #include <QTextStream>
37 #include <kio/netaccess.h>
38 
39 #define SIGNEDMESSAGE_BEGIN QLatin1String( "-----BEGIN PGP SIGNED MESSAGE-----" )
40 #define SIGNEDMESSAGE_END QLatin1String( "-----END PGP SIGNATURE-----" )
41 
42 KgpgTextEdit::KgpgTextEdit(QWidget *parent, KGpgItemModel *model, KeysManager *manager)
43  : KTextEdit(parent),
44  m_posstart(-1),
45  m_posend(-1),
46  m_model(model),
47  m_keysmanager(manager)
48 {
49  setCheckSpellingEnabled(true);
50  setAcceptDrops(true);
51  setReadOnly(false);
52  setUndoRedoEnabled(true);
53 }
54 
55 KgpgTextEdit::~KgpgTextEdit()
56 {
57 }
58 
59 void KgpgTextEdit::dragEnterEvent(QDragEnterEvent *e)
60 {
61  // if a file is dragged into editor ...
62  if (KUrl::List::canDecode(e->mimeData()) || e->mimeData()->hasText())
63  e->acceptProposedAction();
64 }
65 
66 void KgpgTextEdit::dropEvent(QDropEvent *e)
67 {
68  // decode dropped file or dropped text
69  KUrl::List uriList = KUrl::List::fromMimeData(e->mimeData());
70  if (!uriList.isEmpty())
71  slotDroppedFile(uriList.first());
72  else
73  if (e->mimeData()->hasText())
74  insertPlainText(e->mimeData()->text());
75 }
76 
77 void KgpgTextEdit::slotDroppedFile(const KUrl &url)
78 {
79  openDroppedFile(url, true);
80 }
81 
82 void KgpgTextEdit::openDroppedFile(const KUrl& url, const bool probe)
83 {
84  KUrl tmpurl;
85 
86  if (url.isLocalFile()) {
87  m_tempfile = url.path();
88  tmpurl = url;
89  } else {
90  if (KMessageBox::warningContinueCancel(this, i18n("<qt><b>Remote file dropped</b>.<br />The remote file will now be copied to a temporary file to process requested operation. This temporary file will be deleted after operation.</qt>"), QString(), KStandardGuiItem::cont(), KStandardGuiItem::cancel(), QLatin1String( "RemoteFileWarning" )) != KMessageBox::Continue)
91  return;
92 
93  if (!KIO::NetAccess::download(url, m_tempfile, this)) {
94  KMessageBox::sorry(this, i18n("Could not download file."));
95  return;
96  }
97  tmpurl = KUrl::fromPath(m_tempfile);
98  }
99 
100  QString result;
101 
102  QFile qfile(m_tempfile);
103  if (qfile.open(QIODevice::ReadOnly)) {
104  QTextStream t(&qfile);
105  result = t.readAll();
106  qfile.close();
107  }
108 
109  if (result.isEmpty())
110  return;
111 
112  if (!probe || KGpgDecrypt::isEncryptedText(result, &m_posstart, &m_posend)) {
113  // if pgp data found, decode it
114  KGpgDecrypt *decr = new KGpgDecrypt(this, KUrl::List(tmpurl));
115  connect(decr, SIGNAL(done(int)), SLOT(slotDecryptDone(int)));
116  decr->start();
117  return;
118  }
119  // remove only here, as KGpgDecrypt will use and remove the file itself
120  KIO::NetAccess::removeTempFile(m_tempfile);
121  m_tempfile.clear();
122 
123  QString tmpinfo;
124 
125  switch (KGpgImport::isKey(result)) {
126  case 1:
127  tmpinfo = i18n("<qt>This file is a <b>public</b> key.<br />Do you want to import it instead of opening it in editor?</qt>");
128  break;
129  case 2:
130  tmpinfo = i18n("<qt>This file is a <b>private</b> key.<br />Do you want to import it instead of opening it in editor?</qt>");
131  break;
132  }
133 
134  if (!tmpinfo.isEmpty()) {
135  if (KMessageBox::questionYesNo(this, tmpinfo, i18n("Key file dropped on Editor")) != KMessageBox::Yes) {
136  setPlainText(result);
137  } else {
138  KGpgImport *imp = new KGpgImport(this, result);
139  connect(imp, SIGNAL(done(int)), m_keysmanager, SLOT(slotImportDone(int)));
140  imp->start();
141  }
142  } else {
143  if (m_posstart != -1) {
144  Q_ASSERT(m_posend != -1);
145  QString fullcontent = toPlainText();
146  fullcontent.replace(m_posstart, m_posend - m_posstart, result);
147  setPlainText(fullcontent);
148  m_posstart = -1;
149  m_posend = -1;
150  } else {
151  setPlainText(result);
152  }
153  }
154 }
155 
156 void KgpgTextEdit::slotEncode()
157 {
158  QPointer<KgpgSelectPublicKeyDlg> dialog = new KgpgSelectPublicKeyDlg(this, m_model, m_keysmanager->goDefaultShortcut(), true);
159  if (dialog->exec() == KDialog::Accepted) {
160  QStringList options;
161  KGpgEncrypt::EncryptOptions opts = KGpgEncrypt::DefaultEncryption;
162 
163  if (dialog->getArmor())
164  opts |= KGpgEncrypt::AsciiArmored;
165 
166  if (dialog->getUntrusted())
167  opts |= KGpgEncrypt::AllowUntrustedEncryption;
168 
169  if (dialog->getHideId())
170  opts |= KGpgEncrypt::HideKeyId;
171 
172  if (KGpgSettings::allowCustomEncryptionOptions()) {
173  const QString customoptions = dialog->getCustomOptions();
174  if (!customoptions.isEmpty())
175  options << customoptions.split(QLatin1Char(' '), QString::SkipEmptyParts);
176  }
177 
178  if (KGpgSettings::pgpCompatibility())
179  options << QLatin1String( "--pgp6" );
180 
181  QStringList listkeys;
182  if (!dialog->getSymmetric())
183  listkeys = dialog->selectedKeys();
184 
185  KGpgEncrypt *encr = new KGpgEncrypt(this, listkeys, toPlainText(), opts, options);
186  encr->start();
187  connect(encr, SIGNAL(done(int)), SLOT(slotEncodeUpdate(int)));
188  }
189  delete dialog;
190 }
191 
192 void KgpgTextEdit::slotDecode()
193 {
194  const QString fullcontent = toPlainText();
195 
196  if (!KGpgDecrypt::isEncryptedText(fullcontent, &m_posstart, &m_posend))
197  return;
198 
199  KGpgDecrypt *decr = new KGpgDecrypt(this, fullcontent.mid(m_posstart, m_posend - m_posstart));
200  connect(decr, SIGNAL(done(int)), SLOT(slotDecryptDone(int)));
201  decr->start();
202 }
203 
204 void KgpgTextEdit::slotSign(const QString &message)
205 {
206  QString signkeyid;
207 
208  QPointer<KgpgSelectSecretKey> opts = new KgpgSelectSecretKey(this, m_model);
209  if (opts->exec() == QDialog::Accepted)
210  signkeyid = opts->getKeyID();
211  else
212  {
213  delete opts;
214  return;
215  }
216 
217  delete opts;
218 
219  KGpgSignText *signt = new KGpgSignText(this, signkeyid, message);
220  connect(signt, SIGNAL(done(int)), SLOT(slotSignUpdate(int)));
221  signt->start();
222 }
223 
224 void KgpgTextEdit::slotVerify(const QString &message)
225 {
226  const QString startmsg(SIGNEDMESSAGE_BEGIN);
227  const QString endmsg(SIGNEDMESSAGE_END);
228 
229  int posstart = message.indexOf(startmsg);
230  if (posstart == -1)
231  return;
232 
233  int posend = message.indexOf(endmsg, posstart);
234  if (posend == -1)
235  return;
236  posend += endmsg.length();
237 
238  KGpgVerify *verify = new KGpgVerify(this, message.mid(posstart, posend - posstart));
239  connect(verify, SIGNAL(done(int)), SLOT(slotVerifyDone(int)));
240  verify->start();
241 }
242 
243 void KgpgTextEdit::slotDecryptDone(int result)
244 {
245  KGpgDecrypt *decr = qobject_cast<KGpgDecrypt *>(sender());
246  Q_ASSERT(decr != NULL);
247 
248  if (!m_tempfile.isEmpty()) {
249  KIO::NetAccess::removeTempFile(m_tempfile);
250  m_tempfile.clear();
251  }
252 
253  if (result == KGpgTransaction::TS_OK) {
254  // FIXME choose codec
255  setPlainText(decr->decryptedText().join(QLatin1String("\n")) + QLatin1Char('\n'));
256  } else if (result != KGpgTransaction::TS_USER_ABORTED) {
257  KMessageBox::detailedSorry(this, i18n("Decryption failed."), decr->getMessages().join( QLatin1String( "\n" )));
258  }
259 
260  decr->deleteLater();
261 }
262 
263 void KgpgTextEdit::slotEncodeUpdate(int result)
264 {
265  KGpgEncrypt *enc = qobject_cast<KGpgEncrypt *>(sender());
266  Q_ASSERT(enc != NULL);
267 
268  if (result == KGpgTransaction::TS_OK) {
269  const QString lf = QLatin1String("\n");
270  setPlainText(enc->encryptedText().join(lf) + lf);
271  } else {
272  KMessageBox::sorry(this, i18n("The encryption failed with error code %1", result),
273  i18n("Encryption failed."));
274  }
275 
276  sender()->deleteLater();
277 }
278 
279 void KgpgTextEdit::slotSignUpdate(int result)
280 {
281  const KGpgSignText * const signt = qobject_cast<KGpgSignText *>(sender());
282  sender()->deleteLater();
283  Q_ASSERT(signt != NULL);
284 
285  if (result != KGpgTransaction::TS_OK) {
286  KMessageBox::sorry(this, i18n("Signing not possible: bad passphrase or missing key"));
287  return;
288  }
289 
290  const QString content = signt->signedText().join(QLatin1String("\n")) + QLatin1String("\n");
291 
292  setPlainText(content);
293  emit resetEncoding(false);
294 }
295 
296 void KgpgTextEdit::slotVerifyDone(int result)
297 {
298  const KGpgVerify * const verify = qobject_cast<KGpgVerify *>(sender());
299  sender()->deleteLater();
300  Q_ASSERT(verify != NULL);
301 
302  emit verifyFinished();
303 
304  if (result == KGpgVerify::TS_MISSING_KEY) {
305  verifyKeyNeeded(verify->missingId());
306  return;
307  }
308 
309  const QStringList messages = verify->getMessages();
310 
311  if (messages.isEmpty())
312  return;
313 
314  QStringList msglist;
315  foreach (QString rawmsg, messages) // krazy:exclude=foreach
316  msglist << rawmsg.replace(QLatin1Char('<'), QLatin1String("&lt;"));
317 
318  (void) new KgpgDetailedInfo(this, KGpgVerify::getReport(messages, m_model),
319  msglist.join(QLatin1String("<br/>")),
320  QStringList(), i18nc("Caption of message box", "Verification Finished"));
321 }
322 
323 void KgpgTextEdit::verifyKeyNeeded(const QString &id)
324 {
325  KGuiItem importitem = KStandardGuiItem::yes();
326  importitem.setText(i18n("&Import"));
327  importitem.setToolTip(i18n("Import key in your list"));
328 
329  KGuiItem noimportitem = KStandardGuiItem::no();
330  noimportitem.setText(i18n("Do &Not Import"));
331  noimportitem.setToolTip(i18n("Will not import this key in your list"));
332 
333  if (KMessageBox::questionYesNo(this, i18n("<qt><b>Missing signature:</b><br />Key id: %1<br /><br />Do you want to import this key from a keyserver?</qt>", id), i18n("Missing Key"), importitem, noimportitem) == KMessageBox::Yes)
334  {
335  KeyServer *kser = new KeyServer(0, m_model, true);
336  kser->slotSetText(id);
337  kser->slotImport();
338  }
339 }
340 
341 void KgpgTextEdit::slotSignVerify()
342 {
343  signVerifyText(toPlainText());
344 }
345 
346 void KgpgTextEdit::signVerifyText(const QString &message)
347 {
348  if (message.contains(SIGNEDMESSAGE_BEGIN))
349  slotVerify(message);
350  else
351  slotSign(message);
352 }
353 
354 void KgpgTextEdit::slotHighlightText(const QString &, const int matchingindex, const int matchedlength)
355 {
356  highlightWord(matchedlength, matchingindex);
357 }
358 
359 #include "kgpgtextedit.moc"
KGpgItemModel
Definition: kgpgitemmodel.h:44
KGpgDecrypt
decrypt the given text or files
Definition: kgpgdecrypt.h:29
KGpgSettings::pgpCompatibility
static bool pgpCompatibility()
Get Enable PGP 6 compatibility.
Definition: kgpgsettings.h:250
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
QWidget
KgpgTextEdit::slotSign
void slotSign(const QString &message)
Definition: kgpgtextedit.cpp:204
kgpgimport.h
KGpgTransaction::TS_OK
everything went fine
Definition: kgpgtransaction.h:60
kgpgsigntext.h
QDropEvent::mimeData
const QMimeData * mimeData() const
KeyServer
Definition: keyservers.h:51
KgpgSelectPublicKeyDlg
shows a dialog to select a public key for encryption
Definition: selectpublickeydialog.h:39
KgpgTextEdit::dragEnterEvent
void dragEnterEvent(QDragEnterEvent *e)
Definition: kgpgtextedit.cpp:59
KGpgEncrypt::AsciiArmored
output the data as printable ASCII as opposed to binary data
Definition: kgpgencrypt.h:38
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
KGpgVerify
verify the signature of the given text or files
Definition: kgpgverify.h:31
KGpgImport
import one or more keys into the keyring
Definition: kgpgimport.h:31
QPointer
selectsecretkey.h
QStringList::join
QString join(const QString &separator) const
QMimeData::hasText
bool hasText() const
QDropEvent::acceptProposedAction
void acceptProposedAction()
KgpgTextEdit::verifyFinished
void verifyFinished()
KGpgTransaction::start
void start()
Start the operation.
Definition: kgpgtransaction.cpp:390
KgpgTextEdit::signVerifyText
void signVerifyText(const QString &message)
Definition: kgpgtextedit.cpp:346
selectpublickeydialog.h
QFile
QTextStream
KgpgTextEdit::openDroppedFile
void openDroppedFile(const KUrl &url, const bool probe)
Definition: kgpgtextedit.cpp:82
QString::clear
void clear()
KGpgDecrypt::isEncryptedText
static bool isEncryptedText(const QString &text, int *startPos=NULL, int *endPos=NULL)
check if the given text contains an encoded message
Definition: kgpgdecrypt.cpp:93
SIGNEDMESSAGE_BEGIN
#define SIGNEDMESSAGE_BEGIN
Definition: kgpgtextedit.cpp:39
KgpgTextEdit::~KgpgTextEdit
~KgpgTextEdit()
Definition: kgpgtextedit.cpp:55
KGpgVerify::TS_MISSING_KEY
signing key not in keyring
Definition: kgpgverify.h:38
kgpgeditor.h
QMimeData::text
QString text() const
KgpgDetailedInfo
Definition: detailedconsole.h:23
QDropEvent
QList::isEmpty
bool isEmpty() const
KgpgTextEdit::slotDecode
void slotDecode()
Definition: kgpgtextedit.cpp:192
kgpgencrypt.h
QString::isEmpty
bool isEmpty() const
KeyServer::slotSetText
void slotSetText(const QString &text)
Definition: keyservers.cpp:277
KeysManager
Definition: keysmanager.h:66
QObject::deleteLater
void deleteLater()
kgpgdecrypt.h
QString
KgpgTextEdit::KgpgTextEdit
KgpgTextEdit(QWidget *parent, KGpgItemModel *model, KeysManager *manager)
Definition: kgpgtextedit.cpp:42
KGpgSignText
sign the given text or files
Definition: kgpgsigntext.h:29
QFile::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
KGpgEncrypt::HideKeyId
remove anything that shows which key ids this data is encrypted to, ignored for symmetric encryption ...
Definition: kgpgencrypt.h:40
QStringList
KGpgSignText::signedText
QStringList signedText() const
get signing result
Definition: kgpgsigntext.cpp:74
kgpgtextedit.h
KgpgTextEdit::slotHighlightText
void slotHighlightText(const QString &, const int matchingindex, const int matchedlength)
Definition: kgpgtextedit.cpp:354
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
KGpgSettings::allowCustomEncryptionOptions
static bool allowCustomEncryptionOptions()
Get Allow custom encryption options.
Definition: kgpgsettings.h:117
QLatin1Char
KeyServer::slotImport
void slotImport()
Definition: keyservers.cpp:97
KGpgEncrypt::encryptedText
QStringList encryptedText() const
get decryption result
Definition: kgpgencrypt.cpp:81
QFile::close
virtual void close()
KGpgTransaction::TS_USER_ABORTED
the user aborted the transaction
Definition: kgpgtransaction.h:63
KGpgVerify::getReport
static QString getReport(const QStringList &log, const KGpgItemModel *model=NULL)
get verification report
Definition: kgpgverify.cpp:103
kgpgsettings.h
QString::replace
QString & replace(int position, int n, QChar after)
keyservers.h
QString::mid
QString mid(int position, int n) const
QDragEnterEvent
QLatin1String
keysmanager.h
KgpgTextEdit::dropEvent
void dropEvent(QDropEvent *e)
Definition: kgpgtextedit.cpp:66
KgpgTextEdit::slotSignVerify
void slotSignVerify()
Definition: kgpgtextedit.cpp:341
QString::length
int length() const
SIGNEDMESSAGE_END
#define SIGNEDMESSAGE_END
Definition: kgpgtextedit.cpp:40
kgpgverify.h
KGpgVerify::missingId
QString missingId() const
get the missing key id
Definition: kgpgverify.cpp:196
detailedconsole.h
KGpgImport::isKey
static int isKey(const QString &text, const bool incomplete=false)
check if the given text contains a private or public key
Definition: kgpgimport.cpp:276
KgpgTextEdit::slotVerifyDone
void slotVerifyDone(int result)
Definition: kgpgtextedit.cpp:296
KgpgTextEdit::resetEncoding
void resetEncoding(bool)
KgpgTextEdit::slotDroppedFile
void slotDroppedFile(const KUrl &url)
Definition: kgpgtextedit.cpp:77
QTextStream::readAll
QString readAll()
KgpgTextEdit::slotVerify
void slotVerify(const QString &message)
Definition: kgpgtextedit.cpp:224
KeysManager::goDefaultShortcut
KShortcut goDefaultShortcut() const
returns the shortcut to go to the default key in a key selection
Definition: keysmanager.cpp:2724
KGpgEncrypt
encrypt the given text or files
Definition: kgpgencrypt.h:30
KGpgDecrypt::decryptedText
QStringList decryptedText() const
get decryption result
Definition: kgpgdecrypt.cpp:63
KgpgTextEdit::slotEncode
void slotEncode()
Definition: kgpgtextedit.cpp:156
KGpgEncrypt::DefaultEncryption
use whatever GnuPGs defaults are
Definition: kgpgencrypt.h:37
KGpgTextOrFileTransaction::getMessages
const QStringList & getMessages() const
get gpg info message
Definition: kgpgtextorfiletransaction.cpp:159
KTextEdit
KgpgSelectSecretKey
Definition: selectsecretkey.h:23
KGpgEncrypt::AllowUntrustedEncryption
allow encryption with untrusted keys, ignored for symmetric encryption
Definition: kgpgencrypt.h:39
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:42:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kgpg

Skip menu "kgpg"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • ktimer
  • kwallet
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal