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)
{
RsQThreadUtils::postToObject([=](){
auto ev = dynamic_cast<const RsChatStatusEvent *>(event.get());
auto ev = dynamic_cast<const RsChatLobbyEvent *>(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<ChatLobbyDialog*>(ChatDialog::getExistingChat(ChatId(lobby_id)))) {
cld->handleLobbyEvent(event_type, gxs_id, str);

View file

@ -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) ;

View file

@ -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<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
ui.tabWidget->removeTab(ui.tabWidget->indexOf(ui.groupChatTab));
#endif // def RS_DIRECT_CHAT
@ -200,8 +222,9 @@ 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());
@ -216,16 +239,15 @@ void FriendsDialog::chatMessageReceived(const ChatMessage &msg)
QMetaObject::invokeMethod(this, "clearChatNotify", Qt::QueuedConnection);
}
}
}
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);
}
}
void FriendsDialog::addFriend()
{

View file

@ -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

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;
@ -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:

View file

@ -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();

View file

@ -26,6 +26,7 @@
#include "gui/MainWindow.h"
#include "gui/chat/ChatDialog.h"
#include "gui/settings/rsharesettings.h"
#include "util/qtthreadsutils.h"
#include <algorithm>
#include <retroshare/rsnotify.h>
@ -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<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()

View file

@ -42,10 +42,9 @@ public:
virtual bool hasSetting(QString *name, QString *group) override;
private slots:
private:
void chatMessageReceived(ChatMessage msg);
private:
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

View file

@ -109,15 +109,16 @@ NotifyQt::NotifyQt() : cDialog(NULL)
qRegisterMetaType<RsGxsId>("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()
{
{

View file

@ -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);