using rsEvents for Chat rooms

This commit is contained in:
csoler 2025-08-12 21:00:11 +02:00
parent 9b881a6214
commit 2506bc5672
10 changed files with 111 additions and 59 deletions

View file

@ -113,25 +113,27 @@ ChatLobbyWidget::ChatLobbyWidget(QWidget *parent, Qt::WindowFlags flags)
rsEvents->registerEventsHandler( [this](std::shared_ptr<const RsEvent> event) rsEvents->registerEventsHandler( [this](std::shared_ptr<const RsEvent> event)
{ {
RsQThreadUtils::postToObject([=](){ RsQThreadUtils::postToObject([=](){
auto ev = dynamic_cast<const RsChatStatusEvent *>(event.get()); auto ev = dynamic_cast<const RsChatLobbyEvent *>(event.get());
if(!ev) return;
switch(ev->mEventCode) switch(ev->mEventCode)
{ {
case RsChatStatusEventCode::CHAT_LOBBY_INVITE_RECEIVED: case RsChatLobbyEventCode::CHAT_LOBBY_INVITE_RECEIVED:
readChatLobbyInvites(); readChatLobbyInvites();
break; break;
case RsChatStatusEventCode::CHAT_LOBBY_LIST_CHANGED: case RsChatLobbyEventCode::CHAT_LOBBY_LIST_CHANGED:
lobbyChanged(); lobbyChanged();
break; break;
case RsChatStatusEventCode::CHAT_LOBBY_EVENT_PEER_LEFT: case RsChatLobbyEventCode::CHAT_LOBBY_EVENT_PEER_LEFT:
case RsChatStatusEventCode::CHAT_LOBBY_EVENT_PEER_STATUS: case RsChatLobbyEventCode::CHAT_LOBBY_EVENT_PEER_STATUS:
case RsChatStatusEventCode::CHAT_LOBBY_EVENT_PEER_JOINED: case RsChatLobbyEventCode::CHAT_LOBBY_EVENT_PEER_JOINED:
case RsChatStatusEventCode::CHAT_LOBBY_EVENT_PEER_CHANGE_NICKNAME: case RsChatLobbyEventCode::CHAT_LOBBY_EVENT_PEER_CHANGE_NICKNAME:
case RsChatStatusEventCode::CHAT_LOBBY_EVENT_KEEP_ALIVE: case RsChatLobbyEventCode::CHAT_LOBBY_EVENT_KEEP_ALIVE:
handleChatLobbyEvent(ev->mLobbyId,ev->mEventCode,ev->mGxsId,QString::fromUtf8(ev->str.c_str())); handleChatLobbyEvent(ev->mLobbyId,ev->mEventCode,ev->mGxsId,QString::fromUtf8(ev->mStr.c_str()));
break; break;
default: default:
@ -1190,7 +1192,7 @@ void ChatLobbyWidget::itemDoubleClicked(QTreeWidgetItem *item, int /*column*/)
subscribeChatLobbyAtItem(item); subscribeChatLobbyAtItem(item);
} }
void ChatLobbyWidget::handleChatLobbyEvent(uint64_t lobby_id, RsChatStatusEventCode event_type, const RsGxsId &gxs_id, const QString& str) void ChatLobbyWidget::handleChatLobbyEvent(uint64_t lobby_id, RsChatLobbyEventCode event_type, const RsGxsId &gxs_id, const QString& str)
{ {
if (ChatLobbyDialog *cld = dynamic_cast<ChatLobbyDialog*>(ChatDialog::getExistingChat(ChatId(lobby_id)))) { if (ChatLobbyDialog *cld = dynamic_cast<ChatLobbyDialog*>(ChatDialog::getExistingChat(ChatId(lobby_id)))) {
cld->handleLobbyEvent(event_type, gxs_id, str); cld->handleLobbyEvent(event_type, gxs_id, str);

View file

@ -89,7 +89,7 @@ protected slots:
void unsubscribeItem(); void unsubscribeItem();
void itemDoubleClicked(QTreeWidgetItem *item, int column); void itemDoubleClicked(QTreeWidgetItem *item, int column);
void updateCurrentLobby() ; void updateCurrentLobby() ;
void handleChatLobbyEvent(uint64_t lobby_id, RsChatStatusEventCode event_type, const RsGxsId &gxs_id, const QString& str); void handleChatLobbyEvent(uint64_t lobby_id, RsChatLobbyEventCode event_type, const RsGxsId &gxs_id, const QString& str);
void readChatLobbyInvites(); void readChatLobbyInvites();
void showLobby(QTreeWidgetItem *lobby_item) ; void showLobby(QTreeWidgetItem *lobby_item) ;
void showBlankPage(ChatLobbyId id, bool subscribed = false) ; void showBlankPage(ChatLobbyId id, bool subscribed = false) ;

View file

@ -40,6 +40,7 @@
#include "RetroShareLink.h" #include "RetroShareLink.h"
#include "settings/rsharesettings.h" #include "settings/rsharesettings.h"
#include "util/misc.h" #include "util/misc.h"
#include "util/qtthreadsutils.h"
#include "util/DateTime.h" #include "util/DateTime.h"
#include "FriendsDialog.h" #include "FriendsDialog.h"
#include "NetworkView.h" #include "NetworkView.h"
@ -77,8 +78,29 @@ FriendsDialog::FriendsDialog(QWidget *parent) : MainPage(parent)
ui.chatWidget->setWelcomeMessage(msg); ui.chatWidget->setWelcomeMessage(msg);
ui.chatWidget->init(ChatId::makeBroadcastId(), tr("Broadcast")); ui.chatWidget->init(ChatId::makeBroadcastId(), tr("Broadcast"));
connect(NotifyQt::getInstance(), SIGNAL(chatMessageReceived(ChatMessage)), this, SLOT(chatMessageReceived(ChatMessage))); //connect(NotifyQt::getInstance(), SIGNAL(chatMessageReceived(ChatMessage)), this, SLOT(chatMessageReceived(ChatMessage)));
connect(NotifyQt::getInstance(), SIGNAL(chatStatusChanged(ChatId,QString)), this, SLOT(chatStatusReceived(ChatId,QString))); //connect(NotifyQt::getInstance(), SIGNAL(chatStatusChanged(ChatId,QString)), this, SLOT(chatStatusReceived(ChatId,QString)));
mEventHandlerId = 0;
rsEvents->registerEventsHandler( [this](std::shared_ptr<const RsEvent> e)
{
RsQThreadUtils::postToObject([=]()
{
auto fe = dynamic_cast<const RsChatServiceEvent*>(e.get()); if(!fe) return;
switch(fe->mEventCode)
{
case RsChatServiceEventCode::CHAT_MESSAGE_RECEIVED: chatMessageReceived(fe->mMsg); break;
case RsChatServiceEventCode::CHAT_STATUS_CHANGED: chatStatusReceived(fe->mCid,QString::fromUtf8(fe->mStr.c_str())); break;
default:
break;
}
}
, this );
}, mEventHandlerId, RsEventType::CHAT_SERVICE );
#else // def RS_DIRECT_CHAT #else // def RS_DIRECT_CHAT
ui.tabWidget->removeTab(ui.tabWidget->indexOf(ui.groupChatTab)); ui.tabWidget->removeTab(ui.tabWidget->indexOf(ui.groupChatTab));
#endif // def RS_DIRECT_CHAT #endif // def RS_DIRECT_CHAT
@ -200,31 +222,31 @@ void FriendsDialog::processSettings(bool bLoad)
void FriendsDialog::chatMessageReceived(const ChatMessage &msg) void FriendsDialog::chatMessageReceived(const ChatMessage &msg)
{ {
if(msg.chat_id.isBroadcast()) if(!msg.chat_id.isBroadcast())
return;
QDateTime sendTime = QDateTime::fromTime_t(msg.sendTime);
QDateTime recvTime = QDateTime::fromTime_t(msg.recvTime);
QString message = QString::fromUtf8(msg.msg.c_str());
QString name = QString::fromUtf8(rsPeers->getPeerName(msg.broadcast_peer_id).c_str());
ui.chatWidget->addChatMsg(msg.incoming, name, sendTime, recvTime, message, ChatWidget::MSGTYPE_NORMAL);
if(ui.chatWidget->isActive())
{ {
QDateTime sendTime = QDateTime::fromTime_t(msg.sendTime); // clear the chat notify when control returns to the Qt event loop
QDateTime recvTime = QDateTime::fromTime_t(msg.recvTime); // we have to do this later, because we don't know if we or the notify receives the chat message first
QString message = QString::fromUtf8(msg.msg.c_str()); QMetaObject::invokeMethod(this, "clearChatNotify", Qt::QueuedConnection);
QString name = QString::fromUtf8(rsPeers->getPeerName(msg.broadcast_peer_id).c_str());
ui.chatWidget->addChatMsg(msg.incoming, name, sendTime, recvTime, message, ChatWidget::MSGTYPE_NORMAL);
if(ui.chatWidget->isActive())
{
// clear the chat notify when control returns to the Qt event loop
// we have to do this later, because we don't know if we or the notify receives the chat message first
QMetaObject::invokeMethod(this, "clearChatNotify", Qt::QueuedConnection);
}
} }
} }
void FriendsDialog::chatStatusReceived(const ChatId &chat_id, const QString &status_string) void FriendsDialog::chatStatusReceived(const ChatId &chat_id, const QString &status_string)
{ {
if(chat_id.isBroadcast()) if(!chat_id.isBroadcast())
{ return;
QString name = QString::fromUtf8(rsPeers->getPeerName(chat_id.broadcast_status_peer_id).c_str());
ui.chatWidget->updateStatusString(name + " %1", status_string); QString name = QString::fromUtf8(rsPeers->getPeerName(chat_id.broadcast_status_peer_id).c_str());
} ui.chatWidget->updateStatusString(name + " %1", status_string);
} }
void FriendsDialog::addFriend() void FriendsDialog::addFriend()

View file

@ -70,9 +70,6 @@ public:
IdDialog *idDialog; IdDialog *idDialog;
private slots: private slots:
void chatMessageReceived(const ChatMessage& msg);
void chatStatusReceived(const ChatId& chat_id, const QString& status_string);
void addFriend(); void addFriend();
void statusmessage(); void statusmessage();
@ -89,10 +86,16 @@ signals:
void notifyGroupChat(const QString&,const QString&) ; void notifyGroupChat(const QString&,const QString&) ;
private: private:
void chatMessageReceived(const ChatMessage& msg);
void chatStatusReceived(const ChatId& chat_id, const QString& status_string);
void processSettings(bool bLoad); void processSettings(bool bLoad);
/** Qt Designer generated object */ /** Qt Designer generated object */
Ui::FriendsDialog ui; Ui::FriendsDialog ui;
#ifdef RS_DIRECT_CHAT
RsEventsHandlerId_t mEventHandlerId ;
#endif
}; };
#endif #endif

View file

@ -846,7 +846,7 @@ QString ChatLobbyDialog::getParticipantName(const RsGxsId& gxs_id) const
} }
void ChatLobbyDialog::handleLobbyEvent(RsChatStatusEventCode event_type, const RsGxsId& gxs_id, const QString& str) void ChatLobbyDialog::handleLobbyEvent(RsChatLobbyEventCode event_type, const RsGxsId& gxs_id, const QString& str)
{ {
RsGxsId qsParticipant; RsGxsId qsParticipant;
@ -854,17 +854,17 @@ void ChatLobbyDialog::handleLobbyEvent(RsChatStatusEventCode event_type, const R
switch (event_type) switch (event_type)
{ {
case RsChatStatusEventCode::CHAT_LOBBY_EVENT_PEER_LEFT: case RsChatLobbyEventCode::CHAT_LOBBY_EVENT_PEER_LEFT:
qsParticipant=gxs_id; qsParticipant=gxs_id;
ui.chatWidget->addChatMsg(true, tr("Chat room management"), QDateTime::currentDateTime(), QDateTime::currentDateTime(), tr("%1 has left the room.").arg(RsHtml::plainText(name)), ChatWidget::MSGTYPE_SYSTEM); ui.chatWidget->addChatMsg(true, tr("Chat room management"), QDateTime::currentDateTime(), QDateTime::currentDateTime(), tr("%1 has left the room.").arg(RsHtml::plainText(name)), ChatWidget::MSGTYPE_SYSTEM);
emit peerLeft(id()) ; emit peerLeft(id()) ;
break; break;
case RsChatStatusEventCode::CHAT_LOBBY_EVENT_PEER_JOINED: case RsChatLobbyEventCode::CHAT_LOBBY_EVENT_PEER_JOINED:
qsParticipant=gxs_id; qsParticipant=gxs_id;
ui.chatWidget->addChatMsg(true, tr("Chat room management"), QDateTime::currentDateTime(), QDateTime::currentDateTime(), tr("%1 joined the room.").arg(RsHtml::plainText(name)), ChatWidget::MSGTYPE_SYSTEM); ui.chatWidget->addChatMsg(true, tr("Chat room management"), QDateTime::currentDateTime(), QDateTime::currentDateTime(), tr("%1 joined the room.").arg(RsHtml::plainText(name)), ChatWidget::MSGTYPE_SYSTEM);
emit peerJoined(id()) ; emit peerJoined(id()) ;
break; break;
case RsChatStatusEventCode::CHAT_LOBBY_EVENT_PEER_STATUS: case RsChatLobbyEventCode::CHAT_LOBBY_EVENT_PEER_STATUS:
{ {
qsParticipant=gxs_id; qsParticipant=gxs_id;
@ -876,7 +876,7 @@ void ChatLobbyDialog::handleLobbyEvent(RsChatStatusEventCode event_type, const R
} }
break; break;
case RsChatStatusEventCode::CHAT_LOBBY_EVENT_PEER_CHANGE_NICKNAME: case RsChatLobbyEventCode::CHAT_LOBBY_EVENT_PEER_CHANGE_NICKNAME:
{ {
qsParticipant=gxs_id; qsParticipant=gxs_id;
@ -892,7 +892,7 @@ void ChatLobbyDialog::handleLobbyEvent(RsChatStatusEventCode event_type, const R
muteParticipant(RsGxsId(str.toStdString())) ; muteParticipant(RsGxsId(str.toStdString())) ;
} }
break; break;
case RsChatStatusEventCode::CHAT_LOBBY_EVENT_KEEP_ALIVE: case RsChatLobbyEventCode::CHAT_LOBBY_EVENT_KEEP_ALIVE:
//std::cerr << "Received keep alive packet from " << nickname.toStdString() << " in chat room " << getPeerId() << std::endl; //std::cerr << "Received keep alive packet from " << nickname.toStdString() << " in chat room " << getPeerId() << std::endl;
break; break;
default: default:

View file

@ -42,7 +42,7 @@ class ChatLobbyDialog: public ChatDialog
friend class ChatDialog; friend class ChatDialog;
public: public:
void handleLobbyEvent(RsChatStatusEventCode event_type, const RsGxsId& gxs_id, const QString& str); void handleLobbyEvent(RsChatLobbyEventCode event_type, const RsGxsId& gxs_id, const QString& str);
virtual void showDialog(uint chatflags); virtual void showDialog(uint chatflags);
virtual ChatWidget *getChatWidget(); virtual ChatWidget *getChatWidget();

View file

@ -26,6 +26,7 @@
#include "gui/MainWindow.h" #include "gui/MainWindow.h"
#include "gui/chat/ChatDialog.h" #include "gui/chat/ChatDialog.h"
#include "gui/settings/rsharesettings.h" #include "gui/settings/rsharesettings.h"
#include "util/qtthreadsutils.h"
#include <algorithm> #include <algorithm>
#include <retroshare/rsnotify.h> #include <retroshare/rsnotify.h>
@ -57,8 +58,31 @@ static ChatUserNotify* instance = 0;
ChatUserNotify::ChatUserNotify(QObject *parent) : ChatUserNotify::ChatUserNotify(QObject *parent) :
UserNotify(parent) UserNotify(parent)
{ {
connect(NotifyQt::getInstance(), SIGNAL(chatMessageReceived(ChatMessage)), this, SLOT(chatMessageReceived(ChatMessage))); //connect(NotifyQt::getInstance(), SIGNAL(chatMessageReceived(ChatMessage)), this, SLOT(chatMessageReceived(ChatMessage)));
instance = this; instance = this;
mEventHandlerId = 0;
rsEvents->registerEventsHandler( [this](std::shared_ptr<const RsEvent> e)
{
RsQThreadUtils::postToObject([=]()
{
auto fe = dynamic_cast<const RsChatServiceEvent*>(e.get()); if(!fe) return;
if(!fe)
return;
switch(fe->mEventCode)
{
case RsChatServiceEventCode::CHAT_MESSAGE_RECEIVED: chatMessageReceived(fe->mMsg); break;
default:
break;
}
}
, this );
}, mEventHandlerId, RsEventType::CHAT_SERVICE );
} }
ChatUserNotify::~ChatUserNotify() ChatUserNotify::~ChatUserNotify()

View file

@ -42,11 +42,10 @@ public:
virtual bool hasSetting(QString *name, QString *group) override; virtual bool hasSetting(QString *name, QString *group) override;
private slots:
void chatMessageReceived(ChatMessage msg);
private: private:
virtual QIcon getIcon() override; void chatMessageReceived(ChatMessage msg);
virtual QIcon getIcon() override;
virtual QIcon getMainIcon(bool hasNew) override; virtual QIcon getMainIcon(bool hasNew) override;
virtual unsigned int getNewCount() override; virtual unsigned int getNewCount() override;
@ -54,6 +53,8 @@ private:
virtual QString getNotifyMessage(bool plural) override; virtual QString getNotifyMessage(bool plural) override;
virtual void iconClicked() override; virtual void iconClicked() override;
RsEventsHandlerId_t mEventHandlerId;
}; };
#endif // CHATUSERNOTIFY_H #endif // CHATUSERNOTIFY_H

View file

@ -109,15 +109,16 @@ NotifyQt::NotifyQt() : cDialog(NULL)
qRegisterMetaType<RsGxsId>("RsGxsId"); qRegisterMetaType<RsGxsId>("RsGxsId");
} }
//void NotifyQt::notifyErrorMsg(int list, int type, std::string msg) #ifdef TO_REMOVE
//{ void NotifyQt::notifyErrorMsg(int list, int type, std::string msg)
// { {
// QMutexLocker m(&_mutex) ; {
// if(!_enabled) QMutexLocker m(&_mutex) ;
// return ; if(!_enabled)
// } return ;
// emit errorOccurred(list,type,QString::fromUtf8(msg.c_str())) ; }
//} emit errorOccurred(list,type,QString::fromUtf8(msg.c_str())) ;
}
void NotifyQt::notifyChatMessage(const ChatMessage &msg) void NotifyQt::notifyChatMessage(const ChatMessage &msg)
{ {
@ -133,7 +134,6 @@ void NotifyQt::notifyChatMessage(const ChatMessage &msg)
emit chatMessageReceived(msg); emit chatMessageReceived(msg);
} }
#ifdef TO_REMOVE
void NotifyQt::notifyOwnAvatarChanged() void NotifyQt::notifyOwnAvatarChanged()
{ {
{ {

View file

@ -63,7 +63,7 @@ class NotifyQt: public QObject, public NotifyClient
// virtual void notifyListPreChange(int list, int type); // virtual void notifyListPreChange(int list, int type);
virtual void notifyListChange(int list, int type); virtual void notifyListChange(int list, int type);
// virtual void notifyErrorMsg(int list, int sev, std::string msg); // virtual void notifyErrorMsg(int list, int sev, std::string msg);
virtual void notifyChatMessage(const ChatMessage& /* msg */); // virtual void notifyChatMessage(const ChatMessage& /* msg */);
virtual void notifyChatStatus(const ChatId &chat_id,const std::string& status_string); virtual void notifyChatStatus(const ChatId &chat_id,const std::string& status_string);
virtual void notifyChatCleared(const ChatId &chat_id); virtual void notifyChatCleared(const ChatId &chat_id);
virtual void notifyCustomState(const std::string& peer_id, const std::string& status_string); virtual void notifyCustomState(const std::string& peer_id, const std::string& status_string);
@ -125,7 +125,7 @@ class NotifyQt: public QObject, public NotifyClient
// void peerStatusChanged(const QString& /* peer_id */, int /* status */); // void peerStatusChanged(const QString& /* peer_id */, int /* status */);
// void peerStatusChangedSummary() const; // void peerStatusChangedSummary() const;
void gxsChange(const RsGxsChanges& /* changes */); void gxsChange(const RsGxsChanges& /* changes */);
void chatMessageReceived(ChatMessage msg); // void chatMessageReceived(ChatMessage msg);
// void groupsChanged(int type) const ; // void groupsChanged(int type) const ;
void discInfoChanged() const ; void discInfoChanged() const ;
void historyChanged(uint msgId, int type); void historyChanged(uint msgId, int type);