Skip to content

Commit

Permalink
Introduce XData::findField
Browse files Browse the repository at this point in the history
  • Loading branch information
Ri0n committed Oct 13, 2024
1 parent 35cd90c commit b64ba25
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
9 changes: 6 additions & 3 deletions src/xmpp/xmpp-im/httpfileupload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,12 @@ void HttpFileUpload::start()
}
if (ver != XEP0363::vUnknown) {
QVector<std::pair<HttpHost, int>> hosts;
const XData::Field field = item.registeredExtension(xmlns).getField(QLatin1String("max-file-size"));
if (field.isValid() && field.type() == XData::Field::Field_TextSingle)
sizeLimit = field.value().at(0).toULongLong();
auto optField = item.registeredExtension(xmlns).findField(QLatin1String("max-file-size"));
if (optField) {
const XData::Field &field = *optField;
if (field.isValid() && field.type() == XData::Field::Field_TextSingle)
sizeLimit = field.value().value(0).toULongLong();
}
HttpHost host;
host.ver = ver;
host.jid = item.jid();
Expand Down
10 changes: 6 additions & 4 deletions src/xmpp/xmpp-im/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2902,20 +2902,22 @@ CaptchaChallenge::CaptchaChallenge(const Message &m) : d(new CaptchaChallengePri
d->dt = QDateTime::currentDateTime();
}

if (m.getForm().registrarType() != "urn:xmpp:captcha" || m.getForm().type() != XData::Data_Form)
if (m.getForm().registrarType() != QLatin1String("urn:xmpp:captcha") || m.getForm().type() != XData::Data_Form
|| m.id().isEmpty())
return;

if (m.id().isEmpty() || m.getForm().getField("challenge").value().value(0) != m.id())
auto fieldOpt = m.getForm().findField("challenge");
if (!fieldOpt || fieldOpt->value().value(0) != m.id())
return;

if (m.getForm().getField("from").value().value(0).isEmpty())
if (fieldOpt = m.getForm().findField("from"); !fieldOpt || fieldOpt->value().value(0).isEmpty())
return;

d->form = m.getForm();
d->explanation = m.body();
d->urls = m.urlList();
d->arbiter = m.from();
d->offendedJid = Jid(m.getForm().getField("from").value().value(0));
d->offendedJid = Jid(fieldOpt->value().value(0));
}

CaptchaChallenge::~CaptchaChallenge() { }
Expand Down
3 changes: 2 additions & 1 deletion src/xmpp/xmpp-im/xmpp_vcard4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1493,7 +1493,8 @@ void VCard::setPhoto(const PAdvUris &photo)
Item<UriValue> &VCard::setPhoto(const UriValue &photo)
{
INIT_D();
d->photo.append({ Parameters(), photo });
PAdvUris l = { { PAdvUri { Parameters(), photo } } };
d->photo = std::move(l);
return d->photo.last();
}

Expand Down
10 changes: 8 additions & 2 deletions src/xmpp/xmpp-im/xmpp_xdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ void XData::setRegistrarType(const QString &registrarType) { d->registrarType =

const XData::FieldList &XData::fields() const { return d->fields; }

XData::Field XData::getField(const QString &var) const
std::optional<XMPP::XData::Field> XMPP::XData::findField(const QString &var) const
{
if (!d->fields.isEmpty()) {
FieldList::ConstIterator it = d->fields.begin();
Expand All @@ -348,7 +348,13 @@ XData::Field XData::getField(const QString &var) const
return f;
}
}
return Field();
return {};
}

XData::Field XData::getField(const QString &var) const
{
auto of = findField(var);
return of ? *of : XData::Field {};
}

XData::Field &XData::fieldRef(const QString &var)
Expand Down
11 changes: 8 additions & 3 deletions src/xmpp/xmpp-im/xmpp_xdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include <QString>
#include <QStringList>

#include <optional>

class QDomDocument;
class QDomElement;

Expand Down Expand Up @@ -152,9 +154,12 @@ class XData {
typedef QList<Field> FieldList;

const FieldList &fields() const;
Field getField(const QString &var) const;
Field &fieldRef(const QString &var);
void setFields(const FieldList &fl);

std::optional<Field> findField(const QString &var) const;
Field getField(const QString &var) const;
Field &fieldRef(const QString &var); // dangerous

void setFields(const FieldList &fl);

private:
class Private : public QSharedData {
Expand Down

0 comments on commit b64ba25

Please sign in to comment.