/
hostname.cpp
140 lines (120 loc) · 4.68 KB
/
hostname.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
#include "hostname.h"
//#include "ui_hostname.h"
#include <QFileDialog>
#include <QtNetwork>
//need to verify port is an integer
void Hostname::setDefaultPort()
{
this->lineEditHostport->setText(defaultport);
}
void Hostname::displayError(QAbstractSocket::SocketError socketError)
{
switch (socketError) {
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
textBrowser->setText(tr("Airmar Weather Client"
"The server host was not found. Please check the "
"host name and port settings."));
break;
case QAbstractSocket::ConnectionRefusedError:
textBrowser->setText(tr("Airmar Weather Client"
"The connection was refused by the peer. "
"Make sure the Airmar Weather Server is running, "
"and check that the host name and port "
"settings are correct."));
break;
default:
textBrowser->setText(tr("Airmar Weather Client"
"The following error occurred: %1.")
.arg(tcpSocket->errorString()));
}
// getFortuneButton->setEnabled(true);
}
/*void Hostname::openPort()
{
tcpSocket = new QTcpSocket(this);
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readPort()));
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));
QNetworkConfigurationManager manager;
if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
// Get saved network configuration
QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
settings.beginGroup(QLatin1String("QtNetwork"));
const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();
settings.endGroup();
// If the saved network configuration is not currently discovered use the system default
QNetworkConfiguration config = manager.configurationFromIdentifier(id);
if ((config.state() & QNetworkConfiguration::Discovered) !=
QNetworkConfiguration::Discovered) {
config = manager.defaultConfiguration();
}
networkSession = new QNetworkSession(config, this);
connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened()));
//getFortuneButton->setEnabled(false);
statusLabel->setText(tr("Opening network session."));
networkSession->open();
} networkSession = new QNetworkSession(config, this);
//getFortuneButton->setEnabled(false);
// statusLabel->setText(tr("Opening network session."));
// networkSession->open();
}*/
void Hostname::requestNewData()
{
// getFortuneButton->setEnabled(false);
host=lineEditHostname->text();
port=lineEditHostport->text();
blockSize = 0;
tcpSocket->abort();
tcpSocket->connectToHost(host,port.toInt());
}
void Hostname::readPort()
{
// QDataStream in(tcpSocket);
// in.setVersion(QDataStream::Qt_4_0);
// QObject::connect(tcpSocket, &QTcpSocket::readyRead, [tcpSocket] () {
// qDebug()<< "GOT DATA "<< tcpSocket->readLine());// readAll();
// });
/* if (blockSize == 0) {
if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
return;
in >> blockSize;
}
if (tcpSocket->bytesAvailable() < blockSize)
return;
*/
// QString nextFortune;
// in >> nextFortune;
currentFortune = tcpSocket->readLine();
//if (nextFortune == currentFortune) {
// QTimer::singleShot(0, this, SLOT(requestNewFortune()));
// return;
//}
// currentFortune = nextFortune;
textBrowser->append(currentFortune);
//
// statusLabel->setText(currentFortune);
// getFortuneButton->setEnabled(true);
}
Hostname::Hostname(QWidget *parent, QString ihost, QString iport) :
QDialog(parent), networkSession(0)
// ui(new Ui::Hostname)
{
// ui->
defaultport="23000";
host=ihost;
port=iport;
setupUi(this);
this->lineEditHostname->setText(host);
this->lineEditHostport->setText(port);
this->pushButtonDefaultPort->setText(tr("Default port ")+defaultport);
tcpSocket = new QTcpSocket(this);
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readPort()));
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));
QObject::connect(this->pushButtonDefaultPort,SIGNAL(clicked()),this,SLOT(setDefaultPort()));
QObject::connect(this->pushButtonGetData,SIGNAL(clicked()),this,SLOT(requestNewData()));
}
//Hostname::~Hostname()
//{
// delete ui;
//}