Skip to content

Commit

Permalink
Fixed servers list receival in regitration dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Ri0n committed Oct 6, 2024
1 parent 31774cb commit b8bf8da
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 27 deletions.
10 changes: 7 additions & 3 deletions src/accountregdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ AccountRegDlg::~AccountRegDlg() { delete client_; }
void AccountRegDlg::done(int r)
{
if (ui_.busy->isActive()) {
int n = QMessageBox::information(this, tr("Warning"), tr("Are you sure you want to cancel the registration?"),
QMessageBox::Yes | QMessageBox::No);
if (n != 0)
auto btn
= QMessageBox::information(this, tr("Warning"), tr("Are you sure you want to cancel the registration?"),
QMessageBox::Yes | QMessageBox::No);
if (btn != QMessageBox::Yes) {
return;
}
}
client_->close();
QDialog::done(r);
Expand Down Expand Up @@ -157,9 +159,11 @@ void AccountRegDlg::serverListReceived(const QStringList &list)
{
ui_.busy->stop();
unblock();
ui_.le_server->blockSignals(true);
ui_.le_server->clear();
ui_.le_server->addItems(list);
ui_.le_server->showPopup();
ui_.le_server->blockSignals(false);
}

void AccountRegDlg::serverListError(const QString &e)
Expand Down
78 changes: 54 additions & 24 deletions src/serverlistquerier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@

#include "serverlistquerier.h"

#ifdef XML_SERVER_LIST
#include <QDomDocument>
#include <QDomElement>
#include <QDomNodeList>
#else
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#endif
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
Expand All @@ -39,7 +45,7 @@
ServerListQuerier::ServerListQuerier(QObject *parent) : QObject(parent), redirectCount_(0)
{
http_ = new QNetworkAccessManager(this);
url_ = QUrl("https://xmpp.net/directory.php");
url_ = QUrl("https://data.xmpp.net/providers/v2/providers-A.json");
}

void ServerListQuerier::getList()
Expand Down Expand Up @@ -75,35 +81,59 @@ void ServerListQuerier::get_finished()
servers.push_back(jid);
}
}
#else
QStringList servers;
QString contents = QString::fromUtf8(reply->readAll());
int index = 0;
QRegularExpression re("data-original-title=\"([^\"]+)\"");
QRegularExpressionMatch match;
while ((index = contents.indexOf(re, index + 1, &match)) != -1) {
servers.append(match.captured(1));
}
#endif
emit listReceived(servers);
} else if (reply->attribute(QNetworkRequest::RedirectionTargetAttribute).isValid()) {
if (redirectCount_ >= SERVERLIST_MAX_REDIRECT) {
emit error(tr("Maximum redirect count reached"));
return;
}
#else

url_ = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).value<QUrl>().resolved(url_);
if (url_.isValid()) {
QNetworkReply *newReply = http_->get(QNetworkRequest(url_));
connect(newReply, SIGNAL(finished()), SLOT(get_finished()));
++redirectCount_;
} else {
emit error(tr("Invalid redirect URL %1").arg(url_.toString()));
return;
if (auto r = parseJson(reply->readAll()); r) {
emit listReceived(*r);
}
#endif
} else {
emit error(tr("Unexpected HTTP status code: %1").arg(status));
}
}
reply->deleteLater();
}

std::optional<QStringList> ServerListQuerier::parseJson(const QByteArray &data)
{
QStringList jidList;
bool parsingErrorOccurred = false;

QJsonDocument jsonDoc = QJsonDocument::fromJson(data);
if (!jsonDoc.isArray()) {
emit error(tr("Failed to parse json."));
return {};
}

QJsonArray jsonArray = jsonDoc.array();

for (const QJsonValue &providerValue : jsonArray) {
if (!providerValue.isObject()) {
parsingErrorOccurred = true;
continue; // Skip if the item is not a valid object
}

QJsonObject providerObj = providerValue.toObject();
auto jidIt = providerObj.find("jid");

if (jidIt == providerObj.end() || !jidIt->isString()) {
parsingErrorOccurred = true;
continue; // Skip if "jid" is not found or is not a string
}

QString jid = jidIt->toString();
if (!jid.isEmpty()) {
jidList.append(jid);
} else {
parsingErrorOccurred = true; // Handle empty jid case
}
}

// Emit an error if the list is empty and there was a parsing error
if (jidList.isEmpty() && parsingErrorOccurred) {
emit error(tr("Failed to parse any valid server JIDs from %1.").arg(url_.toDisplayString()));
}

return jidList;
}
3 changes: 3 additions & 0 deletions src/serverlistquerier.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class ServerListQuerier : public QObject {
protected slots:
void get_finished();

private:
std::optional<QStringList> parseJson(const QByteArray &providers);

private:
QNetworkAccessManager *http_;
QUrl url_;
Expand Down

0 comments on commit b8bf8da

Please sign in to comment.