2 * This file is part of TimedSilencer.
4 * TimedSilencer is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * TimedSilencer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with TimedSilencer. If not, see <http://www.gnu.org/licenses/>.
18 #include <QHBoxLayout>
19 #include <QVBoxLayout>
20 #include <QDialogButtonBox>
22 #include <QPushButton>
23 #include <QAbstractButton>
26 #include <QMaemo5ValueButton>
27 #include <QMaemo5TimePickSelector>
28 #include <QMaemo5InformationBox>
29 #include "newalarmdlg.h"
30 #include "alarmd_backend.h"
31 #include "qmaemo5weekdayspickselector.h"
32 #include "profileevent.h"
34 NewAlarmDlg::NewAlarmDlg(QWidget *parent, QByteArray edited_id) :
37 if(!edited_id.isNull()) {
38 pe = ProfileEvent::findByID(edited_id);
43 setWindowTitle(tr("Edit profile switching event"));
45 setWindowTitle(tr("New profile switching event"));
47 QHBoxLayout *hori_layout = new QHBoxLayout(this);
48 QVBoxLayout *verticalLayoutL = new QVBoxLayout();
49 verticalLayoutL->addWidget(new QLabel(tr("Use the silent profile")));
50 from_button = new QMaemo5ValueButton(tr("From"));
51 QMaemo5TimePickSelector *from_selector = new QMaemo5TimePickSelector();
53 from_selector->setCurrentTime(pe->from_time);
55 from_selector->setCurrentTime(QTime(22, 30));
57 from_button->setPickSelector(from_selector);
58 from_button->setValueLayout(QMaemo5ValueButton::ValueBesideText);
59 verticalLayoutL->addWidget(from_button);
60 to_button = new QMaemo5ValueButton(tr("To"));
61 QMaemo5TimePickSelector *to_selector = new QMaemo5TimePickSelector();
63 to_selector->setCurrentTime(pe->to_time);
65 to_selector->setCurrentTime(QTime(8, 0));
67 to_button->setPickSelector(to_selector);
68 to_button->setValueLayout(QMaemo5ValueButton::ValueBesideText);
69 verticalLayoutL->addWidget(to_button);
71 //verticalLayoutL->addItem(new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding));
72 repeat_button = new QMaemo5ValueButton(tr("Repeat"));
73 repeat_button->setValueLayout(QMaemo5ValueButton::ValueBesideText);
74 QMaemo5WeekDaysPickSelector *weekDaysSelector = new QMaemo5WeekDaysPickSelector;
76 weekDaysSelector->updateSelection(pe->days);
78 repeat_button->setPickSelector(weekDaysSelector);
79 verticalLayoutL->addWidget(repeat_button);
81 cb_enable = new QCheckBox(tr("Activated"));
82 cb_enable->setChecked(pe->activated);
83 verticalLayoutL->addWidget(cb_enable);
87 hori_layout->addLayout(verticalLayoutL);
88 QVBoxLayout *verticalLayoutR = new QVBoxLayout;
89 verticalLayoutR->addItem(new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding));
90 button_box = new QDialogButtonBox(Qt::Vertical);
93 delete_btn = new QPushButton(tr("Delete"));
94 connect(delete_btn, SIGNAL(clicked()), this, SLOT(deleteAndClose()));
95 button_box->addButton(delete_btn, QDialogButtonBox::ActionRole);
100 done_btn = new QPushButton(tr("Save"));
101 connect(done_btn, SIGNAL(clicked()), this, SLOT(saveAndClose()));
102 button_box->addButton(done_btn, QDialogButtonBox::ActionRole);
103 verticalLayoutR->addWidget(button_box);
104 hori_layout->addLayout(verticalLayoutR);
107 NewAlarmDlg::~NewAlarmDlg() {
112 void NewAlarmDlg::saveAndClose() {
114 ProfileEvent *new_pe = saveEvent();
116 // Update Alarmd events
117 if(pe && pe->activated) {
118 // Delete old alarmd events
119 AlarmdBackend::deleteEvents(pe);
121 if(new_pe->activated) {
122 // Add new alarmd events
123 AlarmdBackend::setProfileEvents(new_pe);
131 void NewAlarmDlg::deleteAndClose() {
133 // Remove Alarmd events
134 AlarmdBackend::deleteEvents(pe);
136 QSettings settings("TimedSilencer", "TimedSilencer");
137 QHash<QString, QVariant> events = settings.value("events").toHash();
138 Q_ASSERT(events.contains(pe->getID()));
139 events.remove(pe->getID());
140 settings.setValue("events", events);
142 emit deletedEvent(pe->getID());
146 ProfileEvent* NewAlarmDlg::saveEvent() {
147 QSettings settings("TimedSilencer", "TimedSilencer");
148 ProfileEvent *new_pe = new ProfileEvent;
149 new_pe->from_time = static_cast<QMaemo5TimePickSelector*>(from_button->pickSelector())->currentTime();
150 new_pe->to_time = static_cast<QMaemo5TimePickSelector*>(to_button->pickSelector())->currentTime();
151 new_pe->days = static_cast<QMaemo5WeekDaysPickSelector*>(repeat_button->pickSelector())->selectedDays();
153 new_pe->activated = cb_enable->isChecked();
154 QHash<QString, QVariant> events = settings.value("events").toHash();
155 if(pe && new_pe->getID() != pe->getID()) {
156 events.remove(pe->getID());
159 qDebug("Saving event...");
160 QVariant var_pe = new_pe->save();
162 if(pe->getID() == new_pe->getID()) {
163 qDebug("ID did not change");
164 if(pe->activated != new_pe->activated) {
165 // Only the activated status may change without altering the ID
166 emit editedEvent(new_pe->getID(), new_pe->activated);
169 qDebug("ID changed");
170 emit deletedEvent(pe->getID());
171 emit newEvent(var_pe);
175 if(events.contains(new_pe->getID())) {
176 QMaemo5InformationBox::information(parentWidget(), tr("This profile switching event already exists"));
179 emit newEvent(var_pe);
182 events.insert(new_pe->getID(), var_pe);
183 settings.setValue("events", events);