| 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 "qgallerytyperequest.h" |
| 43 | #include "qgalleryabstractrequest_p.h" |
| 44 | |
| 45 | #include "qgallerynullresultset_p.h" |
| 46 | #include "qgalleryresource.h" |
| 47 | |
| 48 | QT_BEGIN_NAMESPACE_DOCGALLERY |
| 49 | |
| 50 | class QGalleryTypeRequestPrivate : public QGalleryAbstractRequestPrivate |
| 51 | { |
| 52 | Q_DECLARE_PUBLIC(QGalleryTypeRequest) |
| 53 | public: |
| 54 | QGalleryTypeRequestPrivate(QAbstractGallery *gallery) |
| 55 | : QGalleryAbstractRequestPrivate(gallery, QGalleryAbstractRequest::TypeRequest) |
| 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()->typeChanged(); |
| 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 | QString itemType; |
| 101 | QList<int> propertyKeys; |
| 102 | }; |
| 103 | |
| 104 | /*! |
| 105 | \class QGalleryTypeRequest |
| 106 | |
| 107 | \ingroup gallery |
| 108 | \ingroup gallery-requests |
| 109 | |
| 110 | \inmodule QtDocGallery |
| 111 | |
| 112 | \brief The QGalleryTypeRequest class provides an interface for requesting |
| 113 | the properties of a type from a gallery. |
| 114 | |
| 115 | QGalleryItemRequest executes a query which returns information summarizing |
| 116 | items of the type specified in \l itemType. The query will return |
| 117 | \l {metaData()}{meta-data} values which describe the type as a whole such |
| 118 | as the total number of items of that type. |
| 119 | |
| 120 | When the request has finished and if the type is one recognized by the |
| 121 | gallery the \l valid 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 type 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 type request. |
| 134 | |
| 135 | The \a parent is passed to QObject. |
| 136 | */ |
| 137 | |
| 138 | |
| 139 | QGalleryTypeRequest::QGalleryTypeRequest(QObject *parent) |
| 140 | : QGalleryAbstractRequest(*new QGalleryTypeRequestPrivate(0), parent) |
| 141 | { |
| 142 | } |
| 143 | /*! |
| 144 | Contructs a new type request for the given \a gallery. |
| 145 | |
| 146 | The \a parent is passed to QObject. |
| 147 | */ |
| 148 | |
| 149 | QGalleryTypeRequest::QGalleryTypeRequest(QAbstractGallery *gallery, QObject *parent) |
| 150 | : QGalleryAbstractRequest(*new QGalleryTypeRequestPrivate(gallery), parent) |
| 151 | { |
| 152 | } |
| 153 | |
| 154 | /*! |
| 155 | Destroys a gallery type request. |
| 156 | */ |
| 157 | |
| 158 | QGalleryTypeRequest::~QGalleryTypeRequest() |
| 159 | { |
| 160 | } |
| 161 | /*! |
| 162 | \property QGalleryTypeRequest::propertyNames |
| 163 | |
| 164 | \brief A list of names of meta-data properties a request should return |
| 165 | values for. |
| 166 | */ |
| 167 | |
| 168 | |
| 169 | QStringList QGalleryTypeRequest::propertyNames() const |
| 170 | { |
| 171 | return d_func()->propertyNames; |
| 172 | } |
| 173 | |
| 174 | void QGalleryTypeRequest::setPropertyNames(const QStringList &names) |
| 175 | { |
| 176 | if (d_func()->propertyNames != names) { |
| 177 | d_func()->propertyNames = names; |
| 178 | |
| 179 | emit propertyNamesChanged(); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /*! |
| 184 | \fn QGalleryTypeRequest::propertyNamesChanged() |
| 185 | |
| 186 | Signals that the value of \l propertyNames has changed. |
| 187 | */ |
| 188 | |
| 189 | /*! |
| 190 | \property QGalleryTypeRequest::autoUpdate |
| 191 | |
| 192 | \brief Whether a the results of a request should be updated after a request |
| 193 | has finished. |
| 194 | |
| 195 | If this is true the request will go into the Idle state when the request has |
| 196 | finished rather than returning to Inactive. |
| 197 | */ |
| 198 | |
| 199 | |
| 200 | bool QGalleryTypeRequest::autoUpdate() const |
| 201 | { |
| 202 | return d_func()->autoUpdate; |
| 203 | } |
| 204 | |
| 205 | void QGalleryTypeRequest::setAutoUpdate(bool enabled) |
| 206 | { |
| 207 | if (d_func()->autoUpdate != enabled) { |
| 208 | d_func()->autoUpdate = enabled; |
| 209 | |
| 210 | emit autoUpdateChanged(); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /*! |
| 215 | \fn QGalleryTypeRequest::autoUpdateChanged() |
| 216 | |
| 217 | Signals that the value of \l autoUpdate has changed. |
| 218 | */ |
| 219 | |
| 220 | /*! |
| 221 | \property QGalleryTypeRequest::itemType |
| 222 | |
| 223 | \brief the type a request should return the properties of. |
| 224 | */ |
| 225 | |
| 226 | QString QGalleryTypeRequest::itemType() const |
| 227 | { |
| 228 | return d_func()->itemType; |
| 229 | } |
| 230 | |
| 231 | void QGalleryTypeRequest::setItemType(const QString &itemType) |
| 232 | { |
| 233 | if (d_func()->itemType != itemType) { |
| 234 | d_func()->itemType = itemType; |
| 235 | |
| 236 | emit itemTypeChanged(); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /*! |
| 241 | \fn QGalleryTypeRequest::itemTypeChanged() |
| 242 | |
| 243 | Signals that the \l itemType property has changed. |
| 244 | */ |
| 245 | |
| 246 | /*! |
| 247 | Returns the result set containing the meta-data of a type. |
| 248 | */ |
| 249 | |
| 250 | QGalleryResultSet *QGalleryTypeRequest::resultSet() const |
| 251 | { |
| 252 | return d_func()->resultSet; |
| 253 | } |
| 254 | |
| 255 | /*! |
| 256 | \fn QGalleryTypeRequest::resultSetChanged(QGalleryResultSet *resultSet) |
| 257 | |
| 258 | Signals that the \a resultSet containing the meta-data of a type has |
| 259 | changed. |
| 260 | */ |
| 261 | |
| 262 | /*! |
| 263 | \fn QGalleryTypeRequest::typeChanged() |
| 264 | |
| 265 | Signals that the properties of a type have changed. |
| 266 | */ |
| 267 | |
| 268 | /*! |
| 269 | Returns the key of \a property. |
| 270 | */ |
| 271 | |
| 272 | int QGalleryTypeRequest::propertyKey(const QString &property) const |
| 273 | { |
| 274 | return d_func()->internalResultSet->propertyKey(property); |
| 275 | } |
| 276 | |
| 277 | /*! |
| 278 | Returns the attributes of the property identified by \a key. |
| 279 | */ |
| 280 | |
| 281 | QGalleryProperty::Attributes QGalleryTypeRequest::propertyAttributes(int key) const |
| 282 | { |
| 283 | return d_func()->internalResultSet->propertyAttributes(key); |
| 284 | } |
| 285 | |
| 286 | /*! |
| 287 | Returns the type of the property identified by \a key. |
| 288 | */ |
| 289 | |
| 290 | QVariant::Type QGalleryTypeRequest::propertyType(int key) const |
| 291 | { |
| 292 | return d_func()->internalResultSet->propertyType(key); |
| 293 | } |
| 294 | |
| 295 | /*! |
| 296 | \property QGalleryTypeRequest::valid |
| 297 | |
| 298 | \brief Whether the request currently holds valid type information. |
| 299 | */ |
| 300 | |
| 301 | bool QGalleryTypeRequest::isValid() const |
| 302 | { |
| 303 | return d_func()->internalResultSet->isValid(); |
| 304 | } |
| 305 | |
| 306 | /*! |
| 307 | Returns the value of a meta-data property identified by \a key. |
| 308 | */ |
| 309 | |
| 310 | |
| 311 | QVariant QGalleryTypeRequest::metaData(int key) const |
| 312 | { |
| 313 | return d_func()->internalResultSet->metaData(key); |
| 314 | } |
| 315 | |
| 316 | /*! |
| 317 | Returns the value of a meta-data \a property. |
| 318 | */ |
| 319 | |
| 320 | QVariant QGalleryTypeRequest::metaData(const QString &property) const |
| 321 | { |
| 322 | return d_func()->internalResultSet->metaData( |
| 323 | key: d_func()->internalResultSet->propertyKey(property)); |
| 324 | } |
| 325 | |
| 326 | /*! |
| 327 | \fn QGalleryTypeRequest::metaDataChanged(const QList<int> &keys) |
| 328 | |
| 329 | Signals that the values of meta-data properties identified by \a keys |
| 330 | have changed. |
| 331 | */ |
| 332 | |
| 333 | /*! |
| 334 | \reimp |
| 335 | */ |
| 336 | |
| 337 | void QGalleryTypeRequest::setResponse(QGalleryAbstractResponse *response) |
| 338 | { |
| 339 | Q_D(QGalleryTypeRequest); |
| 340 | |
| 341 | const bool wasValid = d->internalResultSet->isValid(); |
| 342 | |
| 343 | d->resultSet = qobject_cast<QGalleryResultSet *>(object: response); |
| 344 | d->propertyKeys.clear(); |
| 345 | |
| 346 | if (d->resultSet) { |
| 347 | d->internalResultSet = d->resultSet; |
| 348 | |
| 349 | connect(sender: d->resultSet, SIGNAL(itemsInserted(int,int)), receiver: this, SLOT(_q_itemsInserted(int,int))); |
| 350 | connect(sender: d->resultSet, SIGNAL(itemsRemoved(int,int)), receiver: this, SLOT(_q_itemsRemoved(int,int))); |
| 351 | connect(sender: d->resultSet, SIGNAL(itemsMoved(int,int,int)), |
| 352 | receiver: this, SLOT(_q_itemsMoved(int,int,int))); |
| 353 | connect(sender: d->resultSet, SIGNAL(metaDataChanged(int,int,QList<int>)), |
| 354 | receiver: this, SLOT(_q_metaDataChanged(int,int,QList<int>))); |
| 355 | connect(sender: d->resultSet, SIGNAL(currentItemChanged()), receiver: this, SLOT(_q_currentItemChanged())); |
| 356 | |
| 357 | typedef QStringList::const_iterator iterator; |
| 358 | for (iterator it = d->propertyNames.constBegin(), end = d->propertyNames.constEnd(); |
| 359 | it != end; |
| 360 | ++it) { |
| 361 | const int propertyKey = d->resultSet->propertyKey(property: *it); |
| 362 | |
| 363 | if (propertyKey != -1) |
| 364 | d->propertyKeys.append(t: propertyKey); |
| 365 | } |
| 366 | } else { |
| 367 | d->internalResultSet = &d->nullResultSet; |
| 368 | } |
| 369 | |
| 370 | emit resultSetChanged(resultSet: d->resultSet); |
| 371 | |
| 372 | if (d->internalResultSet->itemCount() > 0) |
| 373 | d->internalResultSet->fetch(index: 0); |
| 374 | else if (wasValid) |
| 375 | emit typeChanged(); |
| 376 | } |
| 377 | |
| 378 | #include "moc_qgallerytyperequest.cpp" |
| 379 | |
| 380 | QT_END_NAMESPACE_DOCGALLERY |
| 381 | |