• 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
expirydialog.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  dialogs/expirydialog.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 "expirydialog.h"
36 
37 #include "ui_expirydialog.h"
38 
39 #include <QDate>
40 
41 #include <KDebug>
42 
43 #include <cassert>
44 
45 using namespace Kleo;
46 using namespace Kleo::Dialogs;
47 
48 enum Period {
49  Days,
50  Weeks,
51  Months,
52  Years,
53 
54  NumPeriods
55 };
56 
57 
58 
59 static QDate date_by_amount_and_unit( int inAmount, int inUnit ) {
60  const QDate current = QDate::currentDate();
61  switch ( inUnit ) {
62  case Days: return current.addDays( inAmount ); break;
63  case Weeks: return current.addDays( 7*inAmount ); break;
64  case Months: return current.addMonths( inAmount ); break;
65  case Years: return current.addYears( inAmount ); break;
66  default:
67  assert( !"Should not reach here" );
68  }
69  return QDate();
70 }
71 
72 // these calculations should be precise enough for the forseeable future...
73 static const double DAYS_IN_GREGORIAN_YEAR = 365.2425;
74 
75 static int monthsBetween( const QDate & d1, const QDate & d2 ) {
76  const int days = d1.daysTo( d2 );
77  return qRound( days / DAYS_IN_GREGORIAN_YEAR * 12 );
78 }
79 
80 static int yearsBetween( const QDate & d1, const QDate & d2 ) {
81  const int days = d1.daysTo( d2 );
82  return qRound( days / DAYS_IN_GREGORIAN_YEAR );
83 }
84 
85 
86 
87 
88 class ExpiryDialog::Private {
89  friend class ::Kleo::Dialogs::ExpiryDialog;
90  ExpiryDialog * const q;
91 public:
92  explicit Private( ExpiryDialog * qq )
93  : q( qq ),
94  inUnit( Days ),
95  ui( q )
96  {
97  connect( ui.inSB, SIGNAL(valueChanged(int)),
98  q, SLOT(slotInAmountChanged()) );
99  connect( ui.inCB, SIGNAL(currentIndexChanged(int)),
100  q, SLOT(slotInUnitChanged()) );
101  connect( ui.onCW, SIGNAL(selectionChanged()),
102  q, SLOT(slotOnDateChanged()) );
103 
104  assert( ui.inCB->currentIndex() == inUnit );
105  }
106 
107 private:
108  void slotInAmountChanged();
109  void slotInUnitChanged();
110  void slotOnDateChanged();
111 
112 private:
113  QDate inDate() const;
114  int inAmountByDate( const QDate & date ) const;
115 
116 private:
117  int inUnit;
118 
119  struct UI : public Ui::ExpiryDialog {
120  explicit UI( Dialogs::ExpiryDialog * qq )
121  : Ui::ExpiryDialog()
122  {
123  setupUi( qq->mainWidget() );
124  qq->setButtons( KDialog::Ok | KDialog::Cancel );
125 
126  assert( inCB->count() == NumPeriods );
127 
128  onCW->setMinimumDate( QDate::currentDate().addDays( 1 ) );
129  }
130  } ui;
131 };
132 
133 ExpiryDialog::ExpiryDialog( QWidget * p, Qt::WindowFlags f )
134  : KDialog( p, f ), d( new Private( this ) )
135 {
136 
137 }
138 
139 ExpiryDialog::~ExpiryDialog() {}
140 
141 
142 void ExpiryDialog::setDateOfExpiry( const QDate & date ) {
143  const QDate current = QDate::currentDate();
144  if ( date.isValid() ) {
145  d->ui.onRB->setChecked( true );
146  d->ui.onCW->setSelectedDate( qMax( date, current ) );
147  } else {
148  d->ui.neverRB->setChecked( true );
149  d->ui.onCW->setSelectedDate( current );
150  d->ui.inSB->setValue( 0 );
151  }
152 }
153 
154 QDate ExpiryDialog::dateOfExpiry() const {
155  return
156  d->ui.inRB->isChecked() ? d->inDate() :
157  d->ui.onRB->isChecked() ? d->ui.onCW->selectedDate() :
158  QDate() ;
159 }
160 
161 
162 
163 
164 void ExpiryDialog::Private::slotInUnitChanged() {
165  const int oldInAmount = ui.inSB->value();
166  const QDate targetDate = date_by_amount_and_unit( oldInAmount, inUnit );
167  inUnit = ui.inCB->currentIndex();
168  if ( targetDate.isValid() )
169  ui.inSB->setValue( inAmountByDate( targetDate ) );
170  else
171  slotInAmountChanged();
172 }
173 
174 void ExpiryDialog::Private::slotInAmountChanged() {
175  // Only modify onCW when onCW is slave:
176  if ( ui.inRB->isChecked() )
177  ui.onCW->setSelectedDate( inDate() );
178 }
179 
180 void ExpiryDialog::Private::slotOnDateChanged() {
181  // Only modify inSB/inCB when onCW is master:
182  if ( ui.onRB->isChecked() )
183  ui.inSB->setValue( inAmountByDate( ui.onCW->selectedDate() ) );
184 }
185 
186 
187 
188 
189 QDate ExpiryDialog::Private::inDate() const {
190  return date_by_amount_and_unit( ui.inSB->value(), ui.inCB->currentIndex() );
191 }
192 
193 int ExpiryDialog::Private::inAmountByDate( const QDate & selected ) const {
194  const QDate current = QDate::currentDate();
195 
196  switch ( ui.inCB->currentIndex() ) {
197  case Days: return current.daysTo( selected );
198  case Weeks: return qRound( current.daysTo( selected ) / 7.0 );
199  case Months: return monthsBetween( current, selected );
200  case Years: return yearsBetween( current, selected );
201  };
202  assert( !"Should not reach here" );
203  return -1;
204 }
205 
206 #include "moc_expirydialog.cpp"
QWidget
Kleo::Dialogs::ExpiryDialog::~ExpiryDialog
~ExpiryDialog()
Definition: expirydialog.cpp:139
KDialog
d
#define d
Definition: adduseridcommand.cpp:90
DAYS_IN_GREGORIAN_YEAR
static const double DAYS_IN_GREGORIAN_YEAR
Definition: expirydialog.cpp:73
Kleo::Dialogs::ExpiryDialog::setDateOfExpiry
void setDateOfExpiry(const QDate &date)
Definition: expirydialog.cpp:142
Years
Definition: expirydialog.cpp:52
Months
Definition: expirydialog.cpp:51
NumPeriods
Definition: expirydialog.cpp:54
Kleo::Dialogs::ExpiryDialog::ExpiryDialog
ExpiryDialog(QWidget *parent=0, Qt::WindowFlags f=0)
Definition: expirydialog.cpp:133
Weeks
Definition: expirydialog.cpp:50
expirydialog.h
Kleo::Dialogs::ExpiryDialog
Definition: expirydialog.h:45
Ok
Definition: setinitialpindialog.cpp:59
Period
Period
Definition: expirydialog.cpp:48
q
#define q
Definition: adduseridcommand.cpp:91
Days
Definition: expirydialog.cpp:49
Kleo::Dialogs::ExpiryDialog::dateOfExpiry
QDate dateOfExpiry() const
yearsBetween
static int yearsBetween(const QDate &d1, const QDate &d2)
Definition: expirydialog.cpp:80
date_by_amount_and_unit
static QDate date_by_amount_and_unit(int inAmount, int inUnit)
Definition: expirydialog.cpp:59
monthsBetween
static int monthsBetween(const QDate &d1, const QDate &d2)
Definition: expirydialog.cpp:75
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:41 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