1 /***************************************************************************
2 * Copyright (C) 2010 by Ixonos Plc *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; version 2 of the License. *
8 * This program is distributed in the hope that it will be useful, *
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11 * GNU General Public License for more details. *
13 * You should have received a copy of the GNU General Public License *
14 * along with this program; if not, write to the *
15 * Free Software Foundation, Inc., *
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
17 ***************************************************************************/
18 #ifndef _SERVERDB_HPP_
19 #define _SERVERDB_HPP_
26 #include <qtrapids/settings.hpp>
35 ServerSettings(QSettings &settings)
36 : settings_(settings) { }
40 QString getDbEngine() const {
41 return getParamAndStore("db_engine", getDefaultDbEngine()).toString();
44 QString getDbName() const {
45 QString default_db_path(QDir::home().filePath(appName() + ".sqlite"));
46 return getParamAndStore("db", default_db_path).toString();
49 QString getTorrentsDir() const {
50 QString default_dir(QDir::home().filePath(getTorrentsSubDir()));
51 return getParamAndStore("torrents_dir", default_dir).toString();
55 static QString getTorrentsSubDir() {
56 return QString(".") + appName();
59 QString getDownloadDir() const {
60 QString default_dir(QDir::home().absolutePath());
61 QString v = getParamAndStore("download/directory", default_dir).toString();
65 settings_.setValue("download/directory", default_dir);
70 ports_range_t getListenPorts() const {
72 ports_range_t default_ports(13131, 13132);
73 default_ports.first = getParamAndStore("net/listen_range_begin", default_ports.first).toUInt();
74 default_ports.second = getParamAndStore("net/listen_range_end", default_ports.second).toUInt();
79 @todo deztructor: there is no check for option type yet
81 void setOptions(ParamsMapConst_t &options) {
82 for (ParamsMapConstIterator_t p = options.constBegin(); p != options.constEnd(); ++p) {
83 settings_.setValue(p.key(), p.value());
87 ParamsMap_t getOptions() const {
89 QStringList keys = settings_.allKeys();
90 for (QStringList::const_iterator p = keys.begin(); p != keys.end(); ++p) {
91 options[*p] = settings_.value(*p).toString();
98 ServerSettings(ServerSettings const&);
99 ServerSettings& operator= (ServerSettings const&);
101 static inline QString appName() {
102 return QCoreApplication::applicationName();
105 static QString getDefaultDbEngine() {
106 // for (QStringListIterator p = QSqlDatabase::drivers(); p.hasNext();) {
112 QVariant getParamAndStore(QString const& name, QVariant default_value) const {
113 return GetSettingsStoreDefault(settings_, name, default_value);
116 mutable QSettings &settings_;
124 DbAccessor(QSqlDatabase &db)
127 qDebug() << "cant open db";
145 ServerDb(ServerSettings *settings)
146 : db_(QSqlDatabase::addDatabase(settings->getDbEngine())) {
147 QString db_name(settings->getDbName());
148 db_.setDatabaseName(db_name);
150 qDebug() << "opening db " << db_name;
152 qDebug() << "cant open db";
155 qDebug() << "opened " << db_name;
158 if (!q.exec("create table torrents (hash varchar primary key, path varchar, savepath varchar);\n")) {
159 qDebug() << "cant create table: " << q.lastError().text();
167 void addTorrent(const QString &hash, const QString &path, const QString &save_path) {
169 QSqlQuery query_add_;
170 query_add_.prepare("INSERT INTO torrents (hash, path, savepath) VALUES (?, ?, ?)");
171 query_add_.bindValue(0, hash);
172 query_add_.bindValue(1, path);
173 query_add_.bindValue(2, save_path);
174 if (!query_add_.exec()) {
175 qDebug() << "cant add torrent info into db: "
176 << query_add_.lastError().text();
180 void removeTorrent(const QString &hash) {
182 QSqlQuery query(QString("DELETE FROM torrents WHERE hash='") + hash + "'");
184 qDebug() << "cant delete torrent info from db"
185 << query.lastError().text();
191 ServerDb(ServerDb const&);
192 ServerDb& operator= (ServerDb const&);
197 class TorrentsStorage
201 TorrentsStorage(ServerDb &db)
202 : torrents_("SELECT hash, path, savepath from torrents") { }
205 bool nextTorrent(TorrentDownloadInfo &info) {
206 if (!torrents_.next()) {
209 info.hash = torrents_.value(0).toString();
210 info.path = torrents_.value(1).toString();
211 info.download_path = torrents_.value(2).toString();
220 } // namespace qtrapids
222 #endif // _SERVERDB_HPP_