From a51907b810db0089838aef7349cbbbba5842e957 Mon Sep 17 00:00:00 2001 From: blue Date: Thu, 29 Aug 2019 17:19:35 +0300 Subject: [PATCH] joining groups on opening them, leaving on closing --- core/account.cpp | 22 ++++++++++++++++++++++ core/account.h | 3 +++ core/squawk.cpp | 21 +++++++++++++++++++++ core/squawk.h | 2 ++ main.cpp | 3 +++ ui/squawk.cpp | 13 ++++++++++++- ui/squawk.h | 2 ++ ui/widgets/chat.cpp | 2 +- ui/widgets/conversation.cpp | 5 +++-- ui/widgets/conversation.h | 5 ++++- ui/widgets/messageline.cpp | 9 ++------- ui/widgets/messageline.h | 3 +-- ui/widgets/room.cpp | 8 ++++++-- ui/widgets/room.h | 2 ++ 14 files changed, 84 insertions(+), 16 deletions(-) diff --git a/core/account.cpp b/core/account.cpp index 1bae04e..9aedf26 100644 --- a/core/account.cpp +++ b/core/account.cpp @@ -1033,3 +1033,25 @@ void Core::Account::onMucNickNameChanged(const QString& nickName) {"nick", nickName} }); } + +void Core::Account::setRoomAutoJoin(const QString& jid, bool joined) +{ + std::map::const_iterator cItr = conferences.find(jid); + if (cItr == conferences.end()) { + qDebug() << "An attempt to set auto join to the non existing room" << jid << "of the account" << getName() << ", skipping"; + return; + } + + cItr->second->setAutoJoin(joined); +} + +void Core::Account::setRoomJoined(const QString& jid, bool joined) +{ + std::map::const_iterator cItr = conferences.find(jid); + if (cItr == conferences.end()) { + qDebug() << "An attempt to set joined to the non existing room" << jid << "of the account" << getName() << ", skipping"; + return; + } + + cItr->second->setJoined(joined); +} diff --git a/core/account.h b/core/account.h index d060f4b..b8973ba 100644 --- a/core/account.h +++ b/core/account.h @@ -71,6 +71,9 @@ public: void removeContactRequest(const QString& jid); void addContactRequest(const QString& jid, const QString& name, const QSet& groups); + void setRoomJoined(const QString& jid, bool joined); + void setRoomAutoJoin(const QString& jid, bool joined); + signals: void connectionStateChanged(int); void availabilityChanged(int); diff --git a/core/squawk.cpp b/core/squawk.cpp index 4e64c4d..6de9064 100644 --- a/core/squawk.cpp +++ b/core/squawk.cpp @@ -419,3 +419,24 @@ void Core::Squawk::onAccountRemoveRoom(const QString jid) Account* acc = static_cast(sender()); emit removeRoom(acc->getName(), jid); } + +void Core::Squawk::setRoomJoined(const QString& account, const QString& jid, bool joined) +{ + AccountsMap::const_iterator itr = amap.find(account); + if (itr == amap.end()) { + qDebug() << "An attempt to set autoJoin to the room" << jid << "of non existing account" << account << ", skipping"; + return; + } + itr->second->setRoomJoined(jid, joined); +} + +void Core::Squawk::setRoomAutoJoin(const QString& account, const QString& jid, bool joined) +{ + AccountsMap::const_iterator itr = amap.find(account); + if (itr == amap.end()) { + qDebug() << "An attempt to set autoJoin to the room" << jid << "of non existing account" << account << ", skipping"; + return; + } + itr->second->setRoomAutoJoin(jid, joined); +} + diff --git a/core/squawk.h b/core/squawk.h index d75ff15..8accae4 100644 --- a/core/squawk.h +++ b/core/squawk.h @@ -74,6 +74,8 @@ public slots: void unsubscribeContact(const QString& account, const QString& jid, const QString& reason); void removeContactRequest(const QString& account, const QString& jid); void addContactRequest(const QString& account, const QString& jid, const QString& name, const QSet& groups); + void setRoomJoined(const QString& account, const QString& jid, bool joined); + void setRoomAutoJoin(const QString& account, const QString& jid, bool joined); private: typedef std::deque Accounts; diff --git a/main.cpp b/main.cpp index efda89c..4c48fa0 100644 --- a/main.cpp +++ b/main.cpp @@ -81,6 +81,9 @@ int main(int argc, char *argv[]) QObject::connect(&w, SIGNAL(removeContactRequest(const QString&, const QString&)), squawk, SLOT(removeContactRequest(const QString&, const QString&))); + QObject::connect(&w, SIGNAL(setRoomJoined(const QString&, const QString&, bool)), squawk, SLOT(setRoomJoined(const QString&, const QString&, bool))); + QObject::connect(&w, SIGNAL(setRoomAutoJoin(const QString&, const QString&, bool)), squawk, SLOT(setRoomAutoJoin(const QString&, const QString&, bool))); + QObject::connect(squawk, SIGNAL(newAccount(const QMap&)), &w, SLOT(newAccount(const QMap&))); QObject::connect(squawk, SIGNAL(addContact(const QString&, const QString&, const QString&, const QMap&)), &w, SLOT(addContact(const QString&, const QString&, const QString&, const QMap&))); diff --git a/ui/squawk.cpp b/ui/squawk.cpp index ee17ce5..c1c822e 100644 --- a/ui/squawk.cpp +++ b/ui/squawk.cpp @@ -247,6 +247,10 @@ void Squawk::onRosterItemDoubleClicked(const QModelIndex& item) } else if (room != 0) { created = true; conv = new Room(room); + + if (!room->getJoined()) { + emit setRoomJoined(id->account, id->name, true); + } } if (conv != 0) { @@ -283,11 +287,18 @@ void Squawk::onConversationShown() void Squawk::onConversationClosed(QObject* parent) { Conversation* conv = static_cast(sender()); - Conversations::const_iterator itr = conversations.find({conv->getAccount(), conv->getJid()}); + Models::Roster::ElId id(conv->getAccount(), conv->getJid()); + Conversations::const_iterator itr = conversations.find(id); if (itr == conversations.end()) { qDebug() << "Conversation has been closed but can not be found among other opened conversations, application is most probably going to crash"; return; } + if (conv->isMuc) { + Room* room = static_cast(conv); + if (room->autoJoined()) { + emit setRoomJoined(id.account, id.name, false); + } + } conversations.erase(itr); } diff --git a/ui/squawk.h b/ui/squawk.h index 2d655be..ae26bd3 100644 --- a/ui/squawk.h +++ b/ui/squawk.h @@ -60,6 +60,8 @@ signals: void unsubscribeContact(const QString& account, const QString& jid, const QString& reason); void removeContactRequest(const QString& account, const QString& jid); void addContactRequest(const QString& account, const QString& jid, const QString& name, const QSet& groups); + void setRoomJoined(const QString& account, const QString& jid, bool joined); + void setRoomAutoJoin(const QString& account, const QString& jid, bool joined); public slots: void newAccount(const QMap& account); diff --git a/ui/widgets/chat.cpp b/ui/widgets/chat.cpp index 4822cb6..7739b8e 100644 --- a/ui/widgets/chat.cpp +++ b/ui/widgets/chat.cpp @@ -19,7 +19,7 @@ #include "chat.h" Chat::Chat(Models::Contact* p_contact, QWidget* parent): - Conversation(p_contact->getAccountJid(), p_contact->getAccountResource(), p_contact->getJid(), "", p_contact->getAccountName(), parent), + Conversation(false, p_contact->getAccountJid(), p_contact->getAccountResource(), p_contact->getJid(), "", p_contact->getAccountName(), parent), contact(p_contact) { setName(p_contact->getContactName()); diff --git a/ui/widgets/conversation.cpp b/ui/widgets/conversation.cpp index e67f67c..2eccd9c 100644 --- a/ui/widgets/conversation.cpp +++ b/ui/widgets/conversation.cpp @@ -24,14 +24,15 @@ #include #include -Conversation::Conversation(const QString& mJid, const QString mRes, const QString pJid, const QString pRes, const QString& acc, QWidget* parent): +Conversation::Conversation(bool muc, const QString& mJid, const QString mRes, const QString pJid, const QString pRes, const QString& acc, QWidget* parent): QWidget(parent), + isMuc(muc), myJid(mJid), myResource(mRes), palJid(pJid), activePalResource(pRes), account(acc), - line(new MessageLine()), + line(new MessageLine(muc)), m_ui(new Ui::Conversation()), ker(), thread(), diff --git a/ui/widgets/conversation.h b/ui/widgets/conversation.h index ff1c53b..43fa4ec 100644 --- a/ui/widgets/conversation.h +++ b/ui/widgets/conversation.h @@ -46,7 +46,7 @@ class Conversation : public QWidget { Q_OBJECT public: - Conversation(const QString& mJid, const QString mRes, const QString pJid, const QString pRes, const QString& acc, QWidget* parent = 0); + Conversation(bool muc, const QString& mJid, const QString mRes, const QString pJid, const QString pRes, const QString& acc, QWidget* parent = 0); ~Conversation(); QString getJid() const; @@ -75,6 +75,9 @@ protected slots: void onAttach(); void onFileSelected(); +public: + const bool isMuc; + protected: enum Scroll { nothing, diff --git a/ui/widgets/messageline.cpp b/ui/widgets/messageline.cpp index 62b808f..f4ec827 100644 --- a/ui/widgets/messageline.cpp +++ b/ui/widgets/messageline.cpp @@ -23,7 +23,7 @@ const QRegExp urlReg("^(?!getAccountJid(), p_room->getAccountResource(), p_room->getJid(), "", p_room->getAccountName(), parent), + Conversation(true, p_room->getAccountJid(), p_room->getAccountResource(), p_room->getJid(), "", p_room->getAccountName(), parent), room(p_room) { setName(p_room->getName()); line->setMyName(room->getNick()); - line->setRoom(true); } Room::~Room() @@ -44,3 +43,8 @@ void Room::handleSendMessage(const QString& text) msg.setCurrentTime(); emit sendMessage(msg); } + +bool Room::autoJoined() const +{ + return room->getAutoJoin(); +} diff --git a/ui/widgets/room.h b/ui/widgets/room.h index e328f8c..6f3b9f2 100644 --- a/ui/widgets/room.h +++ b/ui/widgets/room.h @@ -32,6 +32,8 @@ public: Room(Models::Room* p_room, QWidget* parent = 0); ~Room(); + bool autoJoined() const; + protected: void handleSendMessage(const QString & text) override;