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

130 lines
3.7 KiB
C++
Raw Normal View History

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>
#define TOX_ID_SIZE 76
2014-06-25 04:11:11 +08:00
2014-07-01 02:05:01 +08:00
AddFriendForm::AddFriendForm() : dns(this)
2014-06-25 04:11:11 +08:00
{
2014-07-01 02:05:01 +08:00
dns.setType(QDnsLookup::TXT);
2014-06-25 04:11:11 +08:00
main = new QWidget(), head = new QWidget();
QFont bold;
bold.setBold(true);
2014-07-04 03:42:23 +08:00
headLabel.setText(tr("Add Friends"));
2014-06-25 04:11:11 +08:00
headLabel.setFont(bold);
2014-07-04 03:42:23 +08:00
toxIdLabel.setText(tr("Tox ID","Tox ID of the person you're sending a friend request to"));
messageLabel.setText(tr("Message","The message you send in friend requests"));
sendButton.setText(tr("Send friend request"));
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(&sendButton, SIGNAL(clicked()), this, SLOT(onSendTriggered()));
2014-07-01 02:05:01 +08:00
connect(&dns, SIGNAL(finished()), this, SLOT(handleDnsLookup()));
}
AddFriendForm::~AddFriendForm()
{
head->deleteLater();
main->deleteLater();
2014-06-25 04:11:11 +08:00
}
void AddFriendForm::show(Ui::Widget &ui)
{
ui.mainContent->layout()->addWidget(main);
ui.mainHead->layout()->addWidget(head);
main->show();
head->show();
}
2014-07-01 02:05:01 +08:00
bool AddFriendForm::isToxId(const QString &value) const
{
const QRegularExpression hexRegExp("^[A-Fa-f0-9]+$");
return value.length() == TOX_ID_SIZE && value.contains(hexRegExp);
}
void AddFriendForm::showWarning(const QString &message) const
{
QMessageBox warning(main);
warning.setText(message);
warning.setIcon(QMessageBox::Warning);
warning.exec();
}
QString AddFriendForm::getMessage() const
{
const QString msg = message.toPlainText();
2014-07-04 03:42:23 +08:00
return !msg.isEmpty() ? msg : tr("Tox me maybe?","Default message in friend requests if the field is left blank. Write something appropriate!");
2014-07-01 02:05:01 +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
if (id.isEmpty()) {
2014-07-04 03:42:23 +08:00
showWarning(tr("Please fill in a valid Tox ID","Tox ID of the friend you're sending a friend request to"));
2014-07-01 02:05:01 +08:00
} else if (isToxId(id)) {
emit friendRequested(id, getMessage());
2014-07-03 11:13:47 +08:00
this->toxId.setText("");
this->message.setText("");
2014-07-01 02:05:01 +08:00
} else {
2014-07-01 02:33:43 +08:00
id = id.replace("@", "._tox.");
2014-07-01 02:05:01 +08:00
dns.setName(id);
dns.lookup();
}
}
void AddFriendForm::handleDnsLookup()
{
const QString idKeyWord("id=");
if (dns.error() != QDnsLookup::NoError) {
2014-07-04 03:42:23 +08:00
showWarning(tr("Error while looking up DNS","The DNS gives the Tox ID associated to toxme.se addresses"));
2014-07-01 02:05:01 +08:00
return;
}
const QList<QDnsTextRecord> textRecords = dns.textRecords();
if (textRecords.length() != 1) {
2014-07-04 03:42:23 +08:00
showWarning(tr("Unexpected number of text records", "Error with the DNS"));
2014-07-01 02:05:01 +08:00
return;
}
const QList<QByteArray> textRecordValues = textRecords.first().values();
if (textRecordValues.length() != 1) {
2014-07-04 03:42:23 +08:00
showWarning(tr("Unexpected number of values in text record", "Error with the DNS"));
2014-07-01 02:05:01 +08:00
return;
}
const QString entry(textRecordValues.first());
int idx = entry.indexOf(idKeyWord);
if (idx < 0) {
2014-07-04 03:42:23 +08:00
showWarning(tr("The DNS lookup does not contain any Tox ID", "Error with the DNS"));
2014-07-01 02:05:01 +08:00
return;
}
idx += idKeyWord.length();
if (entry.length() < idx + static_cast<int>(TOX_ID_SIZE)) {
2014-07-04 03:42:23 +08:00
showWarning(tr("The DNS lookup does not contain a valid Tox ID", "Error with the DNS"));
2014-07-01 02:05:01 +08:00
return;
}
const QString friendAdress = entry.mid(idx, TOX_ID_SIZE);
if (!isToxId(friendAdress)) {
2014-07-04 03:42:23 +08:00
showWarning(tr("The DNS lookup does not contain a valid Tox ID", "Error with the DNS"));
2014-06-25 04:11:11 +08:00
return;
2014-07-01 02:05:01 +08:00
}
2014-06-25 04:11:11 +08:00
2014-07-01 02:05:01 +08:00
// finally we got it
emit friendRequested(friendAdress, getMessage());
2014-06-25 04:11:11 +08:00
}