7b04b08565022c845c72031b70aee35b754d092f
[speedfreak] / Client / mainwindow.cpp
1 /*
2  * Mainwindow for speedFreak project
3  *
4  * @author      Rikhard Kuutti  <rikhard.kuutti@fudeco.com>
5  * @author      Toni Jussila    <toni.jussila@fudeco.com>
6  * @copyright   (c) 2010 Speed Freak team
7  * @license     http://opensource.org/licenses/gpl-license.php GNU Public License
8  */
9
10 #include "mainwindow.h"
11 #include "ui_mainwindow.h"
12
13 #include <QDesktopServices>
14 #include <QUrl>
15 #include <QSettings>
16 #include <QDebug>
17 #include "usersettings.h"
18
19 MainWindow::MainWindow(QWidget *parent) :
20     QMainWindow(parent),
21     ui(new Ui::MainWindow)
22 {
23     ui->setupUi(this);
24
25     QCoreApplication::setOrganizationName("Fudeco Oy");
26     QCoreApplication::setOrganizationDomain("fudeco.com");
27     QCoreApplication::setApplicationName("Speed Freak");
28
29     //routeDialog = new RouteDialog;
30     //connect(routeDialog,SIGNAL(sendroute()),this,SLOT(clientSendRoute()));
31
32     helpDialog = NULL;
33     accstart = NULL;
34     routeSaveDialog = NULL;
35     topResultDialog = NULL;
36
37     settingsDialog = new SettingsDialog;
38     connect(settingsDialog,SIGNAL(sendregistration()),this,SLOT(clientRegUserToServer()));
39     connect(settingsDialog,SIGNAL(userNameChanged()),this,SLOT(clientUserLogin()));
40     connect(settingsDialog, SIGNAL(logout()), this, SLOT(setUsernameToMainPanel()));
41
42     httpClient = new HttpClient(this);
43     connect(httpClient->myXmlreader, SIGNAL(receivedCategoryList()), this, SLOT(setCategoryCompoBox()));
44     connect(httpClient->myXmlreader, SIGNAL(receivedTop10List()), this, SLOT(showTop10()));    
45
46     //creditsDialog = new CreditsDialog;
47
48     welcomeDialog = new WelcomeDialog;
49     welcomeDialog->show();
50
51     this->setUsernameToMainPanel();
52
53     //Button settings
54     ui->pushButtonAccelerate->setAutoFillBackground(true);
55     ui->pushButtonAccelerate->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
56     ui->pushButtonRoute->setAutoFillBackground(true);
57     ui->pushButtonRoute->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
58     ui->pushButtonResults->setAutoFillBackground(true);
59     ui->pushButtonResults->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
60     ui->pushButtonSettings->setAutoFillBackground(true);
61     ui->pushButtonSettings->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
62     ui->pushButtonWWW->setAutoFillBackground(true);
63     ui->pushButtonWWW->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
64     ui->pushButtonCredits->setAutoFillBackground(true);
65     ui->pushButtonCredits->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
66 }
67
68 MainWindow::~MainWindow()
69 {
70     delete ui;
71
72     if(routeSaveDialog)
73         delete routeSaveDialog;
74
75     if(accstart)
76         delete accstart;
77
78     if(topResultDialog)
79         delete topResultDialog;
80
81     if(settingsDialog)
82         delete settingsDialog;
83
84     if(welcomeDialog)
85         delete welcomeDialog;
86
87     if(httpClient)
88         delete httpClient;
89
90     if(helpDialog)
91         delete helpDialog;
92 }
93
94 void MainWindow::changeEvent(QEvent *e)
95 {
96     QMainWindow::changeEvent(e);
97     switch (e->type()) {
98     case QEvent::LanguageChange:
99         ui->retranslateUi(this);
100         break;
101     default:
102         break;
103     }
104 }
105
106 /**
107   * This slot function opens browser to project www page.
108   */
109 void MainWindow::on_pushButtonWWW_clicked()
110 {
111     QDesktopServices::openUrl(QUrl("http://garage.maemo.org/projects/speedfreak/"));
112 }
113
114 /**
115   * This slot function opens the credits dialog
116   */
117 void MainWindow::on_pushButtonCredits_clicked()
118 {
119     if(!helpDialog)
120         helpDialog = new HelpDialog;
121     helpDialog->show();
122     //creditsDialog->show();
123 }
124
125 /**
126   * This slot function opens the route save dialog
127   */
128 void MainWindow::on_pushButtonRoute_clicked()
129 {
130     if(!routeSaveDialog)
131         routeSaveDialog = new RouteSaveDialog;
132     connect(routeSaveDialog, SIGNAL(sendroute()), this, SLOT(clientSendRoute()));
133     connect(topResultDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
134     routeSaveDialog->show();
135 }
136
137 /**
138   * This slot function opens the settings dialog
139   */
140 void MainWindow::on_pushButtonSettings_clicked()
141 {
142     settingsDialog->show();
143 }
144
145 /**
146   * This slot function opens the acceleration dialog
147   */
148 void MainWindow::on_pushButtonAccelerate_clicked()
149 {
150     if(!accstart)
151         accstart = new accelerationstart(this);
152     connect(accstart, SIGNAL(sendresult(QString, double)), this, SLOT(clientSendResult(QString, double)));
153     connect(accstart, SIGNAL(rejected()), this, SLOT(killDialog()));
154     accstart->show();
155 }
156
157 /**
158   * This slot function opens the top results dialog
159   */
160 void MainWindow::on_pushButtonResults_clicked()
161 {
162     if (!topResultDialog)
163         topResultDialog = new TopResultDialog;
164     clientRequestCategoryList();
165     connect(topResultDialog, SIGNAL(refreshCategoryList()), this, SLOT(clientRequestCategoryList()));
166     connect(topResultDialog, SIGNAL(refreshTopList(int)), this, SLOT(clientRequestTopList(int)));
167     connect(topResultDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
168     topResultDialog->show();
169 }
170
171 /**
172   *This slot function is called when ever mytTopResultDialog emits signal refreshCategoryList button clicked.
173   */
174 void MainWindow::clientRequestCategoryList()
175 {
176     httpClient->requestCategories();
177 }
178
179 /**
180   *This slot function is called when ever mytTopResultDialog emits signal refreshTopList button clicked.
181   */
182 void MainWindow::clientRequestTopList(int index)
183 {
184     QString limit = QString::number(topResultDialog->getLimitNr());
185     httpClient->requestTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(index), limit);
186 }
187
188 /**
189   *This function is used to set items to category combobox. Top-tab view.
190   *@param
191   */
192 void MainWindow::setCategoryCompoBox()
193 {
194     topResultDialog->setCompoBoxCategories(httpClient->myXmlreader->myCategoryList->getCategoryList());
195 }
196
197 /**
198   *This function prcesses UI updating after a new top10List has been received.
199   *@todo Check where limitNr is taken, fixed or user input, see on_comboBoxTopCategory_currentIndexChanged.
200   */
201 void MainWindow::showTop10()
202 {
203     int ind = topResultDialog->getRecentCategoryIndex();
204     setListViewTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(ind), topResultDialog->getLimitNr());
205 }
206
207 /**
208   *This function is used to set items to labelTopList. Top-tab view.
209   *@param Category
210   *@param Size, number of results.
211   */
212 void MainWindow::setListViewTopList(QString category, int size)
213 {
214     QString topList;
215     topList.append(httpClient->myXmlreader->myCategoryList->getTopList(category, size));
216     topResultDialog->showTopList(topList);
217 }
218
219 /**
220   * This function register user to server.
221   */
222 void MainWindow::clientRegUserToServer()
223 {
224     httpClient->requestRegistration();
225 }
226
227 /**
228   * This function performs login to server.
229   */
230 void MainWindow::clientUserLogin()
231 {
232     connect(httpClient, SIGNAL(loginOK()), this, SLOT(setUsernameToMainPanel()));
233     httpClient->checkLogin();
234 }
235
236 /**
237   * This function send route data to server.
238   */
239 void MainWindow::clientSendRoute()
240 {
241     httpClient->sendRouteXml();
242 }
243
244 /**
245   * This function send acceleration data to server.
246   */
247 void MainWindow::clientSendResult(QString category, double result)
248 {
249     qDebug() << "__clientSendResult";
250     if(accstart) {
251         qDebug() << "_clientSendResult, calling server";
252         httpClient->sendResultXml(category, result);
253     }
254 }
255 /**
256   * This slot function called when ever dialog finished.
257   */
258 void MainWindow::killDialog()
259 {
260     if(topResultDialog)
261     {
262         delete topResultDialog;
263         topResultDialog = NULL;
264     }
265     else if(routeSaveDialog)
266     {
267         delete routeSaveDialog;
268         routeSaveDialog = NULL;
269     }
270     else if(accstart)
271     {
272         delete accstart;
273         accstart = NULL;
274     }
275 }
276
277 void MainWindow::setUsernameToMainPanel()
278 {
279     if (loginSaved())
280     {
281         this->setWindowTitle("SpeedFreak - " + settingsDialog->getUserName());
282     }
283     else
284     {
285         this->setWindowTitle("SpeedFreak - Not logged");
286     }
287 }