Browse Source

cloneDatabase in the thread. not sure if this is required

Girish Ramakrishnan 8 years ago
parent
commit
101335be23
2 changed files with 12 additions and 7 deletions
  1. 11 7
      mediamodel.cpp
  2. 1 0
      mediamodel.h

+ 11 - 7
mediamodel.cpp

@@ -13,17 +13,13 @@ class MediaDbReader : public QObject
     Q_OBJECT
 
 public:
-    MediaDbReader(const QSqlDatabase &db, QObject *parent = 0)
+    MediaDbReader(QObject *parent = 0)
         : QObject(parent), m_stop(false)
     {
         qRegisterMetaType<QList<QSqlRecord> >();
+        qRegisterMetaType<QSqlDatabase>();
         qRegisterMetaType<MediaDbReader *>();
         qRegisterMetaType<MediaModel::Node *>();
-
-        // ## check if this should happen in the thread
-        m_db = QSqlDatabase::cloneDatabase(db, QUuid::createUuid().toString());
-        if (!m_db.open())
-            DEBUG << "Erorr opening database" << m_db.lastError().text();
     }
 
     ~MediaDbReader()
@@ -34,6 +30,13 @@ public:
     void stop() { m_stop = true; }
 
 public slots:
+    void initialize(const QSqlDatabase &db)
+    {
+        m_db = QSqlDatabase::cloneDatabase(db, QUuid::createUuid().toString());
+        if (!m_db.open())
+            DEBUG << "Erorr opening database" << m_db.lastError().text();
+    }
+
     void readSongs(MediaModel::Node *node, const QString &lastTitle, const QStringList &ignoreIds, int limit = LIMIT)
     {
         DEBUG << "Reading songs from " << lastTitle;
@@ -487,7 +490,7 @@ void MediaModel::groupBy(MediaModel::GroupBy groupBy)
     m_root->children.clear();
     m_root->loading = m_root->loaded = false;
 
-    MediaDbReader *newReader = new MediaDbReader(QSqlDatabase::database());
+    MediaDbReader *newReader = new MediaDbReader;
     if (m_reader) {
         m_reader->stop();
         m_reader->deleteLater();
@@ -500,6 +503,7 @@ void MediaModel::groupBy(MediaModel::GroupBy groupBy)
         m_readerThread->start();
     }
     m_reader->moveToThread(m_readerThread);
+    QMetaObject::invokeMethod(m_reader, "initialize", Qt::QueuedConnection, Q_ARG(QSqlDatabase, QSqlDatabase::database()));
     connect(m_reader, SIGNAL(dataReady(MediaDbReader *, QList<QSqlRecord>, MediaModel::Node *)), 
             this, SLOT(handleDataReady(MediaDbReader *, QList<QSqlRecord>, MediaModel::Node *)));
 

+ 1 - 0
mediamodel.h

@@ -105,6 +105,7 @@ Q_DECLARE_METATYPE(QList<QSqlRecord>);
 Q_DECLARE_METATYPE(MediaDbReader *);
 Q_DECLARE_METATYPE(QSqlRecord);
 Q_DECLARE_METATYPE(MediaModel::Node *);
+Q_DECLARE_METATYPE(QSqlDatabase);
 
 #include <QtDeclarative>
 QML_DECLARE_TYPE(MediaModel)