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