Route save/open development.
[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 void MainWindow::changeEvent(QEvent *e)
187 {
188     QMainWindow::changeEvent(e);
189     switch (e->type()) {
190     case QEvent::LanguageChange:
191         ui->retranslateUi(this);
192         break;
193     default:
194         break;
195     }
196 }
197
198 /**
199   *This slot function is called when ever mytTopResultDialog emits signal refreshCategoryList button clicked.
200   */
201 void MainWindow::clientRequestCategoryList()
202 {
203     if(httpClient)
204         httpClient->requestCategories();
205 }
206
207 /**
208   *This slot function is called when ever mytTopResultDialog emits signal refreshTopList button clicked.
209   */
210 void MainWindow::clientRequestTopList(int index)
211 {
212     QString limit;
213
214     if(topResultDialog && httpClient->myXmlreader->myCategoryList)
215     {
216         limit = QString::number(topResultDialog->getLimitNr());
217         httpClient->requestTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(index), limit);
218     }
219 }
220
221 /**
222   *This function is used to set items to category combobox.
223   *@param
224   */
225 void MainWindow::setCategoryCompoBox()
226 {
227     if(topResultDialog && httpClient->myXmlreader->myCategoryList)
228         topResultDialog->setCompoBoxCategories(httpClient->myXmlreader->myCategoryList->getCategoryList());
229 }
230
231 /**
232   *This function prcesses UI updating after a new top10List has been received.
233   *@todo Check where limitNr is taken, fixed or user input, see on_comboBoxTopCategory_currentIndexChanged.
234   */
235 void MainWindow::showTop10()
236 {
237     int ind;
238
239     if(topResultDialog && httpClient->myXmlreader->myCategoryList && topResultDialog)
240     {
241         ind = topResultDialog->getRecentCategoryIndex();
242         setListViewTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(ind), topResultDialog->getLimitNr());
243     }
244 }
245
246 /**
247   *This function is used to set items to labelTopList. Top-tab view.
248   *@param Category
249   *@param Size, number of results.
250   */
251 void MainWindow::setListViewTopList(QString category, int size)
252 {
253     QString topList;
254
255     if(httpClient->myXmlreader->myCategoryList && topResultDialog)
256     {
257         topList.append(httpClient->myXmlreader->myCategoryList->getTopList(category, size));
258         topResultDialog->showTopList(topList);
259     }
260 }
261
262 /**
263   * This function register user to server.
264   */
265 void MainWindow::clientRegUserToServer()
266 {
267     if(httpClient)
268         httpClient->requestRegistration();
269 }
270
271 /**
272   * This function performs login to server.
273   */
274 void MainWindow::clientUserLogin()
275 {
276     
277     if(httpClient)
278     {
279         connect(httpClient, SIGNAL(loginOK()), this, SLOT(setUsernameToMainPanel()));
280         httpClient->checkLogin();
281     }
282 }
283
284 /**
285   * This function send route data to server.
286   */
287 void MainWindow::clientSendRoute(QString s,int i)
288 {
289     if(httpClient)
290         httpClient->sendRouteXml(s,i);
291 }
292
293 /**
294   * This function send acceleration data to server.
295   */
296 void MainWindow::clientSendResult(QString category, double result)
297 {
298     qDebug() << "__clientSendResult";
299     if(accstart) {
300         qDebug() << "_clientSendResult, calling server";
301         if(httpClient)
302             httpClient->sendResultXml(category, result);
303     }
304 }
305
306 /**
307   * This slot function called when ever dialog rejected.
308   */
309 void MainWindow::killDialog()
310 {
311     if(topResultDialog)
312     {
313         qDebug() << "__MW kill: topResultDialog";
314         delete topResultDialog;
315         topResultDialog = NULL;
316     }
317     if(routeSaveDialog)
318     {
319         //qDebug() << "__MW kill: routeSaveDialog";
320         //delete routeSaveDialog;
321         //routeSaveDialog = NULL;
322     }
323     if(accstart)
324     {
325         qDebug() << "__MW kill: accstart";
326         delete accstart;
327         accstart = NULL;
328     }
329     if(welcomeDialog)
330     {
331         qDebug() << "__MW kill: welcomeDialog";
332         delete welcomeDialog;
333         welcomeDialog = NULL;
334     }
335     if(helpDialog)
336     {
337         qDebug() << "__MW kill: helpDialog";
338         delete helpDialog;
339         helpDialog = NULL;
340     }
341 }
342
343 /**
344   *
345   */
346 void MainWindow::setUsernameToMainPanel()
347 {
348     if (loginSaved())
349     {
350         this->setWindowTitle("SpeedFreak - " + settingsDialog->getUserName());
351     }
352     else
353     {
354         this->setWindowTitle("SpeedFreak - Not logged");
355     }
356 }
357 /**
358   * This slot function opens acceleration start dialog.
359   */
360 void MainWindow::OpenAccStartDialog()
361 {
362     if(!accstart)
363         accstart = new accelerationstart(this);
364
365     connect(accstart, SIGNAL(sendresult(QString, double)), this, SLOT(clientSendResult(QString, double)));
366     connect(accstart, SIGNAL(rejected()), this, SLOT(killDialog()));
367     accstart->show();
368 }
369 /**
370   * This slot function opens the route save dialog
371   */
372 void MainWindow::OpenRouteDialog()
373 {
374     if(!routeSaveDialog)
375         routeSaveDialog = new RouteSaveDialog;
376
377     connect(routeSaveDialog, SIGNAL(sendroute(QString,int)), this, SLOT(clientSendRoute(QString,int)));
378     connect(routeSaveDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
379     routeSaveDialog->show();
380 }
381 /**
382   * This slot function opens the top results dialog
383   */
384 void MainWindow::OpenResultDialog()
385 {
386     if (!topResultDialog)
387         topResultDialog = new TopResultDialog;
388
389     clientRequestCategoryList();
390     connect(topResultDialog, SIGNAL(refreshCategoryList()), this, SLOT(clientRequestCategoryList()));
391     connect(topResultDialog, SIGNAL(refreshTopList(int)), this, SLOT(clientRequestTopList(int)));
392     connect(topResultDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
393     topResultDialog->show();
394 }
395 /**
396   * This slot function opens the settings dialog
397   */
398 void MainWindow::OpenSettingsDialog()
399 {
400     settingsDialog->show();
401 }
402 /**
403   * This slot function opens browser to project www page.
404   */
405 void MainWindow::OpenWWWPage()
406 {
407     QDesktopServices::openUrl(QUrl("http://garage.maemo.org/projects/speedfreak/"));
408 }
409 /**
410   * This slot function opens the main help dialog
411   */
412 void MainWindow::OpenHelpDialog()
413 {
414     if(!helpDialog)
415         helpDialog = new HelpDialog;
416
417     connect(helpDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
418     helpDialog->show();
419 }
420
421 /**
422   * This slot function save user profile data to server
423   */
424 void MainWindow::saveProfile()
425 {
426     if(httpClient)
427         httpClient->sendProfileXml();
428 }