Introspect qrc files in FolderListModel.
authorGatis Paeglis <gatis.paeglis@digia.com>
Tue, 12 Aug 2014 12:46:05 +0000 (14:46 +0200)
committerGatis Paeglis <gatis.paeglis@digia.com>
Fri, 22 Aug 2014 21:00:37 +0000 (23:00 +0200)
And don't use "file watchers" for resource file paths,
otherwise it generates the following warning:

QInotifyFileSystemWatcherEngine::addPaths: inotify_add_watch failed: No
such file or directory

Change-Id: I6b75c9195fb2b2ba7b3e0bb7d146fc5cd343927e
Task-number: QTBUG-40307
Reviewed-by: Shawn Rutledge <shawn.rutledge@digia.com>
src/imports/folderlistmodel/fileinfothread.cpp
src/imports/folderlistmodel/qquickfolderlistmodel.cpp
tests/auto/qml/qquickfolderlistmodel/data/introspect.qrc [new file with mode: 0644]
tests/auto/qml/qquickfolderlistmodel/data/qrc.qml [new file with mode: 0644]
tests/auto/qml/qquickfolderlistmodel/data/txtdir/hello.txt [new file with mode: 0644]
tests/auto/qml/qquickfolderlistmodel/qquickfolderlistmodel.pro
tests/auto/qml/qquickfolderlistmodel/tst_qquickfolderlistmodel.cpp

index f7587c2..2d53b21 100644 (file)
@@ -92,7 +92,8 @@ void FileInfoThread::removePath(const QString &path)
 {
     QMutexLocker locker(&mutex);
 #ifndef QT_NO_FILESYSTEMWATCHER
-    watcher->removePath(path);
+    if (!path.startsWith(QLatin1Char(':')))
+        watcher->removePath(path);
 #endif
     currentPath.clear();
 }
@@ -103,7 +104,8 @@ void FileInfoThread::setPath(const QString &path)
 
     QMutexLocker locker(&mutex);
 #ifndef QT_NO_FILESYSTEMWATCHER
-    watcher->addPath(path);
+    if (!path.startsWith(QLatin1Char(':')))
+        watcher->addPath(path);
 #endif
     currentPath = path;
     needUpdate = true;
index 5d84d7e..5fc9d98 100644 (file)
@@ -536,8 +536,8 @@ void QQuickFolderListModel::classBegin()
 void QQuickFolderListModel::componentComplete()
 {
     Q_D(QQuickFolderListModel);
-
-    if (!d->currentDir.isValid() || !d->currentDir.isLocalFile() || !QDir().exists(d->currentDir.toLocalFile()))
+    QString localPath = QQmlFile::urlToLocalFileOrQrc(d->currentDir);
+    if (localPath.isEmpty() || !QDir(localPath).exists())
         setFolder(QUrl::fromLocalFile(QDir::currentPath()));
 }
 
