diff options
author | Kent Hansen <khansen@trolltech.com> | 2009-07-06 15:04:56 (GMT) |
---|---|---|
committer | Kent Hansen <khansen@trolltech.com> | 2009-07-06 15:04:56 (GMT) |
commit | bd98c3e2f7117c9538c1c81e95e866358c92f44a (patch) | |
tree | d42cfa3c3b3f340674d56fcf0f5900128f471b1d | |
parent | 292f72349a9ff74320f29b12f725bad6c8c283c4 (diff) | |
download | Qt-bd98c3e2f7117c9538c1c81e95e866358c92f44a.zip Qt-bd98c3e2f7117c9538c1c81e95e866358c92f44a.tar.gz Qt-bd98c3e2f7117c9538c1c81e95e866358c92f44a.tar.bz2 |
implement QScriptEngine::availableExtensions()
-rw-r--r-- | src/script/api/qscriptengine.cpp | 74 | ||||
-rw-r--r-- | src/script/api/qscriptengine_p.h | 4 |
2 files changed, 75 insertions, 3 deletions
diff --git a/src/script/api/qscriptengine.cpp b/src/script/api/qscriptengine.cpp index 87f8e1c..48142bb 100644 --- a/src/script/api/qscriptengine.cpp +++ b/src/script/api/qscriptengine.cpp @@ -44,6 +44,12 @@ #ifndef QT_NO_QOBJECT #include <QtCore/qcoreapplication.h> +#include <QtCore/qdir.h> +#include <QtCore/qfile.h> +#include <QtCore/qfileinfo.h> +#include <QtCore/qpluginloader.h> +#include <QtCore/qset.h> +#include "qscriptextensioninterface.h" #endif Q_DECLARE_METATYPE(QScriptValue) @@ -2519,8 +2525,68 @@ QScriptValue QScriptEngine::importExtension(const QString &extension) */ QStringList QScriptEngine::availableExtensions() const { - qWarning("QScriptEngine::availableExtensions() not implemented"); +#if defined(QT_NO_QOBJECT) || defined(QT_NO_LIBRARY) || defined(QT_NO_SETTINGS) return QStringList(); +#else + QCoreApplication *app = QCoreApplication::instance(); + if (!app) + return QStringList(); + + QSet<QString> result; + + QObjectList staticPlugins = QPluginLoader::staticInstances(); + for (int i = 0; i < staticPlugins.size(); ++i) { + QScriptExtensionInterface *iface; + iface = qobject_cast<QScriptExtensionInterface*>(staticPlugins.at(i)); + if (iface) { + QStringList keys = iface->keys(); + for (int j = 0; j < keys.count(); ++j) + result << keys.at(j); + } + } + + QStringList libraryPaths = app->libraryPaths(); + for (int i = 0; i < libraryPaths.count(); ++i) { + QString libPath = libraryPaths.at(i) + QDir::separator() + QLatin1String("script"); + QDir dir(libPath); + if (!dir.exists()) + continue; + + // look for C++ plugins + QFileInfoList files = dir.entryInfoList(QDir::Files); + for (int j = 0; j < files.count(); ++j) { + QFileInfo entry = files.at(j); + QString filePath = entry.canonicalFilePath(); + QPluginLoader loader(filePath); + QScriptExtensionInterface *iface; + iface = qobject_cast<QScriptExtensionInterface*>(loader.instance()); + if (iface) { + QStringList keys = iface->keys(); + for (int k = 0; k < keys.count(); ++k) + result << keys.at(k); + } + } + + // look for scripts + QString initDotJs = QLatin1String("__init__.js"); + QList<QFileInfo> stack; + stack << dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot); + while (!stack.isEmpty()) { + QFileInfo entry = stack.takeLast(); + QDir dd(entry.canonicalFilePath()); + if (dd.exists(initDotJs)) { + QString rpath = dir.relativeFilePath(dd.canonicalPath()); + QStringList components = rpath.split(QLatin1Char('/')); + result << components.join(QLatin1String(".")); + stack << dd.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot); + } + } + } + + QStringList lst = result.toList(); + qSort(lst); + return lst; +#endif } /*! @@ -2533,8 +2599,10 @@ QStringList QScriptEngine::availableExtensions() const */ QStringList QScriptEngine::importedExtensions() const { - qWarning("QScriptEngine::importedExtensions() not implemented"); - return QStringList(); + Q_D(const QScriptEngine); + QStringList lst = d->importedExtensions.toList(); + qSort(lst); + return lst; } /*! \fn QScriptValue QScriptEngine::toScriptValue(const T &value) diff --git a/src/script/api/qscriptengine_p.h b/src/script/api/qscriptengine_p.h index c828162..8b5133b 100644 --- a/src/script/api/qscriptengine_p.h +++ b/src/script/api/qscriptengine_p.h @@ -28,6 +28,7 @@ #ifndef QT_NO_SCRIPT #include <QtCore/qhash.h> +#include <QtCore/qset.h> #include "RefPtr.h" #include "Structure.h" @@ -158,6 +159,9 @@ public: QHash<int, QScriptTypeInfo*> m_typeInfos; int processEventsInterval; + QSet<QString> importedExtensions; + QSet<QString> extensionsBeingImported; + #ifndef QT_NO_QOBJECT QHash<QObject*, QScript::QObjectData*> m_qobjectData; #endif |