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

Last change on this file since 114 was 112, checked in by Silvan Scherrer, 15 years ago

Smplayer: eol-style

  • Property svn:eol-style set to LF
File size: 4.9 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2010 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
62
63 if (str.toLower() == "hello") {
64 sendText(QString("Hello, this is SMPlayer %1").arg(smplayerVersion()));
65 }
66 else
67 if (str.toLower() == "help") {
68 sendText("Available commands:");
69 sendText(" help");
70 sendText(" quit");
71 sendText(" list functions");
72 sendText(" function [function_name]");
73 sendText(" f [function_name]");
74 sendText(" open [file]");
75 }
76 else
77 if (str.toLower() == "quit") {
78 sendText("Goodbye");
79 socket->disconnectFromHost();
80 }
81 else
82 if (str.toLower() == "list functions") {
83 for (int n=0; n < actions_list.count(); n++) {
84 sendText( actions_list[n] );
85 }
86 }
87 else
88 if (rx_open.indexIn(str) > -1) {
89 QString file = rx_open.cap(1);
90 qDebug("Connection::parseLine: asked to open '%s'", file.toUtf8().data());
91 sendText("OK, file sent to GUI");
92 emit receivedOpen(file);
93 }
94 else
95 if (rx_loadsub.indexIn(str) > -1) {
96 QString file = rx_loadsub.cap(1);
97 qDebug("Connection::parseLine: asked to load subtitle '%s'", file.toUtf8().data());
98 sendText("OK, subtitle file sent to GUI");
99 emit receivedLoadSubtitle(file);
100 }
101 else
102 if ( (str.toLower() == "open_files_start") ||
103 (str.toLower() == "add_files_start") )
104 {
105 files_to_open.clear();
106 sendText("OK, send first file");
107 }
108 else
109 if ( (str.toLower() == "open_files_end") ||
110 (str.toLower() == "add_files_end") )
111 {
112 qDebug("Connection::parseLine: files_to_open:");
113 for (int n=0; n < files_to_open.count(); n++)
114 qDebug("Connection::parseLine: %d: '%s'", n, files_to_open[n].toUtf8().data());
115 sendText("OK, sending files to GUI");
116
117 if (str.toLower() == "open_files_end")
118 emit receivedOpenFiles(files_to_open);
119 else
120 emit receivedAddFiles(files_to_open);
121 }
122 else
123 if (rx_open_files.indexIn(str) > -1) {
124 QString file = rx_open_files.cap(2);
125 qDebug("Connection::parseLine: file: '%s'", file.toUtf8().data());
126 files_to_open.append(file);
127 sendText("OK, file received");
128 }
129 else
130 if (rx_function.indexIn(str) > -1) {
131 QString function = rx_function.cap(2).toLower();
132 qDebug("Connection::parseLine: asked to process function '%s'", function.toUtf8().data());
133 sendText("OK, function sent to GUI");
134 emit receivedFunction(function);
135 }
136 else {
137 sendText("Unknown command");
138 }
139}
140
141
142MyServer::MyServer( QObject * parent ) : QTcpServer(parent)
143{
144 connect(this, SIGNAL(newConnection()), this, SLOT(newConnection_slot()));
145}
146
147bool MyServer::listen( quint16 port ) {
148 return QTcpServer::listen(QHostAddress::LocalHost, port);
149}
150
151void MyServer::newConnection_slot() {
152 Connection * c = new Connection( nextPendingConnection() );
153 c->setActionsList( actionsList() );
154
155 connect(c, SIGNAL(receivedOpen(QString)),
156 this, SIGNAL(receivedOpen(QString)));
157 connect(c, SIGNAL(receivedOpenFiles(QStringList)),
158 this, SIGNAL(receivedOpenFiles(QStringList)));
159 connect(c, SIGNAL(receivedAddFiles(QStringList)),
160 this, SIGNAL(receivedAddFiles(QStringList)));
161 connect(c, SIGNAL(receivedFunction(QString)),
162 this, SIGNAL(receivedFunction(QString)));
163 connect(c, SIGNAL(receivedLoadSubtitle(QString)),
164 this, SIGNAL(receivedLoadSubtitle(QString)));
165}
166
167#include "moc_myserver.cpp"
Note: See TracBrowser for help on using the repository browser.