Commit 13091abc authored by Laurent Montel's avatar Laurent Montel 😁

GIT_SILENT: coding style

parent 09e872c1
......@@ -61,8 +61,9 @@ private Q_SLOTS:
const auto doc = QJsonDocument::fromJson(ref.readAll());
QVERIFY(doc.isArray());
if (extractor.data() != doc.array())
if (extractor.data() != doc.array()) {
qDebug().noquote() << QJsonDocument(extractor.data()).toJson();
}
QCOMPARE(extractor.data(), doc.array());
}
};
......
......@@ -19,7 +19,6 @@
#include "pkpassboardingpass.h"
PkPassBoardingPass::PkPassBoardingPass(QObject *parent)
: PkPassFile(QStringLiteral("boardingPass"), parent)
{
......
......@@ -97,8 +97,9 @@ QImage PkPassFile::logo() const
QImage PkPassFile::barcode() const
{
auto barcodeData = data().value(QLatin1String("barcodes")).toArray().at(0).toObject();
if (barcodeData.isEmpty())
if (barcodeData.isEmpty()) {
barcodeData = data().value(QLatin1String("barcode")).toObject();
}
const auto formatName = barcodeData.value(QLatin1String("format")).toString();
const auto msg = barcodeData.value(QLatin1String("message")).toString();
// TODO: consider messageEncoding, once Prison supports that
......@@ -111,8 +112,7 @@ QImage PkPassFile::barcode() const
code.reset(Prison::createBarcode(Prison::QRCode));
} else if (formatName == QLatin1String("PKBarcodeFormatPDF417")) {
// TODO
}
else if (formatName == QLatin1String("PKBarcodeFormatAztec")) {
} else if (formatName == QLatin1String("PKBarcodeFormatAztec")) {
code.reset(Prison::createBarcode(Prison::Aztec));
} else if (formatName == QLatin1String("PKBarcodeFormatCode128")) {
// TODO
......@@ -131,8 +131,9 @@ QImage PkPassFile::barcode() const
QString PkPassFile::barcodeAltText() const
{
auto barcodeData = data().value(QLatin1String("barcodes")).toArray().at(0).toObject();
if (barcodeData.isEmpty())
if (barcodeData.isEmpty()) {
barcodeData = data().value(QLatin1String("barcode")).toObject();
}
return barcodeData.value(QLatin1String("altText")).toString();
}
......
......@@ -35,8 +35,9 @@ bool Airline::operator!=(const Airline &other) const
static QString localizedDateTime(const QDateTime &dt)
{
auto s = QLocale().toString(dt, QLocale::ShortFormat);
if (dt.timeSpec() == Qt::TimeZone || dt.timeSpec() == Qt::OffsetFromUTC)
if (dt.timeSpec() == Qt::TimeZone || dt.timeSpec() == Qt::OffsetFromUTC) {
s += QLatin1Char(' ') + dt.timeZone().abbreviation(dt);
}
return s;
}
......
......@@ -30,42 +30,44 @@
static QVariant createInstance(const QJsonObject &obj);
// Eurowings workarounds...
static const char* fallbackDateTimePattern[] = {
static const char *fallbackDateTimePattern[] = {
"yyyy-MM-dd HH:mm:ss",
"yyyy-MM-dd HH:mm",
"MM-dd-yyyy HH:mm" // yes, seriously ;(
};
static const auto fallbackDateTimePatternCount = sizeof(fallbackDateTimePattern) / sizeof(const char*);
static const auto fallbackDateTimePatternCount = sizeof(fallbackDateTimePattern) / sizeof(const char *);
static QVariant propertyValue(const QMetaProperty &prop, const QJsonValue &v)
{
switch (prop.type()) {
case QVariant::String:
return v.toString();
case QVariant::DateTime:
{
auto str = v.toString();
auto dt = QDateTime::fromString(str, Qt::ISODate);
for (unsigned int i = 0; i < fallbackDateTimePatternCount && dt.isNull(); ++i) {
dt = QDateTime::fromString(str, QString::fromLatin1(fallbackDateTimePattern[i]));
}
if (dt.isNull())
qCDebug(SEMANTIC_LOG) << "Datetime parsing failed for" << str;
return dt;
case QVariant::String:
return v.toString();
case QVariant::DateTime:
{
auto str = v.toString();
auto dt = QDateTime::fromString(str, Qt::ISODate);
for (unsigned int i = 0; i < fallbackDateTimePatternCount && dt.isNull(); ++i) {
dt = QDateTime::fromString(str, QString::fromLatin1(fallbackDateTimePattern[i]));
}
default:
break;
if (dt.isNull()) {
qCDebug(SEMANTIC_LOG) << "Datetime parsing failed for" << str;
}
return dt;
}
default:
break;
}
return createInstance(v.toObject());
}
template <typename T>
template<typename T>
static QVariant createInstance(const QJsonObject &obj)
{
T t;
for (auto it = obj.begin(); it != obj.end(); ++it) {
if (it.key().startsWith(QLatin1Char('@')))
if (it.key().startsWith(QLatin1Char('@'))) {
continue;
}
const auto idx = T::staticMetaObject.indexOfProperty(it.key().toLatin1());
if (idx < 0) {
qCDebug(SEMANTIC_LOG) << "property" << it.key() << "could not be set on object of type" << T::staticMetaObject.className();
......@@ -103,7 +105,9 @@ QVector<QVariant> JsonLdDocument::fromJson(const QJsonArray &array)
l.reserve(array.size());
for (const auto &obj : array) {
const auto v = createInstance(obj.toObject());
if (!v.isNull())
if (!v.isNull()) {
l.push_back(v);
} return l;
}
}
return l;
}
......@@ -26,8 +26,7 @@
class QJsonArray;
/** Serialization/deserialization code for JSON-LD data. */
namespace JsonLdDocument
{
namespace JsonLdDocument {
QVector<QVariant> fromJson(const QJsonArray &array);
}
......
......@@ -36,22 +36,25 @@ public:
const MimeTreeParser::Interface::BodyPartFormatter *bodyPartFormatter(int idx) const override
{
if (idx == 0)
if (idx == 0) {
return new SemanticProcessor();
}
return nullptr;
}
MessageViewer::MessagePartRendererBase *renderer(int idx) override
{
if (idx == 0)
if (idx == 0) {
return new SemanticRenderer();
}
return nullptr;
}
const MessageViewer::Interface::BodyPartURLHandler *urlHandler(int idx) const override
{
if (idx == 0)
if (idx == 0) {
return new SemanticUrlHandler();
}
return nullptr;
}
};
......
......@@ -29,11 +29,13 @@
MimeTreeParser::MessagePart::Ptr SemanticProcessor::process(MimeTreeParser::Interface::BodyPart &part) const
{
auto nodeHelper = part.nodeHelper();
if (!nodeHelper)
if (!nodeHelper) {
return {};
auto memento = dynamic_cast<SemanticMemento*>(nodeHelper->bodyPartMemento(part.topLevelContent(), "org.kde.messageviewer.semanticData"));
if (memento)
}
auto memento = dynamic_cast<SemanticMemento *>(nodeHelper->bodyPartMemento(part.topLevelContent(), "org.kde.messageviewer.semanticData"));
if (memento) {
return {};
}
qCDebug(SEMANTIC_LOG) << "-------------------------------------------- BEGIN SEMANTIC PARSING";
StructuredDataExtractor extractor;
......
......@@ -37,8 +37,8 @@
#define GRANTLEE_MAKE_GADGET(Class) \
GRANTLEE_BEGIN_LOOKUP(Class) \
const auto idx = Class::staticMetaObject.indexOfProperty(property.toUtf8().constData()); \
if (idx < 0) \
return {}; \
if (idx < 0) { \
return {};} \
const auto mp = Class::staticMetaObject.property(idx); \
return mp.readOnGadget(&object); \
GRANTLEE_END_LOOKUP
......@@ -66,18 +66,21 @@ bool SemanticRenderer::render(const MimeTreeParser::MessagePartPtr &msgPart, Mim
{
Q_UNUSED(context);
const auto mpList = msgPart.dynamicCast<MimeTreeParser::MessagePartList>();
if (!msgPart->isRoot() || !mpList->hasSubParts())
if (!msgPart->isRoot() || !mpList->hasSubParts()) {
return false;
}
qCDebug(SEMANTIC_LOG) << "========================================= Semantic Rendering";
const auto node = mpList->subParts().at(0)->content();
const auto nodeHelper = msgPart->nodeHelper();
if (!nodeHelper || !node)
if (!nodeHelper || !node) {
return false;
}
auto memento = dynamic_cast<SemanticMemento*>(nodeHelper->bodyPartMemento(node->topLevel(), "org.kde.messageviewer.semanticData"));
if (!memento || memento->isEmpty())
auto memento = dynamic_cast<SemanticMemento *>(nodeHelper->bodyPartMemento(node->topLevel(), "org.kde.messageviewer.semanticData"));
if (!memento || memento->isEmpty()) {
return false;
}
auto c = MessageViewer::MessagePartRendererManager::self()->createContext();
c.insert(QStringLiteral("data"), QVariant::fromValue(memento->data()));
......
......@@ -41,7 +41,7 @@ public:
QString statusBarMessage(MimeTreeParser::Interface::BodyPart *part, const QString &path) const override;
private:
SemanticMemento* memento(MimeTreeParser::Interface::BodyPart *part) const;
SemanticMemento *memento(MimeTreeParser::Interface::BodyPart *part) const;
QDate dateForReservation(SemanticMemento *memento) const;
void showCalendar(const QDate &date) const;
......
......@@ -48,56 +48,64 @@ void StructuredDataExtractor::parseXml(const QString &text)
if (reader.name() == QLatin1String("script") && reader.attributes().value(QLatin1String("type")) == QLatin1String("application/ld+json")) {
const auto jsonData = reader.readElementText(QXmlStreamReader::IncludeChildElements);
const auto jsonDoc = QJsonDocument::fromJson(jsonData.toUtf8());
if (jsonDoc.isNull())
if (jsonDoc.isNull()) {
continue;
if (jsonDoc.isArray())
}
if (jsonDoc.isArray()) {
m_data.append(jsonDoc.array());
else if (jsonDoc.isObject())
} else if (jsonDoc.isObject()) {
m_data.push_back(jsonDoc.object());
}
}
// Microdata
const auto itemType = reader.attributes().value(QLatin1String("itemtype")).toString();
if (itemType.startsWith(QLatin1String("http://schema.org/"))) {
auto obj = parseMicroData(reader);
if (obj.isEmpty())
if (obj.isEmpty()) {
continue;
}
obj.insert(QStringLiteral("@context"), QStringLiteral("http://schema.org"));
const QUrl typeUrl(itemType);
obj.insert(QStringLiteral("@type"), typeUrl.fileName());
m_data.push_back(obj);
continue;
}
}
reader.readNext();
}
if (reader.hasError())
if (reader.hasError()) {
qCDebug(SEMANTIC_LOG) << reader.errorString() << reader.lineNumber() << reader.columnNumber();
}
}
void StructuredDataExtractor::findLdJson(const QString &text)
{
for (int i = 0; i < text.size();) {
i = text.indexOf(QLatin1String("<script"), i, Qt::CaseInsensitive);
if (i < 0)
if (i < 0) {
break;
}
i = text.indexOf(QLatin1String("type=\"application/ld+json\""), i, Qt::CaseInsensitive);
if (i < 0)
if (i < 0) {
break;
}
auto begin = text.indexOf(QLatin1Char('>'), i) + 1;
if (i < 0)
if (i < 0) {
break;
}
i = text.indexOf(QLatin1String("</script>"), begin, Qt::CaseInsensitive);
const auto jsonData = text.mid(begin, i - begin);
auto jsonDoc = QJsonDocument::fromJson(jsonData.toUtf8());
if (jsonDoc.isNull())
if (jsonDoc.isNull()) {
continue;
if (jsonDoc.isArray())
}
if (jsonDoc.isArray()) {
m_data.append(jsonDoc.array());
else if (jsonDoc.isObject())
} else if (jsonDoc.isObject()) {
m_data.push_back(jsonDoc.object());
}
}
}
......@@ -138,17 +146,18 @@ QJsonObject StructuredDataExtractor::parseMicroData(QXmlStreamReader &reader) co
obj.insert(prop, valueForItemProperty(reader));
continue;
}
} else if (reader.tokenType() == QXmlStreamReader::EndElement) {
--depth;
if (depth == 0)
if (depth == 0) {
return obj;
}
}
reader.readNext();
}
if (reader.hasError())
if (reader.hasError()) {
qCDebug(SEMANTIC_LOG) << reader.errorString() << reader.lineNumber() << reader.columnNumber();
}
return {};
}
......@@ -156,18 +165,20 @@ QString StructuredDataExtractor::valueForItemProperty(QXmlStreamReader &reader)
{
// TODO see https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/itemprop#Values
const auto elemName = reader.name();
if (elemName == QLatin1String("span"))
if (elemName == QLatin1String("span")) {
return reader.readElementText(QXmlStreamReader::IncludeChildElements);
}
QString v;
if (elemName == QLatin1String("meta"))
if (elemName == QLatin1String("meta")) {
v = reader.attributes().value(QLatin1String("content")).toString();
else if (elemName == QLatin1String("time"))
} else if (elemName == QLatin1String("time")) {
v = reader.attributes().value(QLatin1String("datetime")).toString();
else if (elemName == QLatin1String("link") || elemName == QLatin1String("a"))
} else if (elemName == QLatin1String("link") || elemName == QLatin1String("a")) {
v = reader.attributes().value(QLatin1String("href")).toString();
else
} else {
qCDebug(SEMANTIC_LOG) << "TODO:" << elemName;
}
reader.readNext();
return v;
......
......@@ -33,7 +33,10 @@ class StructuredDataExtractor
{
public:
void parse(const QString &text);
QJsonArray data() const { return m_data; }
QJsonArray data() const
{
return m_data;
}
private:
/** Try to parse using an actual XML parser. */
......
......@@ -371,6 +371,7 @@ QByteArray AdBlockCustomList::readAllFileByteContents(const QString &filename)
return QByteArray();
}
void AdBlockCustomList::saveSubscription()
{
QFile file(filePath());
......
......@@ -196,7 +196,7 @@ void AdBlockSettingWidget::removeRule()
if (KMessageBox::No == KMessageBox::warningYesNo(this,
i18np("Do you want to remove this rule?", "Do you want to remove these rules?", select.count()),
i18n("Remove"))) {
return;
return;
}
for (QListWidgetItem *item : select) {
delete item;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment