diff options
-rw-r--r-- | TODO | 2 | ||||
-rw-r--r-- | attachments.cpp | 171 | ||||
-rw-r--r-- | attachments.h | 66 | ||||
-rw-r--r-- | context.cpp | 7 | ||||
-rw-r--r-- | gpg4usb.pro | 4 | ||||
-rw-r--r-- | gpgwin.cpp | 18 | ||||
-rw-r--r-- | gpgwin.h | 2 | ||||
-rw-r--r-- | keylist.h | 6 |
8 files changed, 268 insertions, 8 deletions
@@ -24,3 +24,5 @@ TODO: advanced: - threading & thread-safety in context.cpp (in, out, ctx) +--- +Use QStringList instead of QList<String> diff --git a/attachments.cpp b/attachments.cpp new file mode 100644 index 0000000..6224171 --- /dev/null +++ b/attachments.cpp @@ -0,0 +1,171 @@ +/* + * attachments.cpp + * + * Copyright 2008 gpg4usb-team <[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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ +#include <QVBoxLayout> +#include <QDebug> +#include <QFileDialog> +#include <QMessageBox> +#include <iostream> + +#include "attachments.h" + +Attachments::Attachments(QWidget *parent) + : QWidget(parent) +{ + m_attachmentList = new QListWidget(); + QVBoxLayout *layout = new QVBoxLayout; + layout->addWidget(m_attachmentList); + setLayout(layout); + createActions(); + +} + +void Attachments::setIconPath(QString path) +{ + this->iconPath = path; +} + +void Attachments::setContext(GpgME::Context *ctx) +{ + m_ctx = ctx; +} + +void Attachments::setKeyList(KeyList *keylist) +{ + m_keyList = keylist; +} + + +void Attachments::contextMenuEvent(QContextMenuEvent *event) +{ + QMenu menu(this); + menu.addAction(addFileAct); + menu.addAction(encryptAct); + menu.addAction(decryptAct); + menu.exec(event->globalPos()); +} + +void Attachments::createActions() +{ + addFileAct = new QAction(tr("Add File"), this); + addFileAct->setStatusTip(tr("Add a file")); + addFileAct->setIcon(QIcon(iconPath + "fileopen.png")); + connect(addFileAct, SIGNAL(triggered()), this, SLOT(addFile())); + + encryptAct = new QAction(tr("Encrypt"), this); + encryptAct->setStatusTip(tr("Encrypt marked File(s)")); + encryptAct->setIcon(QIcon(iconPath + "encrypted.png")); + connect(encryptAct, SIGNAL(triggered()), this, SLOT(encryptFile())); + + decryptAct = new QAction(tr("Decrypt"), this); + decryptAct->setStatusTip(tr("Decrypt marked File(s)")); + decryptAct->setIcon(QIcon(iconPath + "decrypted.png")); + connect(decryptAct, SIGNAL(triggered()), this, SLOT(decryptFile())); +} + +void Attachments::addFile() +{ + QFileDialog dialog(this); + dialog.setFileMode(QFileDialog::ExistingFiles); + + QStringList fileNames; + if (dialog.exec()) + fileNames = dialog.selectedFiles(); + + //foreach(QString tmp, fileNames) qDebug() << tmp; + m_attachmentList->addItems(fileNames); +} + +void Attachments::encryptFile() +{ + qDebug() << "enc"; + + foreach(QString filename, *(getSelected())) { + + QByteArray *outBuffer = new QByteArray(); + QList<QString> *uidList = m_keyList->getChecked(); + + QFile file; + file.setFileName(filename); + + if (!file.open(QIODevice::ReadOnly)) { + qDebug() << tr("couldn't open file: ") + filename; + } + qDebug() << "filesize: " << file.size(); + QByteArray inBuffer = file.readAll(); + qDebug() << "buffsize: " << inBuffer.size(); + + if (m_ctx->encrypt(uidList, inBuffer, outBuffer)) { + //qDebug() << "inb: " << inBuffer.toHex(); + qDebug() << "outb: " << outBuffer->data(); + } + } +} + +void Attachments::decryptFile() +{ + qDebug() << "dec"; + foreach(QString inFilename, *(getSelected())){ + qDebug() << inFilename; + + QFile infile; + infile.setFileName(inFilename); + if (!infile.open(QIODevice::ReadOnly)) { + qDebug() << tr("couldn't open file: ") + inFilename; + } + + QByteArray inBuffer = infile.readAll(); + QByteArray *outBuffer = new QByteArray(); + m_ctx->decrypt(inBuffer, outBuffer); + + QString outFilename = QFileDialog::getSaveFileName(this); + if (outFilename.isEmpty()) { + qDebug() << "need Filename"; + return; + } + + QFile outfile(outFilename); + if (!outfile.open(QFile::WriteOnly)) { + QMessageBox::warning(this, tr("File"), + tr("Cannot write file %1:\n%2.") + .arg(outFilename) + .arg(outfile.errorString())); + return; + } + + QDataStream out(&outfile); + out << outBuffer; + + //qDebug() << "outb: " << outBuffer->toHex(); + + } + +} + +QStringList *Attachments::getSelected() +{ + QStringList *ret = new QStringList(); + for (int i = 0; i < m_attachmentList->count(); i++) { + if (m_attachmentList->item(i)->isSelected() == 1) { + *ret << m_attachmentList->item(i)->text(); + } + } + return ret; +} diff --git a/attachments.h b/attachments.h new file mode 100644 index 0000000..4813b8f --- /dev/null +++ b/attachments.h @@ -0,0 +1,66 @@ +/* + * attachments.h + * + * Copyright 2008 gpg4usb-team <[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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#ifndef __ATTACHMENTS_H__ +#define __ATTACHMENTS_H__ + +#include <QListWidget> +#include <QWidget> +#include <QAction> +#include <QMenu> +#include <QContextMenuEvent> +#include "context.h" +#include "keylist.h" + +class QMenu; + +class Attachments : public QWidget +{ + Q_OBJECT + +public slots: + void addFile(); + void encryptFile(); + void decryptFile(); + +public: + Attachments(QWidget *parent = 0); + void setIconPath(QString iconPath); + void setContext(GpgME::Context *ctx); + void setKeyList(KeyList *keylist); + +private: + void createActions(); + QStringList *getSelected(); + QListWidget *m_attachmentList; + QAction *addFileAct; + QAction *encryptAct; + QAction *decryptAct; + QString iconPath; + GpgME::Context *m_ctx; + KeyList *m_keyList; + +protected: + void contextMenuEvent(QContextMenuEvent *event); + +}; + +#endif // __ATTACHMENTS_H__ diff --git a/context.cpp b/context.cpp index 9db8aad..481ac62 100644 --- a/context.cpp +++ b/context.cpp @@ -76,7 +76,7 @@ Context::Context() /** Setting the output type must be done at the beginning */ - /** think this means ascii-amor --> ? */ + /** think this means ascii-armor --> ? */ gpgme_set_armor(m_ctx, 1); /** passphrase-callback */ gpgme_set_passphrase_cb(m_ctx, passphraseCb, this); @@ -178,9 +178,8 @@ void Context::deleteKeys(QList<QString> *uidList) } } -/** Encrypt String, return String - * should also use QByteArray, so be removed - * in should be std::vector<key> & recipients (703 in context.cpp) +/** Encrypt inBuffer for reciepients-uids, write + * result to outBuffer */ bool Context::encrypt(QList<QString> *uidList, const QByteArray &inBuffer, QByteArray *outBuffer) { diff --git a/gpg4usb.pro b/gpg4usb.pro index 91bb5c2..935a4cd 100644 --- a/gpg4usb.pro +++ b/gpg4usb.pro @@ -11,8 +11,8 @@ DEPENDPATH += . INCLUDEPATH += . # Input -HEADERS += context.h gpgwin.h keylist.h -SOURCES += context.cpp gpgwin.cpp main.cpp keylist.cpp +HEADERS += context.h gpgwin.h keylist.h attachments.h +SOURCES += context.cpp gpgwin.cpp main.cpp keylist.cpp attachments.cpp RC_FILE = gpg4usb.rc # For Static build on Linux: uncomment line below #LIBS += lib/libgpgme.a -static-libgcc -Llib @@ -35,7 +35,6 @@ #include "context.h" #include "gpgwin.h" - GpgWin::GpgWin() { myCtx = new GpgME::Context(); @@ -46,12 +45,21 @@ GpgWin::GpgWin() edit = new QPlainTextEdit(); setCentralWidget(edit); + setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea); + setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea); + /* the list of Keys available*/ m_keyList = new KeyList(); m_keyList->setIconPath(iconPath); m_keyList->setContext(myCtx); + /* List of binary Attachments */ + m_attachments = new Attachments(); + m_attachments->setIconPath(iconPath); + m_attachments->setContext(myCtx); + m_attachments->setKeyList(m_keyList); + createActions(); createMenus(); createToolBars(); @@ -205,9 +213,15 @@ void GpgWin::createStatusBar() void GpgWin::createDockWindows() { QDockWidget *dock = new QDockWidget(tr("Encrypt for:"), this); - dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea); + dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea ); addDockWidget(Qt::RightDockWidgetArea, dock); dock->setWidget(m_keyList); + + dock = new QDockWidget(tr("Attached files:"), this); + dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea ); + addDockWidget(Qt::BottomDockWidgetArea, dock); + dock->setWidget(m_attachments); + } void GpgWin::closeEvent(QCloseEvent *event) @@ -23,6 +23,7 @@ #include <QtGui> #include "context.h" #include "keylist.h" +#include "attachments.h" class QAction; class QMenu; @@ -82,6 +83,7 @@ private: QString curFile; KeyList *m_keyList; + Attachments *m_attachments; GpgME::Context *myCtx; QString iconPath; }; @@ -18,6 +18,10 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. */ + +#ifndef __KEYLIST_H__ +#define __KEYLIST_H__ + #include <QWidget> #include <QListWidget> #include <QPushButton> @@ -53,3 +57,5 @@ private: protected: void contextMenuEvent(QContextMenuEvent *event); }; + +#endif // __KEYLIST_H__ |