Skip to content

Instantly share code, notes, and snippets.

@nurupo
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nurupo/1659ce92e7ec918aa690 to your computer and use it in GitHub Desktop.
Save nurupo/1659ce92e7ec918aa690 to your computer and use it in GitHub Desktop.
/*
Copyright (c) 2014 by nurupo <nurupo.contributions@gmail.com>
This file is part of Tox DNS Record Resolver.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "mainwindow.hpp"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
/*
Copyright (c) 2014 by nurupo <nurupo.contributions@gmail.com>
This file is part of Tox DNS Record Resolver.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "mainwindow.hpp"
#include <QApplication>
#include <QDesktopServices>
#include <QDesktopWidget>
#include <QDnsLookup>
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QRegularExpression>
#include <QUrl>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle("Tox DNS Record Resolver");
const int screenWidth = QApplication::desktop()->width();
const int screenHeight = QApplication::desktop()->height();
const int appWidth = 300;
const int appHeight = 40;
setGeometry((screenWidth - appWidth) / 2, (screenHeight - appHeight) / 2, appWidth, appHeight);
setStyleSheet("QMainWindow{background-color: #414042;}");
QWidget* widget = new QWidget(this);
QVBoxLayout* layout = new QVBoxLayout(widget);
toxtoLineEdit = new QLineEdit(widget);
toxtoLineEdit->setPlaceholderText(tr("Enter a tox DNS record address and press <Enter>"));
toxtoLineEdit->setAlignment(Qt::AlignHCenter);
connect(toxtoLineEdit, &QLineEdit::returnPressed, this, &MainWindow::parse);
toxtoLineEdit->setStyleSheet("QLineEdit{border: 2px solid gray; border-radius: 4px; padding: 0 8px; background: darkgray; selection-background-color: gray;}");
infoLabel = new QLabel("info", widget);
infoLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
infoLabel->setOpenExternalLinks(true);
infoLabel->setAlignment(Qt::AlignHCenter);
infoLabel->setTextFormat(Qt::RichText);
infoLabel->setStyleSheet("QLabel{border-radius: 4px; padding: 2px; color: lightgray }");
layout->addWidget(toxtoLineEdit);
layout->addWidget(infoLabel);
setCentralWidget(widget);
lookup = new QDnsLookup(this);
lookup->setType(QDnsLookup::TXT);
connect(lookup, &QDnsLookup::finished, this, &MainWindow::process);
}
void MainWindow::parse()
{
static const QRegularExpression re("^(\\S+://)?(\\S+)@(\\S+)$");
QRegularExpressionMatch match = re.match(toxtoLineEdit->text().trimmed());
if (match.hasMatch()) {
QString txt = match.captured(2);
QString domain = match.captured(3);
lookup->setName(txt + "._tox." + domain);
infoLabel->setText("Looking up " + txt + "._tox." + domain);
lookup->lookup();
} else {
infoLabel->setText("Error: Couldn't parse");
}
}
void MainWindow::process()
{
if (lookup->error() != QDnsLookup::NoError) {
infoLabel->setText("Error: " + lookup->errorString());
return;
}
QList<QDnsTextRecord> records = lookup->textRecords();
if (records.size() != 1) {
infoLabel->setText(QString("Error: Unexpected number of TXT records (%1)").arg(records.size()));
return;
}
QList<QByteArray> values = records[0].values();
if (values.size() != 1) {
infoLabel->setText(QString("Error: Unexpected number of TXT values (%1)").arg(values.size()));
return;
}
QString toxId = values[0].right(76);
infoLabel->setText("<style type=text/css>a{color:lightgray;}</style><a href=\"tox://" + toxId + "\">" + "tox://" + toxId + "</a>");
QDesktopServices::openUrl(QUrl("tox://" + toxId));
}
/*
Copyright (c) 2014 by nurupo <nurupo.contributions@gmail.com>
This file is part of Tox DNS Record Resolver.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP
#include <QMainWindow>
class QDnsLookup;
class QLabel;
class QLineEdit;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
private:
QLineEdit* toxtoLineEdit;
QLabel* infoLabel;
QDnsLookup* lookup;
private slots:
void parse();
void process();
};
#endif // MAINWINDOW_HPP
#-------------------------------------------------
#
# Project created by QtCreator 2014-04-01T02:11:23
#
#-------------------------------------------------
QT += widgets network
TARGET = ToxDNSRecordResolver
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.hpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment