aboutsummaryrefslogtreecommitdiffstats
path: root/attachments.cpp
diff options
context:
space:
mode:
authorubbo <ubbo@34ebc366-c3a9-4b3c-9f84-69acf7962910>2008-08-16 15:04:44 +0000
committerubbo <ubbo@34ebc366-c3a9-4b3c-9f84-69acf7962910>2008-08-16 15:04:44 +0000
commit6d3f57c0ffe2c8956c64dc7fed52733cfd1ecced (patch)
tree5b27db8335b2a314229b6dac16f041288f4cd509 /attachments.cpp
parentreorganized svn to support tags and branches (diff)
downloadgpg4usb-6d3f57c0ffe2c8956c64dc7fed52733cfd1ecced.tar.gz
gpg4usb-6d3f57c0ffe2c8956c64dc7fed52733cfd1ecced.zip
start of binary-file handling (decrypt/encrypt), kind of works, but still a lot has to be done
git-svn-id: http://cpunk.de/svn/src/gpg4usb/trunk@145 34ebc366-c3a9-4b3c-9f84-69acf7962910
Diffstat (limited to 'attachments.cpp')
-rw-r--r--attachments.cpp171
1 files changed, 171 insertions, 0 deletions
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;
+}