]> git.sur5r.net Git - minitube/blobdiff - src/database.cpp
Upload 3.9.3-2 to unstable
[minitube] / src / database.cpp
index 4e089ff9cdc895d812aabadf81be0470e0a93f05..ec3199893dac65e7e03466d08e3ce30206c890db 100644 (file)
@@ -20,21 +20,20 @@ $END_LICENSE */
 
 #include "database.h"
 #include "constants.h"
-#include <QDesktopServices>
+#include <QtDebug>
 
 static const int DATABASE_VERSION = 1;
 static const QString dbName = QLatin1String(Constants::UNIX_NAME) + ".db";
 static Database *databaseInstance = 0;
 
 Database::Database() {
-#if QT_VERSION >= 0x050000
     QString dataLocation = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
-#else
-    QString dataLocation = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
-#endif
 
-    QDir().mkpath(dataLocation);
+    if (!QDir().mkpath(dataLocation)) {
+        qCritical() << "Failed to create directory " << dataLocation;
+    }
     dbLocation = dataLocation + "/" + dbName;
+    qDebug() << "dbLocation" << dbLocation;
 
     QMutexLocker locker(&lock);
 
@@ -55,6 +54,21 @@ Database::~Database() {
 }
 
 void Database::createDatabase() {
+    qDebug() << __PRETTY_FUNCTION__;
+
+#ifdef APP_LINUX
+    // Qt5 changed its "data" path. Try to move the old db to the new path
+    QString homeLocation = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
+    QString qt4DataLocation = homeLocation + "/.local/share/data/" + Constants::ORG_NAME + "/" + Constants::NAME;
+    QString oldDbLocation = qt4DataLocation + "/" + dbName;
+    qDebug() << oldDbLocation;
+    if (QFile::exists(oldDbLocation)) {
+        if (QFile::copy(oldDbLocation, dbLocation)) {
+            qDebug() << "Moved db from" << oldDbLocation << "to" << dbLocation;
+            return;
+        }
+    }
+#endif
 
     qWarning() << "Creating the database";
 
@@ -101,22 +115,29 @@ void Database::createDatabase() {
               + QString::number(DATABASE_VERSION) + ")", db);
 }
 
+// static
 QString Database::getDbLocation() {
-#if QT_VERSION >= 0x050000
-    static const QString dataLocation = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
-#else
-    static const QString dataLocation = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
-#endif
-    return dataLocation + "/" + dbName;
+    return QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/" + dbName;
 }
 
+// static
 bool Database::exists() {
     static bool fileExists = false;
-    if (!fileExists)
+    if (!fileExists) {
         fileExists = QFile::exists(getDbLocation());
+#ifdef APP_LINUX
+        if (!fileExists) {
+            QString homeLocation = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
+            QString qt4DataLocation = homeLocation + "/.local/share/data/" + Constants::ORG_NAME + "/" + Constants::NAME;
+            QString oldDbLocation = qt4DataLocation + "/" + dbName;
+            fileExists = QFile::exists(oldDbLocation);
+        }
+#endif
+    }
     return fileExists;
 }
 
+// static
 Database& Database::instance() {
     static QMutex mutex;
     QMutexLocker locker(&mutex);
@@ -147,7 +168,7 @@ QSqlDatabase Database::getConnection() {
     }
 }
 
-QVariant Database::getAttribute(QString name) {
+QVariant Database::getAttribute(const QString &name) {
     QSqlQuery query("select value from attributes where name=?", getConnection());
     query.bindValue(0, name);
 
@@ -158,7 +179,7 @@ QVariant Database::getAttribute(QString name) {
     return QVariant();
 }
 
-void Database::setAttribute(QString name, QVariant value) {
+void Database::setAttribute(const QString &name, const QVariant &value) {
     QSqlQuery query(getConnection());
     query.prepare("insert or replace into attributes (name, value) values (?,?)");
     query.bindValue(0, name);
@@ -219,7 +240,7 @@ void Database::drop() {
 }
 
 void Database::closeConnections() {
-    foreach(QSqlDatabase connection, connections.values()) {
+    foreach(QSqlDatabase connection, connections) {
         // qDebug() << "Closing connection" << connection;
         connection.close();
     }