#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);
}
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";
+ 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);
}
}
-QVariant Database::getAttribute(QString name) {
+QVariant Database::getAttribute(const QString &name) {
QSqlQuery query("select value from attributes where name=?", getConnection());
query.bindValue(0, 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);
}
void Database::closeConnections() {
- foreach(QSqlDatabase connection, connections.values()) {
+ foreach(QSqlDatabase connection, connections) {
// qDebug() << "Closing connection" << connection;
connection.close();
}