summaryrefslogblamecommitdiffstats
path: root/src/gui/serverChatDialog.cpp
blob: ec1f299b925b0644e3ca656d278c11bb0f580021 (plain) (tree)
























































































































































































































































































                                                                                               
/*
 # Copyright (c) 2009, 2010 - 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/
 # -----------------------------------------------------------------------------
 # serverChatDialog.cpp
 #  - graphical chat interface for the pvsmgr
 # -----------------------------------------------------------------------------
 */

#include "serverChatDialog.h"
#include <src/gui/mainWindow.h>
#include "ui_serverChatDialog.h"

ServerChatDialog::ServerChatDialog(QWidget *parent) :
        QDialog(parent), uichat(new Ui::ServerChatDialogClass)
{
    uichat->setupUi(this);
    _nickname = "PVSMGR";
    connect(uichat->pushButton, SIGNAL(clicked()), this, SLOT(chat_send()));

    // add first tab for public messages
    uichat->tabWidget->clear();
    QTextEdit *t = new QTextEdit();
    t->setReadOnly(true);
    uichat->tabWidget->addTab(t, "@all");
    _hash = new QHash<QString, QTextEdit*> ();
    _hash->insert(uichat->tabWidget->tabText(0), t);

    // setup menu
	_menu = new QMenu();
	_sendFileAction = new QAction(tr("&Send File..."), this);
	_menu->addAction(_sendFileAction);

    connect(uichat->listWidget, SIGNAL(doubleClicked(QModelIndex)), this,
            SLOT(addTab(QModelIndex)));
    connect(uichat->tabWidget, SIGNAL(tabCloseRequested(int)), this,
            SLOT(removeTab(int)));
    connect(uichat->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(removeIcon(int)));
    connect(uichat->listWidget, SIGNAL(customContextMenuRequested(QPoint)), this,
                SLOT(showMenu(QPoint)));
    connect(_sendFileAction, SIGNAL(triggered()), this, SLOT(sendFile()));

    this->setAcceptDrops(true);
}

ServerChatDialog::~ServerChatDialog()
{
}

void ServerChatDialog::setTrayIcon(QSystemTrayIcon *trayIcon)
{
   // _trayIcon = trayIcon;
    // FIXME: messageClicked() is always emitted, not only on chat msg
    //connect(_trayIcon, SIGNAL(messageClicked()), this, SLOT(open()));
}

////////////////////////////////////////////////////////////////////////////////
// Slots

void ServerChatDialog::open()
{
    uichat->lineEdit->setFocus();
    setVisible(true);
}

void ServerChatDialog::chat_receive(QString nick_from, QString nick_to,
                                    QString msg)
{
    qDebug("[%s] R %s <- %s : %s", metaObject()->className(),
           qPrintable(nick_to), qPrintable(nick_from), qPrintable(msg));

    if (nick_to == uichat->tabWidget->tabText(0))
        showMsg(nick_from, msg, getTab(nick_to)); // public message or own msg
    else
    {
    	if (nick_to == "PVSMGR")
    		showMsg(nick_from, msg, getTab(nick_from)); // private message
    	else if (nick_from == "PVSMGR")
    		showMsg(nick_from, msg, getTab(nick_to)); // private message
    }
}

void ServerChatDialog::chat_send()
{
    QString msg = uichat->lineEdit->text();
    if (msg != "")
    {
    	msg = "PVSMGR:"+ msg;
        QString nick_to = uichat->tabWidget->tabText(uichat->tabWidget->currentIndex());
        PVSMsg myMsg(PVSMESSAGE, nick_to, msg, 0);
        MainWindow::getWindow()->sendChatMsg(myMsg);
        uichat->lineEdit->clear();
        uichat->lineEdit->setFocus();

        qDebug("[%s] S %s -> %s : %s", metaObject()->className(),
               qPrintable(_nickname), qPrintable(nick_to), qPrintable(msg));

    }
}

void ServerChatDialog::chat_nicklist_update()
{
	uichat->listWidget->clear();
	uichat->listWidget->addItems(_nickList);
}

void ServerChatDialog::chat_client_add(QString nick)
{
	if (!_nickList.contains(nick))
		_nickList.append(nick);
    showEvent("-> " + nick + tr(" has joined the chat."));
}

void ServerChatDialog::chat_client_remove(QString nick)
{
	_nickList.removeOne(nick);
    showEvent("<- " + nick + tr(" has left the chat."));
}

