From 196eb8d77a64011640c7a15f8387518744a4e105 Mon Sep 17 00:00:00 2001 From: Diadlo Date: Sun, 20 Aug 2017 17:02:25 +0300 Subject: [PATCH] refactor: Extract the same code to 'addFriend function' --- src/widget/form/addfriendform.cpp | 39 +++++++++++-------------------- src/widget/form/addfriendform.h | 1 + 2 files changed, 14 insertions(+), 26 deletions(-) diff --git a/src/widget/form/addfriendform.cpp b/src/widget/form/addfriendform.cpp index 76481a454..87f7a605e 100644 --- a/src/widget/form/addfriendform.cpp +++ b/src/widget/form/addfriendform.cpp @@ -104,8 +104,7 @@ AddFriendForm::AddFriendForm() retranslateUi(); Translator::registerHandler(std::bind(&AddFriendForm::retranslateUi, this), this); - int size = Settings::getInstance().getFriendRequestSize(); - + const int size = Settings::getInstance().getFriendRequestSize(); for (int i = 0; i < size; ++i) { Settings::Request request = Settings::getInstance().getFriendRequest(i); addFriendRequestWidget(request.address, request.message); @@ -140,7 +139,7 @@ void AddFriendForm::show(ContentLayout* contentLayout) // Fix #3421 // Needed to update tab after opening window - int index = tabWidget->currentIndex(); + const int index = tabWidget->currentIndex(); onCurrentChanged(index); } @@ -180,16 +179,16 @@ void AddFriendForm::onUsernameSet(const QString& username) retranslateUi(); } -void AddFriendForm::onSendTriggered() +static void AddFriendForm::addFriend(const QString& idText) { - QString idText = toxId.text().trimmed(); ToxId friendId(idText); if (!friendId.isValid()) { friendId = Toxme::lookup(idText); // Try Toxme if (!friendId.isValid()) { GUI::showWarning(tr("Couldn't add friend"), - tr("This Tox ID is invalid or does not exist", "Toxme error")); + tr("%1 Tox ID is invalid or does not exist", "Toxme error") + .arg(idText)); return; } } @@ -202,6 +201,12 @@ void AddFriendForm::onSendTriggered() } else { emit friendRequested(friendId, getMessage()); } +} + +void AddFriendForm::onSendTriggered() +{ + const QString idText = toxId.text().trimmed(); + addFriend(idText); this->toxId.clear(); this->message.clear(); @@ -209,26 +214,8 @@ void AddFriendForm::onSendTriggered() void AddFriendForm::onImportSendClicked() { - for (QString& idText : contactsToImport) - { - ToxId friendId(idText); - - if (!friendId.isValid()) - { - friendId = Toxme::lookup(idText); // Try Toxme - if (!friendId.isValid()) { - continue; - } - } - - deleteFriendRequest(friendId); - if (friendId == Core::getInstance()->getSelfId()) { - GUI::showWarning(tr("Couldn't add friend"), - tr("You can't add yourself as a friend!", - "When trying to add your own Tox ID as friend")); - } else { - emit friendRequested(friendId, getImportMessage()); - } + for (const QString& idText : contactsToImport) { + addFriend(idText); } contactsToImport.clear(); diff --git a/src/widget/form/addfriendform.h b/src/widget/form/addfriendform.h index 674540d53..371f71f3f 100644 --- a/src/widget/form/addfriendform.h +++ b/src/widget/form/addfriendform.h @@ -75,6 +75,7 @@ private slots: void onCurrentChanged(int index); private: + static void addFriend(const QString& idText); void retranslateUi(); void addFriendRequestWidget(const QString& friendAddress, const QString& message); void removeFriendRequestWidget(QWidget* friendWidget);