Qt 4.8
Public Functions | Private Functions | Properties | Friends | List of all members
QDeclarativeImportDatabase Class Reference

The QDeclarativeImportDatabase class manages the QML imports for a QDeclarativeEngine. More...

#include <qdeclarativeimport_p.h>

Public Functions

void addImportPath (const QString &dir)
 
void addPluginPath (const QString &path)
 
QStringList importPathList () const
 
bool importPlugin (const QString &filePath, const QString &uri, QString *errorString)
 
QStringList pluginPathList () const
 
 QDeclarativeImportDatabase (QDeclarativeEngine *)
 
void setImportPathList (const QStringList &paths)
 
void setPluginPathList (const QStringList &paths)
 
 ~QDeclarativeImportDatabase ()
 

Private Functions

QString resolvePlugin (const QDir &qmldirPath, const QString &qmldirPluginPath, const QString &baseName, const QStringList &suffixes, const QString &prefix=QString())
 Returns the result of the merge of baseName with path, suffixes, and prefix. More...
 
QString resolvePlugin (const QDir &qmldirPath, const QString &qmldirPluginPath, const QString &baseName)
 Returns the result of the merge of baseName with dir and the platform suffix. More...
 

Properties

QDeclarativeEngineengine
 
QStringList fileImportPath
 
QStringList filePluginPath
 
QSet< QStringinitializedPlugins
 

Friends

class QDeclarativeImportsPrivate
 

Detailed Description

The QDeclarativeImportDatabase class manages the QML imports for a QDeclarativeEngine.

Warning
This function is not part of the public interface.

Definition at line 108 of file qdeclarativeimport_p.h.

Constructors and Destructors

◆ QDeclarativeImportDatabase()

QDeclarativeImportDatabase::QDeclarativeImportDatabase ( QDeclarativeEngine e)

Definition at line 746 of file qdeclarativeimport.cpp.

747 : engine(e)
748 {
750 
751  // Search order is applicationDirPath(), $QML_IMPORT_PATH, QLibraryInfo::ImportsPath
752 
753 #ifndef QT_NO_SETTINGS
755 
756 #if defined(Q_OS_SYMBIAN)
757  // Append imports path for all available drives in Symbian
758  if (installImportsPath.at(1) != QChar(QLatin1Char(':'))) {
759  QString tempPath = installImportsPath;
760  if (tempPath.at(tempPath.length() - 1) != QDir::separator()) {
761  tempPath += QDir::separator();
762  }
763  RFs& fs = qt_s60GetRFs();
764  TPtrC tempPathPtr(reinterpret_cast<const TText*> (tempPath.constData()));
765  // Symbian searches should start from Y:. Fix start drive otherwise TFindFile starts from the session drive
766  _LIT(KStartDir, "Y:");
767  TFileName dirPath(KStartDir);
768  dirPath.Append(tempPathPtr);
769  TFindFile finder(fs);
770  TInt err = finder.FindByDir(tempPathPtr, dirPath);
771  while (err == KErrNone) {
772  QString foundDir(reinterpret_cast<const QChar *>(finder.File().Ptr()),
773  finder.File().Length());
774  foundDir = QDir(foundDir).canonicalPath();
775  addImportPath(foundDir);
776  err = finder.Find();
777  }
778  // TFindFile found the directories in the order we want, but addImportPath reverses it.
779  // Reverse the order again to get it right.
781  } else {
782  addImportPath(installImportsPath);
783  }
784 #else
785  addImportPath(installImportsPath);
786 #endif
787 
788 #endif // QT_NO_SETTINGS
789 
790  // env import paths
791  QByteArray envImportPath = qgetenv("QML_IMPORT_PATH");
792  if (!envImportPath.isEmpty()) {
793 #if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN)
794  QLatin1Char pathSep(';');
795 #else
796  QLatin1Char pathSep(':');
797 #endif
798  QStringList paths = QString::fromLatin1(envImportPath).split(pathSep, QString::SkipEmptyParts);
799  for (int ii = paths.count() - 1; ii >= 0; --ii)
800  addImportPath(paths.at(ii));
801  }
802 
804 }
The QDir class provides access to directory structures and their contents.
Definition: qdir.h:58
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlist.h:267
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QChar separator()
Returns the native directory separator: "/" under Unix (including Mac OS X) and "\\" under Windows...
Definition: qdir.cpp:1831
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
static QString applicationDirPath()
Returns the directory that contains the application executable.
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:270
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
Q_OUTOFLINE_TEMPLATE void qReverse(RandomAccessIterator begin, RandomAccessIterator end)
Definition: qalgorithms.h:399
void addImportPath(const QString &dir)
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
static QString location(LibraryLocation)
Returns the location specified by loc.
QString canonicalPath() const
Returns the canonical path, i.e.
Definition: qdir.cpp:642
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
Splits the string into substrings wherever sep occurs, and returns the list of those strings...
Definition: qstring.cpp:6526
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition: qstring.h:712

