/*
# Copyright (c) 2009 - OpenSLX Project, Computer Center University of Freiburg
#
# This program is free software distributed under the GPL version 2.
# See http://openslx.org/COPYING
#
# If you have any feedback please consult http://openslx.org/feedback and
# send your suggestions, praise, or complaints to feedback@openslx.org
#
# General information about OpenSLX can be found at http://openslx.org/
# -----------------------------------------------------------------------------
# src/gui/connectionWindow.cpp
# -----------------------------------------------------------------------------
*/
#include "connectionWindow.h"
#include <src/core/pvsConnectionManager.h>
#include <src/gui/mainWindow.h>
ConnectionWindow::ConnectionWindow(QWidget *parent) :
QWidget(parent) {
//initialize the position for the frame in the clientView
posX = 4;
posY = 4;
_closeupFrame = NULL;
hasDozent = false; // at the begin we don't have a dozent machine
QPalette newPalette = palette();
newPalette.setColor(QPalette::Window, Qt::white);
setPalette(newPalette);
newDummy = new QAction(tr("&Add a dummy..."), this); // set a dummy
connect(newDummy, SIGNAL(triggered()), this, SLOT(addDummy()));
menu = new QMenu(this);
menu->addAction(newDummy);
setAcceptDrops(true); //drag&drop should be enabled
setStyleSheet("background-color: #DCDCDC;");
}
ConnectionWindow::~ConnectionWindow() {
//
}
void ConnectionWindow::addFrame(ConnectionFrame *cF) {
bool found = false;
foreach (ConnectionFrame* cFr, AllFrameOnWindow)
{
if (QString::compare(cF->getTaskbarTitle(), cFr->getTaskbarTitle(), Qt::CaseInsensitive) == 0)
{
QPoint cuP = currentPosition(cFr);
cF->move(cuP);
AllFrameOnWindow.append(cF);
AllFrameOnWindow.removeOne(cFr); //we don't need this dummy any more
cFr->deleteLater();
found = true;
return;
}
}
/* If the Frame don't belong to the profile
* we have to move it at the default position for now.
* The user can move this later to his favorite position.
*/
if (!found)
{
foreach (clientLocation cur, ClientLocationList)
{
if (QString::compare(cF->getTaskbarTitle(), cur.first, Qt::CaseInsensitive) == 0)
{
cF->move(cur.second);
found = true;
return;
}
}
}
if (!found)
cF->move(10, 10);
}
void ConnectionWindow::addFrameBySettings(QString client, QPoint pos) {
clientLocation cur(client, pos);
ClientLocationList.append(cur);
}
void ConnectionWindow::showFrameFromSettings() {
foreach (clientLocation cur, ClientLocationList)
{
ConnectionFrame* dummy = new ConnectionFrame(
MainWindow::getConnectionWindow());
AllFrameOnWindow.append(dummy);
dummy->setTheTitle("[No name]");
dummy->setTaskbarTitle(cur.first);
dummy->setDummy(true);
dummy->move(cur.second);
}
}
void ConnectionWindow::addDummy() { //function to add a dummy
ConnectionFrame* dummy = new ConnectionFrame(
MainWindow::getConnectionWindow());
dummy->setTheTitle("[No name]");
dummy->setTaskbarTitle("[0.0.0.0]");
QPoint p = QPoint(10, 10);//the default move position, when created a dummy screen
AllFrameOnWindow.append(dummy);
clientLocation cur("[No name]", p);
ClientLocationList.append(cur);
dummy->setDummy(true);
dummy->move(p);
dummy->setStyleSheet(QString::fromUtf8(
"background-color: rgb(150, 150, 168);"));
}
void ConnectionWindow::mousePressEvent(QMouseEvent *event) { //catch the mouse press event
if (event->button() == Qt::RightButton)
menu->exec(QCursor::pos());
}
QPoint ConnectionWindow::currentPosition(ConnectionFrame* cF) {
QPoint pos = QPoint(10, 10);
clientLocation cur;
foreach (cur, ClientLocationList)
{
if (QString::compare(cF->getTaskbarTitle(), cur.first, Qt::CaseInsensitive) == 0)
return cur.second;
}
return pos;
}
int ConnectionWindow::itemAt(ConnectionFrame* cf)
{
for (int i = AllFrameOnWindow.size() - 1; i >= 0; --i)
{
ConnectionFrame* item = AllFrameOnWindow.at(i);
if (cf->pos().x() == item->pos().x() && cf->pos().y() == item->pos().y())
return i;
}
return -1;
}
void ConnectionWindow::removeFromList(ConnectionFrame* cF) {
//dummyList.removeOne(cF);
if(AllFrameOnWindow.contains(cF))
AllFrameOnWindow.removeOne(cF);
cF->deleteLater();
}
void ConnectionWindow::onChangeConnections() {
//clear the view
if (!frameList.empty()) {
ConnectionFrame* tmpIt;
foreach (tmpIt, frameList)
{
if (tmpIt != currentSingleFrame)
tmpIt->setActive(false);
tmpIt->hide();
}
}
}
bool ConnectionWindow::projectStations(QString sname) {
QString password, hostname;
int port, quality;
ConsoleLog writeLine(QString("Projecting from: ").append(sname));
//TODO add method parameter for this
quality = 0;
QList<QString> projectList =
MainWindow::getConnectionList()->getTargetForTheProject(sname);
if (!projectList.isEmpty()) {
PVSClient* tmpSrcConnection =
PVSConnectionManager::getManager()->getClientFromIp(sname);
if (tmpSrcConnection) {
if (!tmpSrcConnection->getVNCAllowed()) {
ConsoleLog writeError(QString("VNC is not allowed on Projection Source: ").append(sname));
ConsoleLog writeError(QString("Trying to start VNC-Server on Client: ").append(sname));
tmpSrcConnection->setProject(true);
return false;
}
password = tmpSrcConnection->getPassword();
port = tmpSrcConnection->getPort();
hostname = sname;
tmpSrcConnection->getConnectionFrame()->setSource();
ConsoleLog writeError (QString("port: ").append(int2String(port)));
ConsoleLog writeError (QString("passwort: ").append(password));
ConsoleLog writeError (QString("pr-sourcename: ").append(sname));
}
else
{
return false;
}
ConsoleLog writeError (QString("port as int: ").append(port));
ConsoleLog writeError (QString("port as String: ").append(int2String(port)));
QString item;
foreach (item, projectList)
{
PVSClient* tmpConnection = PVSConnectionManager::getManager()->getClientFromIp(item);
if (tmpConnection)
{
ConsoleLog writeError (QString("sending vnc data to: ").append(tmpConnection->getIp()));
tmpConnection->sendMessage(PVSCOMMAND,"PROJECT", hostname + " " + int2String(port) + " " + password + " " + int2String(quality));
tmpConnection->getConnectionFrame()->setTarget();
}
else
{
// scream in agony
}
}
}
return true;
}
bool ConnectionWindow::remoteHelp(QString sname) {
//TODO Finish this...
QString password, hostname;
int port;
PVSClient* tmpSrcConnection =
PVSConnectionManager::getManager()->getClientFromIp(sname);
QList<QString> projectList =
MainWindow::getConnectionList()->getTargetForTheProject(sname);
if (tmpSrcConnection) {
if (!tmpSrcConnection->getVNCAllowed()) {
tmpSrcConnection->requestVNCConnect();
tmpSrcConnection->setProject(true);
return false;
}
password = tmpSrcConnection->getRWPassword();
port = tmpSrcConnection->getPort();
hostname = sname;
tmpSrcConnection->getConnectionFrame()->setSource();
} else {
return false;
}
QString item;
foreach (item, projectList)
{
PVSClient
* tmpConnection =
PVSConnectionManager::getManager()->getClientFromIp(item);
if (tmpConnection) {
ConsoleLog writeError(QString("sending vnc data to: ").append(
tmpConnection->getIp()));
tmpConnection->sendMessage(PVSCOMMAND, "PROJECT", hostname
+ " " + int2String(port) + " " + password);
tmpConnection->getConnectionFrame()->setTarget();
} else {
// scream in agony
}
}
return true;
}
bool ConnectionWindow::lockStations() {
if (std::list<QString>* lockList = MainWindow::getConnectionList()->getSelectedClients()) {
if (!lockList->empty()) {
for (std::list<QString>::iterator tmpIt = lockList->begin(); tmpIt
!= lockList->end(); tmpIt++) {
PVSClient
* tmpConnection =
PVSConnectionManager::getManager()->getClientFromIp(
(*tmpIt).toUtf8().data());
if (tmpConnection->getConnectionFrame()->getFrame())
{
// we don't want to lock the dozent machine
if (tmpConnection && !tmpConnection->getConnectionFrame()->getFrame()->isDozent()
&& !tmpConnection->getConnectionFrame()->isOnProjekt())
lockStation(tmpConnection);
else {
// scream in agony
}
}
}
}
delete lockList;
}
return true;
}
void ConnectionWindow::lockStation(PVSClient* pvsCon) {
if (pvsCon->getVNCConnection())
{
pvsCon->getConnectionFrame()->getFrame()->setLockStatus(true);
// we call the update to force Qt to paint the lockicon on the frame
pvsCon->getConnectionFrame()->getFrame()->update();
pvsCon->sendMessage(PVSCOMMAND, "LOCKSTATION", "");
}
}
bool ConnectionWindow::unlockStations() {
if (std::list<QString>* unlockList = MainWindow::getConnectionList()->getSelectedClients()) {
if (!unlockList->empty()) {
for (std::list<QString>::iterator tmpIt = unlockList->begin(); tmpIt
!= unlockList->end(); tmpIt++) {
PVSClient
* tmpConnection =
PVSConnectionManager::getManager()->getClientFromIp(
(*tmpIt).toUtf8().data());
if (tmpConnection && tmpConnection->getConnectionFrame()->getFrame() &&
tmpConnection->getVNCConnection()) {
tmpConnection->getConnectionFrame()->getFrame()->setLockStatus(false);
tmpConnection->getConnectionFrame()->getFrame()->update(); // we call the update to force Qt to paint the lock on the frame
tmpConnection->sendMessage(PVSCOMMAND, "UNLOCKSTATION", "");
} else {
// scream in agony
}
}
}
delete unlockList;
}
return true;
}
void ConnectionWindow::unlockStation(PVSClient* pvsCon) {
if (pvsCon->getVNCConnection())
{
pvsCon->getConnectionFrame()->getFrame()->setLockStatus(false);
// we call the update to force Qt to paint the lockicon on the frame
pvsCon->getConnectionFrame()->getFrame()->update();
pvsCon->sendMessage(PVSCOMMAND, "UNLOCKSTATION", "");
}
}
bool ConnectionWindow::lockAllStations()
{
if (!hasDozent)
{
QString message = QString(tr("You have to set a Superclient-machine before performing this action."));
QMessageBox::information(this, "PVS", message);
return false;
}
std::list<PVSClient*> listAll =
PVSConnectionManager::getManager()->getConnections();
for (std::list<PVSClient*>::iterator it = listAll.begin(); it
!= listAll.end(); it++)
{
if (*it == NULL || (*it)->getConnectionFrame() == NULL) continue;
// we don't want to lock the superclient machine
if ((*it)->getConnectionFrame()->getFrame() &&
!(*it)->getConnectionFrame()->getFrame()->isDozent() &&
(*it)->getVNCConnection())
(*it)->sendMessage(PVSCOMMAND, "LOCKSTATION", "");
else if (!(*it)->getConnectionFrame()->getFrame())
ConsoleLog writeError(QString("The Frame connection from client: ").
append((*it)->getConnectionFrame()->getTaskbarTitle()).
append(QString(" is corrupted. Reconned the client it again.")));
}
return true;
}
bool ConnectionWindow::unlockAllStations() {
std::list<PVSClient*> listAll =
PVSConnectionManager::getManager()->getConnections();
for (std::list<PVSClient*>::iterator it = listAll.begin(); it
!= listAll.end(); it++)
{
if ((*it)->getVNCConnection())
{
(*it)->getConnectionFrame()->getFrame()->setLockStatus(false);
(*it)->getConnectionFrame()->getFrame()->update(); // we call the update to force Qt to repaint the frame
(*it)->sendMessage(PVSCOMMAND, "UNLOCKSTATION", "");
}
// otherwise this will get scattered all over the place
}
return true;
}
bool ConnectionWindow::unprojectStations(QString sname) {
PVSClient* tmpConnection =
PVSConnectionManager::getManager()->getClientFromIp(sname);
if (tmpConnection)
{
tmpConnection->sendMessage(PVSCOMMAND, "UNPROJECT", "");
tmpConnection->getConnectionFrame()->setUnproject();
} else {
// scream in agony
}
return true;
}
bool ConnectionWindow::unprojectAllStations() {
std::list<PVSClient*> listAll =
PVSConnectionManager::getManager()->getConnections();
for (std::list<PVSClient*>::iterator it = listAll.begin(); it
!= listAll.end(); it++)
{
(*it)->sendMessage(PVSCOMMAND, "UNPROJECT", "");
(*it)->getConnectionFrame()->setUnproject();
}
return true;
}
bool ConnectionWindow::lockInvertStations() {
// ok, this method is highly imperformant i guess
// we're going thru all existing connections, and if they dont equal one of the selected (another loop)
// we lock them...
if (std::list<QString>* lockList = MainWindow::getConnectionList()->getSelectedClients()) {
std::list<PVSClient*> listAll =
PVSConnectionManager::getManager()->getConnections();
bool skip = false;
for (std::list<PVSClient*>::iterator it = listAll.begin(); it
!= listAll.end(); it++) {
if (!lockList->empty()) {
for (std::list<QString>::iterator tmpIt = lockList->begin(); tmpIt
!= lockList->end(); tmpIt++) {
PVSClient
* tmpConnection =
PVSConnectionManager::getManager()->getClientFromIp(
(*tmpIt).toUtf8().data());
if (tmpConnection == (*it)) {
skip = true;
break;
}
}
}
if (!skip)
(*it)->sendMessage(PVSCOMMAND, "LOCKSTATION", "");//TODO define some standard to grab this from...
// otherwise this will get scattered all over the place
}
delete lockList;
}
return true;
}
void ConnectionWindow::lockInvertStation(PVSClient* pvsCon) {
pvsCon->sendMessage(PVSCOMMAND, "LOCKSTATION", "");
}
bool ConnectionWindow::messageStations(QString ident, QString message) {
if (std::list<QString>* messageList = MainWindow::getConnectionList()->getSelectedClients()) {
if (!messageList->empty()) {
for (std::list<QString>::iterator tmpIt = messageList->begin(); tmpIt
!= messageList->end(); tmpIt++) {
PVSClient
* tmpConnection =
PVSConnectionManager::getManager()->getClientFromIp(
(*tmpIt).toUtf8().data());
// we don't want to lock the dozent machine
if (tmpConnection && tmpConnection->getConnectionFrame()->getFrame() &&
!tmpConnection->getConnectionFrame()->getFrame()->isDozent())
{
messageStation(ident, message, tmpConnection);
} else {
// scream in agony
}
}
}
delete messageList;
}
return true;
}
void ConnectionWindow::messageStation(QString ident, QString message,
PVSClient* pvsCon) {
pvsCon->sendMessage(PVSMESSAGE, ident, message);
}
/*
* to send COMMAND instead of PVSMESSaGE
*/
bool ConnectionWindow::commandStations(QString ident, QString message) {
if (std::list<QString>* messageList = MainWindow::getConnectionList()->getSelectedClients()) {
if (!messageList->empty()) {
for (std::list<QString>::iterator tmpIt = messageList->begin(); tmpIt
!= messageList->end(); tmpIt++) {
PVSClient
* tmpConnection =
PVSConnectionManager::getManager()->getClientFromIp(
(*tmpIt).toUtf8().data());
// we don't want to lock the dozent machine
if (tmpConnection && tmpConnection->getConnectionFrame()->getFrame() &&
!tmpConnection->getConnectionFrame()->getFrame()->isDozent())
{
//messageStation(ident, message, tmpConnection);
tmpConnection->sendMessage(PVSCOMMAND, ident, message);
} else {
// scream in agony
}
}
}
delete messageList;
}
return true;
}
bool ConnectionWindow::lockStationsWithMessage(QString message)
{
if (!hasDozent)
{
QString message = QString(tr("You have to set a Superclient-machine before performing this action."));
QMessageBox::information(this, "PVS", message);
return false;
}
if (std::list<QString>* messageList = MainWindow::getConnectionList()->getSelectedClients()) {
if (!messageList->empty()) {
for (std::list<QString>::iterator tmpIt = messageList->begin(); tmpIt
!= messageList->end(); tmpIt++) {
PVSClient
* tmpConnection =
PVSConnectionManager::getManager()->getClientFromIp(
(*tmpIt).toUtf8().data());
// we don't want to lock the dozent machine
if (tmpConnection && !tmpConnection->getConnectionFrame()->getFrame()->isDozent())
{
lockStationsWithMessage(message, tmpConnection);
} else {
// scream in agony
}
}
}
delete messageList;
}
return true;
}
void ConnectionWindow::lockStationsWithMessage(QString message,
PVSClient* pvsCon)
{
pvsCon->sendMessage(PVSCOMMAND, "LOCKSTATION", message);
}
bool ConnectionWindow::lockAllStationsWithMessage(QString message) {
if (!hasDozent)
{
QString message = QString(tr("You have to set a Superclient-machine before performing this action."));
QMessageBox::information(this, "PVS", message);
return false;
}
std::list<PVSClient*> listAll =
PVSConnectionManager::getManager()->getConnections();
for (std::list<PVSClient*>::iterator it = listAll.begin(); it
!= listAll.end(); it++)
{
if ((*it) && !(*it)->getConnectionFrame()->getFrame()->isDozent()) // we don't want to lock the dozent machine
(*it)->sendMessage(PVSCOMMAND, "LOCKSTATION", message);
}
return true;
}
void ConnectionWindow::addConnection(PVSClient* newCon) {
if (newCon) {
// QList<QString> ClientList= MainWindow::getConnectionList()->getClientist();
//Check if this client already in the list, before we add it.
//We don't want to have two clients with the same names.
//We comments it for test.
/*if (!ClientList.contains(newCon->getHostString()))
{*/
onVNCAdd(newCon);
MainWindow::getConnectionList()->onAddClient(newCon);
/*
* We check if the pvsmgr run a lockAll on the already connected
* clients. If yes, we lock the new conencted client too.
*/
if (MainWindow::getWindow()->isLockAllStatus() && newCon->getConnectionFrame()->getFrame())
{
newCon->getConnectionFrame()->getFrame()->setLockStatus(true);
// we call the update to force Qt to paint the lockicon on the frame
newCon->getConnectionFrame()->getFrame()->update();
lockStation(newCon);
}
//}
}
}
void ConnectionWindow::removeConnection(PVSClient* newCon) {
if (newCon == NULL) return;
ConnectionFrame* frame = newCon->getConnectionFrame();
if (frame != NULL)
{
removeFromList(frame);
//frame->deleteLater();
}
MainWindow::getConnectionList()->onRemoveClient(newCon);
}
void ConnectionWindow::onView() {
if (std::list<QString>* selectedClient = MainWindow::getConnectionList()->getSelectedClients()) {
if (!selectedClient->empty()) {
if (selectedClient->size() == 1)
;//TODO
}
}
}
void ConnectionWindow::updateConnection(PVSClient* newCon) {
if (newCon) {
MainWindow::getConnectionList()->onUpdateClient(newCon);
}
}
void ConnectionWindow::addVNC() {
if (std::list<QString>* vncAddList = MainWindow::getConnectionList()->getSelectedClients()) {
if (!vncAddList->empty()) {
for (std::list<QString>::iterator tmpIt = vncAddList->begin(); tmpIt
!= vncAddList->end(); tmpIt++) {
PVSClient
* tmpConnection =
PVSConnectionManager::getManager()->getClientFromIp(
(*tmpIt).toUtf8().data());
if (tmpConnection) {
tmpConnection->requestVNCConnect();
} else {
// scream in agony
}
}
}
}
}
void ConnectionWindow::onVNCAdd(PVSClient* newCon) {
if (!newCon->getConnectionFrame()) {
ConnectionFrame* newFrame = newConFrame(newCon);
newFrame->setConnection(newCon);
newFrame->setTaskbarTitle(newFrame->getTaskbarTitle());
newFrame->setDummy(false);
newFrame->setFrameRate(500);
addFrame(newFrame);
if (!frameList.contains(newFrame))
frameList.append(newFrame);
if (!AllFrameOnWindow.contains(newFrame))
AllFrameOnWindow.append(newFrame);
newFrame->show();
} else
// now we have already a pvsconnection, we should set the existing vncconnection to it
newCon->getConnectionFrame()->setConnection(newCon);
// addFrame(newFrame);
}
void ConnectionWindow::removeVNC() {
std::list<QString>* vncRemoveList =
MainWindow::getConnectionList()->getSelectedClients();
// if(!vncRemoveList->empty())
// {
for (std::list<QString>::iterator tmpIt = vncRemoveList->begin(); tmpIt
!= vncRemoveList->end(); tmpIt++) {
PVSClient* tmpConnection =
PVSConnectionManager::getManager()->getClientFromIp(
(*tmpIt).toUtf8().data());
if (tmpConnection) {
if (tmpConnection->getVNCConnection()) {
ConnectionFrame* tmpFrame =
tmpConnection->getVNCConnection()->getFrame();
if (tmpFrame) {
frameList.removeOne(tmpFrame);
onChangeConnections();
tmpFrame->setConnection(NULL);
tmpFrame->initFrame();
//delete tmpFrame;
}
tmpConnection->shutDownVNC();
}
} else {
// scream in agony
}
}
//
// }
}
void ConnectionWindow::onRemoveConnection() {
if (std::list<QString>* removeList = MainWindow::getConnectionList()->getSelectedClients()) {
if (!removeList->empty()) {
for (std::list<QString>::iterator tmpIt = removeList->begin(); tmpIt
!= removeList->end(); tmpIt++) {
PVSClient
* tmpConnection =
PVSConnectionManager::getManager()->getClientFromIp(
(*tmpIt).toUtf8().data());
if (tmpConnection) {
// removal should start from the CM, so hes linking back to the ConnectionWindow::onConnectionRemoved() method
// to finish the removal procedure here
PVSConnectionManager::getManager()->removeConnection(
tmpConnection);
} else {
// scream in agony
}
}
// onChangeConnections();
}
delete removeList;
return;
}
}
void ConnectionWindow::onConnectionRemoved(PVSClient* newConnection) {
if (newConnection->getVNCConnection())
// check for associated frames!
if (newConnection->getVNCConnection()) {
ConnectionFrame* tmpFrame =
newConnection->getVNCConnection()->getFrame();
// now handle the case that the client disconnects while we are in fullscreen mode
// the vncconnection would be cut nevertheless, so this:
if (tmpFrame) {
frameList.removeOne(tmpFrame);
// now handle the case that the client disconnects while we are in fullscreen mode
// the vncconnection would be cut nevertheless, so this:
if (tmpFrame == currentSingleFrame) {
currentSingleFrame = NULL;
}
// no longer needed
delete tmpFrame;
newConnection->getVNCConnection()->setFrame(NULL);
}
}
}
void ConnectionWindow::setSize(int width, int height, bool forceSquare) {
if (height >= 100 && width >= 100) {
MainWindow::getWindow()->setPrevWidth(width);
MainWindow::getWindow()->setPrevHeight(height);
}
ignoreRatio = forceSquare;
if (!frameList.empty()) {
ConnectionFrame* item;
foreach (item, frameList)
{
item->setIgnoreRatio(ignoreRatio);
}
}
}
void ConnectionWindow::getCloseupSize(int &width, int &height) {
if (!onCloseup) {
width = height = 0;
return;
}
width = 150;
height = 150;
}
bool ConnectionWindow::getShowDummies() {
return _showDummies;
}
void ConnectionWindow::setShowDummies(bool show) {
}
ConnectionFrame* ConnectionWindow::newConFrame(PVSClient* newConnection) {
if (newConnection->getConnectionFrame()) {
return NULL;
}
ConnectionFrame* temp = new ConnectionFrame(
MainWindow::getConnectionWindow());
if (!temp) {
return NULL;
}
return temp;
}
void ConnectionWindow::setCloseupFrame(ConnectionFrame* cFrame) {
_closeupFrame = cFrame;
}
ConnectionFrame* ConnectionWindow::getCloseupFrame() {
if (_closeupFrame)
return _closeupFrame;
return NULL;
}