• 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
taskcollection.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  crypto/taskcollection.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2008 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 "taskcollection.h"
36 
37 #include <crypto/task.h>
38 
39 #ifndef Q_MOC_RUN
40 #include <boost/bind.hpp>
41 #endif
42 
43 #include <algorithm>
44 #include <map>
45 
46 #include <cassert>
47 #include <cmath>
48 
49 using namespace Kleo;
50 using namespace Kleo::Crypto;
51 using namespace boost;
52 
53 class TaskCollection::Private {
54  TaskCollection* const q;
55 public:
56  explicit Private( TaskCollection* qq );
57 
58  void taskProgress( const QString &, int, int );
59  void taskResult( const shared_ptr<const Task::Result> & );
60  void taskStarted();
61  void calculateAndEmitProgress();
62 
63  std::map<int, shared_ptr<Task> > m_tasks;
64  mutable int m_totalSize;
65  mutable int m_processedSize;
66  unsigned int m_nCompleted;
67  QString m_lastProgressMessage;
68  bool m_errorOccurred;
69 };
70 
71 TaskCollection::Private::Private( TaskCollection* qq ) : q( qq ), m_totalSize( 0 ), m_processedSize( 0 ), m_nCompleted( 0 ), m_errorOccurred( false )
72 {
73 }
74 
75 int TaskCollection::numberOfCompletedTasks() const
76 {
77  return d->m_nCompleted;
78 }
79 
80 size_t TaskCollection::size() const {
81  return d->m_tasks.size();
82 }
83 
84 bool TaskCollection::allTasksCompleted() const
85 {
86  assert( d->m_nCompleted <= d->m_tasks.size() );
87  return d->m_nCompleted == d->m_tasks.size();
88 }
89 
90 void TaskCollection::Private::taskProgress( const QString & msg, int, int )
91 {
92  m_lastProgressMessage = msg;
93  calculateAndEmitProgress();
94 }
95 
96 void TaskCollection::Private::taskResult( const shared_ptr<const Task::Result> & result )
97 {
98  assert( result );
99  ++m_nCompleted;
100  m_errorOccurred = m_errorOccurred || result->hasError();
101  m_lastProgressMessage.clear();
102  calculateAndEmitProgress();
103  emit q->result( result );
104  if ( q->allTasksCompleted() )
105  emit q->done();
106 }
107 
108 void TaskCollection::Private::taskStarted()
109 {
110  const Task * const task = qobject_cast<Task*>( q->sender() );
111  assert( task );
112  assert( m_tasks.find( task->id() ) != m_tasks.end() );
113  emit q->started( m_tasks[task->id()] );
114  calculateAndEmitProgress(); // start Knight-Rider-Mode right away (gpgsm doesn't report _any_ progress).
115 }
116 
117 void TaskCollection::Private::calculateAndEmitProgress()
118 {
119  typedef std::map<int, shared_ptr<Task> >::const_iterator ConstIterator;
120 
121  int total = 0;
122  int processed = 0;
123  uint knownSize = m_tasks.size();
124 
125  for ( ConstIterator it = m_tasks.begin(), end = m_tasks.end(); it != end; ++it ) {
126  const shared_ptr<Task> & i = it->second;
127  assert( i );
128  if ( i->totalSize() == 0 )
129  --knownSize;
130  processed += i->processedSize();
131  total += i->totalSize();
132  }
133  // use the average of the known sizes to estimate the unknown ones:
134  if ( knownSize > 0 )
135  total += static_cast<int>( std::ceil( ( m_tasks.size() - knownSize ) * ( static_cast<double>( total ) / knownSize ) ) );
136  if ( m_totalSize == total && m_processedSize == processed )
137  return;
138  m_totalSize = total;
139  m_processedSize = processed;
140  if ( processed )
141  emit q->progress( m_lastProgressMessage, processed, total );
142  else
143  // use knight-rider mode until we have some progress
144  emit q->progress( m_lastProgressMessage, processed, 0 );
145 }
146 
147 TaskCollection::TaskCollection( QObject * parent ) : QObject( parent ), d( new Private( this ) )
148 {
149 }
150 
151 TaskCollection::~TaskCollection()
152 {
153 }
154 
155 bool TaskCollection::isEmpty() const
156 {
157  return d->m_tasks.empty();
158 }
159 
160 bool TaskCollection::errorOccurred() const
161 {
162  return d->m_errorOccurred;
163 }
164 
165 shared_ptr<Task> TaskCollection::taskById( int id ) const
166 {
167  const std::map<int, shared_ptr<Task> >::const_iterator it = d->m_tasks.find( id );
168  return it != d->m_tasks.end() ? it->second : shared_ptr<Task>();
169 }
170 
171 std::vector<shared_ptr<Task> > TaskCollection::tasks() const
172 {
173  typedef std::map<int, shared_ptr<Task> >::const_iterator ConstIterator;
174  std::vector<shared_ptr<Task> > res;
175  res.reserve( d->m_tasks.size() );
176  for ( ConstIterator it = d->m_tasks.begin(), end = d->m_tasks.end(); it != end; ++it )
177  res.push_back( it->second );
178  return res;
179 }
180 
181 void TaskCollection::setTasks( const std::vector<shared_ptr<Task> > & tasks )
182 {
183  Q_FOREACH( const shared_ptr<Task> & i, tasks ) {
184  assert( i );
185  d->m_tasks[i->id()] = i;
186  connect( i.get(), SIGNAL(progress(QString,int,int)),
187  this, SLOT(taskProgress(QString,int,int)) );
188  connect( i.get(), SIGNAL(result(boost::shared_ptr<const Kleo::Crypto::Task::Result>)),
189  this, SLOT(taskResult(boost::shared_ptr<const Kleo::Crypto::Task::Result>)) );
190  connect( i.get(), SIGNAL(started()),
191  this, SLOT(taskStarted()) );
192  }
193 }
194 
195 #include "moc_taskcollection.cpp"
196 
Kleo::Crypto::TaskCollection::result
void result(const boost::shared_ptr< const Kleo::Crypto::Task::Result > &result)
Kleo::Crypto::TaskCollection::allTasksCompleted
bool allTasksCompleted() const
Definition: taskcollection.cpp:84
Kleo::Crypto::Task::id
int id() const
Definition: task.cpp:128
Kleo::Crypto::TaskCollection::~TaskCollection
~TaskCollection()
Definition: taskcollection.cpp:151
Kleo::Crypto::TaskCollection::isEmpty
bool isEmpty() const
Definition: taskcollection.cpp:155
Kleo::Crypto::Task
Definition: task.h:57
Kleo::Crypto::TaskCollection::setTasks
void setTasks(const std::vector< boost::shared_ptr< Task > > &tasks)
Definition: taskcollection.cpp:181
Kleo::Crypto::TaskCollection::TaskCollection
TaskCollection(QObject *parent=0)
Definition: taskcollection.cpp:147
Kleo::Crypto::TaskCollection::tasks
std::vector< boost::shared_ptr< Task > > tasks() const
Definition: taskcollection.cpp:171
boost::shared_ptr
Definition: encryptemailcontroller.h:51
d
#define d
Definition: adduseridcommand.cpp:89
QObject
Kleo::Crypto::TaskCollection::started
void started(const boost::shared_ptr< Kleo::Crypto::Task > &task)
QString
Kleo::Crypto::TaskCollection::size
size_t size() const
Definition: taskcollection.cpp:80
task.h
Kleo::Crypto::TaskCollection::taskById
boost::shared_ptr< Task > taskById(int id) const
Definition: taskcollection.cpp:165
Kleo::Crypto::TaskCollection
Definition: taskcollection.h:51
Kleo::Crypto::TaskCollection::progress
void progress(const QString &msg, int processed, int total)
q
#define q
Definition: adduseridcommand.cpp:90
Kleo::Crypto::TaskCollection::errorOccurred
bool errorOccurred() const
Definition: taskcollection.cpp:160
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Kleo::Crypto::TaskCollection::numberOfCompletedTasks
int numberOfCompletedTasks() const
Definition: taskcollection.cpp:75
taskcollection.h
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