doxygen comments
[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;
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[5] = {"en", "cs", "ro", "pl", "lt"};
129         QSettings settings("config/global.ini", QSettings::IniFormat);
130         if(langs) {
131                 settings.setValue("lang", langs[langToSave]);
132                 settings.sync();
133         }
134 }
135
136 /**
137  * \brief Slot assigned to download button click.
138  */
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());
153         settings.sync();
154 }
155
156 /**
157  * \brief Slot assigned to download finished action.
158  */
159 void ConfigTab::httpRequestFinished(int requestId, bool error) {
160         if (requestId != httpId) return;
161
162         file->close();
163
164         if (error) {
165                 file->remove();
166                 emit errMsgBox(tr("Download failed: %1.").arg(http->errorString()));
167         } else {
168                 QFile control("data/data_"+num+".xml");
169                 control.open(QIODevice::ReadOnly);
170                 char c;
171                 control.getChar(&c);
172                 if(c == '<') {
173                         control.close();
174                         QSettings settings("config/global.ini", QSettings::IniFormat);
175                         QString parseNum = num;
176                         int val = num.toInt();
177                         num.setNum(++val);
178                         settings.setValue("nextXmlId", num);
179                         settings.sync();
180                         emit errMsgBox(QString("<span style=\"color:green;\">") + tr("Download OK") + "</span>");
181                         parse("data/data_"+parseNum+".xml");
182                         emit dataLoaded();
183                 } else {
184                         char err[512];
185                         control.ungetChar(c);
186                         control.readLine(err, sizeof(err));
187                         control.close();
188                         emit errMsgBox(QString("<span style=\"color:red;\">") + tr("Error: ") + err + "</span>");
189                         butDown->setEnabled(true);
190                 }
191         }
192         delete file;
193         file = 0;
194 }
195
196 /**
197  * \brief Slot assigned to QHttp response action.
198  * \param responseHeader QHttp response.
199  */
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()));
203                 http->abort();
204                 return;
205         }
206 }
207
208 /**
209  * \brief Slot assigned to errMsgBox signal.
210  * \param text text to show.
211  */
212 void ConfigTab::showMsgBox(const QString &text) {
213         msgbox->clear();
214         msgbox->setText(text);
215 }