841dd046c465f400efc786ee194ac5ba3d4e8bb5
[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 ConfigTab::ConfigTab(QWidget *parent): QWidget(parent) {
10         file = 0;
11         http = new QHttp;
12         QSettings settings("config/global.ini", QSettings::IniFormat);
13         langToSave = settings.value("lang", 0).toInt();
14         useProxy = settings.value("Network/proxyUsed", 0).toInt();
15         num = settings.value("nextXmlId", "0").toString();
16         //old data count
17         int val = num.toInt(); val--;
18         QString old;
19         old.setNum(val);
20         parse("data/data_"+old+".xml");
21         
22         connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(httpRequestFinished(int, bool)));
23         connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
24         
25         msgbox = new QLabel("");
26         connect(this, SIGNAL(errMsgBox(const QString &)), this, SLOT(showMsgBox(const QString &)));
27         
28         nick = new QLineEdit("");
29         pass = new QLineEdit("");
30         nick->setText(settings.value("username", "").toString());
31         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"));
32         nickLabel = new QLabel(tr("soccerproject.com <span style=\"color:red;\">nickname:</span>"));
33         passLabel = new QLabel(tr("soccerproject.com <span style=\"color:red;\">password:</span>"));
34         pass->setEchoMode(QLineEdit::Password);
35         butDown = new QPushButton(tr("Download"));
36         connect(butDown, SIGNAL(clicked()), this, SLOT(downloadLatest()));
37         butProxy = new QPushButton(tr("Proxy server settings"));
38         connect(butProxy, SIGNAL(clicked()), this, SLOT(openProxy()));
39         proxyWin = 0;
40
41         group1 = new QGroupBox(tr("Users data download"));
42         group1Layout = new QVBoxLayout;
43         group1Layout->addWidget(warnLabel);
44         row1 = new QHBoxLayout();
45         row1->addWidget(nickLabel);
46         row1->addWidget(nick);
47         group1Layout->addLayout(row1);
48         row2 = new QHBoxLayout();
49         row2->addWidget(passLabel);
50         row2->addWidget(pass);
51         group1Layout->addLayout(row2);
52         group1Layout->addWidget(butDown);
53         group1Layout->addWidget(butProxy);
54         group1Layout->addWidget(msgbox);
55         group1->setLayout(group1Layout);
56         group2 = new QGroupBox(tr("Language selection"));
57         group2Layout = new QVBoxLayout;
58         lang = new QComboBox();
59         lang->addItem(tr("English"), "en");
60         lang->addItem(tr("Czech"), "cs");
61         lang->addItem(tr("Romanian"), "ro");
62         lang->addItem(tr("Polish"), "pl");
63         lang->addItem(tr("Lithuanian"), "lt");
64         lang->setCurrentIndex(lang->findData(settings.value("lang", "en").toString()));
65         setLang = new QPushButton(tr("save change"));
66         langWarn = new QLabel(tr("Language selection take effect after application restart."));
67         group2Layout->addWidget(langWarn);
68         group2Layout->addWidget(lang);
69         group2Layout->addWidget(setLang);
70         connect(lang, SIGNAL(activated(int)), this, SLOT(changeLang(int)));
71         connect(setLang, SIGNAL(clicked()), this, SLOT(saveSettings()));
72         group2->setLayout(group2Layout);
73         mainLayout = new QVBoxLayout;
74         mainLayout->addWidget(group1);
75         mainLayout->addWidget(group2);
76         setLayout(mainLayout);
77 }
78
79 ConfigTab::~ConfigTab() {
80         if(file) delete file;
81         delete http;
82         delete nick;
83         delete pass;
84         delete butDown;
85         delete butProxy;
86         delete msgbox;
87         if(proxyWin) delete proxyWin;
88         delete warnLabel;
89         delete nickLabel;
90         delete passLabel;
91         delete lang;
92         delete setLang;
93         delete langWarn;
94         delete row1;
95         delete row2;
96         delete group1Layout;
97         delete group1;
98         delete group2Layout;
99         delete group2;
100         delete mainLayout;
101 }
102
103 void ConfigTab::changeLang(int langId) {
104         langToSave = langId;
105 }
106
107 void ConfigTab::openProxy() {
108         proxyWin = new ProxySet();
109 }
110
111 void ConfigTab::saveSettings() {
112         const char* langs[5] = {"en", "cs", "ro", "pl", "lt"};
113         QSettings settings("config/global.ini", QSettings::IniFormat);
114         if(langs) {
115                 settings.setValue("lang", langs[langToSave]);
116                 settings.sync();
117         }
118 }
119
120 void ConfigTab::downloadLatest() {
121         QSettings settings("config/global.ini", QSettings::IniFormat);
122         useProxy = settings.value("Network/proxyUsed", 0).toInt();
123         butDown->setEnabled(false);
124         msgbox->setText("downloading...");
125         file = new QFile("data/data_"+num+".xml");
126         file->open(QIODevice::WriteOnly);
127         QUrl url("http://www.soccerproject.com/sptd.php");
128         int p_port = settings.value("Network/proxyPort", 80).toInt();
129         if(useProxy) http->setProxy(settings.value("Network/proxyAddr", "").toString(), p_port, settings.value("Network/proxyName", "").toString(), settings.value("Network/proxyPass", "").toString());
130         http->setHost(url.host(), 80);
131         //QTextStream out(stdout); out << "log";
132         httpId = http->get(url.path() + "?login=" + QUrl::toPercentEncoding(nick->text()) + "&pw=" + QUrl::toPercentEncoding(pass->text()) + "&sptdid=8&sptdpw=Cerm12", file);
133         settings.setValue("username", nick->text());
134         settings.sync();
135 }
136
137 void ConfigTab::httpRequestFinished(int requestId, bool error) {
138         if (requestId != httpId) return;
139
140         file->close();
141
142         if (error) {
143                 file->remove();
144                 emit errMsgBox(tr("Download failed: %1.").arg(http->errorString()));
145         } else {
146                 QFile control("data/data_"+num+".xml");
147                 control.open(QIODevice::ReadOnly);
148                 char c;
149                 control.getChar(&c);
150                 if(c == '<') {
151                         control.close();
152                         QSettings settings("config/global.ini", QSettings::IniFormat);
153                         QString parseNum = num;
154                         int val = num.toInt();
155                         num.setNum(++val);
156                         settings.setValue("nextXmlId", num);
157                         settings.sync();
158                         emit errMsgBox(QString("<span style=\"color:green;\">") + tr("Download OK") + "</span>");
159                         parse("data/data_"+parseNum+".xml");
160                         emit dataLoaded();
161                 } else {
162                         char err[512];
163                         control.ungetChar(c);
164                         control.readLine(err, sizeof(err));
165                         control.close();
166                         emit errMsgBox(QString("<span style=\"color:red;\">") + tr("Error: ") + err + "</span>");
167                         butDown->setEnabled(true);
168                 }
169         }
170         delete file;
171         file = 0;
172 }
173
174 void ConfigTab::readResponseHeader(const QHttpResponseHeader &responseHeader) {
175         if (responseHeader.statusCode() != 200) {
176                 QMessageBox::information(this, tr("HTTP"), tr("Download failed: %1.").arg(responseHeader.reasonPhrase()));
177                 http->abort();
178                 return;
179         }
180 }
181
182 void ConfigTab::showMsgBox(const QString &text) {
183         msgbox->clear();
184         msgbox->setText(text);
185 }