diff options
-rw-r--r-- | src/imports/localstorage/plugin.cpp | 37 | ||||
-rw-r--r-- | src/qml/qml/qqmlengine.cpp | 21 | ||||
-rw-r--r-- | src/qml/qml/qqmlengine.h | 1 | ||||
-rw-r--r-- | src/qml/qml/qqmlengine_p.h | 1 | ||||
-rw-r--r-- | tests/auto/qml/qqmlengine/tst_qqmlengine.cpp | 29 |
5 files changed, 60 insertions, 29 deletions
diff --git a/src/imports/localstorage/plugin.cpp b/src/imports/localstorage/plugin.cpp index d3ea93c80a..433b7fe430 100644 --- a/src/imports/localstorage/plugin.cpp +++ b/src/imports/localstorage/plugin.cpp @@ -219,24 +219,6 @@ QQmlSqlDatabaseData::~QQmlSqlDatabaseData() { } -static QString qmlsqldatabase_databasesPath(QV4::ExecutionEngine *engine) -{ - return engine->qmlEngine()->offlineStoragePath() + - QDir::separator() + QLatin1String("Databases"); -} - -static void qmlsqldatabase_initDatabasesPath(QV4::ExecutionEngine *engine) -{ - QString databasesPath = qmlsqldatabase_databasesPath(engine); - if (!QDir().mkpath(databasesPath)) - qWarning() << "LocalStorage: can't create path - " << databasesPath; -} - -static QString qmlsqldatabase_databaseFile(const QString& connectionName, QV4::ExecutionEngine *engine) -{ - return qmlsqldatabase_databasesPath(engine) + QDir::separator() + connectionName; -} - static ReturnedValue qmlsqldatabase_rows_index(const QQmlSqlDatabaseWrapper *r, ExecutionEngine *v4, quint32 index, bool *hasProperty = 0) { Scope scope(v4); @@ -450,7 +432,8 @@ static ReturnedValue qmlsqldatabase_changeVersion(CallContext *ctx) if (ok) { *w->d()->version = to_version; #if QT_CONFIG(settings) - QSettings ini(qmlsqldatabase_databaseFile(db.connectionName(), scope.engine) + QLatin1String(".ini"), QSettings::IniFormat); + const QQmlEnginePrivate *enginePrivate = QQmlEnginePrivate::get(scope.engine->qmlEngine()); + QSettings ini(enginePrivate->offlineStorageDatabaseDirectory() + db.connectionName() + QLatin1String(".ini"), QSettings::IniFormat); ini.setValue(QLatin1String("Version"), to_version); #endif } @@ -723,24 +706,20 @@ void QQuickLocalStorage::openDatabaseSync(QQmlV4Function *args) if (scope.engine->qmlEngine()->offlineStoragePath().isEmpty()) V4THROW_SQL2(SQLEXCEPTION_DATABASE_ERR, QQmlEngine::tr("SQL: can't create database, offline storage is disabled.")); - qmlsqldatabase_initDatabasesPath(scope.engine); - - QSqlDatabase database; - QV4::ScopedValue v(scope); QString dbname = (v = (*args)[0])->toQStringNoThrow(); QString dbversion = (v = (*args)[1])->toQStringNoThrow(); QString dbdescription = (v = (*args)[2])->toQStringNoThrow(); int dbestimatedsize = (v = (*args)[3])->toInt32(); FunctionObject *dbcreationCallback = (v = (*args)[4])->as<FunctionObject>(); - - QCryptographicHash md5(QCryptographicHash::Md5); - md5.addData(dbname.toUtf8()); - QString dbid(QLatin1String(md5.result().toHex())); - - QString basename = qmlsqldatabase_databaseFile(dbid, scope.engine); + QString basename = args->v4engine()->qmlEngine()->offlineStorageDatabaseFilePath(dbname); + QFileInfo dbFile(basename); + if (!QDir().mkpath(dbFile.dir().absolutePath())) + V4THROW_SQL2(SQLEXCEPTION_DATABASE_ERR, QQmlEngine::tr("LocalStorage: can't create path ") + dbFile.dir().absolutePath()); + QString dbid = dbFile.fileName(); bool created = false; QString version = dbversion; + QSqlDatabase database; { QSettings ini(basename+QLatin1String(".ini"),QSettings::IniFormat); diff --git a/src/qml/qml/qqmlengine.cpp b/src/qml/qml/qqmlengine.cpp index e1fa97b52f..fe3f1dbaf1 100644 --- a/src/qml/qml/qqmlengine.cpp +++ b/src/qml/qml/qqmlengine.cpp @@ -2178,6 +2178,27 @@ QString QQmlEngine::offlineStoragePath() const return d->offlineStoragePath; } +/*! + Returns the file path where a \l{QtQuick.LocalStorage}{Local Storage} + database with the identifier \a databaseName is (or would be) located. + + \sa LocalStorage.openDatabaseSync() + \since 5.9 +*/ +QString QQmlEngine::offlineStorageDatabaseFilePath(const QString &databaseName) const +{ + Q_D(const QQmlEngine); + QCryptographicHash md5(QCryptographicHash::Md5); + md5.addData(databaseName.toUtf8()); + return d->offlineStorageDatabaseDirectory() + QLatin1String(md5.result().toHex()); +} + +QString QQmlEnginePrivate::offlineStorageDatabaseDirectory() const +{ + Q_Q(const QQmlEngine); + return q->offlineStoragePath() + QDir::separator() + QLatin1String("Databases") + QDir::separator(); +} + QQmlPropertyCache *QQmlEnginePrivate::createCache(QQmlType *type, int minorVersion) { QList<QQmlType *> types; diff --git a/src/qml/qml/qqmlengine.h b/src/qml/qml/qqmlengine.h index 3102a20fac..8cada954fe 100644 --- a/src/qml/qml/qqmlengine.h +++ b/src/qml/qml/qqmlengine.h @@ -136,6 +136,7 @@ public: void setOfflineStoragePath(const QString& dir); QString offlineStoragePath() const; + QString offlineStorageDatabaseFilePath(const QString &databaseName) const; QUrl baseUrl() const; void setBaseUrl(const QUrl &); diff --git a/src/qml/qml/qqmlengine_p.h b/src/qml/qml/qqmlengine_p.h index 916566b6c7..1bdeacd524 100644 --- a/src/qml/qml/qqmlengine_p.h +++ b/src/qml/qml/qqmlengine_p.h @@ -205,6 +205,7 @@ public: inline void deleteInEngineThread(T *); template<typename T> inline static void deleteInEngineThread(QQmlEngine *, T *); + QString offlineStorageDatabaseDirectory() const; // These methods may be called from the loader thread inline QQmlPropertyCache *cache(QQmlType *, int); diff --git a/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp b/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp index 9c155eda5b..e170920486 100644 --- a/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp +++ b/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp @@ -56,6 +56,7 @@ private slots: void baseUrl(); void contextForObject(); void offlineStoragePath(); + void offlineDatabaseStoragePath(); void clearComponentCache(); void trimComponentCache(); void trimComponentCache_data(); @@ -252,6 +253,34 @@ void tst_qqmlengine::offlineStoragePath() QCOMPARE(engine.offlineStoragePath(), QDir::homePath()); } +void tst_qqmlengine::offlineDatabaseStoragePath() +{ + // Without these set, QDesktopServices::storageLocation returns + // strings with extra "//" at the end. We set them to ignore this problem. + qApp->setApplicationName("tst_qqmlengine"); + qApp->setOrganizationName("QtProject"); + qApp->setOrganizationDomain("www.qt-project.org"); + + QQmlEngine engine; + QString dataLocation = QStandardPaths::writableLocation(QStandardPaths::DataLocation); + const QString databaseName = QLatin1String("foo"); + QString databaseLocation = engine.offlineStorageDatabaseFilePath(databaseName); + QCOMPARE(dataLocation.isEmpty(), databaseLocation.isEmpty()); + + QDir dir(dataLocation); + dir.mkpath("QML"); + dir.cd("QML"); + dir.mkpath("OfflineStorage"); + dir.cd("OfflineStorage"); + dir.mkpath("Databases"); + dir.cd("Databases"); + QCOMPARE(QFileInfo(databaseLocation).dir().path(), dir.path()); + + QCryptographicHash md5(QCryptographicHash::Md5); + md5.addData(databaseName.toUtf8()); + QCOMPARE(databaseLocation, QDir::toNativeSeparators(dir.filePath(QLatin1String(md5.result().toHex())))); +} + void tst_qqmlengine::clearComponentCache() { QQmlEngine engine; |