Jump to exact position on page, as identified by TOC entry. Handle
[dorian] / model / bookdb.cpp
1 #include "bookdb.h"
2 #include "platform.h"
3 #include "trace.h"
4
5 BookDb *theInstance;
6
7 BookDb *BookDb::instance()
8 {
9     if (!theInstance) {
10         theInstance = new BookDb;
11     }
12     return theInstance;
13 }
14
15 void BookDb::close()
16 {
17     if (theInstance) {
18         delete theInstance;
19         theInstance = 0;
20     }
21 }
22
23 BookDb::BookDb()
24 {
25     TRACE;
26     bool shouldCreate = false;
27     QFileInfo info(Platform::dbPath());
28     if (!info.exists()) {
29         QDir dbDir;
30         if (!dbDir.mkpath(info.absolutePath())) {
31             qCritical() << "Could not create" << info.absolutePath();
32         }
33         shouldCreate = true;
34     }
35     db = QSqlDatabase::addDatabase("QSQLITE");
36     db.setDatabaseName(QDir::toNativeSeparators(Platform::dbPath()));
37     if (!db.open()) {
38         qCritical() << "Could not open" << Platform::dbPath() << ": Error"
39                 << db.lastError().text();
40     }
41     if (shouldCreate) {
42         create();
43     }
44 }
45
46 BookDb::~BookDb()
47 {
48     db.close();
49 }
50
51 void BookDb::create()
52 {
53     TRACE;
54     QSqlQuery query;
55     if (!query.exec("create table book "
56                     "(name text primary key, content blob)")) {
57         qCritical() << "Failed to create database:"
58                 << query.lastError().text();
59     }
60 }
61
62 QVariantHash BookDb::load(const QString &book)
63 {
64     TRACE;
65     qDebug() << book;
66     QVariantHash ret;
67     QByteArray bytes;
68     QSqlQuery query("select content from book where name = ?");
69     query.bindValue(0, book);
70     query.setForwardOnly(true);
71     if (!query.exec()) {
72         qCritical() << "Query failed:" << query.lastError().text();
73         return ret;
74     }
75     while (query.next()) {
76         bytes = query.value(0).toByteArray();
77         QDataStream in(bytes);
78         in >> ret;
79         break;
80     }
81     return ret;
82 }
83
84 void BookDb::save(const QString &book, const QVariantHash &data)
85 {
86     TRACE;
87     qDebug() << book;
88     QByteArray bytes;
89     QDataStream out(&bytes, QIODevice::WriteOnly);
90     out << data;
91     QSqlQuery query("insert or replace into book values (?, ?)");
92     query.bindValue(0, book);
93     query.bindValue(1, bytes);
94     if (!query.exec()) {
95         qCritical() << "Query failed:" << query.lastError().text();
96     }
97 }
98
99 void BookDb::remove(const QString &book)
100 {
101     TRACE;
102     qDebug() << book;
103     QSqlQuery query("delete from book where name = ?");
104     query.bindValue(0, book);
105     if (!query.exec()) {
106         qCritical() << "Query failed:" << query.lastError().text();
107     }
108 }
109
110 QStringList BookDb::books()
111 {
112     TRACE;
113     QStringList ret;
114     QSqlQuery query("select name from book");
115     query.setForwardOnly(true);
116     if (!query.exec()) {
117         qCritical() << "Query failed:" << query.lastError().text();
118         return ret;
119     }
120     while (query.next()) {
121         ret.append(query.value(0).toString());
122     }
123     qDebug() << ret;
124     return ret;
125 }
126
127 void BookDb::removeAll()
128 {
129     foreach (QString book, books()) {
130         remove(book);
131     }
132 }