Rename method
[kdepim.git] / kalarm / wakedlg.cpp
blob2670b7f72d57869d642045a67931a0425ad3ef66
1 /*
2 * wakedlg.cpp - dialog to configure wake-from-suspend alarms
3 * Program: kalarm
4 * Copyright © 2011-2012 by David Jarvie <djarvie@kde.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "kalarm.h"
22 #include "wakedlg.h"
23 #include "ui_wakedlg.h"
25 #include "alarmcalendar.h"
26 #include "functions.h"
27 #include "kalarmapp.h"
28 #include "mainwindow.h"
29 #include "messagebox.h"
30 #include "preferences.h"
32 #include <kalarmcal/kaevent.h>
34 #include <KLocalizedString>
35 #include <KSharedConfig>
36 #include <kconfiggroup.h>
38 #include <QTimer>
39 #include "kalarm_debug.h"
41 using namespace KAlarmCal;
43 WakeFromSuspendDlg* WakeFromSuspendDlg::mInstance = Q_NULLPTR;
45 WakeFromSuspendDlg* WakeFromSuspendDlg::create(QWidget* parent)
47 if (!mInstance)
48 mInstance = new WakeFromSuspendDlg(parent);
49 return mInstance;
52 WakeFromSuspendDlg::WakeFromSuspendDlg(QWidget* parent)
53 : QDialog(parent)
55 setAttribute(Qt::WA_DeleteOnClose);
56 setWindowTitle(i18nc("@title:window", "Wake From Suspend"));
59 mUi = new Ui_WakeFromSuspendDlgWidget;
60 mUi->setupUi(this);
61 mUi->advanceWakeTime->setValue(Preferences::wakeFromSuspendAdvance());
63 mMainWindow = qobject_cast<MainWindow*>(parent);
64 if (!mMainWindow)
65 mMainWindow = MainWindow::mainMainWindow();
67 // Check if there is any alarm selected in the main window, and enable/disable
68 // the Show and Cancel buttons as necessary.
69 enableDisableUseButton();
71 // Update the Show and Cancel button status every 5 seconds
72 mTimer = new QTimer(this);
73 connect(mTimer, &QTimer::timeout, this, &WakeFromSuspendDlg::checkPendingAlarm);
74 mTimer->start(5000);
76 connect(mMainWindow, &MainWindow::selectionChanged, this, &WakeFromSuspendDlg::enableDisableUseButton);
77 connect(mUi->showWakeButton, &QPushButton::clicked, this, &WakeFromSuspendDlg::showWakeClicked);
78 connect(mUi->useWakeButton, &QPushButton::clicked, this, &WakeFromSuspendDlg::useWakeClicked);
79 connect(mUi->cancelWakeButton, &QPushButton::clicked, this, &WakeFromSuspendDlg::cancelWakeClicked);
80 connect(mUi->buttonBox, &QDialogButtonBox::rejected, this, &WakeFromSuspendDlg::close);
82 connect(theApp(), &KAlarmApp::alarmEnabledToggled, this, &WakeFromSuspendDlg::enableDisableUseButton);
85 WakeFromSuspendDlg::~WakeFromSuspendDlg()
87 if (mInstance == this)
88 mInstance = Q_NULLPTR;
89 delete mUi;
92 /******************************************************************************
93 * Called when the alarm selection in the main window changes.
94 * Enable or disable the Use Highlighted Alarm button.
96 void WakeFromSuspendDlg::enableDisableUseButton()
98 bool enable = theApp()->alarmsEnabled();
99 if (enable)
101 QString wakeFromSuspendId = KAlarm::checkRtcWakeConfig().value(0);
102 const KAEvent event = mMainWindow->selectedEvent();
103 enable = event.isValid()
104 && event.category() == CalEvent::ACTIVE
105 && event.enabled()
106 && !event.mainDateTime().isDateOnly()
107 && event.id() != wakeFromSuspendId;
109 mUi->useWakeButton->setEnabled(enable);
110 checkPendingAlarm();
113 /******************************************************************************
114 * Update the Show and Cancel buttons if the pending alarm status has changed.
115 * Reply = true if an alarm is still pending.
117 bool WakeFromSuspendDlg::checkPendingAlarm()
119 if (KAlarm::checkRtcWakeConfig(true).isEmpty())
121 mUi->showWakeButton->setEnabled(false);
122 mUi->cancelWakeButton->setEnabled(false);
123 return false;
125 return true;
128 /******************************************************************************
129 * Called when the user clicks the Show Current Alarm button.
130 * Highlight the currently scheduled wake-from-suspend alarm in the main window.
132 void WakeFromSuspendDlg::showWakeClicked()
134 if (checkPendingAlarm())
136 QStringList params = KAlarm::checkRtcWakeConfig();
137 if (!params.isEmpty())
139 KAEvent* event = AlarmCalendar::resources()->event(EventId(params[0].toLongLong(), params[1]));
140 if (event)
142 mMainWindow->selectEvent(event->itemId());
143 return;
147 mMainWindow->clearSelection();
150 /******************************************************************************
151 * Called when the user clicks the Use Highlighted Alarm button.
152 * Schedules system wakeup for that alarm.
154 void WakeFromSuspendDlg::useWakeClicked()
156 KAEvent event = mMainWindow->selectedEvent();
157 if (!event.isValid())
158 return;
159 KDateTime dt = event.mainDateTime().kDateTime();
160 if (dt.isDateOnly())
162 KAMessageBox::sorry(this, i18nc("@info", "Cannot schedule wakeup time for a date-only alarm"));
163 return;
165 if (KAMessageBox::warningContinueCancel(this,
166 xi18nc("@info", "<para>This wakeup will cancel any existing wakeup which has been set by KAlarm "
167 "or any other application, because your computer can only schedule a single wakeup time.</para>"
168 "<para><b>Note:</b> Wake From Suspend is not supported at all on some computers, especially older ones, "
169 "and some computers only support setting a wakeup time up to 24 hours ahead. "
170 "You may wish to set up a test alarm to check your system's capability.</para>"),
171 QString(), KStandardGuiItem::cont(), KStandardGuiItem::cancel(), QStringLiteral("wakeupWarning"))
172 != KMessageBox::Continue)
173 return;
174 int advance = mUi->advanceWakeTime->value();
175 unsigned triggerTime = dt.addSecs(-advance * 60).toTime_t();
176 if (KAlarm::setRtcWakeTime(triggerTime, this))
178 QStringList param;
179 param << QString::number(event.collectionId()) << event.id() << QString::number(triggerTime);
180 KConfigGroup config(KSharedConfig::openConfig(), "General");
181 config.writeEntry("RtcWake", param);
182 config.sync();
183 Preferences::setWakeFromSuspendAdvance(advance);
184 close();
188 /******************************************************************************
189 * Called when the user clicks the Cancel Wake From Suspend button.
190 * Cancels any currently scheduled system wakeup.
192 void WakeFromSuspendDlg::cancelWakeClicked()
194 KAlarm::setRtcWakeTime(0, this);
195 KAlarm::deleteRtcWakeConfig();
196 mUi->showWakeButton->setEnabled(false);
197 mUi->cancelWakeButton->setEnabled(false);
198 enableDisableUseButton();
201 // vim: et sw=4: