source: smplayer/trunk/src/myserver.cpp@ 119

Last change on this file since 119 was 119, checked in by Silvan Scherrer, 14 years ago

SMPlayer: latest svn update

  • Property svn:eol-style set to LF
File size: 8.5 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2011 Ricardo Villalba <rvm@escomposlinux.org>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/
18
19#include "myserver.h"
20#include "version.h"
21#include <QHostAddress>
22#include <QRegExp>
23
24Connection::Connection(QTcpSocket * s)
25{
26 socket = s;
27
28 //connect(s, SIGNAL(disconnected()), this, SLOT(deleteLater()));
29 connect(s, SIGNAL(readyRead()), this, SLOT(readData()));
30
31 sendText(QString("SMPlayer %1").arg(smplayerVersion()));
32 sendText("Type help for a list of commands");
33}
34
35Connection::~Connection() {
36 delete socket;
37}
38
39void Connection::sendText(QString l) {
40 qDebug("Connection::sendText: '%s'", l.toUtf8().data());
41
42 socket->write( l.toUtf8() + "\r\n" );
43 socket->flush();
44}
45
46void Connection::readData() {
47 while (socket->canReadLine()) {
48 QString l = QString::fromUtf8( socket->readLine() );
49 l.remove( QRegExp("[\r\n]") );
50 parseLine( l );
51 }
52}
53
54void Connection::parseLine(QString str) {
55 qDebug("Connection::parseLine: '%s'", str.toUtf8().data());
56
57 QRegExp rx_open("^open (.*)");
58 QRegExp rx_open_files("(^open_files|^add_files) (.*)");
59 QRegExp rx_function("^(function|f) (.*)");
60 QRegExp rx_loadsub("^load_sub (.*)");
61 QRegExp rx_playItem("^play item ([0-9]+)");
62 QRegExp rx_moveItem("^move item ([0-9]+) ([+-]?[0-9]+)");
63 QRegExp rx_removeItem("^remove item (\\*|[0-9]+)");
64 QRegExp rx_seek("^seek ([0-9]*\\.*[0-9]*)");
65 QRegExp rx_get("^get (.*)");
66 QRegExp rx_setVol("^set volume ([0-9]+)");
67
68 if (str.toLower() == "hello") {
69 sendText(QString("Hello, this is SMPlayer %1").arg(smplayerVersion()));
70 }
71 else
72 if (str.toLower() == "help") {
73 sendText("Available commands:");
74 sendText(" help");
75 sendText(" quit");
76 sendText(" list functions");
77 sendText(" function [function_name]");
78 sendText(" f [function_name]");
79 sendText(" open [file]");
80 sendText(" open_files_start \\n open_files [file1] \\n open_files [file2] \\n ... open_files_end");
81 sendText(" add_files_start \\n add_files [file1] \\n add_files [file2] \\n ... add_files_end");
82 sendText(" remove item [index]");
83 sendText(" play item [index]");
84 sendText(" move item [index] [shift]");
85 sendText(" view playlist");
86 sendText(" view status");
87 sendText(" view clip info");
88 sendText(" seek [position]");
89 sendText(" get [action]");
90 sendText(" get volume");
91 sendText(" set volume [value]");
92 }
93 else
94 if (str.toLower() == "quit") {
95 sendText("Goodbye");
96 socket->disconnectFromHost();
97 }
98 else
99 if (str.toLower() == "list functions") {
100 for (int n=0; n < actions_list.count(); n++) {
101 sendText( actions_list[n] );
102 }
103 }
104 else
105 if (rx_playItem.indexIn(str) > -1) {
106 QString index = rx_playItem.cap(1);
107 qDebug("Connection::parseLine: asked to play file #%s.", index.toUtf8().data());
108 sendText("OK, command sent to GUI.");
109 emit receivedPlayItem(index.toInt());
110 }
111 else
112 if (rx_removeItem.indexIn(str) > -1) {
113 QString index = rx_removeItem.cap(1);
114 qDebug("Connection::parseLine: asked to remove file %s.", index.toUtf8().data());
115 sendText("OK, command sent to GUI.");
116 emit receivedRemoveItem(index == "*" ? -1 : index.toInt());
117 }
118 else
119 if (rx_moveItem.indexIn(str) > -1) {
120 QString index = rx_moveItem.cap(1);
121 QString shift = rx_moveItem.cap(2);
122 qDebug("Connection::parseLine: asked to move file %s %s.", index.toUtf8().data(), shift.toUtf8().data());
123 sendText("OK, command sent to GUI.");
124 emit receivedMoveItem(index.toInt(), shift.toInt());
125 }
126 else
127 if (rx_open.indexIn(str) > -1) {
128 QString file = rx_open.cap(1);
129 qDebug("Connection::parseLine: asked to open '%s'", file.toUtf8().data());
130 sendText("OK, file sent to GUI");
131 emit receivedOpen(file);
132 }
133 else
134 if (rx_loadsub.indexIn(str) > -1) {
135 QString file = rx_loadsub.cap(1);
136 qDebug("Connection::parseLine: asked to load subtitle '%s'", file.toUtf8().data());
137 sendText("OK, subtitle file sent to GUI");
138 emit receivedLoadSubtitle(file);
139 }
140 else
141 if ( (str.toLower() == "open_files_start") ||
142 (str.toLower() == "add_files_start") )
143 {
144 files_to_open.clear();
145 sendText("OK, send first file");
146 }
147 else
148 if ( (str.toLower() == "open_files_end") ||
149 (str.toLower() == "add_files_end") )
150 {
151 qDebug("Connection::parseLine: files_to_open:");
152 for (int n=0; n < files_to_open.count(); n++)
153 qDebug("Connection::parseLine: %d: '%s'", n, files_to_open[n].toUtf8().data());
154 sendText("OK, sending files to GUI");
155
156 if (str.toLower() == "open_files_end")
157 emit receivedOpenFiles(files_to_open);
158 else
159 emit receivedAddFiles(files_to_open);
160 }
161 else
162 if (rx_open_files.indexIn(str) > -1) {
163 QString file = rx_open_files.cap(2);
164 qDebug("Connection::parseLine: file: '%s'", file.toUtf8().data());
165 files_to_open.append(file);
166 sendText("OK, file received");
167 }
168 else
169 if (rx_function.indexIn(str) > -1) {
170 QString function = rx_function.cap(2).toLower();
171 qDebug("Connection::parseLine: asked to process function '%s'", function.toUtf8().data());
172 sendText("OK, function sent to GUI");
173 emit receivedFunction(function);
174 }
175 else
176 if(str.toLower() == "get volume"){
177 int output = 0;
178 emit receivedGetVolume(&output);
179 sendText(QString::number(output));
180
181 }
182 else
183 if(rx_setVol.indexIn(str) > -1){
184 int value = rx_setVol.cap(1).toInt();
185 emit receivedSetVolume(value);
186
187 }
188 else
189 if(str.toLower() == "view playlist"){
190 QString output = "";
191 emit receivedViewPlaylist(&output);
192 sendText(output);
193
194 }
195 else
196 if(str.toLower() == "view status"){
197 QString output = "";
198 emit receivedViewStatus(&output);
199 sendText(output);
200
201 }
202 else
203 if(str.toLower() == "view clip info"){
204 QString output = "";
205 emit receivedViewClipInfo(&output);
206 sendText(output);
207
208 }
209 else
210 if(rx_seek.indexIn(str) > -1){
211 qDebug("Connection::parseLine: asked to seek to %s", rx_seek.cap(1).toUtf8().data());
212 emit receivedSeek(rx_seek.cap(1).toDouble());
213 }
214 else
215 if(rx_get.indexIn(str) > -1){
216 qDebug("Connection::parseLine: asked to get state of action '%s'", rx_get.cap(1).toUtf8().data());
217 QString value = "";
218 emit receivedGetChecked(rx_get.cap(1), &value);
219 sendText(value);
220 }
221 else {
222 sendText("Unknown command");
223 }
224}
225
226
227MyServer::MyServer( QObject * parent ) : QTcpServer(parent)
228{
229 connect(this, SIGNAL(newConnection()), this, SLOT(newConnection_slot()));
230}
231
232bool MyServer::listen( quint16 port ) {
233 return QTcpServer::listen(QHostAddress::LocalHost, port);
234}
235
236void MyServer::newConnection_slot() {
237 Connection * c = new Connection( nextPendingConnection() );
238 c->setActionsList( actionsList() );
239
240 connect(c, SIGNAL(receivedPlayItem(int)),
241 this, SIGNAL(receivedPlayItem(int)));
242 connect(c, SIGNAL(receivedRemoveItem(int)),
243 this, SIGNAL(receivedRemoveItem(int)));
244 connect(c, SIGNAL(receivedOpen(QString)),
245 this, SIGNAL(receivedOpen(QString)));
246 connect(c, SIGNAL(receivedOpenFiles(QStringList)),
247 this, SIGNAL(receivedOpenFiles(QStringList)));
248 connect(c, SIGNAL(receivedAddFiles(QStringList)),
249 this, SIGNAL(receivedAddFiles(QStringList)));
250 connect(c, SIGNAL(receivedFunction(QString)),
251 this, SIGNAL(receivedFunction(QString)));
252 connect(c, SIGNAL(receivedLoadSubtitle(QString)),
253 this, SIGNAL(receivedLoadSubtitle(QString)));
254 connect(c, SIGNAL(receivedViewPlaylist(QString*)),
255 this, SIGNAL(receivedViewPlaylist(QString*)));
256 connect(c, SIGNAL(receivedViewStatus(QString*)),
257 this, SIGNAL(receivedViewStatus(QString*)));
258 connect(c, SIGNAL(receivedViewClipInfo(QString*)),
259 this, SIGNAL(receivedViewClipInfo(QString*)));
260 connect(c, SIGNAL(receivedSeek(double)),
261 this, SIGNAL(receivedSeek(double)));
262 connect(c, SIGNAL(receivedGetChecked(QString,QString*)),
263 this, SIGNAL(receivedGetChecked(QString,QString*)));
264 connect(c, SIGNAL(receivedMoveItem(int,int)),
265 this, SIGNAL(receivedMoveItem(int,int)));
266 connect(c, SIGNAL(receivedGetVolume(int*)),
267 this, SIGNAL(receivedGetVolume(int*)));
268 connect(c, SIGNAL(receivedSetVolume(int)),
269 this, SIGNAL(receivedSetVolume(int)));
270}
271
272#include "moc_myserver.cpp"
Note: See TracBrowser for help on using the repository browser.