2014-07-07 00:19:45 +08:00
|
|
|
/*
|
2015-06-06 09:40:08 +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.
|
|
|
|
|
2015-06-06 09:40:08 +08:00
|
|
|
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.
|
2015-06-06 09:40:08 +08:00
|
|
|
|
|
|
|
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
|
2015-06-06 09:40:08 +08:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2014-07-07 00:19:45 +08:00
|
|
|
|
2015-06-06 09:40:08 +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>
|
2014-11-07 06:50:36 +08:00
|
|
|
#include <QErrorMessage>
|
2015-06-17 02:25:19 +08:00
|
|
|
#include <QApplication>
|
2015-03-28 00:43:25 +08:00
|
|
|
#include <QClipboard>
|
2015-10-10 23:25:24 +08:00
|
|
|
#include <QRegularExpression>
|
2014-07-06 08:29:57 +08:00
|
|
|
#include <tox/tox.h>
|
2015-02-24 21:10:51 +08:00
|
|
|
#include "src/nexus.h"
|
2015-04-24 08:32:09 +08:00
|
|
|
#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"
|
2015-06-17 02:25:19 +08:00
|
|
|
#include "src/widget/contentlayout.h"
|
2015-07-20 22:03:01 +08:00
|
|
|
#include <QWindow>
|
2014-11-06 06:27:51 +08:00
|
|
|
|
2014-11-06 22:12:10 +08:00
|
|
|
AddFriendForm::AddFriendForm()
|
2014-11-06 06:27:51 +08:00
|
|
|
{
|
2015-08-31 04:23:05 +08:00
|
|
|
main = new QWidget(), 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
|
|
|
|
2015-08-31 04:23:05 +08:00
|
|
|
retranslateUi();
|
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);
|
|
|
|
|
2015-10-10 21:24:23 +08:00
|
|
|
connect(&toxId, &QLineEdit::returnPressed, this, &AddFriendForm::onSendTriggered);
|
|
|
|
connect(&toxId, &QLineEdit::textChanged, this, &AddFriendForm::onIdChanged);
|
2014-06-25 04:11:11 +08:00
|
|
|
connect(&sendButton, SIGNAL(clicked()), this, SLOT(onSendTriggered()));
|
2015-02-24 21:10:51 +08:00
|
|
|
connect(Nexus::getCore(), &Core::usernameSet, this, &AddFriendForm::onUsernameSet);
|
2015-06-06 03:37:01 +08:00
|
|
|
|
|
|
|
Translator::registerHandler(std::bind(&AddFriendForm::retranslateUi, this), this);
|
2014-07-01 02:05:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AddFriendForm::~AddFriendForm()
|
|
|
|
{
|
2015-06-06 03:37:01 +08:00
|
|
|
Translator::unregister(this);
|
2014-07-01 02:05:01 +08:00
|
|
|
head->deleteLater();
|
2015-08-31 04:23:05 +08:00
|
|
|
main->deleteLater();
|
2014-06-25 04:11:11 +08:00
|
|
|
}
|
|
|
|
|
2015-06-17 02:25:19 +08:00
|
|
|
bool AddFriendForm::isShown() const
|
2014-06-25 04:11:11 +08:00
|
|
|
{
|
2015-06-17 02:25:19 +08:00
|
|
|
if (main->isVisible())
|
|
|
|
{
|
2015-07-20 22:03:01 +08:00
|
|
|
head->window()->windowHandle()->alert(0);
|
2015-06-17 02:25:19 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddFriendForm::show(ContentLayout* contentLayout)
|
|
|
|
{
|
|
|
|
contentLayout->mainContent->layout()->addWidget(main);
|
|
|
|
contentLayout->mainHead->layout()->addWidget(head);
|
2015-08-31 04:23:05 +08:00
|
|
|
main->show();
|
2014-06-25 04:11:11 +08:00
|
|
|
head->show();
|
2015-03-28 00:43:25 +08:00
|
|
|
setIdFromClipboard();
|
2015-03-26 19:20:55 +08:00
|
|
|
toxId.setFocus();
|
2014-06-25 04:11:11 +08:00
|
|
|
}
|
|
|
|
|
2014-11-06 22:12:10 +08:00
|
|
|
QString AddFriendForm::getMessage() const
|
2014-07-01 02:05:01 +08:00
|
|
|
{
|
2014-11-06 22:12:10 +08:00
|
|
|
const QString msg = message.toPlainText();
|
|
|
|
return !msg.isEmpty() ? msg : message.placeholderText();
|
2014-07-01 02:05:01 +08:00
|
|
|
}
|
|
|
|
|
2015-02-24 21:10:51 +08:00
|
|
|
void AddFriendForm::onUsernameSet(const QString& username)
|
|
|
|
{
|
2015-06-06 03:37:01 +08:00
|
|
|
lastUsername = username;
|
|
|
|
retranslateUi();
|
2015-02-24 21:10:51 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-10-10 21:24:23 +08:00
|
|
|
if (ToxId::isToxId(id))
|
2015-04-24 19:01:50 +08:00
|
|
|
{
|
2014-09-12 21:41:41 +08:00
|
|
|
if (id.toUpper() == Core::getInstance()->getSelfId().toString().toUpper())
|
2015-03-12 10:17:52 +08:00
|
|
|
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"));
|
2014-09-11 19:40:27 +08:00
|
|
|
else
|
|
|
|
emit friendRequested(id, getMessage());
|
2015-04-24 19:01:50 +08:00
|
|
|
|
2014-11-06 06:27:51 +08:00
|
|
|
this->toxId.clear();
|
|
|
|
this->message.clear();
|
2015-04-24 19:01:50 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-28 20:32:25 +08:00
|
|
|
if (Settings::getInstance().getProxyType() != ProxyType::ptNone)
|
2014-11-07 06:50:36 +08:00
|
|
|
{
|
2014-11-09 20:27:08 +08:00
|
|
|
QMessageBox::StandardButton btn = QMessageBox::warning(main, "qTox", tr("qTox needs to use the Tox DNS, but can't do it through a proxy.\n\
|
2015-01-23 03:21:07 +08:00
|
|
|
Ignore the proxy and connect to the Internet directly?"), QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
|
|
|
|
if (btn != QMessageBox::Yes)
|
2014-11-07 06:50:36 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-18 04:55:50 +08:00
|
|
|
ToxId toxId = ToxDNS::resolveToxAddress(id, true);
|
2014-06-25 04:11:11 +08:00
|
|
|
|
2014-11-06 22:12:10 +08:00
|
|
|
if (toxId.toString().isEmpty())
|
2014-11-06 06:27:51 +08:00
|
|
|
{
|
2015-03-12 10:17:52 +08:00
|
|
|
GUI::showWarning(tr("Couldn't add friend"), tr("This Tox ID does not exist","DNS error"));
|
2014-11-06 22:12:10 +08:00
|
|
|
return;
|
2014-11-06 06:27:51 +08:00
|
|
|
}
|
|
|
|
|
2014-11-06 22:12:10 +08:00
|
|
|
emit friendRequested(toxId.toString(), getMessage());
|
|
|
|
this->toxId.clear();
|
|
|
|
this->message.clear();
|
2014-11-06 06:27:51 +08:00
|
|
|
}
|
2014-06-25 04:11:11 +08:00
|
|
|
}
|
2015-03-28 00:43:25 +08:00
|
|
|
|
2015-10-10 21:24:23 +08:00
|
|
|
void AddFriendForm::onIdChanged(const QString &id)
|
|
|
|
{
|
|
|
|
QString tId = id.trimmed();
|
2015-10-10 23:25:24 +08:00
|
|
|
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
|
|
|
}
|
2015-10-10 23:25:24 +08:00
|
|
|
|
2015-10-10 21:24:23 +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-10 23:25:24 +08:00
|
|
|
|
2015-10-13 06:32:04 +08:00
|
|
|
sendButton.setEnabled(isValidId && !tId.isEmpty());
|
2015-10-10 21:24:23 +08:00
|
|
|
}
|
|
|
|
|
2015-03-28 00:43:25 +08:00
|
|
|
void AddFriendForm::setIdFromClipboard()
|
|
|
|
{
|
|
|
|
QClipboard* clipboard = QApplication::clipboard();
|
|
|
|
QString id = clipboard->text().trimmed();
|
2015-05-18 04:55:50 +08:00
|
|
|
if (Core::getInstance()->isReady() && !id.isEmpty() && ToxId::isToxId(id))
|
2015-04-24 19:01:50 +08:00
|
|
|
{
|
2015-12-17 18:24:01 +08:00
|
|
|
if (!ToxId(id).isSelf())
|
2015-03-28 00:43:25 +08:00
|
|
|
toxId.setText(id);
|
|
|
|
}
|
|
|
|
}
|
2015-08-21 22:32:00 +08:00
|
|
|
|
2015-06-06 03:37:01 +08:00
|
|
|
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());
|
2015-08-22 00:36:59 +08:00
|
|
|
}
|