summaryrefslogtreecommitdiffstats
path: root/src/gallery/simulator/qsimulatorgalleryresultset.cpp
blob: 36cddfd4233fa55b6f69f6a962790ea58215f1d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtDocGallery module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qsimulatorgalleryresultset_p.h"

#include "qdocumentgallery.h"

#include <QtCore/QDateTime>

QT_DOCGALLERY_BEGIN_NAMESPACE

namespace Utility {
enum PropertyKeys {
    // Query Properties
    FileName = 0,
    FilePath,
    FileExtension,
    FileSize,
    MimeType,
    LastModified,
    LastAccessed,
    // Image Properties
    Title,
    Width,
    Height,
    Keywords
};
}

QSimulatorGalleryResultSet::QSimulatorGalleryResultSet(QObject *parent) :
    QGalleryResultSet(parent),
    connection(0),
    valid(false),
    mCurrentIndex(0)
{
    queryRequest = qobject_cast<QGalleryQueryRequest*>(parent);
    itemRequest = qobject_cast<QGalleryItemRequest*>(parent);
    if (queryRequest) {
        if (queryRequest->rootType() == QLatin1String("Image")) {
            // Do the image magic
            connection = Simulator::DocGalleryConnection::instance();
        } else {
            QGalleryFilter filter = queryRequest->filter();
            if (filter.toMetaDataFilter().propertyName() != QLatin1String("filePath")) {
                qWarning("The Qt Simulator can only handle filePath filters currently");
                return;
            }
            filePath = filter.toMetaDataFilter().value().toString();
        }
    } else if (itemRequest) {
        filePath = itemRequest->itemId().toString();
    } else {
        qWarning("Unsupport Request Type!");
        return;
    }

    image = QImage(filePath);
    if (!image.isNull())
        itemTypeString = QDocumentGallery::Image.name();
    else if (!filePath.isEmpty())
        itemTypeString = QDocumentGallery::File.name();

    valid = true;
    finish();
}

QSimulatorGalleryResultSet::~QSimulatorGalleryResultSet()
{
}

int QSimulatorGalleryResultSet::propertyKey(const QString &property) const
{
    if (property == QDocumentGallery::url.name() ||
        property == QDocumentGallery::fileName.name())
        return Utility::FileName;
    else if(property == QDocumentGallery::filePath.name() ||
              property == QDocumentGallery::path.name())
        return Utility::FilePath;
    else if (property == QDocumentGallery::fileExtension.name())
        return Utility::FileExtension;
    else if (property == QDocumentGallery::fileSize.name())
        return Utility::FileSize;
    else if (property == QDocumentGallery::mimeType.name())
        return Utility::MimeType;
    else if (property == QDocumentGallery::lastModified.name())
        return Utility::LastModified;
    else if (property == QDocumentGallery::lastAccessed.name())
        return Utility::LastAccessed;
    else if (property == QDocumentGallery::title.name())
        return Utility::Title;
    else if (property == QDocumentGallery::width.name())
        return Utility::Width;
    else if (property == QDocumentGallery::height.name())
        return Utility::Height;
    else if (property == QDocumentGallery::keywords.name())
        return Utility::Keywords;
    return -1;
}

QGalleryProperty::Attributes QSimulatorGalleryResultSet::propertyAttributes(int key) const
{
    switch(key) {
    case Utility::FileName: return QGalleryProperty::CanRead;
    case Utility::FilePath: return QGalleryProperty::CanRead;
    case Utility::FileExtension: return QGalleryProperty::CanRead;
    case Utility::FileSize: return QGalleryProperty::CanRead;
    case Utility::LastModified: return QGalleryProperty::CanRead;
    case Utility::LastAccessed: return QGalleryProperty::CanRead;
    case Utility::Title: return QGalleryProperty::CanRead;
    case Utility::Width: return QGalleryProperty::CanRead;
    case Utility::Height: return QGalleryProperty::CanRead;
    case Utility::Keywords: return QGalleryProperty::CanRead;
    default: return QGalleryProperty::Attributes();
    }
}

QVariant::Type QSimulatorGalleryResultSet::propertyType(int key) const
{
    QVariant::Type ret = QVariant::Invalid;
    switch(key) {
    case Utility::FileName: ret = QVariant::String; break;
    case Utility::FilePath: ret = QVariant::String; break;
    case Utility::FileExtension: ret = QVariant::String; break;
    case Utility::FileSize: ret = QVariant::Int; break;
    case Utility::LastModified: ret = QVariant::DateTime; break;
    case Utility::LastAccessed: ret = QVariant::DateTime; break;
    case Utility::Title: ret = QVariant::String; break;
    case Utility::Width: ret = QVariant::Int; break;
    case Utility::Keywords: ret = QVariant::String; break;
    default: ret = QVariant::Invalid;break;
    }

    return ret;
}

int QSimulatorGalleryResultSet::itemCount() const
{
    if (connection)
        return connection->galleryData().images.count();
    return valid ? 1 : 0;
}

bool QSimulatorGalleryResultSet::isValid() const
{
    return valid;
}

QVariant QSimulatorGalleryResultSet::itemId() const
{
    if (valid) {
        return currentFileInfo().absoluteFilePath();
    }
    return QVariant();
}

QUrl QSimulatorGalleryResultSet::itemUrl() const
{
    if (connection) {
        QString data = currentFileInfo().absoluteFilePath();
        return QUrl::fromLocalFile(data);
    }
    return QUrl();
}

QString QSimulatorGalleryResultSet::itemType() const
{
    return itemTypeString;
}

QVariant QSimulatorGalleryResultSet::metaData(int key) const
{
    QFileInfo info = currentFileInfo();
    if (key == Utility::FileName)
        return info.absoluteFilePath();
    else if (key == Utility::FilePath)
        return info.absolutePath();
    else if (key == Utility::FileExtension)
        return info.suffix();
    else if (key == Utility::FileSize)
        return info.size();
    else if (key == Utility::LastAccessed)
        return info.lastRead();
    else if (key == Utility::LastModified)
        return info.lastModified();
    else if (key == Utility::Title)
        return QLatin1String("Image Title of Simulator");
    else if (key == Utility::Width)
        return image.width();
    else if (key == Utility::Height)
        return image.height();
    else if (key == Utility::Keywords)
        return QLatin1String("Simulator, Some Tags, Not read from file yet");
    return QVariant();
}

bool QSimulatorGalleryResultSet::setMetaData(int key, const QVariant &value)
{
    Q_UNUSED(key);
    Q_UNUSED(value);
    return false;
}

int QSimulatorGalleryResultSet::currentIndex() const
{
    return mCurrentIndex;
}

bool QSimulatorGalleryResultSet::fetch(int index)
{
    if (connection) {
        if (index < 0 || index >= connection->galleryData().images.size())
            return false;
        mCurrentIndex = index;
    }
    return true;
}

QFileInfo QSimulatorGalleryResultSet::currentFileInfo() const
{
    if (connection)
        return QFileInfo(connection->galleryData().images.at(mCurrentIndex).fileName);
    else
        return QFileInfo(filePath);
}

QT_DOCGALLERY_END_NAMESPACE