From 941d546170f459219a9a6f4e71dde575f0a1fa09 Mon Sep 17 00:00:00 2001 From: "David A. Cuadrado" Date: Sat, 27 Oct 2007 16:17:17 -0500 Subject: [PATCH] Completed chat implemention --- .../network/package/connected.cpp} | 72 +++++++++++----------- .../network/package/connected.h} | 47 ++++++-------- .../network/package/disconnected.cpp} | 61 ++++++++---------- .../network/package/disconnected.h} | 48 ++++++--------- src/dash/network_module.pri | 10 ++- src/dashserver/project/store.cpp | 25 +++++++- src/shell/comm/chatwindow.cpp | 61 +++++++++++++++++- src/shell/comm/chatwindow.h | 6 ++ src/shell/comm/chatwindow.ui | 9 ++- src/shell/networkhandler.cpp | 35 +++++++++++ 10 files changed, 239 insertions(+), 135 deletions(-) copy src/{shell/comm/chatwindow.h => dash/network/package/connected.cpp} (70%) copy src/{shell/comm/chatwindow.h => dash/network/package/connected.h} (73%) copy src/{shell/comm/chatwindow.h => dash/network/package/disconnected.cpp} (70%) copy src/{shell/comm/chatwindow.h => dash/network/package/disconnected.h} (71%) diff --git a/src/shell/comm/chatwindow.h b/src/dash/network/package/connected.cpp similarity index 70% copy from src/shell/comm/chatwindow.h copy to src/dash/network/package/connected.cpp index 6280a23..394c413 100644 --- a/src/shell/comm/chatwindow.h +++ b/src/dash/network/package/connected.cpp @@ -19,43 +19,45 @@ ***************************************************************************/ -#ifndef CHATWINDOW_H -#define CHATWINDOW_H +#include "connected.h" -#include +namespace Dash { +namespace Network { +namespace Package { -namespace Ui { - class ChatWindow; +Connected::Connected(const QString &login) + : QDomDocument() +{ + QDomElement root = createElement("connected"); + + QDomElement user = createElement("user"); + user.setAttribute("login", login); + root.appendChild(user); + + appendChild(root); +} + +Connected::Connected(const QStringList &logins) +{ + QDomElement root = createElement("connected"); + + foreach(QString login, logins) + { + QDomElement user = createElement("user"); + user.setAttribute("login", login); + root.appendChild(user); + } + + appendChild(root); } -/** - @author David Cuadrado -*/ -class ChatWindow : public QDialog +Connected::~Connected() { - Q_OBJECT; - public: - enum { - Chat = 0x01, - Notice, - Wall - }; - ChatWindow(QWidget *parent = 0); - ~ChatWindow(); - - void addMessage(const QString &from, const QString &text); - void addNotice(const QString &from, const QString &text); - void addWall(const QString &from, const QString &text); - - private slots: - void onEditFinished(); - - signals: - void sendText(const QString &text, int type); - - private: - Ui::ChatWindow *ui; - -}; - -#endif +} + + +} + +} + +} diff --git a/src/shell/comm/chatwindow.h b/src/dash/network/package/connected.h similarity index 73% copy from src/shell/comm/chatwindow.h copy to src/dash/network/package/connected.h index 6280a23..5c5b2d6 100644 --- a/src/shell/comm/chatwindow.h +++ b/src/dash/network/package/connected.h @@ -19,43 +19,34 @@ ***************************************************************************/ -#ifndef CHATWINDOW_H -#define CHATWINDOW_H +#ifndef DASH_NETWORK_PACKAGECONNECTED_H +#define DASH_NETWORK_PACKAGECONNECTED_H -#include +#include +#include -namespace Ui { - class ChatWindow; -} +#include + +namespace Dash { +namespace Network { +namespace Package { /** @author David Cuadrado */ -class ChatWindow : public QDialog +class DASH_EXPORT Connected : public QDomDocument { - Q_OBJECT; public: - enum { - Chat = 0x01, - Notice, - Wall - }; - ChatWindow(QWidget *parent = 0); - ~ChatWindow(); - - void addMessage(const QString &from, const QString &text); - void addNotice(const QString &from, const QString &text); - void addWall(const QString &from, const QString &text); - - private slots: - void onEditFinished(); - - signals: - void sendText(const QString &text, int type); - - private: - Ui::ChatWindow *ui; + Connected(const QString &login); + Connected(const QStringList &logins); + ~Connected(); }; +} + +} + +} + #endif diff --git a/src/shell/comm/chatwindow.h b/src/dash/network/package/disconnected.cpp similarity index 70% copy from src/shell/comm/chatwindow.h copy to src/dash/network/package/disconnected.cpp index 6280a23..21f56c9 100644 --- a/src/shell/comm/chatwindow.h +++ b/src/dash/network/package/disconnected.cpp @@ -19,43 +19,34 @@ ***************************************************************************/ -#ifndef CHATWINDOW_H -#define CHATWINDOW_H +#include "disconnected.h" -#include +namespace Dash { -namespace Ui { - class ChatWindow; +namespace Network { + +namespace Package { + +Disconnected::Disconnected(const QString &login) + : QDomDocument() +{ + QDomElement root = createElement("disconnected"); + + QDomElement user = createElement("user"); + user.setAttribute("login", login); + root.appendChild(user); + + appendChild(root); } -/** - @author David Cuadrado -*/ -class ChatWindow : public QDialog + +Disconnected::~Disconnected() { - Q_OBJECT; - public: - enum { - Chat = 0x01, - Notice, - Wall - }; - ChatWindow(QWidget *parent = 0); - ~ChatWindow(); - - void addMessage(const QString &from, const QString &text); - void addNotice(const QString &from, const QString &text); - void addWall(const QString &from, const QString &text); - - private slots: - void onEditFinished(); - - signals: - void sendText(const QString &text, int type); - - private: - Ui::ChatWindow *ui; - -}; - -#endif +} + + +} + +} + +} diff --git a/src/shell/comm/chatwindow.h b/src/dash/network/package/disconnected.h similarity index 71% copy from src/shell/comm/chatwindow.h copy to src/dash/network/package/disconnected.h index 6280a23..96dbfbe 100644 --- a/src/shell/comm/chatwindow.h +++ b/src/dash/network/package/disconnected.h @@ -19,43 +19,31 @@ ***************************************************************************/ -#ifndef CHATWINDOW_H -#define CHATWINDOW_H +#ifndef DASH_NETWORK_PACKAGEDISCONNECTED_H +#define DASH_NETWORK_PACKAGEDISCONNECTED_H -#include +#include -namespace Ui { - class ChatWindow; -} +#include + +namespace Dash { +namespace Network { +namespace Package { /** - @author David Cuadrado + * @author David Cuadrado */ -class ChatWindow : public QDialog +class DASH_EXPORT Disconnected : public QDomDocument { - Q_OBJECT; public: - enum { - Chat = 0x01, - Notice, - Wall - }; - ChatWindow(QWidget *parent = 0); - ~ChatWindow(); - - void addMessage(const QString &from, const QString &text); - void addNotice(const QString &from, const QString &text); - void addWall(const QString &from, const QString &text); - - private slots: - void onEditFinished(); - - signals: - void sendText(const QString &text, int type); - - private: - Ui::ChatWindow *ui; - + Disconnected(const QString &login); + ~Disconnected(); }; +} + +} + +} + #endif diff --git a/src/dash/network_module.pri b/src/dash/network_module.pri index 0e75591..58ddcfd 100644 --- a/src/dash/network_module.pri +++ b/src/dash/network_module.pri @@ -48,7 +48,10 @@ network/filereceiver.h \ ./network/parser/projectlistparser.h \ ./network/parser/projectparser.h \ ./network/package/transfer.h \ -./network/parser/commparser.h +./network/parser/commparser.h \ +./network/package/connected.h \ +./network/package/disconnected.h + SOURCES += network/socket.cpp \ network/package/source.cpp \ network/package/observer.cpp \ @@ -57,7 +60,10 @@ network/filereceiver.cpp \ ./network/parser/projectlistparser.cpp \ ./network/parser/projectparser.cpp \ ./network/package/transfer.cpp \ -./network/parser/commparser.cpp +./network/parser/commparser.cpp \ +./network/package/connected.cpp \ +./network/package/disconnected.cpp + INSTALLS += network package parser network.path = /include/dash/network/ diff --git a/src/dashserver/project/store.cpp b/src/dashserver/project/store.cpp index 20624b8..e736cd2 100644 --- a/src/dashserver/project/store.cpp +++ b/src/dashserver/project/store.cpp @@ -31,10 +31,13 @@ #include "project.h" #include "dashserver/connection.h" +#include "dashserver/users/user.h" #include "dashserver/xdbms/manager.h" #include "dashserver/xdbms/table.h" #include "dash/network/package/transfer.h" +#include "dash/network/package/connected.h" +#include "dash/network/package/disconnected.h" namespace DashServer { namespace Project { @@ -112,7 +115,24 @@ void Store::addToProject(const QString &projectName, DashServer::Connection *cnx Project *project = d->projects.value(projectName); cnx->setData(ProjectName, projectName); + + QStringList members; + foreach(DashServer::Connection *member, project->members()) + { + Dash::Network::Package::Connected pkg(cnx->user()->login()); + member->sendToClient(pkg.toString(), false); + + members << member->user()->login(); + } + project->addClient(cnx); + + if( !members.isEmpty() ) + { + Dash::Network::Package::Connected pkg(members); + dfDebug << pkg.toString(); + cnx->sendToClient(pkg.toString(), true); + } } } @@ -157,8 +177,8 @@ bool Store::openProject(const QString &projectName, DashServer::Connection *sour if( project->save() ) { - project->sendToClient(source); addToProject(projectName, source); + project->sendToClient(source); } return true; @@ -178,6 +198,9 @@ void Store::removeClient(DashServer::Connection *client) if( d->projects.contains(projectName) ) { + Dash::Network::Package::Disconnected pkg(client->user()->login()); + sendToMembers(projectName, pkg.toString()); + Project *project = d->projects.value(projectName); project->removeClient(client); diff --git a/src/shell/comm/chatwindow.cpp b/src/shell/comm/chatwindow.cpp index bd36d3d..f00a65f 100644 --- a/src/shell/comm/chatwindow.cpp +++ b/src/shell/comm/chatwindow.cpp @@ -21,6 +21,10 @@ #include "chatwindow.h" +#include + +#include + #include "ui_chatwindow.h" ChatWindow::ChatWindow(QWidget *parent) @@ -29,6 +33,11 @@ ChatWindow::ChatWindow(QWidget *parent) ui = new Ui::ChatWindow; ui->setupUi(this); + QFont font = ui->area->document()->defaultFont(); + font.setPointSize(12); + ui->area->document()->setDefaultFont(font); + + ui->nicks->setFont(font); connect(ui->text, SIGNAL(returnPressed()), this, SLOT(onEditFinished())); } @@ -41,17 +50,20 @@ ChatWindow::~ChatWindow() void ChatWindow::addMessage(const QString &from, const QString &text) { - ui->area->append(QString("<%1> %2").arg(from).arg(text)); + ui->area->append(QString("<%1> ").arg(from)); + ui->area->insertHtml(replaceText(text)); } void ChatWindow::addNotice(const QString &from, const QString &text) { - ui->area->append(tr("***Notice from %1: %2").arg(from).arg(text)); + ui->area->append(tr("***Notice from %1: ").arg(from)); + ui->area->insertHtml(replaceText(text)); } void ChatWindow::addWall(const QString &from, const QString &text) { - ui->area->append(tr("***Attention: %1 says: %2").arg(from).arg(text)); + ui->area->append(tr("***Attention: %1 says: ").arg(from)); + ui->area->insertHtml(replaceText(text)); } void ChatWindow::onEditFinished() @@ -76,4 +88,47 @@ void ChatWindow::onEditFinished() } } +QString ChatWindow::replaceText(const QString &text) +{ + QString rich = Qt::escape(text); + + rich = rich.replace("o:)", QString("path("face-angel.svg") +"\" />"); + rich = rich.replace(":'(", QString("path("face-crying.svg") +"\" />"); + rich = rich.replace("}:)", QString("path("face-devilish.svg") +"\" />"); + rich = rich.replace("8)", QString("path("face-glasses.svg") +"\" />"); + rich = rich.replace(":D", QString("path("face-grin.svg") +"\" />"); + rich = rich.replace(":*)", QString("path("face-kiss.svg") +"\" />"); + rich = rich.replace(":(|)", QString("path("face-monkey.svg") +"\" />"); + rich = rich.replace(":|", QString("path("face-plain.svg") +"\" />"); + rich = rich.replace(":(", QString("path("face-sad.svg") +"\" />"); + rich = rich.replace(":]", QString("path("face-smile-big.svg") +"\" />"); + rich = rich.replace(":o", QString("path("face-surprise.svg") +"\" />"); + rich = rich.replace(";)", QString("path("face-wink.svg") +"\" />"); + rich = rich.replace(":)", QString("path("face-smile.svg") +"\" />"); + + + return rich; +} + +void ChatWindow::addUsers(const QStringList & logins) +{ + ui->nicks->addItems(logins); + foreach(QString login, logins) + { + ui->area->append(tr("***Connected: %1").arg(login)); + } +} + +void ChatWindow::removeUser(const QString & login) +{ + QList items = ui->nicks->findItems(login , Qt::MatchFixedString ); + if(!items.isEmpty()) + { + QListWidgetItem *toRemove = items.first(); + ui->nicks->takeItem(ui->nicks->row(toRemove)); + delete toRemove; + ui->area->append(tr("***Disconnected: %1").arg(login)); + } + +} diff --git a/src/shell/comm/chatwindow.h b/src/shell/comm/chatwindow.h index 6280a23..8d291e4 100644 --- a/src/shell/comm/chatwindow.h +++ b/src/shell/comm/chatwindow.h @@ -47,6 +47,9 @@ class ChatWindow : public QDialog void addNotice(const QString &from, const QString &text); void addWall(const QString &from, const QString &text); + void addUsers(const QStringList & logins); + void removeUser(const QString & login); + private slots: void onEditFinished(); @@ -54,6 +57,9 @@ class ChatWindow : public QDialog void sendText(const QString &text, int type); private: + QString replaceText(const QString &text); + + private: Ui::ChatWindow *ui; }; diff --git a/src/shell/comm/chatwindow.ui b/src/shell/comm/chatwindow.ui index 09581a0..6f3ba85 100644 --- a/src/shell/comm/chatwindow.ui +++ b/src/shell/comm/chatwindow.ui @@ -32,7 +32,14 @@ - + + + true + + + false + + diff --git a/src/shell/networkhandler.cpp b/src/shell/networkhandler.cpp index b2013c3..ebc5252 100644 --- a/src/shell/networkhandler.cpp +++ b/src/shell/networkhandler.cpp @@ -333,6 +333,41 @@ void NetworkHandler::readed(const QString &txt) } } } + else if( root == "connected" ) + { + QXmlStreamReader reader(txt); + QStringList logins; + while(! reader.atEnd() ) + { + if( reader.readNext() == QXmlStreamReader::StartElement ) + { + if(reader.name() == "user") + { + logins << reader.attributes().value("login").toString(); + } + } + } + m_comm->chatWindow()->addUsers(logins); + } + else if( root == "disconnected" ) + { + QString login; + + QXmlStreamReader reader(txt); + while(! reader.atEnd() ) + { + if( reader.readNext() == QXmlStreamReader::StartElement ) + { + if(reader.name() == "user") + { + login = reader.attributes().value("login").toString(); + break; + } + } + } + + m_comm->chatWindow()->removeUser(login); + } else { m_dispatcher->addSource(root, txt); -- 2.11.4.GIT