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