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 "qdeclarativegallerytype.h"
43
44
45#include <qgalleryresultset.h>
46
47#include <QtCore/qcoreapplication.h>
48#include <QtQml/qqmlinfo.h>
49#include <QtQml/qqmlpropertymap.h>
50
51QT_BEGIN_NAMESPACE_DOCGALLERY
52
53QDeclarativeGalleryType::QDeclarativeGalleryType(QObject *parent)
54 : QObject(parent)
55 , m_metaData(0)
56 , m_status(Null)
57 , m_updateStatus(Incomplete)
58{
59 connect(sender: &m_request, SIGNAL(stateChanged(QGalleryAbstractRequest::State)),
60 receiver: this, SLOT(_q_stateChanged()));
61 connect(sender: &m_request, SIGNAL(progressChanged(int,int)), receiver: this, SIGNAL(progressChanged()));
62
63 connect(sender: &m_request, SIGNAL(typeChanged()),
64 receiver: this, SLOT(_q_typeChanged()));
65 connect(sender: &m_request, SIGNAL(metaDataChanged(QList<int>)),
66 receiver: this, SLOT(_q_metaDataChanged(QList<int>)));
67
68 m_metaData = new QQmlPropertyMap(this);
69}
70
71QDeclarativeGalleryType::~QDeclarativeGalleryType()
72{
73}
74
75qreal QDeclarativeGalleryType::progress() const
76{
77 const int max = m_request.maximumProgress();
78
79 return max > 0
80 ? qreal(m_request.currentProgress()) / max
81 : qreal(0.0);
82}
83
84void QDeclarativeGalleryType::setPropertyNames(const QStringList &names)
85{
86 if (m_updateStatus == Incomplete) {
87 m_request.setPropertyNames(names);
88
89 emit propertyNamesChanged();
90 }
91}
92
93void QDeclarativeGalleryType::setAutoUpdate(bool enabled)
94{
95 if (m_request.autoUpdate() != enabled) {
96 m_request.setAutoUpdate(enabled);
97
98 if (enabled)
99 deferredExecute();
100 else if (m_status == Idle)
101 m_request.cancel();
102
103 emit autoUpdateChanged();
104 }
105}
106
107void QDeclarativeGalleryType::componentComplete()
108{
109 m_updateStatus = NoUpdate;
110
111 if (!m_request.itemType().isEmpty())
112 m_request.execute();
113}
114
115void QDeclarativeGalleryType::reload()
116{
117 if (m_updateStatus == PendingUpdate)
118 m_updateStatus = CanceledUpdate;
119
120 m_request.execute();
121}
122
123void QDeclarativeGalleryType::cancel()
124{
125 if (m_updateStatus == PendingUpdate)
126 m_updateStatus = CanceledUpdate;
127
128 m_request.cancel();
129}
130
131void QDeclarativeGalleryType::clear()
132{
133 if (m_updateStatus == PendingUpdate)
134 m_updateStatus = CanceledUpdate;
135
136 m_request.clear();
137}
138
139void QDeclarativeGalleryType::deferredExecute()
140{
141 if (m_updateStatus == NoUpdate) {
142 m_updateStatus = PendingUpdate;
143
144 QCoreApplication::postEvent(receiver: this, event: new QEvent(QEvent::UpdateRequest));
145 } else if (m_updateStatus == CanceledUpdate) {
146 m_updateStatus = PendingUpdate;
147 }
148}
149
150bool QDeclarativeGalleryType::event(QEvent *event)
151{
152 if (event->type() == QEvent::UpdateRequest) {
153 UpdateStatus status = m_updateStatus;
154 m_updateStatus = NoUpdate;
155
156 if (status == PendingUpdate)
157 m_request.execute();
158
159 return true;
160 } else {
161 return QObject::event(event);
162 }
163}
164
165void QDeclarativeGalleryType::_q_stateChanged()
166{
167 m_status = Status(m_request.state());
168
169 if (m_status == Error) {
170 const QString message = m_request.errorString();
171
172 if (!message.isEmpty()) {
173 qmlInfo(me: this) << message;
174 } else {
175 switch (m_request.error()) {
176 case QDocumentGallery::ConnectionError:
177 qmlInfo(me: this) << tr(s: "An error was encountered connecting to the document gallery");
178 break;
179 case QDocumentGallery::ItemTypeError:
180 qmlInfo(me: this) << tr(s: "DocumentGallery.%1 is not a supported item type")
181 .arg(a: m_request.itemType());
182 break;
183 default:
184 break;
185 }
186 }
187 emit statusChanged();
188 } else if (m_status == Idle && !m_request.autoUpdate()) {
189 m_request.cancel();
190 } else {
191 emit statusChanged();
192 }
193}
194
195void QDeclarativeGalleryType::_q_typeChanged()
196{
197 if (m_request.isValid()) {
198 for (QHash<int, QString>::const_iterator it = m_propertyKeys.constBegin();
199 it != m_propertyKeys.constEnd();
200 ++it) {
201 if (m_request.propertyKey(property: it.value()) < 0)
202 m_metaData->clear(key: it.value());
203 }
204
205 m_propertyKeys.clear();
206
207 const QStringList propertyNames = m_request.propertyNames();
208
209 for (QStringList::const_iterator it = propertyNames.begin(); it != propertyNames.end(); ++it) {
210 const int key = m_request.propertyKey(property: *it);
211
212 if (key >= 0) {
213 m_propertyKeys.insert(akey: key, avalue: *it);
214
215 QVariant value = m_request.metaData(key);
216 m_metaData->insert(key: *it, value: value.isNull()
217 ? QVariant(m_request.propertyType(key))
218 : value);
219 }
220 }
221 } else {
222 typedef QHash<int, QString>::const_iterator iterator;
223 for (iterator it = m_propertyKeys.constBegin(); it != m_propertyKeys.constEnd(); ++it)
224 m_metaData->clear(key: it.value());
225
226 m_propertyKeys.clear();
227 }
228
229 emit availableChanged();
230}
231
232void QDeclarativeGalleryType::_q_metaDataChanged(const QList<int> &keys)
233{
234 typedef QList<int>::const_iterator iterator;
235 for (iterator it = keys.begin(); it != keys.end(); ++it){
236 QVariant value = m_request.metaData(key: *it);
237 m_metaData->insert(key: m_propertyKeys.value(akey: *it), value: value.isNull()
238 ? QVariant(m_request.propertyType(key: *it))
239 : value);
240 }
241}
242
243/*!
244 \qmltype DocumentGalleryType
245 \instantiates QDeclarativeDocumentGalleryType
246
247 \inmodule QtDocGallery
248 \ingroup qml-gallery
249
250 \brief The DocumentGalleryType element allows you to request information
251 about an item type from the document gallery.
252
253 This element is part of the \b {QtMobility.gallery 1.1} module.
254
255 \sa DocumentGalleryModel, DocumentGalleryItem
256*/
257
258QDeclarativeDocumentGalleryType::QDeclarativeDocumentGalleryType(QObject *parent)
259 : QDeclarativeGalleryType(parent)
260{
261}
262
263QDeclarativeDocumentGalleryType::~QDeclarativeDocumentGalleryType()
264{
265}
266
267void QDeclarativeDocumentGalleryType::classBegin()
268{
269 m_request.setGallery(QDeclarativeDocumentGallery::gallery(object: this));
270}
271
272/*!
273 \qmlproperty enum DocumentGalleryType::status
274
275 This property holds the status of a type request. It can be one of:
276
277 \list
278 \li Null No \l itemType has been specified.
279 \li Active Information about an \l itemType is being fetched from the gallery.
280 \li Finished Information about an \l itemType is available.
281 \li Idle Information about an \l itemType which will be automatically
282 updated is available.
283 \li Canceling The query was canceled but hasn't yet reached the
284 canceled status.
285 \li Canceled The query was canceled.
286 \li Error Information about a type could not be retrieved due to an error.
287 \endlist
288*/
289
290/*!
291 \qmlproperty real DocumentGalleryType::progress
292
293 This property holds the current progress of the request, from 0.0 (started)
294 to 1.0 (finished).
295*/
296
297/*!
298 \qmlproperty QStringList DocumentGalleryType::properties
299
300 This property holds the type properties a request should return values for.
301*/
302
303/*!
304 \qmlproperty bool DocumentGalleryType::autoUpdate
305
306 This property holds whether a request should refresh its results
307 automatically.
308*/
309
310/*!
311 \qmlproperty enum DocumentGalleryType::itemType
312
313 This property holds the item type that a request fetches information about.
314 It can be one of:
315
316 \list
317 \li DocumentGallery.InvalidType
318 \li DocumentGallery.File
319 \li DocumentGallery.Folder
320 \li DocumentGallery.Document
321 \li DocumentGallery.Text
322 \li DocumentGallery.Audio
323 \li DocumentGallery.Image
324 \li DocumentGallery.Video
325 \li DocumentGallery.Playlist
326 \li DocumentGallery.Artist
327 \li DocumentGallery.AlbumArtist
328 \li DocumentGallery.Album
329 \li DocumentGallery.AudioGenre
330 \li DocumentGallery.PhotoAlbum
331 \endlist
332*/
333
334QDeclarativeDocumentGallery::ItemType QDeclarativeDocumentGalleryType::itemType() const
335{
336 return QDeclarativeDocumentGallery::itemTypeFromString(string: m_request.itemType());
337}
338
339void QDeclarativeDocumentGalleryType::setItemType(QDeclarativeDocumentGallery::ItemType itemType)
340{
341 const QString type = QDeclarativeDocumentGallery::toString(type: itemType);
342
343 if (type != m_request.itemType()) {
344 m_request.setItemType(type);
345
346 if (m_updateStatus != Incomplete) {
347 if (!type.isEmpty())
348 m_request.execute();
349 else
350 m_request.clear();
351 }
352
353 emit itemTypeChanged();
354 }
355}
356
357/*!
358 \qmlproperty bool DocumentGalleryType::available
359
360 This property holds whether the meta-data of a type is available.
361*/
362
363/*!
364 \qmlproperty object DocumentGalleryType::metaData
365
366 This property holds the meta-data of an item type/
367*/
368
369/*!
370 \qmlmethod DocumentGalleryType::reload()
371
372 Re-queries the gallery.
373*/
374
375/*!
376 \qmlmethod DocumentGalleryType::cancel()
377
378 Cancels an executing request.
379*/
380
381/*!
382 \qmlmethod DocumentGalleryType::clear()
383
384 Clears the results of a request.
385*/
386
387#include "moc_qdeclarativegallerytype.cpp"
388
389QT_END_NAMESPACE_DOCGALLERY
390

source code of qtdocgallery/src/imports/gallery/qdeclarativegallerytype.cpp