← Back to team overview

uonedb-qt team mailing list archive

[Merge] lp:~3v1n0/u1db-qt/uri-path-parsing into lp:u1db-qt

 

Marco Trevisan (Treviño) has proposed merging lp:~3v1n0/u1db-qt/uri-path-parsing into lp:u1db-qt.

Commit message:
Database: support parsing of URI paths using something such as "file://"+ path

Requested reviews:
  U1DB Qt developers (uonedb-qt)
Related bugs:
  Bug #1426180 in U1DB Qt/ QML: "U1db.Database does not support QUri as path"
  https://bugs.launchpad.net/u1db-qt/+bug/1426180

For more details, see:
https://code.launchpad.net/~3v1n0/u1db-qt/uri-path-parsing/+merge/251369

Make the database to parse and use local URIs starting with file://

Unfortunately this does not fix lp:1426178, which seems related to something
else.
-- 
Your team U1DB Qt developers is requested to review the proposed merge of lp:~3v1n0/u1db-qt/uri-path-parsing into lp:u1db-qt.
=== modified file 'examples/u1db-qt-example-1/u1db-qt-example-1.qml'
--- examples/u1db-qt-example-1/u1db-qt-example-1.qml	2013-05-02 18:20:33 +0000
+++ examples/u1db-qt-example-1/u1db-qt-example-1.qml	2015-02-28 16:53:58 +0000
@@ -36,7 +36,7 @@
     
     U1db.Database {
         id: aDatabase
-        path: "aDatabase1"
+        path: "file:///tmp/aDatabase1.db";
     }
     
     /*!

=== modified file 'src/database.cpp'
--- src/database.cpp	2015-02-19 10:37:54 +0000
+++ src/database.cpp	2015-02-28 16:53:58 +0000
@@ -24,6 +24,7 @@
 #include <QStandardPaths>
 #include <QDir>
 #include <QSqlError>
+#include <QUrl>
 #include <QUuid>
 #include <QStringList>
 #include <QJsonDocument>
@@ -85,6 +86,19 @@
 }
 
 /*!
+    Sanitize path
+ */
+QString Database::sanitizePath(const QString &path)
+{
+    QUrl url(path);
+
+    if (url.isValid() && url.isLocalFile())
+        return url.path();
+
+    return path;
+}
+
+/*!
     Checks if the underlying SQLite database is ready to be used
     Only to be used as a utility function by initializeIfNeeded()
  */
@@ -746,16 +760,18 @@
 void
 Database::setPath(const QString& path)
 {
-    if (m_path == path)
+    const QString& parsed_path = sanitizePath(path);
+
+    if (m_path == parsed_path)
         return;
 
     beginResetModel();
     m_db.close();
-    initializeIfNeeded(path);
+    initializeIfNeeded(parsed_path);
     endResetModel();
 
-    m_path = path;
-    Q_EMIT pathChanged(path);
+    m_path = parsed_path;
+    Q_EMIT pathChanged(m_path);
 }
 
 /*!

=== modified file 'src/database.h'
--- src/database.h	2014-01-24 11:13:35 +0000
+++ src/database.h	2015-02-28 16:53:58 +0000
@@ -82,6 +82,7 @@
     QString m_error;
 
     QString getReplicaUid();
+    QString sanitizePath(const QString& path);
     bool isInitialized();
     bool initializeIfNeeded(const QString& path=":memory:");
     bool setError(const QString& error);

=== modified file 'tests/test-database.cpp'
--- tests/test-database.cpp	2014-11-07 14:30:31 +0000
+++ tests/test-database.cpp	2015-02-28 16:53:58 +0000
@@ -47,6 +47,16 @@
         QCOMPARE(db.getPath(), file.fileName());
     }
 
+    void testCanSetPathUsingQUrl()
+    {
+        Database db;
+        QCOMPARE(db.getPath(), QString(""));
+        QSignalSpy modelReset(&db, SIGNAL(pathChanged(const QString&)));
+        QTemporaryFile file;
+        db.setPath(QUrl::fromLocalFile(file.fileName()).toString());
+        QCOMPARE(db.getPath(), file.fileName());
+    }
+
     void testNonExistingParentFolder()
     {
         Database db;


References