10#include "expiration.h"
11#include "openpgpcertificatecreationconfig.h"
13#include <KConfigGroup>
14#include <KDateComboBox>
15#include <KLocalizedString>
16#include <KSharedConfig>
17#include <qlatin1stringview.h>
19QDate Kleo::Expiration::maximumAllowedDate()
21 static const QDate maxAllowedDate{2106, 2, 5};
22 return maxAllowedDate;
25QDate Kleo::Expiration::minimumExpirationDate()
27 return expirationDateRange().minimum;
30QDate Kleo::Expiration::maximumExpirationDate()
32 return expirationDateRange().maximum;
35Kleo::Expiration::DateRange Kleo::Expiration::expirationDateRange()
37 Kleo::Expiration::DateRange range;
39 const auto settings = Kleo::OpenPGPCertificateCreationConfig{};
42 const auto minimumExpiry = std::max(1, settings.validityPeriodInDaysMin());
43 range.minimum = std::min(today.addDays(minimumExpiry), maximumAllowedDate());
45 const auto maximumExpiry = settings.validityPeriodInDaysMax();
46 if (maximumExpiry >= 0) {
47 range.maximum = std::min(std::max(today.addDays(maximumExpiry), range.minimum), maximumAllowedDate());
53QDate Kleo::Expiration::defaultExpirationDate(Kleo::Expiration::ExpirationOnUnlimitedValidity onUnlimitedValidity)
57 const auto settings = Kleo::OpenPGPCertificateCreationConfig{};
58 const auto defaultExpirationInDays = settings.validityPeriodInDays();
59 if (defaultExpirationInDays > 0) {
61 }
else if (defaultExpirationInDays < 0 || onUnlimitedValidity == ExpirationOnUnlimitedValidity::InternalDefaultExpiration) {
65 const auto allowedRange = expirationDateRange();
66 expirationDate = std::max(expirationDate, allowedRange.minimum);
67 if (allowedRange.maximum.isValid()) {
68 expirationDate = std::min(expirationDate, allowedRange.maximum);
71 return expirationDate;
74bool Kleo::Expiration::isValidExpirationDate(
const QDate &date)
76 const auto allowedRange = expirationDateRange();
78 return (date >= allowedRange.minimum
79 && ((allowedRange.maximum.isValid() && date <= allowedRange.maximum)
80 || (!allowedRange.maximum.isValid() && date <= maximumAllowedDate())));
82 return !allowedRange.maximum.isValid();
90 auto locale = widget ? widget->locale() :
QLocale{};
91 const auto dateFormat = (locale
95 return locale.
toString(date, dateFormat);
98QString Kleo::Expiration::validUntilLabel()
100 const auto dateRange = expirationDateRange();
101 if (dateRange.minimum == dateRange.maximum) {
102 return i18nc(
"@label Valid until (<a date>):",
"Valid until (%1):", dateToString(dateRange.minimum));
104 return i18nc(
"@label ... (between <a date> and <another date>):",
105 "Valid until (between %1 and %2):",
106 dateToString(dateRange.minimum),
107 dateToString(dateRange.maximum.isValid() ? dateRange.maximum : Expiration::maximumAllowedDate()));
111static QString validityPeriodHint(
const Kleo::Expiration::DateRange &dateRange,
QWidget *widget)
114 if (dateRange.maximum.
isValid()) {
115 if (dateRange.maximum == dateRange.minimum) {
116 return i18nc(
"@info",
"The date cannot be changed.");
118 return i18nc(
"@info ... between <a date> and <another date>.",
119 "Enter a date between %1 and %2.",
120 dateToString(dateRange.minimum, widget),
121 dateToString(dateRange.maximum, widget));
124 return i18nc(
"@info ... between <a date> and <another date>.",
125 "Enter a date between %1 and %2.",
126 dateToString(dateRange.minimum, widget),
127 dateToString(Kleo::Expiration::maximumAllowedDate(), widget));
131QString Kleo::Expiration::validityPeriodHint()
133 return ::validityPeriodHint(expirationDateRange(),
nullptr);
136void Kleo::Expiration::setUpExpirationDateComboBox(
KDateComboBox *dateCB,
const Kleo::Expiration::DateRange &range)
138 const auto dateRange = range.minimum.
isValid() ? range : expirationDateRange();
142 const auto hintAndErrorMessage = validityPeriodHint(dateRange, dateCB);
143 dateCB->
setDateRange(dateRange.minimum, dateRange.maximum.
isValid() ? dateRange.maximum : maximumAllowedDate(), hintAndErrorMessage, hintAndErrorMessage);
147 {today.
addYears(3),
i18nc(
"@item:inlistbox",
"Three years from now")},
148 {today.
addYears(2),
i18nc(
"@item:inlistbox",
"Two years from now")},
149 {today.
addYears(1),
i18nc(
"@item:inlistbox",
"One year from now")},
void setOptions(Options options)
void setDateMap(QMap< QDate, QString > dateMap)
void setDateRange(const QDate &minDate, const QDate &maxDate, const QString &minWarnMsg=QString(), const QString &maxWarnMsg=QString())
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QAction * replace(const QObject *recvr, const char *slot, QObject *parent)
QDate addDays(qint64 ndays) const const
QDate addYears(int nyears) const const
bool isValid(int year, int month, int day)
QString toString(QDate date, FormatType format) const const