• 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
  • dialogs
selftestdialog.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  dialogs/selftestdialog.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 "selftestdialog.h"
36 
37 #include "ui_selftestdialog.h"
38 
39 #include <selftest/selftest.h>
40 
41 #include <KDebug>
42 
43 #include <QAbstractTableModel>
44 #include <QHeaderView>
45 #include <QSortFilterProxyModel>
46 
47 #ifndef Q_MOC_RUN
48 #include <boost/shared_ptr.hpp>
49 #endif
50 
51 #include <cassert>
52 #include <vector>
53 
54 using namespace Kleo;
55 using namespace Kleo::Dialogs;
56 using namespace boost;
57 
58 namespace {
59 
60  class Model : public QAbstractTableModel {
61  Q_OBJECT
62  public:
63  explicit Model( QObject * parent=0 )
64  : QAbstractTableModel( parent ),
65  m_tests()
66  {
67 
68  }
69 
70  enum Column {
71  TestName,
72  TestResult,
73 
74  NumColumns
75  };
76 
77  const shared_ptr<SelfTest> & fromModelIndex( const QModelIndex & idx ) const {
78  const unsigned int row = idx.row();
79  if ( row < m_tests.size() )
80  return m_tests[row];
81  static const shared_ptr<SelfTest> null;
82  return null;
83  }
84 
85  /* reimp */ int rowCount( const QModelIndex & idx ) const { return idx.isValid() ? 0 : m_tests.size() ; }
86  /* reimp */ int columnCount( const QModelIndex & ) const { return NumColumns; }
87 
88  /* reimp */ QVariant data( const QModelIndex & idx, int role ) const {
89  const unsigned int row = idx.row();
90  if ( idx.isValid() && row < m_tests.size() )
91  switch ( role ) {
92  case Qt::DisplayRole:
93  case Qt::ToolTipRole:
94  switch ( idx.column() ) {
95  case TestName:
96  return m_tests[row]->name();
97  case TestResult:
98  return
99  m_tests[row]->skipped() ? i18n("Skipped") :
100  m_tests[row]->passed() ? i18n("Passed") :
101  /* else */ m_tests[row]->shortError();
102  }
103  break;
104  case Qt::BackgroundRole:
105  return QColor( m_tests[row]->skipped() ? Qt::yellow :
106  m_tests[row]->passed() ? Qt::green :
107  Qt::red );
108  }
109  return QVariant();
110  }
111 
112  /* reimp */ QVariant headerData( int section, Qt::Orientation o, int role ) const {
113  if ( o == Qt::Horizontal &&
114  section >= 0 && section < NumColumns &&
115  role == Qt::DisplayRole )
116  switch ( section ) {
117  case TestName: return i18n("Test Name");
118  case TestResult: return i18n("Result");
119  }
120  return QVariant();
121  }
122 
123  void clear() {
124  if ( m_tests.empty() )
125  return;
126  beginRemoveRows( QModelIndex(), 0, m_tests.size() - 1 );
127  m_tests.clear();
128  endRemoveRows();
129  }
130 
131  void append( const std::vector< shared_ptr<SelfTest> > & tests ) {
132  if ( tests.empty() )
133  return;
134  beginInsertRows( QModelIndex(), m_tests.size(), m_tests.size() + tests.size() );
135  m_tests.insert( m_tests.end(), tests.begin(), tests.end() );
136  endInsertRows();
137  }
138 
139  void reloadData() {
140  if ( !m_tests.empty() )
141  emit dataChanged( index( 0, 0 ), index( m_tests.size() - 1, NumColumns - 1 ) );
142  }
143 
144  const shared_ptr<SelfTest> & at( unsigned int idx ) const {
145  return m_tests.at( idx );
146  }
147 
148  private:
149  std::vector< shared_ptr<SelfTest> > m_tests;
150  };
151 
152  class Proxy : public QSortFilterProxyModel {
153  Q_OBJECT
154  public:
155  explicit Proxy( QObject * parent=0 )
156  : QSortFilterProxyModel( parent ), m_showAll( true )
157  {
158  setDynamicSortFilter( true );
159  }
160 
161  bool showAll() const { return m_showAll; }
162 
163  Q_SIGNALS:
164  void showAllChanged( bool );
165 
166  public Q_SLOTS:
167  void setShowAll( bool on ) {
168  if ( on == m_showAll )
169  return;
170  m_showAll = on;
171  invalidateFilter();
172  emit showAllChanged( on );
173  }
174 
175  private:
176  /* reimp */ bool filterAcceptsRow( int src_row, const QModelIndex & src_parent ) const
177  {
178  if ( m_showAll ) {
179  return true;
180  }
181  if ( const Model * const model = qobject_cast<Model*>( sourceModel() ) ) {
182  if ( !src_parent.isValid() && src_row >= 0 &&
183  src_row < model->rowCount( src_parent ) ) {
184  if ( const shared_ptr<SelfTest> & t = model->at( src_row ) ) {
185  return !t->passed() ;
186  } else {
187  kWarning() << "NULL test??";
188  }
189  } else {
190  if ( src_parent.isValid() ) {
191  kWarning() << "view asks for subitems!";
192  } else {
193  kWarning() << "index " << src_row
194  << " is out of range [" << 0
195  << "," << model->rowCount( src_parent )
196  << "]";
197  }
198  }
199  } else {
200  kWarning() << "expected a ::Model, got ";
201  if ( !sourceModel() ) {
202  kWarning() << "a null pointer";
203  } else {
204  kWarning() << sourceModel()->metaObject()->className();
205  }
206 
207  }
208  return false;
209  }
210 
211  private:
212  bool m_showAll;
213  };
214 
215 }
216 
217 class SelfTestDialog::Private {
218  friend class ::Kleo::Dialogs::SelfTestDialog;
219  SelfTestDialog * const q;
220 public:
221  explicit Private( SelfTestDialog * qq )
222  : q( qq ),
223  model( q ),
224  proxy( q ),
225  ui( q )
226  {
227  proxy.setSourceModel( &model );
228  ui.resultsTV->setModel( &proxy );
229 
230  ui.detailsGB->hide();
231  ui.proposedCorrectiveActionGB->hide();
232 
233  connect( ui.resultsTV->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
234  q, SLOT(slotSelectionChanged()) );
235  connect( ui.showAllCB, SIGNAL(toggled(bool)),
236  &proxy, SLOT(setShowAll(bool)) );
237  }
238 
239 private:
240  void slotSelectionChanged() {
241  const int row = selectedRowIndex();
242  if ( row < 0 ) {
243  ui.detailsLB->setText( i18n("(select test first)") );
244  ui.detailsGB->hide();
245  ui.proposedCorrectiveActionGB->hide();
246  } else {
247  const shared_ptr<SelfTest> & t = model.at( row );
248  ui.detailsLB->setText( t->longError() );
249  ui.detailsGB->setVisible( !t->passed() );
250  const QString action = t->proposedFix();
251  ui.proposedCorrectiveActionGB->setVisible( !t->passed() && !action.isEmpty() );
252  ui.proposedCorrectiveActionLB->setText( action );
253  ui.doItPB->setVisible( !t->passed() && t->canFixAutomatically() );
254  }
255  }
256  void slotDoItClicked() {
257  if ( const shared_ptr<SelfTest> st = model.fromModelIndex( selectedRow() ) )
258  if ( st->fix() )
259  model.reloadData();
260  }
261 
262 private:
263  void updateColumnSizes() {
264  ui.resultsTV->header()->resizeSections( QHeaderView::ResizeToContents );
265  }
266 
267 private:
268  QModelIndex selectedRow() const {
269  const QItemSelectionModel * const ism = ui.resultsTV->selectionModel();
270  if ( !ism )
271  return QModelIndex();
272  const QModelIndexList mil = ism->selectedRows();
273  return mil.empty() ? QModelIndex() : proxy.mapToSource( mil.front() ) ;
274  }
275  int selectedRowIndex() const {
276  return selectedRow().row();
277  }
278 
279 private:
280  Model model;
281  Proxy proxy;
282 
283  struct UI : public Ui_SelfTestDialog {
284 
285  QPushButton * rerunPB;
286 
287  explicit UI( SelfTestDialog * qq )
288  : Ui_SelfTestDialog(),
289  rerunPB( new QPushButton( i18n("Rerun Tests") ) )
290  {
291  setupUi( qq );
292 
293  buttonBox->addButton( rerunPB, QDialogButtonBox::ActionRole );
294  buttonBox->button( QDialogButtonBox::Ok )->setText( i18n("Continue") );
295 
296  connect( rerunPB, SIGNAL(clicked()),
297  qq, SIGNAL(updateRequested()) );
298  }
299  } ui;
300 };
301 
302 SelfTestDialog::SelfTestDialog( QWidget * p, Qt::WindowFlags f )
303  : QDialog( p, f ), d( new Private( this ) )
304 {
305  setAutomaticMode( false );
306 }
307 
308 SelfTestDialog::SelfTestDialog( const std::vector< shared_ptr<SelfTest> > & tests, QWidget * p, Qt::WindowFlags f )
309  : QDialog( p, f ), d( new Private( this ) )
310 {
311  addSelfTests( tests );
312  setAutomaticMode( false );
313 }
314 
315 SelfTestDialog::~SelfTestDialog() {}
316 
317 void SelfTestDialog::clear() {
318  d->model.clear();
319 }
320 
321 void SelfTestDialog::addSelfTest( const shared_ptr<SelfTest> & test ) {
322  d->model.append( std::vector< shared_ptr<SelfTest> >( 1, test ) );
323  d->updateColumnSizes();
324 }
325 
326 void SelfTestDialog::addSelfTests( const std::vector< shared_ptr<SelfTest> > & tests ) {
327  d->model.append( tests );
328  d->updateColumnSizes();
329 }
330 
331 void SelfTestDialog::setRunAtStartUp( bool on ) {
332  d->ui.runAtStartUpCB->setChecked( on );
333 }
334 
335 bool SelfTestDialog::runAtStartUp() const {
336  return d->ui.runAtStartUpCB->isChecked();
337 }
338 
339 void SelfTestDialog::setAutomaticMode( bool automatic ) {
340  d->ui.buttonBox->button( QDialogButtonBox::Ok )->setVisible( automatic );
341  d->ui.buttonBox->button( QDialogButtonBox::Cancel )->setVisible( automatic );
342  d->ui.buttonBox->button( QDialogButtonBox::Close )->setVisible( !automatic );
343 }
344 
345 #include "selftestdialog.moc"
346 #include "moc_selftestdialog.cpp"
QModelIndex
QWidget
Kleo::Dialogs::SelfTestDialog::runAtStartUp
bool runAtStartUp() const
QAbstractTableModel
Kleo::Dialogs::SelfTestDialog::SelfTestDialog
SelfTestDialog(QWidget *parent=0, Qt::WindowFlags f=0)
Definition: selftestdialog.cpp:302
QModelIndex::isValid
bool isValid() const
boost::shared_ptr
Definition: encryptemailcontroller.h:51
d
#define d
Definition: adduseridcommand.cpp:89
Kleo::Dialogs::SelfTestDialog::~SelfTestDialog
~SelfTestDialog()
Definition: selftestdialog.cpp:315
Kleo::Dialogs::SelfTestDialog::setAutomaticMode
void setAutomaticMode(bool automatic)
Definition: selftestdialog.cpp:339
QObject
QString::isEmpty
bool isEmpty() const
Kleo::Dialogs::SelfTestDialog::addSelfTest
void addSelfTest(const boost::shared_ptr< SelfTest > &test)
Definition: selftestdialog.cpp:321
QItemSelectionModel::selectedRows
QModelIndexList selectedRows(int column) const
QModelIndex::row
int row() const
Kleo::Dialogs::SelfTestDialog::clear
void clear()
Definition: selftestdialog.cpp:317
QString
QColor
Kleo::Dialogs::SelfTestDialog::addSelfTests
void addSelfTests(const std::vector< boost::shared_ptr< SelfTest > > &tests)
Definition: selftestdialog.cpp:326
QSortFilterProxyModel
QItemSelection
Kleo::Dialogs::SelfTestDialog::setRunAtStartUp
void setRunAtStartUp(bool run)
Definition: selftestdialog.cpp:331
selftestdialog.h
Ok
Definition: setinitialpindialog.cpp:61
Kleo::Dialogs::SelfTestDialog
Definition: selftestdialog.h:52
q
#define q
Definition: adduseridcommand.cpp:90
QModelIndex::column
int column() const
QDialog
QPushButton
Qt::WindowFlags
typedef WindowFlags
selftest.h
QItemSelectionModel
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