Skip to content

Commit

Permalink
Small code enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
pasnox committed Apr 24, 2019
1 parent be8ba7f commit 74961f9
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 61 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ object_script.*
/bin
/lib
/plugins
*.user*
28 changes: 14 additions & 14 deletions src/xmpp/xmpp-im/xmpp_carbons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ JT_MessageCarbons::JT_MessageCarbons(Task *parent)

void JT_MessageCarbons::enable()
{
iq = createIQ(doc(), QLatin1String("set"), QString(), id());
QDomElement enable = doc()->createElement(QLatin1String("enable"));
enable.setAttribute(QLatin1String("xmlns"), xmlns_carbons);
iq = createIQ(doc(), QString::fromLatin1("set"), QString(), id());
QDomElement enable = doc()->createElement(QString::fromLatin1("enable"));
enable.setAttribute(QString::fromLatin1("xmlns"), xmlns_carbons);
iq.appendChild(enable);
}

void JT_MessageCarbons::disable()
{
iq = createIQ(doc(), QLatin1String("set"), QString(), id());
QDomElement disable = doc()->createElement(QLatin1String("disable"));
disable.setAttribute(QLatin1String("xmlns"), xmlns_carbons);
iq = createIQ(doc(), QString::fromLatin1("set"), QString(), id());
QDomElement disable = doc()->createElement(QString::fromLatin1("disable"));
disable.setAttribute(QString::fromLatin1("xmlns"), xmlns_carbons);
iq.appendChild(disable);
}

Expand All @@ -93,7 +93,7 @@ void JT_MessageCarbons::onGo()
bool JT_MessageCarbons::take(const QDomElement &e)
{
if (iqVerify(e, Jid(), id())) {
if (e.attribute(QLatin1String("type")) != QLatin1String("result"))
if (e.attribute(QString::fromLatin1("type")) != QString::fromLatin1("result"))
setError(e);
else
setSuccess();
Expand All @@ -111,8 +111,8 @@ bool CarbonsSubscriber::xmlEvent(const QDomElement &root, QDomElement &e, Client
bool drop = false;
frw.setType(Forwarding::ForwardedNone);
if (!nested) {
Jid from(root.attribute(QLatin1String("from")));
Jid to(root.attribute(QLatin1String("to")));
Jid from(root.attribute(QStringLiteral("from")));
Jid to(root.attribute(QStringLiteral("to")));
if (from.resource().isEmpty() && from.compare(to, false)) {
QDomElement child = e.firstChildElement();
while (!child.isNull()) {
Expand Down Expand Up @@ -152,14 +152,14 @@ class CarbonsManager::Private {
}

void subscribe() {
push_m->subscribeXml(sbs.get(), QLatin1String("received"), xmlns_carbons, Forwarding::ForwardedCarbonsReceived);
push_m->subscribeXml(sbs.get(), QLatin1String("sent"), xmlns_carbons, Forwarding::ForwardedCarbonsSent);
push_m->subscribeXml(sbs.get(), QString::fromLatin1("received"), xmlns_carbons, Forwarding::ForwardedCarbonsReceived);
push_m->subscribeXml(sbs.get(), QString::fromLatin1("sent"), xmlns_carbons, Forwarding::ForwardedCarbonsSent);
push_m->subscribeMessage(sbs.get(), 0);
}

void unsubscribe() {
push_m->unsubscribeXml(sbs.get(), QLatin1String("received"), xmlns_carbons);
push_m->unsubscribeXml(sbs.get(), QLatin1String("sent"), xmlns_carbons);
push_m->unsubscribeXml(sbs.get(), QString::fromLatin1("received"), xmlns_carbons);
push_m->unsubscribeXml(sbs.get(), QString::fromLatin1("sent"), xmlns_carbons);
push_m->unsubscribeMessage(sbs.get());
}

Expand All @@ -182,7 +182,7 @@ CarbonsManager::~CarbonsManager()

QDomElement CarbonsManager::privateElement(QDomDocument &doc)
{
return doc.createElementNS(xmlns_carbons, QLatin1String("private"));
return doc.createElementNS(xmlns_carbons, QString::fromLatin1("private"));
}

void CarbonsManager::setEnabled(bool enable)
Expand Down
22 changes: 11 additions & 11 deletions src/xmpp/xmpp-im/xmpp_forwarding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ void Forwarding::setMessage(const Message &msg)

bool Forwarding::fromXml(const QDomElement &e, Client *client)
{
if (e.tagName() != QLatin1String("forwarded") || e.attribute(QLatin1String("xmlns")) != xmlns_forward)
if (e.tagName() != QString::fromLatin1("forwarded") || e.attribute(QString::fromLatin1("xmlns")) != xmlns_forward)
return false;

bool correct = false;
type_ = Forwarding::ForwardedNone;
QDomElement child = e.firstChildElement();
while (!child.isNull()) {
if (child.tagName() == QLatin1String("message")) {
if (child.tagName() == QString::fromLatin1("message")) {
if (client->pushMessage()->processXmlSubscribers(child, client, true))
break;
Stanza s = client->stream().createStanza(addCorrectNS(child));
Expand All @@ -126,8 +126,8 @@ bool Forwarding::fromXml(const QDomElement &e, Client *client)
correct = true;
}
}
else if (child.tagName() == QLatin1String("delay") && child.attribute(QLatin1String("xmlns")) == xmlns_delay) {
ts_ = QDateTime::fromString(child.attribute(QLatin1String("stamp")).left(19), Qt::ISODate);
else if (child.tagName() == QString::fromLatin1("delay") && child.attribute(QString::fromLatin1("xmlns")) == xmlns_delay) {
ts_ = QDateTime::fromString(child.attribute(QString::fromLatin1("stamp")).left(19), Qt::ISODate);
}
child = child.nextSiblingElement();
}
Expand All @@ -139,12 +139,12 @@ QDomElement Forwarding::toXml(Stream *stream) const
if (type_ == ForwardedNone || !msg_)
return QDomElement();

QDomElement e = stream->doc().createElement(QLatin1String("forwarded"));
e.setAttribute(QLatin1String("xmlns"), xmlns_forward);
QDomElement e = stream->doc().createElement(QString::fromLatin1("forwarded"));
e.setAttribute(QString::fromLatin1("xmlns"), xmlns_forward);
if (ts_.isValid()) {
QDomElement delay = stream->doc().createElement(QLatin1String("delay"));
delay.setAttribute(QLatin1String("xmlns"), xmlns_delay);
delay.setAttribute(QLatin1String("stamp"), ts_.toUTC().toString(Qt::ISODate) + "Z");
QDomElement delay = stream->doc().createElement(QString::fromLatin1("delay"));
delay.setAttribute(QString::fromLatin1("xmlns"), xmlns_delay);
delay.setAttribute(QString::fromLatin1("stamp"), ts_.toUTC().toString(Qt::ISODate) + "Z");
e.appendChild(delay);
}
e.appendChild(msg_.toStanza(stream).element());
Expand Down Expand Up @@ -219,11 +219,11 @@ void ForwardingManager::setEnabled(bool enabled)

if (enabled) {
d->sbs.reset(new ForwardingSubscriber());
d->push_m->subscribeXml(d->sbs.get(), QLatin1String("forwarded"), xmlns_forward, 0);
d->push_m->subscribeXml(d->sbs.get(), QString::fromLatin1("forwarded"), xmlns_forward, 0);
d->push_m->subscribeMessage(d->sbs.get(), 0);
}
else {
d->push_m->unsubscribeXml(d->sbs.get(), QLatin1String("forwarded"), xmlns_forward);
d->push_m->unsubscribeXml(d->sbs.get(), QString::fromLatin1("forwarded"), xmlns_forward);
d->push_m->unsubscribeMessage(d->sbs.get());
d->sbs.reset();
}
Expand Down
63 changes: 27 additions & 36 deletions src/xmpp/xmpp-im/xmpp_tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

using namespace XMPP;

#define GET_SUBSCRIBER_ITERATOR(list, sbs) std::find_if(list.begin(), list.end(), [sbs](const Private::SubsData &value) { return value.sbs == sbs; })

static QString lineEncode(QString str)
{
Expand Down Expand Up @@ -896,26 +897,27 @@ class JT_PushMessage::Private {
Subscriber *sbs = nullptr;
int userData = -1;
};
using SubsDataList = QVector<SubsData>;
EncryptionHandler *m_encryptionHandler;
QHash<QString, QList<SubsData> > subsData;
QList<SubsData> subsMData;
QHash<QString, SubsDataList> subsData;
SubsDataList subsMData;

QString genKey(const QString &s1, const QString &s2)
{
return QString(QLatin1String("%1&%2")).arg(s1, s2);
return QString::fromLatin1("%1&%2").arg(s1, s2);
}

bool processChildStanzaNode(const QDomElement &root, QDomElement &e, Client *c, bool nested)
{
QString tagName = e.tagName();
QString xmlnsStr = e.attribute(QLatin1String("xmlns"));
QString xmlnsStr = e.attribute(QString::fromLatin1("xmlns"));
QString key = genKey(tagName, xmlnsStr);
auto it = subsData.constFind(key);
if (it != subsData.constEnd()) {
foreach (const SubsData &sd, it.value()) {
if (sd.sbs->xmlEvent(root, e, c, sd.userData, nested))
return true;
if (e.tagName() != tagName || e.attribute(QLatin1String("xmlns")) != tagName)
if (e.tagName() != tagName || e.attribute(QString::fromLatin1("xmlns")) != tagName)
return false;
}
}
Expand Down Expand Up @@ -968,56 +970,45 @@ void JT_PushMessage::subscribeXml(Subscriber *sbs, const QString &tagName, const
{
QString key = d->genKey(tagName, xmlnsStr);
auto it = d->subsData.find(key);
if (it == d->subsData.constEnd()) {
if (it != d->subsData.end()) {
Private::SubsDataList &list = it.value();
auto lit = GET_SUBSCRIBER_ITERATOR(list, sbs);
if (lit == list.end())
list.append({ sbs, userData });
} else {
d->subsData.insert(key, {{ sbs, userData }});
return;
}

foreach (const Private::SubsData &sd, it.value()) {
if (sd.sbs == sbs)
return;
}

it.value().append({ sbs, userData });
}

void JT_PushMessage::unsubscribeXml(Subscriber *sbs, const QString &tagName, const QString &xmlnsStr)
{
QString key = d->genKey(tagName, xmlnsStr);
auto it = d->subsData.find(key);
if (it != d->subsData.end()) {
QList<Private::SubsData> &list = it.value();
int cnt = list.count();
for (int i = 0; i < cnt; ++i) {
if (list.at(i).sbs == sbs) {
if (cnt == 1)
d->subsData.erase(it);
else
list.removeAt(i);
break;
}
Private::SubsDataList &list = it.value();
auto lit = GET_SUBSCRIBER_ITERATOR(list, sbs);
if (lit != list.end()) {
list.erase(lit);
if (list.isEmpty())
d->subsData.erase(it);
}
}
}

void JT_PushMessage::subscribeMessage(Subscriber *sbs, int userData)
{
foreach (const Private::SubsData &sd, d->subsMData) {
if (sd.sbs == sbs)
return;
}
d->subsMData.append({ sbs, userData });
auto &list = d->subsMData;
auto lit = GET_SUBSCRIBER_ITERATOR(list, sbs);
if (lit == list.end())
list.append({ sbs, userData });
}

void JT_PushMessage::unsubscribeMessage(Subscriber *sbs)
{
int sz = d->subsMData.size();
for (int i = 0; i < sz; ++i) {
if (d->subsMData.at(i).sbs == sbs) {
d->subsMData.removeAt(i);
break;
}
}
auto &list = d->subsMData;
auto lit = GET_SUBSCRIBER_ITERATOR(list, sbs);
if (lit != list.end())
list.erase(lit);
}

bool JT_PushMessage::processXmlSubscribers(QDomElement &el, Client *client, bool nested)
Expand Down

0 comments on commit 74961f9

Please sign in to comment.