• 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
  • gui
verifychecksumsdialog.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  crypto/gui/verifychecksumsdialog.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2010 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 "verifychecksumsdialog.h"
36 
37 #ifndef QT_NO_DIRMODEL
38 
39 #include <KDebug>
40 #include <KLocalizedString>
41 #include <KMessageBox>
42 
43 #include <QHBoxLayout>
44 #include <QLabel>
45 #include <QStringList>
46 #include <QVBoxLayout>
47 #include <QHash>
48 #include <QTreeView>
49 #include <QSortFilterProxyModel>
50 #include <QDirModel>
51 #include <QProgressBar>
52 #include <QDialogButtonBox>
53 #include <QPushButton>
54 #include <QHeaderView>
55 
56 #include <boost/static_assert.hpp>
57 
58 #include <cassert>
59 
60 using namespace Kleo;
61 using namespace Kleo::Crypto;
62 using namespace Kleo::Crypto::Gui;
63 using namespace boost;
64 
65 namespace {
66 
67  static Qt::GlobalColor statusColor[] = {
68  Qt::color0, // Unknown - nothing
69  Qt::green, // OK
70  Qt::red, // Failed
71  Qt::darkRed, // Error
72  };
73  BOOST_STATIC_ASSERT((sizeof statusColor/sizeof *statusColor == VerifyChecksumsDialog::NumStatii));
74 
75  class ColorizedFileSystemModel : public QDirModel {
76  Q_OBJECT
77  public:
78  explicit ColorizedFileSystemModel( QObject * parent=0 )
79  : QDirModel( parent ),
80  statusMap()
81  {
82 
83  }
84 
85  /* reimp */ QVariant data( const QModelIndex & mi, int role=Qt::DisplayRole ) const {
86  if ( mi.isValid() && role == Qt::BackgroundRole ) {
87  const QHash<QString,VerifyChecksumsDialog::Status>::const_iterator
88  it = statusMap.find( filePath( mi ) );
89  if ( it != statusMap.end() )
90  if ( const Qt::GlobalColor c = statusColor[*it] )
91  return c;
92  }
93  return QDirModel::data( mi, role );
94  }
95 
96  public Q_SLOTS:
97  void setStatus( const QString & file, VerifyChecksumsDialog::Status status ) {
98 
99  if ( status >= VerifyChecksumsDialog::NumStatii || file.isEmpty() )
100  return;
101 
102  // canonicalize filename:
103  const QModelIndex mi = index( file );
104  const QString canonical = filePath( mi );
105  if ( canonical.isEmpty() ) {
106  kDebug() << "can't locate file " << file;
107  return;
108  }
109 
110  const QHash<QString,VerifyChecksumsDialog::Status>::iterator
111  it = statusMap.find( canonical );
112 
113  if ( it != statusMap.end() )
114  if ( *it == status )
115  return; // nothing to do
116  else
117  *it = status;
118  else
119  statusMap[canonical] = status;
120 
121  emitDataChangedFor( mi );
122  }
123 
124  void clearStatusInformation() {
125  using std::swap;
126 
127  QHash<QString,VerifyChecksumsDialog::Status> oldStatusMap;
128  swap( statusMap, oldStatusMap );
129 
130  for ( QHash<QString,VerifyChecksumsDialog::Status>::const_iterator it = oldStatusMap.constBegin(), end = oldStatusMap.constEnd() ; it != end ; ++it )
131  emitDataChangedFor( it.key() );
132  }
133 
134  private:
135  void emitDataChangedFor( const QString & file ) {
136  emitDataChangedFor( index( file ) );
137  }
138  void emitDataChangedFor( const QModelIndex & mi ) {
139  const QModelIndex p = parent( mi );
140  emit dataChanged( index( mi.row(), 0, p ), index( mi.row(), columnCount( p ) - 1, p ) );
141  }
142 
143  private:
144  QHash<QString,VerifyChecksumsDialog::Status> statusMap;
145  };
146 
147 
148  static int find_layout_item( const QBoxLayout & blay ) {
149  for ( int i = 0, end = blay.count() ; i < end ; ++i )
150  if ( QLayoutItem * item = blay.itemAt( i ) )
151  if ( item->layout() )
152  return i;
153  return 0;
154  }
155 
156  struct BaseWidget {
157  QSortFilterProxyModel proxy;
158  QLabel label;
159  QTreeView view;
160 
161  BaseWidget( QDirModel * model, QWidget * parent, QVBoxLayout * vlay )
162  : proxy(),
163  label( parent ),
164  view( parent )
165  {
166  KDAB_SET_OBJECT_NAME( proxy );
167  KDAB_SET_OBJECT_NAME( label );
168  KDAB_SET_OBJECT_NAME( view );
169 
170  const int row = find_layout_item( *vlay );
171  vlay->insertWidget( row, &label );
172  vlay->insertWidget( row+1, &view, 1 );
173 
174  proxy.setSourceModel( model );
175 
176  view.setModel( &proxy );
177 
178  QRect r;
179  for( int i = 0; i < proxy.columnCount(); ++i )
180  view.resizeColumnToContents( i );
181 
182  // define some minimum sizes
183  view.header()->resizeSection( 0, qMax( view.header()->sectionSize( 0 ), 220 ) );
184  view.header()->resizeSection( 1, qMax( view.header()->sectionSize( 1 ), 75 ) );
185  view.header()->resizeSection( 2, qMax( view.header()->sectionSize( 2 ), 75 ) );
186  view.header()->resizeSection( 3, qMax( view.header()->sectionSize( 3 ), 140 ) );
187 
188  for( int i = 0; i < proxy.rowCount(); ++i )
189  r = r.united( view.visualRect( proxy.index( proxy.columnCount() - 1, i ) ) );
190  view.setMinimumSize( QSize( qBound( r.width() + 4 * view.frameWidth(), 220+75+75+140 + 4 * view.frameWidth(), 1024 ), // 100 is the default defaultSectionSize
191  qBound( r.height(), 220, 512 ) ) );
192  }
193 
194  void setBase( const QString & base ) {
195  label.setText( base );
196  if ( QDirModel * fsm = qobject_cast<QDirModel*>( proxy.sourceModel() ) ) {
197  view.setRootIndex( proxy.mapFromSource( fsm->index( base ) ) );
198  } else {
199  kWarning() << "expect a QDirModel-derived class as proxy.sourceModel(), got ";
200  if ( !proxy.sourceModel() ) {
201  kWarning() << "a null pointer";
202  } else {
203  kWarning() << proxy.sourceModel()->metaObject()->className();
204  }
205  }
206  }
207  };
208 
209 } // anon namespace
210 
211 
212 class VerifyChecksumsDialog::Private {
213  friend class ::Kleo::Crypto::Gui::VerifyChecksumsDialog;
214  VerifyChecksumsDialog * const q;
215 public:
216  explicit Private( VerifyChecksumsDialog * qq )
217  : q( qq ),
218  bases(),
219  errors(),
220  model(),
221  ui( q )
222  {
223  qRegisterMetaType<Status>( "Kleo::Crypto::Gui::VerifyChecksumsDialog::Status" );
224  }
225 
226 private:
227  void slotErrorButtonClicked() {
228  KMessageBox::errorList( q, i18n("The following errors and warnings were recorded:"),
229  errors, i18n("Checksum Verification Errors") );
230  }
231 
232 private:
233  void updateErrors() {
234  const bool active = ui.isProgressBarActive();
235  ui.progressLabel.setVisible( active );
236  ui.progressBar. setVisible( active );
237  ui.errorLabel. setVisible( !active );
238  ui.errorButton. setVisible( !active && !errors.empty() );
239  if ( errors.empty() )
240  ui.errorLabel.setText( i18n("No errors occurred" ) );
241  else
242  ui.errorLabel.setText( i18np( "One error occurred", "%1 errors occurred", errors.size() ) );
243  }
244 
245 private:
246  QStringList bases;
247  QStringList errors;
248  ColorizedFileSystemModel model;
249 
250  struct UI {
251  std::vector<BaseWidget*> baseWidgets;
252  QLabel progressLabel;
253  QProgressBar progressBar;
254  QLabel errorLabel;
255  QPushButton errorButton;
256  QDialogButtonBox buttonBox;
257  QVBoxLayout vlay;
258  QHBoxLayout hlay[2];
259 
260  explicit UI( VerifyChecksumsDialog * q )
261  : baseWidgets(),
262  progressLabel( i18n("Progress:"), q ),
263  progressBar( q ),
264  errorLabel( i18n("No errors occurred"), q ),
265  errorButton( i18nc("Show Errors","Show"), q ),
266  buttonBox( QDialogButtonBox::Close, Qt::Horizontal, q ),
267  vlay( q )
268  {
269  KDAB_SET_OBJECT_NAME( progressLabel );
270  KDAB_SET_OBJECT_NAME( progressBar );
271  KDAB_SET_OBJECT_NAME( errorLabel );
272  KDAB_SET_OBJECT_NAME( errorButton );
273  KDAB_SET_OBJECT_NAME( buttonBox );
274  KDAB_SET_OBJECT_NAME( vlay );
275  KDAB_SET_OBJECT_NAME( hlay[0] );
276  KDAB_SET_OBJECT_NAME( hlay[1] );
277 
278  errorButton.setAutoDefault( false );
279 
280  hlay[0].addWidget( &progressLabel );
281  hlay[0].addWidget( &progressBar, 1 );
282 
283  hlay[1].addWidget( &errorLabel, 1 );
284  hlay[1].addWidget( &errorButton );
285 
286  vlay.addLayout( &hlay[0] );
287  vlay.addLayout( &hlay[1] );
288  vlay.addWidget( &buttonBox );
289 
290  errorLabel.hide();
291  errorButton.hide();
292 
293  QPushButton * close = closeButton();
294 
295  connect( close, SIGNAL(clicked()), q, SIGNAL(canceled()) );
296  connect( close, SIGNAL(clicked()), q, SLOT(accept()) );
297 
298  connect( &errorButton, SIGNAL(clicked()), q, SLOT(slotErrorButtonClicked()) );
299  }
300 
301  ~UI() {
302  qDeleteAll( baseWidgets );
303  }
304 
305  QPushButton * closeButton() const {
306  return buttonBox.button( QDialogButtonBox::Close );
307  }
308 
309  void setBases( const QStringList & bases, QDirModel * model ) {
310 
311  // create new BaseWidgets:
312  for ( unsigned int i = baseWidgets.size(), end = bases.size() ; i < end ; ++i )
313  baseWidgets.push_back( new BaseWidget( model, vlay.parentWidget(), &vlay ) );
314 
315  // shed surplus BaseWidgets:
316  for ( unsigned int i = bases.size(), end = baseWidgets.size() ; i < end ; ++i ) {
317  delete baseWidgets.back();
318  baseWidgets.pop_back();
319  }
320 
321  assert( static_cast<unsigned>( bases.size() ) == baseWidgets.size() );
322 
323  // update bases:
324  for ( unsigned int i = 0 ; i < baseWidgets.size() ; ++i )
325  baseWidgets[i]->setBase( bases[i] );
326 
327  }
328 
329  void setProgress( int cur, int tot ) {
330  progressBar.setMaximum( tot );
331  progressBar.setValue( cur );
332  }
333 
334  bool isProgressBarActive() const {
335  const int tot = progressBar.maximum();
336  const int cur = progressBar.value();
337  return !tot || cur != tot ;
338  }
339 
340  } ui;
341 };
342 
343 VerifyChecksumsDialog::VerifyChecksumsDialog( QWidget * parent, Qt::WindowFlags flags )
344  : QDialog( parent, flags ),
345  d( new Private( this ) )
346 {
347 
348 }
349 
350 VerifyChecksumsDialog::~VerifyChecksumsDialog() {}
351 
352 // slot
353 void VerifyChecksumsDialog::setBaseDirectories( const QStringList & bases ) {
354  if ( d->bases == bases )
355  return;
356  d->bases = bases;
357  d->ui.setBases( bases, &d->model );
358 }
359 
360 // slot
361 void VerifyChecksumsDialog::setErrors( const QStringList & errors ) {
362  if ( d->errors == errors )
363  return;
364  d->errors = errors;
365  d->updateErrors();
366 }
367 
368 // slot
369 void VerifyChecksumsDialog::setProgress( int cur, int tot ) {
370  d->ui.setProgress( cur, tot );
371  d->updateErrors();
372 }
373 
374 // slot
375 void VerifyChecksumsDialog::setStatus( const QString & file, Status status ) {
376  d->model.setStatus( file, status );
377 }
378 
379 // slot
380 void VerifyChecksumsDialog::clearStatusInformation() {
381  d->errors.clear();
382  d->updateErrors();
383  d->model.clearStatusInformation();
384 }
385 
386 #include "verifychecksumsdialog.moc"
387 #include "moc_verifychecksumsdialog.cpp"
388 
389 #endif // QT_NO_DIRMODEL
Kleo::Crypto::Gui::VerifyChecksumsDialog::VerifyChecksumsDialog
VerifyChecksumsDialog(QWidget *parent=0, Qt::WindowFlags flags=0)
Definition: verifychecksumsdialog.cpp:343
flags
static const char * flags[]
Definition: readerstatus.cpp:115
Kleo::Crypto::Gui::VerifyChecksumsDialog::setErrors
void setErrors(const QStringList &errors)
Definition: verifychecksumsdialog.cpp:361
Kleo::Crypto::Gui::VerifyChecksumsDialog::setStatus
void setStatus(const QString &file, Kleo::Crypto::Gui::VerifyChecksumsDialog::Status status)
Definition: verifychecksumsdialog.cpp:375
QDialog
Kleo::Crypto::Gui::VerifyChecksumsDialog
Definition: verifychecksumsdialog.h:47
QWidget
Kleo::Crypto::Gui::VerifyChecksumsDialog::setProgress
void setProgress(int current, int total)
Definition: verifychecksumsdialog.cpp:369
status
VerifyChecksumsDialog::Status status
Definition: verifychecksumscontroller.cpp:512
d
#define d
Definition: adduseridcommand.cpp:90
verifychecksumsdialog.h
KDAB_SET_OBJECT_NAME
#define KDAB_SET_OBJECT_NAME(x)
Definition: kdtoolsglobal.h:66
Kleo::Crypto::Gui::VerifyChecksumsDialog::~VerifyChecksumsDialog
~VerifyChecksumsDialog()
Definition: verifychecksumsdialog.cpp:350
QSortFilterProxyModel
Kleo::Crypto::Gui::VerifyChecksumsDialog::clearStatusInformation
void clearStatusInformation()
Definition: verifychecksumsdialog.cpp:380
BOOST_STATIC_ASSERT
BOOST_STATIC_ASSERT((sizeof icons/sizeof(*icons)==NumStates))
q
#define q
Definition: adduseridcommand.cpp:91
Kleo::Crypto::Gui::VerifyChecksumsDialog::Status
Status
Definition: verifychecksumsdialog.h:54
Kleo::Crypto::Gui::VerifyChecksumsDialog::setBaseDirectories
void setBaseDirectories(const QStringList &bases)
Definition: verifychecksumsdialog.cpp:353
Kleo::Crypto::Gui::VerifyChecksumsDialog::NumStatii
Definition: verifychecksumsdialog.h:59
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