diff options
author | Andrew den Exter <andrew.den.exter@jollamobile.com> | 2013-03-11 14:20:12 +1000 |
---|---|---|
committer | Andrew den Exter <andrew.den.exter@qinetic.com.au> | 2013-03-27 08:32:52 +0100 |
commit | c4a36f94bd50d0236aaa7bddbe72bc7e0fbdac4f (patch) | |
tree | 1c85b8c72e2feb44520230be0f202f5f2005100a | |
parent | a1cd792c51fbd53254423a1ead401394961a35d2 (diff) |
Fix DocumentGallery returning encoded urls.
Tracker stores url properties in encoded form, these need to be
decoded before they are returned and encoded when used in queries.
Change-Id: I26db02e758cb14e6789418631979fa21cf58daff
Reviewed-by: Marko Mattila <marko.mattila@jollamobile.com>
Reviewed-by: Christopher Adams <chris.adams@jollamobile.com>
4 files changed, 152 insertions, 44 deletions
diff --git a/src/gallery/maemo6/qgallerytrackerlistcolumn.cpp b/src/gallery/maemo6/qgallerytrackerlistcolumn.cpp index 56cba926b8..dfb107c1e8 100644 --- a/src/gallery/maemo6/qgallerytrackerlistcolumn.cpp +++ b/src/gallery/maemo6/qgallerytrackerlistcolumn.cpp @@ -60,6 +60,11 @@ QVariant QGalleryTrackerStringListColumn::toVariant(const QString &string) const return string.split(m_separatorChar, QString::SkipEmptyParts); } +QVariant QGalleryTrackerUrlColumn::toVariant(const QString &string) const +{ + return QUrl::fromEncoded(string.toUtf8(), QUrl::StrictMode); +} + QString QGalleryTrackerStringListColumn::toString(const QVariant &variant) const { return variant.type() == QVariant::StringList @@ -117,7 +122,7 @@ QVariant QGalleryTrackerCompositeIdColumn::value(QVector<QVariant>::const_iterat QVariant QGalleryTrackerFileUrlColumn::value(QVector<QVariant>::const_iterator row) const { - return QUrl( (row + m_column )->toString()); + return *(row + m_column); } QGalleryTrackerCompositeColumn *QGalleryTrackerFileUrlColumn::create(const QVector<int> &) @@ -127,7 +132,7 @@ QGalleryTrackerCompositeColumn *QGalleryTrackerFileUrlColumn::create(const QVect QVariant QGalleryTrackerFilePathColumn::value(QVector<QVariant>::const_iterator row) const { - return QUrl( (row + QGALLERYTRACKERFILEURLCOLUMN_DEFAULT_COL)->toString() ).path(); + return (row + QGALLERYTRACKERFILEURLCOLUMN_DEFAULT_COL)->toUrl().path(); } QGalleryTrackerCompositeColumn *QGalleryTrackerFilePathColumn::create(const QVector<int> &) @@ -137,7 +142,7 @@ QGalleryTrackerCompositeColumn *QGalleryTrackerFilePathColumn::create(const QVec QVariant QGalleryTrackerPathColumn::value(QVector<QVariant>::const_iterator row) const { - QString filePath = QUrl((row + QGALLERYTRACKERFILEURLCOLUMN_DEFAULT_COL)->toString()).path(); + QString filePath = (row + QGALLERYTRACKERFILEURLCOLUMN_DEFAULT_COL)->toUrl().path(); return filePath.section(QLatin1Char('/'), 0, -2); } @@ -148,7 +153,7 @@ QGalleryTrackerCompositeColumn *QGalleryTrackerPathColumn::create(const QVector< QVariant QGalleryTrackerFileExtensionColumn::value(QVector<QVariant>::const_iterator row) const { - QString fileName = (row + m_column)->toString(); + QString fileName = (row + m_column)->toUrl().path(); const int index = fileName.lastIndexOf(QLatin1Char('.')); return index > fileName.lastIndexOf(QLatin1Char('/')) ? QVariant(fileName.mid(index + 1)) diff --git a/src/gallery/maemo6/qgallerytrackerlistcolumn_p.h b/src/gallery/maemo6/qgallerytrackerlistcolumn_p.h index bb7f8f6759..ff5a6cd386 100644 --- a/src/gallery/maemo6/qgallerytrackerlistcolumn_p.h +++ b/src/gallery/maemo6/qgallerytrackerlistcolumn_p.h @@ -85,6 +85,12 @@ public: QVariant toVariant(const QString &string) const; }; +class QGalleryTrackerUrlColumn : public QGalleryTrackerValueColumn +{ +public: + QVariant toVariant(const QString &string) const; +}; + class QGalleryTrackerStringListColumn : public QGalleryTrackerValueColumn { public: diff --git a/src/gallery/maemo6/qgallerytrackerschema.cpp b/src/gallery/maemo6/qgallerytrackerschema.cpp index 3e7e27e901..c147aed1ed 100644 --- a/src/gallery/maemo6/qgallerytrackerschema.cpp +++ b/src/gallery/maemo6/qgallerytrackerschema.cpp @@ -401,20 +401,28 @@ static bool qt_write_comparison( const QLatin1String &field, const QVariant &value, const char *op, - QString *query) + QString *query, + QVariant::Type type = QVariant::String) { - if (value.canConvert(QVariant::String)) { - *query += QLatin1String("(") - + field - + QLatin1String(op) - + QLatin1String("'") - + value.toString() - + QLatin1String("')"); - return true; + QString stringValue; + if (type == QVariant::Url && value.canConvert(QVariant::Url)) { + QByteArray encodedUrl = value.toUrl().toEncoded(); + stringValue = QString::fromUtf8(encodedUrl.data(), encodedUrl.length()); + } else if (value.canConvert(QVariant::String)) { + stringValue = value.toString(); } else { *error = QDocumentGallery::FilterError; return false; } + + *query += QLatin1String("(") + + field + + QLatin1String(op) + + QLatin1String("'") + + stringValue + + QLatin1String("')"); + + return true; } static bool qt_write_function( @@ -438,20 +446,28 @@ static bool qt_write_function( const char *function, const QString &field, const QVariant &value, - QString *query) + QString *query, + QVariant::Type type = QVariant::String) { - if (value.canConvert(QVariant::String)) { - *query += QLatin1String(function) - + QLatin1String("(") - + field - + QLatin1String(",'") - + value.toString() - + QLatin1String("')"); - return true; + QString stringValue; + if (type == QVariant::Url && value.canConvert(QVariant::Url)) { + QByteArray encodedUrl = value.toUrl().toEncoded(); + stringValue = QString::fromUtf8(encodedUrl.data(), encodedUrl.length()); + } else if (value.canConvert(QVariant::String)) { + stringValue = value.toString(); } else { *error = QDocumentGallery::FilterError; return false; } + + *query += QLatin1String(function) + + QLatin1String("(") + + field + + QLatin1String(",'") + + stringValue + + QLatin1String("')"); + + return true; } static bool qt_writeCondition( @@ -470,32 +486,33 @@ static bool qt_writeCondition( if ((index = properties.indexOfProperty(propertyName)) != -1) { const QVariant value = filter.value(); + const QGalleryItemProperty &property = properties[index]; switch (filter.comparator()) { case QGalleryFilter::Equals: return value.type() != QVariant::RegExp - ? qt_write_comparison(error, properties[index].field, value, "=", query) + ? qt_write_comparison(error, property.field, value, "=", query, property.type) : qt_write_function(error, "REGEX", properties[index].field, value.toRegExp(), query); case QGalleryFilter::LessThan: - return qt_write_comparison(error, properties[index].field, value, "<", query); + return qt_write_comparison(error, property.field, value, "<", query, property.type); case QGalleryFilter::GreaterThan: - return qt_write_comparison(error, properties[index].field, value, ">", query); + return qt_write_comparison(error, property.field, value, ">", query, property.type); case QGalleryFilter::LessThanEquals: - return qt_write_comparison(error, properties[index].field, value, "<=", query); + return qt_write_comparison(error, property.field, value, "<=", query, property.type); case QGalleryFilter::GreaterThanEquals: - return qt_write_comparison(error, properties[index].field, value, ">=", query); + return qt_write_comparison(error, property.field, value, ">=", query, property.type); case QGalleryFilter::Contains: - return qt_write_function(error, "fn:contains", properties[index].field, value, query); + return qt_write_function(error, "fn:contains", property.field, value, query, property.type); case QGalleryFilter::StartsWith: - return qt_write_function(error, "fn:starts-with", properties[index].field, value, query); + return qt_write_function(error, "fn:starts-with", property.field, value, query, property.type); case QGalleryFilter::EndsWith: - return qt_write_function(error, "fn:ends-with", properties[index].field, value, query); + return qt_write_function(error, "fn:ends-with", property.field, value, query, property.type); case QGalleryFilter::Wildcard: - return qt_write_function(error, "fn:contains", properties[index].field, value, query); + return qt_write_function(error, "fn:contains", property.field, value, query, property.type); case QGalleryFilter::RegExp: return value.type() != QVariant::RegExp - ? qt_write_function(error, "REGEX", properties[index].field, value, query) - : qt_write_function(error, "REGEX", properties[index].field, value.toRegExp(), query); + ? qt_write_function(error, "REGEX", property.field, value, query, property.type) + : qt_write_function(error, "REGEX", property.field, value.toRegExp(), query); default: *error = QDocumentGallery::FilterError; @@ -637,7 +654,7 @@ static bool qt_writeFileExtensionCondition( // nie:url nie:isPartOf, nie:created, nie:lastRefreshed, nie:interpretedAs, nie:dataSource, // nie:byteSize #define QT_GALLERY_NIE_DATAOBJECT_PROPERTIES \ - QT_GALLERY_ITEM_PROPERTY("url", "nie:url(?x)", String, CanRead | CanSort | CanFilter | IsResource) + QT_GALLERY_ITEM_PROPERTY("url", "nie:url(?x)", Url, CanRead | CanSort | CanFilter | IsResource) //nie:InformationElement // nie:usageCounter, nie:rootElementOf, nie:contentSize, nie:isLogicalPartOf, nie:characterSet, @@ -1342,6 +1359,9 @@ static QVector<QGalleryTrackerValueColumn *> qt_createValueColumns( case QVariant::DateTime: columns.append(new QGalleryTrackerDateTimeColumn); break; + case QVariant::Url: + columns.append(new QGalleryTrackerUrlColumn); + break; default: Q_ASSERT(false); break; @@ -1488,7 +1508,7 @@ void QGalleryTrackerSchema::populateItemArguments( arguments->typeColumn.reset(new QGalleryTrackerServiceTypeColumn); arguments->valueColumns = QVector<QGalleryTrackerValueColumn *>() << new QGalleryTrackerStringColumn - << new QGalleryTrackerStringColumn + << new QGalleryTrackerUrlColumn << new QGalleryTrackerServiceIndexColumn << qt_createValueColumns(valueTypes + extendedValueTypes); } else { diff --git a/tests/auto/qgallerytrackerschema_maemo6/tst_qgallerytrackerschema.cpp b/tests/auto/qgallerytrackerschema_maemo6/tst_qgallerytrackerschema.cpp index d844813890..2e396cf44a 100644 --- a/tests/auto/qgallerytrackerschema_maemo6/tst_qgallerytrackerschema.cpp +++ b/tests/auto/qgallerytrackerschema_maemo6/tst_qgallerytrackerschema.cpp @@ -103,6 +103,8 @@ private Q_SLOTS: void queryResponseRootItem(); void queryResponseFilter_data(); void queryResponseFilter(); + void queryResponseItemUrl_data(); + void queryResponseItemUrl(); void queryResponseValueColumnToVariant_data(); void queryResponseValueColumnToVariant(); void queryResponseValueColumnToString_data(); @@ -497,7 +499,7 @@ void tst_QGalleryTrackerSchema::prepareValidItemResponse_data() << QStringList() << (QVector<QVariant>() << QLatin1String("uuid:ff172362-d959-99e0-a792-0ddafdd2c559") - << QLatin1String("file:///path/to/file.ext") + << QUrl(QLatin1String("file:///path/to/file.ext")) << 0) << QVariant(QUrl(QLatin1String("file:///path/to/file.ext"))) << QVariant(QLatin1String("File")) @@ -622,7 +624,7 @@ void tst_QGalleryTrackerSchema::queryResponseRootType_data() << 1 << (QVector<QVariant>() << QLatin1String("uuid:ff172362-d959-99e0-a792-0ddafdd2c559") - << QLatin1String("file:///path/to/file.ext") + << QUrl(QLatin1String("file:///path/to/file.ext")) << 0) << "file::uuid:ff172362-d959-99e0-a792-0ddafdd2c559" << QVariant(QUrl(QLatin1String("file:///path/to/file.ext"))) @@ -637,7 +639,7 @@ void tst_QGalleryTrackerSchema::queryResponseRootType_data() << 1 << (QVector<QVariant>() << QLatin1String("uuid:ff172362-d959-99e0-a792-0ddafdd2c559") - << QLatin1String("file:///path/to/image.png") + << QUrl(QLatin1String("file:///path/to/image.png")) << 4) << "image::uuid:ff172362-d959-99e0-a792-0ddafdd2c559" << QVariant(QUrl(QLatin1String("file:///path/to/image.png"))) @@ -652,7 +654,7 @@ void tst_QGalleryTrackerSchema::queryResponseRootType_data() << 1 << (QVector<QVariant>() << QLatin1String("uuid:ff172362-d959-99e0-a792-0ddafdd2c559") - << QLatin1String("file:///path/to/text.txt") + << QUrl(QLatin1String("file:///path/to/text.txt")) << 7) << "text::uuid:ff172362-d959-99e0-a792-0ddafdd2c559" << QVariant(QUrl(QLatin1String("file:///path/to/text.txt"))) @@ -1731,6 +1733,21 @@ void tst_QGalleryTrackerSchema::queryResponseFilter_data() "} " "GROUP BY ?x"; } { + QGalleryFilter filter + = QDocumentGallery::url == QUrl::fromLocalFile(QString::fromUtf8("/path/to/K\xc3\xa4rp\xc3\xa4ssieni.jpg")); + + QTest::newRow("File.url == file:///path/to/K\xc3\xa4rp\xc3\xa4ssieni.jpg") + << "File" + << QString() + << QGalleryQueryRequest::AllDescendants + << filter + << "SELECT ?x nie:url(?x) rdf:type(?x) " + "WHERE {" + "{?x rdf:type nfo:FileDataObject}" + "FILTER((nie:url(?x)='file:///path/to/K%C3%A4rp%C3%A4ssieni.jpg'))" + "} " + "GROUP BY ?x"; + } { QGalleryFilter filter = QDocumentGallery::filePath == QLatin1String("/path/to/file.ext"); QTest::newRow("File.filePath == /path/to/file.ext") @@ -1745,6 +1762,21 @@ void tst_QGalleryTrackerSchema::queryResponseFilter_data() "} " "GROUP BY ?x"; } { + QGalleryFilter filter + = QDocumentGallery::filePath == QString::fromUtf8("/path/to/K\xc3\xa4rp\xc3\xa4ssieni.jpg"); + + QTest::newRow("File.filePath == /path/to/K\xc3\xa4rp\xc3\xa4ssieni.jpg") + << "File" + << QString() + << QGalleryQueryRequest::AllDescendants + << filter + << "SELECT ?x nie:url(?x) rdf:type(?x) " + "WHERE {" + "{?x rdf:type nfo:FileDataObject}" + "FILTER((nie:url(?x)='file:///path/to/K%C3%A4rp%C3%A4ssieni.jpg'))" + "} " + "GROUP BY ?x"; + } { QGalleryFilter filter = QDocumentGallery::filePath > QLatin1String("/path/to/file.ext"); QTest::newRow("File.filePath > /path/to/file.ext") @@ -2292,6 +2324,51 @@ void tst_QGalleryTrackerSchema::queryResponseFilter() QCOMPARE(arguments.sparql, sparql); } + +void tst_QGalleryTrackerSchema::queryResponseItemUrl_data() +{ + QTest::addColumn<QString>("rootType"); + QTest::addColumn<QByteArray>("encodedUrl"); + QTest::addColumn<QUrl>("url"); + + QTest::newRow("File ascii") + << "File" + << QByteArray("file://path/to/file.ext") + << QUrl(QLatin1String("file://path/to/file.ext")); + + QTest::newRow("Image encoded") + << "Image" + << QByteArray("file://path/to/K%C3%A4rp%C3%A4ssieni.jpg") + << QUrl(QString::fromUtf8("file://path/to/K\xc3\xa4rp\xc3\xa4ssieni.jpg")); +} + +void tst_QGalleryTrackerSchema::queryResponseItemUrl() +{ + QFETCH(QString, rootType); + QFETCH(QByteArray, encodedUrl); + QFETCH(QUrl, url); + + QGalleryTrackerResultSetArguments arguments; + + QGalleryTrackerSchema schema(rootType); + + QCOMPARE( + schema.prepareQueryResponse( + &arguments, + this, + QGalleryQueryRequest::AllDescendants, + QString(), + QGalleryFilter(), + QStringList(), + QStringList(), + 0, + 0), + QDocumentGallery::NoError); + + QCOMPARE(arguments.valueColumns.count(), 3); + QCOMPARE(arguments.valueColumns.at(1)->toVariant(encodedUrl).toUrl(), url); +} + void tst_QGalleryTrackerSchema::queryResponseValueColumnToVariant_data() { QTest::addColumn<QString>("rootType"); @@ -2568,7 +2645,7 @@ void tst_QGalleryTrackerSchema::queryResponseCompositeColumn_data() << QString::fromLatin1("filePath") << (QVector<QVariant>() << QLatin1String("uuid:ff172362-d959-99e0-a792-0ddafdd2c559") - << QLatin1String("file:///path/to/file.ext") + << QUrl(QLatin1String("file:///path/to/file.ext")) << QLatin1String("Files")) << QVariant(QLatin1String("/path/to/file.ext")); @@ -2577,7 +2654,7 @@ void tst_QGalleryTrackerSchema::queryResponseCompositeColumn_data() << QString::fromLatin1("path") << (QVector<QVariant>() << QLatin1String("uuid:ff172362-d959-99e0-a792-0ddafdd2c559") - << QLatin1String("file:///path/to/file.ext") + << QUrl(QLatin1String("file:///path/to/file.ext")) << QLatin1String("Files")) << QVariant(QLatin1String("/path/to")); @@ -2586,7 +2663,7 @@ void tst_QGalleryTrackerSchema::queryResponseCompositeColumn_data() << QString::fromLatin1("path") << (QVector<QVariant>() << QLatin1String("uuid:ff172362-d959-99e0-a792-0ddafdd2c559") - << QLatin1String("file:///path/to/") + << QUrl(QLatin1String("file:///path/to/")) << QLatin1String("Files")) << QVariant(QLatin1String("/path/to")); @@ -2595,7 +2672,7 @@ void tst_QGalleryTrackerSchema::queryResponseCompositeColumn_data() << QString::fromLatin1("fileExtension") << (QVector<QVariant>() << QLatin1String("uuid:ff172362-d959-99e0-a792-0ddafdd2c559") - << QLatin1String("file:///path/to/file.ext") + << QUrl(QLatin1String("file:///path/to/file.ext")) << QLatin1String("Files")) << QVariant(QLatin1String("ext")); @@ -2605,7 +2682,7 @@ void tst_QGalleryTrackerSchema::queryResponseCompositeColumn_data() << QString::fromLatin1("fileExtension") << (QVector<QVariant>() << QLatin1String("uuid:ff172362-d959-99e0-a792-0ddafdd2c559") - << QLatin1String("file:///path/to") + << QUrl(QLatin1String("file:///path/to")) << QLatin1String("Files")) << QVariant(); } |