File indexing completed on 2025-01-19 04:58:03

0001 /*
0002     SPDX-FileCopyrightText: 2019 Harald Sitter <sitter@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "querycommand.h"
0008 #include <QMetaProperty>
0009 #include <QVariant>
0010 
0011 namespace Bugzilla
0012 {
0013 Query QueryCommand::toQuery() const
0014 {
0015     Query query;
0016     return expandQuery(query, QSet<QString>());
0017 }
0018 
0019 Query QueryCommand::expandQuery(Query &query, const QSet<QString> &seen) const
0020 {
0021     const auto propertyCount = metaObject()->propertyCount();
0022     for (int i = 0; i < propertyCount; ++i) {
0023         const auto property = metaObject()->property(i);
0024         const auto name = QString::fromLatin1(property.name());
0025         const auto value = property.read(this);
0026 
0027         if (query.hasQueryItem(name) || seen.contains(name) || name == QLatin1String("objectName")) {
0028             // The element was manually set or builtin property.
0029             continue;
0030         }
0031 
0032         if (value.toLongLong() < 0) {
0033             // Invalid value => member was not set!
0034             // This does generally also work for all integers, ulonglong of
0035             // course being the only one that can cause trouble.
0036             continue;
0037         }
0038 
0039         // Lists must be serialized manually. They could have a number of representations.
0040         Q_ASSERT_X(value.type() != QVariant::StringList, Q_FUNC_INFO, qPrintable(QStringLiteral("Trying to auto serialize string list %1").arg(name)));
0041 
0042         // Either can't serialize or not set.
0043         if (value.toString().isEmpty()) {
0044             continue;
0045         }
0046 
0047         query.addQueryItem(name, property.read(this).toString());
0048     }
0049 
0050     return query;
0051 }
0052 
0053 } // namespace Bugzilla