diff options
-rw-r--r-- | gpg4usb.pro | 6 | ||||
-rw-r--r-- | kgpg/transactions/kgpgexport.cpp | 197 | ||||
-rw-r--r-- | kgpg/transactions/kgpgexport.h | 147 | ||||
-rw-r--r-- | mainwindow.cpp | 21 | ||||
-rw-r--r-- | mainwindow.h | 2 |
5 files changed, 368 insertions, 5 deletions
diff --git a/gpg4usb.pro b/gpg4usb.pro index e7a8b6d..0a831af 100644 --- a/gpg4usb.pro +++ b/gpg4usb.pro @@ -66,7 +66,8 @@ HEADERS += attachments.h \ kgpg/transactions/kgpgtransaction.h \ kgpg/transactions/kgpgtextorfiletransaction.h \ kgpg/transactions/kgpgencrypt.h \ - kgpg/transactions/kgpgdecrypt.h + kgpg/transactions/kgpgdecrypt.h \ + kgpg/transactions/kgpgexport.h SOURCES += attachments.cpp \ @@ -116,7 +117,8 @@ SOURCES += attachments.cpp \ kgpg/transactions/kgpgtransaction.cpp \ kgpg/transactions/kgpgtextorfiletransaction.cpp \ kgpg/transactions/kgpgencrypt.cpp \ - kgpg/transactions/kgpgdecrypt.cpp + kgpg/transactions/kgpgdecrypt.cpp \ + kgpg/transactions/kgpgexport.cpp RC_FILE = gpg4usb.rc diff --git a/kgpg/transactions/kgpgexport.cpp b/kgpg/transactions/kgpgexport.cpp new file mode 100644 index 0000000..79f6afe --- /dev/null +++ b/kgpg/transactions/kgpgexport.cpp @@ -0,0 +1,197 @@ +/* + * Copyright (C) 2009,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 "kgpgexport.h" + +#include "../gpgproc.h" + +#include <QFile> +#include <QProcess> + +KGpgExport::KGpgExport(QObject *parent, const QStringList &ids, QProcess *outp, const QStringList &options, const bool secret) + : KGpgTransaction(parent), + m_keyids(ids), + m_outp(outp), + m_outputmode(ModeProcess) +{ + procSetup(options, secret); +} + +KGpgExport::KGpgExport(QObject *parent, const QStringList &ids, const QString &file, const QStringList &options, const bool secret) + : KGpgTransaction(parent), + m_keyids(ids), + m_outp(NULL), + m_outf(file), + m_outputmode(ModeFile) +{ + procSetup(options, secret); +} + +KGpgExport::KGpgExport(QObject *parent, const QStringList &ids, const QStringList &options, const bool secret) + : KGpgTransaction(parent), + m_keyids(ids), + m_outp(NULL), + m_outputmode(ModeStdout) +{ + procSetup(options, secret); +} + +KGpgExport::KGpgExport(QObject *parent, const QStringList &ids, KGpgTransaction *outt, const QStringList &options, const bool secret) + : KGpgTransaction(parent), + m_keyids(ids), + m_outp(NULL), + m_outputmode(ModeTransaction) +{ + procSetup(options, secret); + outt->setInputTransaction(this); +} + +KGpgExport::~KGpgExport() +{ +} + +void +KGpgExport::setKeyId(const QString &id) +{ + m_keyids.clear(); + m_keyids.append(id); +} + +void +KGpgExport::setKeyIds(const QStringList &ids) +{ + m_keyids = ids; +} + +const QStringList & +KGpgExport::getKeyIds() const +{ + return m_keyids; +} + +void +KGpgExport::setOutputProcess(QProcess *outp) +{ + m_outf.clear(); + m_outp = outp; + m_outputmode = ModeProcess; +} + +void +KGpgExport::setOutputFile(const QString &filename) +{ + m_outp = NULL; + m_outf = filename; + if (filename.isEmpty()) + m_outputmode = ModeStdout; + else + m_outputmode = ModeFile; +} + +void +KGpgExport::setOutputTransaction(KGpgTransaction *outt) +{ + m_outp = NULL; + m_outf.clear(); + m_outputmode = ModeTransaction; + outt->setInputTransaction(this); +} + +const QString & +KGpgExport::getOutputFile() const +{ + return m_outf; +} + +const QByteArray & +KGpgExport::getOutputData() const +{ + return m_data; +} + +bool +KGpgExport::preStart() +{ + setSuccess(TS_OK); + + switch (m_outputmode) { + case ModeFile: + { + Q_ASSERT(!m_outf.isEmpty()); + Q_ASSERT(m_outp == NULL); + + addArgument(QLatin1String( "--output" )); + addArgument(m_outf); + + QFile ofile(m_outf); + if (ofile.exists()) + ofile.remove(); + + break; + } + case ModeProcess: + Q_ASSERT(m_outf.isEmpty()); + Q_ASSERT(m_outp != NULL); + + getProcess()->setStandardOutputProcess(m_outp); + + break; + case ModeStdout: + Q_ASSERT(m_outf.isEmpty()); + Q_ASSERT(m_outp == NULL); + break; + case ModeTransaction: + Q_ASSERT(m_outf.isEmpty()); + Q_ASSERT(m_outp == NULL); + break; + default: + Q_ASSERT(0); + } + + addArguments(m_keyids); + + m_data.clear(); + + return true; +} + +bool +KGpgExport::nextLine(const QString &line) +{ + // key exporting does not send any messages + + m_data.append(line.toAscii() + '\n'); + + if (m_outputmode != 2) + setSuccess(TS_MSG_SEQUENCE); + + return false; +} + +void +KGpgExport::procSetup(const QStringList &options, const bool secret) +{ + getProcess()->resetProcess(); + + if (secret) + addArgument(QLatin1String( "--export-secret-key" )); + else + addArgument(QLatin1String( "--export" )); + + if ((m_outputmode == 2) && !options.contains(QLatin1String( "--armor" ))) + addArgument(QLatin1String( "--armor" )); + + addArguments(options); +} + +//#include "kgpgexport.moc" diff --git a/kgpg/transactions/kgpgexport.h b/kgpg/transactions/kgpgexport.h new file mode 100644 index 0000000..63aeaac --- /dev/null +++ b/kgpg/transactions/kgpgexport.h @@ -0,0 +1,147 @@ +/* + * Copyright (C) 2009 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 KGPGEXPORT_H +#define KGPGEXPORT_H + +#include <QObject> +#include <QStringList> + +#include <QUrl> + +#include "kgpgtransaction.h" + +class QProcess; + +/** + * @brief export one or more keys from keyring + * + * The exported keys can be written to a file or sent to standard input of another + * QProcess. + */ +class KGpgExport: public KGpgTransaction { + Q_OBJECT + + KGpgExport(); // = delete C++0x + Q_DISABLE_COPY(KGpgExport) +public: + /** + * @brief export keys to QProcess + * @param parent parent object + * @param ids ids to export + * @param outp process to write into + * @param options additional options to pass to GnuPG (e.g. export ascii armored) + * @param secret if secret key exporting is allowed + */ + KGpgExport(QObject *parent, const QStringList &ids, QProcess *outp, const QStringList &options = QStringList(), const bool secret = false); + + /** + * @brief export keys to KGpgTransaction + * @param parent parent object + * @param ids ids to export + * @param outt transaction to write into + * @param options additional options to pass to GnuPG (e.g. export ascii armored) + * @param secret if secret key exporting is allowed + */ + KGpgExport(QObject *parent, const QStringList &ids, KGpgTransaction *outt, const QStringList &options = QStringList(), const bool secret = false); + + /** + * @brief export keys to file + * @param parent parent object + * @param ids ids to export + * @param file filename to write into + * @param options additional options to pass to GnuPG (e.g. export ascii armored) + * @param secret if secret key exporting is allowed + */ + KGpgExport(QObject *parent, const QStringList &ids, const QString &file, const QStringList &options = QStringList(), const bool secret = false); + + /** + * @brief export keys to standard output + * @param parent parent object + * @param ids ids to export + * @param options additional options to pass to GnuPG (e.g. export ascii armored) + * @param secret if secret key exporting is allowed + * + * Only ascii-armored export is supported in standard output mode. If it is not + * already set in the given option it will be added automatically. + */ + KGpgExport(QObject *parent, const QStringList &ids, const QStringList &options = QStringList(), const bool secret = false); + + /** + * @brief destructor + */ + virtual ~KGpgExport(); + + /** + * @brief set key id to export + * @param id key fingerprint + */ + void setKeyId(const QString &id); + /** + * @brief set key ids to export + * @param ids key fingerprints + */ + void setKeyIds(const QStringList &ids); + /** + * @brief return the key ids to export + * @return list of key fingerprints + */ + const QStringList &getKeyIds() const; + /** + * @brief set the process the output is sent to + * @param outp process to send output to + */ + void setOutputProcess(QProcess *outp); + /** + * @brief set the transaction the output is sent to + * @param outd transaction to send output to + */ + void setOutputTransaction(KGpgTransaction *outt); + /** + * @brief set filename to send output to + * @param filename file to send output to + */ + void setOutputFile(const QString &filename); + /** + * @brief return the output filename currently set + * @return filename key will get written to + */ + const QString &getOutputFile() const; + /** + * @brief return the data read from standard output + * @return standard output data + */ + const QByteArray &getOutputData() const; + +protected: + virtual bool preStart(); + virtual bool nextLine(const QString &line); + +private: + QStringList m_keyids; + QProcess *m_outp; + QString m_outf; + QByteArray m_data; + + enum OutputMode { + ModeFile = 0, + ModeProcess = 1, + ModeStdout = 2, + ModeTransaction = 3 + }; + enum OutputMode m_outputmode; + + void procSetup(const QStringList &options, const bool secret); +}; + +#endif // KGPGEXPORT_H diff --git a/mainwindow.cpp b/mainwindow.cpp index b7d8f45..985ee0d 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -929,9 +929,24 @@ void MainWindow::appendSelectedKeys() return; } - QByteArray *keyArray = new QByteArray(); - mCtx->exportKeys(mKeyList->getSelected(), keyArray); - edit->curTextPage()->append(*keyArray); + QStringList expopts; + KGpgExport *exp = new KGpgExport(this, *mKeyList->getSelected(), expopts); + connect(exp, SIGNAL(done(int)), SLOT(slotAppendSelectedKeysReady(int))); + exp->start(); +} + +void MainWindow::slotAppendSelectedKeysReady(int result) { + KGpgExport *exp = qobject_cast<KGpgExport *>(sender()); + Q_ASSERT(exp != NULL); + + if (result == KGpgTransaction::TS_OK) { + edit->curTextPage()->append(QLatin1String( exp->getOutputData() )); + } else { + //KMessageBox::sorry(this, i18n("Your public key could not be exported\nCheck the key.")); + qDebug() << "Your public key could not be exported\nCheck the key."; + } + + exp->deleteLater(); } void MainWindow::copyMailAddressToClipboard() diff --git a/mainwindow.h b/mainwindow.h index 3f0ea58..c9867c2 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -33,6 +33,7 @@ #include "kgpg/core/kgpgkey.h" #include "kgpg/transactions/kgpgencrypt.h" #include "kgpg/transactions/kgpgdecrypt.h" +#include "kgpg/transactions/kgpgexport.h" QT_BEGIN_NAMESPACE class QMainWindow; @@ -123,6 +124,7 @@ private slots: * @details Append the selected keys to currently active textedit. */ void appendSelectedKeys(); + void slotAppendSelectedKeysReady(int result); /** * @details Copy the mailaddress of selected key to clipboard. |