Libkleo

expiration.cpp
1/* -*- mode: c++; c-basic-offset:4 -*-
2 This file is part of libkleopatra
3
4 SPDX-FileCopyrightText: 2023 g10 Code GmbH
5 SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
6
7 SPDX-License-Identifier: GPL-2.0-or-later
8*/
9
10#include "expiration.h"
11#include "openpgpcertificatecreationconfig.h"
12
13#include <KConfigGroup>
14#include <KDateComboBox>
15#include <KLocalizedString>
16#include <KSharedConfig>
17#include <qlatin1stringview.h>
18
19QDate Kleo::Expiration::maximumAllowedDate()
20{
21 static const QDate maxAllowedDate{2106, 2, 5};
22 return maxAllowedDate;
23}
24
25QDate Kleo::Expiration::minimumExpirationDate()
26{
27 return expirationDateRange().minimum;
28}
29
30QDate Kleo::Expiration::maximumExpirationDate()
31{
32 return expirationDateRange().maximum;
33}
34
35Kleo::Expiration::DateRange Kleo::Expiration::expirationDateRange()
36{
37 Kleo::Expiration::DateRange range;
38
39 const auto settings = Kleo::OpenPGPCertificateCreationConfig{};
40 const auto today = QDate::currentDate();
41
42 const auto minimumExpiry = std::max(1, settings.validityPeriodInDaysMin());
43 range.minimum = std::min(today.addDays(minimumExpiry), maximumAllowedDate());
44
45 const auto maximumExpiry = settings.validityPeriodInDaysMax();
46 if (maximumExpiry >= 0) {
47 range.maximum = std::min(std::max(today.addDays(maximumExpiry), range.minimum), maximumAllowedDate());
48 }
49
50 return range;
51}
52
53QDate Kleo::Expiration::defaultExpirationDate(Kleo::Expiration::ExpirationOnUnlimitedValidity onUnlimitedValidity)
54{
55 QDate expirationDate;
56
57 const auto settings = Kleo::OpenPGPCertificateCreationConfig{};
58 const auto defaultExpirationInDays = settings.validityPeriodInDays();
59 if (defaultExpirationInDays > 0) {
60 expirationDate = QDate::currentDate().addDays(defaultExpirationInDays);
61 } else if (defaultExpirationInDays < 0 || onUnlimitedValidity == ExpirationOnUnlimitedValidity::InternalDefaultExpiration) {
62 expirationDate = QDate::currentDate().addYears(3);
63 }
64
65 const auto allowedRange = expirationDateRange();
66 expirationDate = std::max(expirationDate, allowedRange.minimum);
67 if (allowedRange.maximum.isValid()) {
68 expirationDate = std::min(expirationDate, allowedRange.maximum);
69 }
70
71 return expirationDate;
72}
73
74bool Kleo::Expiration::isValidExpirationDate(const QDate &date)
75{
76 const auto allowedRange = expirationDateRange();
77 if (date.isValid()) {
78 return (date >= allowedRange.minimum //
79 && ((allowedRange.maximum.isValid() && date <= allowedRange.maximum) //
80 || (!allowedRange.maximum.isValid() && date <= maximumAllowedDate())));
81 } else {
82 return !allowedRange.maximum.isValid();
83 }
84}
85
86static QString dateToString(const QDate &date, QWidget *widget = nullptr)
87{
88 // workaround for QLocale using "yy" way too often for years
89 // stolen from KDateComboBox
90 auto locale = widget ? widget->locale() : QLocale{};
91 const auto dateFormat = (locale
92 .dateFormat(QLocale::ShortFormat) //
94 .replace(QLatin1StringView{"yyyyyyyy"}, QLatin1StringView{"yyyy"}));
95 return locale.toString(date, dateFormat);
96}
97
98QString Kleo::Expiration::validUntilLabel()
99{
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));
103 } else {
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()));
108 }
109}
110
111static QString validityPeriodHint(const Kleo::Expiration::DateRange &dateRange, QWidget *widget)
112{
113 // the minimum date is always valid
114 if (dateRange.maximum.isValid()) {
115 if (dateRange.maximum == dateRange.minimum) {
116 return i18nc("@info", "The date cannot be changed.");
117 } else {
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));
122 }
123 } else {
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));
128 }
129}
130
131QString Kleo::Expiration::validityPeriodHint()
132{
133 return ::validityPeriodHint(expirationDateRange(), nullptr);
134}
135
136void Kleo::Expiration::setUpExpirationDateComboBox(KDateComboBox *dateCB, const Kleo::Expiration::DateRange &range)
137{
138 const auto dateRange = range.minimum.isValid() ? range : expirationDateRange();
139 // enable warning on invalid or not allowed dates
142 const auto hintAndErrorMessage = validityPeriodHint(dateRange, dateCB);
143 dateCB->setDateRange(dateRange.minimum, dateRange.maximum.isValid() ? dateRange.maximum : maximumAllowedDate(), hintAndErrorMessage, hintAndErrorMessage);
144 dateCB->setToolTip(hintAndErrorMessage);
145 const QDate today = QDate::currentDate();
146 dateCB->setDateMap({
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")},
150 });
151}
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...)
KCALUTILS_EXPORT QString dateToString(QDate date, bool shortfmt=true)
QAction * replace(const QObject *recvr, const char *slot, QObject *parent)
QDate addDays(qint64 ndays) const const
QDate addYears(int nyears) const const
QDate currentDate()
bool isValid(int year, int month, int day)
QString toString(QDate date, FormatType format) const const
void setToolTip(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:29:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.