Server methods to store/get options
[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         static QString getTorrentsSubDir() {
38                 return QString(".") + appName();
39         }
40
41         QString getDownloadDir() const {
42                 QString default_dir(QDir::home().absolutePath());
43                 QString v = getParamAndStore("download/directory", default_dir).toString();
44                 if (!v.isEmpty()) {
45                         return v;
46                 } else {
47                         settings_.setValue("download/directory", default_dir);
48                         return default_dir;
49                 }
50         }
51
52         ports_range_t getListenPorts() const {
53
54                 ports_range_t default_ports(13131, 13132);
55                 default_ports.first = getParamAndStore("net/listen_range_begin", default_ports.first).toUInt();
56                 default_ports.second = getParamAndStore("net/listen_range_end", default_ports.second).toUInt();
57                 return default_ports;
58         }
59
60         /**
61            @todo deztructor: there is no check for option type yet
62          */
63         void setOptions(ParamsMapConst_t &options) {
64                 for (ParamsMapConstIterator_t p = options.constBegin(); p != options.constEnd(); ++p) {
65                         settings_.setValue(p.key(), p.value());
66                 }
67         }
68
69         ParamsMap_t getOptions() const {
70                 ParamsMap_t options;
71                 QStringList keys = settings_.allKeys();
72                 for (QStringList::const_iterator p = keys.begin(); p != keys.end(); ++p) {
73                         options[*p] = settings_.value(*p).toString();
74                 }
75                 return options;
76         }
77
78 private:
79
80         ServerSettings(ServerSettings const&);
81         ServerSettings& operator= (ServerSettings const&);
82
83         static inline QString appName() {
84                 return QCoreApplication::applicationName();
85         }
86
87         static QString getDefaultDbEngine() {
88                 // for (QStringListIterator p = QSqlDatabase::drivers(); p.hasNext();) {
89                 //     return p.next();
90                 // }
91                 return "QSQLITE";
92         }
93
94         QVariant getParamAndStore(QString const& name, QVariant default_value) const {
95                 return GetSettingsStoreDefault(settings_, name, default_value);
96         }
97
98         mutable QSettings &settings_;
99 };
100
101 namespace {
102
103 class DbAccessor
104 {
105 public:
106         DbAccessor(QSqlDatabase &db)
107                         : db_(db) {
108                 if (!db_.open()) {
109                         qDebug() << "cant open db";
110                 }
111         }
112
113         ~DbAccessor() {
114                 db_.close();
115         }
116
117 private:
118         QSqlDatabase &db_;
119 };
120
121 }
122
123 class ServerDb
124 {
125
126 public:
127         ServerDb(ServerSettings *settings)
128                         : db_(QSqlDatabase::addDatabase(settings->getDbEngine())) {
129                 QString db_name(settings->getDbName());
130                 db_.setDatabaseName(db_name);
131
132                 qDebug() << "opening db " << db_name;
133                 if (!db_.open()) {
134                         qDebug() << "cant open db";
135                         return;
136                 }
137                 qDebug() << "opened " << db_name;
138
139                 QSqlQuery q;
140                 if (!q.exec("create table torrents (hash varchar primary key, path varchar, savepath varchar);\n")) {
141                         qDebug() << "cant create table: " << q.lastError().text();
142                 }
143         }
144
145         ~ServerDb() {
146                 db_.close();
147         }
148
149         void addTorrent(const QString &hash, const QString &path, const QString &save_path) {
150                 DbAccessor dba(db_);
151                 QSqlQuery query_add_;
152                 query_add_.prepare("INSERT INTO torrents (hash, path, savepath) VALUES (?, ?, ?)");
153                 query_add_.bindValue(0, hash);
154                 query_add_.bindValue(1, path);
155                 query_add_.bindValue(2, save_path);
156                 if (!query_add_.exec()) {
157                         qDebug() << "cant add torrent info into db: "
158                         << query_add_.lastError().text();
159                 }
160         }
161
162         void removeTorrent(const QString &hash) {
163                 DbAccessor dba(db_);
164                 QSqlQuery query(QString("DELETE FROM torrents WHERE hash='") + hash + "'");
165                 if (!query.exec()) {
166                         qDebug() << "cant delete torrent info from db"
167                         << query.lastError().text();
168                 }
169         }
170
171 private:
172
173         ServerDb(ServerDb const&);
174         ServerDb& operator= (ServerDb const&);
175
176         QSqlDatabase db_;
177 };
178
179 class TorrentsStorage
180 {
181 public:
182
183         TorrentsStorage(ServerDb &db)
184                         : torrents_("SELECT hash, path, savepath from torrents") { }
185
186
187         bool nextTorrent(TorrentDownloadInfo &info) {
188                 if (!torrents_.next()) {
189                         return false;
190                 }
191                 info.hash = torrents_.value(0).toString();
192                 info.path = torrents_.value(1).toString();
193                 info.download_path = torrents_.value(2).toString();
194                 return true;
195         }
196
197
198 private:
199         QSqlQuery torrents_;
200 };
201
202 } // namespace qtrapids
203
204 #endif // _SERVERDB_HPP_