Page MenuHome GnuPG

No OneTemporary

diff --git a/src/utils/expiration.cpp b/src/utils/expiration.cpp
index 27f9ea4d..af86d04e 100644
--- a/src/utils/expiration.cpp
+++ b/src/utils/expiration.cpp
@@ -1,142 +1,155 @@
/* -*- mode: c++; c-basic-offset:4 -*-
This file is part of libkleopatra
SPDX-FileCopyrightText: 2023 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "expiration.h"
#include "openpgpcertificatecreationconfig.h"
#include <KConfigGroup>
#include <KDateComboBox>
#include <KLocalizedString>
#include <KSharedConfig>
#include <qlatin1stringview.h>
QDate Kleo::Expiration::maximumAllowedDate()
{
static const QDate maxAllowedDate{2106, 2, 5};
return maxAllowedDate;
}
QDate Kleo::Expiration::minimumExpirationDate()
{
return expirationDateRange().minimum;
}
QDate Kleo::Expiration::maximumExpirationDate()
{
return expirationDateRange().maximum;
}
Kleo::Expiration::DateRange Kleo::Expiration::expirationDateRange()
{
Kleo::Expiration::DateRange range;
const auto settings = Kleo::OpenPGPCertificateCreationConfig{};
const auto today = QDate::currentDate();
const auto minimumExpiry = std::max(1, settings.validityPeriodInDaysMin());
range.minimum = std::min(today.addDays(minimumExpiry), maximumAllowedDate());
const auto maximumExpiry = settings.validityPeriodInDaysMax();
if (maximumExpiry >= 0) {
range.maximum = std::min(std::max(today.addDays(maximumExpiry), range.minimum), maximumAllowedDate());
}
return range;
}
QDate Kleo::Expiration::defaultExpirationDate(Kleo::Expiration::ExpirationOnUnlimitedValidity onUnlimitedValidity)
{
QDate expirationDate;
const auto settings = Kleo::OpenPGPCertificateCreationConfig{};
const auto defaultExpirationInDays = settings.validityPeriodInDays();
if (defaultExpirationInDays > 0) {
expirationDate = QDate::currentDate().addDays(defaultExpirationInDays);
} else if (defaultExpirationInDays < 0 || onUnlimitedValidity == ExpirationOnUnlimitedValidity::InternalDefaultExpiration) {
expirationDate = QDate::currentDate().addYears(3);
}
const auto allowedRange = expirationDateRange();
expirationDate = std::max(expirationDate, allowedRange.minimum);
if (allowedRange.maximum.isValid()) {
expirationDate = std::min(expirationDate, allowedRange.maximum);
}
return expirationDate;
}
bool Kleo::Expiration::isValidExpirationDate(const QDate &date)
{
const auto allowedRange = expirationDateRange();
if (date.isValid()) {
return (date >= allowedRange.minimum //
&& ((allowedRange.maximum.isValid() && date <= allowedRange.maximum) //
|| (!allowedRange.maximum.isValid() && date <= maximumAllowedDate())));
} else {
return !allowedRange.maximum.isValid();
}
}
-static QString dateToString(const QDate &date, QWidget *widget)
+static QString dateToString(const QDate &date, QWidget *widget = nullptr)
{
// workaround for QLocale using "yy" way too often for years
// stolen from KDateComboBox
auto locale = widget ? widget->locale() : QLocale{};
const auto dateFormat = (locale
.dateFormat(QLocale::ShortFormat) //
.replace(QLatin1StringView{"yy"}, QLatin1StringView{"yyyy"})
.replace(QLatin1StringView{"yyyyyyyy"}, QLatin1StringView{"yyyy"}));
return locale.toString(date, dateFormat);
}
+QString Kleo::Expiration::validUntilLabel()
+{
+ const auto dateRange = expirationDateRange();
+ if (dateRange.minimum == dateRange.maximum) {
+ return i18nc("@label Valid until (<a date>):", "Valid until (%1):", dateToString(dateRange.minimum));
+ } else {
+ return i18nc("@label ... (between <a date> and <another date>):",
+ "Valid until (between %1 and %2):",
+ dateToString(dateRange.minimum),
+ dateToString(dateRange.maximum.isValid() ? dateRange.maximum : Expiration::maximumAllowedDate()));
+ }
+}
+
static QString validityPeriodHint(const Kleo::Expiration::DateRange &dateRange, QWidget *widget)
{
// the minimum date is always valid
if (dateRange.maximum.isValid()) {
if (dateRange.maximum == dateRange.minimum) {
return i18nc("@info", "The date cannot be changed.");
} else {
return i18nc("@info ... between <a date> and <another date>.",
"Enter a date between %1 and %2.",
dateToString(dateRange.minimum, widget),
dateToString(dateRange.maximum, widget));
}
} else {
return i18nc("@info ... between <a date> and <another date>.",
"Enter a date between %1 and %2.",
dateToString(dateRange.minimum, widget),
dateToString(Kleo::Expiration::maximumAllowedDate(), widget));
}
}
QString Kleo::Expiration::validityPeriodHint()
{
return ::validityPeriodHint(expirationDateRange(), nullptr);
}
void Kleo::Expiration::setUpExpirationDateComboBox(KDateComboBox *dateCB, const Kleo::Expiration::DateRange &range)
{
const auto dateRange = range.minimum.isValid() ? range : expirationDateRange();
// enable warning on invalid or not allowed dates
dateCB->setOptions(KDateComboBox::EditDate | KDateComboBox::SelectDate | KDateComboBox::DatePicker | KDateComboBox::DateKeywords
| KDateComboBox::WarnOnInvalid);
const auto hintAndErrorMessage = validityPeriodHint(dateRange, dateCB);
dateCB->setDateRange(dateRange.minimum, dateRange.maximum.isValid() ? dateRange.maximum : maximumAllowedDate(), hintAndErrorMessage, hintAndErrorMessage);
if (dateRange.minimum == dateRange.maximum) {
// only one date is allowed, so that changing it no sense
dateCB->setEnabled(false);
}
dateCB->setToolTip(hintAndErrorMessage);
const QDate today = QDate::currentDate();
dateCB->setDateMap({
{today.addYears(3), i18nc("@item:inlistbox", "Three years from now")},
{today.addYears(2), i18nc("@item:inlistbox", "Two years from now")},
{today.addYears(1), i18nc("@item:inlistbox", "One year from now")},
});
}
diff --git a/src/utils/expiration.h b/src/utils/expiration.h
index c7974e94..486e5e7e 100644
--- a/src/utils/expiration.h
+++ b/src/utils/expiration.h
@@ -1,105 +1,114 @@
/* -*- mode: c++; c-basic-offset:4 -*-
utils/expiration.h
This file is part of Libkleo
SPDX-FileCopyrightText: 2023 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include "kleo_export.h"
#include <QDate>
class KDateComboBox;
namespace Kleo
{
namespace Expiration
{
struct KLEO_EXPORT DateRange {
QDate minimum;
QDate maximum;
};
/**
* Returns a date a bit before the technically possible latest expiration
* date (~2106-02-07) that is safe to use as latest expiration date.
*/
KLEO_EXPORT QDate maximumAllowedDate();
/**
* Returns the earliest allowed expiration date.
*
* This is either tomorrow or the configured number of days after today
* (whichever is later).
*
* \sa OpenPGPCertificateCreationConfig::validityPeriodInDaysMin
*/
KLEO_EXPORT QDate minimumExpirationDate();
/**
* Returns the latest allowed expiration date.
*
* If unlimited validity is allowed, then an invalid date is returned.
* Otherwise, either the configured number of days after today or
* the maximum allowed date, whichever is earlier, is returned.
* Additionally, the returned date is never earlier than the minimum
* expiration date.
*
* \sa OpenPGPCertificateCreationConfig::validityPeriodInDaysMax
*/
KLEO_EXPORT QDate maximumExpirationDate();
/**
* Returns the allowed range for the expiration date.
*
* \sa minimumExpirationDate, maximumExpirationDate
*/
KLEO_EXPORT DateRange expirationDateRange();
enum class KLEO_EXPORT ExpirationOnUnlimitedValidity {
NoExpiration,
InternalDefaultExpiration,
};
/**
* Returns a useful value for the default expiration date based on the current
* date and the configured default validity. If the configured validity is
* unlimited, then the return value depends on \p onUnlimitedValidity.
*
* The returned value is always in the allowed range for the expiration date.
*
* \sa expirationDateRange
*/
KLEO_EXPORT QDate defaultExpirationDate(ExpirationOnUnlimitedValidity onUnlimitedValidity);
/**
* Returns true, if \p date is a valid expiration date.
*/
KLEO_EXPORT bool isValidExpirationDate(const QDate &date);
/**
- * Returns a hint which dates are valid expiration dates for the date
- * combo box \p dateCB.
+ * Returns a text which can be used as label for a date combo box.
+ *
+ * If the allowed range for the expiration date is not empty then the text
+ * "Valid until (between MIN_DATE and MAX_DATE):" is returned. Otherwise,
+ * "Valid until (MIN_DATE):" is returned.
+ */
+KLEO_EXPORT QString validUntilLabel();
+
+/**
+ * Returns a hint which dates are valid expiration dates for a date
+ * combo box.
* The hint can be used as tool tip or as error message when the user
* entered an invalid date.
*/
KLEO_EXPORT QString validityPeriodHint();
/**
* Configures the date combo box \p dateCB for choosing an expiration date.
*
* Sets the allowed date range to the \p dateRange, or to the configured
* validity period range if the minimum date is invalid. If the maximum
* date is invalid, then the maximumAllowedDate is set as maximum.
* Also sets a tooltip and a few fixed values to choose from, enables
* warnings on invalid or not allowed dates, and disables the combo box if
* the date range spans a single day.
*/
KLEO_EXPORT void setUpExpirationDateComboBox(KDateComboBox *dateCB, const Kleo::Expiration::DateRange &dateRange = {});
}
}

File Metadata

Mime Type
text/x-diff
Expires
Tue, Sep 16, 12:57 AM (7 h, 18 m)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
07/d4/a5b37ad20e0dbe5e0cc53edf93fb

Event Timeline