1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00
qTox/src/widget/form/addfriendform.cpp

312 lines
10 KiB
C++
Raw Normal View History

2014-07-07 00:19:45 +08:00
/*
Copyright © 2014-2015 by The qTox Project
2014-07-07 00:19:45 +08:00
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre software: you can redistribute it and/or modify
2014-07-07 00:19:45 +08:00
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
qTox is distributed in the hope that it will be useful,
2014-07-07 00:19:45 +08:00
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2014-07-07 00:19:45 +08:00
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
2014-07-07 00:19:45 +08:00
*/
2014-06-25 04:11:11 +08:00
#include "addfriendform.h"
2014-07-01 02:05:01 +08:00
2014-06-25 04:11:11 +08:00
#include <QFont>
2014-07-01 02:05:01 +08:00
#include <QMessageBox>
#include <QErrorMessage>
#include <QApplication>
#include <QClipboard>
#include <QRegularExpression>
#include <QTabWidget>
#include <QSignalMapper>
#include <tox/tox.h>
#include "src/nexus.h"
#include "src/core/core.h"
2015-06-06 07:44:47 +08:00
#include "src/core/cdata.h"
#include "src/net/toxdns.h"
#include "src/persistence/settings.h"
2015-03-12 10:17:52 +08:00
#include "src/widget/gui.h"
2015-06-06 07:44:47 +08:00
#include "src/widget/translator.h"
#include "src/widget/contentlayout.h"
#include "src/net/toxme.h"
#include <QWindow>
2016-02-21 07:45:18 +08:00
#include <QScrollArea>
AddFriendForm::AddFriendForm()
{
tabWidget = new QTabWidget();
main = new QWidget(tabWidget), head = new QWidget();
2014-06-25 04:11:11 +08:00
QFont bold;
bold.setBold(true);
headLabel.setFont(bold);
2015-10-13 06:32:04 +08:00
toxIdLabel.setTextFormat(Qt::RichText);
2014-06-25 04:11:11 +08:00
tabWidget->addTab(main, QString());
QScrollArea* scrollArea = new QScrollArea(tabWidget);
QWidget* requestWidget = new QWidget(tabWidget);
scrollArea->setWidget(requestWidget);
scrollArea->setWidgetResizable(true);
requestsLayout = new QVBoxLayout(requestWidget);
requestsLayout->addStretch(1);
tabWidget->addTab(scrollArea, QString());
2015-08-21 05:58:03 +08:00
2014-06-25 04:11:11 +08:00
main->setLayout(&layout);
layout.addWidget(&toxIdLabel);
layout.addWidget(&toxId);
layout.addWidget(&messageLabel);
layout.addWidget(&message);
layout.addWidget(&sendButton);
head->setLayout(&headLayout);
headLayout.addWidget(&headLabel);
connect(&toxId, &QLineEdit::returnPressed, this, &AddFriendForm::onSendTriggered);
connect(&toxId, &QLineEdit::textChanged, this, &AddFriendForm::onIdChanged);
connect(tabWidget, &QTabWidget::currentChanged, this, &AddFriendForm::onCurrentChanged);
connect(&toxId,&QLineEdit::returnPressed, this, &AddFriendForm::onSendTriggered);
2014-06-25 04:11:11 +08:00
connect(&sendButton, SIGNAL(clicked()), this, SLOT(onSendTriggered()));
connect(Nexus::getCore(), &Core::usernameSet, this, &AddFriendForm::onUsernameSet);
retranslateUi();
Translator::registerHandler(std::bind(&AddFriendForm::retranslateUi, this), this);
int size = Settings::getInstance().getFriendRequestSize();
for (int i = 0; i < size; ++i)
{
QPair<QString, QString> request = Settings::getInstance().getFriendRequest(i);
addFriendRequestWidget(request.first, request.second);
}
2014-07-01 02:05:01 +08:00
}
AddFriendForm::~AddFriendForm()
{
Translator::unregister(this);
2014-07-01 02:05:01 +08:00
head->deleteLater();
tabWidget->deleteLater();
2014-06-25 04:11:11 +08:00
}
bool AddFriendForm::isShown() const
2014-06-25 04:11:11 +08:00
{
if (main->isVisible())
{
head->window()->windowHandle()->alert(0);
return true;
}
return false;
}
void AddFriendForm::show(ContentLayout* contentLayout)
{
2016-02-21 07:45:18 +08:00
contentLayout->mainContent->layout()->addWidget(tabWidget);
contentLayout->mainHead->layout()->addWidget(head);
tabWidget->show();
2014-06-25 04:11:11 +08:00
head->show();
setIdFromClipboard();
toxId.setFocus();
2014-06-25 04:11:11 +08:00
}
QString AddFriendForm::getMessage() const
2014-07-01 02:05:01 +08:00
{
const QString msg = message.toPlainText();
return !msg.isEmpty() ? msg : message.placeholderText();
2014-07-01 02:05:01 +08:00
}
void AddFriendForm::setMode(Mode mode)
{
tabWidget->setCurrentIndex(mode);
}
void AddFriendForm::addFriendRequest(const QString &friendAddress, const QString &message)
{
addFriendRequestWidget(friendAddress, message);
Settings::getInstance().addFriendRequest(friendAddress, message);
onCurrentChanged(tabWidget->currentIndex());
}
void AddFriendForm::onUsernameSet(const QString& username)
{
lastUsername = username;
retranslateUi();
}
2014-06-25 04:11:11 +08:00
void AddFriendForm::onSendTriggered()
{
2014-07-01 02:33:43 +08:00
QString id = toxId.text().trimmed();
2014-07-01 02:05:01 +08:00
2016-01-30 06:30:07 +08:00
if (!ToxId::isToxId(id))
2015-04-24 19:01:50 +08:00
{
ToxId toxId = Toxme::lookup(id); // Try Toxme
if (toxId.toString().isEmpty()) // If it isn't supported
{
2016-01-30 06:30:07 +08:00
qDebug() << "Toxme didn't return a ToxID, trying ToxDNS";
if (Settings::getInstance().getProxyType() != ProxyType::ptNone)
{
QMessageBox::StandardButton btn = QMessageBox::warning(main, "qTox", tr("qTox needs to use the Tox DNS, but can't do it through a proxy.\n\
Ignore the proxy and connect to the Internet directly?"), QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
if (btn != QMessageBox::Yes)
return;
}
toxId = ToxDNS::resolveToxAddress(id, true); // Use ToxDNS
if (toxId.toString().isEmpty())
{
GUI::showWarning(tr("Couldn't add friend"), tr("This Tox ID does not exist","DNS error"));
return;
}
}
2016-01-30 06:30:07 +08:00
id = toxId.toString();
}
2016-01-30 06:30:07 +08:00
if (id.toUpper() == Core::getInstance()->getSelfId().toString().toUpper())
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(id, getMessage());
this->toxId.clear();
this->message.clear();
2014-06-25 04:11:11 +08:00
}
void AddFriendForm::onIdChanged(const QString &id)
{
QString tId = id.trimmed();
QRegularExpression dnsIdExpression("^\\S+@\\S+$");
2015-10-13 06:32:04 +08:00
bool isValidId = tId.isEmpty() || ToxId::isToxId(tId) || tId.contains(dnsIdExpression);
QString toxIdText(tr("Tox ID", "Tox ID of the person you're sending a friend request to"));
2015-10-25 16:51:13 +08:00
QString toxIdComment(tr("either 76 hexadecimal characters or name@example.com", "Tox ID format description"));
2015-10-13 06:32:04 +08:00
if(isValidId)
{
2015-10-14 02:24:04 +08:00
toxIdLabel.setText(toxIdText +
QStringLiteral(" (") +
toxIdComment +
QStringLiteral(")"));
2015-10-13 06:32:04 +08:00
} else
{
2015-10-14 02:24:04 +08:00
toxIdLabel.setText(toxIdText +
QStringLiteral(" <font color='red'>(") +
toxIdComment +
QStringLiteral(")</font>"));
2015-10-13 06:32:04 +08:00
}
toxId.setStyleSheet(isValidId ? QStringLiteral("") : QStringLiteral("QLineEdit { background-color: #FFC1C1; }"));
2015-10-13 06:32:04 +08:00
toxId.setToolTip(isValidId ? QStringLiteral("") : tr("Invalid Tox ID format"));
2015-10-13 06:32:04 +08:00
sendButton.setEnabled(isValidId && !tId.isEmpty());
}
void AddFriendForm::setIdFromClipboard()
{
QClipboard* clipboard = QApplication::clipboard();
QString id = clipboard->text().trimmed();
if (Core::getInstance()->isReady() && !id.isEmpty() && ToxId::isToxId(id))
2015-04-24 19:01:50 +08:00
{
if (!ToxId(id).isSelf())
toxId.setText(id);
}
}
2015-08-21 22:32:00 +08:00
void AddFriendForm::onFriendRequestAccepted()
{
2016-02-21 20:46:45 +08:00
QPushButton* acceptButton = static_cast<QPushButton*>(sender());
QWidget* friendWidget = acceptButton->parentWidget();
int index = requestsLayout->indexOf(friendWidget);
friendWidget->deleteLater();
requestsLayout->removeWidget(friendWidget);
emit friendRequestAccepted(Settings::getInstance().getFriendRequest(requestsLayout->count() - index - 1).first);
Settings::getInstance().removeFriendRequest(requestsLayout->count() - index - 1);
Settings::getInstance().savePersonal();
}
void AddFriendForm::onFriendRequestRejected()
{
2016-02-21 20:46:45 +08:00
QPushButton* rejectButton = static_cast<QPushButton*>(sender());
QWidget* friendWidget = rejectButton->parentWidget();
int index = requestsLayout->indexOf(friendWidget);
friendWidget->deleteLater();
requestsLayout->removeWidget(friendWidget);
Settings::getInstance().removeFriendRequest(requestsLayout->count() - index - 1);
Settings::getInstance().savePersonal();
}
void AddFriendForm::onCurrentChanged(int index)
{
if (index == FriendRequest && Settings::getInstance().getUnreadFriendRequests() != 0)
{
Settings::getInstance().clearUnreadFriendRequests();
Settings::getInstance().savePersonal();
emit friendRequestsSeen();
}
}
void AddFriendForm::retranslateUi()
{
headLabel.setText(tr("Add Friends"));
messageLabel.setText(tr("Message","The message you send in friend requests"));
sendButton.setText(tr("Send friend request"));
message.setPlaceholderText(tr("%1 here! Tox me maybe?",
"Default message in friend requests if the field is left blank. Write something appropriate!")
.arg(lastUsername));
2015-10-13 06:32:04 +08:00
onIdChanged(toxId.text());
tabWidget->setTabText(0, tr("Add a friend"));
tabWidget->setTabText(1, tr("Friend requests"));
for (QPushButton* acceptButton : acceptButtons)
retranslateAcceptButton(acceptButton);
for (QPushButton* rejectButton : rejectButtons)
retranslateRejectButton(rejectButton);
}
void AddFriendForm::addFriendRequestWidget(const QString &friendAddress, const QString &message)
{
QWidget* friendWidget = new QWidget(tabWidget);
QHBoxLayout* friendLayout = new QHBoxLayout(friendWidget);
QVBoxLayout* horLayout = new QVBoxLayout();
horLayout->setMargin(0);
friendLayout->addLayout(horLayout);
2016-02-21 07:45:18 +08:00
QLabel* friendLabel = new QLabel(friendWidget);
friendLabel->setText("<b>" + friendAddress + "</b>");
horLayout->addWidget(friendLabel);
QLabel* messageLabel = new QLabel(message);
messageLabel->setWordWrap(true);
horLayout->addWidget(messageLabel, 1);
QPushButton* acceptButton = new QPushButton(friendWidget);
acceptButtons.insert(acceptButton);
2016-02-21 20:46:45 +08:00
connect(acceptButton, &QPushButton::clicked, this, &AddFriendForm::onFriendRequestAccepted);
friendLayout->addWidget(acceptButton);
retranslateAcceptButton(acceptButton);
QPushButton* rejectButton = new QPushButton(friendWidget);
2016-02-21 20:46:45 +08:00
acceptButtons.insert(rejectButton);
connect(rejectButton, &QPushButton::clicked, this, &AddFriendForm::onFriendRequestRejected);
friendLayout->addWidget(rejectButton);
retranslateRejectButton(rejectButton);
requestsLayout->insertWidget(0, friendWidget);
}
void AddFriendForm::retranslateAcceptButton(QPushButton *acceptButton)
{
acceptButton->setText(tr("Accept"));
}
void AddFriendForm::retranslateRejectButton(QPushButton *rejectButton)
{
rejectButton->setText(tr("Reject"));
}