diff options
-rw-r--r-- | doc/scripting-api/qdesktopservices.qdoc | 8 | ||||
-rw-r--r-- | src/libs/installer/scriptengine.cpp | 24 | ||||
-rw-r--r-- | src/libs/installer/scriptengine_p.h | 10 | ||||
-rw-r--r-- | tests/auto/installer/scriptengine/tst_scriptengine.cpp | 28 |
4 files changed, 68 insertions, 2 deletions
diff --git a/doc/scripting-api/qdesktopservices.qdoc b/doc/scripting-api/qdesktopservices.qdoc index 037a0b6dd..7c2b31ea3 100644 --- a/doc/scripting-api/qdesktopservices.qdoc +++ b/doc/scripting-api/qdesktopservices.qdoc @@ -99,3 +99,11 @@ Returns the specified \a location. */ + +/*! + \qmlmethod array QDesktopServices::findFiles(string path, string pattern) + + Returns file names matching \a pattern. Searches the files recursively from \a path. + \a Pattern understands * and ? wildcards. +*/ + diff --git a/src/libs/installer/scriptengine.cpp b/src/libs/installer/scriptengine.cpp index 28f91a394..4a3c163d2 100644 --- a/src/libs/installer/scriptengine.cpp +++ b/src/libs/installer/scriptengine.cpp @@ -72,6 +72,28 @@ QJSValue InstallerProxy::componentByName(const QString &componentName) return QJSValue(); } +QJSValue QDesktopServicesProxy::findFiles(const QString &path, const QString &pattern) +{ + QStringList result; + findRecursion(path, pattern, &result); + + QJSValue scriptComponentsObject = m_engine->newArray(result.count()); + for (int i = 0; i < result.count(); ++i) { + scriptComponentsObject.setProperty(i, result.at(i)); + } + return scriptComponentsObject; +} + +void QDesktopServicesProxy::findRecursion(const QString &path, const QString &pattern, QStringList *result) +{ + QDir currentDir(path); + const QString prefix = path + QLatin1Char('/'); + foreach (const QString &match, currentDir.entryList(QStringList(pattern), QDir::Files | QDir::NoSymLinks)) + result->append(prefix + match); + foreach (const QString &dir, currentDir.entryList(QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot)) + findRecursion(prefix + dir, pattern, result); +} + GuiProxy::GuiProxy(ScriptEngine *engine, QObject *parent) : QObject(parent), m_engine(engine), @@ -526,7 +548,7 @@ QJSValue ScriptEngine::generateDesktopServicesObject() SETPROPERTY(desktopServices, GenericCacheLocation, QStandardPaths) SETPROPERTY(desktopServices, GenericConfigLocation, QStandardPaths) - QJSValue object = m_engine.newQObject(new QDesktopServicesProxy); + QJSValue object = m_engine.newQObject(new QDesktopServicesProxy(this)); object.setPrototype(desktopServices); // attach the properties return object; } diff --git a/src/libs/installer/scriptengine_p.h b/src/libs/installer/scriptengine_p.h index 7af5cbab8..adbe2dc0c 100644 --- a/src/libs/installer/scriptengine_p.h +++ b/src/libs/installer/scriptengine_p.h @@ -93,7 +93,8 @@ class QDesktopServicesProxy : public QObject Q_DISABLE_COPY(QDesktopServicesProxy) public: - QDesktopServicesProxy() {} + QDesktopServicesProxy(ScriptEngine *engine) + : m_engine(engine){} public slots : bool openUrl(const QString &url) const { @@ -108,6 +109,13 @@ public slots : QString storageLocation(qint32 location) const { return QStandardPaths::writableLocation(QStandardPaths::StandardLocation(location)); } + QJSValue findFiles(const QString &path, const QString &pattern); + +private: + void findRecursion(const QString &path, const QString &pattern, QStringList *result); + +private: + ScriptEngine *m_engine; }; #if QT_VERSION < 0x050400 diff --git a/tests/auto/installer/scriptengine/tst_scriptengine.cpp b/tests/auto/installer/scriptengine/tst_scriptengine.cpp index 2049e6323..7118d067d 100644 --- a/tests/auto/installer/scriptengine/tst_scriptengine.cpp +++ b/tests/auto/installer/scriptengine/tst_scriptengine.cpp @@ -198,6 +198,7 @@ private slots: m_core.appendRootComponent(component); m_scriptEngine = m_core.componentScriptEngine(); + m_applicatonDirPath = qApp->applicationDirPath(); } void testDefaultScriptEngineValues() @@ -234,6 +235,8 @@ private slots: .hasProperty(QLatin1String("displayName")), true); QCOMPARE(global.property(QLatin1String("QDesktopServices")) .hasProperty(QLatin1String("storageLocation")), true); + QCOMPARE(global.property(QLatin1String("QDesktopServices")) + .hasProperty(QLatin1String("findFiles")), true); QCOMPARE(global.hasProperty(QLatin1String("buttons")), true); QCOMPARE(global.hasProperty(QLatin1String("QInstaller")), true); @@ -342,6 +345,30 @@ private slots: } } + void testFindFiles() + { + const QString expectedOutput = QString::fromLatin1("Found file %1/tst_scriptengine.moc").arg(m_applicatonDirPath); + QByteArray array = expectedOutput.toLatin1(); + const char *c_str2 = array.data(); + + setExpectedScriptOutput(c_str2); + const QString script = QString::fromLatin1("var directory = \"C:/Qt/test\";" + "\n" + "var pattern = \"*.moc\";" + "\n" + "var fileArray = QDesktopServices.findFiles('%1', pattern)" + "\n" + "for (i = 0; i < fileArray.length; i++) {" + "print(\"Found file \"+fileArray[i]);" + "}").arg(m_applicatonDirPath); + const QJSValue result = m_scriptEngine->evaluate(script); + qDebug()<<result.isArray(); + qDebug()<<result.isObject(); + qDebug()<<result.isString(); + qDebug()<<result.isVariant(); + QCOMPARE(result.isError(), false); + } + void loadSimpleComponentScript() { try { @@ -585,6 +612,7 @@ private: PackageManagerCore m_core; Component *m_component; ScriptEngine *m_scriptEngine; + QString m_applicatonDirPath; }; |