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 *******************************************************************/
10 * \brief Config Tab constructor.
12 ConfigTab::ConfigTab(QWidget *parent): QWidget(parent) {
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();
20 int val = num.toInt(); val--;
23 parse("data/data_"+old+".xml");
25 connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(httpRequestFinished(int, bool)));
26 connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
28 msgbox = new QLabel("");
29 connect(this, SIGNAL(errMsgBox(const QString &)), this, SLOT(showMsgBox(const QString &)));
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()));
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);
83 * \brief Config Tab destructor.
85 ConfigTab::~ConfigTab() {
93 if(proxyWin) delete proxyWin;
110 * \brief Slot assigned to language change action.
111 * \param langId language ID.
113 void ConfigTab::changeLang(int langId) {
118 * \brief Slot assigned to proxy settings opening action.
120 void ConfigTab::openProxy() {
121 proxyWin = new ProxySet();
125 * \brief Slot assigned to save button click.
127 void ConfigTab::saveSettings() {
128 const char* langs[5] = {"en", "cs", "ro", "pl", "lt"};
129 QSettings settings("config/global.ini", QSettings::IniFormat);
131 settings.setValue("lang", langs[langToSave]);
137 * \brief Slot assigned to download button click.
139 void ConfigTab::downloadLatest() {
140 QSettings settings("config/global.ini", QSettings::IniFormat);
141 useProxy = settings.value("Network/proxyUsed", 0).toInt();
142 butDown->setEnabled(false);
143 msgbox->setText("downloading...");
144 file = new QFile("data/data_"+num+".xml");
145 file->open(QIODevice::WriteOnly);
146 QUrl url("http://www.soccerproject.com/sptd.php");
147 int p_port = settings.value("Network/proxyPort", 80).toInt();
148 if(useProxy) http->setProxy(settings.value("Network/proxyAddr", "").toString(), p_port, settings.value("Network/proxyName", "").toString(), settings.value("Network/proxyPass", "").toString());
149 http->setHost(url.host(), 80);
150 //QTextStream out(stdout); out << "log";
151 httpId = http->get(url.path() + "?login=" + QUrl::toPercentEncoding(nick->text()) + "&pw=" + QUrl::toPercentEncoding(pass->text()) + "&sptdid=8&sptdpw=Cerm12", file);
152 settings.setValue("username", nick->text());
157 * \brief Slot assigned to download finished action.
159 void ConfigTab::httpRequestFinished(int requestId, bool error) {
160 if (requestId != httpId) return;
166 emit errMsgBox(tr("Download failed: %1.").arg(http->errorString()));
168 QFile control("data/data_"+num+".xml");
169 control.open(QIODevice::ReadOnly);
174 QSettings settings("config/global.ini", QSettings::IniFormat);
175 QString parseNum = num;
176 int val = num.toInt();
178 settings.setValue("nextXmlId", num);
180 emit errMsgBox(QString("<span style=\"color:green;\">") + tr("Download OK") + "</span>");
181 parse("data/data_"+parseNum+".xml");
185 control.ungetChar(c);
186 control.readLine(err, sizeof(err));
188 emit errMsgBox(QString("<span style=\"color:red;\">") + tr("Error: ") + err + "</span>");
189 butDown->setEnabled(true);
197 * \brief Slot assigned to QHttp response action.
198 * \param responseHeader QHttp response.
200 void ConfigTab::readResponseHeader(const QHttpResponseHeader &responseHeader) {
201 if (responseHeader.statusCode() != 200) {
202 QMessageBox::information(this, tr("HTTP"), tr("Download failed: %1.").arg(responseHeader.reasonPhrase()));
209 * \brief Slot assigned to errMsgBox signal.
210 * \param text text to show.
212 void ConfigTab::showMsgBox(const QString &text) {
214 msgbox->setText(text);