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