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
|
/* smplayer, GUI front-end for mplayer.
Copyright (C) 2006-2010 Ricardo Villalba <rvm@escomposlinux.org>
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "myserver.h"
#include "version.h"
#include <QHostAddress>
#include <QRegExp>
Connection::Connection(QTcpSocket * s)
{
socket = s;
//connect(s, SIGNAL(disconnected()), this, SLOT(deleteLater()));
connect(s, SIGNAL(readyRead()), this, SLOT(readData()));
sendText(QString("SMPlayer %1").arg(smplayerVersion()));
sendText("Type help for a list of commands");
}
Connection::~Connection() {
delete socket;
}
void Connection::sendText(QString l) {
qDebug("Connection::sendText: '%s'", l.toUtf8().data());
socket->write( l.toUtf8() + "\r\n" );
socket->flush();
}
void Connection::readData() {
while (socket->canReadLine()) {
QString l = QString::fromUtf8( socket->readLine() );
l.remove( QRegExp("[\r\n]") );
parseLine( l );
}
}
void Connection::parseLine(QString str) {
qDebug("Connection::parseLine: '%s'", str.toUtf8().data());
QRegExp rx_open("^open (.*)");
QRegExp rx_open_files("(^open_files|^add_files) (.*)");
QRegExp rx_function("^(function|f) (.*)");
QRegExp rx_loadsub("^load_sub (.*)");
if (str.toLower() == "hello") {
sendText(QString("Hello, this is SMPlayer %1").arg(smplayerVersion()));
}
else
if (str.toLower() == "help") {
sendText("Available commands:");
sendText(" help");
sendText(" quit");
sendText(" list functions");
sendText(" function [function_name]");
sendText(" f [function_name]");
sendText(" open [file]");
}
else
if (str.toLower() == "quit") {
sendText("Goodbye");
socket->disconnectFromHost();
}
else
if (str.toLower() == "list functions") {
for (int n=0; n < actions_list.count(); n++) {
sendText( actions_list[n] );
}
}
else
if (rx_open.indexIn(str) > -1) {
QString file = rx_open.cap(1);
qDebug("Connection::parseLine: asked to open '%s'", file.toUtf8().data());
sendText("OK, file sent to GUI");
emit receivedOpen(file);
}
else
if (rx_loadsub.indexIn(str) > -1) {
QString file = rx_loadsub.cap(1);
qDebug("Connection::parseLine: asked to load subtitle '%s'", file.toUtf8().data());
sendText("OK, subtitle file sent to GUI");
emit receivedLoadSubtitle(file);
}
else
if ( (str.toLower() == "open_files_start") ||
(str.toLower() == "add_files_start") )
{
files_to_open.clear();
sendText("OK, send first file");
}
else
if ( (str.toLower() == "open_files_end") ||
(str.toLower() == "add_files_end") )
{
qDebug("Connection::parseLine: files_to_open:");
for (int n=0; n < files_to_open.count(); n++)
qDebug("Connection::parseLine: %d: '%s'", n, files_to_open[n].toUtf8().data());
sendText("OK, sending files to GUI");
if (str.toLower() == "open_files_end")
emit receivedOpenFiles(files_to_open);
else
emit receivedAddFiles(files_to_open);
}
else
if (rx_open_files.indexIn(str) > -1) {
QString file = rx_open_files.cap(2);
qDebug("Connection::parseLine: file: '%s'", file.toUtf8().data());
files_to_open.append(file);
sendText("OK, file received");
}
else
if (rx_function.indexIn(str) > -1) {
QString function = rx_function.cap(2).toLower();
qDebug("Connection::parseLine: asked to process function '%s'", function.toUtf8().data());
sendText("OK, function sent to GUI");
emit receivedFunction(function);
}
else {
sendText("Unknown command");
}
}
MyServer::MyServer( QObject * parent ) : QTcpServer(parent)
{
connect(this, SIGNAL(newConnection()), this, SLOT(newConnection_slot()));
}
bool MyServer::listen( quint16 port ) {
return QTcpServer::listen(QHostAddress::LocalHost, port);
}
void MyServer::newConnection_slot() {
Connection * c = new Connection( nextPendingConnection() );
c->setActionsList( actionsList() );
connect(c, SIGNAL(receivedOpen(QString)),
this, SIGNAL(receivedOpen(QString)));
connect(c, SIGNAL(receivedOpenFiles(QStringList)),
this, SIGNAL(receivedOpenFiles(QStringList)));
connect(c, SIGNAL(receivedAddFiles(QStringList)),
this, SIGNAL(receivedAddFiles(QStringList)));
connect(c, SIGNAL(receivedFunction(QString)),
this, SIGNAL(receivedFunction(QString)));
connect(c, SIGNAL(receivedLoadSubtitle(QString)),
this, SIGNAL(receivedLoadSubtitle(QString)));
}
#include "moc_myserver.cpp"
|