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

kleopatra

  • sources
  • kde-4.12
  • 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 <KLocale>
50 
51 #include <QIcon>
52 #include <QString>
53 
54 #include <boost/bind.hpp>
55 
56 using namespace Kleo;
57 using namespace Kleo::Crypto;
58 using namespace boost;
59 using namespace GpgME;
60 
61 namespace {
62 
63  class ErrorResult : public Task::Result {
64  public:
65  ErrorResult( int code, const QString & details )
66  : Task::Result(), m_code( code ), m_details( details ) {}
67 
68  /* reimp */ QString overview() const { return makeOverview( m_details ); }
69  /* reimp */ QString details() const { return QString(); }
70  /* reimp */ int errorCode() const { return m_code; }
71  /* reimp */ QString errorString() const { return m_details; }
72  /* reimp */ VisualCode code() const { return NeutralError; }
73  /* reimp */ AuditLog auditLog() const { return AuditLog(); }
74  private:
75  int m_code;
76  QString m_details;
77  };
78 }
79 
80 class Task::Private {
81  friend class ::Kleo::Crypto::Task;
82  Task * const q;
83 public:
84  explicit Private( Task * qq );
85 
86 private:
87  QString m_progressLabel;
88  double m_processedPercent;
89  bool m_asciiArmor;
90  int m_id;
91 };
92 
93 namespace {
94  static int nextTaskId = 0;
95 }
96 
97 Task::Private::Private( Task * qq )
98  : q( qq ), m_progressLabel(), m_processedPercent( 0.0 ), m_asciiArmor( false ), m_id( nextTaskId++ )
99 {
100 
101 }
102 
103 Task::Task( QObject * p )
104  : QObject( p ), d( new Private( this ) )
105 {
106 
107 }
108 
109 Task::~Task() {}
110 
111 void Task::setAsciiArmor( bool armor )
112 {
113  d->m_asciiArmor = armor;
114 }
115 
116 bool Task::asciiArmor() const
117 {
118  return d->m_asciiArmor;
119 }
120 
121 shared_ptr<Task> Task::makeErrorTask( int code, const QString & details, const QString & label ) {
122  const shared_ptr<SimpleTask> t( new SimpleTask( label ) );
123  t->setResult( t->makeErrorResult( code, details ) );
124  return t;
125 }
126 
127 int Task::id() const
128 {
129  return d->m_id;
130 }
131 
132 unsigned long long Task::processedSize() const
133 {
134  return qRound( d->m_processedPercent * totalSize() );
135 }
136 
137 unsigned long long Task::totalSize() const
138 {
139  return inputSize();
140 }
141 
142 QString Task::tag() const {
143  return QString();
144 }
145 
146 QString Task::progressLabel() const
147 {
148  return d->m_progressLabel;
149 }
150 
151 void Task::setProgress( const QString & label, int processed, int total )
152 {
153  const double percent = total > 0 ? static_cast<double>( processed ) / total : 0.0;
154  d->m_processedPercent = percent;
155  d->m_progressLabel = label;
156  emit progress( label, processedSize(), totalSize() );
157 }
158 
159 void Task::start() {
160  try {
161  doStart();
162  } catch ( const Kleo::Exception & e ) {
163  QMetaObject::invokeMethod( this, "emitError", Qt::QueuedConnection, Q_ARG( int, e.error().encodedError() ), Q_ARG( QString, e.message() ) );
164  } catch ( const GpgME::Exception & e ) {
165  QMetaObject::invokeMethod( this, "emitError", Qt::QueuedConnection, Q_ARG( int, e.error().encodedError() ), Q_ARG( QString, QString::fromLocal8Bit( e.what() ) ) );
166  } catch ( const std::exception & e ) {
167  QMetaObject::invokeMethod( this, "emitError", Qt::QueuedConnection, Q_ARG( int, makeGnuPGError( GPG_ERR_UNEXPECTED ) ), Q_ARG( QString, QString::fromLocal8Bit( e.what() ) ) );
168  } catch ( ... ) {
169  QMetaObject::invokeMethod( this, "emitError", Qt::QueuedConnection, Q_ARG( int, makeGnuPGError( GPG_ERR_UNEXPECTED ) ), Q_ARG( QString, i18n( "Unknown exception in Task::start()") ) );
170  }
171  emit started();
172 }
173 
174 void Task::emitError( int errCode, const QString& details ) {
175  emitResult( makeErrorResult( errCode, details ) );
176 }
177 
178 void Task::emitResult( const shared_ptr<const Task::Result> & r )
179 {
180  d->m_processedPercent = 1.0;
181  emit progress( progressLabel(), processedSize(), totalSize() );
182  emit result( r );
183 }
184 
185 shared_ptr<Task::Result> Task::makeErrorResult( int errCode, const QString& details )
186 {
187  return shared_ptr<Task::Result>( new ErrorResult( errCode, details ) );
188 }
189 
190 
191 static QString makeNonce() {
192  // ### make better
193  return QString::number( qrand(), 16 );
194 }
195 
196 class Task::Result::Private {
197 public:
198  Private() {}
199 };
200 
201 Task::Result::Result() : m_nonce( makeNonce() ), d( new Private() ) {}
202 Task::Result::~Result() {}
203 
204 QString Task::Result::formatKeyLink( const char * fpr, const QString & content ) const {
205  return QLatin1String("<a href=\"key:") + m_nonce + QLatin1Char(':') + QLatin1String(fpr) + QLatin1String("\">") + content + QLatin1String("</a>");
206 }
207 
208 bool Task::Result::hasError() const
209 {
210  return errorCode() != 0;
211 }
212 
213 static QString image( const char* img ) {
214  // ### escape?
215  return KIconLoader::global()->iconPath( QLatin1String(img), KIconLoader::Small );
216 }
217 
218 QString Task::Result::makeOverview( const QString& msg )
219 {
220  return QLatin1String("<b>") + msg + QLatin1String("</b>");
221 }
222 
223 QString Task::Result::iconPath( VisualCode code )
224 {
225  switch ( code ) {
226  case Danger:
227  return image( "dialog-error" );
228  case AllGood:
229  return image( "dialog-ok" );
230  case Warning:
231  return image( "dialog-warning" );
232  case NeutralError:
233  case NeutralSuccess:
234  default:
235  return QString();
236 
237  }
238 }
239 
240 QString Task::Result::icon() const { return iconPath( code() ); }
241 
242 
243 #include "task_p.moc"
244 #include "moc_task.cpp"
task_p.h
Kleo::Crypto::SimpleTask
Definition: task_p.h:44
Kleo::Crypto::Task::setAsciiArmor
void setAsciiArmor(bool armor)
Definition: task.cpp:111
Kleo::Crypto::Task::id
int id() const
Definition: task.cpp:127
auditlog.h
Kleo::Crypto::Task::Result::icon
virtual QString icon() const
Definition: task.cpp:240
Kleo::Crypto::Task::Result::VisualCode
VisualCode
Definition: task.h:127
Kleo::AuditLog
Definition: auditlog.h:48
Kleo::Crypto::Task::Result::formatKeyLink
QString formatKeyLink(const char *fingerprint, const QString &content) const
Definition: task.cpp:204
Kleo::Crypto::Task
Definition: task.h:55
Kleo::Crypto::Task::Result::~Result
virtual ~Result()
Definition: task.cpp:202
Kleo::Crypto::Task::tag
virtual QString tag() const
Definition: task.cpp:142
image
static QString image(const char *img)
Definition: task.cpp:213
boost::shared_ptr
Definition: encryptemailcontroller.h:51
d
#define d
Definition: adduseridcommand.cpp:90
Kleo::Crypto::Task::totalSize
unsigned long long totalSize() const
Definition: task.cpp:137
Kleo::Crypto::Task::processedSize
unsigned long long processedSize() const
Definition: task.cpp:132
Kleo::Crypto::Task::start
void start()
Definition: task.cpp:159
Kleo::Crypto::Task::~Task
~Task()
Definition: task.cpp:109
gnupg-helper.h
Kleo::Crypto::Task::makeErrorTask
static boost::shared_ptr< Task > makeErrorTask(int code, const QString &details, const QString &label)
Definition: task.cpp:121
Kleo::Crypto::Task::setProgress
void setProgress(const QString &msg, int processed, int total)
Definition: task.cpp:151
Kleo::makeGnuPGError
int makeGnuPGError(int code)
Definition: gnupg-helper.cpp:69
Kleo::Crypto::Task::asciiArmor
bool asciiArmor() const
Definition: task.cpp:116
makeNonce
static QString makeNonce()
Definition: task.cpp:191
Kleo::Crypto::Task::Result
Definition: task.h:117
task.h
Kleo::Crypto::Task::progressLabel
QString progressLabel() const
Definition: task.cpp:146
q
#define q
Definition: adduseridcommand.cpp:91
Kleo::Crypto::Task::Result::hasError
bool hasError() const
Definition: task.cpp:208
Kleo::Crypto::Task::makeErrorResult
boost::shared_ptr< Result > makeErrorResult(int errCode, const QString &details)
Definition: task.cpp:185
Kleo::Crypto::Task::Result::iconPath
static QString iconPath(VisualCode code)
Definition: task.cpp:223
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:178
Kleo::Crypto::Task::Result::makeOverview
static QString makeOverview(const QString &msg)
Definition: task.cpp:218
Kleo::Crypto::Task::Result::Result
Result()
Definition: task.cpp:201
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:42 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

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