File indexing completed on 2024-04-21 05:50:49

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         QDateTime date;
0071         date.setDate(m_datepicker->date());
0072         return date;
0073     }
0074 }
0075 
0076 void SelectExpiryDate::slotCheckDate(const QDate& date)
0077 {
0078     okButton->setEnabled(date >= QDate::currentDate());
0079 }
0080 
0081 void SelectExpiryDate::slotEnableDate(const bool ison)
0082 {
0083     m_datepicker->setEnabled(!ison);
0084     if (ison)
0085         okButton->setEnabled(true);
0086     else
0087         slotCheckDate(m_datepicker->date());
0088 }