e08ab2cac9ee185baeca18f8fce0c7364dfebdb4
[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 #include "poialerts.h"
33 #include "speedalarm.h"
34 #include "mediaplayer.h"
35
36 MainWindow::MainWindow(): QMainWindow(0), menu_(0), themeLoader_(0), mainScreen_(0)
37 {
38     setWindowTitle(tr("jSpeed"));
39     showFullScreen();
40     addScreens();
41     QTimer::singleShot(500, this, SLOT(loadServices()));
42 }
43
44 MainWindow::~MainWindow()
45 {
46     delete themeLoader_;
47 }
48
49 void MainWindow::loadServices()
50 {
51     Odometer::instance().start();
52     QApplication::processEvents();
53     startBacklight();
54     QApplication::processEvents();
55     PoiAlerts::instance().start();
56     QApplication::processEvents();
57     SpeedAlarm::instance().start();
58     QApplication::processEvents();
59     MediaPlayer::init();
60 }
61
62 void MainWindow::addScreens()
63 {
64     stack_ = new MainWindowStack(this);
65
66     connect(stack_, SIGNAL(minimizePressed()), this, SLOT(minimize()));
67     connect(stack_, SIGNAL(settingsPressed()), this, SLOT(openMenu()));
68     connect(stack_, SIGNAL(closePressed()), this, SIGNAL(quit()));
69
70     mainScreen_ = new WidgetScreen(this);
71     WidgetScreen* detailScreen = new WidgetScreen(this);
72
73     themeLoader_ = new ThemeLoader(mainScreen_, detailScreen);
74
75     if(!loadTheme())
76     {
77         return;
78     }
79
80     stack_->addScreen(mainScreen_);
81     stack_->addScreen(detailScreen);
82
83     connect(QApplication::desktop(), SIGNAL(resized(int)), stack_, SLOT(reArrange()));
84
85     setCentralWidget(stack_);
86 }
87
88 bool MainWindow::loadTheme()
89 {
90     if(!themeLoader_->load())
91     {
92         QMaemo5InformationBox::information(this, tr("Unable to load theme: %1").arg(themeLoader_->error()));
93         close();
94         return false;
95     }
96
97     if(mainScreen_->orientationEnabled(WidgetScreen::LANDSCAPE) &&
98        mainScreen_->orientationEnabled(WidgetScreen::PORTRAIT))
99     {
100         setAttribute(Qt::WA_Maemo5AutoOrientation, true);
101     }
102     else if(mainScreen_->orientationEnabled(WidgetScreen::PORTRAIT))
103     {
104         setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
105     }
106     else
107     {
108         setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
109     }
110
111     return true;
112 }
113
114 void MainWindow::minimize()
115 {
116     QDBusConnection connection = QDBusConnection::sessionBus();
117     QDBusMessage msg = QDBusMessage::createSignal("/",
118                                                   "com.nokia.hildon_desktop",
119                                                   "exit_app_view");
120     connection.send(msg);
121 }
122
123 void MainWindow::openMenu()
124 {
125     if(!menu_)
126     {
127         menu_ = new MainMenu(this);
128         connect(menu_, SIGNAL(resetTrip()), &(Odometer::instance()), SLOT(resetTrip()));
129         connect(menu_, SIGNAL(resetAll()), &(Odometer::instance()), SLOT(resetAll()));
130         connect(menu_, SIGNAL(flip()), stack_, SLOT(flip()));
131         connect(menu_, SIGNAL(themeChanged()), this, SLOT(loadTheme()));
132         connect(menu_, SIGNAL(unitChanged()), &(Odometer::instance()), SLOT(updateUnit()));
133     }
134
135     menu_->show();
136 }
137
138 void MainWindow::startBacklight()
139 {
140     QTimer* timer = new QTimer(this);
141     timer->setInterval(25000);
142     connect(timer, SIGNAL(timeout()), this, SLOT(keepBacklightOn()));
143     timer->start();
144 }
145
146 void MainWindow::keepBacklightOn()
147 {
148     QDBusConnection connection = QDBusConnection::systemBus();
149     QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.mce",
150                                                       "/com/nokia/mce/request",
151                                                       "com.nokia.mce.request",
152                                                       "req_display_blanking_pause");
153
154     connection.call(msg);
155 }