◆ ~QDeclarativeImportDatabase()

QDeclarativeImportDatabase::~QDeclarativeImportDatabase ( )

Definition at line 806 of file qdeclarativeimport.cpp.

807 {
808 }

Functions

◆ addImportPath()

void QDeclarativeImportDatabase::addImportPath ( const QString path)
Warning
This function is not part of the public interface.

Definition at line 1016 of file qdeclarativeimport.cpp.

Referenced by QDeclarativeImportDatabase().

1017 {
1018  if (qmlImportTrace())
1019  qDebug().nospace() << "QDeclarativeImportDatabase::addImportPath: " << path;
1020 
1021  if (path.isEmpty())
1022  return;
1023 
1024  QUrl url = QUrl(path);
1025  QString cPath;
1026 
1027  if (url.isRelative() || url.scheme() == QLatin1String("file")
1028  || (url.scheme().length() == 1 && QFile::exists(path)) ) { // windows path
1029  QDir dir = QDir(path);
1030  cPath = dir.canonicalPath();
1031  } else {
1032  cPath = path;
1033  cPath.replace(QLatin1Char('\\'), QLatin1Char('/'));
1034  }
1035 
1036  if (!cPath.isEmpty()
1037  && !fileImportPath.contains(cPath))
1038  fileImportPath.prepend(cPath);
1039 }
The QDir class provides access to directory structures and their contents.
Definition: qdir.h:58
QString & replace(int i, int len, QChar after)
Definition: qstring.cpp:2005
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isRelative() const
Returns true if the URL is relative; otherwise returns false.
Definition: qurl.cpp:5880
bool exists() const
Returns true if the file specified by fileName() exists; otherwise returns false. ...
Definition: qfile.cpp:626
Q_CORE_EXPORT void qDebug(const char *,...)
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
void prepend(const T &t)
Inserts value at the beginning of the list.
Definition: qlist.h:541
QBool contains(const QString &str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the list contains the string str; otherwise returns false.
Definition: qstringlist.h:172
QString scheme() const
Returns the scheme of the URL.
Definition: qurl.cpp:4550
QString canonicalPath() const
Returns the canonical path, i.e.
Definition: qdir.cpp:642
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ addPluginPath()

void QDeclarativeImportDatabase::addPluginPath ( const QString path)
Warning
This function is not part of the public interface.

Definition at line 998 of file qdeclarativeimport.cpp.

999 {
1000  if (qmlImportTrace())
1001  qDebug().nospace() << "QDeclarativeImportDatabase::addPluginPath: " << path;
1002 
1003  QUrl url = QUrl(path);
1004  if (url.isRelative() || url.scheme() == QLatin1String("file")
1005  || (url.scheme().length() == 1 && QFile::exists(path)) ) { // windows path
1006  QDir dir = QDir(path);
1008  } else {
1009  filePluginPath.prepend(path);
1010  }
1011 }
The QDir class provides access to directory structures and their contents.
Definition: qdir.h:58
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
bool isRelative() const
Returns true if the URL is relative; otherwise returns false.
Definition: qurl.cpp:5880
bool exists() const
Returns true if the file specified by fileName() exists; otherwise returns false. ...
Definition: qfile.cpp:626
Q_CORE_EXPORT void qDebug(const char *,...)
void prepend(const T &t)
Inserts value at the beginning of the list.
Definition: qlist.h:541
QString scheme() const
Returns the scheme of the URL.
Definition: qurl.cpp:4550
QString canonicalPath() const
Returns the canonical path, i.e.
Definition: qdir.cpp:642

◆ importPathList()

QStringList QDeclarativeImportDatabase::importPathList ( ) const
Warning
This function is not part of the public interface.

Definition at line 1044 of file qdeclarativeimport.cpp.

1045 {
1046  return fileImportPath;
1047 }

◆ importPlugin()

bool QDeclarativeImportDatabase::importPlugin ( const QString filePath,
const QString uri,
QString errorString 
)
Warning
This function is not part of the public interface.

Definition at line 1060 of file qdeclarativeimport.cpp.

Referenced by QDeclarativeImportsPrivate::importExtension().

1061 {
1062  if (qmlImportTrace())
1063  qDebug().nospace() << "QDeclarativeImportDatabase::importPlugin: " << uri << " from " << filePath;
1064 
1065 #ifndef QT_NO_LIBRARY
1066  QFileInfo fileInfo(filePath);
1067  const QString absoluteFilePath = fileInfo.absoluteFilePath();
1068 
1069  bool engineInitialized = initializedPlugins.contains(absoluteFilePath);
1070  bool typesRegistered = qmlEnginePluginsWithRegisteredTypes()->contains(absoluteFilePath);
1071 
1072  if (typesRegistered) {
1073  Q_ASSERT_X(qmlEnginePluginsWithRegisteredTypes()->value(absoluteFilePath) == uri,
1074  "QDeclarativeImportDatabase::importExtension",
1075  "Internal error: Plugin imported previously with different uri");
1076  }
1077 
1078  if (!engineInitialized || !typesRegistered) {
1079  if (!QDeclarative_isFileCaseCorrect(absoluteFilePath)) {
1080  if (errorString)
1081  *errorString = tr("File name case mismatch for \"%1\"").arg(absoluteFilePath);
1082  return false;
1083  }
1084  QPluginLoader loader(absoluteFilePath);
1085 
1086  if (!loader.load()) {
1087  if (errorString)
1088  *errorString = loader.errorString();
1089  return false;
1090  }
1091 
1092  if (QDeclarativeExtensionInterface *iface = qobject_cast<QDeclarativeExtensionInterface *>(loader.instance())) {
1093 
1094  const QByteArray bytes = uri.toUtf8();
1095  const char *moduleId = bytes.constData();
1096  if (!typesRegistered) {
1097 
1098  // ### this code should probably be protected with a mutex.
1099  qmlEnginePluginsWithRegisteredTypes()->insert(absoluteFilePath, uri);
1100  iface->registerTypes(moduleId);
1101  }
1102  if (!engineInitialized) {
1103  // things on the engine (eg. adding new global objects) have to be done for every engine.
1104 
1105  // protect against double initialization
1106  initializedPlugins.insert(absoluteFilePath);
1107  iface->initializeEngine(engine, moduleId);
1108  }
1109  } else {
1110  if (errorString)
1111  *errorString = loader.errorString();
1112  return false;
1113  }
1114  }
1115 
1116  return true;
1117 #else
1118  return false;
1119 #endif
1120 }
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
The QString class provides a Unicode character string.
Definition: qstring.h:83
Q_CORE_EXPORT void qDebug(const char *,...)
bool QDeclarative_isFileCaseCorrect(const QString &fileName)
Returns true if the case of fileName is equivalent to the file case of fileName on disk...
bool contains(const T &value) const
Definition: qset.h:91
const_iterator insert(const T &value)
Definition: qset.h:179
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
The QPluginLoader class loads a plugin at run-time.
Definition: qpluginloader.h:62

◆ pluginPathList()

QStringList QDeclarativeImportDatabase::pluginPathList ( ) const
Warning
This function is not part of the public interface.

Definition at line 982 of file qdeclarativeimport.cpp.

983 {
984  return filePluginPath;
985 }

◆ resolvePlugin() [1/2]

QString QDeclarativeImportDatabase::resolvePlugin ( const QDir qmldirPath,
const QString qmldirPluginPath,
const QString baseName,
const QStringList suffixes,
const QString prefix = QString() 
)
private

Returns the result of the merge of baseName with path, suffixes, and prefix.

Warning
This function is not part of the public interface.

The prefix must contain the dot.

qmldirPath is the location of the qmldir file.

Definition at line 853 of file qdeclarativeimport.cpp.

Referenced by QDeclarativeImportsPrivate::importExtension(), and resolvePlugin().

856 {
857  QStringList searchPaths = filePluginPath;
858  bool qmldirPluginPathIsRelative = QDir::isRelativePath(qmldirPluginPath);
859  if (!qmldirPluginPathIsRelative)
860  searchPaths.prepend(qmldirPluginPath);
861 
862  foreach (const QString &pluginPath, searchPaths) {
863 
864  QString resolvedPath;
865 
866  if (pluginPath == QLatin1String(".")) {
867  if (qmldirPluginPathIsRelative)
868  resolvedPath = qmldirPath.absoluteFilePath(qmldirPluginPath);
869  else
870  resolvedPath = qmldirPath.absolutePath();
871  } else {
872  resolvedPath = pluginPath;
873  }
874 
875  // hack for resources, should probably go away
876  if (resolvedPath.startsWith(QLatin1Char(':')))
877  resolvedPath = QCoreApplication::applicationDirPath();
878 
879  QDir dir(resolvedPath);
880  foreach (const QString &suffix, suffixes) {
881  QString pluginFileName = prefix;
882 
883  pluginFileName += baseName;
884  pluginFileName += suffix;
885 
886  QFileInfo fileInfo(dir, pluginFileName);
887 
888  if (fileInfo.exists())
889  return fileInfo.absoluteFilePath();
890  }
891  }
892 
893  if (qmlImportTrace())
894  qDebug() << "QDeclarativeImportDatabase::resolvePlugin: Could not resolve plugin" << baseName
895  << "in" << qmldirPath.absolutePath();
896 
897  return QString();
898 }
The QDir class provides access to directory structures and their contents.
Definition: qdir.h:58
QString absoluteFilePath(const QString &fileName) const
Returns the absolute path name of a file in the directory.
Definition: qdir.cpp:701
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
static bool isRelativePath(const QString &path)
Returns true if path is relative; returns false if it is absolute.
Definition: qdir.cpp:2210
The QString class provides a Unicode character string.
Definition: qstring.h:83
Q_CORE_EXPORT void qDebug(const char *,...)
static QString applicationDirPath()
Returns the directory that contains the application executable.
QString absolutePath() const
Returns the absolute path (a path that starts with "/" or with a drive specification), which may contain symbolic links, but never contains redundant ".", ".." or multiple separators.
Definition: qdir.cpp:619
void prepend(const T &t)
Inserts value at the beginning of the list.
Definition: qlist.h:541
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QByteArray suffix
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ resolvePlugin() [2/2]

QString QDeclarativeImportDatabase::resolvePlugin ( const QDir qmldirPath,
const QString qmldirPluginPath,
const QString baseName 
)
private

Returns the result of the merge of baseName with dir and the platform suffix.

Warning
This function is not part of the public interface.
Platform Valid suffixes
Windows .dll
Unix/Linux .so
AIX .a
HP-UX .sl, .so (HP-UXi)
Mac OS X .dylib, .bundle, .so
Symbian .dll

Version number on unix are ignored.

Definition at line 920 of file qdeclarativeimport.cpp.

922 {
923 #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
924  return resolvePlugin(qmldirPath, qmldirPluginPath, baseName,
925  QStringList()
926 # ifdef QT_DEBUG
927  << QLatin1String("d.dll") // try a qmake-style debug build first
928 # endif
929  << QLatin1String(".dll"));
930 #elif defined(Q_OS_SYMBIAN)
931  return resolvePlugin(qmldirPath, qmldirPluginPath, baseName,
932  QStringList()
933  << QLatin1String(".dll")
934  << QLatin1String(".qtplugin"));
935 #else
936 
937 # if defined(Q_OS_DARWIN)
938 
939  return resolvePlugin(qmldirPath, qmldirPluginPath, baseName,
940  QStringList()
941 # ifdef QT_DEBUG
942  << QLatin1String("_debug.dylib") // try a qmake-style debug build first
943  << QLatin1String(".dylib")
944 # else
945  << QLatin1String(".dylib")
946  << QLatin1String("_debug.dylib") // try a qmake-style debug build after
947 # endif
948  << QLatin1String(".so")
949  << QLatin1String(".bundle"),
950  QLatin1String("lib"));
951 # else // Generic Unix
952  QStringList validSuffixList;
953 
954 # if defined(Q_OS_HPUX)
955 /*
956  See "HP-UX Linker and Libraries User's Guide", section "Link-time Differences between PA-RISC and IPF":
957  "In PA-RISC (PA-32 and PA-64) shared libraries are suffixed with .sl. In IPF (32-bit and 64-bit),
958  the shared libraries are suffixed with .so. For compatibility, the IPF linker also supports the .sl suffix."
959  */
960  validSuffixList << QLatin1String(".sl");
961 # if defined __ia64
962  validSuffixList << QLatin1String(".so");
963 # endif
964 # elif defined(Q_OS_AIX)
965  validSuffixList << QLatin1String(".a") << QLatin1String(".so");
966 # elif defined(Q_OS_UNIX)
967  validSuffixList << QLatin1String(".so");
968 # endif
969 
970  // Examples of valid library names:
971  // libfoo.so
972 
973  return resolvePlugin(qmldirPath, qmldirPluginPath, baseName, validSuffixList, QLatin1String("lib"));
974 # endif
975 
976 #endif
977 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
#define QT_DEBUG
Definition: qglobal.h:1746
QString resolvePlugin(const QDir &qmldirPath, const QString &qmldirPluginPath, const QString &baseName, const QStringList &suffixes, const QString &prefix=QString())
Returns the result of the merge of baseName with path, suffixes, and prefix.

◆ setImportPathList()

void QDeclarativeImportDatabase::setImportPathList ( const QStringList paths)
Warning
This function is not part of the public interface.

Definition at line 1052 of file qdeclarativeimport.cpp.

1053 {
1054  fileImportPath = paths;
1055 }

◆ setPluginPathList()

void QDeclarativeImportDatabase::setPluginPathList ( const QStringList paths)
Warning
This function is not part of the public interface.

Definition at line 990 of file qdeclarativeimport.cpp.

991 {
992  filePluginPath = paths;
993 }

Friends and Related Functions

◆ QDeclarativeImportsPrivate

Definition at line 126 of file qdeclarativeimport_p.h.

Properties

◆ engine

QDeclarativeEngine* QDeclarativeImportDatabase::engine
private

Definition at line 138 of file qdeclarativeimport_p.h.

Referenced by importPlugin().

◆ fileImportPath

QStringList QDeclarativeImportDatabase::fileImportPath
private

◆ filePluginPath

QStringList QDeclarativeImportDatabase::filePluginPath
private

◆ initializedPlugins

QSet<QString> QDeclarativeImportDatabase::initializedPlugins
private

Definition at line 137 of file qdeclarativeimport_p.h.

Referenced by importPlugin().


The documentation for this class was generated from the following files: