9bda67728adbc0029246061fcfb9fe23dd5f3054
[qtrapids] / src / server / ServerDb.hpp
1 #ifndef _SERVERDB_HPP_
2 #define _SERVERDB_HPP_
3
4 #include <QSettings>
5 #include <QtSql>
6 #include <QDir>
7
8 #include <qtrapids/settings.hpp>
9
10 namespace qtrapids
11 {
12
13 class ServerSettings
14 {
15
16 public:
17         ServerSettings(QSettings &settings)
18                         : settings_(settings) { }
19
20         ~ServerSettings() { }
21
22         QString getDbEngine() const {
23                 return getParamAndStore("db_engine", getDefaultDbEngine()).toString();
24         }
25
26         QString getDbName() const {
27                 QString default_db_path(QDir::home().filePath(appName() + ".sqlite"));
28                 return getParamAndStore("db", default_db_path).toString();
29         }
30
31         QString getTorrentsDir() const {
32                 QString default_dir(QDir::home().filePath(getTorrentsSubDir()));
33                 return getParamAndStore("torrents_dir", default_dir).toString();
34
35         }
36
37         QString getDownloadDir() const {
38                 QString default_dir(QDir::home().absolutePath());
39                 QString v = getParamAndStore("download/directory", default_dir).toString();
40                 if (!v.isEmpty()) {
41                         return v;
42                 } else {
43                         settings_.setValue("download/directory", default_dir);
44                         return default_dir;
45                 }
46         }
47
48         static QString getTorrentsSubDir() {
49                 return QString(".") + appName();
50         }
51
52 private:
53
54         ServerSettings(ServerSettings const&);
55         ServerSettings& operator= (ServerSettings const&);
56
57         static inline QString appName() {
58                 return QCoreApplication::applicationName();
59         }
60
61         static QString getDefaultDbEngine() {
62                 // for (QStringListIterator p = QSqlDatabase::drivers(); p.hasNext();) {
63                 //     return p.next();
64                 // }
65                 return "QSQLITE";
66         }
67
68         QVariant getParamAndStore(QString const& name, QVariant default_value) const {
69                 return GetSettingsStoreDefault(settings_, name, default_value);
70         }
71
72         mutable QSettings &settings_;
73 };
74
75 namespace {
76
77 class DbAccessor
78 {
79 public:
80         DbAccessor(QSqlDatabase &db)
81                         : db_(db) {
82                 if (!db_.open()) {
83                         qDebug() << "cant open db";
84                 }
85         }
86
87         ~DbAccessor() {
88                 db_.close();
89         }
90
91 private:
92         QSqlDatabase &db_;
93 };
94
95 }
96
97 class ServerDb
98 {
99
100 public:
101         ServerDb(ServerSettings *settings)
102                         : db_(QSqlDatabase::addDatabase(settings->getDbEngine())) {
103                 QString db_name(settings->getDbName());
104                 db_.setDatabaseName(db_name);
105
106                 qDebug() << "opening db " << db_name;
107                 if (!db_.open()) {
108                         qDebug() << "cant open db";
109                         return;
110                 }
111                 qDebug() << "opened " << db_name;
112
113                 QSqlQuery q;
114                 if (!q.exec("create table torrents (hash varchar primary key, path varchar, savepath varchar);\n")) {
115                         qDebug() << "cant create table: " << q.lastError().text();
116                 }
117         }
118
119         ~ServerDb() {
120                 db_.close();
121         }
122
123         void addTorrent(const QString &hash, const QString &path, const QString &save_path) {
124                 DbAccessor dba(db_);
125                 QSqlQuery query_add_;
126                 query_add_.prepare("INSERT INTO torrents (hash, path, savepath) VALUES (?, ?, ?)");
127                 query_add_.bindValue(0, hash);
128                 query_add_.bindValue(1, path);
129                 query_add_.bindValue(2, save_path);
130                 if (!query_add_.exec()) {
131                         qDebug() << "cant add torrent info into db: "
132                         << query_add_.lastError().text();
133                 }
134         }
135
136         void removeTorrent(const QString &hash) {
137                 DbAccessor dba(db_);
138                 QSqlQuery query(QString("DELETE FROM torrents WHERE hash='") + hash + "'");
139                 if (!query.exec()) {
140                         qDebug() << "cant delete torrent info from db"
141                         << query.lastError().text();
142                 }
143         }
144
145 private:
146
147         ServerDb(ServerDb const&);
148         ServerDb& operator= (ServerDb const&);
149
150         QSqlDatabase db_;
151 };
152
153 class TorrentsStorage
154 {
155 public:
156
157         TorrentsStorage(ServerDb &db)
158                         : torrents_("SELECT hash, path, savepath from torrents") { }
159
160
161         bool nextTorrent(TorrentDownloadInfo &info) {
162                 if (!torrents_.next()) {
163                         return false;
164                 }
165                 info.hash = torrents_.value(0).toString();
166                 info.path = torrents_.value(1).toString();
167                 info.download_path = torrents_.value(2).toString();
168                 return true;
169         }
170
171
172 private:
173         QSqlQuery torrents_;
174 };
175
176 } // namespace qtrapids
177
178 #endif // _SERVERDB_HPP_