Progress bar added to route dialog. Profile send to server disabled.
[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 /**
20   *
21   */
22 MainWindow::MainWindow(QWidget *parent) :
23     QMainWindow(parent),
24     ui(new Ui::MainWindow)
25 {
26     ui->setupUi(this);
27
28     QCoreApplication::setOrganizationName("Fudeco Oy");
29     QCoreApplication::setOrganizationDomain("fudeco.com");
30     QCoreApplication::setApplicationName("Speed Freak");
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     connect(settingsDialog, SIGNAL(saveprofile()),      this, SLOT(saveProfile()));
42
43     httpClient = new HttpClient(this);
44     connect(httpClient->myXmlreader, SIGNAL(receivedCategoryList()), this, SLOT(setCategoryCompoBox()));
45     connect(httpClient->myXmlreader, SIGNAL(receivedTop10List()), this, SLOT(showTop10()));    
46
47     welcomeDialog = new WelcomeDialog;
48     welcomeDialog->show();
49
50     this->setUsernameToMainPanel();
51
52     //Create icon for acceleration start button
53     QIcon* icon = new QIcon();
54     icon->addFile(QString(":/new/prefix1/Graphics/Speedometer.png"), QSize(125,125), QIcon::Normal, QIcon::Off);
55     icon->addFile(QString(":/new/prefix1/Graphics/Speedometer2.png"), QSize(125,125), QIcon::Normal, QIcon::On);
56
57     //Acceleration start button
58
59     customButtonAccelerate = new CustomButton(this,icon);
60     delete icon;
61
62     int buttons_x = 50,buttons_y = 165;
63     customButtonAccelerate->setGeometry(buttons_x,buttons_y,130,130);
64     connect(customButtonAccelerate, SIGNAL(OpenDialog()), this, SLOT(OpenAccStartDialog()));
65     customButtonAccelerate->show();
66
67     //Create icon for route dialog button
68     icon = new QIcon();
69     icon->addFile(QString(":/new/prefix1/Graphics/route.png"), QSize(125,125), QIcon::Normal, QIcon::Off);
70     icon->addFile(QString(":/new/prefix1/Graphics/route_selected.png"), QSize(125,125), QIcon::Normal, QIcon::On);
71
72     //Route dialog button
73
74     customButtonRoute = new CustomButton(this,icon);
75     delete icon;
76
77     buttons_x += 140;
78     customButtonRoute->setGeometry(buttons_x,buttons_y,130,130);
79     connect(customButtonRoute, SIGNAL(OpenDialog()), this, SLOT(OpenRouteDialog()));
80     customButtonRoute->show();
81
82     //Create icon for results dialog button
83     icon = new QIcon();
84     icon->addFile(QString(":/new/prefix1/Graphics/trophy_gold.png"), QSize(125,125), QIcon::Normal, QIcon::Off);
85     icon->addFile(QString(":/new/prefix1/Graphics/trophy_gold_selected.png"), QSize(125,125), QIcon::Normal, QIcon::On);
86
87     //Results dialog button
88
89     customButtonResults = new CustomButton(this,icon);
90     delete icon;
91
92     buttons_x += 140;
93     customButtonResults->setGeometry(buttons_x,buttons_y,130,130);
94     connect(customButtonResults, SIGNAL(OpenDialog()), this, SLOT(OpenResultDialog()));
95     customButtonResults->show();
96     //Create icon for settings dialog button
97     icon = new QIcon();
98     icon->addFile(QString(":/new/prefix1/Graphics/settings.png"), QSize(125,125), QIcon::Normal, QIcon::Off);
99     icon->addFile(QString(":/new/prefix1/Graphics/settings_selected.png"), QSize(125,125), QIcon::Normal, QIcon::On);
100
101     //Settings dialog button
102
103     customButtonSettings = new CustomButton(this,icon);
104     delete icon;
105
106     buttons_x += 140;
107     customButtonSettings->setGeometry(buttons_x,buttons_y,130,130);
108     connect(customButtonSettings, SIGNAL(OpenDialog()), this, SLOT(OpenSettingsDialog()));
109     customButtonSettings->show();
110
111     //Create icon for www page button
112     icon = new QIcon();
113     icon->addFile(QString(":/new/prefix1/Graphics/applications_internet.png"), QSize(125,125), QIcon::Normal, QIcon::Off);
114     icon->addFile(QString(":/new/prefix1/Graphics/applications_internet_selected.png"), QSize(125,125), QIcon::Normal, QIcon::On);
115
116     //WWW page button
117
118     customButtonWWW = new CustomButton(this,icon);
119     delete icon;
120
121     buttons_x += 140;
122     customButtonWWW->setGeometry(buttons_x,buttons_y,130,130);
123     connect(customButtonWWW, SIGNAL(OpenDialog()), this, SLOT(OpenWWWPage()));
124     customButtonWWW->show();
125
126     //Create icon for help dialog button
127     icon = new QIcon();
128     icon->addFile(QString(":/new/prefix1/Graphics/info.png"), QSize(105,105), QIcon::Normal, QIcon::Off);
129     icon->addFile(QString(":/new/prefix1/Graphics/info_selected.png"), QSize(105,105), QIcon::Normal, QIcon::On);
130
131     //Help dialog button
132
133     customButtonHelp = new CustomButton(this,icon);
134     delete icon;
135
136     customButtonHelp->setGeometry(670,10,105,105);
137     connect(customButtonHelp, SIGNAL(OpenDialog()), this, SLOT(OpenHelpDialog()));
138     customButtonHelp->show();
139 }
140
141 /**
142   *
143   */
144 MainWindow::~MainWindow()
145 {
146     delete ui;
147
148     if(routeSaveDialog)
149         delete routeSaveDialog;
150
151     if(accstart)
152         delete accstart;
153
154     if(topResultDialog)
155         delete topResultDialog;
156
157     if(settingsDialog)
158         delete settingsDialog;
159
160     if(welcomeDialog)
161         delete welcomeDialog;
162
163     if(httpClient)
164         delete httpClient;
165
166     if(helpDialog)
167         delete helpDialog;
168
169     if(customButtonAccelerate)
170         delete customButtonAccelerate;
171     if(customButtonRoute)
172         delete customButtonRoute;
173     if(customButtonResults)
174         delete customButtonResults;
175     if(customButtonSettings)
176         delete customButtonSettings;
177     if(customButtonWWW)
178         delete customButtonWWW;
179     if(customButtonHelp)
180         delete customButtonHelp;
181 }
182
183
184 /**
185   *
186   */
187 void MainWindow::changeEvent(QEvent *e)
188 {
189     QMainWindow::changeEvent(e);
190     switch (e->type()) {
191     case QEvent::LanguageChange:
192         ui->retranslateUi(this);
193         break;
194     default:
195         break;
196     }
197 }
198
199 /**
200   *This slot function is called when ever mytTopResultDialog emits signal refreshCategoryList button clicked.
201   */
202 void MainWindow::clientRequestCategoryList()
203 {
204     if(httpClient)
205         httpClient->requestCategories();
206 }
207
208 /**
209   *This slot function is called when ever mytTopResultDialog emits signal refreshTopList button clicked.
210   */
211 void MainWindow::clientRequestTopList(int index)
212 {
213     QString limit;
214
215     if(topResultDialog && httpClient->myXmlreader->myCategoryList)
216     {
217         limit = QString::number(topResultDialog->getLimitNr());
218         httpClient->requestTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(index), limit);
219     }
220 }
221
222 /**
223   *This function is used to set items to category combobox. Top-tab view.
224   *@param
225   */
226 void MainWindow::setCategoryCompoBox()
227 {
228     if(topResultDialog && httpClient->myXmlreader->myCategoryList)
229         topResultDialog->setCompoBoxCategories(httpClient->myXmlreader->myCategoryList->getCategoryList());
230 }
231
232 /**
233   *This function prcesses UI updating after a new top10List has been received.
234   *@todo Check where limitNr is taken, fixed or user input, see on_comboBoxTopCategory_currentIndexChanged.
235   */
236 void MainWindow::showTop10()
237 {
238     int ind;
239
240     if(topResultDialog && httpClient->myXmlreader->myCategoryList && topResultDialog)
241     {
242         ind = topResultDialog->getRecentCategoryIndex();
243         setListViewTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(ind), topResultDialog->getLimitNr());
244     }
245 }
246
247 /**
248   *This function is used to set items to labelTopList. Top-tab view.
249   *@param Category
250   *@param Size, number of results.
251   */
252 void MainWindow::setListViewTopList(QString category, int size)
253 {
254     QString topList;
255
256     if(httpClient->myXmlreader->myCategoryList && topResultDialog)
257     {
258         topList.append(httpClient->myXmlreader->myCategoryList->getTopList(category, size));
259         topResultDialog->showTopList(topList);
260     }
261 }
262
263 /**
264   * This function register user to server.
265   */
266 void MainWindow::clientRegUserToServer()
267 {
268     if(httpClient)
269         httpClient->requestRegistration();
270 }
271
272 /**
273   * This function performs login to server.
274   */
275 void MainWindow::clientUserLogin()
276 {
277     
278     if(httpClient)
279     {
280         connect(httpClient, SIGNAL(loginOK()), this, SLOT(setUsernameToMainPanel()));
281         httpClient->checkLogin();
282     }
283 }
284
285 /**
286   * This function send route data to server.
287   */
288 void MainWindow::clientSendRoute()
289 {
290     if(httpClient)
291         httpClient->sendRouteXml();
292 }
293
294 /**
295   * This function send acceleration data to server.
296   */
297 void MainWindow::clientSendResult(QString category, double result)
298 {
299     qDebug() << "__clientSendResult";
300     if(accstart) {
301         qDebug() << "_clientSendResult, calling server";
302         if(httpClient)
303             httpClient->sendResultXml(category, result);
304     }
305 }
306
307 /**
308   * This slot function called when ever dialog rejected.
309   */
310 void MainWindow::killDialog()
311 {
312     if(topResultDialog)
313     {
314         qDebug() << "__MW kill: topResultDialog";
315         delete topResultDialog;
316         topResultDialog = NULL;
317     }
318     if(routeSaveDialog)
319     {
320         //qDebug() << "__MW kill: routeSaveDialog";
321         //delete routeSaveDialog;
322         //routeSaveDialog = NULL;
323     }
324     if(accstart)
325     {
326         qDebug() << "__MW kill: accstart";
327         delete accstart;
328         accstart = NULL;
329     }
330     if(welcomeDialog)
331     {
332         qDebug() << "__MW kill: welcomeDialog";
333         delete welcomeDialog;
334         welcomeDialog = NULL;
335     }
336     if(helpDialog)
337     {
338         qDebug() << "__MW kill: helpDialog";
339         delete helpDialog;
340         helpDialog = NULL;
341     }
342 }
343
344 /**
345   *
346   */
347 void MainWindow::setUsernameToMainPanel()
348 {
349     if (loginSaved())
350     {
351         this->setWindowTitle("SpeedFreak - " + settingsDialog->getUserName());
352     }
353     else
354     {
355         this->setWindowTitle("SpeedFreak - Not logged");
356     }
357 }
358 /**
359   * This slot function opens acceleration start dialog.
360   */
361 void MainWindow::OpenAccStartDialog()
362 {
363     if(!accstart)
364         accstart = new accelerationstart(this);
365
366     connect(accstart, SIGNAL(sendresult(QString, double)), this, SLOT(clientSendResult(QString, double)));
367     connect(accstart, SIGNAL(rejected()), this, SLOT(killDialog()));
368     accstart->show();
369 }
370 /**
371   * This slot function opens the route save dialog
372   */
373 void MainWindow::OpenRouteDialog()
374 {
375     if(!routeSaveDialog)
376         routeSaveDialog = new RouteSaveDialog;
377
378     connect(routeSaveDialog, SIGNAL(sendroute()), this, SLOT(clientSendRoute()));
379     connect(routeSaveDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
380     routeSaveDialog->show();
381 }
382 /**
383   * This slot function opens the top results dialog
384   */
385 void MainWindow::OpenResultDialog()
386 {
387     if (!topResultDialog)
388         topResultDialog = new TopResultDialog;
389
390     clientRequestCategoryList();
391     connect(topResultDialog, SIGNAL(refreshCategoryList()), this, SLOT(clientRequestCategoryList()));
392     connect(topResultDialog, SIGNAL(refreshTopList(int)), this, SLOT(clientRequestTopList(int)));
393     connect(topResultDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
394     topResultDialog->show();
395 }
396 /**
397   * This slot function opens the settings dialog
398   */
399 void MainWindow::OpenSettingsDialog()
400 {
401     settingsDialog->show();
402 }
403 /**
404   * This slot function opens browser to project www page.
405   */
406 void MainWindow::OpenWWWPage()
407 {
408     QDesktopServices::openUrl(QUrl("http://garage.maemo.org/projects/speedfreak/"));
409 }
410 /**
411   * This slot function opens the main help dialog
412   */
413 void MainWindow::OpenHelpDialog()
414 {
415     if(!helpDialog)
416         helpDialog = new HelpDialog;
417
418     connect(helpDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
419     helpDialog->show();
420 }
421
422 /**
423   * This slot function save user profile data to server
424   */
425 void MainWindow::saveProfile()
426 {
427     if(httpClient)
428         httpClient->sendProfileXml();
429 }