From e0cb65474654c46d58cf2ec4b7bdbbf31448352a Mon Sep 17 00:00:00 2001 From: Gaelle Braud Date: Tue, 5 Aug 2025 14:24:52 +0200 Subject: [PATCH] switch default account if clicking on a notification leading to a chat room from another account --- Linphone/core/chat/ChatCore.cpp | 5 +++++ Linphone/core/chat/ChatCore.hpp | 4 ++++ Linphone/tool/Utils.cpp | 19 ++++++++++++++++++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Linphone/core/chat/ChatCore.cpp b/Linphone/core/chat/ChatCore.cpp index 7c3665cfa..9e9cb59da 100644 --- a/Linphone/core/chat/ChatCore.cpp +++ b/Linphone/core/chat/ChatCore.cpp @@ -108,6 +108,7 @@ ChatCore::ChatCore(const std::shared_ptr &chatRoom) : QObjec mChatRoomState = LinphoneEnums::fromLinphone(chatRoom->getState()); mIsEncrypted = chatRoom->hasCapability((int)linphone::ChatRoom::Capabilities::Encrypted); auto localAccount = ToolModel::findAccount(chatRoom->getLocalAddress()); + if (localAccount) mLocalAccount = AccountCore::create(localAccount); bool associatedAccountHasIMEncryptionMandatory = localAccount && localAccount->getParams() && localAccount->getParams()->getInstantMessagingEncryptionMandatory(); @@ -693,6 +694,10 @@ QList> ChatCore::getParticipants() const { return mParticipants; } +QSharedPointer ChatCore::getLocalAccount() const { + return mLocalAccount; +} + void ChatCore::updateInfo(const std::shared_ptr &updatedFriend, bool isRemoval) { mustBeInLinphoneThread(log().arg(Q_FUNC_INFO)); auto fAddress = ToolModel::interpretUrl(mPeerAddress); diff --git a/Linphone/core/chat/ChatCore.hpp b/Linphone/core/chat/ChatCore.hpp index c2794ca3c..baa02fa90 100644 --- a/Linphone/core/chat/ChatCore.hpp +++ b/Linphone/core/chat/ChatCore.hpp @@ -34,6 +34,7 @@ class EventLogCore; class FriendModel; +class AccountCore; class ChatCore : public QObject, public AbstractObject { Q_OBJECT @@ -148,6 +149,8 @@ public: QVariantList getParticipantsGui() const; QStringList getParticipantsAddresses() const; + QSharedPointer getLocalAccount() const; + void updateInfo(const std::shared_ptr &updatedFriend, bool isRemoval = false); signals: @@ -222,6 +225,7 @@ private: std::shared_ptr mChatModel; QSharedPointer mLastMessage; QList> mEventLogList; + QSharedPointer mLocalAccount; std::shared_ptr mFriendModel; QSharedPointer> mChatModelConnection; QSharedPointer> mCoreModelConnection; diff --git a/Linphone/tool/Utils.cpp b/Linphone/tool/Utils.cpp index c0a6e50be..a93753333 100644 --- a/Linphone/tool/Utils.cpp +++ b/Linphone/tool/Utils.cpp @@ -1657,7 +1657,24 @@ void Utils::openChat(ChatGui *chat) { smartShowWindow(mainWindow); if (mainWindow && chat) { emit chat->mCore->messageOpen(); - QMetaObject::invokeMethod(mainWindow, "openChat", Q_ARG(QVariant, QVariant::fromValue(chat))); + auto localChatAccount = chat->mCore->getLocalAccount(); + auto accountList = App::getInstance()->getAccountList(); + auto defaultAccount = accountList->getDefaultAccountCore(); + // If multiple accounts, we must switch to the correct account before opening the chatroom, otherwise, + // a chat room corresponding to the wrong account could be added in the chat list + if (localChatAccount && localChatAccount->getIdentityAddress() != defaultAccount->getIdentityAddress()) { + connect(accountList.get(), &AccountList::defaultAccountChanged, accountList.get(), + [localChatAccount, accountList, chat] { + auto defaultAccount = accountList->getDefaultAccountCore(); + if (defaultAccount->getIdentityAddress() == localChatAccount->getIdentityAddress()) { + disconnect(accountList.get(), &AccountList::defaultAccountChanged, accountList.get(), + nullptr); + QMetaObject::invokeMethod(getMainWindow(), "openChat", + Q_ARG(QVariant, QVariant::fromValue(chat))); + } + }); + localChatAccount->lSetDefaultAccount(); + } else QMetaObject::invokeMethod(mainWindow, "openChat", Q_ARG(QVariant, QVariant::fromValue(chat))); } }