Changelog fixed.
[jspeed] / src / mainwindow.cpp
1 /*
2  * This file is part of jSpeed.
3  *
4  * jSpeed 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  * jSpeed 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 jSpeed.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18
19 #include <QtDBus/QDBusConnection>
20 #include <QtDBus/QDBusMessage>
21 #include <QtGui/QApplication>
22 #include <QtGui/QDesktopWidget>
23 #include <QtCore/QTimer>
24 #include <QtCore/QDebug>
25 #include <QMaemo5InformationBox>
26 #include "mainwindow.h"
27 #include "mainwindowstack.h"
28 #include "themeloader.h"
29 #include "mainmenu.h"
30 #include "odometer.h"
31 #include "widgetscreen.h"
32
33 MainWindow::MainWindow(): QMainWindow(0), menu_(0), themeLoader_(0), mainScreen_(0)
34 {
35     setWindowTitle(tr("jSpeed"));
36     showFullScreen();
37     Odometer::instance().start();
38     addScreens();
39     startBacklight();
40 }
41
42 MainWindow::~MainWindow()
43 {
44     delete themeLoader_;
45 }
46
47 void MainWindow::addScreens()
48 {
49     stack_ = new MainWindowStack(this);
50
51     connect(stack_, SIGNAL(minimizePressed()), this, SLOT(minimize()));
52     connect(stack_, SIGNAL(settingsPressed()), this, SLOT(openMenu()));
53     connect(stack_, SIGNAL(closePressed()), this, SIGNAL(quit()));
54
55     mainScreen_ = new WidgetScreen(this);
56     WidgetScreen* detailScreen = new WidgetScreen(this);
57
58     themeLoader_ = new ThemeLoader(mainScreen_, detailScreen);
59
60     if(!loadTheme())
61     {
62         return;
63     }
64
65     stack_->addScreen(mainScreen_);
66     stack_->addScreen(detailScreen);
67
68     connect(QApplication::desktop(), SIGNAL(resized(int)), stack_, SLOT(reArrange()));
69
70     setCentralWidget(stack_);
71 }
72
73 bool MainWindow::loadTheme()
74 {
75     if(!themeLoader_->load())
76     {
77         QMaemo5InformationBox::information(this, tr("Unable to load theme: %1").arg(themeLoader_->error()));
78         close();
79         return false;
80     }
81
82     if(mainScreen_->orientationEnabled(WidgetScreen::LANDSCAPE) &&
83        mainScreen_->orientationEnabled(WidgetScreen::PORTRAIT))
84     {
85         setAttribute(Qt::WA_Maemo5AutoOrientation, true);
86     }
87     else if(mainScreen_->orientationEnabled(WidgetScreen::PORTRAIT))
88     {
89         setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
90     }
91     else
92     {
93         setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
94     }
95
96     return true;
97 }
98
99 void MainWindow::minimize()
100 {
101     QDBusConnection connection = QDBusConnection::sessionBus();
102     QDBusMessage msg = QDBusMessage::createSignal("/",
103                                                   "com.nokia.hildon_desktop",
104                                                   "exit_app_view");
105     connection.send(msg);
106 }
107
108 void MainWindow::openMenu()
109 {
110     if(!menu_)
111     {
112         menu_ = new MainMenu(this);
113         connect(menu_, SIGNAL(resetTrip()), &(Odometer::instance()), SLOT(resetTrip()));
114         connect(menu_, SIGNAL(resetAll()), &(Odometer::instance()), SLOT(resetAll()));
115         connect(menu_, SIGNAL(flip()), stack_, SLOT(flip()));
116         connect(menu_, SIGNAL(themeChanged()), this, SLOT(loadTheme()));
117         connect(menu_, SIGNAL(unitChanged()), &(Odometer::instance()), SLOT(updateUnit()));
118     }
119
120     menu_->show();
121 }
122
123 void MainWindow::startBacklight()
124 {
125     QTimer* timer = new QTimer(this);
126     timer->setInterval(25000);
127     connect(timer, SIGNAL(timeout()), this, SLOT(keepBacklightOn()));
128     timer->start();
129 }
130
131 void MainWindow::keepBacklightOn()
132 {
133     QDBusConnection connection = QDBusConnection::systemBus();
134     QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.mce",
135                                                       "/com/nokia/mce/request",
136                                                       "com.nokia.mce.request",
137                                                       "req_display_blanking_pause");
138
139     connection.call(msg);
140 }