void ServerChatDialog::addTab(QModelIndex i)
{
    QString text = i.data().toString();
    if (_hash->contains(text))
    {
    	uichat->tabWidget->setCurrentWidget(_hash->value(text));
    }
    else
    {
        QTextEdit *t = new QTextEdit();
        t->setReadOnly(true);
        uichat->tabWidget->setCurrentIndex(uichat->tabWidget->addTab(t, text));
        _hash->insert(text, t);
    }
    uichat->lineEdit->setFocus();
}

void ServerChatDialog::removeTab(int i)
{
    if (i != 0)
    {
        _tabList.removeOne(_hash->value(uichat->tabWidget->tabText(i)));
        _hash->remove(uichat->tabWidget->tabText(i));
        uichat->tabWidget->removeTab(i);
        uichat->lineEdit->setFocus();
    }
}

void ServerChatDialog::removeIcon(int i)
{
	uichat->tabWidget->setTabIcon(i, QIcon());
}

////////////////////////////////////////////////////////////////////////////////
// Private

QTextEdit* ServerChatDialog::getTab(QString text)
{
    if (!_hash->contains(text))
    {
        QTextEdit *t = new QTextEdit();
        t->setReadOnly(true);
        uichat->tabWidget->addTab(t, text);
        _hash->insert(text, t);
        _tabList.append(t);
    }
    uichat->lineEdit->setFocus();
    return _hash->value(text);
}

void ServerChatDialog::showMsg(QString nick_from, QString msg, QTextEdit *t)
{
    // move cursor at the end
    t->moveCursor(QTextCursor::End);

    // print time
    if (nick_from == "PVSMGR")
    	t->setTextColor(QColor(0, 100, 100));
    else
    	t->setTextColor(QColor(0, 100, 0));
    t->append("[" + QTime::currentTime().toString("hh:mm") + "] ");

    // print nickname
    if (nick_from == "PVSMGR")
        t->setTextColor(QColor(0, 100, 255));
    else
    	t->setTextColor(QColor(0, 0, 255));
    t->insertPlainText("<" + nick_from + "> ");

    // print message
    if (nick_from == "PVSMGR")
        t->setTextColor(QColor(100, 100, 100));
    else
    	t->setTextColor(QColor(0, 0, 0));
    t->insertPlainText(msg);

    // show icon if not current tab
    if (uichat->tabWidget->currentIndex() != uichat->tabWidget->indexOf(t))
    	uichat->tabWidget->setTabIcon(uichat->tabWidget->indexOf(t), QIcon(":chat_msg16.svg"));
}

void ServerChatDialog::showMenu(QPoint p)
{
    _menu->popup(uichat->listWidget->mapToGlobal(p));
}

void ServerChatDialog::sendFile()
{
	ServerFileTransfert* sft = new ServerFileTransfert(this);
    QString cTab = uichat->listWidget->currentItem()->text();
    QString hostIP = PVSConnectionManager::getManager()->getClientFromUsername(cTab)->getIp();
    if (hostIP != "")
		sft->sendFileToHost(hostIP);
}

void ServerChatDialog::sendFile(QString filename)
{
    if (uichat->tabWidget->currentIndex() == 0 || filename == "")
    	return;

    // ask user
    QString nick = uichat->tabWidget->tabText(uichat->tabWidget->currentIndex());
    QMessageBox::StandardButton result = QMessageBox::question(0,
            tr("PVS File Transfer with ")+nick,
            tr("Send file '") + filename + tr("' to ") + nick + "?",
            QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok);

    if (result !=  QMessageBox::Ok)
        return;

    ServerFileTransfert* sft = new ServerFileTransfert(this);
    QString hostIP = PVSConnectionManager::getManager()->getClientFromUsername(nick)->getIp();
    if (hostIP != "")
    	sft->sendFileToHost(hostIP, filename);
}


void ServerChatDialog::showEvent(QString msg)
{
    QTextEdit *t = _hash->value(uichat->tabWidget->tabText(0));

    // move cursor at the end
    t->moveCursor(QTextCursor::End);

    t->setTextColor(QColor(150, 150, 150));

    // print time
    t->append("[" + QTime::currentTime().toString("hh:mm") + "] ");

    // print message
    t->insertPlainText(msg);
}

void ServerChatDialog::dragEnterEvent(QDragEnterEvent *event)
{
	event->accept();
}

void ServerChatDialog::dragMoveEvent(QDragMoveEvent *event)
{
	event->accept();
}

void ServerChatDialog::dragLeaveEvent(QDragLeaveEvent *event)
{
	event->accept();
}

void ServerChatDialog::dropEvent(QDropEvent *event)
{
	event->accept();
	if (event->mimeData()->hasUrls())
		sendFile(event->mimeData()->urls()[0].toLocalFile());
}