From 2506bc5672d7dabb08fa7a5de338a9498134e902 Mon Sep 17 00:00:00 2001 From: csoler Date: Tue, 12 Aug 2025 21:00:11 +0200 Subject: [PATCH] using rsEvents for Chat rooms --- retroshare-gui/src/gui/ChatLobbyWidget.cpp | 22 ++++--- retroshare-gui/src/gui/ChatLobbyWidget.h | 2 +- retroshare-gui/src/gui/FriendsDialog.cpp | 64 +++++++++++++------ retroshare-gui/src/gui/FriendsDialog.h | 9 ++- .../src/gui/chat/ChatLobbyDialog.cpp | 12 ++-- retroshare-gui/src/gui/chat/ChatLobbyDialog.h | 2 +- .../src/gui/chat/ChatUserNotify.cpp | 26 +++++++- retroshare-gui/src/gui/chat/ChatUserNotify.h | 9 +-- retroshare-gui/src/gui/notifyqt.cpp | 20 +++--- retroshare-gui/src/gui/notifyqt.h | 4 +- 10 files changed, 111 insertions(+), 59 deletions(-) diff --git a/retroshare-gui/src/gui/ChatLobbyWidget.cpp b/retroshare-gui/src/gui/ChatLobbyWidget.cpp index 79a6664b3..d751dabf3 100644 --- a/retroshare-gui/src/gui/ChatLobbyWidget.cpp +++ b/retroshare-gui/src/gui/ChatLobbyWidget.cpp @@ -113,25 +113,27 @@ ChatLobbyWidget::ChatLobbyWidget(QWidget *parent, Qt::WindowFlags flags) rsEvents->registerEventsHandler( [this](std::shared_ptr event) { RsQThreadUtils::postToObject([=](){ - auto ev = dynamic_cast(event.get()); + auto ev = dynamic_cast(event.get()); + + if(!ev) return; switch(ev->mEventCode) { - case RsChatStatusEventCode::CHAT_LOBBY_INVITE_RECEIVED: + case RsChatLobbyEventCode::CHAT_LOBBY_INVITE_RECEIVED: readChatLobbyInvites(); break; - case RsChatStatusEventCode::CHAT_LOBBY_LIST_CHANGED: + case RsChatLobbyEventCode::CHAT_LOBBY_LIST_CHANGED: lobbyChanged(); break; - case RsChatStatusEventCode::CHAT_LOBBY_EVENT_PEER_LEFT: - case RsChatStatusEventCode::CHAT_LOBBY_EVENT_PEER_STATUS: - case RsChatStatusEventCode::CHAT_LOBBY_EVENT_PEER_JOINED: - case RsChatStatusEventCode::CHAT_LOBBY_EVENT_PEER_CHANGE_NICKNAME: - case RsChatStatusEventCode::CHAT_LOBBY_EVENT_KEEP_ALIVE: + case RsChatLobbyEventCode::CHAT_LOBBY_EVENT_PEER_LEFT: + case RsChatLobbyEventCode::CHAT_LOBBY_EVENT_PEER_STATUS: + case RsChatLobbyEventCode::CHAT_LOBBY_EVENT_PEER_JOINED: + case RsChatLobbyEventCode::CHAT_LOBBY_EVENT_PEER_CHANGE_NICKNAME: + 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; default: @@ -1190,7 +1192,7 @@ void ChatLobbyWidget::itemDoubleClicked(QTreeWidgetItem *item, int /*column*/) 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(ChatDialog::getExistingChat(ChatId(lobby_id)))) { cld->handleLobbyEvent(event_type, gxs_id, str); diff --git a/retroshare-gui/src/gui/ChatLobbyWidget.h b/retroshare-gui/src/gui/ChatLobbyWidget.h index 648e7dac9..8b355515a 100644 --- a/retroshare-gui/src/gui/ChatLobbyWidget.h +++ b/retroshare-gui/src/gui/ChatLobbyWidget.h @@ -89,7 +89,7 @@ protected slots: void unsubscribeItem(); void itemDoubleClicked(QTreeWidgetItem *item, int column); 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 showLobby(QTreeWidgetItem *lobby_item) ; void showBlankPage(ChatLobbyId id, bool subscribed = false) ; diff --git a/retroshare-gui/src/gui/FriendsDialog.cpp b/retroshare-gui/src/gui/FriendsDialog.cpp index 532a87f4a..4fe8e50be 100644 --- a/retroshare-gui/src/gui/FriendsDialog.cpp +++ b/retroshare-gui/src/gui/FriendsDialog.cpp @@ -40,6 +40,7 @@ #include "RetroShareLink.h" #include "settings/rsharesettings.h" #include "util/misc.h" +#include "util/qtthreadsutils.h" #include "util/DateTime.h" #include "FriendsDialog.h" #include "NetworkView.h" @@ -77,8 +78,29 @@ FriendsDialog::FriendsDialog(QWidget *parent) : MainPage(parent) ui.chatWidget->setWelcomeMessage(msg); ui.chatWidget->init(ChatId::makeBroadcastId(), tr("Broadcast")); - 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(chatMessageReceived(ChatMessage)), this, SLOT(chatMessageReceived(ChatMessage))); + //connect(NotifyQt::getInstance(), SIGNAL(chatStatusChanged(ChatId,QString)), this, SLOT(chatStatusReceived(ChatId,QString))); + + mEventHandlerId = 0; + + rsEvents->registerEventsHandler( [this](std::shared_ptr e) + { + RsQThreadUtils::postToObject([=]() + { + auto fe = dynamic_cast(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 ui.tabWidget->removeTab(ui.tabWidget->indexOf(ui.groupChatTab)); #endif // def RS_DIRECT_CHAT @@ -200,31 +222,31 @@ void FriendsDialog::processSettings(bool bLoad) 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); - 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()) - { - // 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); - } + // 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) { - if(chat_id.isBroadcast()) - { - QString name = QString::fromUtf8(rsPeers->getPeerName(chat_id.broadcast_status_peer_id).c_str()); - ui.chatWidget->updateStatusString(name + " %1", status_string); - } + 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); } void FriendsDialog::addFriend() diff --git a/retroshare-gui/src/gui/FriendsDialog.h b/retroshare-gui/src/gui/FriendsDialog.h index 7e86669b8..af85b6f76 100644 --- a/retroshare-gui/src/gui/FriendsDialog.h +++ b/retroshare-gui/src/gui/FriendsDialog.h @@ -70,9 +70,6 @@ public: IdDialog *idDialog; private slots: - void chatMessageReceived(const ChatMessage& msg); - void chatStatusReceived(const ChatId& chat_id, const QString& status_string); - void addFriend(); void statusmessage(); @@ -89,10 +86,16 @@ signals: void notifyGroupChat(const QString&,const QString&) ; private: + void chatMessageReceived(const ChatMessage& msg); + void chatStatusReceived(const ChatId& chat_id, const QString& status_string); + void processSettings(bool bLoad); /** Qt Designer generated object */ Ui::FriendsDialog ui; +#ifdef RS_DIRECT_CHAT + RsEventsHandlerId_t mEventHandlerId ; +#endif }; #endif diff --git a/retroshare-gui/src/gui/chat/ChatLobbyDialog.cpp b/retroshare-gui/src/gui/chat/ChatLobbyDialog.cpp index a0bed44f9..d6c7788a6 100644 --- a/retroshare-gui/src/gui/chat/ChatLobbyDialog.cpp +++ b/retroshare-gui/src/gui/chat/ChatLobbyDialog.cpp @@ -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; @@ -854,17 +854,17 @@ void ChatLobbyDialog::handleLobbyEvent(RsChatStatusEventCode event_type, const R switch (event_type) { - case RsChatStatusEventCode::CHAT_LOBBY_EVENT_PEER_LEFT: + case RsChatLobbyEventCode::CHAT_LOBBY_EVENT_PEER_LEFT: 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); emit peerLeft(id()) ; break; - case RsChatStatusEventCode::CHAT_LOBBY_EVENT_PEER_JOINED: + case RsChatLobbyEventCode::CHAT_LOBBY_EVENT_PEER_JOINED: 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); emit peerJoined(id()) ; break; - case RsChatStatusEventCode::CHAT_LOBBY_EVENT_PEER_STATUS: + case RsChatLobbyEventCode::CHAT_LOBBY_EVENT_PEER_STATUS: { qsParticipant=gxs_id; @@ -876,7 +876,7 @@ void ChatLobbyDialog::handleLobbyEvent(RsChatStatusEventCode event_type, const R } break; - case RsChatStatusEventCode::CHAT_LOBBY_EVENT_PEER_CHANGE_NICKNAME: + case RsChatLobbyEventCode::CHAT_LOBBY_EVENT_PEER_CHANGE_NICKNAME: { qsParticipant=gxs_id; @@ -892,7 +892,7 @@ void ChatLobbyDialog::handleLobbyEvent(RsChatStatusEventCode event_type, const R muteParticipant(RsGxsId(str.toStdString())) ; } 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; break; default: diff --git a/retroshare-gui/src/gui/chat/ChatLobbyDialog.h b/retroshare-gui/src/gui/chat/ChatLobbyDialog.h index 1d388b57f..5d2d36817 100644 --- a/retroshare-gui/src/gui/chat/ChatLobbyDialog.h +++ b/retroshare-gui/src/gui/chat/ChatLobbyDialog.h @@ -42,7 +42,7 @@ class ChatLobbyDialog: public ChatDialog friend class ChatDialog; 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 ChatWidget *getChatWidget(); diff --git a/retroshare-gui/src/gui/chat/ChatUserNotify.cpp b/retroshare-gui/src/gui/chat/ChatUserNotify.cpp index dcafae59a..ac80761bc 100644 --- a/retroshare-gui/src/gui/chat/ChatUserNotify.cpp +++ b/retroshare-gui/src/gui/chat/ChatUserNotify.cpp @@ -26,6 +26,7 @@ #include "gui/MainWindow.h" #include "gui/chat/ChatDialog.h" #include "gui/settings/rsharesettings.h" +#include "util/qtthreadsutils.h" #include #include @@ -57,8 +58,31 @@ static ChatUserNotify* instance = 0; ChatUserNotify::ChatUserNotify(QObject *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; + + mEventHandlerId = 0; + + rsEvents->registerEventsHandler( [this](std::shared_ptr e) + { + RsQThreadUtils::postToObject([=]() + { + auto fe = dynamic_cast(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() diff --git a/retroshare-gui/src/gui/chat/ChatUserNotify.h b/retroshare-gui/src/gui/chat/ChatUserNotify.h index 5ce8cb10a..2824acf5f 100644 --- a/retroshare-gui/src/gui/chat/ChatUserNotify.h +++ b/retroshare-gui/src/gui/chat/ChatUserNotify.h @@ -42,11 +42,10 @@ public: virtual bool hasSetting(QString *name, QString *group) override; -private slots: - void chatMessageReceived(ChatMessage msg); - private: - virtual QIcon getIcon() override; + void chatMessageReceived(ChatMessage msg); + + virtual QIcon getIcon() override; virtual QIcon getMainIcon(bool hasNew) override; virtual unsigned int getNewCount() override; @@ -54,6 +53,8 @@ private: virtual QString getNotifyMessage(bool plural) override; virtual void iconClicked() override; + + RsEventsHandlerId_t mEventHandlerId; }; #endif // CHATUSERNOTIFY_H diff --git a/retroshare-gui/src/gui/notifyqt.cpp b/retroshare-gui/src/gui/notifyqt.cpp index 3c415b115..c4bd08799 100644 --- a/retroshare-gui/src/gui/notifyqt.cpp +++ b/retroshare-gui/src/gui/notifyqt.cpp @@ -109,15 +109,16 @@ NotifyQt::NotifyQt() : cDialog(NULL) qRegisterMetaType("RsGxsId"); } -//void NotifyQt::notifyErrorMsg(int list, int type, std::string msg) -//{ -// { -// QMutexLocker m(&_mutex) ; -// if(!_enabled) -// return ; -// } -// emit errorOccurred(list,type,QString::fromUtf8(msg.c_str())) ; -//} +#ifdef TO_REMOVE +void NotifyQt::notifyErrorMsg(int list, int type, std::string msg) +{ + { + QMutexLocker m(&_mutex) ; + if(!_enabled) + return ; + } + emit errorOccurred(list,type,QString::fromUtf8(msg.c_str())) ; +} void NotifyQt::notifyChatMessage(const ChatMessage &msg) { @@ -133,7 +134,6 @@ void NotifyQt::notifyChatMessage(const ChatMessage &msg) emit chatMessageReceived(msg); } -#ifdef TO_REMOVE void NotifyQt::notifyOwnAvatarChanged() { { diff --git a/retroshare-gui/src/gui/notifyqt.h b/retroshare-gui/src/gui/notifyqt.h index c2c2a831a..f581f45e1 100644 --- a/retroshare-gui/src/gui/notifyqt.h +++ b/retroshare-gui/src/gui/notifyqt.h @@ -63,7 +63,7 @@ class NotifyQt: public QObject, public NotifyClient // virtual void notifyListPreChange(int list, int type); virtual void notifyListChange(int list, int type); // 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 notifyChatCleared(const ChatId &chat_id); 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 peerStatusChangedSummary() const; void gxsChange(const RsGxsChanges& /* changes */); - void chatMessageReceived(ChatMessage msg); +// void chatMessageReceived(ChatMessage msg); // void groupsChanged(int type) const ; void discInfoChanged() const ; void historyChanged(uint msgId, int type);