Added build script of Debian
[qtrapids] / src / server / ServerDb.hpp
index 342d26a..b68d2f3 100644 (file)
@@ -1,10 +1,30 @@
+/***************************************************************************
+ *   Copyright (C) 2010 by Ixonos Plc   *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; version 2 of the License.               *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************/
 #ifndef _SERVERDB_HPP_
 #define _SERVERDB_HPP_
 
+#include <QDebug>
 #include <QSettings>
 #include <QtSql>
 #include <QDir>
 
+#include <qtrapids/settings.hpp>
+
 namespace qtrapids
 {
 
@@ -12,12 +32,10 @@ class ServerSettings
 {
 
 public:
-       ServerSettings(QSettings *settings)
+       ServerSettings(QSettings &settings)
                        : settings_(settings) { }
 
-       ~ServerSettings() {
-
-       }
+       ~ServerSettings() { }
 
        QString getDbEngine() const {
                return getParamAndStore("db_engine", getDefaultDbEngine()).toString();
@@ -29,9 +47,50 @@ public:
        }
 
        QString getTorrentsDir() const {
-               QString default_dir(QDir::home().filePath(QString(".") + appName()));
-               return getParamAndStore("db", default_dir).toString();
+               QString default_dir(QDir::home().filePath(getTorrentsSubDir()));
+               return getParamAndStore("torrents_dir", default_dir).toString();
+
+       }
+
+       static QString getTorrentsSubDir() {
+               return QString(".") + appName();
+       }
+
+       QString getDownloadDir() const {
+               QString default_dir(QDir::home().absolutePath());
+               QString v = getParamAndStore("download/directory", default_dir).toString();
+               if (!v.isEmpty()) {
+                       return v;
+               } else {
+                       settings_.setValue("download/directory", default_dir);
+                       return default_dir;
+               }
+       }
+
+       ports_range_t getListenPorts() const {
+
+               ports_range_t default_ports(13131, 13132);
+               default_ports.first = getParamAndStore("net/listen_range_begin", default_ports.first).toUInt();
+               default_ports.second = getParamAndStore("net/listen_range_end", default_ports.second).toUInt();
+               return default_ports;
+       }
 
+       /**
+          @todo deztructor: there is no check for option type yet
+        */
+       void setOptions(ParamsMapConst_t &options) {
+               for (ParamsMapConstIterator_t p = options.constBegin(); p != options.constEnd(); ++p) {
+                       settings_.setValue(p.key(), p.value());
+               }
+       }
+
+       ParamsMap_t getOptions() const {
+               ParamsMap_t options;
+               QStringList keys = settings_.allKeys();
+               for (QStringList::const_iterator p = keys.begin(); p != keys.end(); ++p) {
+                       options[*p] = settings_.value(*p).toString();
+               }
+               return options;
        }
 
 private:
@@ -51,18 +110,34 @@ private:
        }
 
        QVariant getParamAndStore(QString const& name, QVariant default_value) const {
-               QVariant v(settings_->value(name));
-               if (!v.isNull()) {
-                       return v;
+               return GetSettingsStoreDefault(settings_, name, default_value);
+       }
+
+       mutable QSettings &settings_;
+};
+
+namespace {
+
+class DbAccessor
+{
+public:
+       DbAccessor(QSqlDatabase &db)
+                       : db_(db) {
+               if (!db_.open()) {
+                       qDebug() << "cant open db";
                }
+       }
 
-               settings_->setValue(name, default_value);
-               return default_value;
+       ~DbAccessor() {
+               db_.close();
        }
 
-       mutable QSettings *settings_;
+private:
+       QSqlDatabase &db_;
 };
 
+}
+
 class ServerDb
 {
 
@@ -72,6 +147,7 @@ public:
                QString db_name(settings->getDbName());
                db_.setDatabaseName(db_name);
 
+               qDebug() << "opening db " << db_name;
                if (!db_.open()) {
                        qDebug() << "cant open db";
                        return;
@@ -89,9 +165,7 @@ public:
        }
 
        void addTorrent(const QString &hash, const QString &path, const QString &save_path) {
-               if (!db_.open()) {
-                       qDebug() << "cant open db";
-               }
+               DbAccessor dba(db_);
                QSqlQuery query_add_;
                query_add_.prepare("INSERT INTO torrents (hash, path, savepath) VALUES (?, ?, ?)");
                query_add_.bindValue(0, hash);
@@ -101,7 +175,15 @@ public:
                        qDebug() << "cant add torrent info into db: "
                        << query_add_.lastError().text();
                }
-               db_.close();
+       }
+
+       void removeTorrent(const QString &hash) {
+               DbAccessor dba(db_);
+               QSqlQuery query(QString("DELETE FROM torrents WHERE hash='") + hash + "'");
+               if (!query.exec()) {
+                       qDebug() << "cant delete torrent info from db"
+                       << query.lastError().text();
+               }
        }
 
 private:
@@ -112,6 +194,29 @@ private:
        QSqlDatabase db_;
 };
 
+class TorrentsStorage
+{
+public:
+
+       TorrentsStorage(ServerDb &db)
+                       : torrents_("SELECT hash, path, savepath from torrents") { }
+
+
+       bool nextTorrent(TorrentDownloadInfo &info) {
+               if (!torrents_.next()) {
+                       return false;
+               }
+               info.hash = torrents_.value(0).toString();
+               info.path = torrents_.value(1).toString();
+               info.download_path = torrents_.value(2).toString();
+               return true;
+       }
+
+
+private:
+       QSqlQuery torrents_;
+};
+
 } // namespace qtrapids
 
 #endif // _SERVERDB_HPP_