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

messageviewer

  • sources
  • kde-4.14
  • kdepim
  • messageviewer
  • job
attachmentencryptwithchiasmusjob.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2014-2015 Montel Laurent <montel@kde.org>
3 
4  This program is free software; you can redistribute it and/or modify it
5  under the terms of the GNU General Public License, version 2, as
6  published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful, but
9  WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include "attachmentencryptwithchiasmusjob.h"
19 #include "viewer/chiasmuskeyselector.h"
20 #include "utils/util.h"
21 #include "settings/globalsettings.h"
22 #include "utils/autoqpointer.h"
23 #include <gpgme++/error.h>
24 
25 #include <kio/jobuidelegate.h>
26 #include <KIO/Job>
27 #include <KMessageBox>
28 #include <KLocalizedString>
29 #include <KFileDialog>
30 
31 #include <kleo/cryptobackendfactory.h>
32 #include <kleo/cryptobackend.h>
33 #include <kleo/specialjob.h>
34 
35 
36 
37 using namespace MessageViewer;
38 
39 static const QString chomp( const QString & base, const QString & suffix, bool cs )
40 {
41  return base.endsWith( suffix, cs ? (Qt::CaseSensitive) : (Qt::CaseInsensitive) ) ? base.left( base.length() - suffix.length() ) : base ;
42 }
43 
44 
45 AttachmentEncryptWithChiasmusJob::AttachmentEncryptWithChiasmusJob(QObject *parent)
46  : QObject(parent),
47  mJob(0),
48  mMainWindow(0),
49  mContent(0)
50 {
51 
52 }
53 
54 AttachmentEncryptWithChiasmusJob::~AttachmentEncryptWithChiasmusJob()
55 {
56 
57 }
58 
59 void AttachmentEncryptWithChiasmusJob::setContent(KMime::Content *content)
60 {
61  mContent = content;
62 }
63 
64 void AttachmentEncryptWithChiasmusJob::setCurrentFileName(const QString &currentFileName)
65 {
66  mCurrentFileName = currentFileName;
67 }
68 
69 void AttachmentEncryptWithChiasmusJob::setMainWindow(QWidget *mainWindow)
70 {
71  mMainWindow = mainWindow;
72 }
73 
74 void AttachmentEncryptWithChiasmusJob::start()
75 {
76  Q_UNUSED( mContent );
77 
78  // FIXME: better detection of mimetype??
79  if ( !mCurrentFileName.endsWith( QLatin1String(".xia"), Qt::CaseInsensitive ) ) {
80  deleteLater();
81  return;
82  }
83 
84  const Kleo::CryptoBackend::Protocol * chiasmus =
85  Kleo::CryptoBackendFactory::instance()->protocol( "Chiasmus" );
86  Q_ASSERT( chiasmus );
87  if ( !chiasmus ) {
88  deleteLater();
89  return;
90  }
91 
92  const std::auto_ptr<Kleo::SpecialJob> listjob( chiasmus->specialJob( "x-obtain-keys", QMap<QString,QVariant>() ) );
93  if ( !listjob.get() ) {
94  const QString msg = i18n( "Chiasmus backend does not offer the "
95  "\"x-obtain-keys\" function. Please report this bug." );
96  KMessageBox::error( mMainWindow, msg, i18n( "Chiasmus Backend Error" ) );
97  deleteLater();
98  return;
99  }
100 
101  if ( listjob->exec() ) {
102  listjob->showErrorDialog( mMainWindow, i18n( "Chiasmus Backend Error" ) );
103  deleteLater();
104  return;
105  }
106 
107  const QVariant result = listjob->property( "result" );
108  if ( result.type() != QVariant::StringList ) {
109  const QString msg = i18n( "Unexpected return value from Chiasmus backend: "
110  "The \"x-obtain-keys\" function did not return a "
111  "string list. Please report this bug." );
112  KMessageBox::error( mMainWindow, msg, i18n( "Chiasmus Backend Error" ) );
113  deleteLater();
114  return;
115  }
116 
117  const QStringList keys = result.toStringList();
118  if ( keys.empty() ) {
119  const QString msg = i18n( "No keys have been found. Please check that a "
120  "valid key path has been set in the Chiasmus "
121  "configuration." );
122  KMessageBox::error( mMainWindow, msg, i18n( "Chiasmus Backend Error" ) );
123  deleteLater();
124  return;
125  }
126  AutoQPointer<ChiasmusKeySelector> selectorDlg( new ChiasmusKeySelector( mMainWindow,
127  i18n( "Chiasmus Decryption Key Selection" ),
128  keys, GlobalSettings::chiasmusDecryptionKey(),
129  GlobalSettings::chiasmusDecryptionOptions() ) );
130  if ( selectorDlg->exec() != QDialog::Accepted || !selectorDlg ) {
131  deleteLater();
132  return;
133  }
134 
135  GlobalSettings::setChiasmusDecryptionOptions( selectorDlg->options() );
136  GlobalSettings::setChiasmusDecryptionKey( selectorDlg->key() );
137  assert( !GlobalSettings::chiasmusDecryptionKey().isEmpty() );
138  Kleo::SpecialJob * job = chiasmus->specialJob( "x-decrypt", QMap<QString,QVariant>() );
139  if ( !job ) {
140  const QString msg = i18n( "Chiasmus backend does not offer the "
141  "\"x-decrypt\" function. Please report this bug." );
142  KMessageBox::error( mMainWindow, msg, i18n( "Chiasmus Backend Error" ) );
143  deleteLater();
144  return;
145  }
146 
147  //PORT IT
148  const QByteArray input;// = node->msgPart().bodyDecodedBinary();
149 
150  if ( !job->setProperty( "key", GlobalSettings::chiasmusDecryptionKey() ) ||
151  !job->setProperty( "options", GlobalSettings::chiasmusDecryptionOptions() ) ||
152  !job->setProperty( "input", input ) ) {
153  const QString msg = i18n( "The \"x-decrypt\" function does not accept "
154  "the expected parameters. Please report this bug." );
155  KMessageBox::error( mMainWindow, msg, i18n( "Chiasmus Backend Error" ) );
156  deleteLater();
157  return;
158  }
159 
160  if ( job->start() ) {
161  job->showErrorDialog( mMainWindow, i18n( "Chiasmus Decryption Error" ) );
162  deleteLater();
163  return;
164  }
165 
166  mJob = job;
167  connect( job, SIGNAL(result(GpgME::Error,QVariant)),
168  this, SLOT(slotAtmDecryptWithChiasmusResult(GpgME::Error,QVariant)) );
169 }
170 
171 
172 
173 
174 void AttachmentEncryptWithChiasmusJob::slotAtmDecryptWithChiasmusResult( const GpgME::Error & err, const QVariant & result )
175 {
176  if ( !mJob ) {
177  deleteLater();
178  return;
179  }
180  Q_ASSERT( mJob == sender() );
181  if ( mJob != sender() ) {
182  deleteLater();
183  return;
184  }
185  Kleo::Job * job = mJob;
186  mJob = 0;
187  if ( err.isCanceled() ) {
188  deleteLater();
189  return;
190  }
191  if ( err ) {
192  job->showErrorDialog( mMainWindow, i18n( "Chiasmus Decryption Error" ) );
193  deleteLater();
194  return;
195  }
196 
197  if ( result.type() != QVariant::ByteArray ) {
198  const QString msg = i18n( "Unexpected return value from Chiasmus backend: "
199  "The \"x-decrypt\" function did not return a "
200  "byte array. Please report this bug." );
201  KMessageBox::error( mMainWindow, msg, i18n( "Chiasmus Backend Error" ) );
202  deleteLater();
203  return;
204  }
205 
206  const KUrl url = KFileDialog::getSaveUrl( chomp( mCurrentFileName, QLatin1String(".xia"), false ), QString(), mMainWindow );
207  if ( url.isEmpty() ) {
208  deleteLater();
209  return;
210  }
211 
212  bool overwrite = MessageViewer::Util::checkOverwrite( url, mMainWindow );
213  if ( !overwrite ) {
214  deleteLater();
215  return;
216  }
217 
218  KIO::Job * uploadJob = KIO::storedPut( result.toByteArray(), url, -1, KIO::Overwrite );
219  uploadJob->ui()->setWindow( mMainWindow );
220  connect( uploadJob, SIGNAL(result(KJob*)),
221  this, SLOT(slotAtmDecryptWithChiasmusUploadResult(KJob*)) );
222 }
223 
224 void AttachmentEncryptWithChiasmusJob::slotAtmDecryptWithChiasmusUploadResult( KJob * job )
225 {
226  if ( job->error() )
227  static_cast<KIO::Job*>(job)->ui()->showErrorMessage();
228  deleteLater();
229 }
QVariant::toByteArray
QByteArray toByteArray() const
globalsettings.h
QWidget
MessageViewer::AttachmentEncryptWithChiasmusJob::setCurrentFileName
void setCurrentFileName(const QString &currentFileName)
Definition: attachmentencryptwithchiasmusjob.cpp:64
QByteArray
MessageViewer::AttachmentEncryptWithChiasmusJob::start
void start()
Definition: attachmentencryptwithchiasmusjob.cpp:74
QObject::sender
QObject * sender() const
MessageViewer::AttachmentEncryptWithChiasmusJob::setMainWindow
void setMainWindow(QWidget *mainWindow)
Definition: attachmentencryptwithchiasmusjob.cpp:69
QMap
MessageViewer::AttachmentEncryptWithChiasmusJob::setContent
void setContent(KMime::Content *content)
Definition: attachmentencryptwithchiasmusjob.cpp:59
MessageViewer::AttachmentEncryptWithChiasmusJob::AttachmentEncryptWithChiasmusJob
AttachmentEncryptWithChiasmusJob(QObject *parent=0)
Definition: attachmentencryptwithchiasmusjob.cpp:45
chomp
static const QString chomp(const QString &base, const QString &suffix, bool cs)
Definition: attachmentencryptwithchiasmusjob.cpp:39
QList::empty
bool empty() const
MessageViewer::AutoQPointer
A QPointer which when destructed, deletes the object it points to.
Definition: autoqpointer.h:38
MessageViewer::ChiasmusKeySelector::options
QString options() const
Definition: chiasmuskeyselector.cpp:67
MessageViewer::AttachmentEncryptWithChiasmusJob::~AttachmentEncryptWithChiasmusJob
~AttachmentEncryptWithChiasmusJob()
Definition: attachmentencryptwithchiasmusjob.cpp:54
autoqpointer.h
QObject
QString::endsWith
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
chiasmuskeyselector.h
MessageViewer::ChiasmusKeySelector
Definition: chiasmuskeyselector.h:13
QObject::deleteLater
void deleteLater()
QString
util.h
QStringList
attachmentencryptwithchiasmusjob.h
QVariant::toStringList
QStringList toStringList() const
QLatin1String
MessageViewer::Util::checkOverwrite
bool MESSAGEVIEWER_EXPORT checkOverwrite(const KUrl &url, QWidget *w)
Definition: util.cpp:79
QString::length
int length() const
QString::left
QString left(int n) const
MessageViewer::ChiasmusKeySelector::key
QString key() const
Definition: chiasmuskeyselector.cpp:58
QVariant::type
Type type() const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KJob
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:45 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

messageviewer

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

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

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