Modified the license text comment type.
[emufront] / src / db / databasemanager.cpp
1 /*
2 ** EmuFront
3 ** Copyright 2010 Mikko Keinänen
4 **
5 ** This file is part of EmuFront.
6 **
7 **
8 ** EmuFront is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License version 2 as published by
10 ** the Free Software Foundation and appearing in the file gpl.txt included in the
11 ** packaging of this file.
12 **
13 ** EmuFront is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ** GNU General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include "databasemanager.h"
22 #include <QObject>
23 #include <QSqlDatabase>
24 #include <QSqlTableModel>
25 #include <QSqlError>
26 #include <QSqlQuery>
27 #include <QSqlRecord>
28 #include <QFile>
29 #include <QDir>
30 #include <QVariant>
31 #include <QDebug>
32 #include <QDateTime>
33
34 const QString DatabaseManager::DB_FILENAME = QString("emufront.db.sqlite");
35 const QString DatabaseManager::DATABASE = QString("QSQLITE");
36 const QString DatabaseManager::DB_TABLE_NAME_MEDIATYPE = QString("mediatype");
37 const QString DatabaseManager::DB_TABLE_NAME_PLATFORM = QString("platform");
38 const QString DatabaseManager::DB_TABLE_NAME_FILE= QString("file");
39 const QString DatabaseManager::DB_TABLE_NAME_FILEPATH = QString("filepath");
40 const QString DatabaseManager::DB_TABLE_NAME_SETUP = QString("setup");
41 const QString DatabaseManager::DB_TABLE_MEDIAIMAGECONTAINER = QString("mediaimagecontainer");
42 const QString DatabaseManager::DB_TABLE_MEDIAIMAGECONTAINER_MEDIAIMAGE = QString("mediaimagecontainer_mediaimage");
43 const QString DatabaseManager::DB_TABLE_EXECUTABLE = QString("executable");
44
45 DatabaseManager::DatabaseManager(QObject *parent)
46         : QObject(parent)
47 {
48     sqlTableModel = 0;
49 }
50
51 DatabaseManager::~DatabaseManager()
52 {
53     // no need to explicitily destroy sqlTableModel
54     // because it is parented QObject and will
55     // be destroyed when parent is destroyed
56 }
57
58 /*
59  You may wanna set the possible filters (filterDataObjects) before calling getDataModel.
60  After filtering do not set update to true. Data model is already updated.
61 */
62 QSqlQueryModel* DatabaseManager::getDataModel(bool update)
63 {
64     if (!sqlTableModel) {
65         sqlTableModel = getData();
66     }
67     else if (update)
68         clearFilters();
69     return sqlTableModel;
70 }
71
72 bool DatabaseManager::openDB()
73 {
74     QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
75     db.setDatabaseName(DatabaseManager::getDbPath());
76     return db.open();
77 }
78
79 QString DatabaseManager::getDbPath()
80 {
81         QString path;
82 #ifdef Q_OS_LINUX
83         path.append(QDir::home().path());
84         path.append(QDir::separator()).append(DB_FILENAME);
85 #else
86         path.append(DB_FILENAME);       
87 #endif
88         return path;
89 }
90
91 void DatabaseManager::resetModel()
92 {
93     if (!sqlTableModel) return;
94     clearFilters();
95 }
96
97 // sql must return a count(*) value
98 int DatabaseManager::countRows(QString tableName, QString columnName, int id) const
99 {
100     QString sql = QString("SELECT COUNT(*) FROM %1 WHERE %2 = %3")
101         .arg(tableName).arg(columnName).arg(id);
102     int numEntries = 0;
103     QSqlQuery query(sql);
104     if (query.next())
105         numEntries = query.value(0).toInt();
106     return numEntries;
107 }
108
109
110 /* Throws EmuFrontException if filtered data was not found. */
111 EmuFrontObject* DatabaseManager::getDataObject(int id)
112 {
113     filterById(id);
114     return getFilteredDataObject();
115 }
116
117 /* Throws EmuFrontException if filtered data was not found. */
118 EmuFrontObject* DatabaseManager::getDataObject(QString filter)
119 {
120     QList<QString> filters;
121     filters.append(filter);
122     filterDataObjects(filters);
123     return getFilteredDataObject(); // throws EmuFrontException
124 }
125
126 /* Throws EmuFrontException if filtered data was not found. */
127 EmuFrontObject* DatabaseManager::getFilteredDataObject()
128 {
129     EmuFrontObject *plf = 0;
130     // TODO: if record has more than one the first instance is returned
131     // ... check if this is ok in all cases!
132     if (sqlTableModel->rowCount() >= 1)
133     {
134         QSqlRecord record = sqlTableModel->record(0);
135         if (record.isEmpty()) {
136             throw EmuFrontException(tr("No filtered data available"));
137         }
138         else plf = recordToDataObject(&record);
139     }
140      return plf;
141 }
142 /* Throws EmuFrontException */
143 EmuFrontObject* DatabaseManager::getDataObjectFromModel(QModelIndex *index)
144 {
145     if (!sqlTableModel) sqlTableModel = getDataModel();
146     QSqlRecord record = sqlTableModel->record(index->row());
147     return recordToDataObject(&record);
148 }
149
150 int DatabaseManager::getCurrentTimeStamp() {
151     return QDateTime::currentDateTime().toTime_t();
152 }
153
154 int DatabaseManager::countDataObjectRefs(int id) const
155 {
156     int ret = 0;
157     QSqlQuery q;
158     q.prepare(getCountRefsSelect(id));
159     q.exec();
160     QSqlRecord rec;
161     if (q.next()) {
162         rec = q.record();
163         ret = rec.value(0).toInt();
164     }
165     qDebug() << "Found " << ret << " references.";
166     return ret;
167 }