forked from piratfm/fmstick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow_http.cpp
236 lines (208 loc) · 8.92 KB
/
mainwindow_http.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "mainwindow.h"
#include "querier.h"
#include "ui_mainwindow.h"
#include "fmtx.h"
#include "logging.h"
#include "requestmapper.h"
#include "httplistener.h"
/******************************************************************
* HTTP server staff
******************************************************************/
void MainWindow::updateHTTPListenerAccess()
{
http_admin_login=ui->lineEdit_sw_remote_login->text();
http_admin_pwd=ui->lineEdit_sw_remote_pwd->text();
}
bool MainWindow::checkHTTPListenerPort()
{
bool ret=FALSE;
int port = ui->spinBox_sw_remote_port->value();
QTcpServer *test_server = new QTcpServer();
test_server->listen(QHostAddress::Any, port);
if (!test_server->isListening()) {
//qCritical("HttpListener: Cannot bind on port %i: %s",port,qPrintable(errorString()));
qDebug("HttpListener: Cannot bind on port %i",port);
ui->spinBox_sw_remote_port->setStyleSheet("background-color: rgb(255,127,127);");
ui->textBrowser_sw_remote_uri->setHtml("<font color=red>this port is not avaliable</font>");
} else {
ui->spinBox_sw_remote_port->setStyleSheet("background-color: rgb(127,255,127);");
qDebug("HttpListener: Listening on port %i",port);
ui->textBrowser_sw_remote_uri->setHtml("<font color=red>this port is avaliable</font>");
ret=TRUE;
//ui->label_check_conn->setText("");
}
test_server->close();
test_server->~QTcpServer();
return ret;
}
void MainWindow::updateHTTPListener()
{
if(listener) {
listener->close();
listener->~HttpListener();
listener=NULL;
}
if(ui->checkBox_sw_remote->isChecked()) {
ui->frame_sw_remoteaccesss->setVisible(true);
if(checkHTTPListenerPort()) {
QSettings* web_cfg=new QSettings();
web_cfg->setValue("port",ui->spinBox_sw_remote_port->value());
/*web_cfg->setValue("minThreads", "1");
web_cfg->setValue("maxThreads", "10");
web_cfg->setValue("cleanupInterval", "1000");
web_cfg->setValue("readTimeout", "60000");
web_cfg->setValue("maxRequestSize", "16000");
web_cfg->setValue("maxMultiPartSize", "1000000");*/
RequestMapper *req_mapper = new RequestMapper();
req_mapper->mw_ptr = this;
listener=new HttpListener(web_cfg,req_mapper,this);
/* getting IP-address */
QString IP_address = "127.0.0.1";
QNetworkConfigurationManager mgr;
QNetworkConfiguration nconfig = mgr.defaultConfiguration();
QNetworkSession session ( nconfig );
QNetworkInterface ninter = session.interface();
// the next statement gives you a funny name on windows
//qDebug() << ninter.name() << endl;
// this gives ip addresses in different sequence, but is is a static method anyhow
// (did not see in first place)
//qDebug() << ninter.allAddresses() << endl;
// this provides two ip addresses (1 ipv4 and 1 ipv6) at least on my machine
QList<QNetworkAddressEntry> laddr = ninter.addressEntries();
for ( QList<QNetworkAddressEntry> ::const_iterator it = laddr.begin(); it != laddr.end(); ++it )
{
QHostAddress address = it->ip();
if(address.protocol() == QAbstractSocket::IPv4Protocol)
IP_address = address.toString();
}
ui->textBrowser_sw_remote_uri->setHtml(
QString("Web-UI link: <a href=\"http://%1:%2/\" target=\"_blank\">http://%1:%2/</a>")
.arg(IP_address)
.arg(ui->spinBox_sw_remote_port->value())
);
//ui->textBrowser_sw_remote_uri->setReadOnly();
connect( req_mapper, SIGNAL(updateItem(QObject *, QString *)),
this, SLOT(updateItemFromHTTPListener(QObject *, QString *)) );
}
} else {
ui->frame_sw_remoteaccesss->setVisible(false);
}
}
bool MainWindow::updateItemFromHTTPListener(QObject *obj, QString *value)
{
QString objTypeName = obj->metaObject()->className();
if(!value) value = new QString("");
qDebug() << "[MAIN] object class name:" << objTypeName << ", name: " << obj->objectName() << "value:" << *value;
if(objTypeName == "QPlainTextEdit") {
QPlainTextEdit *item = (QPlainTextEdit *) obj;
if(item->isReadOnly()){
qDebug() << "[MAIN] trying to set read-only item " << objTypeName << " with name " << obj->objectName();
return FALSE;
}
if(!item->isEnabled()){
qDebug() << "[MAIN] trying to set disabled item " << objTypeName << " with name " << obj->objectName();
return FALSE;
}
QTextDocument doc;
doc.setHtml( QString(*value) );
item->setPlainText(doc.toPlainText());
}
else if(objTypeName == "QLineEdit") {
QLineEdit *item = (QLineEdit *) obj;
if(item->isReadOnly()){
qDebug() << "[MAIN] trying to set read-only item " << objTypeName << " with name " << obj->objectName();
return FALSE;
}
if(!item->isEnabled()){
qDebug() << "[MAIN] trying to set disabled item " << objTypeName << " with name " << obj->objectName();
return FALSE;
}
item->setText(QString(*value));
}
else if(objTypeName == "QCheckBox") {
QCheckBox *item = (QCheckBox *) obj;
if(!item->isEnabled()){
qDebug() << "[MAIN] trying to set disabled item " << objTypeName << " with name " << obj->objectName();
return FALSE;
}
item->setChecked(QString(*value) == "" ? false : true);
}
else if(objTypeName == "QRadioButton") {
QRadioButton *item = (QRadioButton *) obj;
if(!item->isEnabled()){
qDebug() << "[MAIN] trying to set disabled item " << objTypeName << " with name " << obj->objectName();
return FALSE;
}
item->setChecked(QString(*value) == "" ? false : true);
}
else if(objTypeName == "QSpinBox") {
QSpinBox *item = (QSpinBox *) obj;
if(item->isReadOnly()){
qDebug() << "[MAIN] trying to set read-only item " << objTypeName << " with name " << obj->objectName();
return FALSE;
}
if(!item->isEnabled()){
qDebug() << "[MAIN] trying to set disabled item " << objTypeName << " with name " << obj->objectName();
return FALSE;
}
item->setValue(QString(*value).toInt());
}
else if(objTypeName == "QDoubleSpinBox") {
QDoubleSpinBox *item = (QDoubleSpinBox *) obj;
if(item->isReadOnly()){
qDebug() << "[MAIN] trying to set read-only item " << objTypeName << " with name " << obj->objectName();
return FALSE;
}
if(!item->isEnabled()){
qDebug() << "[MAIN] trying to set disabled item " << objTypeName << " with name " << obj->objectName();
return FALSE;
}
item->setValue(QString(*value).toDouble());
}
else if(objTypeName == "QComboBox") {
QComboBox *item = (QComboBox *) obj;
if(!item->isEnabled()){
qDebug() << "[MAIN] trying to set disabled item " << objTypeName << " with name " << obj->objectName();
return FALSE;
}
item->setCurrentIndex(QString(*value).toInt());
}
else if(objTypeName == "QPushButton") {
QPushButton *item = (QPushButton *) obj;
if(!item->isEnabled()){
qDebug() << "[MAIN] trying to set disabled item " << objTypeName << " with name " << obj->objectName();
return FALSE;
}
if(item->objectName() == "pushButton_sw_send_custom_rds")
SetRDSCustomGroup(value);
else
item->click();
}
else if(objTypeName == "QAction") {
QAction *item = (QAction *) obj;
if(!item->isEnabled()){
qDebug() << "[MAIN] trying to set disabled item " << objTypeName << " with name " << obj->objectName();
return FALSE;
}
/* save params for http access */
if(item->objectName() == "actionReset_to_Default") {
bool http_serve = ui->checkBox_sw_remote->isChecked();
int http_port = ui->spinBox_sw_remote_port->value();
QString http_login(ui->lineEdit_sw_remote_login->text());
QString http_password(ui->lineEdit_sw_remote_pwd->text());
item->trigger();
ui->checkBox_sw_remote->setChecked(http_serve);
ui->spinBox_sw_remote_port->setValue(http_port);
ui->lineEdit_sw_remote_login->setText(http_login);
ui->lineEdit_sw_remote_pwd->setText(http_password);
} else {
item->trigger();
}
}
else {
qDebug() << "[MAIN] unimplemented method for " << objTypeName;
return FALSE;
}
qDebug() << "[MAIN] done";
return TRUE;
}