/*
# 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/connectionList.cpp
# -----------------------------------------------------------------------------
*/
#include <QtGui>
#include <iostream>
#include "connectionList.h"
#include <src/gui/dialog.h>
//#include <src/gui/projectionDialog.h>
//#include "mainWindow.h"
#define COL_FULLNAME 0
#define COL_IP 1
#define COL_USERNAME 2
ConnectionList::ConnectionList(QWidget *parent):
QTableView(parent)
{
/*Das Modelfestlegen, wo die Clientname angezeigt werden.*/
model = new QStandardItemModel(0,3,this); //Leere Tabelle mit einer Spalte erstellen
model->setHeaderData(0, Qt::Horizontal, tr("Name")); //Spalte name definieren
model->setHeaderData(1, Qt::Horizontal, tr("IP")); //IP-column will be hide
model->setHeaderData(2, Qt::Horizontal, tr("Username"));
setModel(model);
this->setColumnHidden(1, true);
this->setColumnHidden(2, true);
QItemSelectionModel *selectionModel = new QItemSelectionModel(model);
setSelectionModel(selectionModel);
QHeaderView *headerView = horizontalHeader();
headerView->setStretchLastSection(true);
/*Das popup einstellen*/
preparePopup();
_useUserName = true;
setEditTriggers(QAbstractItemView::NoEditTriggers); //Die Einträge in der Tabelle werden nicht editierbar.
setSelectionMode(QAbstractItemView::ExtendedSelection); //Damit mehere Einträge selektierbar werden.
resizeColumnToContents(0);
clientindex = 0;
isOnProjection = false; //No projection on progress now!
projSourceName = ""; //The projection source name is empty.
}
ConnectionList::~ConnectionList()
{
}
void ConnectionList::preparePopup()
{
/*Aktion definieren*/
/* a1 = new QAction(tr("&Remove Clients (debug)"),model);
connect(a1, SIGNAL(triggered()), this, SLOT(on_removeClient()));
a2 = new QAction(tr("&Request VNC-Window"),model);
connect(a2, SIGNAL(triggered()), this, SLOT(on_vnc_add()));
a3 = new QAction(tr("&Remove VNC-Window"),model);
connect(a3, SIGNAL(triggered()), this, SLOT(on_vnc_remove()));*/
_lockClient = new QAction(tr("&Lock Client(s)"),model);
connect(_lockClient, SIGNAL(triggered()), this, SLOT(on_lock()));
_uLockClient = new QAction(tr("&Unlock Client(s)"),model);
connect(_uLockClient, SIGNAL(triggered()), this, SLOT(on_unlock()));
/* invertlockClient = new QAction(tr("&Invertlock Client(s)"),model);
connect(invertlockClient, SIGNAL(triggered()), this, SLOT(on_lock_invert()));
LockAllClient = new QAction(tr("&Lock all Client(s)"),model);
connect(LockAllClient, SIGNAL(triggered()), this, SLOT(on_lock_all()));
uLockAllClient = new QAction(tr("&Unlock all Client(s)"),model);
connect(uLockAllClient, SIGNAL(triggered()), this, SLOT(on_unlock_all()));*/
_msgLockClient = new QAction(tr("&MsgLock Client(s)"),model);
connect(_msgLockClient, SIGNAL(triggered()), this, SLOT(on_lock_with_message()));
_msgLockAllClient = new QAction(tr("&MsgLock all Client(s)"),model);
connect(_msgLockAllClient, SIGNAL(triggered()), this, SLOT(on_lock_all_with_message()));
_msgClient = new QAction(tr("&Message Client(s)"),model);
connect(_msgClient, SIGNAL(triggered()), this, SLOT(on_message()));
//msgClient = createToolbarButton(tr("&Message Client(s)"), SLOT(on_message()));
_project = new QAction(tr("&Projection"),model);
connect(_project, SIGNAL(triggered()), this, SLOT(on_projection()));
_uproject = new QAction(tr("&Unprojection"),model);
connect(_uproject, SIGNAL(triggered()), this, SLOT(on_unprojection()));
/* rHelp = new QAction(tr("&Remote Help"),model);
connect(rHelp, SIGNAL(triggered()), this, SLOT(on_remoteHelp()));*/
/*Menü definieren*/
_popupMenu = new QMenu(this);
/*Aktionen in Menü aufnehmen*/
/* menu->addAction(a1);
menu->addAction(a2);
menu->addAction(a3);*/
_popupMenu->addAction(_lockClient);
_popupMenu->addAction(_uLockClient);
/* menu->addAction(invertlockClient);
menu->addAction(LockAllClient);
menu->addAction(uLockAllClient);*/
_popupMenu->addAction(_msgLockClient);
_popupMenu->addAction(_msgLockAllClient);
_popupMenu->addAction(_msgClient);
_popupMenu->addAction(_project);
_popupMenu->addAction(_uproject);
// menu->addAction(rHelp);
}
QAction* ConnectionList::createToolbarButton(const QString &name, const char *slot)
{
QAction *action = new QAction(name, model);
connect(action, SIGNAL(triggered()), this, slot);
_popupMenu->addAction(action);
return action;
}
void ConnectionList::mouseReleaseEvent(QMouseEvent * e )
{
// setCurrentIndex ( currentIndex () );
if (e->button() == Qt::RightButton)
{
if (model->rowCount(QModelIndex()) > 0)
foreach (QAction *a, _popupMenu->actions())
{
a->setDisabled(false);
}
else
foreach (QAction *a, _popupMenu->actions())
{
a->setDisabled(true);
}
_popupMenu->exec(QCursor::pos());
}
}
void ConnectionList::onAddClient(PVSClient* newConnection)
{
if (newConnection)
{
//clientindex = model->rowCount(QModelIndex())+1;
model->insertRow(clientindex, QModelIndex());
//QString uname = newConnection->getPVSClientConnection()->getNameUser();
QString uname = newConnection->getLoginName();
if (_useUserName)
{
QString username = newConnection->getUserName();
QString ip = newConnection->getIp();
model->setData(model->index(clientindex, COL_FULLNAME, QModelIndex()),username);
model->setData(model->index(clientindex, COL_IP, QModelIndex()),ip);
model->setData(model->index(clientindex, COL_USERNAME, QModelIndex()),uname);
newConnection->getConnectionFrame()->setTaskbarTitle(ip);
addClientToList(username);
}
else
{
QString desktopname = QString(newConnection->getDesktopName().toUtf8().data());
QString ip = newConnection->getIp();
model->setData(model->index(clientindex, COL_FULLNAME, QModelIndex()),desktopname);
model->setData(model->index(clientindex, COL_IP, QModelIndex()),ip);
model->setData(model->index(clientindex, COL_USERNAME, QModelIndex()),uname);
}
setCurrentIndex(model->index(clientindex, 0));
on_vnc_add(); // triggert the automatic vnc request!
setCurrentIndex(model->index(-1, 0));
newConnection->setClientindex(clientindex);
clientindex++;
}
}
void ConnectionList::onRemoveClient(PVSClient* newCon)
{
if (newCon && model->rowCount(QModelIndex()) > 0)
{
QString host = newCon->getIp();
for (int i=0; i<model->rowCount(QModelIndex()); i++)
{
if (model->index(i, 1, QModelIndex()).data(Qt::DisplayRole).toString()
.compare(host) == 0)
{
removeClientToList(model->index(i, 1, QModelIndex()).data(Qt::DisplayRole).toString());
model->removeRow(i, QModelIndex());
clientindex--;
break;
}
}
/*
* We have to check, if this disconnected client is involved in a projection process.
* If true, we have to unproject it, this will unproject the others clients that becomme
* the projection.
*/
if (targetList.contains(host) || sourceList.contains(host))
unproject(host);
}
}
void ConnectionList::addClientToList(QString clientname)
{
if (!_clientNames.contains(clientname))
_clientNames.append(clientname);
}
void ConnectionList::removeClientToList(QString clientname)
{
if (_clientNames.contains(clientname))
{
_clientNames.removeOne(clientname);
}
}
void ConnectionList::onUpdateClient(PVSClient* newConnection)
{
if (model->rowCount(QModelIndex()) > 0)
{
for (int i=0; i<model->rowCount(QModelIndex()); i++)
{
if (model->index(i, 1, QModelIndex()).data(Qt::DisplayRole).toString()
.compare(newConnection->getIp()) == 0)
{
if (_useUserName)
{
newConnection->getConnectionFrame()->setTheTitle(newConnection->getUserName());
model->setData(model->index(i, COL_FULLNAME, QModelIndex()),newConnection->getUserName());
}
else
{
newConnection->getConnectionFrame()->setTheTitle(newConnection->getDesktopName());
model->setData(model->index(i, COL_FULLNAME, QModelIndex()),newConnection->getDesktopName());
}
break;
}
}
}
else
{
// something went awfully wrong!
}
}
bool ConnectionList::useUserName()
{
return _useUserName;
}
bool ConnectionList::useUserName(bool use)
{
if (use != _useUserName)
{
_useUserName = use;
}
return useUserName();
}
void ConnectionList::on_vnc_add()
{
MainWindow::getConnectionWindow()->addVNC();
}
void ConnectionList::on_vnc_remove()
{
MainWindow::getConnectionWindow()->removeVNC();
}
void ConnectionList::unproject(QString source)
{
if (sourceList.contains(source))
{
QList<QString> target;
if (sourceMap.contains(source))
{
QString item;
foreach (item, sourceMap.value(source))
{
MainWindow::getConnectionWindow()->unprojectStations(item); //Nur target must be unproject
targetList.removeOne(item);
}
sourceMap.take(source);
}
MainWindow::getConnectionWindow()->unprojectStations(source); //the source schould be too unprojected.
sourceList.removeOne(source);
}
else
{
MainWindow::getConnectionWindow()->unprojectStations(source); //unproject a target
targetList.removeOne(source);
}
/*if (targetList.size() == 1)
targetList.clear();*/
}
void ConnectionList::on_projection()
{
if(model->rowCount(QModelIndex())>1)
{
std::list<QString>* cuList = getSelectedClients();
if (targetList.size() == (model->rowCount(QModelIndex())-1))
{
QString message = QString(tr("No Target is available for a projection!\nPerform an unprojection to get a target."));
QMessageBox::information(this, "PVS", message);
}
else if(cuList->size() == 1)
{
QString source = cuList->front();
if (targetList.contains(source))
{
QString message = QString(tr("This source is already in a projection process involved.\n You can perform a unprojection on it to get it free."));
QMessageBox::information(this, "PVS", message);
}
else
{
ProjectionDialog projDialog;
int result = projDialog.exec();
if(result == 1)
{
if(projectList.size()>0)
{
QList<QString> list;
QString item;
foreach(item, projectList)
{
list.append(item);
}
sourceMap.insert(projSourceName, list);
if (!sourceList.contains(source))
sourceList.append(source);
MainWindow::getConnectionWindow()->projectStations(projSourceName);
}
else
ConsoleLog writeError( "Strange...the selected target aren't be transmitted to the MainWindow.");
}
else
; // :(
}
}
else if(cuList->size() > 1)
{
QString message = QString(tr("To perform a projection you have to select only one source in a list with more than one index!"));
QMessageBox::information(this, "PVS", message);
}
}
else
{
QString message = QString(tr("To perform a projection you have to select only one source in a list with more than one index!"));
QMessageBox::information(this, "PVS", message);
}
}
void ConnectionList::on_unprojection()
{
if(model->rowCount(QModelIndex())>1)
{
std::list<QString>* cuList = getSelectedClients();
if(cuList->size() == 1)
{
QString source = cuList->front();
unproject(source);
}
else
{
QString message = QString(tr("An unprojection is only possible on a selected source that is involved in a projection process, in a list with more than one index!"));
QMessageBox::information(this, "PVS", message);
}
}
else
{
QString message = QString(tr("An unprojection is only possible on a selected source that is involved in a projection process, in a list with more than one index!"));
QMessageBox::information(this, "PVS", message);
}
}
void ConnectionList::on_remoteHelp()
{
if(model->rowCount(QModelIndex())>1)
{
std::list<QString>* cuList = getSelectedClients();
if (targetList.size() == (model->rowCount(QModelIndex())-1))
{
QString message = QString(tr("No Target is available for remote Help!\nPerform an unprojection or remove remote help to get a target."));
QMessageBox::information(this, "PVS", message);
}
else if(cuList->size() == 1)
{
QString source = cuList->front();
if (targetList.contains(source))
{
QString message = QString(tr("This source is already in a projection or remote Help process involved.\n You can perform a unprojection or remove remote Help on it to get it free."));
QMessageBox::information(this, "PVS", message);
}
else
{
ProjectionDialog projDialog;
int result = projDialog.exec();
if(result == 1)
{
if(projectList.size() > 0 && projectList.size() < 2)
{
QList<QString> list;
QString item;
foreach(item, projectList)
{
list.append(item);
}
sourceMap.insert(projSourceName, list);
MainWindow::getConnectionWindow()->remoteHelp(projSourceName);
}
else
ConsoleLog writeError( "Strange...the selected targets aren't be transmitted to the MainWindow.");
}
else
; // :(
}
}
}
else
{
QString message = QString(tr("To perform a projection you have to select only one source in a list with more than one index!"));
QMessageBox::information(this, "PVS", message);
}
}
void ConnectionList::on_lock()
{
MainWindow::getConnectionWindow()->lockStations();
}
void ConnectionList::on_unlock()
{
MainWindow::getConnectionWindow()->unlockStations();
}
void ConnectionList::on_message()
{
Dialog msgD;
QString myString = NULL;
int result = msgD.exec();
if (result == 1)
{
myString = MainWindow::getWindow()->getMsgDialog();
if(!myString.isEmpty())
MainWindow::getConnectionWindow()->messageStations("BROADCAST",myString);
}
}
void ConnectionList::on_lock_with_message()
{
Dialog msgD;
QString myString = NULL;
int result = msgD.exec();
if(result == 1)
{
myString = MainWindow::getWindow()->getMsgDialog();
if(!myString.isEmpty())
{
MainWindow::getConnectionWindow()->lockStationsWithMessage(myString);
}
}
}
void ConnectionList::on_lock_all_with_message()
{
QString myString = NULL;
Dialog msgD;
int result = msgD.exec();
if(result == 1)
{
myString = MainWindow::getWindow()->getMsgDialog();
if(!myString.isEmpty())
{
MainWindow::getConnectionWindow()->lockAllStationsWithMessage(myString);
}
}
}
void ConnectionList::on_lock_all()
{
MainWindow::getConnectionWindow()->lockAllStations();
}
void ConnectionList::on_unlock_all()
{
MainWindow::getConnectionWindow()->unlockAllStations();
}
void ConnectionList::on_unproject_all()
{
MainWindow::getConnectionWindow()->unprojectAllStations();
}
void ConnectionList::on_lock_invert()
{
MainWindow::getConnectionWindow()->lockInvertStations();
}
void ConnectionList::on_removeClient()
{
QTableView *temp = static_cast<QTableView*>(this);
QItemSelectionModel *selectionModel = temp->selectionModel();
QModelIndexList indexes = selectionModel->selectedRows();
removeClient(indexes);
}
void ConnectionList::removeClient(QModelIndexList indexes)
{
QModelIndex index;
foreach(index, indexes)
{
int row = currentIndex().row();
QString current = model->index(row, 0).data(Qt::DisplayRole).toString();
removeClientToList(current);
model->removeRow(row, QModelIndex());
}
}
std::list<QString>* ConnectionList::getSelectedClients(bool isClickOnWindow)
{
std::list<QString>* currentList = new std::list<QString>;
QTableView *temp = static_cast<QTableView*>(this);
QItemSelectionModel *selectionModel = temp->selectionModel();
QModelIndexList indexes = selectionModel->selectedIndexes();
QModelIndex index;
if (indexes.size() > 0)
{
foreach (index, indexes)
{
QString current = model->index(index.row(), 1).data(Qt::DisplayRole).toString();
currentList->push_back(current);
}
}
return currentList;
}
void ConnectionList::setProjectProporties(QString source)
{
projSourceName = source; // The source for this projection
sourceList.append(source); // The list for all source in pvsmgr
}
QList<QString> ConnectionList::getTargetToDisplay(QString source)
{
displayList.clear();
projectList.clear();
for (int i=0; i<model->rowCount(QModelIndex()); i++)
{
QString item = model->index(i, 1, QModelIndex()).data(Qt::DisplayRole).toString();
if (item.compare(source) != 0 && !targetList.contains(item) && !sourceList.contains(item))
{
displayList.append(item);
}
}
return displayList;
}
void ConnectionList::addTargetToProjectList(QString name)
{
if (!targetList.contains(name))
{
projectList.append(name);
targetList .append(name);
}
}
QList<QString> ConnectionList::getTargetForTheProject(QString source)
{
QList<QString> target;
if(sourceMap.contains(source)){
target = sourceMap.value(source);
}
return target;
}
bool ConnectionList::isOnProjection = false;