Skip to content

Commit

Permalink
cleanup: Avoid contextless connect.
Browse files Browse the repository at this point in the history
To ensure signals are disconnected when target objects go out of scope.
  • Loading branch information
iphydf committed Dec 15, 2024
1 parent b2d75b6 commit ea0c192
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 74 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ add_definitions(-DQT_NO_CAST_TO_ASCII)
add_definitions(-DQT_RESTRICTED_CAST_FROM_ASCII)
add_definitions(-DQT_NO_NARROWING_CONVERSIONS_IN_CONNECT)
add_definitions(-DQT_NO_URL_CAST_FROM_STRING)
add_definitions(-DQT_NO_CONTEXTLESS_CONNECT)

# Use ccache when available to speed up builds.
if(USE_CCACHE)
Expand Down
4 changes: 2 additions & 2 deletions src/core/toxcall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ ToxFriendCall::ToxFriendCall(uint32_t friendNum, bool VideoEnabled, CoreAV& av_,
cameraSource.setupDefault();
}
cameraSource.subscribe();
videoInConn = QObject::connect(&cameraSource, &VideoSource::frameAvailable,
videoInConn = QObject::connect(&cameraSource, &VideoSource::frameAvailable, &av_,
[&av_, friendNum](std::shared_ptr<VideoFrame> frame) {
av_.sendCallVideo(friendNum, frame);
});
Expand Down Expand Up @@ -219,7 +219,7 @@ ToxConferenceCall::~ToxConferenceCall()
void ToxConferenceCall::onAudioSourceInvalidated()
{
auto newSrc = audio.makeSource();
connect(audioSource.get(), &IAudioSource::frameAvailable,
connect(audioSource.get(), &IAudioSource::frameAvailable, this,
[this](const int16_t* pcm, size_t samples, uint8_t chans, uint32_t rate) {
if (conference.getPeersCount() <= 1) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/net/avatarbroadcaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void AvatarBroadcaster::enableAutoBroadcast(bool state)
{
disconnect(&core, nullptr, this, nullptr);
if (state) {
connect(&core, &Core::friendStatusChanged,
connect(&core, &Core::friendStatusChanged, this,
[this](uint32_t friendId, Status::Status) { sendAvatarTo(friendId); });
}
}
2 changes: 1 addition & 1 deletion src/nexus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void Nexus::start()

minimizeAction = windowMenu->addAction(QString());
minimizeAction->setShortcut(Qt::CTRL | Qt::Key_M);
connect(minimizeAction, &QAction::triggered, [this]() {
connect(minimizeAction, &QAction::triggered, this, [this]() {
minimizeAction->setEnabled(false);
QApplication::focusWindow()->showMinimized();
});
Expand Down
8 changes: 4 additions & 4 deletions src/video/netcamview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,23 @@ NetCamView::NetCamView(ToxPk friendPk_, CameraSource& cameraSource_, Settings& s
connections +=
connect(selfVideoSurface, &VideoSurface::ratioChanged, this, &NetCamView::updateRatio);

connections += connect(videoSurface, &VideoSurface::boundaryChanged, [this]() {
connections += connect(videoSurface, &VideoSurface::boundaryChanged, this, [this]() {
QRect boundingRect = videoSurface->getBoundingRect();
updateFrameSize(boundingRect.size());
selfFrame->setBoundary(boundingRect);
});

connections += connect(videoSurface, &VideoSurface::ratioChanged, [this]() {
connections += connect(videoSurface, &VideoSurface::ratioChanged, this, [this]() {
selfFrame->setMinimumWidth(selfFrame->minimumHeight() * selfVideoSurface->getRatio());
QRect boundingRect = videoSurface->getBoundingRect();
updateFrameSize(boundingRect.size());
selfFrame->resetBoundary(boundingRect);
});

connections += connect(&profile, &Profile::selfAvatarChanged,
connections += connect(&profile, &Profile::selfAvatarChanged, this,
[this](const QPixmap& pixmap) { selfVideoSurface->setAvatar(pixmap); });

connections += connect(&profile, &Profile::friendAvatarChanged,
connections += connect(&profile, &Profile::friendAvatarChanged, this,
[this](ToxPk friendPkArg, const QPixmap& pixmap) {
if (friendPk == friendPkArg)
videoSurface->setAvatar(pixmap);
Expand Down
4 changes: 2 additions & 2 deletions src/widget/circlewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ CircleWidget::CircleWidget(const Core& core_, FriendListWidget* parent, int id_,
setName(settings.getCircleName(id), false);
circleList[id] = this;

connect(nameLabel, &CroppingLabel::editFinished, [this](const QString& newName) {
connect(nameLabel, &CroppingLabel::editFinished, this, [this](const QString& newName) {
if (!newName.isEmpty())
emit renameRequested(this, newName);
});

connect(nameLabel, &CroppingLabel::editRemoved, [this]() {
connect(nameLabel, &CroppingLabel::editRemoved, this, [this]() {
if (isCompact())
nameLabel->minimizeMaximumWidth();
});
Expand Down
2 changes: 1 addition & 1 deletion src/widget/form/conferenceform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ void ConferenceForm::peerAudioPlaying(ToxPk peerPk)
if (!peerAudioTimers[peerPk]) {
peerAudioTimers[peerPk] = new QTimer(this);
peerAudioTimers[peerPk]->setSingleShot(true);
connect(peerAudioTimers[peerPk], &QTimer::timeout, [this, peerPk] {
connect(peerAudioTimers[peerPk], &QTimer::timeout, this, [this, peerPk] {
auto it = peerLabels.find(peerPk);
if (it != peerLabels.end()) {
peerLabels[peerPk]->setProperty("playingAudio", LABEL_PEER_NOT_PLAYING_AUDIO);
Expand Down
13 changes: 7 additions & 6 deletions src/widget/form/conferenceinviteform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ ConferenceInviteForm::ConferenceInviteForm(Settings& settings_, Core& core_)
, core{core_}
{
QVBoxLayout* layout = new QVBoxLayout(this);
connect(createButton, &QPushButton::clicked,
connect(createButton, &QPushButton::clicked, this,
[this]() { emit conferenceCreate(TOX_CONFERENCE_TYPE_AV); });

QWidget* innerWidget = new QWidget(scroll);
Expand Down Expand Up @@ -114,12 +114,13 @@ bool ConferenceInviteForm::addConferenceInvite(const ConferenceInvite& inviteInf
ConferenceInviteWidget* widget = new ConferenceInviteWidget(this, inviteInfo, settings, core);
scroll->widget()->layout()->addWidget(widget);
invites.append(widget);
connect(widget, &ConferenceInviteWidget::accepted, [this](const ConferenceInvite& inviteInfo_) {
deleteInviteWidget(inviteInfo_);
emit conferenceInviteAccepted(inviteInfo_);
});
connect(widget, &ConferenceInviteWidget::accepted, this,
[this](const ConferenceInvite& inviteInfo_) {
deleteInviteWidget(inviteInfo_);
emit conferenceInviteAccepted(inviteInfo_);
});

connect(widget, &ConferenceInviteWidget::rejected,
connect(widget, &ConferenceInviteWidget::rejected, this,
[this](const ConferenceInvite& inviteInfo_) { deleteInviteWidget(inviteInfo_); });
if (isVisible()) {
emit conferenceInvitesSeen();
Expand Down
4 changes: 2 additions & 2 deletions src/widget/form/conferenceinvitewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ ConferenceInviteWidget::ConferenceInviteWidget(QWidget* parent, const Conference
, settings{settings_}
, core{core_}
{
connect(acceptButton, &QPushButton::clicked, [this] { emit accepted(inviteInfo); });
connect(rejectButton, &QPushButton::clicked, [this] { emit rejected(inviteInfo); });
connect(acceptButton, &QPushButton::clicked, this, [this] { emit accepted(inviteInfo); });
connect(rejectButton, &QPushButton::clicked, this, [this] { emit rejected(inviteInfo); });
widgetLayout->addWidget(inviteMessageLabel);
widgetLayout->addWidget(acceptButton);
widgetLayout->addWidget(rejectButton);
Expand Down
6 changes: 3 additions & 3 deletions src/widget/form/debug/debuglog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ DebugLogForm::DebugLogForm(Paths& paths, Style& style, QWidget* parent)

// Reload logs every 5 seconds
reloadTimer_->start(5000);
connect(reloadTimer_.get(), &QTimer::timeout, [this, &paths] {
connect(reloadTimer_.get(), &QTimer::timeout, this, [this, &paths] {
if (ui_->cbAutoReload->isChecked()) {
debugLogModel_->reload(loadLogs(paths));
}
Expand All @@ -74,12 +74,12 @@ DebugLogForm::DebugLogForm(Paths& paths, Style& style, QWidget* parent)
for (int i = 0; i < filterEnum.keyCount(); ++i) {
ui_->cmbFilters->addItem(QString::fromUtf8(filterEnum.key(i)));
}
connect(ui_->cmbFilters, qOverload<int>(&QComboBox::currentIndexChanged), [this](int index) {
connect(ui_->cmbFilters, qOverload<int>(&QComboBox::currentIndexChanged), this, [this](int index) {
debugLogModel_->setFilter(static_cast<DebugLogModel::Filter>(index));
});

ui_->debugLog->setModel(debugLogModel_.get());
connect(ui_->debugLog, &QListView::doubleClicked, [this](const QModelIndex& index) {
connect(ui_->debugLog, &QListView::doubleClicked, this, [this](const QModelIndex& index) {
const int originalIndex = debugLogModel_->originalIndex(index);
ui_->cmbFilters->setCurrentIndex(0);
ui_->debugLog->scrollTo(debugLogModel_->index(originalIndex));
Expand Down
8 changes: 4 additions & 4 deletions src/widget/form/filesform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,10 @@ FilesForm::FilesForm(CoreFile& coreFile, Settings& settings, Style& style,
coreFile.cancelFileSend(file.friendId, file.fileNum);
};

connect(recvdModel, &FileTransferList::Model::togglePause, pauseFile);
connect(recvdModel, &FileTransferList::Model::cancel, cancelFileRecv);
connect(sentModel, &FileTransferList::Model::togglePause, pauseFile);
connect(sentModel, &FileTransferList::Model::cancel, cancelFileSend);
connect(recvdModel, &FileTransferList::Model::togglePause, &coreFile, pauseFile);
connect(recvdModel, &FileTransferList::Model::cancel, &coreFile, cancelFileRecv);
connect(sentModel, &FileTransferList::Model::togglePause, &coreFile, pauseFile);
connect(sentModel, &FileTransferList::Model::cancel, &coreFile, cancelFileSend);

recvd = new FileTransferList::View(recvdModel, settings, style);
sent = new FileTransferList::View(sentModel, settings, style);
Expand Down
2 changes: 1 addition & 1 deletion src/widget/form/settings/aboutform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void AboutForm::onUpdateAvailable(QString latestVersion, QUrl link)
{
std::ignore = latestVersion;
QObject::disconnect(linkConnection);
linkConnection = connect(bodyUI->updateAvailableButton, &QPushButton::clicked,
linkConnection = connect(bodyUI->updateAvailableButton, &QPushButton::clicked, this,
[link]() { QDesktopServices::openUrl(link); });
bodyUI->updateStack->setCurrentIndex(static_cast<int>(updateIndex::available));
}
Expand Down
9 changes: 5 additions & 4 deletions src/widget/friendlistwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,11 @@ FriendListWidget::SortingMode FriendListWidget::getMode() const
void FriendListWidget::addConferenceWidget(ConferenceWidget* widget)
{
Conference* c = widget->getConference();
connect(c, &Conference::titleChanged, [this, widget](const QString& author, const QString& name) {
std::ignore = author;
renameConferenceWidget(widget, name);
});
connect(c, &Conference::titleChanged, this,
[this, widget](const QString& author, const QString& name) {
std::ignore = author;
renameConferenceWidget(widget, name);
});

manager->addFriendListItem(widget);
}
Expand Down
6 changes: 3 additions & 3 deletions src/widget/friendwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void FriendWidget::onContextMenuCalled(QContextMenuEvent* event)

if (chatroom->possibleToOpenInNewWindow()) {
const auto openChatWindow = menu.addAction(tr("Open chat in new window"));
connect(openChatWindow, &QAction::triggered, [this]() { emit newWindowOpened(this); });
connect(openChatWindow, &QAction::triggered, this, [this]() { emit newWindowOpened(this); });
}

if (chatroom->canBeRemovedFromWindow()) {
Expand All @@ -117,7 +117,7 @@ void FriendWidget::onContextMenuCalled(QContextMenuEvent* event)
for (const auto& conference : chatroom->getConferences()) {
const auto conferenceAction =
inviteMenu->addAction(tr("Invite to conference '%1'").arg(conference.name));
connect(conferenceAction, &QAction::triggered,
connect(conferenceAction, &QAction::triggered, this,
[this, conference] { chatroom->inviteFriend(conference.conference); });
}

Expand All @@ -139,7 +139,7 @@ void FriendWidget::onContextMenuCalled(QContextMenuEvent* event)

for (const auto& circle : chatroom->getOtherCircles()) {
QAction* action = new QAction(tr("Move to circle \"%1\"").arg(circle.name), circleMenu);
connect(action, &QAction::triggered, [this, circle] { moveToCircle(circle.circleId); });
connect(action, &QAction::triggered, this, [this, circle] { moveToCircle(circle.circleId); });
circleMenu->addAction(action);
}

Expand Down
76 changes: 38 additions & 38 deletions src/widget/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ void Widget::init()

fileMenu->menu()->addSeparator();
logoutAction = fileMenu->menu()->addAction(QString());
connect(logoutAction, &QAction::triggered, [this]() { nexus.showLogin(); });
connect(logoutAction, &QAction::triggered, this, [this]() { nexus.showLogin(); });

editMenu = globalMenu->insertMenu(viewMenu, new QMenu(this));
editMenu->menu()->addSeparator();
Expand All @@ -414,7 +414,7 @@ void Widget::init()
nextConversationAction = new QAction(this);
nexus.windowMenu->insertAction(frontAction, nextConversationAction);
nextConversationAction->setShortcut(QKeySequence::SelectNextPage);
connect(nextConversationAction, &QAction::triggered, [this]() {
connect(nextConversationAction, &QAction::triggered, this, [this]() {
if (contentDialogManager->current() == QApplication::activeWindow())
contentDialogManager->current()->cycleChats(true);
else if (QApplication::activeWindow() == this)
Expand All @@ -424,7 +424,7 @@ void Widget::init()
previousConversationAction = new QAction(this);
nexus.windowMenu->insertAction(frontAction, previousConversationAction);
previousConversationAction->setShortcut(QKeySequence::SelectPreviousPage);
connect(previousConversationAction, &QAction::triggered, [this] {
connect(previousConversationAction, &QAction::triggered, this, [this] {
if (contentDialogManager->current() == QApplication::activeWindow())
contentDialogManager->current()->cycleChats(false);
else if (QApplication::activeWindow() == this)
Expand All @@ -439,7 +439,7 @@ void Widget::init()

QAction* aboutAction = viewMenu->menu()->addAction(QString());
aboutAction->setMenuRole(QAction::AboutRole);
connect(aboutAction, &QAction::triggered, [this]() {
connect(aboutAction, &QAction::triggered, this, [this]() {
onShowSettings();
settingsWidget->showAbout();
});
Expand Down Expand Up @@ -1199,15 +1199,12 @@ void Widget::addFriend(uint32_t friendId, const ToxPk& friendPk)

chatListWidget->addFriendWidget(widget);


auto notifyReceivedCallback = [this, friendPk](const ToxPk& author, const Message& message) {
std::ignore = author;
newFriendMessageAlert(friendPk, message.content);
};

auto notifyReceivedConnection =
connect(friendMessageDispatcher.get(), &IMessageDispatcher::messageReceived,
notifyReceivedCallback);
connect(friendMessageDispatcher.get(), &IMessageDispatcher::messageReceived, this,
[this, friendPk](const ToxPk& author, const Message& message) {
std::ignore = author;
newFriendMessageAlert(friendPk, message.content);
});

friendAlertConnections.insert(friendPk, notifyReceivedConnection);
connect(newfriend, &Friend::aliasChanged, this, &Widget::onFriendAliasChanged);
Expand Down Expand Up @@ -1454,11 +1451,12 @@ void Widget::addFriendDialog(const Friend* frnd, ContentDialog* dialog)
connect(friendWidget, &FriendWidget::contextMenuCalled, widget,
[=](QContextMenuEvent* event) { emit widget->contextMenuCalled(event); });

connect(friendWidget, &FriendWidget::chatroomWidgetClicked, [=](GenericChatroomWidget* w) {
std::ignore = w;
emit widget->chatroomWidgetClicked(widget);
});
connect(friendWidget, &FriendWidget::newWindowOpened, [=](GenericChatroomWidget* w) {
connect(friendWidget, &FriendWidget::chatroomWidgetClicked, widget,
[widget](GenericChatroomWidget* w) {
std::ignore = w;
emit widget->chatroomWidgetClicked(widget);
});
connect(friendWidget, &FriendWidget::newWindowOpened, widget, [widget](GenericChatroomWidget* w) {
std::ignore = w;
emit widget->newWindowOpened(widget);
});
Expand Down Expand Up @@ -1502,15 +1500,17 @@ void Widget::addConferenceDialog(const Conference* conference, ContentDialog* di
// Signal transmission from the created `conferenceWidget` (which shown in
// ContentDialog) to the `widget` (which shown in main widget)
// FIXME: emit should be removed
connect(conferenceWidget, &ConferenceWidget::chatroomWidgetClicked, [=](GenericChatroomWidget* w) {
std::ignore = w;
emit widget->chatroomWidgetClicked(widget);
});

connect(conferenceWidget, &ConferenceWidget::newWindowOpened, [=](GenericChatroomWidget* w) {
std::ignore = w;
emit widget->newWindowOpened(widget);
});
connect(conferenceWidget, &ConferenceWidget::chatroomWidgetClicked, widget,
[widget](GenericChatroomWidget* w) {
std::ignore = w;
emit widget->chatroomWidgetClicked(widget);
});

connect(conferenceWidget, &ConferenceWidget::newWindowOpened, widget,
[widget](GenericChatroomWidget* w) {
std::ignore = w;
emit widget->newWindowOpened(widget);
});

// FIXME: emit should be removed
emit widget->chatroomWidgetClicked(widget);
Expand Down Expand Up @@ -2098,7 +2098,7 @@ Conference* Widget::createConference(uint32_t conferencenumber, const Conference
assert(newconference);

if (enabled) {
connect(newconference, &Conference::userLeft, [this, newconference](const ToxPk& user) {
connect(newconference, &Conference::userLeft, this, [this, newconference](const ToxPk& user) {
CoreAV* av = core->getAv();
assert(av);
av->invalidateConferenceCallPeerSource(*newconference, user);
Expand All @@ -2121,17 +2121,17 @@ Conference* Widget::createConference(uint32_t conferencenumber, const Conference
auto chatHistory = std::make_shared<ChatHistory>(*newconference, history, *core, settings,
*messageDispatcher, *friendList, *conferenceList);

auto notifyReceivedCallback = [this, conferenceId](const ToxPk& author, const Message& message) {
auto isTargeted = std::any_of(message.metadata.begin(), message.metadata.end(),
[](MessageMetadata metadata) {
return metadata.type == MessageMetadataType::selfMention;
});
newConferenceMessageAlert(conferenceId, author, message.content,
isTargeted || settings.getConferenceAlwaysNotify());
};

auto notifyReceivedConnection =
connect(messageDispatcher.get(), &IMessageDispatcher::messageReceived, notifyReceivedCallback);
connect(messageDispatcher.get(), &IMessageDispatcher::messageReceived, this,
[this, conferenceId](const ToxPk& author, const Message& message) {
auto isTargeted =
std::any_of(message.metadata.begin(), message.metadata.end(),
[](MessageMetadata metadata) {
return metadata.type == MessageMetadataType::selfMention;
});
newConferenceMessageAlert(conferenceId, author, message.content,
isTargeted || settings.getConferenceAlwaysNotify());
});
conferenceAlertConnections.insert(conferenceId, notifyReceivedConnection);

auto form = new ConferenceForm(*core, newconference, *chatHistory, *messageDispatcher, settings,
Expand Down Expand Up @@ -2585,7 +2585,7 @@ void Widget::friendRequestsUpdate()
friendRequestsButton->setObjectName("green");
ui->statusLayout->insertWidget(2, friendRequestsButton);

connect(friendRequestsButton, &QPushButton::released, [this]() {
connect(friendRequestsButton, &QPushButton::released, this, [this]() {
onAddClicked();
addFriendForm->setMode(AddFriendForm::Mode::FriendRequest);
});
Expand Down
Loading

0 comments on commit ea0c192

Please sign in to comment.