Version 0.3
[aspt.git] / configtab.cc
1 /*******************************************************************
2 Jan Cermak | johniez | aspt.johniez.com
3 Creative Commons 2.5 Attribution-NonCommercial-NoDerivs 2.5 license
4 http://creativecommons.org/licenses/by-nc-nd/2.5/legalcode
5 *******************************************************************/
6
7 #include "configtab.h"
8
9 /**
10  * \brief Config Tab constructor.
11  */
12 ConfigTab::ConfigTab(QWidget *parent): QWidget(parent) {
13         file = 0;
14         http = new QHttp("https://www.soccerproject.com/sptd.php", QHttp::ConnectionModeHttps, 443);
15         QSettings settings("config/global.ini", QSettings::IniFormat);
16         langToSave = settings.value("lang", 0).toInt();
17         useProxy = settings.value("Network/proxyUsed", 0).toInt();
18         num = settings.value("nextXmlId", "0").toString();
19         //old data count
20         int val = num.toInt(); val--;
21         QString old;
22         old.setNum(val);
23         parse("data/data_"+old+".xml");
24         
25         connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(httpRequestFinished(int, bool)));
26         connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
27         
28         msgbox = new QLabel("");
29         connect(this, SIGNAL(errMsgBox(const QString &)), this, SLOT(showMsgBox(const QString &)));
30         
31         nick = new QLineEdit("");
32         pass = new QLineEdit("");
33         nick->setText(settings.value("username", "").toString());
34         warnLabel = new QLabel(tr("<span style=\"color:red;\">Warning:</span><br>You can download data file only once between simulations.<br>Data files could be used for building statistics, do not delete them.<br>If you want to download your data, type your soccerproject.com login details.<br>Bugs or feature requests on: http://flyspray.johniez.com/proj2"));
35         nickLabel = new QLabel(tr("soccerproject.com <span style=\"color:red;\">nickname:</span>"));
36         passLabel = new QLabel(tr("soccerproject.com <span style=\"color:red;\">password:</span>"));
37         pass->setEchoMode(QLineEdit::Password);
38         butDown = new QPushButton(tr("Download"));
39         connect(butDown, SIGNAL(clicked()), this, SLOT(downloadLatest()));
40         butProxy = new QPushButton(tr("Proxy server settings"));
41         connect(butProxy, SIGNAL(clicked()), this, SLOT(openProxy()));
42         proxyWin = 0;
43
44         group1 = new QGroupBox(tr("Users data download"));
45         group1Layout = new QVBoxLayout;
46         group1Layout->addWidget(warnLabel);
47         row1 = new QHBoxLayout();
48         row1->addWidget(nickLabel);
49         row1->addWidget(nick);
50         group1Layout->addLayout(row1);
51         row2 = new QHBoxLayout();
52         row2->addWidget(passLabel);
53         row2->addWidget(pass);
54         group1Layout->addLayout(row2);
55         group1Layout->addWidget(butDown);
56         group1Layout->addWidget(butProxy);
57         group1Layout->addWidget(msgbox);
58         group1->setLayout(group1Layout);
59         group2 = new QGroupBox(tr("Language selection"));
60         group2Layout = new QVBoxLayout;
61         lang = new QComboBox();
62         lang->addItem(tr("English"), "en");
63         lang->addItem(tr("Czech"), "cs");
64         lang->addItem(tr("Romanian"), "ro");
65         lang->addItem(tr("Polish"), "pl");
66         lang->addItem(tr("Lithuanian"), "lt");
67         lang->setCurrentIndex(lang->findData(settings.value("lang", "en").toString()));
68         setLang = new QPushButton(tr("save change"));
69         langWarn = new QLabel(tr("Language selection take effect after application restart."));
70         group2Layout->addWidget(langWarn);
71         group2Layout->addWidget(lang);
72         group2Layout->addWidget(setLang);
73         connect(lang, SIGNAL(activated(int)), this, SLOT(changeLang(int)));
74         connect(setLang, SIGNAL(clicked()), this, SLOT(saveSettings()));
75         group2->setLayout(group2Layout);
76         mainLayout = new QVBoxLayout;
77         mainLayout->addWidget(group1);
78         mainLayout->addWidget(group2);
79         setLayout(mainLayout);
80 }
81
82 /**
83  * \brief Config Tab destructor.
84  */
85 ConfigTab::~ConfigTab() {
86         if(file) delete file;
87         delete http;
88         delete nick;
89         delete pass;
90         delete butDown;
91         delete butProxy;
92         delete msgbox;
93         if(proxyWin) delete proxyWin;
94         delete warnLabel;
95         delete nickLabel;
96         delete passLabel;
97         delete lang;
98         delete setLang;
99         delete langWarn;
100         delete row1;
101         delete row2;
102         delete group1Layout;
103         delete group1;
104         delete group2Layout;
105         delete group2;
106         delete mainLayout;
107 }
108
109 /**
110  * \brief Slot assigned to language change action.
111  * \param langId language ID.
112  */
113 void ConfigTab::changeLang(int langId) {
114         langToSave = langId;
115 }
116
117 /**
118  * \brief Slot assigned to proxy settings opening action.
119  */
120 void ConfigTab::openProxy() {
121         proxyWin = new ProxySet();
122 }
123
124 /**
125  * \brief Slot assigned to save button click.
126  */
127 void ConfigTab::saveSettings() {
128         const char* langs[] = {"en", "cs", "ro", "pl", "lt"};
129         const unsigned langSize = sizeof(langs) / sizeof(char *);
130         QSettings settings("config/global.ini", QSettings::IniFormat);
131         settings.setValue("lang", langs[langToSave % langSize]);
132         settings.sync();
133 }
134
135 /**
136  * \brief Slot assigned to download button click.
137  */
138 void ConfigTab::downloadLatest() {
139         QSettings settings("config/global.ini", QSettings::IniFormat);
140         useProxy = settings.value("Network/proxyUsed", 0).toInt();
141         butDown->setEnabled(false);
142         msgbox->setText("downloading...");
143         file = new QFile("data/data_"+num+".xml");
144         file->open(QIODevice::WriteOnly);
145         QUrl url("https://www.soccerproject.com/sptd.php");
146         int p_port = settings.value("Network/proxyPort", 443).toInt();
147         if(useProxy) http->setProxy(settings.value("Network/proxyAddr", "").toString(), p_port, settings.value("Network/proxyName", "").toString(), settings.value("Network/proxyPass", "").toString());
148         http->setHost(url.host(), QHttp::ConnectionModeHttps, 443);
149         //QTextStream out(stdout); out << "log";
150         httpId = http->get(url.path() + "?login=" + QUrl::toPercentEncoding(nick->text()) + "&pw=" + QUrl::toPercentEncoding(pass->text()) + "&sptdid=8&sptdpw=Cerm12", file);
151         settings.setValue("username", nick->text());
152         settings.sync();
153 }
154
155 /**
156  * \brief Slot assigned to download finished action.
157  */
158 void ConfigTab::httpRequestFinished(int requestId, bool error) {
159         if (requestId != httpId) return;
160
161         file->close();
162
163         if (error) {
164                 file->remove();
165                 emit errMsgBox(tr("Download failed: %1.").arg(http->errorString()));
166         } else {
167                 QFile control("data/data_"+num+".xml");
168                 control.open(QIODevice::ReadOnly);
169                 char c;
170                 control.getChar(&c);
171                 if(c == '<') {
172                         control.close();
173                         QSettings settings("config/global.ini", QSettings::IniFormat);
174                         QString parseNum = num;
175                         int val = num.toInt();
176                         num.setNum(++val);
177                         settings.setValue("nextXmlId", num);
178                         settings.sync();
179                         emit errMsgBox(QString("<span style=\"color:green;\">") + tr("Download OK") + "</span>");
180                         parse("data/data_"+parseNum+".xml");
181                         emit dataLoaded();
182                 } else {
183                         char err[512];
184                         control.ungetChar(c);
185                         control.readLine(err, sizeof(err));
186                         control.close();
187                         emit errMsgBox(QString("<span style=\"color:red;\">") + tr("Error: ") + err + "</span>");
188                         butDown->setEnabled(true);
189                 }
190         }
191         delete file;
192         file = 0;
193 }
194
195 /**
196  * \brief Slot assigned to QHttp response action.
197  * \param responseHeader QHttp response.
198  */
199 void ConfigTab::readResponseHeader(const QHttpResponseHeader &responseHeader) {
200         if (responseHeader.statusCode() != 200) {
201                 QMessageBox::information(this, tr("HTTP"), tr("Download failed: %1.").arg(responseHeader.reasonPhrase()));
202                 http->abort();
203                 return;
204         }
205 }
206
207 /**
208  * \brief Slot assigned to errMsgBox signal.
209  * \param text text to show.
210  */
211 void ConfigTab::showMsgBox(const QString &text) {
212         msgbox->clear();
213         msgbox->setText(text);
214 }