Changeset View
Changeset View
Standalone View
Standalone View
src/bugzillaintegration/libbugzilla/models/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 <QDebug> | ||||
24 | | ||||
25 | #include <QMetaObject> | ||||
26 | #include <QMetaMethod> | ||||
27 | #include <QMetaType> | ||||
28 | | ||||
29 | namespace Bugzilla { | ||||
30 | | ||||
31 | Product::Product(const QVariantHash &object, const Connection &connection, QObject *parent) | ||||
32 | : QObject(parent) | ||||
33 | , m_connection(connection) | ||||
34 | { | ||||
35 | registerVariantConverters(); | ||||
36 | | ||||
37 | for (auto it = object.constBegin(); it != object.constEnd(); ++it) { | ||||
38 | setProperty(qPrintable(it.key()), it.value()); | ||||
39 | } | ||||
40 | } | ||||
41 | | ||||
42 | QList<ProductVersion *> Product::versions() const | ||||
43 | { | ||||
44 | return m_versions; | ||||
45 | } | ||||
46 | | ||||
47 | void Product::setVersions(const QList<ProductVersion *> &versions) | ||||
48 | { | ||||
49 | m_versions = versions; | ||||
50 | } | ||||
51 | | ||||
52 | QList<ProductComponent *> Product::components() const | ||||
53 | { | ||||
54 | return m_components; | ||||
55 | } | ||||
56 | | ||||
57 | void Product::setComponents(const QList<ProductComponent *> &components) | ||||
58 | { | ||||
59 | m_components = components; | ||||
60 | } | ||||
61 | | ||||
62 | Product::~Product() | ||||
63 | { | ||||
64 | qDeleteAll(m_components); | ||||
65 | qDeleteAll(m_versions); | ||||
66 | } | ||||
67 | | ||||
68 | bool Product::isActive() const | ||||
69 | { | ||||
70 | return m_active; | ||||
71 | } | ||||
72 | | ||||
73 | QStringList Product::componentNames() const | ||||
74 | { | ||||
75 | QStringList ret; | ||||
76 | for (const auto *component : m_components) { | ||||
77 | ret << component->name(); | ||||
78 | } | ||||
79 | return ret; | ||||
80 | } | ||||
81 | | ||||
82 | QStringList Product::allVersions() const | ||||
83 | { | ||||
84 | QStringList ret; | ||||
85 | for (const auto *version : m_versions) { | ||||
86 | ret << version->name(); | ||||
87 | } | ||||
88 | return ret; | ||||
89 | } | ||||
90 | | ||||
91 | QStringList Product::inactiveVersions() const | ||||
92 | { | ||||
93 | QStringList ret; | ||||
94 | for (const auto *version : m_versions) { | ||||
95 | if (!version->isActive()) { | ||||
96 | ret << version->name(); | ||||
97 | } | ||||
98 | } | ||||
99 | return ret; | ||||
100 | } | ||||
101 | | ||||
102 | void Product::registerVariantConverters() | ||||
103 | { | ||||
104 | // The way List QVariant get converted to QList<T> is a bit meh. | ||||
105 | // A List variant by default only can convert to a QStringList, regardless | ||||
106 | // of the T itself being a metatype known to QVariant. i.e. the QVariant | ||||
107 | // may know how to iterate a QList, and it may know how to convert T, but | ||||
108 | // it doesn't know how to put the two together into a list conversion. | ||||
109 | // This is true for all Ts. You can have a QList<int>, QVariant::fromValue | ||||
110 | // that into a QVariant{QVariantList} but that variant will no longer | ||||
111 | // convert<QList<int>>. | ||||
112 | // To bridge the conversion gap we need to register custom converters which | ||||
113 | // iterate the variant list and turn it into the relevant type. | ||||
114 | | ||||
115 | static bool convertersRegistered = false; | ||||
116 | if (convertersRegistered) { | ||||
117 | return; | ||||
118 | } | ||||
119 | convertersRegistered = true; | ||||
120 | | ||||
121 | QMetaType::registerConverter<QVariantList, QList<ProductComponent *>>( | ||||
122 | [](QVariantList v) -> QList<ProductComponent *> | ||||
123 | { | ||||
124 | QList<ProductComponent *> list; | ||||
125 | list.reserve(v.size()); | ||||
126 | for (const QVariant &variant : qAsConst(v)) { | ||||
127 | list.append(new ProductComponent(variant.toHash())); | ||||
128 | } | ||||
129 | return list; | ||||
130 | }); | ||||
131 | | ||||
132 | QMetaType::registerConverter<QVariantList, QList<ProductVersion *>>( | ||||
133 | [](QVariantList v) -> QList<ProductVersion *> | ||||
134 | { | ||||
135 | QList<ProductVersion *> list; | ||||
136 | list.reserve(v.size()); | ||||
137 | for (const QVariant &variant : qAsConst(v)) { | ||||
138 | list.append(new ProductVersion(variant.toHash())); | ||||
139 | } | ||||
140 | return list; | ||||
141 | }); | ||||
142 | } | ||||
143 | | ||||
144 | void Product::setActive(bool active) | ||||
145 | { | ||||
146 | m_active = active; | ||||
147 | } | ||||
148 | | ||||
149 | ProductVersion::ProductVersion(const QVariantHash &object, QObject *parent) | ||||
150 | : QObject(parent) | ||||
151 | { | ||||
152 | for (auto it = object.constBegin(); it != object.constEnd(); ++it) { | ||||
153 | setProperty(qPrintable(it.key()), it.value()); | ||||
154 | } | ||||
155 | } | ||||
156 | | ||||
157 | ProductComponent::ProductComponent(const QVariantHash &object, QObject *parent) | ||||
158 | : QObject(parent) | ||||
159 | { | ||||
160 | for (auto it = object.constBegin(); it != object.constEnd(); ++it) { | ||||
161 | setProperty(qPrintable(it.key()), it.value()); | ||||
162 | } | ||||
163 | } | ||||
164 | | ||||
165 | } // namespace Bugzilla |