]> git.sur5r.net Git - minitube/blobdiff - src/database.cpp
New upstream version 3.8
[minitube] / src / database.cpp
index 3e7c537d5970aadeb2c1f607fa804fee03d4926b..ec3199893dac65e7e03466d08e3ce30206c890db 100644 (file)
@@ -20,29 +20,32 @@ $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);
 
-    if(QFile::exists(dbLocation)) {
+    if (QFile::exists(dbLocation)) {
         // check db version
         int databaseVersion = getAttribute("version").toInt();
-        if (databaseVersion != DATABASE_VERSION)
+        if (databaseVersion > DATABASE_VERSION)
             qWarning("Wrong database version: %d", databaseVersion);
+
+        if (!getAttribute("channelIdFix").toBool())
+            fixChannelIds();
+
     } else createDatabase();
 }
 
@@ -51,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";
 
@@ -58,9 +76,9 @@ void Database::createDatabase() {
 
     QSqlQuery("create table subscriptions ("
               "id integer primary key autoincrement,"
-              "user_id varchar,"
-              "user_name varchar,"
-              "name varchar,"
+              "user_id varchar," // this is really channel_id
+              "user_name varchar," // obsolete yt2 username
+              "name varchar," // this is really channel_title
               "description varchar,"
               "thumb_url varchar,"
               "country varchar,"
@@ -77,13 +95,13 @@ void Database::createDatabase() {
     QSqlQuery("create table subscriptions_videos ("
               "id integer primary key autoincrement,"
               "video_id varchar,"
-              "channel_id integer,"
+              "channel_id integer," // this is really subscription_id
               "published integer,"
               "added integer,"
               "watched integer,"
               "title varchar,"
-              "author varchar,"
-              "user_id varchar,"
+              "author varchar," // this is really channel_title
+              "user_id varchar," // this is really channel_id
               "description varchar,"
               "url varchar,"
               "thumb_url varchar,"
@@ -97,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);
@@ -143,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);
 
@@ -154,13 +179,33 @@ 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("update attributes set value=? where name=?");
-    query.bindValue(0, value);
-    query.bindValue(1, name);
+    query.prepare("insert or replace into attributes (name, value) values (?,?)");
+    query.bindValue(0, name);
+    query.bindValue(1, value);
     bool success = query.exec();
-    if (!success) qDebug() << query.lastError().text();
+    if (!success) qWarning() << query.lastError().text();
+}
+
+void Database::fixChannelIds() {
+    if (!getConnection().transaction())
+        qWarning() << "Transaction failed" << __PRETTY_FUNCTION__;
+
+    qWarning() << "Fixing channel ids";
+
+    QSqlQuery query(getConnection());
+    bool success = query.exec("update subscriptions set user_id='UC' || user_id where user_id not like 'UC%'");
+    if (!success) qWarning() << query.lastError().text();
+
+    query = QSqlQuery(getConnection());
+    success = query.exec("update subscriptions_videos set user_id='UC' || user_id where user_id not like 'UC%'");
+    if (!success) qWarning() << query.lastError().text();
+
+    setAttribute("channelIdFix", 1);
+
+    if (!getConnection().commit())
+        qWarning() << "Commit failed" << __PRETTY_FUNCTION__;
 }
 
 /**
@@ -195,7 +240,7 @@ void Database::drop() {
 }
 
 void Database::closeConnections() {
-    foreach(QSqlDatabase connection, connections.values()) {
+    foreach(QSqlDatabase connection, connections) {
         // qDebug() << "Closing connection" << connection;
         connection.close();
     }