-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote.cpp
217 lines (199 loc) · 5.13 KB
/
remote.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
#include "remote.h"
Remote::Remote(QObject *parent) :
QTcpServer(parent)
{
local = new QLocalServer();
connect(this,SIGNAL(newConnection()),SLOT(con()));
connect(local,SIGNAL(newConnection()),SLOT(localcon()));
local->listen("iniTunes");
listen(QHostAddress::Any,8080);
QTcpServer *server = new QTcpServer();
connect(server,SIGNAL(newConnection()),SLOT(con()));
server->listen(QHostAddress::Any,serverPort()+1);
}
void Remote::con()
{
QTcpServer *server = qobject_cast<QTcpServer*>(sender());
//server->disconnect(SIGNAL(newConnection()));
QTcpSocket *socket = server->nextPendingConnection();
QTextStream text(socket);
//text << "RMusic"<<endl;
sockets<<socket;
int sport = serverPort();
int port = socket->localPort();
if(port == sport)
{
qDebug()<<"Connected to 5000:"<<connect(socket,SIGNAL(readyRead()),SLOT(receive()));
}
else//if(port == sport +1)
{
qDebug()<<"Connected to 5001:"<<connect(socket,SIGNAL(readyRead()),SLOT(seekReceive()));
}
connect(socket,SIGNAL(disconnected()),SLOT(removeSocket()));
}
void Remote::receive()
{
QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
int action = socket->readAll().toInt();//socket->readAll().toInt();
qDebug()<<"Action received:"<<action;
QTextStream text(socket);
QString read;
switch(action)
{
case PLAY:
emit play_pause();
break;
case NEXT:
emit next();
break;
case PREVIOUS:
emit previous();
break;
case REWIND:
emit rewin();
break;
case FORWARD:
emit forward();
break;
case VOLUMEDOWN:
emit volume(false);
break;
case VOLUMEUP:
emit volume(true);
break;
case RANDOMON:
qDebug()<<"Random";
emit random(true);
break;
case RANDOMOFF:
qDebug()<<"Random";
emit random(false);
break;
case REPEATON:
emit repeat(true);
break;
case REPEATOFF:
emit repeat(false);
break;
case QUIT:
qDebug()<<"Quit";
emit quit();
break;
case UP:
qDebug()<<"Up";
emit up();
break;
case DOWN:
qDebug()<<"Down";
emit down();
break;
case OK:
emit ok();
break;
case LIBRARY:
qDebug()<<"Library";
text << "li\n"<<endl;
text << songlist.count()+"\n"<<endl;
foreach(Song song, songlist)
{
qDebug()<<"Sending a song";
text << song.title+"///"+song.length+"///"+song.album+"///"+song.artist+"///"+song.genre<<endl;
Sleep(100);
}
text << "il"<<endl;
qDebug()<<"Songs sent";
break;
case SEARCH:
read = socket->readLine();
emit newQuery(read);
break;
case SONG:
socket->waitForReadyRead();
QString song = socket->readLine();
emit songChosen(song.toInt());
break;
}
}
void Remote::seekCon()
{
//QTcpSocket *socket = qobject_cast<QTcpServer*>(sender());
}
void Remote::sendSeek(qint64 time)
{
foreach (QTcpSocket *socket, sockets)
{
if(socket->localPort()==serverPort()+1)
{
QTextStream text(socket);
text << QString::number(time)<<endl;
}
}
}
void Remote::newTrack(const Song & song)
{
foreach (QTcpSocket *socket, sockets)
{
if(socket->localPort()!=serverPort()+1)
{
QTextStream text(socket);
text <<"New Song"<<endl<<song.title<<endl<<song.album<<endl<<song.artist<<endl<<song.genre<<endl<<song.length<<endl;
}
}
}
void Remote::removeSocket()
{
QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());
sockets.removeOne(socket);
}
void Remote::repeatChanged(QMediaPlaylist::PlaybackMode mode)
{
foreach (QTcpSocket *socket, sockets)
{
if(socket->localPort()!=serverPort()+1)
{
QTextStream text(socket);
text << "rep"<<endl<<mode<<endl;
}
}
}
void Remote::localcon()
{
QLocalSocket *socket = local->nextPendingConnection();
connect(socket,SIGNAL(readyRead()),SLOT(localreceive()));
}
void Remote::localreceive()
{
qDebug()<<"Action received";
QLocalSocket *socket = qobject_cast<QLocalSocket *>(sender());
QTextStream stream(socket);
int action = stream.readLine().toInt();
switch(action)
{
case PLAY:
qDebug()<<PLAY;
QString filename = stream.readLine();
emit open(Library::getSong(filename));
break;
}
}
void Remote::seekReceive()
{
QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
QTextStream text(socket);
QString stime;
text >> stime;
emit seek(stime.toInt());
}
void Remote::randomChanged(bool ch)
{
qDebug()<<"Random changed";
foreach (QTcpSocket *socket, sockets)
{
if(socket->localPort()!=serverPort()+1)
{
QTextStream text(socket);
QString rep = ch?"true":"false";
text << "ran"<<endl<<rep<<endl;
}
}
}