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 *******************************************************************/
9 ConfigTab::ConfigTab(QWidget *parent): QWidget(parent) {
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();
17 int val = num.toInt(); val--;
20 parse("data/data_"+old+".xml");
22 connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(httpRequestFinished(int, bool)));
23 connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
25 msgbox = new QLabel("");
26 connect(this, SIGNAL(errMsgBox(const QString &)), this, SLOT(showMsgBox(const QString &)));
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()));
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);
79 ConfigTab::~ConfigTab() {
87 if(proxyWin) delete proxyWin;
103 void ConfigTab::changeLang(int langId) {
107 void ConfigTab::openProxy() {
108 proxyWin = new ProxySet();
111 void ConfigTab::saveSettings() {
112 const char* langs[5] = {"en", "cs", "ro", "pl", "lt"};
113 QSettings settings("config/global.ini", QSettings::IniFormat);
115 settings.setValue("lang", langs[langToSave]);
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());
137 void ConfigTab::httpRequestFinished(int requestId, bool error) {
138 if (requestId != httpId) return;
144 emit errMsgBox(tr("Download failed: %1.").arg(http->errorString()));
146 QFile control("data/data_"+num+".xml");
147 control.open(QIODevice::ReadOnly);
152 QSettings settings("config/global.ini", QSettings::IniFormat);
153 QString parseNum = num;
154 int val = num.toInt();
156 settings.setValue("nextXmlId", num);
158 emit errMsgBox(QString("<span style=\"color:green;\">") + tr("Download OK") + "</span>");
159 parse("data/data_"+parseNum+".xml");
163 control.ungetChar(c);
164 control.readLine(err, sizeof(err));
166 emit errMsgBox(QString("<span style=\"color:red;\">") + tr("Error: ") + err + "</span>");
167 butDown->setEnabled(true);
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()));
182 void ConfigTab::showMsgBox(const QString &text) {
184 msgbox->setText(text);