From 17d5d55259ab9864c430df8bdc9b440a0315a59c Mon Sep 17 00:00:00 2001 From: jenli669 Date: Mon, 24 Jun 2019 12:34:26 +0200 Subject: [PATCH] fix(core): correctly relink ui/core when core is changed Widget was only connecting the Core to itself during initialization, but the Core instance could change during a restartCore call. This commit will make Widget link the Core to itself when it changes rather than only on initialization. Fixes #5710 --- src/nexus.cpp | 32 ++----------------- src/persistence/profile.cpp | 9 ++++++ src/persistence/profile.h | 1 + src/widget/widget.cpp | 61 +++++++++++++++++++++++++++++-------- src/widget/widget.h | 7 +++-- 5 files changed, 66 insertions(+), 44 deletions(-) diff --git a/src/nexus.cpp b/src/nexus.cpp index 256f814496..fb62c7dfaa 100644 --- a/src/nexus.cpp +++ b/src/nexus.cpp @@ -237,40 +237,12 @@ void Nexus::showMainGUI() // Connections connect(profile, &Profile::selfAvatarChanged, widget, &Widget::onSelfAvatarLoaded); - Core* core = profile->getCore(); - connect(core, &Core::requestSent, profile, &Profile::onRequestSent); + connect(profile, &Profile::coreChanged, widget, &Widget::onCoreChanged); - connect(core, &Core::connected, widget, &Widget::onConnected); - connect(core, &Core::disconnected, widget, &Widget::onDisconnected); connect(profile, &Profile::failedToStart, widget, &Widget::onFailedToStartCore, Qt::BlockingQueuedConnection); + connect(profile, &Profile::badProxy, widget, &Widget::onBadProxyCore, Qt::BlockingQueuedConnection); - connect(core, &Core::statusSet, widget, &Widget::onStatusSet); - connect(core, &Core::usernameSet, widget, &Widget::setUsername); - connect(core, &Core::statusMessageSet, widget, &Widget::setStatusMessage); - connect(core, &Core::friendAdded, widget, &Widget::addFriend); - connect(core, &Core::failedToAddFriend, widget, &Widget::addFriendFailed); - connect(core, &Core::friendUsernameChanged, widget, &Widget::onFriendUsernameChanged); - connect(core, &Core::friendStatusChanged, widget, &Widget::onFriendStatusChanged); - connect(core, &Core::friendStatusMessageChanged, widget, &Widget::onFriendStatusMessageChanged); - connect(core, &Core::friendRequestReceived, widget, &Widget::onFriendRequestReceived); - connect(core, &Core::friendMessageReceived, widget, &Widget::onFriendMessageReceived); - connect(core, &Core::receiptRecieved, widget, &Widget::onReceiptReceived); - connect(core, &Core::groupInviteReceived, widget, &Widget::onGroupInviteReceived); - connect(core, &Core::groupMessageReceived, widget, &Widget::onGroupMessageReceived); - connect(core, &Core::groupPeerlistChanged, widget, &Widget::onGroupPeerlistChanged); - connect(core, &Core::groupPeerNameChanged, widget, &Widget::onGroupPeerNameChanged); - connect(core, &Core::groupTitleChanged, widget, &Widget::onGroupTitleChanged); - connect(core, &Core::groupPeerAudioPlaying, widget, &Widget::onGroupPeerAudioPlaying); - connect(core, &Core::emptyGroupCreated, widget, &Widget::onEmptyGroupCreated); - connect(core, &Core::groupJoined, widget, &Widget::onGroupJoined); - connect(core, &Core::friendTypingChanged, widget, &Widget::onFriendTypingChanged); - connect(core, &Core::groupSentFailed, widget, &Widget::onGroupSendFailed); - connect(core, &Core::usernameSet, widget, &Widget::refreshPeerListsLocal); - - connect(widget, &Widget::statusSet, core, &Core::setStatus); - connect(widget, &Widget::friendRequested, core, &Core::requestFriendship); - connect(widget, &Widget::friendRequestAccepted, core, &Core::acceptFriendRequest); profile->startCore(); diff --git a/src/persistence/profile.cpp b/src/persistence/profile.cpp index 1bc48bda1f..460f3a0e94 100644 --- a/src/persistence/profile.cpp +++ b/src/persistence/profile.cpp @@ -299,6 +299,10 @@ QString Profile::getName() const */ void Profile::startCore() { + // kriby: code duplication belongs in initCore, but cannot yet due to Core/Profile coupling + connect(core.get(), &Core::requestSent, this, &Profile::onRequestSent); + emit coreChanged(*core); + core->start(); const ToxId& selfId = core->getSelfId(); @@ -799,6 +803,11 @@ void Profile::restartCore() assert(audioBak != nullptr); initCore(savedata, Settings::getInstance(), isNewProfile); core->getAv()->setAudio(*audioBak); + + // kriby: code duplication belongs in initCore, but cannot yet due to Core/Profile coupling + connect(core.get(), &Core::requestSent, this, &Profile::onRequestSent); + emit coreChanged(*core); + core->start(); } else { qCritical() << "Failed to save, not restarting core"; diff --git a/src/persistence/profile.h b/src/persistence/profile.h index 6b8c9dd08c..ad1cb10b16 100644 --- a/src/persistence/profile.h +++ b/src/persistence/profile.h @@ -84,6 +84,7 @@ class Profile : public QObject // TODO(sudden6): this doesn't seem to be the right place for Core errors void failedToStart(); void badProxy(); + void coreChanged(Core& core); public slots: void onRequestSent(const ToxPk& friendPk, const QString& message); diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index 7982b05284..f99853bdc4 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -219,9 +219,9 @@ void Widget::init() filterDisplayActivity->setCheckable(true); filterDisplayGroup->addAction(filterDisplayActivity); filterMenu->addAction(filterDisplayActivity); - settings.getFriendSortingMode() == FriendListWidget::SortingMode::Name ? - filterDisplayName->setChecked(true) : - filterDisplayActivity->setChecked(true); + settings.getFriendSortingMode() == FriendListWidget::SortingMode::Name + ? filterDisplayName->setChecked(true) + : filterDisplayActivity->setChecked(true); filterMenu->addSeparator(); filterAllAction = new QAction(this); @@ -502,9 +502,9 @@ void Widget::updateIcons() return; } - const QString assetSuffix = - Status::getAssetSuffix(static_cast(ui->statusButton->property("status").toInt())) - + (eventIcon ? "_event" : ""); + const QString assetSuffix = Status::getAssetSuffix(static_cast( + ui->statusButton->property("status").toInt())) + + (eventIcon ? "_event" : ""); // Some builds of Qt appear to have a bug in icon loading: // QIcon::hasThemeIcon is sometimes unaware that the icon returned @@ -675,6 +675,38 @@ void Widget::onSelfAvatarLoaded(const QPixmap& pic) profilePicture->setPixmap(pic); } +void Widget::onCoreChanged(Core& core) +{ + + connect(&core, &Core::connected, this, &Widget::onConnected); + connect(&core, &Core::disconnected, this, &Widget::onDisconnected); + connect(&core, &Core::statusSet, this, &Widget::onStatusSet); + connect(&core, &Core::usernameSet, this, &Widget::setUsername); + connect(&core, &Core::statusMessageSet, this, &Widget::setStatusMessage); + connect(&core, &Core::friendAdded, this, &Widget::addFriend); + connect(&core, &Core::failedToAddFriend, this, &Widget::addFriendFailed); + connect(&core, &Core::friendUsernameChanged, this, &Widget::onFriendUsernameChanged); + connect(&core, &Core::friendStatusChanged, this, &Widget::onFriendStatusChanged); + connect(&core, &Core::friendStatusMessageChanged, this, &Widget::onFriendStatusMessageChanged); + connect(&core, &Core::friendRequestReceived, this, &Widget::onFriendRequestReceived); + connect(&core, &Core::friendMessageReceived, this, &Widget::onFriendMessageReceived); + connect(&core, &Core::receiptRecieved, this, &Widget::onReceiptReceived); + connect(&core, &Core::groupInviteReceived, this, &Widget::onGroupInviteReceived); + connect(&core, &Core::groupMessageReceived, this, &Widget::onGroupMessageReceived); + connect(&core, &Core::groupPeerlistChanged, this, &Widget::onGroupPeerlistChanged); + connect(&core, &Core::groupPeerNameChanged, this, &Widget::onGroupPeerNameChanged); + connect(&core, &Core::groupTitleChanged, this, &Widget::onGroupTitleChanged); + connect(&core, &Core::groupPeerAudioPlaying, this, &Widget::onGroupPeerAudioPlaying); + connect(&core, &Core::emptyGroupCreated, this, &Widget::onEmptyGroupCreated); + connect(&core, &Core::groupJoined, this, &Widget::onGroupJoined); + connect(&core, &Core::friendTypingChanged, this, &Widget::onFriendTypingChanged); + connect(&core, &Core::groupSentFailed, this, &Widget::onGroupSendFailed); + connect(&core, &Core::usernameSet, this, &Widget::refreshPeerListsLocal); + connect(this, &Widget::statusSet, &core, &Core::setStatus); + connect(this, &Widget::friendRequested, &core, &Core::requestFriendship); + connect(this, &Widget::friendRequestAccepted, &core, &Core::acceptFriendRequest); +} + void Widget::onConnected() { ui->statusButton->setEnabled(true); @@ -1513,13 +1545,15 @@ bool Widget::newFriendMessageAlert(const ToxPk& friendId, const QString& text, b ui->friendList->trackWidget(widget); #if DESKTOP_NOTIFICATIONS if (settings.getNotifyHide()) { - notifier.notifyMessageSimple(file ? DesktopNotify::MessageType::FRIEND_FILE : DesktopNotify::MessageType::FRIEND); + notifier.notifyMessageSimple(file ? DesktopNotify::MessageType::FRIEND_FILE + : DesktopNotify::MessageType::FRIEND); } else { QString title = f->getDisplayedName(); if (file) { title += " - " + tr("File sent"); } - notifier.notifyMessagePixmap(title, text, Nexus::getProfile()->loadAvatar(f->getPublicKey())); + notifier.notifyMessagePixmap(title, text, + Nexus::getProfile()->loadAvatar(f->getPublicKey())); } #endif @@ -1537,7 +1571,8 @@ bool Widget::newFriendMessageAlert(const ToxPk& friendId, const QString& text, b return false; } -bool Widget::newGroupMessageAlert(const GroupId& groupId, const ToxPk& authorPk, const QString& message, bool notify) +bool Widget::newGroupMessageAlert(const GroupId& groupId, const ToxPk& authorPk, + const QString& message, bool notify) { bool hasActive; QWidget* currentWindow; @@ -1563,12 +1598,13 @@ bool Widget::newGroupMessageAlert(const GroupId& groupId, const ToxPk& authorPk, if (settings.getNotifyHide()) { notifier.notifyMessageSimple(DesktopNotify::MessageType::GROUP); } else { - Friend *f = FriendList::findFriend(authorPk); + Friend* f = FriendList::findFriend(authorPk); QString title = g->getPeerList().value(authorPk) + " (" + g->getDisplayedName() + ")"; if (!f) { notifier.notifyMessage(title, message); } else { - notifier.notifyMessagePixmap(title, message, Nexus::getProfile()->loadAvatar(f->getPublicKey())); + notifier.notifyMessagePixmap(title, message, + Nexus::getProfile()->loadAvatar(f->getPublicKey())); } } #endif @@ -1903,7 +1939,8 @@ void Widget::onGroupInviteReceived(const GroupInvite& inviteInfo) if (settings.getNotifyHide()) { notifier.notifyMessageSimple(DesktopNotify::MessageType::GROUP_INVITE); } else { - notifier.notifyMessagePixmap(f->getDisplayedName() + tr(" invites you to join a group."), {}, Nexus::getProfile()->loadAvatar(f->getPublicKey())); + notifier.notifyMessagePixmap(f->getDisplayedName() + tr(" invites you to join a group."), + {}, Nexus::getProfile()->loadAvatar(f->getPublicKey())); } #endif } diff --git a/src/widget/widget.h b/src/widget/widget.h index 020b14cecd..525a95dedf 100644 --- a/src/widget/widget.h +++ b/src/widget/widget.h @@ -127,8 +127,10 @@ class Widget final : public QMainWindow void showUpdateDownloadProgress(); void addFriendDialog(const Friend* frnd, ContentDialog* dialog); void addGroupDialog(Group* group, ContentDialog* dialog); - bool newFriendMessageAlert(const ToxPk& friendId, const QString& text, bool sound = true, bool file = false); - bool newGroupMessageAlert(const GroupId& groupId, const ToxPk& authorPk, const QString& message, bool notify); + bool newFriendMessageAlert(const ToxPk& friendId, const QString& text, bool sound = true, + bool file = false); + bool newGroupMessageAlert(const GroupId& groupId, const ToxPk& authorPk, const QString& message, + bool notify); bool getIsWindowMinimized(); void updateIcons(); @@ -193,6 +195,7 @@ public slots: void toggleFullscreen(); void refreshPeerListsLocal(const QString& username); void onUpdateAvailable(QString latestVersion, QUrl link); + void onCoreChanged(Core& core); signals: void friendRequestAccepted(const ToxPk& friendPk);