From 9306e946f82352a1912556eaf83f753218566d82 Mon Sep 17 00:00:00 2001 From: sudden6 Date: Sat, 2 May 2020 22:55:49 +0200 Subject: [PATCH] refactor: remove Core::getInstance() from ChatForm classes --- src/nexus.cpp | 6 +-- src/persistence/profile.cpp | 7 ++-- src/persistence/profile.h | 2 +- src/widget/form/chatform.cpp | 53 +++++++++++++------------- src/widget/form/chatform.h | 5 ++- src/widget/form/genericchatform.cpp | 58 ++++++++++++++--------------- src/widget/form/genericchatform.h | 4 +- src/widget/form/groupchatform.cpp | 21 ++++++----- src/widget/form/groupchatform.h | 3 +- src/widget/widget.cpp | 10 +++-- src/widget/widget.h | 3 +- 11 files changed, 89 insertions(+), 83 deletions(-) diff --git a/src/nexus.cpp b/src/nexus.cpp index 66c6014fa..dc1afdad3 100644 --- a/src/nexus.cpp +++ b/src/nexus.cpp @@ -184,7 +184,7 @@ void Nexus::bootstrapWithProfile(Profile* p) if (profile) { audioControl = std::unique_ptr(Audio::makeAudio(*settings)); assert(audioControl != nullptr); - profile->getCore()->getAv()->setAudio(*audioControl); + profile->getCore().getAv()->setAudio(*audioControl); start(); } } @@ -224,7 +224,7 @@ void Nexus::showMainGUI() assert(profile); // Create GUI - widget = new Widget(*audioControl); + widget = new Widget(*profile, *audioControl); // Start GUI widget->init(); @@ -278,7 +278,7 @@ Core* Nexus::getCore() if (!nexus.profile) return nullptr; - return nexus.profile->getCore(); + return &nexus.profile->getCore(); } /** diff --git a/src/persistence/profile.cpp b/src/persistence/profile.cpp index 4c5c7af22..f5cf8224d 100644 --- a/src/persistence/profile.cpp +++ b/src/persistence/profile.cpp @@ -404,10 +404,11 @@ const QStringList Profile::getAllProfileNames() return profiles; } -Core* Profile::getCore() +Core& Profile::getCore() const { - // TODO(sudden6): this is evil - return core.get(); + Core* c = core.get(); + assert(c != nullptr); + return *c; } QString Profile::getName() const diff --git a/src/persistence/profile.h b/src/persistence/profile.h index f50c5c577..5388a02b9 100644 --- a/src/persistence/profile.h +++ b/src/persistence/profile.h @@ -48,7 +48,7 @@ public: const QCommandLineParser* parser); ~Profile(); - Core* getCore(); + Core& getCore() const; QString getName() const; void startCore(); diff --git a/src/widget/form/chatform.cpp b/src/widget/form/chatform.cpp index 5112bd666..27e99d5ed 100644 --- a/src/widget/form/chatform.cpp +++ b/src/widget/form/chatform.cpp @@ -104,8 +104,9 @@ QString secondsToDHMS(quint32 duration) } } // namespace -ChatForm::ChatForm(Friend* chatFriend, IChatLog& chatLog, IMessageDispatcher& messageDispatcher) - : GenericChatForm(chatFriend, chatLog, messageDispatcher) +ChatForm::ChatForm(Profile& profile, Friend* chatFriend, IChatLog& chatLog, IMessageDispatcher& messageDispatcher) + : GenericChatForm(profile.getCore(), chatFriend, chatLog, messageDispatcher) + , core{profile.getCore()} , f(chatFriend) , isTyping{false} , lastCallIsVideo{false} @@ -136,17 +137,15 @@ ChatForm::ChatForm(Friend* chatFriend, IChatLog& chatLog, IMessageDispatcher& me copyStatusAction = statusMessageMenu.addAction(QString(), this, SLOT(onCopyStatusMessage())); - const Core* core = Core::getInstance(); - const Profile* profile = Nexus::getProfile(); - const CoreFile* coreFile = core->getCoreFile(); - connect(profile, &Profile::friendAvatarChanged, this, &ChatForm::onAvatarChanged); + const CoreFile* coreFile = core.getCoreFile(); + connect(&profile, &Profile::friendAvatarChanged, this, &ChatForm::onAvatarChanged); connect(coreFile, &CoreFile::fileReceiveRequested, this, &ChatForm::updateFriendActivityForFile); connect(coreFile, &CoreFile::fileSendStarted, this, &ChatForm::updateFriendActivityForFile); - connect(core, &Core::friendTypingChanged, this, &ChatForm::onFriendTypingChanged); - connect(core, &Core::friendStatusChanged, this, &ChatForm::onFriendStatusChanged); + connect(&core, &Core::friendTypingChanged, this, &ChatForm::onFriendTypingChanged); + connect(&core, &Core::friendStatusChanged, this, &ChatForm::onFriendStatusChanged); connect(coreFile, &CoreFile::fileNameChanged, this, &ChatForm::onFileNameChanged); - const CoreAV* av = core->getAv(); + const CoreAV* av = core.getAv(); connect(av, &CoreAV::avInvite, this, &ChatForm::onAvInvite); connect(av, &CoreAV::avStart, this, &ChatForm::onAvStart); connect(av, &CoreAV::avEnd, this, &ChatForm::onAvEnd); @@ -167,8 +166,8 @@ ChatForm::ChatForm(Friend* chatFriend, IChatLog& chatLog, IMessageDispatcher& me } }); - connect(&typingTimer, &QTimer::timeout, this, [=] { - Core::getInstance()->sendTyping(f->getId(), false); + connect(&typingTimer, &QTimer::timeout, this, [&] { + core.sendTyping(f->getId(), false); isTyping = false; }); @@ -229,14 +228,14 @@ void ChatForm::onTextEditChanged() if (!Settings::getInstance().getTypingNotification()) { if (isTyping) { isTyping = false; - Core::getInstance()->sendTyping(f->getId(), false); + core.sendTyping(f->getId(), false); } return; } bool isTypingNow = !msgEdit->toPlainText().isEmpty(); if (isTyping != isTypingNow) { - Core::getInstance()->sendTyping(f->getId(), isTypingNow); + core.sendTyping(f->getId(), isTypingNow); if (isTypingNow) { typingTimer.start(TYPING_NOTIFICATION_DURATION); } @@ -254,7 +253,6 @@ void ChatForm::onAttachClicked() return; } - Core* core = Core::getInstance(); for (QString path : paths) { QFile file(path); QString fileName = QFileInfo(path).fileName(); @@ -273,7 +271,7 @@ void ChatForm::onAttachClicked() } qint64 filesize = file.size(); - core->getCoreFile()->sendFile(f->getId(), fileName, path, filesize); + core.getCoreFile()->sendFile(f->getId(), fileName, path, filesize); } } @@ -293,7 +291,7 @@ void ChatForm::onAvInvite(uint32_t friendId, bool video) if (Settings::getInstance().getAutoAcceptCall(f->getPublicKey()).testFlag(testedFlag)) { uint32_t friendId = f->getId(); qDebug() << "automatic call answer"; - CoreAV* coreav = Core::getInstance()->getAv(); + CoreAV* coreav = core.getAv(); QMetaObject::invokeMethod(coreav, "answerCall", Qt::QueuedConnection, Q_ARG(uint32_t, friendId), Q_ARG(bool, video)); onAvStart(friendId, video); @@ -358,7 +356,7 @@ void ChatForm::onAnswerCallTriggered(bool video) emit acceptCall(friendId); updateCallButtons(); - CoreAV* av = Core::getInstance()->getAv(); + CoreAV* av = core.getAv(); if (!av->answerCall(friendId, video)) { updateCallButtons(); stopCounter(); @@ -377,7 +375,7 @@ void ChatForm::onRejectCallTriggered() void ChatForm::onCallTriggered() { - CoreAV* av = Core::getInstance()->getAv(); + CoreAV* av = core.getAv(); uint32_t friendId = f->getId(); if (av->isCallStarted(f)) { av->cancelCall(friendId); @@ -388,7 +386,7 @@ void ChatForm::onCallTriggered() void ChatForm::onVideoCallTriggered() { - CoreAV* av = Core::getInstance()->getAv(); + CoreAV* av = core.getAv(); uint32_t friendId = f->getId(); if (av->isCallStarted(f)) { // TODO: We want to activate video on the active call. @@ -402,7 +400,7 @@ void ChatForm::onVideoCallTriggered() void ChatForm::updateCallButtons() { - CoreAV* av = Core::getInstance()->getAv(); + CoreAV* av = core.getAv(); const bool audio = av->isCallActive(f); const bool video = av->isCallVideoEnabled(f); const bool online = Status::isOnline(f->getStatus()); @@ -413,14 +411,14 @@ void ChatForm::updateCallButtons() void ChatForm::onMicMuteToggle() { - CoreAV* av = Core::getInstance()->getAv(); + CoreAV* av = core.getAv(); av->toggleMuteCallInput(f); updateMuteMicButton(); } void ChatForm::onVolMuteToggle() { - CoreAV* av = Core::getInstance()->getAv(); + CoreAV* av = core.getAv(); av->toggleMuteCallOutput(f); updateMuteVolButton(); } @@ -483,7 +481,7 @@ std::unique_ptr ChatForm::createNetcam() qDebug() << "creating netcam"; uint32_t friendId = f->getId(); std::unique_ptr view = std::unique_ptr(new NetCamView(f->getPublicKey(), this)); - CoreAV* av = Core::getInstance()->getAv(); + CoreAV* av = core.getAv(); VideoSource* source = av->getVideoSourceFromCall(friendId); view->show(source, f->getDisplayedName()); connect(view.get(), &NetCamView::videoCallEnd, this, &ChatForm::onVideoCallTriggered); @@ -505,7 +503,6 @@ void ChatForm::dropEvent(QDropEvent* ev) return; } - Core* core = Core::getInstance(); for (const QUrl& url : ev->mimeData()->urls()) { QFileInfo info(url.path()); QFile file(info.absoluteFilePath()); @@ -538,7 +535,7 @@ void ChatForm::dropEvent(QDropEvent* ev) } if (info.exists()) { - core->getCoreFile()->sendFile(f->getId(), fileName, info.absoluteFilePath(), info.size()); + core.getCoreFile()->sendFile(f->getId(), fileName, info.absoluteFilePath(), info.size()); } } } @@ -582,7 +579,7 @@ void ChatForm::sendImage(const QPixmap& pixmap) qint64 filesize = file.size(); file.close(); QFileInfo fi(file); - CoreFile* coreFile = Core::getInstance()->getCoreFile(); + CoreFile* coreFile = core.getCoreFile(); coreFile->sendFile(f->getId(), fi.fileName(), fi.filePath(), filesize); } else { QMessageBox::warning(this, @@ -611,7 +608,7 @@ void ChatForm::onCopyStatusMessage() void ChatForm::updateMuteMicButton() { - const CoreAV* av = Core::getInstance()->getAv(); + const CoreAV* av = core.getAv(); bool active = av->isCallActive(f); bool inputMuted = av->isCallInputMuted(f); headWidget->updateMuteMicButton(active, inputMuted); @@ -622,7 +619,7 @@ void ChatForm::updateMuteMicButton() void ChatForm::updateMuteVolButton() { - const CoreAV* av = Core::getInstance()->getAv(); + const CoreAV* av = core.getAv(); bool active = av->isCallActive(f); bool outputMuted = av->isCallOutputMuted(f); headWidget->updateMuteVolButton(active, outputMuted); diff --git a/src/widget/form/chatform.h b/src/widget/form/chatform.h index 011eb6d9d..84a51eaf1 100644 --- a/src/widget/form/chatform.h +++ b/src/widget/form/chatform.h @@ -47,8 +47,8 @@ class ChatForm : public GenericChatForm { Q_OBJECT public: - ChatForm(Friend* chatFriend, IChatLog& chatLog, IMessageDispatcher& messageDispatcher); - ~ChatForm(); + ChatForm(Profile& profile, Friend* chatFriend, IChatLog& chatLog, IMessageDispatcher& messageDispatcher); + ~ChatForm() override; void setStatusMessage(const QString& newMessage); void setFriendTyping(bool isTyping); @@ -122,6 +122,7 @@ protected: void showEvent(QShowEvent* event) final; private: + Core& core; Friend* f; CroppingLabel* statusMessageLabel; QMenu statusMessageMenu; diff --git a/src/widget/form/genericchatform.cpp b/src/widget/form/genericchatform.cpp index 08327871b..13eba2508 100644 --- a/src/widget/form/genericchatform.cpp +++ b/src/widget/form/genericchatform.cpp @@ -182,7 +182,7 @@ ChatMessage::Ptr createMessage(const QString& displayName, bool isSelf, bool col isSelf, chatLogMessage.state, timestamp, colorizeNames); } -void renderMessage(const QString& displayName, bool isSelf, bool colorizeNames, +void renderMessageRaw(const QString& displayName, bool isSelf, bool colorizeNames, const ChatLogMessage& chatLogMessage, ChatMessage::Ptr& chatMessage) { @@ -208,33 +208,6 @@ void renderFile(QString displayName, ToxFile file, bool isSelf, QDateTime timest } } -void renderItem(const ChatLogItem& item, bool hideName, bool colorizeNames, ChatMessage::Ptr& chatMessage) -{ - const auto& sender = item.getSender(); - - const Core* core = Core::getInstance(); - bool isSelf = sender == core->getSelfId().getPublicKey(); - - switch (item.getContentType()) { - case ChatLogItem::ContentType::message: { - const auto& chatLogMessage = item.getContentAsMessage(); - - renderMessage(item.getDisplayName(), isSelf, colorizeNames, chatLogMessage, chatMessage); - - break; - } - case ChatLogItem::ContentType::fileTransfer: { - const auto& file = item.getContentAsFile(); - renderFile(item.getDisplayName(), file.file, isSelf, item.getTimestamp(), chatMessage); - break; - } - } - - if (hideName) { - chatMessage->hideSender(); - } -} - ChatLogIdx firstItemAfterDate(QDate date, const IChatLog& chatLog) { auto idxs = chatLog.getDateIdxs(date, 1); @@ -246,9 +219,10 @@ ChatLogIdx firstItemAfterDate(QDate date, const IChatLog& chatLog) } } // namespace -GenericChatForm::GenericChatForm(const Contact* contact, IChatLog& chatLog, +GenericChatForm::GenericChatForm(const Core& _core, const Contact* contact, IChatLog& chatLog, IMessageDispatcher& messageDispatcher, QWidget* parent) : QWidget(parent, Qt::Window) + , core{_core} , audioInputFlag(false) , audioOutputFlag(false) , chatLog(chatLog) @@ -1076,6 +1050,32 @@ void GenericChatForm::handleSearchResult(SearchResult result, SearchDirection di renderMessages(endRenderedIdx, firstRenderedIdx, [this]{enableSearchText();}); } +void GenericChatForm::renderItem(const ChatLogItem& item, bool hideName, bool colorizeNames, ChatMessage::Ptr& chatMessage) +{ + const auto& sender = item.getSender(); + + bool isSelf = sender == core.getSelfId().getPublicKey(); + + switch (item.getContentType()) { + case ChatLogItem::ContentType::message: { + const auto& chatLogMessage = item.getContentAsMessage(); + + renderMessageRaw(item.getDisplayName(), isSelf, colorizeNames, chatLogMessage, chatMessage); + + break; + } + case ChatLogItem::ContentType::fileTransfer: { + const auto& file = item.getContentAsFile(); + renderFile(item.getDisplayName(), file.file, isSelf, item.getTimestamp(), chatMessage); + break; + } + } + + if (hideName) { + chatMessage->hideSender(); + } +} + void GenericChatForm::renderMessage(ChatLogIdx idx) { renderMessages(idx, idx + 1); diff --git a/src/widget/form/genericchatform.h b/src/widget/form/genericchatform.h index c100fdcab..71a2ac9a9 100644 --- a/src/widget/form/genericchatform.h +++ b/src/widget/form/genericchatform.h @@ -70,7 +70,7 @@ class GenericChatForm : public QWidget { Q_OBJECT public: - GenericChatForm(const Contact* contact, IChatLog& chatLog, + GenericChatForm(const Core& _core, const Contact* contact, IChatLog& chatLog, IMessageDispatcher& messageDispatcher, QWidget* parent = nullptr); ~GenericChatForm() override; @@ -136,6 +136,7 @@ private: void removeFirstsMessages(const int num); void removeLastsMessages(const int num); + void renderItem(const ChatLogItem &item, bool hideName, bool colorizeNames, ChatMessage::Ptr &chatMessage); protected: ChatMessage::Ptr createMessage(const ToxPk& author, const QString& message, const QDateTime& datetime, bool isAction, bool isSent, bool colorizeName = false); @@ -153,6 +154,7 @@ protected: std::pair indexForSearchInLine(const QString& txt, const QString& phrase, const ParameterSearch& parameter, SearchDirection direction); protected: + const Core& core; bool audioInputFlag; bool audioOutputFlag; int curRow; diff --git a/src/widget/form/groupchatform.cpp b/src/widget/form/groupchatform.cpp index 7b323d057..28c6a329e 100644 --- a/src/widget/form/groupchatform.cpp +++ b/src/widget/form/groupchatform.cpp @@ -82,8 +82,9 @@ QString editName(const QString& name) * @brief Timeout = peer stopped sending audio. */ -GroupChatForm::GroupChatForm(Group* chatGroup, IChatLog& chatLog, IMessageDispatcher& messageDispatcher) - : GenericChatForm(chatGroup, chatLog, messageDispatcher) +GroupChatForm::GroupChatForm(Core& _core, Group* chatGroup, IChatLog& chatLog, IMessageDispatcher& messageDispatcher) + : GenericChatForm(_core, chatGroup, chatLog, messageDispatcher) + , core{_core} , group(chatGroup) , inCall(false) { @@ -185,7 +186,7 @@ void GroupChatForm::updateUserNames() /* we store the peer labels by their ToxPk, but the namelist layout * needs it in alphabetical order, so we first create and store the labels * and then sort them by their text and add them to the layout in that order */ - const auto selfPk = Core::getInstance()->getSelfPublicKey(); + const auto selfPk = core.getSelfPublicKey(); for (const auto& peerPk : peers.keys()) { const QString peerName = peers.value(peerPk); const QString editedName = editName(peerName); @@ -296,14 +297,14 @@ void GroupChatForm::dropEvent(QDropEvent* ev) int friendId = frnd->getId(); int groupId = group->getId(); if (Status::isOnline(frnd->getStatus())) { - Core::getInstance()->groupInviteFriend(friendId, groupId); + core.groupInviteFriend(friendId, groupId); } } void GroupChatForm::onMicMuteToggle() { if (audioInputFlag) { - CoreAV* av = Core::getInstance()->getAv(); + CoreAV* av = core.getAv(); const bool oldMuteState = av->isGroupCallInputMuted(group); const bool newMute = !oldMuteState; av->muteCallInput(group, newMute); @@ -314,7 +315,7 @@ void GroupChatForm::onMicMuteToggle() void GroupChatForm::onVolMuteToggle() { if (audioOutputFlag) { - CoreAV* av = Core::getInstance()->getAv(); + CoreAV* av = core.getAv(); const bool oldMuteState = av->isGroupCallOutputMuted(group); const bool newMute = !oldMuteState; av->muteCallOutput(group, newMute); @@ -324,7 +325,7 @@ void GroupChatForm::onVolMuteToggle() void GroupChatForm::onCallClicked() { - CoreAV* av = Core::getInstance()->getAv(); + CoreAV* av = core.getAv(); if (!inCall) { joinGroupCall(); @@ -391,7 +392,7 @@ void GroupChatForm::onLabelContextMenuRequested(const QPoint& localPos) Settings& s = Settings::getInstance(); QStringList blackList = s.getBlackList(); QMenu* const contextMenu = new QMenu(this); - const ToxPk selfPk = Core::getInstance()->getSelfPublicKey(); + const ToxPk selfPk = core.getSelfPublicKey(); ToxPk peerPk; // delete menu after it stops being used @@ -436,7 +437,7 @@ void GroupChatForm::onLabelContextMenuRequested(const QPoint& localPos) void GroupChatForm::joinGroupCall() { - CoreAV* av = Core::getInstance()->getAv(); + CoreAV* av = core.getAv(); av->joinGroupCall(*group); audioInputFlag = true; audioOutputFlag = true; @@ -445,7 +446,7 @@ void GroupChatForm::joinGroupCall() void GroupChatForm::leaveGroupCall() { - CoreAV* av = Core::getInstance()->getAv(); + CoreAV* av = core.getAv(); av->leaveGroupCall(group->getId()); audioInputFlag = false; audioOutputFlag = false; diff --git a/src/widget/form/groupchatform.h b/src/widget/form/groupchatform.h index 6cce22c96..b6c4bb904 100644 --- a/src/widget/form/groupchatform.h +++ b/src/widget/form/groupchatform.h @@ -39,7 +39,7 @@ class GroupChatForm : public GenericChatForm { Q_OBJECT public: - explicit GroupChatForm(Group* chatGroup, IChatLog& chatLog, IMessageDispatcher& messageDispatcher); + explicit GroupChatForm(Core& _core, Group* chatGroup, IChatLog& chatLog, IMessageDispatcher& messageDispatcher); ~GroupChatForm(); void peerAudioPlaying(ToxPk peerPk); @@ -71,6 +71,7 @@ private: void leaveGroupCall(); private: + Core& core; Group* group; QMap peerLabels; QMap peerAudioTimers; diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index 02727538b..fad233d1c 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -137,8 +137,9 @@ void Widget::acceptFileTransfer(const ToxFile& file, const QString& path) Widget* Widget::instance{nullptr}; -Widget::Widget(IAudioControl& audio, QWidget* parent) +Widget::Widget(Profile &_profile, IAudioControl& audio, QWidget* parent) : QMainWindow(parent) + , profile{_profile} , trayMenu{nullptr} , ui(new Ui::MainWindow) , activeChatroomWidget{nullptr} @@ -277,7 +278,7 @@ void Widget::init() addFriendForm = new AddFriendForm; groupInviteForm = new GroupInviteForm; - core = Nexus::getCore(); + core = &profile.getCore(); #if UPDATE_CHECK_ENABLED updateCheck = std::unique_ptr(new UpdateCheck(settings)); @@ -1146,7 +1147,7 @@ void Widget::addFriend(uint32_t friendId, const ToxPk& friendPk) auto chatHistory = std::make_shared(*newfriend, history, *core, Settings::getInstance(), *friendMessageDispatcher); - auto friendForm = new ChatForm(newfriend, *chatHistory, *friendMessageDispatcher); + auto friendForm = new ChatForm(profile, newfriend, *chatHistory, *friendMessageDispatcher); connect(friendForm, &ChatForm::updateFriendActivity, this, &Widget::updateFriendActivity); friendMessageDispatchers[friendPk] = friendMessageDispatcher; @@ -2110,7 +2111,8 @@ Group* Widget::createGroup(uint32_t groupnumber, const GroupId& groupId) connect(messageDispatcher.get(), &IMessageDispatcher::messageReceived, notifyReceivedCallback); groupAlertConnections.insert(groupId, notifyReceivedConnection); - auto form = new GroupChatForm(newgroup, *groupChatLog, *messageDispatcher); + assert(core != nullptr); + auto form = new GroupChatForm(*core, newgroup, *groupChatLog, *messageDispatcher); connect(&settings, &Settings::nameColorsChanged, form, &GenericChatForm::setColorizedNames); form->setColorizedNames(settings.getEnableGroupChatsColor()); groupMessageDispatchers[groupId] = messageDispatcher; diff --git a/src/widget/widget.h b/src/widget/widget.h index fdad45d7f..593671a78 100644 --- a/src/widget/widget.h +++ b/src/widget/widget.h @@ -117,7 +117,7 @@ private: }; public: - explicit Widget(IAudioControl& audio, QWidget* parent = nullptr); + explicit Widget(Profile& _profile, IAudioControl& audio, QWidget* parent = nullptr); ~Widget() override; void init(); void setCentralWidget(QWidget* widget, const QString& widgetName); @@ -278,6 +278,7 @@ private: void acceptFileTransfer(const ToxFile &file, const QString &path); private: + Profile& profile; std::unique_ptr icon; QMenu* trayMenu; QAction* statusOnline;