Initial commit
[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 <QMenuBar>
25 #include <QSettings>
26
27 #include "mainwindow.h"
28 #include "alarmd_backend.h"
29 #include "dbus_backend.h"
30
31 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
32   setCentralWidget(new QWidget());
33   QVBoxLayout *verticalLayout = new QVBoxLayout(centralWidget());
34   QLabel *from_lbl = new QLabel(tr("Use silent profile between"));
35   from_lbl->setAlignment(Qt::AlignHCenter);
36   verticalLayout->addWidget(from_lbl);
37   from_button = new QMaemo5ValueButton();
38   from_button->setPickSelector(new QMaemo5TimePickSelector());
39   verticalLayout->addWidget(from_button);
40   QLabel *to_lbl = new QLabel(tr("and"));
41   to_lbl->setAlignment(Qt::AlignHCenter);
42   verticalLayout->addWidget(to_lbl);
43   to_button = new QMaemo5ValueButton();
44   to_button->setPickSelector(new QMaemo5TimePickSelector());
45   verticalLayout->addWidget(to_button);
46   verticalLayout->addItem(new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding));
47   // Menu actions
48   active_action = menuBar()->addAction(tr("Enabled"));
49   active_action->setCheckable(true);
50   // Load settings
51   loadSettings();
52   connect(active_action, SIGNAL(triggered(bool)), this, SLOT(enableSilencing(bool)));
53   // Auto rotation
54   setAttribute(Qt::WA_Maemo5AutoOrientation, true);
55 }
56
57 MainWindow::~MainWindow() {
58   if(active_action->isChecked()) {
59     QMaemo5InformationBox::information(this, tr("The timed silencer is enabled"), 0);
60     setProfileEvents();
61   } else {
62     QMaemo5InformationBox::information(this, tr("The timed silencer is disabled"), 0);
63   }
64   saveSettings();
65   delete from_button;
66   delete to_button;
67 }
68
69 void MainWindow::saveSettings() {
70   QSettings settings("TimedSilencer", "TimedSilencer");
71   settings.setValue("from_time", static_cast<QMaemo5TimePickSelector*>(from_button->pickSelector())->currentTime());
72   settings.setValue("to_time", static_cast<QMaemo5TimePickSelector*>(to_button->pickSelector())->currentTime());
73   settings.setValue("enabled", active_action->isChecked());
74 }
75
76 void MainWindow::loadSettings() {
77   QSettings settings("TimedSilencer", "TimedSilencer");
78   QTime from_time = settings.value("from_time", QTime(22, 0)).toTime();
79   static_cast<QMaemo5TimePickSelector*>(from_button->pickSelector())->setCurrentTime(from_time);
80   QTime to_time = settings.value("to_time", QTime(8, 0)).toTime();
81   static_cast<QMaemo5TimePickSelector*>(to_button->pickSelector())->setCurrentTime(to_time);
82   active_action->setChecked(settings.value("enabled", true).toBool());
83 }
84
85 void MainWindow::enableSilencing(bool enabled) {
86   if(enabled) {
87     setProfileEvents();
88     //QMaemo5InformationBox::information(this, tr("The Timed Silencer is now enabled"), QMaemo5InformationBox::DefaultTimeout);
89   } else {
90     AlarmdBackend::deleteEvents();
91     //QMaemo5InformationBox::information(this, tr("The Timed Silencer is now disabled"), QMaemo5InformationBox::DefaultTimeout);
92   }
93 }
94
95 void MainWindow::setProfileEvents() {
96   // Set profile events in Alarmd
97   QTime from_time = static_cast<QMaemo5TimePickSelector*>(from_button->pickSelector())->currentTime();
98   qDebug("From time: %s", qPrintable(from_time.toString()));
99   AlarmdBackend::setProfileEvent(SILENT, from_time);
100   QTime to_time = static_cast<QMaemo5TimePickSelector*>(to_button->pickSelector())->currentTime();
101   AlarmdBackend::setProfileEvent(GENERAL, to_time);
102   qDebug("To time: %s", qPrintable(to_time.toString()));
103   // Update current profile
104   bool in_silent_mode = false;
105   QTime ctime = QTime::currentTime();
106   if(from_time < to_time) {
107     in_silent_mode = (ctime > from_time && ctime < to_time);
108   } else {
109     // to_time is the next day
110     in_silent_mode = (ctime > from_time || (ctime < from_time && ctime < to_time));
111   }
112   if(in_silent_mode)
113     DBusBackend::setProfile(SILENT);
114   /*else
115     DBusBackend::setProfile(GENERAL);*/
116 }