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