167bf33bcbf6dc0042b954cf8b62c306df9c07d0
[speedfreak] / Client / carmainwindow.cpp
1 /**
2   * CarMainWindow main class
3   *
4   * @author     Toni Jussila <toni.jussila@fudeco.com>
5   * @author     Janne Änäkkälä <janne.anakkala@fudeco.com>
6   * @author     Tiina Kivilinna-Korhola <tiina.kivilinna-korhola@fudeco.com>
7   * @author     Olavi Pulkkinen <olavi.pulkkinen@fudeco.com>
8   * @copyright  (c) 2010 Speed Freak team
9   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
10   */
11
12 #include "carmainwindow.h"
13
14 /**
15   *Constructor of this class.
16   *@param QWidget pointer to parent object. By default the value is NULL.
17   */
18 CarMainWindow::CarMainWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::CarMainWindow)
19 {
20     ui->setupUi(this);
21     result = new ResultDialog();
22     measure = new MeasureDialog();
23     xmlreader = new XmlReader();
24
25     initComboBoxStartTabUnits();
26     initListViewStartTabAccelerationCategories();
27
28     myLogin = new LoginWindow(this);
29     myRegistration = new Registration(this);
30     xmlwriter = new XmlWriter();
31     manager = new QNetworkAccessManager(this);
32     connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(networkResponse(QNetworkReply*)));
33     connect(myRegistration,SIGNAL(sendregistration()),this,SLOT(registrate()));
34     connect(result,SIGNAL(sendresult()),this,SLOT(sendXml()));
35
36 }
37
38 /**
39   *Destructor of this class. Should be used to release all allocated resources.
40   */
41 CarMainWindow::~CarMainWindow()
42 {
43     delete ui;
44     delete result;
45     delete measure;
46     delete xmlreader;
47     delete xmlwriter;
48     delete manager;
49 }
50
51 /**
52   *This function is used to .
53   *@param
54   */
55 void CarMainWindow::changeEvent(QEvent *e)
56 {
57     QMainWindow::changeEvent(e);
58     switch (e->type()) {
59     case QEvent::LanguageChange:
60         ui->retranslateUi(this);
61         break;
62     default:
63         break;
64     }
65 }
66
67 /**
68   *This slot function is called when ever list view is update. Start-tab view.
69   */
70 void CarMainWindow::on_listViewStartTabAccelerationCategories_clicked(QModelIndex index)
71 {
72     QString str = index.data().toString();
73     QStringList list = str.split("-");
74     QStringList list2 = list[1].split(" ");
75
76     ui->lineEditStartTabMin->setText(list[0]);
77     ui->lineEditStartTabMax->setText(list2[0]);
78     updateComboBoxStartTabUnits(list2[1]);
79 }
80
81 /**
82   *This slot function is called when ever auto start button clicked. Start-tab view.
83   */
84 void CarMainWindow::on_autoStartButton_clicked()
85 {
86
87     delete measure;
88     measure = NULL;
89     measure = new MeasureDialog();
90
91     connect(measure, SIGNAL(speedAchieved()), this, SLOT(openResultView()));
92     // Show measure dialog.
93     measure->show();
94 }
95
96 /**
97   *This slot function is called when ever list view is update. Start-tab view.
98   *@param QString unit.
99   */
100 void CarMainWindow::updateComboBoxStartTabUnits(QString unit)
101 {
102     ui->comboBoxStartTabUnits->setCurrentIndex(ui->comboBoxStartTabUnits->findText(unit, Qt::MatchExactly));
103 }
104
105 /**
106   *This function is used to init unit combobox. Start-tab view.
107   */
108 void CarMainWindow::initComboBoxStartTabUnits()
109 {
110     units << "km/h" << "km" << "h" << "m" << "min" << "Mile" << "Mph" << "in" << "ft" << "yrd";
111     ui->comboBoxStartTabUnits->addItems(units);
112 }
113
114 /**
115   *This function is used to set items to unit combobox. Start-tab view.
116   *@param QStringlist units
117   */
118 void CarMainWindow::setComboBoxStartTabUnits(QStringList units)
119 {
120     ui->comboBoxStartTabUnits->addItems(units);
121 }
122
123 /**
124   *This function is used to init listViewStartTabAccelerationCategories. Start-tab view.
125   */
126 void CarMainWindow::initListViewStartTabAccelerationCategories()
127 {
128     accelerationCategoriesStartTab << "0-40 km/h" << "0-100 km/h"; //<< "0-1/4 Mile" << "0-1/8 Mile" << "0-50 km" << "50-100 Mile" << "0-60 Mph" << "0-100 m" << "0-50 ft" << "0-50 yrd" << "0-500 in";
129     QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
130     ui->listViewStartTabAccelerationCategories->setModel(model);
131 }
132
133 /**
134   *This function is used to set items to listViewStartTabAccelerationCategories. Start-tab view.
135   *@param QStringlist accelerationCategoriesStartTab
136   */
137 void CarMainWindow::setListViewStartTabAccelerationCategories(QStringList accelerationCategoriesStartTab)
138 {
139     QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
140     ui->listViewStartTabAccelerationCategories->setModel(model);
141 }
142
143 /**
144   *This function is used to set items to category combobox. Top-tab view.
145   *@param
146   */
147 void CarMainWindow::setCategoryCompoBox()
148 {
149     ui->comboBoxTopCategory->addItems(xmlreader->getTop10List());
150 }
151
152 /**
153   *This function is used to set items to labelTopList. Top-tab view.
154   *@param QString category
155   */
156 void CarMainWindow::setListViewTopList(QString category)
157 {
158     QString topList;
159
160     if (category == "acceleration-0-100")
161     {
162         topList.append(xmlreader->getTop10AccelerationList());
163     }
164
165     else if (category == "Speed")
166     {
167         topList.append(xmlreader->getTop10SpeedList());
168     }
169
170     else if (category == "G-force")
171     {
172         topList.append(xmlreader->getTop10GforceList());
173     }
174     ui->labelTopList->setText(topList);
175 }
176
177 /**
178   *This slot function is called when speed is achieved in measure dialog. Opens result dialog.
179   */
180 void CarMainWindow::openResultView()
181 {
182     result->saveMeasuresToArray(measure->measures);
183     // Show result dialog.
184     result->show();
185 }
186
187 /**
188   *This slot function is called when the server has finished guery.
189   */
190 void CarMainWindow::networkResponse(QNetworkReply *reply)
191 {
192
193 }
194
195 /**
196   *This slot function is called when the user will to send data to server.
197   *@todo Where is this callback connected?
198   */
199 void CarMainWindow::on_pushButton_clicked()
200 {
201      sendXml();
202 }
203
204 /**
205   *This slot function is called when login/logout button is clicked.
206   */
207 void CarMainWindow::on_loginLogoutButton_clicked()
208 {
209     //LoginWindow myLogin;
210
211     myLogin->show();
212     //ui->loginLogoutButton->setText("logout");
213 }
214
215 /**
216   *This slot function is called when registrate button is clicked.
217   */
218 void CarMainWindow::on_registratePushButton_clicked()
219 {
220     myRegistration->show();
221 }
222
223 /**
224   *This slot function is called when ever refresh button clicked. Top-tab view.
225   */
226 void CarMainWindow::on_buttonTopRefresh_clicked()
227 {
228     setCategoryCompoBox();
229 }
230
231 /**
232   *This slot function is called when ever category combobox current index changed. Top-tab view.
233   *@param QString category
234   */
235 void CarMainWindow::on_comboBoxTopCategory_currentIndexChanged(QString category)
236 {
237     setListViewTopList(category);
238 }
239
240 /**
241   *This slot function is called when ever category combobox activated. Top-tab view.
242   *@param QString category
243   */
244 void CarMainWindow::on_comboBoxTopCategory_activated(QString category)
245 {
246     setListViewTopList(category);
247 }
248
249 /**
250   *This slot function is called when set/change user button is clicked.
251   */
252 void CarMainWindow::on_setUserPushButton_clicked()
253 {
254     myLogin->show();
255
256     ui->userNameLabel->setText( "User: " + myLogin->getUserName());
257     ui->setUserPushButton->setText( "Change User");
258 }
259
260 /**
261   *@brief Sends registration information to the server in xml format.
262   *Reads user name, password and emaol address from resuldialogs internal variables.
263   *@todo Replace msg box with better reaction to server`s responce.
264   *@todo Write error handling.
265   */
266 void CarMainWindow::registrate()
267 {
268     qDebug() << "_registrate" ;
269     qDebug() << this->myRegistration->getUserName() << "+" << this->myRegistration->getPassword() << "+" << this->myRegistration->getEmail();
270
271     QBuffer *regbuffer = new QBuffer();
272
273     QNetworkReply *currentDownload;
274
275     QUrl qurl("http//:api.speedfreak-app.com/register");
276     QNetworkRequest request(qurl);
277
278     //write also to a file during development, :
279     xmlwriter->writeXml(this->myRegistration->getUserName(),
280                       this->myRegistration->getPassword(),
281                       this->myRegistration->getEmail());
282     xmlwriter->writeRegistering(regbuffer,
283                       this->myRegistration->getUserName(),
284                       this->myRegistration->getPassword(),
285                       this->myRegistration->getEmail());
286     //Tmp msgbox - later server responce
287     QMessageBox::about(this,"Registrate",this->myRegistration->getUserName() + this->myRegistration->getPassword() + this->myRegistration->getEmail());
288
289     currentDownload = manager->post(request, ("data=" + regbuffer->data()));
290
291     //ackFromServer function gets called when HTTP request is completed
292     connect(currentDownload, SIGNAL(finished()),SLOT(ackOfRegistration()));
293 }
294
295 /**
296   *@brief Sends result(s) to the server in xml format with authentication information in the header.
297   *@todo Write error handling.
298   */
299 void CarMainWindow::sendXml()
300 {
301     qDebug() << "_sendXml";
302
303     QBuffer *xmlbuffer = new QBuffer();
304     QNetworkReply *currentDownload;
305
306     QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword();
307     credentials = "Basic " + credentials.toAscii().toBase64();
308
309     QUrl qurl("http//:api.speedfreak-app.com/update/acceleration-0-40");
310     QNetworkRequest request(qurl);
311
312     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
313
314     xmlwriter->writeResult(xmlbuffer);
315
316     currentDownload = manager->post(request, ("data=" + xmlbuffer->data()));
317     //QString data("abcdefg");    //testing
318     //currentDownload = manager->post(request,"data=" + QUrl::toPercentEncoding(data));   //testing
319
320
321     //ackFromServer function gets called when HTTP request is completed
322     connect(currentDownload, SIGNAL(finished()),SLOT(ackOfResult()));
323
324 }
325
326 /**
327   *@brief React to servers responce after result has been sent.
328   *@todo Implement function and write error handling.
329   */
330 void CarMainWindow::ackOfResult()
331 {
332     qDebug() << "Server acknowledged posting of result";
333 }
334
335 /**
336   *@brief React to servers responce after registration has been sent.
337   *@todo Implement function and write error handling.
338   */
339
340 void CarMainWindow::ackOfRegistration()
341 {
342     qDebug() << "Server acknowledged registration";
343 }
344
345
346 /**
347   *@brief Just for development, for the real button is not shown until
348   *measurin started and there are results.
349   *@todo Implement with real code and yet leave sendXml in the bottom in use.
350   */
351
352 void CarMainWindow::on_manualStartButton_clicked()
353 {
354     sendXml();
355 }