/
squeezelistner.cpp
134 lines (104 loc) · 3.63 KB
/
squeezelistner.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
/*
Copyright (C) 2010 Michael Rahr
This file is part of SqueezeRemote.
SqueezeRemote is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License.
SqueezeRemote is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SqueezeRemote. If not, see <http://www.gnu.org/licenses/>.
*/
#include "squeezelistner.h"
squeezeListner::squeezeListner(QObject *parent,QString mIpaddr,QString mPortNr)
:QObject ( parent )
{
bool ret;
ret=false;
port_nr=mPortNr;
ip_addr=mIpaddr;
tcpSocket = new QTcpSocket(this);
if (QObject::connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readData1())));
qDebug()<<"Signal ok";
QObject::connect(tcpSocket, SIGNAL(disconnected()),this,SLOT(disConnected()));
tcpSocket->abort();
tcpSocket->connectToHost(ip_addr,port_nr.toInt());
if (tcpSocket->waitForConnected(1000))
{
ret=true;
qDebug("Connected!");
}
}
void squeezeListner::disConnected()
{
tcpSocket->close();
connected=false;
}
squeezeListner::~squeezeListner()
{
if (tcpSocket->isOpen())
tcpSocket->close();
}
bool squeezeListner::connectTo(QString mIpaddr, QString mPortNr)
{
bool ret;
ret=false;
connected=false;
tcpSocket->abort();
tcpSocket->connectToHost(mIpaddr,mPortNr.toInt());
if (tcpSocket->waitForConnected(3000))
{
ret=true;
qDebug("Connected!");
connected=true;
}
return ret;
}
void squeezeListner::readData1()
{
QString text,text1,command;
text="";
while (tcpSocket->canReadLine())
{
text1 = tcpSocket->readLine();
text =text+ text1;
// qDebug()<<"readline="<<text1;
}
// qDebug()<<" ";
// qDebug()<<"Server Data "<<text;
emit sendStatus(text);
}
void squeezeListner::displayError(QAbstractSocket::SocketError socketError)
{
switch (socketError) {
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
// QMessageBox::information(this, tr("Fortune Client"),
// tr("The host was not found. Please check the "
// "host name and port settings."));
break;
case QAbstractSocket::ConnectionRefusedError:
// QMessageBox::information(this, tr("Fortune Client"),
// tr("The connection was refused by the peer. "
// "Make sure the fortune server is running, "
// "and check that the host name and port "
// "settings are correct."));
break;
// QMessageBox::information(this, tr("Fortune Client"),
// tr("The following error occurred: %1.")
// .arg(tcpSocket->errorString()));
default: ;
}
}
void squeezeListner::subscribeTo(QString subscribe)
{
if (tcpSocket->isOpen())
{
qDebug()<<"Sending Data"<< subscribe;
QTextStream stream(tcpSocket);
stream <<subscribe;
}
}