Changeset View
Changeset View
Standalone View
Standalone View
src/bugzillaintegration/libbugzilla/product.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright 2019 Harald Sitter <sitter@kde.org> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Lesser General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | later version accepted by the membership of KDE e.V. (or its | ||||
9 | successor approved by the membership of KDE e.V.), which shall | ||||
10 | act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | | ||||
12 | This library is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | Lesser General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU Lesser General Public | ||||
18 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "product.h" | ||||
22 | | ||||
23 | #include <QMetaObject> | ||||
24 | #include <QMetaMethod> | ||||
25 | #include <QMetaType> | ||||
26 | | ||||
27 | namespace Bugzilla { | ||||
28 | | ||||
29 | Product::Product(const QVariantHash &object, const Connection &connection, QObject *parent) | ||||
30 | : QObject(parent) | ||||
31 | , m_connection(connection) | ||||
32 | { | ||||
33 | #warning there may be a better way but I really cannot find it | ||||
34 | QMetaType::registerConverter<QVariantList, QList<ProductComponent *>>([](QVariantList v) -> QList<ProductComponent *> | ||||
35 | { | ||||
36 | QList<ProductComponent *> list; | ||||
37 | list.reserve(v.size()); | ||||
38 | for (const QVariant &variant : v) { | ||||
39 | list.append(new ProductComponent(variant.toHash())); | ||||
40 | } | ||||
41 | return list; | ||||
42 | }); | ||||
43 | | ||||
44 | QMetaType::registerConverter<QVariantList, QList<ProductVersion *>>([](QVariantList v) -> QList<ProductVersion *> | ||||
45 | { | ||||
46 | QList<ProductVersion *> list; | ||||
47 | list.reserve(v.size()); | ||||
48 | for (const QVariant &variant : v) { | ||||
49 | list.append(new ProductVersion(variant.toHash())); | ||||
50 | } | ||||
51 | return list; | ||||
52 | }); | ||||
53 | | ||||
54 | for (auto it = object.constBegin(); it != object.constEnd(); ++it) { | ||||
55 | setProperty(qPrintable(it.key()), it.value()); | ||||
56 | } | ||||
57 | } | ||||
58 | | ||||
59 | QList<ProductVersion *> Product::versions() const | ||||
60 | { | ||||
61 | return m_versions; | ||||
62 | } | ||||
63 | | ||||
64 | void Product::setVersions(const QList<ProductVersion *> &versions) | ||||
65 | { | ||||
66 | m_versions = versions; | ||||
67 | } | ||||
68 | | ||||
69 | #warning fixme get prefix to allow for compat helper components | ||||
70 | QList<ProductComponent *> Product::getComponents() const | ||||
71 | { | ||||
72 | return m_components; | ||||
73 | } | ||||
74 | | ||||
75 | void Product::setComponents(const QList<ProductComponent *> &components) | ||||
76 | { | ||||
77 | m_components = components; | ||||
78 | } | ||||
79 | | ||||
80 | Product::~Product() | ||||
81 | { | ||||
82 | qDeleteAll(m_components); | ||||
83 | qDeleteAll(m_versions); | ||||
84 | } | ||||
85 | | ||||
86 | bool Product::isActive() const | ||||
87 | { | ||||
88 | return m_active; | ||||
89 | } | ||||
90 | | ||||
91 | #warning COMPAT COMPAT CIMPAT | ||||
92 | QStringList Product::components() const | ||||
93 | { | ||||
94 | QStringList ret; | ||||
95 | for (const auto *component : m_components) { | ||||
96 | ret << component->name(); | ||||
97 | } | ||||
98 | return ret; | ||||
99 | } | ||||
100 | #warning COMPAT COMPAT CIMPAT | ||||
101 | QStringList Product::allVersions() const | ||||
102 | { | ||||
103 | QStringList ret; | ||||
104 | for (const auto *version : m_versions) { | ||||
105 | ret << version->name(); | ||||
106 | } | ||||
107 | return ret; | ||||
108 | } | ||||
109 | #warning COMPAT COMPAT CIMPAT | ||||
110 | QStringList Product::inactiveVersions() const | ||||
111 | { | ||||
112 | QStringList ret; | ||||
113 | for (const auto *version : m_versions) { | ||||
114 | if (!version->isActive()) { | ||||
115 | ret << version->name(); | ||||
116 | } | ||||
117 | } | ||||
118 | return ret; | ||||
119 | } | ||||
120 | | ||||
121 | void Product::setActive(bool active) | ||||
122 | { | ||||
123 | m_active = active; | ||||
124 | } | ||||
125 | | ||||
126 | ProductVersion::ProductVersion(const QVariantHash &object, QObject *parent) | ||||
127 | : QObject(parent) | ||||
128 | { | ||||
129 | for (auto it = object.constBegin(); it != object.constEnd(); ++it) { | ||||
130 | setProperty(qPrintable(it.key()), it.value()); | ||||
131 | } | ||||
132 | } | ||||
133 | | ||||
134 | ProductComponent::ProductComponent(const QVariantHash &object, QObject *parent) | ||||
135 | : QObject(parent) | ||||
136 | { | ||||
137 | for (auto it = object.constBegin(); it != object.constEnd(); ++it) { | ||||
138 | setProperty(qPrintable(it.key()), it.value()); | ||||
139 | } | ||||
140 | } | ||||
141 | | ||||
142 | ProductClient::ProductClient(const Connection &connection) | ||||
143 | : m_connection(connection) | ||||
144 | { | ||||
145 | } | ||||
146 | | ||||
147 | Product::Ptr ProductClient::get(KJob *kjob) | ||||
148 | { | ||||
149 | auto job = qobject_cast<APIJob *>(kjob); | ||||
150 | Q_ASSERT(job->error() == KJob::NoError); | ||||
151 | | ||||
152 | auto document = job->document(); | ||||
153 | const QJsonArray productsArray = document.object().value(QLatin1String("products")).toArray(); | ||||
154 | Q_ASSERT(productsArray.size() == 1); | ||||
155 | | ||||
156 | auto obj = productsArray.at(0).toObject().toVariantHash(); | ||||
157 | | ||||
158 | return Product::Ptr(new Product(obj, m_connection)); | ||||
159 | } | ||||
160 | | ||||
161 | KJob *ProductClient::get(const QString &idOrName) | ||||
162 | { | ||||
163 | return m_connection.get(QStringLiteral("/product/%1").arg(idOrName)); | ||||
164 | } | ||||
165 | | ||||
166 | } // namespace Bugzilla |