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

Merge pull request #34 from retuxx/dns

Add Tox DNS lookup
This commit is contained in:
Tux3 / Mlkj / !Lev.uXFMLA 2014-06-30 20:36:15 +02:00
commit d465fa7356
2 changed files with 100 additions and 11 deletions

View File

@ -1,9 +1,14 @@
#include "addfriendform.h" #include "addfriendform.h"
#include "ui_widget.h"
#include <QFont>
AddFriendForm::AddFriendForm() #include <QFont>
#include <QMessageBox>
#define TOX_ID_SIZE 76
AddFriendForm::AddFriendForm() : dns(this)
{ {
dns.setType(QDnsLookup::TXT);
main = new QWidget(), head = new QWidget(); main = new QWidget(), head = new QWidget();
QFont bold; QFont bold;
bold.setBold(true); bold.setBold(true);
@ -25,6 +30,13 @@ AddFriendForm::AddFriendForm()
headLayout.addWidget(&headLabel); headLayout.addWidget(&headLabel);
connect(&sendButton, SIGNAL(clicked()), this, SLOT(onSendTriggered())); connect(&sendButton, SIGNAL(clicked()), this, SLOT(onSendTriggered()));
connect(&dns, SIGNAL(finished()), this, SLOT(handleDnsLookup()));
}
AddFriendForm::~AddFriendForm()
{
head->deleteLater();
main->deleteLater();
} }
void AddFriendForm::show(Ui::Widget &ui) void AddFriendForm::show(Ui::Widget &ui)
@ -35,13 +47,81 @@ void AddFriendForm::show(Ui::Widget &ui)
head->show(); head->show();
} }
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();
return !msg.isEmpty() ? msg : "Tox me maybe?";
}
void AddFriendForm::onSendTriggered() void AddFriendForm::onSendTriggered()
{ {
QString id = toxId.text(), msg = message.toPlainText(); QString id = toxId.text().trimmed();
if (id.isEmpty())
return;
if (msg.isEmpty())
msg = "Tox me maybe?";
emit friendRequested(id, msg); if (id.isEmpty()) {
showWarning("Please fill in a valid Tox ID");
} else if (isToxId(id)) {
emit friendRequested(id, getMessage());
} else {
id = id.replace("@", "._tox.");
dns.setName(id);
dns.lookup();
}
}
void AddFriendForm::handleDnsLookup()
{
const QString idKeyWord("id=");
if (dns.error() != QDnsLookup::NoError) {
showWarning("Error while looking up DNS");
return;
}
const QList<QDnsTextRecord> textRecords = dns.textRecords();
if (textRecords.length() != 1) {
showWarning("Unexpected number of text records");
return;
}
const QList<QByteArray> textRecordValues = textRecords.first().values();
if (textRecordValues.length() != 1) {
showWarning("Unexpected number of values in text record");
return;
}
const QString entry(textRecordValues.first());
int idx = entry.indexOf(idKeyWord);
if (idx < 0) {
showWarning("The DNS lookup does not contain any Tox ID");
return;
}
idx += idKeyWord.length();
if (entry.length() < idx + static_cast<int>(TOX_ID_SIZE)) {
showWarning("The DNS lookup does not contain a valid Tox ID");
return;
}
const QString friendAdress = entry.mid(idx, TOX_ID_SIZE);
if (!isToxId(friendAdress)) {
showWarning("The DNS lookup does not contain a valid Tox ID");
return;
}
// finally we got it
emit friendRequested(friendAdress, getMessage());
} }

View File

@ -1,27 +1,33 @@
#ifndef ADDFRIENDFORM_H #ifndef ADDFRIENDFORM_H
#define ADDFRIENDFORM_H #define ADDFRIENDFORM_H
#include "ui_widget.h"
#include <QVBoxLayout> #include <QVBoxLayout>
#include <QLabel> #include <QLabel>
#include <QLineEdit> #include <QLineEdit>
#include <QTextEdit> #include <QTextEdit>
#include <QPushButton> #include <QPushButton>
#include <QDnsLookup>
#include "ui_widget.h"
class AddFriendForm : public QObject class AddFriendForm : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
AddFriendForm(); AddFriendForm();
~AddFriendForm();
void show(Ui::Widget& ui); void show(Ui::Widget& ui);
bool isToxId(const QString& value) const;
void showWarning(const QString& message) const;
QString getMessage() const;
signals: signals:
void friendRequested(const QString& friendAddress, const QString& message); void friendRequested(const QString& friendAddress, const QString& message);
private slots: private slots:
void onSendTriggered(); void onSendTriggered();
void handleDnsLookup();
private: private:
QLabel headLabel, toxIdLabel, messageLabel; QLabel headLabel, toxIdLabel, messageLabel;
@ -30,6 +36,9 @@ private:
QTextEdit message; QTextEdit message;
QVBoxLayout layout, headLayout; QVBoxLayout layout, headLayout;
QWidget *head, *main; QWidget *head, *main;
/** will be used for dns discovery if necessary */
QDnsLookup dns;
}; };
#endif // ADDFRIENDFORM_H #endif // ADDFRIENDFORM_H