Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
fix(core): correctly relink ui/core when core is changed
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Kribylet committed Jun 26, 2019
1 parent a5754ab commit 17d5d55
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 44 deletions.
32 changes: 2 additions & 30 deletions src/nexus.cpp
Expand Up @@ -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();

Expand Down
9 changes: 9 additions & 0 deletions src/persistence/profile.cpp
Expand Up @@ -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();
Expand Down Expand Up @@ -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";
Expand Down
1 change: 1 addition & 0 deletions src/persistence/profile.h
Expand Up @@ -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);
Expand Down
61 changes: 49 additions & 12 deletions src/widget/widget.cpp
Expand Up @@ -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);
Expand Down Expand Up @@ -502,9 +502,9 @@ void Widget::updateIcons()
return;
}

const QString assetSuffix =
Status::getAssetSuffix(static_cast<Status::Status>(ui->statusButton->property("status").toInt()))
+ (eventIcon ? "_event" : "");
const QString assetSuffix = Status::getAssetSuffix(static_cast<Status::Status>(
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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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

Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 5 additions & 2 deletions src/widget/widget.h
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 17d5d55

Please sign in to comment.