Make sure settings are only saved when the "Save" button is pressed and the daily...
[timedsilencer] / mainwindow.cpp
1 /*
2  * This file is part of TimedSilencer.
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 #include <QMaemo5ValueButton>
19 #include <QMaemo5TimePickSelector>
20 #include <QMaemo5InformationBox>
21 #include <QVBoxLayout>
22 #include <QLabel>
23 #include <QSpacerItem>
24 #include <QSettings>
25 #include <QCheckBox>
26 #include <QPushButton>
27 #include <QHBoxLayout>
28 #include <QCloseEvent>
29
30 #include "mainwindow.h"
31 #include "alarmd_backend.h"
32 #include "dbus_backend.h"
33
34 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
35   setCentralWidget(new QWidget());
36   QHBoxLayout *hori_layout = new QHBoxLayout(centralWidget());
37   QVBoxLayout *verticalLayoutL = new QVBoxLayout();
38   verticalLayoutL->addItem(new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding));
39   QLabel *from_lbl = new QLabel(tr("Use the silent profile between"));
40   from_lbl->setAlignment(Qt::AlignHCenter);
41   verticalLayoutL->addWidget(from_lbl);
42   from_button = new QMaemo5ValueButton();
43   from_button->setPickSelector(new QMaemo5TimePickSelector());
44   verticalLayoutL->addWidget(from_button);
45   QLabel *to_lbl = new QLabel(tr("and"));
46   to_lbl->setAlignment(Qt::AlignHCenter);
47   verticalLayoutL->addWidget(to_lbl);
48   to_button = new QMaemo5ValueButton();
49   to_button->setPickSelector(new QMaemo5TimePickSelector());
50   verticalLayoutL->addWidget(to_button);
51   // Status
52   verticalLayoutL->addItem(new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding));
53   cb_enable = new QCheckBox(tr("Activated"));
54   verticalLayoutL->addWidget(cb_enable);
55   hori_layout->addLayout(verticalLayoutL);
56   QVBoxLayout *verticalLayoutR = new QVBoxLayout;
57   verticalLayoutR->addItem(new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding));
58   done_btn = new QPushButton(tr("Save"));
59   done_btn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
60   connect(done_btn, SIGNAL(clicked()), this, SLOT(saveAndClose()));
61   verticalLayoutR->addWidget(done_btn);
62   hori_layout->addLayout(verticalLayoutR);
63   // Load settings
64   loadSettings();
65   // Auto rotation
66   setAttribute(Qt::WA_Maemo5AutoOrientation, true);
67 }
68
69 MainWindow::~MainWindow() {
70   delete from_button;
71   delete to_button;
72   delete cb_enable;
73   delete done_btn;
74 }
75
76 void MainWindow::saveAndClose() {
77   // Save the settings and set the events
78   if(cb_enable->isChecked()) {
79     setProfileEvents();
80     QMaemo5InformationBox::information(this, tr("The daily profile switching is activated"), 0);
81   } else {
82     AlarmdBackend::deleteEvents();
83     QMaemo5InformationBox::information(this, tr("The daily profile switching is deactivated"), 0);
84   }
85   saveSettings();
86   // Close the window
87   close();
88 }
89
90 void MainWindow::saveSettings() {
91   QSettings settings("TimedSilencer", "TimedSilencer");
92   settings.setValue("from_time", static_cast<QMaemo5TimePickSelector*>(from_button->pickSelector())->currentTime());
93   settings.setValue("to_time", static_cast<QMaemo5TimePickSelector*>(to_button->pickSelector())->currentTime());
94   settings.setValue("enabled", cb_enable->isChecked());
95 }
96
97 void MainWindow::loadSettings() {
98   QSettings settings("TimedSilencer", "TimedSilencer");
99   QTime from_time = settings.value("from_time", QTime(22, 0)).toTime();
100   static_cast<QMaemo5TimePickSelector*>(from_button->pickSelector())->setCurrentTime(from_time);
101   QTime to_time = settings.value("to_time", QTime(8, 0)).toTime();
102   static_cast<QMaemo5TimePickSelector*>(to_button->pickSelector())->setCurrentTime(to_time);
103   cb_enable->setChecked(settings.value("enabled", false).toBool());
104 }
105
106 void MainWindow::setProfileEvents() {
107   // Set profile events in Alarmd
108   QTime from_time = static_cast<QMaemo5TimePickSelector*>(from_button->pickSelector())->currentTime();
109   qDebug("From time: %s", qPrintable(from_time.toString()));
110   AlarmdBackend::setProfileEvent(SILENT, from_time);
111   QTime to_time = static_cast<QMaemo5TimePickSelector*>(to_button->pickSelector())->currentTime();
112   AlarmdBackend::setProfileEvent(GENERAL, to_time);
113   qDebug("To time: %s", qPrintable(to_time.toString()));
114   // Update current profile
115   bool in_silent_mode = false;
116   QTime ctime = QTime::currentTime();
117   if(from_time < to_time) {
118     in_silent_mode = (ctime > from_time && ctime < to_time);
119   } else {
120     // to_time is the next day
121     in_silent_mode = (ctime > from_time || (ctime < from_time && ctime < to_time));
122   }
123   if(in_silent_mode)
124     DBusBackend::setProfile(SILENT);
125   /*else
126     DBusBackend::setProfile(GENERAL);*/
127 }