Warning, file /pim/kalarm/src/wakedlg.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  *  wakedlg.cpp  -  dialog to configure wake-from-suspend alarms
0003  *  Program:  kalarm
0004  *  SPDX-FileCopyrightText: 2011-2022 David Jarvie <djarvie@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include "wakedlg.h"
0010 #include "ui_wakedlg.h"
0011 
0012 #include "functions.h"
0013 #include "kalarmapp.h"
0014 #include "mainwindow.h"
0015 #include "resourcescalendar.h"
0016 #include "lib/messagebox.h"
0017 #include "kalarmcalendar/kaevent.h"
0018 
0019 #include <KLocalizedString>
0020 #include <KSharedConfig>
0021 #include <KConfigGroup>
0022 
0023 #include <QTimer>
0024 
0025 using namespace KAlarmCal;
0026 
0027 WakeFromSuspendDlg* WakeFromSuspendDlg::mInstance = nullptr;
0028 
0029 WakeFromSuspendDlg* WakeFromSuspendDlg::create(QWidget* parent)
0030 {
0031     if (!mInstance)
0032         mInstance = new WakeFromSuspendDlg(parent);
0033     return mInstance;
0034 }
0035 
0036 WakeFromSuspendDlg::WakeFromSuspendDlg(QWidget* parent)
0037     : QDialog(parent)
0038 {
0039     setAttribute(Qt::WA_DeleteOnClose);
0040     setWindowTitle(i18nc("@title:window", "Wake From Suspend"));
0041 
0042 
0043     mUi = new Ui_WakeFromSuspendDlgWidget;
0044     mUi->setupUi(this);
0045     mUi->advanceWakeTime->setValue(Preferences::wakeFromSuspendAdvance());
0046 
0047     mMainWindow = qobject_cast<MainWindow*>(parent);
0048     if (!mMainWindow)
0049         mMainWindow = MainWindow::mainMainWindow();
0050 
0051     // Check if there is any alarm selected in the main window, and enable/disable
0052     // the Show and Cancel buttons as necessary.
0053     enableDisableUseButton();
0054 
0055     // Update the Show and Cancel button status every 5 seconds
0056     mTimer = new QTimer(this);
0057     connect(mTimer, &QTimer::timeout, this, &WakeFromSuspendDlg::checkPendingAlarm);
0058     mTimer->start(5000);
0059 
0060     connect(mMainWindow, &MainWindow::selectionChanged, this, &WakeFromSuspendDlg::enableDisableUseButton);
0061     connect(mUi->showWakeButton, &QPushButton::clicked, this, &WakeFromSuspendDlg::showWakeClicked);
0062     connect(mUi->useWakeButton, &QPushButton::clicked, this, &WakeFromSuspendDlg::useWakeClicked);
0063     connect(mUi->cancelWakeButton, &QPushButton::clicked, this, &WakeFromSuspendDlg::cancelWakeClicked);
0064     connect(mUi->buttonBox, &QDialogButtonBox::rejected, this, &WakeFromSuspendDlg::close);
0065 
0066     connect(theApp(), &KAlarmApp::alarmEnabledToggled, this, &WakeFromSuspendDlg::enableDisableUseButton);
0067 }
0068 
0069 WakeFromSuspendDlg::~WakeFromSuspendDlg()
0070 {
0071     if (mInstance == this)
0072         mInstance = nullptr;
0073     delete mUi;
0074 }
0075 
0076 /******************************************************************************
0077 * Called when the alarm selection in the main window changes.
0078 * Enable or disable the Use Highlighted Alarm button.
0079 */
0080 void WakeFromSuspendDlg::enableDisableUseButton()
0081 {
0082     bool enable = theApp()->alarmsEnabled();
0083     if (enable)
0084     {
0085         const QString wakeFromSuspendId = KAlarm::checkRtcWakeConfig().value(0);
0086         const KAEvent event = mMainWindow->selectedEvent();
0087         enable = event.isValid()
0088               && event.category() == CalEvent::ACTIVE
0089               && event.enabled()
0090               && !event.mainDateTime().isDateOnly()
0091               && event.id() != wakeFromSuspendId;
0092     }
0093     mUi->useWakeButton->setEnabled(enable);
0094     checkPendingAlarm();
0095 }
0096 
0097 /******************************************************************************
0098 * Update the Show and Cancel buttons if the pending alarm status has changed.
0099 * Reply = true if an alarm is still pending.
0100 */
0101 bool WakeFromSuspendDlg::checkPendingAlarm()
0102 {
0103     if (KAlarm::checkRtcWakeConfig(true).isEmpty())
0104     {
0105         mUi->showWakeButton->setEnabled(false);
0106         mUi->cancelWakeButton->setEnabled(false);
0107         return false;
0108     }
0109     return true;
0110 }
0111 
0112 /******************************************************************************
0113 * Called when the user clicks the Show Current Alarm button.
0114 * Highlight the currently scheduled wake-from-suspend alarm in the main window.
0115 */
0116 void WakeFromSuspendDlg::showWakeClicked()
0117 {
0118     if (checkPendingAlarm())
0119     {
0120         const QStringList params = KAlarm::checkRtcWakeConfig();
0121         if (!params.isEmpty())
0122         {
0123             const KAEvent event = ResourcesCalendar::event(EventId(params[0].toLongLong(), params[1]));
0124             if (event.isValid())
0125             {
0126                 mMainWindow->selectEvent(event.id());
0127                 return;
0128             }
0129         }
0130     }
0131     mMainWindow->clearSelection();
0132 }
0133 
0134 /******************************************************************************
0135 * Called when the user clicks the Use Highlighted Alarm button.
0136 * Schedules system wakeup for that alarm.
0137 */
0138 void WakeFromSuspendDlg::useWakeClicked()
0139 {
0140     const KAEvent event = mMainWindow->selectedEvent();
0141     if (!event.isValid())
0142         return;
0143     const KADateTime dt = event.mainDateTime().kDateTime();
0144     if (dt.isDateOnly())
0145     {
0146         KAMessageBox::error(this, i18nc("@info", "Cannot schedule wakeup time for a date-only alarm"));
0147         return;
0148     }
0149     if (KAMessageBox::warningContinueCancel(this,
0150                 xi18nc("@info", "<para>This wakeup will cancel any existing wakeup which has been set by KAlarm "
0151                                "or any other application, because your computer can only schedule a single wakeup time.</para>"
0152                                "<para><b>Note:</b> Wake From Suspend is not supported at all on some computers, especially older ones, "
0153                                "and some computers only support setting a wakeup time up to 24 hours ahead. "
0154                                "You may wish to set up a test alarm to check your system's capability.</para>"),
0155                 QString(), KStandardGuiItem::cont(), KStandardGuiItem::cancel(), QStringLiteral("wakeupWarning"))
0156             != KMessageBox::Continue)
0157         return;
0158     const int advance = mUi->advanceWakeTime->value();
0159     const qint64 triggerTime = dt.addSecs(-advance * 60).toSecsSinceEpoch();
0160     if (KAlarm::setRtcWakeTime(triggerTime, this))
0161     {
0162         const QStringList param{QString::number(event.resourceId()), event.id(), QString::number(triggerTime)};
0163         KConfigGroup config(KSharedConfig::openConfig(), QStringLiteral("General"));
0164         config.writeEntry("RtcWake", param);
0165         config.sync();
0166         Preferences::setWakeFromSuspendAdvance(static_cast<unsigned>(advance));
0167         close();
0168     }
0169 }
0170 
0171 /******************************************************************************
0172 * Called when the user clicks the Cancel Wake From Suspend button.
0173 * Cancels any currently scheduled system wakeup.
0174 */
0175 void WakeFromSuspendDlg::cancelWakeClicked()
0176 {
0177     KAlarm::setRtcWakeTime(0, this);
0178     KAlarm::deleteRtcWakeConfig();
0179     mUi->showWakeButton->setEnabled(false);
0180     mUi->cancelWakeButton->setEnabled(false);
0181     enableDisableUseButton();
0182 }
0183 
0184 #include "moc_wakedlg.cpp"
0185 
0186 // vim: et sw=4: