aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorubbo <ubbo@34ebc366-c3a9-4b3c-9f84-69acf7962910>2012-08-03 21:13:48 +0000
committerubbo <ubbo@34ebc366-c3a9-4b3c-9f84-69acf7962910>2012-08-03 21:13:48 +0000
commit64e2d69a1f034fedf20905c42ce785ace69b3b5a (patch)
treefddba81170d2808c9e0269e714fa59d9b39d1e10
parentencryption works (diff)
downloadgpg4usb-64e2d69a1f034fedf20905c42ce785ace69b3b5a.tar.gz
gpg4usb-64e2d69a1f034fedf20905c42ce785ace69b3b5a.zip
decrypt works
git-svn-id: http://cpunk.de/svn/src/gpg4usb/branches/0.3.2-mac@934 34ebc366-c3a9-4b3c-9f84-69acf7962910
-rw-r--r--gpg4usb.pro6
-rw-r--r--kgpg/kprocess.cpp2
-rw-r--r--kgpg/transactions/kgpgdecrypt.cpp141
-rw-r--r--kgpg/transactions/kgpgdecrypt.h86
-rw-r--r--mainwindow.cpp58
-rw-r--r--mainwindow.h2
6 files changed, 279 insertions, 16 deletions
diff --git a/gpg4usb.pro b/gpg4usb.pro
index 5d90051..e7a8b6d 100644
--- a/gpg4usb.pro
+++ b/gpg4usb.pro
@@ -65,7 +65,8 @@ HEADERS += attachments.h \
kgpg/core/images.h \
kgpg/transactions/kgpgtransaction.h \
kgpg/transactions/kgpgtextorfiletransaction.h \
- kgpg/transactions/kgpgencrypt.h
+ kgpg/transactions/kgpgencrypt.h \
+ kgpg/transactions/kgpgdecrypt.h
SOURCES += attachments.cpp \
@@ -114,7 +115,8 @@ SOURCES += attachments.cpp \
kgpg/core/images.cpp \
kgpg/transactions/kgpgtransaction.cpp \
kgpg/transactions/kgpgtextorfiletransaction.cpp \
- kgpg/transactions/kgpgencrypt.cpp
+ kgpg/transactions/kgpgencrypt.cpp \
+ kgpg/transactions/kgpgdecrypt.cpp
RC_FILE = gpg4usb.rc
diff --git a/kgpg/kprocess.cpp b/kgpg/kprocess.cpp
index 907c75c..ba71ebd 100644
--- a/kgpg/kprocess.cpp
+++ b/kgpg/kprocess.cpp
@@ -341,7 +341,7 @@ QStringList KProcess::program() const
void KProcess::start()
{
Q_D(KProcess);
- qDebug() << "prog: " << d->prog << " | args: " << d->args;
+ //qDebug() << "prog: " << d->prog << " | args: " << d->args;
QProcess::start(d->prog, d->args, d->openMode);
}
diff --git a/kgpg/transactions/kgpgdecrypt.cpp b/kgpg/transactions/kgpgdecrypt.cpp
new file mode 100644
index 0000000..20f0c50
--- /dev/null
+++ b/kgpg/transactions/kgpgdecrypt.cpp
@@ -0,0 +1,141 @@
+/*
+ * Copyright (C) 2010,2011,2012 Rolf Eike Beer <[email protected]>
+ */
+
+/***************************************************************************
+ * *
+ * This program 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 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+
+#include "kgpgdecrypt.h"
+
+#include "../gpgproc.h"
+//#include "kgpgsettings.h"
+
+//#include <KLocale>
+
+KGpgDecrypt::KGpgDecrypt(QObject *parent, const QString &text)
+ : KGpgTextOrFileTransaction(parent, text),
+ m_fileIndex(-1),
+ m_plainLength(-1)
+{
+}
+
+KGpgDecrypt::KGpgDecrypt(QObject *parent, const QList<QUrl> &files)
+ : KGpgTextOrFileTransaction(parent, files),
+ m_fileIndex(0),
+ m_plainLength(-1)
+{
+}
+
+KGpgDecrypt::KGpgDecrypt(QObject* parent, const QUrl& infile, const QUrl& outfile)
+ : KGpgTextOrFileTransaction(parent, QList<QUrl>() << infile ),
+ m_fileIndex(0),
+ m_plainLength(-1),
+ m_outFilename(outfile.toLocalFile())
+{
+}
+
+KGpgDecrypt::~KGpgDecrypt()
+{
+}
+
+QStringList
+KGpgDecrypt::command() const
+{
+ QStringList ret;
+
+ ret << QLatin1String("--decrypt") << QLatin1String("--command-fd=0");
+
+ if (!m_outFilename.isEmpty())
+ ret << QLatin1String("-o") << m_outFilename;
+
+ //ret << KGpgSettings::customDecrypt().simplified().split(QLatin1Char(' '), QString::SkipEmptyParts);
+
+ return ret;
+}
+
+QStringList
+KGpgDecrypt::decryptedText() const
+{
+ QStringList result;
+ int txtlength = 0;
+
+ foreach (const QString &line, getMessages())
+ if (!line.startsWith(QLatin1String("[GNUPG:] "))) {
+ result.append(line);
+ txtlength += line.length() + 1;
+ }
+
+ if (result.isEmpty())
+ return result;
+
+ QString last = result.last();
+ // this may happen when the original text did not end with a newline
+ if (last.endsWith(QLatin1String("[GNUPG:] DECRYPTION_OKAY"))) {
+ // if GnuPG doesn't tell us the length assume that this happend
+ // if it told us the length then check if it _really_ happend
+ if (((m_plainLength != -1) && (txtlength != m_plainLength)) ||
+ (m_plainLength == -1)) {
+ last.chop(24);
+ result[result.count() - 1] = last;
+ }
+ }
+
+ return result;
+}
+
+bool
+KGpgDecrypt::isEncryptedText(const QString &text, int *startPos, int *endPos)
+{
+ int posStart = text.indexOf(QLatin1String("-----BEGIN PGP MESSAGE-----"));
+ if (posStart == -1)
+ return false;
+
+ int posEnd = text.indexOf(QLatin1String("-----END PGP MESSAGE-----"), posStart);
+ if (posEnd == -1)
+ return false;
+
+ if (startPos != NULL)
+ *startPos = posStart;
+ if (endPos != NULL)
+ *endPos = posEnd;
+
+ return true;
+}
+
+bool
+KGpgDecrypt::nextLine(const QString& line)
+{
+ const QList<QUrl> &inputFiles = getInputFiles();
+
+ if (!inputFiles.isEmpty()) {
+ if (line == QLatin1String("[GNUPG:] BEGIN_DECRYPTION")) {
+ emit statusMessage(tr("Status message 'Decrypting <filename>' (operation starts)", "Decrypting %1").arg(inputFiles.at(m_fileIndex).toLocalFile()));
+ emit infoProgress(2 * m_fileIndex + 1, inputFiles.count() * 2);
+ } else if (line == QLatin1String("[GNUPG:] END_DECRYPTION")) {
+ emit statusMessage(tr("Status message 'Decrypted <filename>' (operation was completed)", "Decrypted %1").arg(inputFiles.at(m_fileIndex).toLocalFile()));
+ m_fileIndex++;
+ emit infoProgress(2 * m_fileIndex, inputFiles.count() * 2);
+ }
+ } else {
+ if (line.startsWith(QLatin1String("[GNUPG:] PLAINTEXT_LENGTH "))) {
+ bool ok;
+ m_plainLength = line.mid(26).toInt(&ok);
+ if (!ok)
+ m_plainLength = -1;
+ } else if (line == QLatin1String("[GNUPG:] BEGIN_DECRYPTION")) {
+ // close the command channel (if any) to signal GnuPG that it
+ // can start sending the output.
+ getProcess()->closeWriteChannel();
+ }
+ }
+
+ return KGpgTextOrFileTransaction::nextLine(line);
+}
+
+//#include "kgpgdecrypt.moc"
diff --git a/kgpg/transactions/kgpgdecrypt.h b/kgpg/transactions/kgpgdecrypt.h
new file mode 100644
index 0000000..aa28c19
--- /dev/null
+++ b/kgpg/transactions/kgpgdecrypt.h
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2010,2011 Rolf Eike Beer <[email protected]>
+ */
+
+/***************************************************************************
+ * *
+ * This program 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 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+
+#ifndef KGPGDECRYPT_H
+#define KGPGDECRYPT_H
+
+#include <QObject>
+
+#include <QUrl>
+
+#include "kgpgtextorfiletransaction.h"
+
+class QProcess;
+class QStringList;
+
+/**
+ * @brief decrypt the given text or files
+ */
+class KGpgDecrypt: public KGpgTextOrFileTransaction {
+ Q_OBJECT
+
+ Q_DISABLE_COPY(KGpgDecrypt)
+ KGpgDecrypt(); // = delete C++0x
+public:
+ /**
+ * @brief decrypt given text
+ * @param parent parent object
+ * @param text text to decrypt
+ */
+ explicit KGpgDecrypt(QObject *parent, const QString &text = QString());
+
+ /**
+ * @brief decrypt file(s)
+ * @param parent parent object
+ * @param files list of file locations to decrypt
+ */
+ KGpgDecrypt(QObject *parent, const QList<QUrl> &files);
+
+ /**
+ * @brief decrypt file to given output filename
+ * @param parent parent object
+ * @param infile name of file to decrypt
+ * @param outfile name of file to write output to (will be overwritten)
+ */
+ KGpgDecrypt(QObject *parent, const QUrl &infile, const QUrl &outfile);
+
+ /**
+ * @brief destructor
+ */
+ virtual ~KGpgDecrypt();
+
+ /**
+ * @brief get decryption result
+ * @return decrypted text
+ */
+ QStringList decryptedText() const;
+
+ /**
+ * @brief check if the given text contains an encoded message
+ * @param text text to check
+ * @param startPos if not NULL start offset of encoded text will be returned here
+ * @param endPos if not NULL end offset of encoded text will be returned here
+ */
+ static bool isEncryptedText(const QString &text, int *startPos = NULL, int *endPos = NULL);
+
+protected:
+ virtual QStringList command() const;
+ virtual bool nextLine(const QString &line);
+
+private:
+ int m_fileIndex;
+ int m_plainLength; ///< length of decrypted plain text if given by GnuPG
+ const QString m_outFilename; ///< name of file to write output to
+};
+
+#endif // KGPGDECRYPT_H
diff --git a/mainwindow.cpp b/mainwindow.cpp
index 8c7e5cf..b7d8f45 100644
--- a/mainwindow.cpp
+++ b/mainwindow.cpp
@@ -779,12 +779,6 @@ void MainWindow::encrypt()
return;
}
- //QByteArray *tmp = new QByteArray();
- /*if (mCtx->encrypt(uidList, edit->curTextPage()->toPlainText().toUtf8(), tmp)) {
- QString *tmp2 = new QString(*tmp);
- edit->fillTextEditWithText(*tmp2);
- }*/
-
QStringList options;
KGpgEncrypt::EncryptOptions opts = KGpgEncrypt::DefaultEncryption;
@@ -835,21 +829,36 @@ void MainWindow::decrypt()
return;
}
- QByteArray *decrypted = new QByteArray();
- QByteArray text = edit->curTextPage()->toPlainText().toAscii(); // TODO: toUtf8() here?
- mCtx->preventNoDataErr(&text);
+ //QByteArray *decrypted = new QByteArray();
+ //QByteArray text = edit->curTextPage()->toPlainText().toAscii(); // TODO: toUtf8() here?
+
+
+ const QString fullcontent = edit->curTextPage()->toPlainText();
+ // TODO: do we still need this with kgpg?
+ //mCtx->preventNoDataErr(fullcontent.toAscii());
+
+ // TODO: whats the use of this?
+ int m_posstart = -1;
+ int m_posend = -1;
+
+ if (!KGpgDecrypt::isEncryptedText(fullcontent, &m_posstart, &m_posend))
+ return;
+
+ KGpgDecrypt *decr = new KGpgDecrypt(this, fullcontent.mid(m_posstart, m_posend - m_posstart));
+ connect(decr, SIGNAL(done(int)), SLOT(slotDecryptDone(int)));
+ decr->start();
// try decrypt, if fail do nothing, especially don't replace text
- if(!mCtx->decrypt(text, decrypted)) {
+ /* if(!mCtx->decrypt(text, decrypted)) {
return;
- }
+ }*/
/*
* 1) is it mime (content-type:)
* 2) parse header
* 2) choose action depending on content-type
*/
- if(Mime::isMime(decrypted)) {
+/* if(Mime::isMime(decrypted)) {
Header header = Mime::getHeader(decrypted);
// is it multipart, is multipart-parsing enabled
if(header.getValue("Content-Type") == "multipart/mixed"
@@ -865,9 +874,32 @@ void MainWindow::decrypt()
}
}
}
- edit->fillTextEditWithText(QString::fromUtf8(*decrypted));
+ edit->fillTextEditWithText(QString::fromUtf8(*decrypted));*/
}
+void MainWindow::slotDecryptDone(int result)
+{
+ KGpgDecrypt *decr = qobject_cast<KGpgDecrypt *>(sender());
+ Q_ASSERT(decr != NULL);
+
+ /*if (!m_tempfile.isEmpty()) {
+ KIO::NetAccess::removeTempFile(m_tempfile);
+ m_tempfile.clear();
+ }*/
+
+ if (result == KGpgTransaction::TS_OK) {
+ // FIXME choose codec
+ //setPlainText(decr->decryptedText().join(QLatin1String("\n")) + QLatin1Char('\n'));
+ edit->fillTextEditWithText(decr->decryptedText().join(QLatin1String("\n")) + QLatin1Char('\n'));
+ } else if (result != KGpgTransaction::TS_USER_ABORTED) {
+ //KMessageBox::detailedSorry(this, i18n("Decryption failed."), decr->getMessages().join( QLatin1String( "\n" )));
+ qDebug() << "Decryption failed." << decr->getMessages().join( QLatin1String( "\n" ));
+ }
+
+ decr->deleteLater();
+}
+
+
void MainWindow::verify()
{
if (edit->tabCount()==0 || edit->curPage() == 0) {
diff --git a/mainwindow.h b/mainwindow.h
index bbf940b..3f0ea58 100644
--- a/mainwindow.h
+++ b/mainwindow.h
@@ -32,6 +32,7 @@
#include "wizard.h"
#include "kgpg/core/kgpgkey.h"
#include "kgpg/transactions/kgpgencrypt.h"
+#include "kgpg/transactions/kgpgdecrypt.h"
QT_BEGIN_NAMESPACE
class QMainWindow;
@@ -94,6 +95,7 @@ private slots:
* @details Show a passphrase dialog and decrypt the text of currently active tab.
*/
void decrypt();
+ void slotDecryptDone(int result);
/**
* @details Sign the text of currently active tab with the checked private keys