File indexing completed on 2021-12-21 14:36:55

0001 /*
0002     SPDX-FileCopyrightText: 2002 Jean-Baptiste Mardelle <bj@altern.org>
0003     SPDX-FileCopyrightText: 2007 Jimmy Gilles <jimmygilles@gmail.com>
0004     SPDX-FileCopyrightText: 2008 Rolf Eike Beer <kde@opensource.sf-tec.de>
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "selectexpirydate.h"
0009 
0010 #include <QCheckBox>
0011 #include <QVBoxLayout>
0012 
0013 #include <KConfigGroup>
0014 #include <KDatePicker>
0015 #include <KLocalizedString>
0016 
0017 #include <QDialogButtonBox>
0018 #include <QPushButton>
0019 
0020 SelectExpiryDate::SelectExpiryDate(QWidget* parent, QDateTime date)
0021     : QDialog(parent)
0022 {
0023     setWindowTitle(i18n("Choose New Expiration"));
0024     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
0025     QWidget *mainWidget = new QWidget(this);
0026     QVBoxLayout *mainLayout = new QVBoxLayout(this);
0027     setLayout(mainLayout);
0028     mainLayout->addWidget(mainWidget);
0029     okButton = buttonBox->button(QDialogButtonBox::Ok);
0030     okButton->setDefault(true);
0031     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0032     connect(buttonBox, &QDialogButtonBox::accepted, this, &SelectExpiryDate::accept);
0033     connect(buttonBox, &QDialogButtonBox::rejected, this, &SelectExpiryDate::reject);
0034     okButton->setDefault(true);
0035 
0036     QWidget *page = new QWidget(this);
0037     m_unlimited = new QCheckBox(i18nc("Key has unlimited lifetime", "Unlimited"), page);
0038     m_unlimited->setChecked(date.isNull());
0039 
0040     if (date.isNull())
0041         date = QDateTime::currentDateTime();
0042 
0043     m_datepicker = new KDatePicker(date.date(), page);
0044     if (date.isNull()) {
0045         m_datepicker->setEnabled(false);
0046         m_unlimited->setChecked(true);
0047     }
0048 
0049     QVBoxLayout *layout = new QVBoxLayout(page);
0050     layout->setSpacing(3);
0051     layout->addWidget(m_datepicker);
0052     layout->addWidget(m_unlimited);
0053 
0054     connect(m_unlimited, &QCheckBox::toggled, this, &SelectExpiryDate::slotEnableDate);
0055     connect(m_datepicker, &KDatePicker::dateChanged, this, &SelectExpiryDate::slotCheckDate);
0056     connect(m_datepicker, &KDatePicker::dateEntered, this, &SelectExpiryDate::slotCheckDate);
0057 
0058     mainLayout->addWidget(page);
0059     mainLayout->addWidget(buttonBox);
0060     show();
0061 
0062     slotEnableDate(m_unlimited->isChecked());
0063 }
0064 
0065 QDateTime SelectExpiryDate::date() const
0066 {
0067     if (m_unlimited->isChecked())
0068         return QDateTime();
0069     else
0070         return QDateTime(m_datepicker->date());
0071 }
0072 
0073 void SelectExpiryDate::slotCheckDate(const QDate& date)
0074 {
0075     okButton->setEnabled(QDateTime(date) >= QDateTime::currentDateTime());
0076 }
0077 
0078 void SelectExpiryDate::slotEnableDate(const bool ison)
0079 {
0080     m_datepicker->setEnabled(!ison);
0081     if (ison)
0082         okButton->setEnabled(true);
0083     else
0084         slotCheckDate(m_datepicker->date());
0085 }