diff --git a/tests/auto/qml/qquickfolderlistmodel/data/introspect.qrc b/tests/auto/qml/qquickfolderlistmodel/data/introspect.qrc
new file mode 100644 (file)
index 0000000..9f07597
--- /dev/null
@@ -0,0 +1,5 @@
+<RCC>
+<qresource prefix="/myprefix/subdir">
+<file>txtdir/hello.txt</file>
+</qresource>
+</RCC>
diff --git a/tests/auto/qml/qquickfolderlistmodel/data/qrc.qml b/tests/auto/qml/qquickfolderlistmodel/data/qrc.qml
new file mode 100644 (file)
index 0000000..30bc290
--- /dev/null
@@ -0,0 +1,7 @@
+import Qt.labs.folderlistmodel 2.1
+
+FolderListModel {
+     nameFilters: [ "*.txt" ]
+     folder: "qrc:/myprefix/subdir/txtdir"
+}
+
diff --git a/tests/auto/qml/qquickfolderlistmodel/data/txtdir/hello.txt b/tests/auto/qml/qquickfolderlistmodel/data/txtdir/hello.txt
new file mode 100644 (file)
index 0000000..87186bd
--- /dev/null
@@ -0,0 +1 @@
+Hello from hello.txt
index d14f4fb..c4fdcdc 100644 (file)
@@ -11,3 +11,5 @@ TESTDATA = data/*
 CONFIG += parallel_test
 QT += core-private gui-private qml-private testlib
 DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0
+
+RESOURCES += data/introspect.qrc
index 4296ae4..17367ba 100644 (file)
@@ -83,6 +83,7 @@ private slots:
     void showDotAndDotDot();
     void showDotAndDotDot_data();
     void sortReversed();
+    void introspectQrc();
 
 private:
     void checkNoErrors(const QQmlComponent& component);
@@ -127,7 +128,7 @@ void tst_qquickfolderlistmodel::basicProperties()
     QSignalSpy folderChangedSpy(flm, SIGNAL(folderChanged()));
     flm->setProperty("folder", dataDirectoryUrl());
     QVERIFY(folderChangedSpy.wait());
-    QCOMPARE(flm->property("count").toInt(), 6);
+    QCOMPARE(flm->property("count").toInt(), 8);
     QCOMPARE(flm->property("folder").toUrl(), dataDirectoryUrl());
     QCOMPARE(flm->property("parentFolder").toUrl(), QUrl::fromLocalFile(QDir(directory()).canonicalPath()));
     QCOMPARE(flm->property("sortField").toInt(), int(Name));
@@ -153,12 +154,12 @@ void tst_qquickfolderlistmodel::showFiles()
     QVERIFY(flm != 0);
 
     flm->setProperty("folder", dataDirectoryUrl());
-    QTRY_COMPARE(flm->property("count").toInt(), 6); // wait for refresh
+    QTRY_COMPARE(flm->property("count").toInt(), 8); // wait for refresh
     QCOMPARE(flm->property("showFiles").toBool(), true);
 
     flm->setProperty("showFiles", false);
     QCOMPARE(flm->property("showFiles").toBool(), false);
-    QTRY_COMPARE(flm->property("count").toInt(), 1); // wait for refresh
+    QTRY_COMPARE(flm->property("count").toInt(), 2); // wait for refresh
 }
 
 void tst_qquickfolderlistmodel::resetFiltering()
@@ -225,7 +226,7 @@ void tst_qquickfolderlistmodel::refresh()
     QVERIFY(flm != 0);
 
     flm->setProperty("folder", dataDirectoryUrl());
-    QTRY_COMPARE(flm->property("count").toInt(),6); // wait for refresh
+    QTRY_COMPARE(flm->property("count").toInt(),8); // wait for refresh
 
     int count = flm->rowCount();
 
@@ -329,7 +330,7 @@ void tst_qquickfolderlistmodel::showDotAndDotDot()
     flm->setProperty("rootFolder", rootFolder);
     flm->setProperty("showDotAndDotDot", showDotAndDotDot);
 
-    int count = 6;
+    int count = 9;
     if (showDot) count++;
     if (showDotDot) count++;
     QTRY_COMPARE(flm->property("count").toInt(), count); // wait for refresh
@@ -360,10 +361,18 @@ void tst_qquickfolderlistmodel::sortReversed()
     QAbstractListModel *flm = qobject_cast<QAbstractListModel*>(component.create());
     QVERIFY(flm != 0);
     flm->setProperty("folder", dataDirectoryUrl());
+    QTRY_COMPARE(flm->property("count").toInt(), 9); // wait for refresh
+    QCOMPARE(flm->data(flm->index(0),FileNameRole).toString(), QLatin1String("txtdir"));
+}
 
-    int count = 6;
-    QTRY_COMPARE(flm->property("count").toInt(), count); // wait for refresh
-    QCOMPARE(flm->data(flm->index(0),FileNameRole).toString(), QLatin1String("sortReversed.qml"));
+void tst_qquickfolderlistmodel::introspectQrc()
+{
+    QQmlComponent component(&engine, testFileUrl("qrc.qml"));
+    checkNoErrors(component);
+    QAbstractListModel *flm = qobject_cast<QAbstractListModel*>(component.create());
+    QVERIFY(flm != 0);
+    QTRY_COMPARE(flm->property("count").toInt(), 1); // wait for refresh
+    QCOMPARE(flm->data(flm->index(0),FileNameRole).toString(), QLatin1String("hello.txt"));
 }
 
 QTEST_MAIN(tst_qquickfolderlistmodel)