04aeb97c14c03b815e11139237c5b0893f28afd4
[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   * @param QString oldName, old file name
287   * @param QString newName, new file name
288   * @param int i
289   */
290 void MainWindow::clientSendRoute(QString oldName, QString newName, int i)
291 {
292     if(httpClient)
293         httpClient->sendRouteXml(oldName, newName, i);
294 }
295
296 /**
297   * This function send acceleration data to server.
298   */
299 void MainWindow::clientSendResult(QString category, double result)
300 {
301     qDebug() << "__clientSendResult";
302     if(accstart) {
303         qDebug() << "_clientSendResult, calling server";
304         if(httpClient)
305             httpClient->sendResultXml(category, result);
306     }
307 }
308
309 /**
310   * This slot function called when ever dialog rejected.
311   */
312 void MainWindow::killDialog()
313 {
314     if(topResultDialog)
315     {
316         qDebug() << "__MW kill: topResultDialog";
317         delete topResultDialog;
318         topResultDialog = NULL;
319     }
320     if(routeSaveDialog)
321     {
322         //qDebug() << "__MW kill: routeSaveDialog";
323         //delete routeSaveDialog;
324         //routeSaveDialog = NULL;
325     }
326     if(accstart)
327     {
328         qDebug() << "__MW kill: accstart";
329         delete accstart;
330         accstart = NULL;
331     }
332     if(welcomeDialog)
333     {
334         qDebug() << "__MW kill: welcomeDialog";
335         delete welcomeDialog;
336         welcomeDialog = NULL;
337     }
338     if(helpDialog)
339     {
340         qDebug() << "__MW kill: helpDialog";
341         delete helpDialog;
342         helpDialog = NULL;
343     }
344 }
345
346 /**
347   *
348   */
349 void MainWindow::setUsernameToMainPanel()
350 {
351     if (loginSaved())
352     {
353         this->setWindowTitle("SpeedFreak - " + settingsDialog->getUserName());
354     }
355     else
356     {
357         this->setWindowTitle("SpeedFreak - Not logged");
358     }
359 }
360 /**
361   * This slot function opens acceleration start dialog.
362   */
363 void MainWindow::OpenAccStartDialog()
364 {
365     if(!accstart)
366         accstart = new accelerationstart(this);
367
368     connect(accstart, SIGNAL(sendresult(QString, double)), this, SLOT(clientSendResult(QString, double)));
369     connect(accstart, SIGNAL(rejected()), this, SLOT(killDialog()));
370     accstart->show();
371 }
372 /**
373   * This slot function opens the route save dialog
374   */
375 void MainWindow::OpenRouteDialog()
376 {
377     if(!routeSaveDialog)
378         routeSaveDialog = new RouteSaveDialog;
379
380     connect(routeSaveDialog, SIGNAL(sendroute(QString,QString,int)), this, SLOT(clientSendRoute(QString,QString,int)));
381     connect(routeSaveDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
382     routeSaveDialog->show();
383 }
384 /**
385   * This slot function opens the top results dialog
386   */
387 void MainWindow::OpenResultDialog()
388 {
389     if (!topResultDialog)
390         topResultDialog = new TopResultDialog;
391
392     clientRequestCategoryList();
393     connect(topResultDialog, SIGNAL(refreshCategoryList()), this, SLOT(clientRequestCategoryList()));
394     connect(topResultDialog, SIGNAL(refreshTopList(int)), this, SLOT(clientRequestTopList(int)));
395     connect(topResultDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
396     topResultDialog->show();
397 }
398 /**
399   * This slot function opens the settings dialog
400   */
401 void MainWindow::OpenSettingsDialog()
402 {
403     settingsDialog->show();
404 }
405 /**
406   * This slot function opens browser to project www page.
407   */
408 void MainWindow::OpenWWWPage()
409 {
410     QDesktopServices::openUrl(QUrl("http://garage.maemo.org/projects/speedfreak/"));
411 }
412 /**
413   * This slot function opens the main help dialog
414   */
415 void MainWindow::OpenHelpDialog()
416 {
417     if(!helpDialog)
418         helpDialog = new HelpDialog;
419
420     connect(helpDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
421     helpDialog->show();
422 }
423
424 /**
425   * This slot function save user profile data to server
426   */
427 void MainWindow::saveProfile()
428 {
429     if(httpClient)
430         httpClient->sendProfileXml();
431 }