helpForUsers files added.
[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  * @author      Janne Änäkkälä  <janne.anakkala@fudeco.com>
7  * @copyright   (c) 2010 Speed Freak team
8  * @license     http://opensource.org/licenses/gpl-license.php GNU Public License
9  */
10
11 #include <QDesktopServices>
12 #include <QUrl>
13 #include <QSettings>
14 #include <QDebug>
15 #include "mainwindow.h"
16 #include "ui_mainwindow.h"
17 #include "usersettings.h"
18
19 /**
20   * Constructor of this class.
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     customButtonRoute = new CustomButton(this,icon);
76     delete icon;
77
78     buttons_x += 140;
79     customButtonRoute->setGeometry(buttons_x,buttons_y,130,130);
80     connect(customButtonRoute, SIGNAL(OpenDialog()), this, SLOT(OpenRouteDialog()));
81     customButtonRoute->show();
82
83     // Create icon for results dialog button
84     icon = new QIcon();
85     icon->addFile(QString(":/new/prefix1/Graphics/trophy_gold.png"), QSize(125,125), QIcon::Normal, QIcon::Off);
86     icon->addFile(QString(":/new/prefix1/Graphics/trophy_gold_selected.png"), QSize(125,125), QIcon::Normal, QIcon::On);
87
88     // Results dialog button
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     customButtonSettings = new CustomButton(this,icon);
103     delete icon;
104
105     buttons_x += 140;
106     customButtonSettings->setGeometry(buttons_x,buttons_y,130,130);
107     connect(customButtonSettings, SIGNAL(OpenDialog()), this, SLOT(OpenSettingsDialog()));
108     customButtonSettings->show();
109
110     //Create icon for users dialog button
111     icon = new QIcon();
112     icon->addFile(QString(":/new/prefix1/Graphics/users.png"), QSize(125,125), QIcon::Normal, QIcon::Off);
113     icon->addFile(QString(":/new/prefix1/Graphics/users_selected.png"), QSize(125,125), QIcon::Normal, QIcon::On);
114
115     //Users dialog button
116     customButtonUsers = new CustomButton(this,icon);
117
118     // Create icon for www page button
119     icon = new QIcon();
120     icon->addFile(QString(":/new/prefix1/Graphics/applications_internet.png"), QSize(125,125), QIcon::Normal, QIcon::Off);
121     icon->addFile(QString(":/new/prefix1/Graphics/applications_internet_selected.png"), QSize(125,125), QIcon::Normal, QIcon::On);
122
123     // WWW page button
124     customButtonWWW = new CustomButton(this,icon);
125
126     delete icon;
127
128     buttons_x += 140;
129     customButtonUsers->setGeometry(buttons_x,buttons_y,130,130);
130     connect(customButtonUsers, SIGNAL(OpenDialog()), this, SLOT(openUsersDialog()));
131     customButtonUsers->show();
132
133     // Create icon for help dialog button
134     icon = new QIcon();
135     icon->addFile(QString(":/new/prefix1/Graphics/info.png"), QSize(85,85), QIcon::Normal, QIcon::Off);
136     icon->addFile(QString(":/new/prefix1/Graphics/info_selected.png"), QSize(85,85), QIcon::Normal, QIcon::On);
137
138     // Help dialog button
139     customButtonHelp = new CustomButton(this,icon);
140     delete icon;
141
142     customButtonHelp->setGeometry(670,10,105,105);
143     connect(customButtonHelp, SIGNAL(OpenDialog()), this, SLOT(OpenHelpDialog()));
144     customButtonHelp->show();
145
146
147     //Create icon for www page button
148     icon = new QIcon();
149     icon->addFile(QString(":/new/prefix1/Graphics/applications_internet.png"), QSize(85,85), QIcon::Normal, QIcon::Off);
150     icon->addFile(QString(":/new/prefix1/Graphics/applications_internet_selected.png"), QSize(85,85), QIcon::Normal, QIcon::On);
151
152     //WWW page button
153     customButtonWWW = new CustomButton(this,icon);
154     delete icon;
155
156     customButtonWWW->setGeometry(670,320,105,105);
157     connect(customButtonWWW, SIGNAL(OpenDialog()), this, SLOT(OpenWWWPage()));
158     customButtonWWW->show();
159
160 }
161
162 /**
163   * Destructor of this class
164   */
165 MainWindow::~MainWindow()
166 {
167     delete ui;
168
169     if(routeSaveDialog)
170         delete routeSaveDialog;
171
172     if(accstart)
173         delete accstart;
174
175     if(topResultDialog)
176         delete topResultDialog;
177
178     if(settingsDialog)
179         delete settingsDialog;
180
181     if(welcomeDialog)
182         delete welcomeDialog;
183
184     if(httpClient)
185         delete httpClient;
186
187     if(helpDialog)
188         delete helpDialog;
189
190     if(usersDialog)
191         delete usersDialog;
192
193     if(customButtonAccelerate)
194         delete customButtonAccelerate;
195     if(customButtonRoute)
196         delete customButtonRoute;
197     if(customButtonResults)
198         delete customButtonResults;
199     if(customButtonSettings)
200         delete customButtonSettings;
201     if(customButtonWWW)
202         delete customButtonWWW;
203     if(customButtonHelp)
204         delete customButtonHelp;
205 }
206
207 /**
208   *
209   */
210 void MainWindow::changeEvent(QEvent *e)
211 {
212     QMainWindow::changeEvent(e);
213     switch (e->type()) {
214     case QEvent::LanguageChange:
215         ui->retranslateUi(this);
216         break;
217     default:
218         break;
219     }
220 }
221
222 /**
223   * This slot function is called when ever mytTopResultDialog emits signal refreshCategoryList button clicked.
224   */
225 void MainWindow::clientRequestCategoryList()
226 {
227     if(httpClient)
228         httpClient->requestCategories();
229 }
230
231 /**
232   * This slot function is called when ever mytTopResultDialog emits signal refreshTopList button clicked.
233   *
234   * @param int index
235   */
236 void MainWindow::clientRequestTopList(int index)
237 {
238     QString limit;
239
240     if(topResultDialog && httpClient->myXmlreader->myCategoryList)
241     {
242         limit = QString::number(topResultDialog->getLimitNr());
243         httpClient->requestTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(index), limit);
244     }
245 }
246
247 /**
248   * This function is used to set items to category combobox.
249   */
250 void MainWindow::setCategoryCompoBox()
251 {
252     if(topResultDialog && httpClient->myXmlreader->myCategoryList)
253         topResultDialog->setCompoBoxCategories(httpClient->myXmlreader->myCategoryList->getCategoryList());
254 }
255
256 /**
257   * This function prcesses UI updating after a new top10List has been received.
258   *
259   * @todo Check where limitNr is taken, fixed or user input, see on_comboBoxTopCategory_currentIndexChanged.
260   */
261 void MainWindow::showTop10()
262 {
263     int ind;
264
265     if(topResultDialog && httpClient->myXmlreader->myCategoryList && topResultDialog)
266     {
267         ind = topResultDialog->getRecentCategoryIndex();
268         setListViewTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(ind), topResultDialog->getLimitNr());
269     }
270 }
271
272 /**
273   * This function is used to set items to labelTopList.
274   *
275   * @param QString Category
276   * @param int Size, number of results.
277   */
278 void MainWindow::setListViewTopList(QString category, int size)
279 {
280     QString topList;
281
282     if(httpClient->myXmlreader->myCategoryList && topResultDialog)
283     {
284         topList.append(httpClient->myXmlreader->myCategoryList->getTopList(category, size));
285         topResultDialog->showTopList(topList);
286     }
287 }
288
289 /**
290   * This function register user to server.
291   */
292 void MainWindow::clientRegUserToServer()
293 {
294     if(httpClient)
295         httpClient->requestRegistration();
296 }
297
298 /**
299   * This function performs login to server.
300   */
301 void MainWindow::clientUserLogin()
302 {
303     if(httpClient)
304     {
305         connect(httpClient, SIGNAL(loginOK()), this, SLOT(setUsernameToMainPanel()));
306         httpClient->checkLogin();
307     }
308 }
309
310 /**
311   * This function send route data to server.
312   *
313   * @param QString oldName, old file name
314   * @param QString newName, new file name
315   * @param int i
316   */
317 void MainWindow::clientSendRoute(QString oldName, QString newName, int i)
318 {
319     if(httpClient)
320         httpClient->sendRouteXml(oldName, newName, i);
321 }
322
323 /**
324   * This function send acceleration data to server.
325   *
326   * @param QString category
327   * @param double result
328   */
329 void MainWindow::clientSendResult(QString category, double result)
330 {
331     qDebug() << "__clientSendResult";
332     if(accstart) {
333         qDebug() << "_clientSendResult, calling server";
334         if(httpClient)
335             httpClient->sendResultXml(category, result);
336     }
337 }
338
339 /**
340   * This slot function called when ever dialog rejected.
341   */
342 void MainWindow::killDialog()
343 {
344     if(topResultDialog)
345     {
346         qDebug() << "__MW kill: topResultDialog";
347         delete topResultDialog;
348         topResultDialog = NULL;
349     }
350     if(routeSaveDialog)
351     {
352         //qDebug() << "__MW kill: routeSaveDialog";
353         //delete routeSaveDialog;
354         //routeSaveDialog = NULL;
355     }
356     if(accstart)
357     {
358         qDebug() << "__MW kill: accstart";
359         delete accstart;
360         accstart = NULL;
361     }
362     if(welcomeDialog)
363     {
364         qDebug() << "__MW kill: welcomeDialog";
365         delete welcomeDialog;
366         welcomeDialog = NULL;
367     }
368     if(helpDialog)
369     {
370         qDebug() << "__MW kill: helpDialog";
371         delete helpDialog;
372         helpDialog = NULL;
373     }
374     if(usersDialog)
375     {
376         qDebug() << "__MW kill: usersDialog";
377         delete usersDialog;
378         usersDialog = NULL;
379     }
380 }
381
382 /**
383   * Set user name to main panel.
384   */
385 void MainWindow::setUsernameToMainPanel()
386 {
387     if (loginSaved())
388     {
389         this->setWindowTitle("SpeedFreak - " + settingsDialog->getUserName());
390     }
391     else
392     {
393         this->setWindowTitle("SpeedFreak - Not logged");
394     }
395 }
396 /**
397   * This slot function opens acceleration start dialog.
398   */
399 void MainWindow::OpenAccStartDialog()
400 {
401     if(!accstart)
402         accstart = new accelerationstart(this);
403
404     connect(accstart, SIGNAL(sendresult(QString, double)), this, SLOT(clientSendResult(QString, double)));
405     connect(accstart, SIGNAL(rejected()), this, SLOT(killDialog()));
406     accstart->show();
407 }
408 /**
409   * This slot function opens the route save dialog
410   */
411 void MainWindow::OpenRouteDialog()
412 {
413     if(!routeSaveDialog)
414         routeSaveDialog = new RouteSaveDialog;
415
416     connect(routeSaveDialog, SIGNAL(sendroute(QString,QString,int)), this, SLOT(clientSendRoute(QString,QString,int)));
417     connect(routeSaveDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
418     routeSaveDialog->show();
419 }
420 /**
421   * This slot function opens the top results dialog
422   */
423 void MainWindow::OpenResultDialog()
424 {
425     if (!topResultDialog)
426         topResultDialog = new TopResultDialog;
427
428     clientRequestCategoryList();
429     connect(topResultDialog, SIGNAL(refreshCategoryList()), this, SLOT(clientRequestCategoryList()));
430     connect(topResultDialog, SIGNAL(refreshTopList(int)), this, SLOT(clientRequestTopList(int)));
431     connect(topResultDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
432     topResultDialog->show();
433 }
434 /**
435   * This slot function opens the settings dialog
436   */
437 void MainWindow::OpenSettingsDialog()
438 {
439     settingsDialog->show();
440 }
441 /**
442   * This slot function opens browser to project www page.
443   */
444 void MainWindow::OpenWWWPage()
445 {
446     QDesktopServices::openUrl(QUrl("http://www.speedfreak-app.com/"));
447 }
448 /**
449   * This slot function opens the main help dialog
450   */
451 void MainWindow::OpenHelpDialog()
452 {
453     if(!helpDialog)
454         helpDialog = new HelpDialog;
455
456     connect(helpDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
457     helpDialog->show();
458 }
459
460 /**
461   * This slot function save user profile data to server.
462   */
463 void MainWindow::saveProfile()
464 {
465     if(httpClient)
466         httpClient->sendProfileXml();
467 }
468
469 /**
470   * This slot function calls httpClients requestUserInfo for getting user's information from server.
471   *
472   * @param QString name
473   */
474 void MainWindow::requestGetUserInfo(QString name)
475 {
476     qDebug() << "getUserInfo signal " + name;
477     if(httpClient)
478     {
479         httpClient->requestUserInfo(name);
480     }
481 }
482
483 /**
484   * This slot function calls httpClients requestUsers for getting usernames from server.
485   */
486 void MainWindow::requestGetUsers()
487 {
488     qDebug() << "getUsers signal";
489     if(httpClient)
490     {
491         httpClient->requestUsers();
492     }
493 }
494
495 /**
496   * This slot function open users dialog.
497   */
498 void MainWindow::openUsersDialog()
499 {
500     if(!usersDialog)
501         usersDialog = new UsersDialog;
502
503     connect(usersDialog, SIGNAL(getUserInfo(QString)), this, SLOT(requestGetUserInfo(QString)));
504     //connect(usersDialog, SIGNAL(getUsers()), this, SLOT(requestGetUsers()));
505     requestGetUsers();
506     connect(usersDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
507     usersDialog->show();
508 }