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