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