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 | QList<ProductComponent *> Product::components() const | ||||
70 | { | ||||
71 | return m_components; | ||||
72 | } | ||||
73 | | ||||
74 | void Product::setComponents(const QList<ProductComponent *> &components) | ||||
75 | { | ||||
76 | m_components = components; | ||||
77 | } | ||||
78 | | ||||
79 | Product::~Product() | ||||
80 | { | ||||
81 | qDeleteAll(m_components); | ||||
82 | qDeleteAll(m_versions); | ||||
83 | } | ||||
84 | | ||||
85 | bool Product::isActive() const | ||||
86 | { | ||||
87 | return m_active; | ||||
88 | } | ||||
89 | | ||||
90 | QStringList Product::componentNames() const | ||||
91 | { | ||||
92 | QStringList ret; | ||||
93 | for (const auto *component : m_components) { | ||||
94 | ret << component->name(); | ||||
95 | } | ||||
96 | return ret; | ||||
97 | } | ||||
98 | QStringList Product::allVersions() const | ||||
99 | { | ||||
100 | QStringList ret; | ||||
101 | for (const auto *version : m_versions) { | ||||
102 | ret << version->name(); | ||||
103 | } | ||||
104 | return ret; | ||||
105 | } | ||||
106 | QStringList Product::inactiveVersions() const | ||||
107 | { | ||||
108 | QStringList ret; | ||||
109 | for (const auto *version : m_versions) { | ||||
110 | if (!version->isActive()) { | ||||
111 | ret << version->name(); | ||||
112 | } | ||||
113 | } | ||||
114 | return ret; | ||||
115 | } | ||||
116 | | ||||
117 | void Product::setActive(bool active) | ||||
118 | { | ||||
119 | m_active = active; | ||||
120 | } | ||||
121 | | ||||
122 | ProductVersion::ProductVersion(const QVariantHash &object, QObject *parent) | ||||
123 | : QObject(parent) | ||||
124 | { | ||||
125 | for (auto it = object.constBegin(); it != object.constEnd(); ++it) { | ||||
126 | setProperty(qPrintable(it.key()), it.value()); | ||||
127 | } | ||||
128 | } | ||||
129 | | ||||
130 | ProductComponent::ProductComponent(const QVariantHash &object, QObject *parent) | ||||
131 | : QObject(parent) | ||||
132 | { | ||||
133 | for (auto it = object.constBegin(); it != object.constEnd(); ++it) { | ||||
134 | setProperty(qPrintable(it.key()), it.value()); | ||||
135 | } | ||||
136 | } | ||||
137 | | ||||
138 | ProductClient::ProductClient(const Connection &connection) | ||||
139 | : m_connection(connection) | ||||
140 | { | ||||
141 | } | ||||
142 | | ||||
143 | Product::Ptr ProductClient::get(KJob *kjob) | ||||
144 | { | ||||
145 | auto job = qobject_cast<APIJob *>(kjob); | ||||
146 | Q_ASSERT(job->error() == KJob::NoError); | ||||
147 | | ||||
148 | auto document = job->document(); | ||||
149 | const QJsonArray productsArray = document.object().value(QLatin1String("products")).toArray(); | ||||
150 | Q_ASSERT(productsArray.size() == 1); | ||||
151 | | ||||
152 | auto obj = productsArray.at(0).toObject().toVariantHash(); | ||||
153 | | ||||
154 | return Product::Ptr(new Product(obj, m_connection)); | ||||
155 | } | ||||
156 | | ||||
157 | KJob *ProductClient::get(const QString &idOrName) | ||||
158 | { | ||||
159 | return m_connection.get(QStringLiteral("/product/%1").arg(idOrName)); | ||||
160 | } | ||||
161 | | ||||
162 | } // namespace Bugzilla |