mirror of
https://github.com/QuasarApp/installer-framework.git
synced 2025-05-07 18:49:34 +00:00
Add findFiles method
Task-number: QTIFW-1094 Change-Id: Ibc37e9b568f7f54e37f6ed6a5b040940cab5aebd Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io> Reviewed-by: Iikka Eklund <iikka.eklund@qt.io>
This commit is contained in:
parent
3f281b049c
commit
6664ca85f0
doc/scripting-api
src/libs/installer
tests/auto/installer/scriptengine
@ -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.
|
||||
*/
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user