2007-03-29 21:07:43 +00:00
|
|
|
/* This file is part of the KDE project
|
|
|
|
|
Copyright (C) 2007 Alessandro Praduroux <pradu@pradu.it>
|
2007-04-02 20:44:39 +00:00
|
|
|
(C) 2001-2003 by Tim Jansen <tim@tjansen.de>
|
2007-03-29 21:07:43 +00:00
|
|
|
|
|
|
|
|
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; version 2
|
|
|
|
|
of the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2007-03-29 20:54:53 +00:00
|
|
|
#include "krfbserver.h"
|
|
|
|
|
#include "krfbserver.moc"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <QTcpServer>
|
|
|
|
|
#include <QTcpSocket>
|
|
|
|
|
#include <QTimer>
|
2007-04-02 20:44:39 +00:00
|
|
|
#include <QHostInfo>
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QDesktopWidget>
|
2007-03-29 20:54:53 +00:00
|
|
|
|
|
|
|
|
#include <KConfig>
|
|
|
|
|
#include <KGlobal>
|
2007-04-02 20:44:39 +00:00
|
|
|
#include <KUser>
|
|
|
|
|
#include <KLocale>
|
2007-04-04 14:57:27 +00:00
|
|
|
#include <KStaticDeleter>
|
|
|
|
|
#include <KNotification>
|
2007-04-02 20:44:39 +00:00
|
|
|
|
2007-04-04 14:57:27 +00:00
|
|
|
#include "connectioncontroller.h"
|
2007-04-02 20:44:39 +00:00
|
|
|
|
2007-04-04 14:57:27 +00:00
|
|
|
const int DEFAULT_TCP_PORT = 5900;
|
2007-04-02 20:44:39 +00:00
|
|
|
|
2007-04-04 14:57:27 +00:00
|
|
|
static KStaticDeleter<KrfbServer> sd;
|
|
|
|
|
KrfbServer * KrfbServer::_self = 0;
|
|
|
|
|
KrfbServer * KrfbServer::self() {
|
|
|
|
|
if (!_self) sd.setObject(_self, new KrfbServer);
|
|
|
|
|
return _self;
|
|
|
|
|
}
|
2007-03-29 20:54:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
KrfbServer::KrfbServer()
|
|
|
|
|
{
|
2007-04-04 14:57:27 +00:00
|
|
|
kDebug() << "starting " << endl;
|
2007-03-29 20:54:53 +00:00
|
|
|
QTimer::singleShot(0, this, SLOT(startListening()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void KrfbServer::startListening() {
|
|
|
|
|
|
|
|
|
|
KSharedConfigPtr conf = KGlobal::config();
|
|
|
|
|
KConfigGroup tcpConfig(conf, "TCP");
|
|
|
|
|
|
|
|
|
|
int port = tcpConfig.readEntry("port",DEFAULT_TCP_PORT);
|
|
|
|
|
|
2007-04-04 14:57:27 +00:00
|
|
|
_server = new TcpServer(this);
|
|
|
|
|
connect(_server,SIGNAL(connectionReceived(int)),SLOT(newConnection(int)));
|
2007-03-29 20:54:53 +00:00
|
|
|
|
|
|
|
|
if (!_server->listen(QHostAddress::Any, port)) {
|
2007-03-30 21:54:18 +00:00
|
|
|
// TODO: handle error more gracefully
|
2007-03-29 20:54:53 +00:00
|
|
|
kDebug() << "server listen error" << endl;
|
|
|
|
|
deleteLater();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2007-04-04 14:57:27 +00:00
|
|
|
kDebug() << "server listening on port " << DEFAULT_TCP_PORT << endl;
|
2007-03-29 20:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
KrfbServer::~KrfbServer()
|
|
|
|
|
{
|
|
|
|
|
//delete _controller;
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-04 14:57:27 +00:00
|
|
|
void KrfbServer::newConnection(int fdNum)
|
2007-03-29 20:54:53 +00:00
|
|
|
{
|
2007-04-04 14:57:27 +00:00
|
|
|
// TODO: get peer address
|
2007-04-02 20:44:39 +00:00
|
|
|
startServer(fdNum);
|
2007-03-29 20:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void KrfbServer::enableDesktopControl(bool enable)
|
|
|
|
|
{
|
|
|
|
|
// TODO
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void KrfbServer::disconnectAndQuit()
|
|
|
|
|
{
|
|
|
|
|
// TODO: cleanup of existing connections
|
|
|
|
|
_server->close();
|
|
|
|
|
emit quitApp();
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-04 14:57:27 +00:00
|
|
|
|
2007-04-02 20:44:39 +00:00
|
|
|
void KrfbServer::startServer(int fd)
|
|
|
|
|
{
|
2007-04-04 14:57:27 +00:00
|
|
|
ConnectionController *cc = new ConnectionController(fd, this);
|
2007-04-05 17:16:17 +00:00
|
|
|
cc->run();
|
2007-04-04 14:57:27 +00:00
|
|
|
}
|
2007-04-02 20:44:39 +00:00
|
|
|
|
2007-04-04 14:57:27 +00:00
|
|
|
TcpServer::TcpServer(QObject * parent)
|
|
|
|
|
:QTcpServer(parent)
|
|
|
|
|
{
|
|
|
|
|
}
|
2007-04-02 20:44:39 +00:00
|
|
|
|
2007-04-04 14:57:27 +00:00
|
|
|
void TcpServer::incomingConnection(int fd)
|
|
|
|
|
{
|
|
|
|
|
emit connectionReceived(fd);
|
|
|
|
|
}
|
2007-04-02 20:44:39 +00:00
|
|
|
|
2007-04-04 14:57:27 +00:00
|
|
|
void KrfbServer::handleNotifications(QString name, QString desc )
|
|
|
|
|
{
|
|
|
|
|
KNotification::event(name, desc);
|
|
|
|
|
}
|
2007-04-02 20:44:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-03-29 20:54:53 +00:00
|
|
|
|