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

kleopatra

  • sources
  • kde-4.14
  • kdepim
  • kleopatra
  • crypto
task.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  crypto/task.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2007 Klarälvdalens Datakonsult AB
6 
7  Kleopatra is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  Kleopatra is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the Qt library by Trolltech AS, Norway (or with modified versions
24  of Qt that use the same license as Qt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  Qt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 #include <config-kleopatra.h>
34 
35 #include "task.h"
36 #include "task_p.h"
37 
38 #include <kleo/exception.h>
39 
40 #include <utils/gnupg-helper.h>
41 #include <utils/auditlog.h>
42 
43 #include <gpgme++/exception.h>
44 
45 #include <gpg-error.h>
46 
47 #include <KIcon>
48 #include <KIconLoader>
49 #include <KLocalizedString>
50 
51 #include <QString>
52 
53 #ifndef Q_MOC_RUN
54 #include <boost/bind.hpp>
55 #endif
56 
57 using namespace Kleo;
58 using namespace Kleo::Crypto;
59 using namespace boost;
60 using namespace GpgME;
61 
62 namespace {
63 
64  class ErrorResult : public Task::Result {
65  public:
66  ErrorResult( int code, const QString & details )
67  : Task::Result(), m_code( code ), m_details( details ) {}
68 
69  /* reimp */ QString overview() const { return makeOverview( m_details ); }
70  /* reimp */ QString details() const { return QString(); }
71  /* reimp */ int errorCode() const { return m_code; }
72  /* reimp */ QString errorString() const { return m_details; }
73  /* reimp */ VisualCode code() const { return NeutralError; }
74  /* reimp */ AuditLog auditLog() const { return AuditLog(); }
75  private:
76  int m_code;
77  QString m_details;
78  };
79 }
80 
81 class Task::Private {
82  friend class ::Kleo::Crypto::Task;
83  Task * const q;
84 public:
85  explicit Private( Task * qq );
86 
87 private:
88  QString m_progressLabel;
89  double m_processedPercent;
90  bool m_asciiArmor;
91  int m_id;
92 };
93 
94 namespace {
95  static int nextTaskId = 0;
96 }
97 
98 Task::Private::Private( Task * qq )
99  : q( qq ), m_progressLabel(), m_processedPercent( 0.0 ), m_asciiArmor( false ), m_id( nextTaskId++ )
100 {
101 
102 }
103 
104 Task::Task( QObject * p )
105  : QObject( p ), d( new Private( this ) )
106 {
107 
108 }
109 
110 Task::~Task() {}
111 
112 void Task::setAsciiArmor( bool armor )
113 {
114  d->m_asciiArmor = armor;
115 }
116 
117 bool Task::asciiArmor() const
118 {
119  return d->m_asciiArmor;
120 }
121 
122 shared_ptr<Task> Task::makeErrorTask( int code, const QString & details, const QString & label ) {
123  const shared_ptr<SimpleTask> t( new SimpleTask( label ) );
124  t->setResult( t->makeErrorResult( code, details ) );
125  return t;
126 }
127 
128 int Task::id() const
129 {
130  return d->m_id;
131 }
132 
133 unsigned long long Task::processedSize() const
134 {
135  return qRound( d->m_processedPercent * totalSize() );
136 }
137 
138 unsigned long long Task::totalSize() const
139 {
140  return inputSize();
141 }
142 
143 QString Task::tag() const {
144  return QString();
145 }
146 
147 QString Task::progressLabel() const
148 {
149  return d->m_progressLabel;
150 }
151 
152 void Task::setProgress( const QString & label, int processed, int total )
153 {
154  const double percent = total > 0 ? static_cast<double>( processed ) / total : 0.0;
155  d->m_processedPercent = percent;
156  d->m_progressLabel = label;
157  emit progress( label, processedSize(), totalSize() );
158 }
159 
160 void Task::start() {
161  try {
162  doStart();
163  } catch ( const Kleo::Exception & e ) {
164  QMetaObject::invokeMethod( this, "emitError", Qt::QueuedConnection, Q_ARG( int, e.error().encodedError() ), Q_ARG( QString, e.message() ) );
165  } catch ( const GpgME::Exception & e ) {
166  QMetaObject::invokeMethod( this, "emitError", Qt::QueuedConnection, Q_ARG( int, e.error().encodedError() ), Q_ARG( QString, QString::fromLocal8Bit( e.what() ) ) );
167  } catch ( const std::exception & e ) {
168  QMetaObject::invokeMethod( this, "emitError", Qt::QueuedConnection, Q_ARG( int, makeGnuPGError( GPG_ERR_UNEXPECTED ) ), Q_ARG( QString, QString::fromLocal8Bit( e.what() ) ) );
169  } catch ( ... ) {
170  QMetaObject::invokeMethod( this, "emitError", Qt::QueuedConnection, Q_ARG( int, makeGnuPGError( GPG_ERR_UNEXPECTED ) ), Q_ARG( QString, i18n( "Unknown exception in Task::start()") ) );
171  }
172  emit started();
173 }
174 
175 void Task::emitError( int errCode, const QString& details ) {
176  emitResult( makeErrorResult( errCode, details ) );
177 }
178 
179 void Task::emitResult( const shared_ptr<const Task::Result> & r )
180 {
181  d->m_processedPercent = 1.0;
182  emit progress( progressLabel(), processedSize(), totalSize() );
183  emit result( r );
184 }
185 
186 shared_ptr<Task::Result> Task::makeErrorResult( int errCode, const QString& details )
187 {
188  return shared_ptr<Task::Result>( new ErrorResult( errCode, details ) );
189 }
190 
191 
192 static QString makeNonce() {
193  // ### make better
194  return QString::number( qrand(), 16 );
195 }
196 
197 class Task::Result::Private {
198 public:
199  Private() {}
200 };
201 
202 Task::Result::Result() : m_nonce( makeNonce() ), d( new Private() ) {}
203 Task::Result::~Result() {}
204 
205 QString Task::Result::formatKeyLink( const char * fpr, const QString & content ) const {
206  return QLatin1String("<a href=\"key:") + m_nonce + QLatin1Char(':') + QLatin1String(fpr) + QLatin1String("\">") + content + QLatin1String("</a>");
207 }
208 
209 bool Task::Result::hasError() const
210 {
211  return errorCode() != 0;
212 }
213 
214 static QString image( const char* img ) {
215  // ### escape?
216  return KIconLoader::global()->iconPath( QLatin1String(img), KIconLoader::Small );
217 }
218 
219 QString Task::Result::makeOverview( const QString& msg )
220 {
221  return QLatin1String("<b>") + msg + QLatin1String("</b>");
222 }
223 
224 QString Task::Result::iconPath( VisualCode code )
225 {
226  switch ( code ) {
227  case Danger:
228  return image( "dialog-error" );
229  case AllGood:
230  return image( "dialog-ok" );
231  case Warning:
232  return image( "dialog-warning" );
233  case NeutralError:
234  case NeutralSuccess:
235  default:
236  return QString();
237 
238  }
239 }
240 
241 QString Task::Result::icon() const { return iconPath( code() ); }
242 
243 
244 #include "moc_task_p.cpp"
task_p.h
Kleo::Crypto::SimpleTask
Definition: task_p.h:44
Kleo::Crypto::Task::setAsciiArmor
void setAsciiArmor(bool armor)
Definition: task.cpp:112
Kleo::Crypto::Task::id
int id() const
Definition: task.cpp:128
auditlog.h
Kleo::Crypto::Task::Result::icon
virtual QString icon() const
Definition: task.cpp:241
Kleo::Crypto::Task::Result::VisualCode
VisualCode
Definition: task.h:129
Kleo::AuditLog
Definition: auditlog.h:48
Kleo::Crypto::Task::Result::formatKeyLink
QString formatKeyLink(const char *fingerprint, const QString &content) const
Definition: task.cpp:205
Kleo::Crypto::Task
Definition: task.h:57
Kleo::Crypto::Task::Result::~Result
virtual ~Result()
Definition: task.cpp:203
Kleo::Crypto::Task::tag
virtual QString tag() const
Definition: task.cpp:143
image
static QString image(const char *img)
Definition: task.cpp:214
boost::shared_ptr
Definition: encryptemailcontroller.h:51
QString::number
QString number(int n, int base)
QString::fromLocal8Bit
QString fromLocal8Bit(const char *str, int size)
d
#define d
Definition: adduseridcommand.cpp:89
Kleo::Crypto::Task::totalSize
unsigned long long totalSize() const
Definition: task.cpp:138
QObject
Kleo::Crypto::Task::processedSize
unsigned long long processedSize() const
Definition: task.cpp:133
Kleo::Crypto::Task::start
void start()
Definition: task.cpp:160
QString
Kleo::Crypto::Task::~Task
~Task()
Definition: task.cpp:110
gnupg-helper.h
Kleo::Crypto::Task::makeErrorTask
static boost::shared_ptr< Task > makeErrorTask(int code, const QString &details, const QString &label)
Definition: task.cpp:122
QLatin1Char
Kleo::Crypto::Task::setProgress
void setProgress(const QString &msg, int processed, int total)
Definition: task.cpp:152
Kleo::makeGnuPGError
int makeGnuPGError(int code)
Definition: gnupg-helper.cpp:69
QMetaObject::invokeMethod
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
Kleo::Crypto::Task::asciiArmor
bool asciiArmor() const
Definition: task.cpp:117
makeNonce
static QString makeNonce()
Definition: task.cpp:192
Kleo::Crypto::Task::Result
Definition: task.h:119
task.h
QLatin1String
Kleo::Crypto::Task::progressLabel
QString progressLabel() const
Definition: task.cpp:147
q
#define q
Definition: adduseridcommand.cpp:90
Kleo::Crypto::Task::Result::hasError
bool hasError() const
Definition: task.cpp:209
Kleo::Crypto::Task::makeErrorResult
boost::shared_ptr< Result > makeErrorResult(int errCode, const QString &details)
Definition: task.cpp:186
Kleo::Crypto::Task::Result::iconPath
static QString iconPath(VisualCode code)
Definition: task.cpp:224
Kleo::Crypto::Task::label
virtual QString label() const =0
Kleo::Crypto::Task::emitResult
void emitResult(const boost::shared_ptr< const Task::Result > &result)
Definition: task.cpp:179
Kleo::Crypto::Task::Result::makeOverview
static QString makeOverview(const QString &msg)
Definition: task.cpp:219
Kleo::Crypto::Task::Result::Result
Result()
Definition: task.cpp:202
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:11 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kleopatra

Skip menu "kleopatra"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

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