1/****************************************************************************
2**
3** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
5**
6** This file is part of the QtDocGallery module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Digia. For licensing terms and
14** conditions see http://qt.digia.com/licensing. For further information
15** use the contact form at http://qt.digia.com/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, Digia gives you certain additional
26** rights. These rights are described in the Digia Qt LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
36**
37**
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qgalleryitemrequest.h"
43#include "qgalleryabstractrequest_p.h"
44
45#include "qgallerynullresultset_p.h"
46#include "qgalleryresource.h"
47
48QT_BEGIN_NAMESPACE_DOCGALLERY
49
50class QGalleryItemRequestPrivate : public QGalleryAbstractRequestPrivate
51{
52 Q_DECLARE_PUBLIC(QGalleryItemRequest)
53public:
54 QGalleryItemRequestPrivate(QAbstractGallery *gallery)
55 : QGalleryAbstractRequestPrivate(gallery, QGalleryAbstractRequest::ItemRequest)
56 , autoUpdate(false)
57 , resultSet(0)
58 , internalResultSet(0)
59 {
60 internalResultSet = &nullResultSet;
61 }
62
63 void _q_itemsInserted(int index, int)
64 {
65 if (index == 0)
66 resultSet->fetch(index: 0);
67 }
68
69 void _q_itemsRemoved(int index, int)
70 {
71 if (index == 0)
72 resultSet->fetch(index: 0);
73 }
74
75 void _q_itemsMoved(int from, int to, int)
76 {
77 if (from == 0 || to == 0)
78 resultSet->fetch(index: 0);
79 }
80
81 void _q_currentItemChanged()
82 {
83 emit q_func()->itemChanged();
84
85 if (!propertyKeys.isEmpty())
86 emit q_func()->metaDataChanged(keys: propertyKeys);
87 }
88
89 void _q_metaDataChanged(int index, int, const QList<int> &keys)
90 {
91 if (index == 0)
92 emit q_func()->metaDataChanged(keys);
93 }
94
95 bool autoUpdate;
96 QGalleryResultSet *resultSet;
97 QGalleryResultSet *internalResultSet;
98 QGalleryNullResultSet nullResultSet;
99 QStringList propertyNames;
100 QVariant itemId;
101 QList<int> propertyKeys;
102};
103
104/*!
105 \class QGalleryItemRequest
106
107 \ingroup gallery
108 \ingroup gallery-requests
109
110 \inmodule QtDocGallery
111
112 \brief The QGalleryItemRequest class provides a request for the properties
113 of a single item from a gallery.
114
115 QGalleryItemRequest executes a query which returns information about the
116 gallery item specified in \l itemId. The query will return an \l itemUrl,
117 an \l itemType, \l resources and \l {metaData()}{meta-data} values for the
118 properties listed in \l propertyNames.
119
120 When the request has finished and if the item could be found the \l valid
121 property will be true, if not it will be false.
122
123 If the \l autoUpdate property is true when the request is executed it will
124 enter an \l Idle state on finishing and will refresh the queried
125 information if the item changes. If the gallery can't provide updates
126 it will instead go immediately to the \l Finished state. Automatic updates
127 can be canceled by calling cancel() on a idle request.
128
129 \sa QDocumentGallery
130*/
131
132/*!
133 Constructs a new gallery item request.
134
135 The \a parent is passed to QObject.
136*/
137
138
139QGalleryItemRequest::QGalleryItemRequest(QObject *parent)
140 : QGalleryAbstractRequest(*new QGalleryItemRequestPrivate(0), parent)
141{
142}
143/*!
144 Contructs a new item request for the given \a gallery.
145
146 The \a parent is passed to QObject.
147*/
148
149QGalleryItemRequest::QGalleryItemRequest(QAbstractGallery *gallery, QObject *parent)
150 : QGalleryAbstractRequest(*new QGalleryItemRequestPrivate(gallery), parent)
151{
152}
153
154/*!
155 Destroys a gallery item request.
156*/
157
158QGalleryItemRequest::~QGalleryItemRequest()
159{
160}
161
162/*!
163 \property QGalleryItemRequest::propertyNames
164
165 \brief A list of names of meta-data properties a request should return values for.
166*/
167
168QStringList QGalleryItemRequest::propertyNames() const
169{
170 return d_func()->propertyNames;
171}
172
173void QGalleryItemRequest::setPropertyNames(const QStringList &names)
174{
175 if (d_func()->propertyNames != names) {
176 d_func()->propertyNames = names;
177
178 emit propertyNamesChanged();
179 }
180}
181
182/*!
183 \fn QGalleryItemRequest::propertyNamesChanged()
184
185 Signals that the value of \l propertyNames has changed.
186*/
187
188/*!
189 \property QGalleryItemRequest::autoUpdate
190
191 \brief Whether a the results of a request should be updated after a request
192 has finished.
193
194 If this is true the request will go into the Idle state when the request has
195 finished rather than returning to Inactive.
196*/
197
198bool QGalleryItemRequest::autoUpdate() const
199{
200 return d_func()->autoUpdate;
201}
202
203void QGalleryItemRequest::setAutoUpdate(bool enabled)
204{
205 if (d_func()->autoUpdate != enabled) {
206 d_func()->autoUpdate = enabled;
207
208 emit autoUpdateChanged();
209 }
210}
211
212/*!
213 \fn QGalleryItemRequest::autoUpdateChanged()
214
215 Signals that the value of \l autoUpdate has changed.
216*/
217
218/*!
219 \property QGalleryItemRequest::itemId
220
221 \brief the ID of an item the request should return the properties of.
222*/
223
224QVariant QGalleryItemRequest::itemId() const
225{
226 return d_func()->itemId;
227}
228
229void QGalleryItemRequest::setItemId(const QVariant &itemId)
230{
231 if (d_func()->itemId != itemId) {
232 d_func()->itemId = itemId;
233
234 emit itemIdChanged();
235 }
236}
237
238/*!
239 \fn QGalleryItemRequest::itemIdChanged()
240
241 Signals that the value of the \l itemId property has changed.
242*/
243
244/*!
245 Returns the result set containing the meta-data of a type.
246*/
247
248QGalleryResultSet *QGalleryItemRequest::resultSet() const
249{
250 return d_func()->resultSet;
251}
252
253/*!
254 \fn QGalleryItemRequest::resultSetChanged(QGalleryResultSet *resultSet)
255
256 Signals that the \a resultSet containing the meta-data of an item has
257 changed.
258*/
259
260/*!
261 \fn QGalleryItemRequest::itemChanged()
262
263 Signals that the properties of an item have changed.
264*/
265
266/*!
267 Returns the key of \a property.
268*/
269
270int QGalleryItemRequest::propertyKey(const QString &property) const
271{
272 return d_func()->internalResultSet->propertyKey(property);
273}
274
275/*!
276 Returns the attributes of the property identified by \a key.
277*/
278
279QGalleryProperty::Attributes QGalleryItemRequest::propertyAttributes(int key) const
280{
281 return d_func()->internalResultSet->propertyAttributes(key);
282}
283
284/*!
285 Returns the type of the property identified by \a key.
286*/
287
288QVariant::Type QGalleryItemRequest::propertyType(int key) const
289{
290 return d_func()->internalResultSet->propertyType(key);
291}
292
293/*!
294 \property QGalleryItemRequest::valid
295
296 \brief Whether the request currently holds valid type information.
297*/
298
299bool QGalleryItemRequest::isValid() const
300{
301 return d_func()->internalResultSet->isValid();
302}
303
304/*!
305 \property QGalleryItemRequest::itemUrl
306
307 \brief The URL of an item.
308*/
309
310QUrl QGalleryItemRequest::itemUrl() const
311{
312 return d_func()->internalResultSet->itemUrl();
313}
314
315/*!
316 \property QGalleryItemRequest::itemType
317
318 \brief the type of an item.
319*/
320
321QString QGalleryItemRequest::itemType() const
322{
323 return d_func()->internalResultSet->itemType();
324}
325
326/*!
327 \property QGalleryItemRequest::resources
328
329 \brief the resources of an item.
330*/
331
332QList<QGalleryResource> QGalleryItemRequest::resources() const
333{
334 return d_func()->internalResultSet->resources();
335}
336
337/*!
338 Returns the value of a meta-data property identified by \a key.
339*/
340
341QVariant QGalleryItemRequest::metaData(int key) const
342{
343 return d_func()->internalResultSet->metaData(key);
344}
345
346/*!
347 Sets the \a value of a meta-data property identified by \a key.
348
349 Returns true if the value was changed; otherwise returns false.
350*/
351
352bool QGalleryItemRequest::setMetaData(int key, const QVariant &value)
353{
354 return d_func()->internalResultSet->setMetaData(key, value);
355}
356
357/*!
358 Returns the value of a meta-data \a property.
359*/
360
361QVariant QGalleryItemRequest::metaData(const QString &property) const
362{
363 return d_func()->internalResultSet->metaData(
364 key: d_func()->internalResultSet->propertyKey(property));
365}
366
367/*!
368 Sets the \value of a meta-data \a property.
369
370 Returns true if the value was changed; otherwise returns false.
371*/
372
373bool QGalleryItemRequest::setMetaData(const QString &property, const QVariant &value)
374{
375 return d_func()->internalResultSet->setMetaData(
376 key: d_func()->internalResultSet->propertyKey(property), value);
377}
378
379/*!
380 \fn QGalleryItemRequest::metaDataChanged(const QList<int> &keys)
381
382 Signals that the values of meta-data properties identified by \a keys
383 have changed.
384*/
385
386/*!
387 \reimp
388*/
389
390void QGalleryItemRequest::setResponse(QGalleryAbstractResponse *response)
391{
392 Q_D(QGalleryItemRequest);
393
394 const bool wasValid = d->internalResultSet->isValid();
395
396 d->resultSet = qobject_cast<QGalleryResultSet *>(object: response);
397 d->propertyKeys.clear();
398
399 if (d->resultSet) {
400 d->internalResultSet = d->resultSet;
401
402 connect(sender: d->resultSet, SIGNAL(itemsInserted(int,int)), receiver: this, SLOT(_q_itemsInserted(int,int)));
403 connect(sender: d->resultSet, SIGNAL(itemsRemoved(int,int)), receiver: this, SLOT(_q_itemsRemoved(int,int)));
404 connect(sender: d->resultSet, SIGNAL(itemsMoved(int,int,int)),
405 receiver: this, SLOT(_q_itemsMoved(int,int,int)));
406 connect(sender: d->resultSet, SIGNAL(metaDataChanged(int,int,QList<int>)),
407 receiver: this, SLOT(_q_metaDataChanged(int,int,QList<int>)));
408 connect(sender: d->resultSet, SIGNAL(currentItemChanged()), receiver: this, SLOT(_q_currentItemChanged()));
409
410 typedef QStringList::const_iterator iterator;
411 for (iterator it = d->propertyNames.constBegin(), end = d->propertyNames.constEnd();
412 it != end;
413 ++it) {
414 const int propertyKey = d->resultSet->propertyKey(property: *it);
415
416 if (propertyKey != -1)
417 d->propertyKeys.append(t: propertyKey);
418 }
419 } else {
420 d->internalResultSet = &d->nullResultSet;
421 }
422
423 emit resultSetChanged(resultSet: d->resultSet);
424
425 if (d->internalResultSet->itemCount() > 0)
426 d->internalResultSet->fetch(index: 0);
427 else if (wasValid)
428 emit itemChanged();
429}
430
431#include "moc_qgalleryitemrequest.cpp"
432
433QT_END_NAMESPACE_DOCGALLERY
434

source code of qtdocgallery/src/gallery/qgalleryitemrequest.cpp