From 71c3f997b495208332fbf0b3ea1142e1ff71982d Mon Sep 17 00:00:00 2001 From: Anthony Bilinski Date: Tue, 29 Mar 2022 03:42:50 -0700 Subject: [PATCH] refactor: Move FriendList global state into class --- src/friendlist.cpp | 3 - src/friendlist.h | 22 ++-- src/grouplist.cpp | 4 +- src/grouplist.h | 4 +- src/model/chathistory.cpp | 5 +- src/model/chathistory.h | 4 +- src/model/chatroom/groupchatroom.cpp | 9 +- src/model/chatroom/groupchatroom.h | 5 +- src/model/group.cpp | 8 +- src/model/group.h | 6 +- src/model/sessionchatlog.cpp | 13 ++- src/model/sessionchatlog.h | 8 +- src/widget/circlewidget.cpp | 16 +-- src/widget/circlewidget.h | 4 +- src/widget/contentdialog.cpp | 10 +- src/widget/contentdialog.h | 5 +- src/widget/contentdialogmanager.cpp | 7 +- src/widget/contentdialogmanager.h | 2 + src/widget/form/chatform.cpp | 4 +- src/widget/form/chatform.h | 3 +- src/widget/form/filesform.cpp | 12 ++- src/widget/form/filesform.h | 6 +- src/widget/form/genericchatform.cpp | 6 +- src/widget/form/genericchatform.h | 7 +- src/widget/form/groupchatform.cpp | 10 +- src/widget/form/groupchatform.h | 4 +- src/widget/friendlistwidget.cpp | 9 +- src/widget/friendlistwidget.h | 5 +- src/widget/widget.cpp | 88 ++++++++-------- src/widget/widget.h | 2 + test/model/groupmessagedispatcher_test.cpp | 6 +- test/model/notificationgenerator_test.cpp | 15 +-- test/model/sessionchatlog_test.cpp | 4 +- test/widget/filesform_test.cpp | 115 ++++++++++----------- 34 files changed, 245 insertions(+), 186 deletions(-) diff --git a/src/friendlist.cpp b/src/friendlist.cpp index 65598b6dd..60ca877e9 100644 --- a/src/friendlist.cpp +++ b/src/friendlist.cpp @@ -26,9 +26,6 @@ #include #include -QHash FriendList::friendList; -QHash FriendList::id2key; - Friend* FriendList::addFriend(uint32_t friendId, const ToxPk& friendPk, Settings& settings) { auto friendChecker = friendList.find(friendPk); diff --git a/src/friendlist.h b/src/friendlist.h index de3005575..9d2833fd6 100644 --- a/src/friendlist.h +++ b/src/friendlist.h @@ -21,10 +21,10 @@ #include +#include + template class QList; -template -class QHash; class Friend; class QByteArray; class QString; @@ -34,15 +34,15 @@ class Settings; class FriendList { public: - static Friend* addFriend(uint32_t friendId, const ToxPk& friendPk, Settings& settings); - static Friend* findFriend(const ToxPk& friendPk); - static const ToxPk& id2Key(uint32_t friendId); - static QList getAllFriends(); - static void removeFriend(const ToxPk& friendPk, Settings& settings, bool fake = false); - static void clear(); - static QString decideNickname(const ToxPk& friendPk, const QString& origName); + Friend* addFriend(uint32_t friendId, const ToxPk& friendPk, Settings& settings); + Friend* findFriend(const ToxPk& friendPk); + const ToxPk& id2Key(uint32_t friendId); + QList getAllFriends(); + void removeFriend(const ToxPk& friendPk, Settings& settings, bool fake = false); + void clear(); + QString decideNickname(const ToxPk& friendPk, const QString& origName); private: - static QHash friendList; - static QHash id2key; + QHash friendList; + QHash id2key; }; diff --git a/src/grouplist.cpp b/src/grouplist.cpp index e0c640c23..6d2117e38 100644 --- a/src/grouplist.cpp +++ b/src/grouplist.cpp @@ -26,14 +26,14 @@ QHash GroupList::groupList; QHash GroupList::id2key; Group* GroupList::addGroup(Core& core, int groupNum, const GroupId& groupId, const QString& name, bool isAvGroupchat, - const QString& selfName) + const QString& selfName, FriendList& friendList) { auto checker = groupList.find(groupId); if (checker != groupList.end()) { qWarning() << "addGroup: groupId already taken"; } - Group* newGroup = new Group(groupNum, groupId, name, isAvGroupchat, selfName, core, core); + Group* newGroup = new Group(groupNum, groupId, name, isAvGroupchat, selfName, core, core, friendList); groupList[groupId] = newGroup; id2key[groupNum] = groupId; return newGroup; diff --git a/src/grouplist.h b/src/grouplist.h index 522b0a7d7..a0bd28980 100644 --- a/src/grouplist.h +++ b/src/grouplist.h @@ -28,11 +28,13 @@ template class QList; class Group; class QString; +class FriendList; class GroupList { public: - static Group* addGroup(Core& core, int groupNum, const GroupId& persistentGroupId, const QString& name, bool isAvGroupchat, const QString& selfName); + static Group* addGroup(Core& core, int groupNum, const GroupId& persistentGroupId, + const QString& name, bool isAvGroupchat, const QString& selfName, FriendList& friendList); static Group* findGroup(const GroupId& groupId); static const GroupId& id2Key(uint32_t groupNum); static void removeGroup(const GroupId& groupId, bool fake = false); diff --git a/src/model/chathistory.cpp b/src/model/chathistory.cpp index ffedcfdaf..c14153c60 100644 --- a/src/model/chathistory.cpp +++ b/src/model/chathistory.cpp @@ -71,12 +71,13 @@ bool handleActionPrefix(QString& content) } // namespace ChatHistory::ChatHistory(Friend& f_, History* history_, const ICoreIdHandler& coreIdHandler_, - const Settings& settings_, IMessageDispatcher& messageDispatcher) + const Settings& settings_, IMessageDispatcher& messageDispatcher, + FriendList& friendList) : f(f_) , history(history_) , settings(settings_) , coreIdHandler(coreIdHandler_) - , sessionChatLog(getInitialChatLogIdx(), coreIdHandler_) + , sessionChatLog(getInitialChatLogIdx(), coreIdHandler_, friendList) { connect(&messageDispatcher, &IMessageDispatcher::messageComplete, this, &ChatHistory::onMessageComplete); diff --git a/src/model/chathistory.h b/src/model/chathistory.h index 9a88a8e0a..bcc9927cc 100644 --- a/src/model/chathistory.h +++ b/src/model/chathistory.h @@ -27,13 +27,15 @@ #include class Settings; +class FriendList; class ChatHistory : public IChatLog { Q_OBJECT public: ChatHistory(Friend& f_, History* history_, const ICoreIdHandler& coreIdHandler_, - const Settings& settings_, IMessageDispatcher& messageDispatcher); + const Settings& settings_, IMessageDispatcher& messageDispatcher, + FriendList& friendList); const ChatLogItem& at(ChatLogIdx idx) const override; SearchResult searchForward(SearchPos startIdx, const QString& phrase, const ParameterSearch& parameter) const override; diff --git a/src/model/chatroom/groupchatroom.cpp b/src/model/chatroom/groupchatroom.cpp index 039676c67..f637c9f9e 100644 --- a/src/model/chatroom/groupchatroom.cpp +++ b/src/model/chatroom/groupchatroom.cpp @@ -27,11 +27,14 @@ #include "src/model/group.h" #include "src/model/status.h" #include "src/persistence/settings.h" +#include "src/friendlist.h" -GroupChatroom::GroupChatroom(Group* group_, IDialogsManager* dialogsManager_, Core& core_) +GroupChatroom::GroupChatroom(Group* group_, IDialogsManager* dialogsManager_, Core& core_, + FriendList& friendList_) : group{group_} , dialogsManager{dialogsManager_} , core{core_} + , friendList{friendList_} { } @@ -58,12 +61,12 @@ void GroupChatroom::resetEventFlags() bool GroupChatroom::friendExists(const ToxPk& pk) { - return FriendList::findFriend(pk) != nullptr; + return friendList.findFriend(pk) != nullptr; } void GroupChatroom::inviteFriend(const ToxPk& pk) { - const Friend* frnd = FriendList::findFriend(pk); + const Friend* frnd = friendList.findFriend(pk); const auto friendId = frnd->getId(); const auto groupId = group->getId(); const auto canInvite = Status::isOnline(frnd->getStatus()); diff --git a/src/model/chatroom/groupchatroom.h b/src/model/chatroom/groupchatroom.h index 45353dc32..28a829430 100644 --- a/src/model/chatroom/groupchatroom.h +++ b/src/model/chatroom/groupchatroom.h @@ -27,12 +27,14 @@ class Core; class IDialogsManager; class Group; class ToxPk; +class FriendList; class GroupChatroom : public QObject, public Chatroom { Q_OBJECT public: - GroupChatroom(Group* group_, IDialogsManager* dialogsManager_, Core& core_); + GroupChatroom(Group* group_, IDialogsManager* dialogsManager_, Core& core_, + FriendList& friendList); Chat* getChat() override; @@ -52,4 +54,5 @@ private: Group* group{nullptr}; IDialogsManager* dialogsManager{nullptr}; Core& core; + FriendList& friendList; }; diff --git a/src/model/group.cpp b/src/model/group.cpp index ac2827659..6fcb5c5ca 100644 --- a/src/model/group.cpp +++ b/src/model/group.cpp @@ -33,7 +33,8 @@ const int MAX_GROUP_TITLE_LENGTH = 128; } // namespace Group::Group(int groupId_, const GroupId persistentGroupId, const QString& name, bool isAvGroupchat, - const QString& selfName_, ICoreGroupQuery& groupQuery_, ICoreIdHandler& idHandler_) + const QString& selfName_, ICoreGroupQuery& groupQuery_, ICoreIdHandler& idHandler_, + FriendList& friendList_) : groupQuery(groupQuery_) , idHandler(idHandler_) , selfName{selfName_} @@ -41,6 +42,7 @@ Group::Group(int groupId_, const GroupId persistentGroupId, const QString& name, , toxGroupNum(groupId_) , groupId{persistentGroupId} , avGroupchat{isAvGroupchat} + , friendList{friendList_} { // in groupchats, we only notify on messages containing your name <-- dumb // sound notifications should be on all messages, but system popup notification @@ -99,7 +101,7 @@ void Group::regeneratePeerList() if (pk == idHandler.getSelfPublicKey()) { peerDisplayNames[pk] = idHandler.getUsername(); } else { - peerDisplayNames[pk] = FriendList::decideNickname(pk, peers[i]); + peerDisplayNames[pk] = friendList.decideNickname(pk, peers[i]); } } for (const auto& pk : oldPeerNames.keys()) { @@ -124,7 +126,7 @@ void Group::regeneratePeerList() void Group::updateUsername(ToxPk pk, const QString newName) { - const QString displayName = FriendList::decideNickname(pk, newName); + const QString displayName = friendList.decideNickname(pk, newName); assert(peerDisplayNames.contains(pk)); if (peerDisplayNames[pk] != displayName) { // there could be no actual change even if their username changed due to an alias being set diff --git a/src/model/group.h b/src/model/group.h index 5bd4b7a57..c03e5bb9d 100644 --- a/src/model/group.h +++ b/src/model/group.h @@ -31,12 +31,15 @@ #include #include +class FriendList; + class Group : public Chat { Q_OBJECT public: Group(int groupId_, const GroupId persistentGroupId, const QString& name, bool isAvGroupchat, - const QString& selfName_, ICoreGroupQuery& groupQuery_, ICoreIdHandler& idHandler_); + const QString& selfName_, ICoreGroupQuery& groupQuery_, ICoreIdHandler& idHandler_, + FriendList& friendList); bool isAvGroupchat() const; uint32_t getId() const override; const GroupId& getPersistentId() const override; @@ -79,4 +82,5 @@ private: int toxGroupNum; const GroupId groupId; bool avGroupchat; + FriendList& friendList; }; diff --git a/src/model/sessionchatlog.cpp b/src/model/sessionchatlog.cpp index ae426ab1e..75f8df127 100644 --- a/src/model/sessionchatlog.cpp +++ b/src/model/sessionchatlog.cpp @@ -107,9 +107,9 @@ firstItemAfterDate(QDate date, const std::map& items) }); } -QString resolveToxPk(const ToxPk& pk) +QString resolveToxPk(FriendList& friendList, const ToxPk& pk) { - Friend* f = FriendList::findFriend(pk); + Friend* f = friendList.findFriend(pk); if (f) { return f->getDisplayedName(); } @@ -125,16 +125,19 @@ QString resolveToxPk(const ToxPk& pk) } } // namespace -SessionChatLog::SessionChatLog(const ICoreIdHandler& coreIdHandler_) +SessionChatLog::SessionChatLog(const ICoreIdHandler& coreIdHandler_, FriendList& friendList_) : coreIdHandler(coreIdHandler_) + , friendList{friendList_} {} /** * @brief Alternate constructor that allows for an initial index to be set */ -SessionChatLog::SessionChatLog(ChatLogIdx initialIdx, const ICoreIdHandler& coreIdHandler_) +SessionChatLog::SessionChatLog(ChatLogIdx initialIdx, const ICoreIdHandler& coreIdHandler_, + FriendList& friendList_) : coreIdHandler(coreIdHandler_) , nextIdx(initialIdx) + , friendList{friendList_} {} SessionChatLog::~SessionChatLog() = default; @@ -144,7 +147,7 @@ QString SessionChatLog::resolveSenderNameFromSender(const ToxPk& sender) bool isSelf = sender == coreIdHandler.getSelfPublicKey(); QString myNickName = coreIdHandler.getUsername().isEmpty() ? sender.toString() : coreIdHandler.getUsername(); - return isSelf ? myNickName : resolveToxPk(sender); + return isSelf ? myNickName : resolveToxPk(friendList, sender); } const ChatLogItem& SessionChatLog::at(ChatLogIdx idx) const diff --git a/src/model/sessionchatlog.h b/src/model/sessionchatlog.h index 81b78fdca..116c21e2e 100644 --- a/src/model/sessionchatlog.h +++ b/src/model/sessionchatlog.h @@ -26,14 +26,15 @@ #include struct SessionChatLogMetadata; - +class FriendList; class SessionChatLog : public IChatLog { Q_OBJECT public: - SessionChatLog(const ICoreIdHandler& coreIdHandler_); - SessionChatLog(ChatLogIdx initialIdx, const ICoreIdHandler& coreIdHandler_); + SessionChatLog(const ICoreIdHandler& coreIdHandler_, FriendList& friendList); + SessionChatLog(ChatLogIdx initialIdx, const ICoreIdHandler& coreIdHandler_, + FriendList& friendList); ~SessionChatLog(); const ChatLogItem& at(ChatLogIdx idx) const override; @@ -95,4 +96,5 @@ private: * is marked as completed */ QMap outgoingMessages; + FriendList& friendList; }; diff --git a/src/widget/circlewidget.cpp b/src/widget/circlewidget.cpp index 33bf2ac6c..c205c8a66 100644 --- a/src/widget/circlewidget.cpp +++ b/src/widget/circlewidget.cpp @@ -41,13 +41,15 @@ QHash CircleWidget::circleList; CircleWidget::CircleWidget(const Core &core_, FriendListWidget* parent, int id_, - Settings& settings_, Style& style_, IMessageBoxManager& messageBoxManager_) + Settings& settings_, Style& style_, IMessageBoxManager& messageBoxManager_, + FriendList& friendList_) : CategoryWidget(isCompact(), settings_, style_, parent) , id(id_) , core{core_} , settings{settings_} , style{style_} , messageBoxManager{messageBoxManager_} + , friendList{friendList_} { setName(settings.getCircleName(id), false); circleList[id] = this; @@ -103,10 +105,10 @@ void CircleWidget::contextMenuEvent(QContextMenuEvent* event) if (selectedItem == renameAction) { editName(); } else if (selectedItem == removeAction) { - FriendListWidget* friendList = static_cast(parentWidget()); - moveFriendWidgets(friendList); + FriendListWidget* friendListWidget = static_cast(parentWidget()); + moveFriendWidgets(friendListWidget); - friendList->removeCircleWidget(this); + friendListWidget->removeCircleWidget(this); int replacedCircle = settings.removeCircle(id); @@ -118,7 +120,7 @@ void CircleWidget::contextMenuEvent(QContextMenuEvent* event) circleList.remove(replacedCircle); } else if (selectedItem == openAction) { - ContentDialog* dialog = new ContentDialog(core, settings, style, messageBoxManager); + ContentDialog* dialog = new ContentDialog(core, settings, style, messageBoxManager, friendList); emit newContentDialog(*dialog); for (int i = 0; i < friendOnlineLayout()->count(); ++i) { QWidget* const widget = friendOnlineLayout()->itemAt(i)->widget(); @@ -151,7 +153,7 @@ void CircleWidget::dragEnterEvent(QDragEnterEvent* event) return; } ToxPk toxPk(event->mimeData()->data("toxPk")); - Friend* f = FriendList::findFriend(toxPk); + Friend* f = friendList.findFriend(toxPk); if (f != nullptr) event->acceptProposedAction(); @@ -179,7 +181,7 @@ void CircleWidget::dropEvent(QDropEvent* event) } // Check, that the user has a friend with the same ToxId ToxPk toxPk{event->mimeData()->data("toxPk")}; - Friend* f = FriendList::findFriend(toxPk); + Friend* f = friendList.findFriend(toxPk); if (!f) return; diff --git a/src/widget/circlewidget.h b/src/widget/circlewidget.h index 72091165c..6559a4cb7 100644 --- a/src/widget/circlewidget.h +++ b/src/widget/circlewidget.h @@ -26,13 +26,14 @@ class Core; class Settings; class Style; class IMessageBoxManager; +class FriendList; class CircleWidget final : public CategoryWidget { Q_OBJECT public: CircleWidget(const Core& core_, FriendListWidget* parent, int id_, Settings& settings, - Style& style, IMessageBoxManager& messageboxManager); + Style& style, IMessageBoxManager& messageboxManager, FriendList& friendList); ~CircleWidget(); void editName(); @@ -61,4 +62,5 @@ private: Settings& settings; Style& style; IMessageBoxManager& messageBoxManager; + FriendList& friendList; }; diff --git a/src/widget/contentdialog.cpp b/src/widget/contentdialog.cpp index 14090a366..0f19a4908 100644 --- a/src/widget/contentdialog.cpp +++ b/src/widget/contentdialog.cpp @@ -54,7 +54,8 @@ const QSize defaultSize(720, 400); } // namespace ContentDialog::ContentDialog(const Core &core, Settings& settings_, - Style& style_, IMessageBoxManager& messageBoxManager_, QWidget* parent) + Style& style_, IMessageBoxManager& messageBoxManager_, FriendList& friendList_, + QWidget* parent) : ActivateDialog(style_, parent, Qt::Window) , splitter{new QSplitter(this)} , friendLayout{new FriendListLayout(this)} @@ -64,6 +65,7 @@ ContentDialog::ContentDialog(const Core &core, Settings& settings_, , settings{settings_} , style{style_} , messageBoxManager{messageBoxManager_} + , friendList{friendList_} { friendLayout->setMargin(0); friendLayout->setSpacing(0); @@ -481,7 +483,7 @@ void ContentDialog::dragEnterEvent(QDragEnterEvent* event) if (frnd) { assert(event->mimeData()->hasFormat("toxPk")); ToxPk toxPk{event->mimeData()->data("toxPk")}; - Friend* contact = FriendList::findFriend(toxPk); + Friend* contact = friendList.findFriend(toxPk); if (!contact) { return; } @@ -514,7 +516,7 @@ void ContentDialog::dropEvent(QDropEvent* event) if (frnd) { assert(event->mimeData()->hasFormat("toxPk")); const ToxPk toxId(event->mimeData()->data("toxPk")); - Friend* contact = FriendList::findFriend(toxId); + Friend* contact = friendList.findFriend(toxId); if (!contact) { return; } @@ -647,7 +649,7 @@ void ContentDialog::setStatusMessage(const ToxPk& friendPk, const QString& messa void ContentDialog::updateFriendWidget(const ToxPk& friendPk, QString alias) { std::ignore = alias; - Friend* f = FriendList::findFriend(friendPk); + Friend* f = friendList.findFriend(friendPk); FriendWidget* friendWidget = qobject_cast(chatWidgets[friendPk]); Status::Status status = f->getStatus(); diff --git a/src/widget/contentdialog.h b/src/widget/contentdialog.h index bf184f7c8..9152bb9c7 100644 --- a/src/widget/contentdialog.h +++ b/src/widget/contentdialog.h @@ -48,13 +48,15 @@ class QScrollArea; class Settings; class Style; class IMessageBoxManager; +class FriendList; class ContentDialog : public ActivateDialog, public IDialogs { Q_OBJECT public: ContentDialog(const Core& core, Settings& settings, Style& style, - IMessageBoxManager& messageBoxManager, QWidget* parent = nullptr); + IMessageBoxManager& messageBoxManager, FriendList& friendList, + QWidget* parent = nullptr); ~ContentDialog() override; FriendWidget* addFriend(std::shared_ptr chatroom, GenericChatForm* form); @@ -142,4 +144,5 @@ private: Settings& settings; Style& style; IMessageBoxManager& messageBoxManager; + FriendList& friendList; }; diff --git a/src/widget/contentdialogmanager.cpp b/src/widget/contentdialogmanager.cpp index eb497a32a..c05404a4d 100644 --- a/src/widget/contentdialogmanager.cpp +++ b/src/widget/contentdialogmanager.cpp @@ -41,6 +41,11 @@ void removeDialog(ContentDialog* dialog, QHash& d } } // namespace +ContentDialogManager::ContentDialogManager(FriendList& friendList_) + : friendList{friendList_} +{ +} + ContentDialog* ContentDialogManager::current() { return currentDialog; @@ -132,7 +137,7 @@ void ContentDialogManager::updateFriendStatus(const ToxPk& friendPk) dialog->updateTitleAndStatusIcon(); } - Friend* f = FriendList::findFriend(friendPk); + Friend* f = friendList.findFriend(friendPk); dialog->updateFriendStatus(friendPk, f->getStatus()); } diff --git a/src/widget/contentdialogmanager.h b/src/widget/contentdialogmanager.h index b9b025d1a..1ab58e87c 100644 --- a/src/widget/contentdialogmanager.h +++ b/src/widget/contentdialogmanager.h @@ -34,6 +34,7 @@ class ContentDialogManager : public QObject, public IDialogsManager { Q_OBJECT public: + explicit ContentDialogManager(FriendList& friendList); ContentDialog* current(); bool chatWidgetExists(const ChatId& chatId); void focusChat(const ChatId& chatId); @@ -64,4 +65,5 @@ private: ContentDialog* currentDialog = nullptr; QHash chatDialogs; + FriendList& friendList; }; diff --git a/src/widget/form/chatform.cpp b/src/widget/form/chatform.cpp index 5c4188443..9ed47bd19 100644 --- a/src/widget/form/chatform.cpp +++ b/src/widget/form/chatform.cpp @@ -110,9 +110,9 @@ ChatForm::ChatForm(Profile& profile, Friend* chatFriend, IChatLog& chatLog_, IMessageDispatcher& messageDispatcher_, DocumentCache& documentCache_, SmileyPack& smileyPack_, CameraSource& cameraSource_, Settings& settings_, Style& style_, IMessageBoxManager& messageBoxManager, - ContentDialogManager& contentDialogManager_) + ContentDialogManager& contentDialogManager_, FriendList& friendList_) : GenericChatForm(profile.getCore(), chatFriend, chatLog_, messageDispatcher_, - documentCache_, smileyPack_, settings_, style_, messageBoxManager) + documentCache_, smileyPack_, settings_, style_, messageBoxManager, friendList_) , core{profile.getCore()} , f(chatFriend) , isTyping{false} diff --git a/src/widget/form/chatform.h b/src/widget/form/chatform.h index 39336ce3b..1978d02bc 100644 --- a/src/widget/form/chatform.h +++ b/src/widget/form/chatform.h @@ -48,6 +48,7 @@ class Settings; class Style; class IMessageBoxManager; class ContentDialogManager; +class FriendList; class ChatForm : public GenericChatForm { @@ -56,7 +57,7 @@ public: ChatForm(Profile& profile, Friend* chatFriend, IChatLog& chatLog_, IMessageDispatcher& messageDispatcher_, DocumentCache& documentCache, SmileyPack& smileyPack, CameraSource& cameraSource, Settings& settings, Style& style, IMessageBoxManager& messageBoxManager, - ContentDialogManager& contentDialogManager); + ContentDialogManager& contentDialogManager, FriendList& friendList); ~ChatForm() override; void setStatusMessage(const QString& newMessage); diff --git a/src/widget/form/filesform.cpp b/src/widget/form/filesform.cpp index 78774243f..dd6cd81b9 100644 --- a/src/widget/form/filesform.cpp +++ b/src/widget/form/filesform.cpp @@ -169,8 +169,9 @@ namespace FileTransferList return static_cast(in); } - Model::Model(QObject* parent) + Model::Model(FriendList& friendList_, QObject* parent) : QAbstractTableModel(parent) + , friendList{friendList_} {} QVariant Model::headerData(int section, Qt::Orientation orientation, int role) const @@ -271,7 +272,7 @@ namespace FileTransferList return files[row].fileName; case Column::contact: { - auto f = FriendList::findFriend(FriendList::id2Key(files[row].friendId)); + auto f = friendList.findFriend(friendList.id2Key(files[row].friendId)); if (f == nullptr) { qWarning("Invalid friend for file transfer"); return "Unknown"; @@ -429,7 +430,8 @@ namespace FileTransferList } // namespace FileTransferList -FilesForm::FilesForm(CoreFile& coreFile, Settings& settings, Style& style, IMessageBoxManager& messageBoxManager_) +FilesForm::FilesForm(CoreFile& coreFile, Settings& settings, Style& style, + IMessageBoxManager& messageBoxManager_, FriendList& friendList) : QObject() , messageBoxManager{messageBoxManager_} { @@ -440,8 +442,8 @@ FilesForm::FilesForm(CoreFile& coreFile, Settings& settings, Style& style, IMess head->setLayout(&headLayout); headLayout.addWidget(&headLabel); - recvdModel = new FileTransferList::Model(this); - sentModel = new FileTransferList::Model(this); + recvdModel = new FileTransferList::Model(friendList, this); + sentModel = new FileTransferList::Model(friendList, this); auto pauseFile = [&coreFile] (ToxFile file) { coreFile.pauseResumeFile(file.friendId, file.fileNum); diff --git a/src/widget/form/filesform.h b/src/widget/form/filesform.h index 96a9206a1..9e035d5e7 100644 --- a/src/widget/form/filesform.h +++ b/src/widget/form/filesform.h @@ -38,6 +38,7 @@ class Settings; class Style; class QFileInfo; class IMessageBoxManager; +class FriendList; namespace FileTransferList { @@ -69,7 +70,7 @@ namespace FileTransferList { Q_OBJECT public: - Model(QObject* parent = nullptr); + Model(FriendList& friendList, QObject* parent = nullptr); ~Model() = default; void onFileUpdated(const ToxFile& file); @@ -87,6 +88,7 @@ namespace FileTransferList private: QHash idToRow; std::vector files; + FriendList& friendList; }; class Delegate : public QStyledItemDelegate @@ -117,7 +119,7 @@ class FilesForm : public QObject public: FilesForm(CoreFile& coreFile, Settings& settings, Style& style, - IMessageBoxManager& messageBoxManager); + IMessageBoxManager& messageBoxManager, FriendList& friendList); ~FilesForm(); bool isShown() const; diff --git a/src/widget/form/genericchatform.cpp b/src/widget/form/genericchatform.cpp index 1b0dde22e..944d13a19 100644 --- a/src/widget/form/genericchatform.cpp +++ b/src/widget/form/genericchatform.cpp @@ -96,7 +96,7 @@ QString fontToCss(const QFont& font, const QString& name) */ QString GenericChatForm::resolveToxPk(const ToxPk& pk) { - Friend* f = FriendList::findFriend(pk); + Friend* f = friendList.findFriend(pk); if (f) { return f->getDisplayedName(); } @@ -139,7 +139,8 @@ QPushButton* createButton(const QString& name, T* self, Fun onClickSlot, GenericChatForm::GenericChatForm(const Core& core_, const Chat* chat, IChatLog& chatLog_, IMessageDispatcher& messageDispatcher_, DocumentCache& documentCache, SmileyPack& smileyPack_, Settings& settings_, Style& style_, - IMessageBoxManager& messageBoxManager, QWidget* parent_) + IMessageBoxManager& messageBoxManager, FriendList& friendList_, + QWidget* parent_) : QWidget(parent_, Qt::Window) , core{core_} , audioInputFlag(false) @@ -149,6 +150,7 @@ GenericChatForm::GenericChatForm(const Core& core_, const Chat* chat, IChatLog& , smileyPack{smileyPack_} , settings{settings_} , style{style_} + , friendList{friendList_} { curRow = 0; headWidget = new ChatFormHeader(settings, style); diff --git a/src/widget/form/genericchatform.h b/src/widget/form/genericchatform.h index 61f627520..3c8c0d8f6 100644 --- a/src/widget/form/genericchatform.h +++ b/src/widget/form/genericchatform.h @@ -58,6 +58,7 @@ class SmileyPack; class Settings; class Style; class IMessageBoxManager; +class FriendList; namespace Ui { class MainWindow; @@ -76,7 +77,8 @@ public: GenericChatForm(const Core& core_, const Chat* chat, IChatLog& chatLog_, IMessageDispatcher& messageDispatcher_, DocumentCache& documentCache, SmileyPack& smileyPack, Settings& settings, Style& style, - IMessageBoxManager& messageBoxmanager, QWidget* parent_ = nullptr); + IMessageBoxManager& messageBoxmanager, FriendList& friendList, + QWidget* parent_ = nullptr); ~GenericChatForm() override; void setName(const QString& newName); @@ -84,7 +86,7 @@ public: void addSystemInfoMessage(const QDateTime& datetime, SystemMessageType messageType, SystemMessage::Args messageArgs); - static QString resolveToxPk(const ToxPk& pk); + QString resolveToxPk(const ToxPk& pk); QDateTime getLatestTime() const; signals: @@ -175,4 +177,5 @@ protected: SmileyPack& smileyPack; Settings& settings; Style& style; + FriendList& friendList; }; diff --git a/src/widget/form/groupchatform.cpp b/src/widget/form/groupchatform.cpp index 8929d8b22..d7b87a62b 100644 --- a/src/widget/form/groupchatform.cpp +++ b/src/widget/form/groupchatform.cpp @@ -85,14 +85,16 @@ QString editName(const QString& name) GroupChatForm::GroupChatForm(Core& core_, Group* chatGroup, IChatLog& chatLog_, IMessageDispatcher& messageDispatcher_, Settings& settings_, DocumentCache& documentCache_, - SmileyPack& smileyPack_, Style& style_, IMessageBoxManager& messageBoxManager) + SmileyPack& smileyPack_, Style& style_, IMessageBoxManager& messageBoxManager, + FriendList& friendList_) : GenericChatForm(core_, chatGroup, chatLog_, messageDispatcher_, - documentCache_, smileyPack_, settings_, style_, messageBoxManager) + documentCache_, smileyPack_, settings_, style_, messageBoxManager, friendList_) , core{core_} , group(chatGroup) , inCall(false) , settings(settings_) , style{style_} + , friendList{friendList_} { nusersLabel = new QLabel(); @@ -298,7 +300,7 @@ void GroupChatForm::dragEnterEvent(QDragEnterEvent* ev) return; } ToxPk toxPk{ev->mimeData()->data("toxPk")}; - Friend* frnd = FriendList::findFriend(toxPk); + Friend* frnd = friendList.findFriend(toxPk); if (frnd) ev->acceptProposedAction(); } @@ -309,7 +311,7 @@ void GroupChatForm::dropEvent(QDropEvent* ev) return; } ToxPk toxPk{ev->mimeData()->data("toxPk")}; - Friend* frnd = FriendList::findFriend(toxPk); + Friend* frnd = friendList.findFriend(toxPk); if (!frnd) return; diff --git a/src/widget/form/groupchatform.h b/src/widget/form/groupchatform.h index 728380708..0ccd68e7a 100644 --- a/src/widget/form/groupchatform.h +++ b/src/widget/form/groupchatform.h @@ -39,6 +39,7 @@ class DocumentCache; class SmileyPack; class Style; class IMessageBoxManager; +class FriendList; class GroupChatForm : public GenericChatForm { @@ -47,7 +48,7 @@ public: GroupChatForm(Core& core_, Group* chatGroup, IChatLog& chatLog_, IMessageDispatcher& messageDispatcher_, Settings& settings_, DocumentCache& documentCache, SmileyPack& smileyPack, Style& style, - IMessageBoxManager& messageBoxManager); + IMessageBoxManager& messageBoxManager, FriendList& friendList); ~GroupChatForm(); void peerAudioPlaying(ToxPk peerPk); @@ -89,4 +90,5 @@ private: bool inCall; Settings& settings; Style& style; + FriendList& friendList; }; diff --git a/src/widget/friendlistwidget.cpp b/src/widget/friendlistwidget.cpp index 3c09b3537..1a3b84bcc 100644 --- a/src/widget/friendlistwidget.cpp +++ b/src/widget/friendlistwidget.cpp @@ -101,12 +101,13 @@ qint64 timeUntilTomorrow() FriendListWidget::FriendListWidget(const Core &core_, Widget* parent, Settings& settings_, Style& style_, IMessageBoxManager& messageBoxManager_, - bool groupsOnTop) + FriendList& friendList_, bool groupsOnTop) : QWidget(parent) , core{core_} , settings{settings_} , style{style_} , messageBoxManager{messageBoxManager_} + , friendList{friendList_} { int countContacts = core.getFriendList().size(); manager = new FriendListManager(countContacts, this); @@ -525,7 +526,7 @@ void FriendListWidget::dragEnterEvent(QDragEnterEvent* event) return; } ToxPk toxPk(event->mimeData()->data("toxPk")); - Friend* frnd = FriendList::findFriend(toxPk); + Friend* frnd = friendList.findFriend(toxPk); if (frnd) event->acceptProposedAction(); } @@ -541,7 +542,7 @@ void FriendListWidget::dropEvent(QDropEvent* event) // Check, that the user has a friend with the same ToxPk assert(event->mimeData()->hasFormat("toxPk")); const ToxPk toxPk{event->mimeData()->data("toxPk")}; - Friend* f = FriendList::findFriend(toxPk); + Friend* f = friendList.findFriend(toxPk); if (!f) return; @@ -619,7 +620,7 @@ CircleWidget* FriendListWidget::createCircleWidget(int id) } CircleWidget* circleWidget = new CircleWidget(core, this, id, settings, style, - messageBoxManager); + messageBoxManager, friendList); emit connectCircleWidget(*circleWidget); connect(this, &FriendListWidget::onCompactChanged, circleWidget, &CircleWidget::onCompactChanged); connect(circleWidget, &CircleWidget::renameRequested, this, &FriendListWidget::renameCircleWidget); diff --git a/src/widget/friendlistwidget.h b/src/widget/friendlistwidget.h index 7014fab74..2a31ea06b 100644 --- a/src/widget/friendlistwidget.h +++ b/src/widget/friendlistwidget.h @@ -41,6 +41,7 @@ class IFriendListItem; class Settings; class Style; class IMessageBoxManager; +class FriendList; class FriendListWidget : public QWidget { @@ -48,7 +49,8 @@ class FriendListWidget : public QWidget public: using SortingMode = Settings::FriendListSortingMode; FriendListWidget(const Core& core, Widget* parent, Settings& settings, Style& style, - IMessageBoxManager& messageBoxManager, bool groupsOnTop = true); + IMessageBoxManager& messageBoxManager, FriendList& friendList, + bool groupsOnTop = true); ~FriendListWidget(); void setMode(SortingMode mode); SortingMode getMode() const; @@ -104,4 +106,5 @@ private: Settings& settings; Style& style; IMessageBoxManager& messageBoxManager; + FriendList& friendList; }; diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index 08f51608c..eaa442b56 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -160,7 +160,8 @@ Widget::Widget(Profile &profile_, IAudioControl& audio_, CameraSource& cameraSou , cameraSource{cameraSource_} , style{style_} , messageBoxManager(messageBoxManager_) - , contentDialogManager(new ContentDialogManager()) + , friendList(new FriendList()) + , contentDialogManager(new ContentDialogManager(*friendList)) { installEventFilter(this); QString locale = settings.getTranslation(); @@ -267,7 +268,7 @@ void Widget::init() sharedMessageProcessorParams.reset(new MessageProcessor::SharedParams(core->getMaxMessageSize(), coreExt->getMaxExtendedMessageSize())); chatListWidget = new FriendListWidget(*core, this, settings, style, - messageBoxManager, settings.getGroupchatPosition()); + messageBoxManager, *friendList, settings.getGroupchatPosition()); connect(chatListWidget, &FriendListWidget::searchCircle, this, &Widget::searchCircle); connect(chatListWidget, &FriendListWidget::connectCircleWidget, this, &Widget::connectCircleWidget); @@ -295,7 +296,7 @@ void Widget::init() style.setThemeColor(settings, settings.getThemeColor()); CoreFile* coreFile = core->getCoreFile(); - filesForm = new FilesForm(*coreFile, settings, style, messageBoxManager); + filesForm = new FilesForm(*coreFile, settings, style, messageBoxManager, *friendList); addFriendForm = new AddFriendForm(core->getSelfId(), settings, style, messageBoxManager, *core); groupInviteForm = new GroupInviteForm(settings, *core); @@ -623,7 +624,7 @@ Widget::~Widget() removeGroup(g, true); } - for (Friend* f : FriendList::getAllFriends()) { + for (Friend* f : friendList->getAllFriends()) { removeFriend(f, true); } @@ -640,7 +641,7 @@ Widget::~Widget() delete contentLayout; delete settingsWidget; - FriendList::clear(); + friendList->clear(); GroupList::clear(); delete trayMenu; delete ui; @@ -1060,7 +1061,7 @@ void Widget::cleanupNotificationSound() void Widget::incomingNotification(uint32_t friendNum) { - const auto& friendId = FriendList::id2Key(friendNum); + const auto& friendId = friendList->id2Key(friendNum); newFriendMessageAlert(friendId, {}, false); // loop until call answered or rejected @@ -1091,8 +1092,8 @@ void Widget::onStopNotification() */ void Widget::dispatchFile(ToxFile file) { - const auto& friendId = FriendList::id2Key(file.friendId); - Friend* f = FriendList::findFriend(friendId); + const auto& friendId = friendList->id2Key(file.friendId); + Friend* f = friendList->findFriend(friendId); if (!f) { return; } @@ -1132,7 +1133,7 @@ void Widget::dispatchFileWithBool(ToxFile file, bool pausedOrBroken) void Widget::dispatchFileSendFailed(uint32_t friendId, const QString& fileName) { - const auto& friendPk = FriendList::id2Key(friendId); + const auto& friendPk = friendList->id2Key(friendId); auto chatForm = chatForms.find(friendPk); if (chatForm == chatForms.end()) { @@ -1154,7 +1155,7 @@ void Widget::addFriend(uint32_t friendId, const ToxPk& friendPk) assert(core != nullptr); settings.updateFriendAddress(friendPk.toString()); - Friend* newfriend = FriendList::addFriend(friendId, friendPk, settings); + Friend* newfriend = friendList->addFriend(friendId, friendPk, settings); auto rawChatroom = new FriendChatroom(newfriend, contentDialogManager.get(), *core, settings); std::shared_ptr chatroom(rawChatroom); const auto compact = settings.getCompactLayout(); @@ -1170,10 +1171,10 @@ void Widget::addFriend(uint32_t friendId, const ToxPk& friendPk) // ChatHistory hooks them up in a very specific order auto chatHistory = std::make_shared(*newfriend, history, *core, settings, - *friendMessageDispatcher); + *friendMessageDispatcher, *friendList); auto friendForm = new ChatForm(profile, newfriend, *chatHistory, *friendMessageDispatcher, *documentCache, *smileyPack, cameraSource, - settings, style, messageBoxManager, *contentDialogManager); + settings, style, messageBoxManager, *contentDialogManager, *friendList); connect(friendForm, &ChatForm::updateFriendActivity, this, &Widget::updateFriendActivity); friendMessageDispatchers[friendPk] = friendMessageDispatcher; @@ -1243,8 +1244,8 @@ void Widget::addFriendFailed(const ToxPk& userId, const QString& errorInfo) void Widget::onCoreFriendStatusChanged(int friendId, Status::Status status) { - const auto& friendPk = FriendList::id2Key(friendId); - Friend* f = FriendList::findFriend(friendPk); + const auto& friendPk = friendList->id2Key(friendId); + Friend* f = friendList->findFriend(friendPk); if (!f) { return; } @@ -1288,8 +1289,8 @@ void Widget::onFriendStatusChanged(const ToxPk& friendPk, Status::Status status) void Widget::onFriendStatusMessageChanged(int friendId, const QString& message) { - const auto& friendPk = FriendList::id2Key(friendId); - Friend* f = FriendList::findFriend(friendPk); + const auto& friendPk = friendList->id2Key(friendId); + Friend* f = friendList->findFriend(friendPk); if (!f) { return; } @@ -1322,8 +1323,8 @@ void Widget::onFriendDisplayedNameChanged(const QString& displayed) void Widget::onFriendUsernameChanged(int friendId, const QString& username) { - const auto& friendPk = FriendList::id2Key(friendId); - Friend* f = FriendList::findFriend(friendPk); + const auto& friendPk = friendList->id2Key(friendId); + Friend* f = friendList->findFriend(friendPk); if (!f) { return; } @@ -1408,8 +1409,8 @@ void Widget::openDialog(GenericChatroomWidget* widget, bool newWindow) void Widget::onFriendMessageReceived(uint32_t friendnumber, const QString& message, bool isAction) { - const auto& friendId = FriendList::id2Key(friendnumber); - Friend* f = FriendList::findFriend(friendId); + const auto& friendId = friendList->id2Key(friendnumber); + Friend* f = friendList->findFriend(friendId); if (!f) { return; } @@ -1419,8 +1420,8 @@ void Widget::onFriendMessageReceived(uint32_t friendnumber, const QString& messa void Widget::onReceiptReceived(int friendId, ReceiptNum receipt) { - const auto& friendKey = FriendList::id2Key(friendId); - Friend* f = FriendList::findFriend(friendKey); + const auto& friendKey = friendList->id2Key(friendId); + Friend* f = friendList->findFriend(friendKey); if (!f) { return; } @@ -1430,8 +1431,8 @@ void Widget::onReceiptReceived(int friendId, ReceiptNum receipt) void Widget::onExtendedMessageSupport(uint32_t friendNumber, bool supported) { - const auto& friendKey = FriendList::id2Key(friendNumber); - Friend* f = FriendList::findFriend(friendKey); + const auto& friendKey = friendList->id2Key(friendNumber); + Friend* f = friendList->findFriend(friendKey); if (!f) { return; } @@ -1441,13 +1442,13 @@ void Widget::onExtendedMessageSupport(uint32_t friendNumber, bool supported) void Widget::onFriendExtMessageReceived(uint32_t friendNumber, const QString& message) { - const auto& friendKey = FriendList::id2Key(friendNumber); + const auto& friendKey = friendList->id2Key(friendNumber); friendMessageDispatchers[friendKey]->onExtMessageReceived(message); } void Widget::onExtReceiptReceived(uint32_t friendNumber, uint64_t receiptId) { - const auto& friendKey = FriendList::id2Key(friendNumber); + const auto& friendKey = friendList->id2Key(friendNumber); friendMessageDispatchers[friendKey]->onExtReceiptReceived(receiptId); } @@ -1557,7 +1558,7 @@ bool Widget::newFriendMessageAlert(const ToxPk& friendId, const QString& text, b bool hasActive; QWidget* currentWindow; ContentDialog* contentDialog = contentDialogManager->getFriendDialog(friendId); - Friend* f = FriendList::findFriend(friendId); + Friend* f = friendList->findFriend(friendId); if (contentDialog != nullptr) { currentWindow = contentDialog->window(); @@ -1724,7 +1725,7 @@ void Widget::onFriendRequestReceived(const ToxPk& friendPk, const QString& messa void Widget::onFileReceiveRequested(const ToxFile& file) { - const ToxPk& friendPk = FriendList::id2Key(file.friendId); + const ToxPk& friendPk = friendList->id2Key(file.friendId); newFriendMessageAlert(friendPk, {}, true, file.fileName, file.progress.getFileSize()); } @@ -1771,7 +1772,7 @@ void Widget::removeFriend(Friend* f, bool fake) lastDialog->removeFriend(friendPk); } - FriendList::removeFriend(friendPk, settings, fake); + friendList->removeFriend(friendPk, settings, fake); if (!fake) { core->removeFriend(f->getId()); // aliases aren't supported for non-friend peers in groups, revert to basic username @@ -1796,7 +1797,7 @@ void Widget::removeFriend(Friend* f, bool fake) void Widget::removeFriend(const ToxPk& friendId) { - removeFriend(FriendList::findFriend(friendId), false); + removeFriend(friendList->findFriend(friendId), false); } void Widget::onDialogShown(GenericChatroomWidget* widget) @@ -1837,7 +1838,8 @@ void Widget::onUpdateAvailable() ContentDialog* Widget::createContentDialog() const { - ContentDialog* contentDialog = new ContentDialog(*core, settings, style, messageBoxManager); + ContentDialog* contentDialog = new ContentDialog(*core, settings, style, + messageBoxManager, *friendList); registerContentDialog(*contentDialog); return contentDialog; @@ -1948,7 +1950,7 @@ ContentLayout* Widget::createContentDialog(DialogType type) const void Widget::copyFriendIdToClipboard(const ToxPk& friendId) { - Friend* f = FriendList::findFriend(friendId); + Friend* f = friendList->findFriend(friendId); if (f != nullptr) { QClipboard* clipboard = QApplication::clipboard(); clipboard->setText(friendId.toString(), QClipboard::Clipboard); @@ -1958,8 +1960,8 @@ void Widget::copyFriendIdToClipboard(const ToxPk& friendId) void Widget::onGroupInviteReceived(const GroupInvite& inviteInfo) { const uint32_t friendId = inviteInfo.getFriendId(); - const ToxPk& friendPk = FriendList::id2Key(friendId); - const Friend* f = FriendList::findFriend(friendPk); + const ToxPk& friendPk = friendList->id2Key(friendId); + const Friend* f = friendList->findFriend(friendPk); updateFriendActivity(*f); const uint8_t confType = inviteInfo.getType(); @@ -2019,7 +2021,7 @@ void Widget::onGroupPeerNameChanged(uint32_t groupnumber, const ToxPk& peerPk, c Group* g = GroupList::findGroup(groupId); assert(g); - const QString setName = FriendList::decideNickname(peerPk, newName); + const QString setName = friendList->decideNickname(peerPk, newName); g->updateUsername(peerPk, newName); } @@ -2115,7 +2117,8 @@ Group* Widget::createGroup(uint32_t groupnumber, const GroupId& groupId) const auto groupName = tr("Groupchat #%1").arg(groupnumber); const bool enabled = core->getGroupAvEnabled(groupnumber); Group* newgroup = - GroupList::addGroup(*core, groupnumber, groupId, groupName, enabled, core->getUsername()); + GroupList::addGroup(*core, groupnumber, groupId, groupName, enabled, core->getUsername(), + *friendList); assert(newgroup); if (enabled) { @@ -2125,7 +2128,8 @@ Group* Widget::createGroup(uint32_t groupnumber, const GroupId& groupId) av->invalidateGroupCallPeerSource(*newgroup, user); }); } - auto rawChatroom = new GroupChatroom(newgroup, contentDialogManager.get(), *core); + auto rawChatroom = new GroupChatroom(newgroup, contentDialogManager.get(), *core, + *friendList); std::shared_ptr chatroom(rawChatroom); const auto compact = settings.getCompactLayout(); @@ -2134,7 +2138,7 @@ Group* Widget::createGroup(uint32_t groupnumber, const GroupId& groupId) auto messageDispatcher = std::make_shared(*newgroup, std::move(messageProcessor), *core, *core, settings); - auto groupChatLog = std::make_shared(*core); + auto groupChatLog = std::make_shared(*core, *friendList); connect(messageDispatcher.get(), &IMessageDispatcher::messageReceived, groupChatLog.get(), &SessionChatLog::onMessageReceived); @@ -2159,7 +2163,7 @@ Group* Widget::createGroup(uint32_t groupnumber, const GroupId& groupId) groupAlertConnections.insert(groupId, notifyReceivedConnection); auto form = new GroupChatForm(*core, newgroup, *groupChatLog, *messageDispatcher, - settings, *documentCache, *smileyPack, style, messageBoxManager); + settings, *documentCache, *smileyPack, style, messageBoxManager, *friendList); connect(&settings, &Settings::nameColorsChanged, form, &GenericChatForm::setColorizedNames); form->setColorizedNames(settings.getEnableGroupChatsColor()); groupMessageDispatchers[groupId] = messageDispatcher; @@ -2367,8 +2371,8 @@ void Widget::onGroupSendFailed(uint32_t groupnumber) void Widget::onFriendTypingChanged(uint32_t friendnumber, bool isTyping) { - const auto& friendId = FriendList::id2Key(friendnumber); - Friend* f = FriendList::findFriend(friendId); + const auto& friendId = friendList->id2Key(friendnumber); + Friend* f = friendList->findFriend(friendId); if (!f) { return; } @@ -2443,7 +2447,7 @@ bool Widget::filterOnline(FilterCriteria index) void Widget::clearAllReceipts() { - QList frnds = FriendList::getAllFriends(); + QList frnds = friendList->getAllFriends(); for (Friend* f : frnds) { friendMessageDispatchers[f->getPublicKey()]->clearOutgoingMessages(); } diff --git a/src/widget/widget.h b/src/widget/widget.h index 825a38c62..042165693 100644 --- a/src/widget/widget.h +++ b/src/widget/widget.h @@ -37,6 +37,7 @@ #include "src/core/toxpk.h" #include "src/model/friendmessagedispatcher.h" #include "src/model/groupmessagedispatcher.h" +#include "src/friendlist.h" #if DESKTOP_NOTIFICATIONS #include "src/model/notificationgenerator.h" #include "src/platform/desktop_notifications/desktopnotify.h" @@ -392,6 +393,7 @@ private: CameraSource& cameraSource; Style& style; IMessageBoxManager& messageBoxManager; + std::unique_ptr friendList; std::unique_ptr contentDialogManager; }; diff --git a/test/model/groupmessagedispatcher_test.cpp b/test/model/groupmessagedispatcher_test.cpp index 16ca28d99..b9a182f23 100644 --- a/test/model/groupmessagedispatcher_test.cpp +++ b/test/model/groupmessagedispatcher_test.cpp @@ -23,6 +23,7 @@ #include "src/model/message.h" #include "src/persistence/settings.h" #include "src/persistence/igroupsettings.h" +#include "src/friendlist.h" #include "util/interface.h" #include "mock/mockcoreidhandler.h" @@ -136,6 +137,7 @@ private: std::set outgoingMessages; std::deque sentMessages; std::deque receivedMessages; + std::unique_ptr friendList; }; TestGroupMessageDispatcher::TestGroupMessageDispatcher() {} @@ -145,11 +147,13 @@ TestGroupMessageDispatcher::TestGroupMessageDispatcher() {} */ void TestGroupMessageDispatcher::init() { + friendList = std::unique_ptr(new FriendList()); groupSettings = std::unique_ptr(new MockGroupSettings()); groupQuery = std::unique_ptr(new MockGroupQuery()); coreIdHandler = std::unique_ptr(new MockCoreIdHandler()); g = std::unique_ptr( - new Group(0, GroupId(), "TestGroup", false, "me", *groupQuery, *coreIdHandler)); + new Group(0, GroupId(), "TestGroup", false, "me", *groupQuery, *coreIdHandler, + *friendList)); messageSender = std::unique_ptr(new MockGroupMessageSender()); sharedProcessorParams = std::unique_ptr(new MessageProcessor::SharedParams(tox_max_message_length(), 10 * 1024 * 1024)); diff --git a/test/model/notificationgenerator_test.cpp b/test/model/notificationgenerator_test.cpp index 9e8e3fc0f..963f5f286 100644 --- a/test/model/notificationgenerator_test.cpp +++ b/test/model/notificationgenerator_test.cpp @@ -19,6 +19,7 @@ */ #include "src/model/notificationgenerator.h" +#include "src/friendlist.h" #include "mock/mockcoreidhandler.h" #include "mock/mockgroupquery.h" @@ -89,10 +90,12 @@ private: std::unique_ptr notificationGenerator; std::unique_ptr groupQuery; std::unique_ptr coreIdHandler; + std::unique_ptr friendList; }; void TestNotificationGenerator::init() { + friendList.reset(new FriendList()); notificationSettings.reset(new MockNotificationSettings()); notificationGenerator.reset(new NotificationGenerator(*notificationSettings, nullptr)); groupQuery.reset(new MockGroupQuery()); @@ -139,7 +142,7 @@ void TestNotificationGenerator::testNotificationClear() void TestNotificationGenerator::testGroupMessage() { - Group g(0, GroupId(0), "groupName", false, "selfName", *groupQuery, *coreIdHandler); + Group g(0, GroupId(0), "groupName", false, "selfName", *groupQuery, *coreIdHandler, *friendList); auto sender = groupQuery->getGroupPeerPk(0, 0); g.updateUsername(sender, "sender1"); @@ -150,7 +153,7 @@ void TestNotificationGenerator::testGroupMessage() void TestNotificationGenerator::testMultipleGroupMessages() { - Group g(0, GroupId(0), "groupName", false, "selfName", *groupQuery, *coreIdHandler); + Group g(0, GroupId(0), "groupName", false, "selfName", *groupQuery, *coreIdHandler, *friendList); auto sender = groupQuery->getGroupPeerPk(0, 0); g.updateUsername(sender, "sender1"); @@ -182,8 +185,8 @@ void TestNotificationGenerator::testMultipleFriendSourceMessages() void TestNotificationGenerator::testMultipleGroupSourceMessages() { - Group g(0, GroupId(QByteArray(32, 0)), "groupName", false, "selfName", *groupQuery, *coreIdHandler); - Group g2(1, GroupId(QByteArray(32, 1)), "groupName2", false, "selfName", *groupQuery, *coreIdHandler); + Group g(0, GroupId(QByteArray(32, 0)), "groupName", false, "selfName", *groupQuery, *coreIdHandler, *friendList); + Group g2(1, GroupId(QByteArray(32, 1)), "groupName2", false, "selfName", *groupQuery, *coreIdHandler, *friendList); auto sender = groupQuery->getGroupPeerPk(0, 0); g.updateUsername(sender, "sender1"); @@ -200,7 +203,7 @@ void TestNotificationGenerator::testMixedSourceMessages() Friend f(0, ToxPk()); f.setName("friend"); - Group g(0, GroupId(QByteArray(32, 0)), "group", false, "selfName", *groupQuery, *coreIdHandler); + Group g(0, GroupId(QByteArray(32, 0)), "group", false, "selfName", *groupQuery, *coreIdHandler, *friendList); auto sender = groupQuery->getGroupPeerPk(0, 0); g.updateUsername(sender, "sender1"); @@ -315,7 +318,7 @@ void TestNotificationGenerator::testSimpleFileTransfer() void TestNotificationGenerator::testSimpleGroupMessage() { - Group g(0, GroupId(0), "groupName", false, "selfName", *groupQuery, *coreIdHandler); + Group g(0, GroupId(0), "groupName", false, "selfName", *groupQuery, *coreIdHandler, *friendList); auto sender = groupQuery->getGroupPeerPk(0, 0); g.updateUsername(sender, "sender1"); diff --git a/test/model/sessionchatlog_test.cpp b/test/model/sessionchatlog_test.cpp index 28eefa7dc..a5354111b 100644 --- a/test/model/sessionchatlog_test.cpp +++ b/test/model/sessionchatlog_test.cpp @@ -72,6 +72,7 @@ private slots: private: MockCoreIdHandler idHandler; std::unique_ptr chatLog; + std::unique_ptr friendList; }; /** @@ -79,7 +80,8 @@ private: */ void TestSessionChatLog::init() { - chatLog = std::unique_ptr(new SessionChatLog(idHandler)); + friendList = std::unique_ptr(new FriendList()); + chatLog = std::unique_ptr(new SessionChatLog(idHandler, *friendList)); } /** diff --git a/test/widget/filesform_test.cpp b/test/widget/filesform_test.cpp index da804bcbf..7633983ea 100644 --- a/test/widget/filesform_test.cpp +++ b/test/widget/filesform_test.cpp @@ -28,7 +28,7 @@ class TestFileTransferList : public QObject { Q_OBJECT private slots: - + void init(); void testFileTransferListConversion(); void testEditorActionConversion(); @@ -44,14 +44,22 @@ private slots: void testAvatarIgnored(); void testMultipleFiles(); void testFileRemoval(); +private: + std::unique_ptr model; + std::unique_ptr friendList; }; using namespace FileTransferList; +void TestFileTransferList::init() +{ + friendList = std::unique_ptr(new FriendList()); + model = std::unique_ptr(new Model(*friendList)); +} + void TestFileTransferList::testFileTransferListConversion() { - Model model; - for (int i = 0; i < model.columnCount(); ++i) { + for (int i = 0; i < model->columnCount(); ++i) { QVERIFY(toFileTransferListColumn(i) != Column::invalid); } QCOMPARE(toFileTransferListColumn(100), Column::invalid); @@ -66,14 +74,12 @@ void TestFileTransferList::testEditorActionConversion() void TestFileTransferList::testFileName() { - Model model; - ToxFile file; file.fileKind = TOX_FILE_KIND_DATA; file.fileName = "Test"; - model.onFileUpdated(file); + model->onFileUpdated(file); - const auto idx = model.index(0, static_cast(Column::fileName)); + const auto idx = model->index(0, static_cast(Column::fileName)); const auto fileName = idx.data(); QCOMPARE(fileName.toString(), QString("Test")); @@ -81,13 +87,11 @@ void TestFileTransferList::testFileName() void TestFileTransferList::testProgress() { - Model model; - ToxFile file(0, 0, "", "", 1000, ToxFile::FileDirection::SENDING); file.progress.addSample(100, QTime(1, 0, 0)); - model.onFileUpdated(file); + model->onFileUpdated(file); - const auto idx = model.index(0, static_cast(Column::progress)); + const auto idx = model->index(0, static_cast(Column::progress)); const auto progress = idx.data(); // Progress should be in percent units, 100/1000 == 10 @@ -96,12 +100,10 @@ void TestFileTransferList::testProgress() void TestFileTransferList::testSize() { - Model model; - ToxFile file(0, 0, "", "", 1000, ToxFile::FileDirection::SENDING); - model.onFileUpdated(file); + model->onFileUpdated(file); - const auto idx = model.index(0, static_cast(Column::size)); + const auto idx = model->index(0, static_cast(Column::size)); auto size = idx.data(); // Size should be a human readable string @@ -109,21 +111,19 @@ void TestFileTransferList::testSize() // 1GB + a little to avoid floating point inaccuracy file = ToxFile(0, 0, "", "", 1024 * 1024 * 1024 + 2, ToxFile::FileDirection::SENDING); - model.onFileUpdated(file); + model->onFileUpdated(file); size = idx.data(); QCOMPARE(size.toString(), QString("1.00GiB")); } void TestFileTransferList::testSpeed() { - Model model; - ToxFile file(0, 0, "", "", 1024 * 1024, ToxFile::FileDirection::SENDING); file.progress.addSample(100 * 1024, QTime(1, 0, 0)); file.progress.addSample(200 * 1024, QTime(1, 0, 1)); - model.onFileUpdated(file); + model->onFileUpdated(file); - const auto idx = model.index(0, static_cast(Column::speed)); + const auto idx = model->index(0, static_cast(Column::speed)); const auto speed = idx.data(); // Speed should be a human readable string @@ -132,20 +132,18 @@ void TestFileTransferList::testSpeed() void TestFileTransferList::testStatus() { - Model model; - ToxFile file(0, 0, "", "", 1024 * 1024, ToxFile::FileDirection::SENDING); file.status = ToxFile::TRANSMITTING; - model.onFileUpdated(file); + model->onFileUpdated(file); - const auto idx = model.index(0, static_cast(Column::status)); + const auto idx = model->index(0, static_cast(Column::status)); auto status = idx.data(); QCOMPARE(status.toString(), QString("Transmitting")); file.status = ToxFile::PAUSED; file.pauseStatus.remotePause(); - model.onFileUpdated(file); + model->onFileUpdated(file); status = idx.data(); QCOMPARE(status.toString(), QString("Remote paused")); @@ -153,7 +151,7 @@ void TestFileTransferList::testStatus() file.status = ToxFile::PAUSED; file.pauseStatus.localPause(); file.pauseStatus.remoteResume(); - model.onFileUpdated(file); + model->onFileUpdated(file); status = idx.data(); QCOMPARE(status.toString(), QString("Paused")); @@ -161,87 +159,82 @@ void TestFileTransferList::testStatus() void TestFileTransferList::testControl() { - Model model; bool cancelCalled = false; bool pauseCalled = false; - QObject::connect(&model, &Model::cancel, [&] (ToxFile file) { + QObject::connect(model.get(), &Model::cancel, [&] (ToxFile file) { std::ignore = file; cancelCalled = true; }); - QObject::connect(&model, &Model::togglePause, [&] (ToxFile file) { + QObject::connect(model.get(), &Model::togglePause, [&] (ToxFile file) { std::ignore = file; pauseCalled = true; }); ToxFile file(0, 0, "", "", 1024 * 1024, ToxFile::FileDirection::SENDING); file.status = ToxFile::TRANSMITTING; - model.onFileUpdated(file); + model->onFileUpdated(file); - const auto idx = model.index(0, static_cast(Column::control)); - model.setData(idx, static_cast(EditorAction::pause)); + const auto idx = model->index(0, static_cast(Column::control)); + model->setData(idx, static_cast(EditorAction::pause)); QVERIFY(pauseCalled); QVERIFY(!cancelCalled); pauseCalled = false; - model.setData(idx, static_cast(EditorAction::cancel)); + model->setData(idx, static_cast(EditorAction::cancel)); QVERIFY(!pauseCalled); QVERIFY(cancelCalled); file.status = ToxFile::TRANSMITTING; - model.onFileUpdated(file); + model->onFileUpdated(file); // True if paused QCOMPARE(idx.data().toBool(), false); file.status = ToxFile::PAUSED; file.pauseStatus.localPause(); - model.onFileUpdated(file); + model->onFileUpdated(file); // True if _local_ paused QCOMPARE(idx.data().toBool(), true); } void TestFileTransferList::testAvatarIgnored() { - Model model; - ToxFile file; file.fileKind = TOX_FILE_KIND_AVATAR; - model.onFileUpdated(file); + model->onFileUpdated(file); - QCOMPARE(model.rowCount(), 0); + QCOMPARE(model->rowCount(), 0); } void TestFileTransferList::testMultipleFiles() { - Model model; - ToxFile file; file.resumeFileId = QByteArray(); file.fileKind = TOX_FILE_KIND_DATA; file.status = ToxFile::TRANSMITTING; file.fileName = "a"; - model.onFileUpdated(file); + model->onFileUpdated(file); // File map keys off resume file ID file.resumeFileId = QByteArray("asdfasdf"); file.fileName = "b"; - model.onFileUpdated(file); + model->onFileUpdated(file); - QCOMPARE(model.rowCount(), 2); + QCOMPARE(model->rowCount(), 2); - auto idx = model.index(0, static_cast(Column::fileName)); + auto idx = model->index(0, static_cast(Column::fileName)); QCOMPARE(idx.data().toString(), QString("a")); - idx = model.index(1, static_cast(Column::fileName)); + idx = model->index(1, static_cast(Column::fileName)); QCOMPARE(idx.data().toString(), QString("b")); // File name should be updated instead of inserting a new file since the // resume file ID is the same file.fileName = "c"; - model.onFileUpdated(file); - QCOMPARE(model.rowCount(), 2); + model->onFileUpdated(file); + QCOMPARE(model->rowCount(), 2); QCOMPARE(idx.data().toString(), QString("c")); } @@ -250,34 +243,32 @@ void TestFileTransferList::testFileRemoval() // Model should keep files in the list if they are finished, but not if they // were broken or canceled - Model model; - ToxFile file; file.fileKind = TOX_FILE_KIND_DATA; file.status = ToxFile::TRANSMITTING; - model.onFileUpdated(file); + model->onFileUpdated(file); - QCOMPARE(model.rowCount(), 1); + QCOMPARE(model->rowCount(), 1); file.status = ToxFile::BROKEN; - model.onFileUpdated(file); - QCOMPARE(model.rowCount(), 0); + model->onFileUpdated(file); + QCOMPARE(model->rowCount(), 0); file.status = ToxFile::TRANSMITTING; - model.onFileUpdated(file); - QCOMPARE(model.rowCount(), 1); + model->onFileUpdated(file); + QCOMPARE(model->rowCount(), 1); file.status = ToxFile::CANCELED; - model.onFileUpdated(file); - QCOMPARE(model.rowCount(), 0); + model->onFileUpdated(file); + QCOMPARE(model->rowCount(), 0); file.status = ToxFile::TRANSMITTING; - model.onFileUpdated(file); - QCOMPARE(model.rowCount(), 1); + model->onFileUpdated(file); + QCOMPARE(model->rowCount(), 1); file.status = ToxFile::FINISHED; - model.onFileUpdated(file); - QCOMPARE(model.rowCount(), 1); + model->onFileUpdated(file); + QCOMPARE(model->rowCount(), 1); } QTEST_GUILESS_MAIN(TestFileTransferList)