- Uncommitted files
[qtrapids] / src / engine / QBittorrentSession.cpp
1 /***************************************************************************
2  *   Copyright (C) 2009 by Lassi Väätämöinen   *
3  *   lassi.vaatamoinen@ixonos.com   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include <QDebug>
22
23 #include "AlertWaiterThread.h"
24 #include "QBittorrentSession.h"
25
26 namespace qtrapids
27 {
28
29 QBittorrentSession::QBittorrentSession(QObject *parent):
30                 QObject(parent),
31                 btSession_(),
32                 alertWaiter_(NULL)
33 {
34         alertWaiter_ = new AlertWaiterThread(&btSession_, this);
35         alertWaiter_->allAlerts();
36         connect(alertWaiter_, SIGNAL(alert(Alert const*)), this, SLOT(on_alert(Alert const*)));
37         alertWaiter_->start();
38         
39         SessionSettings settings;
40         settings.ignore_limits_on_local_network = false;
41         btSession_.set_settings(settings);
42         
43 }
44
45
46 QBittorrentSession::~QBittorrentSession()
47 {
48 }
49
50
51 qtrapids::QTorrentHandle
52 QBittorrentSession::addTorrent(AddTorrentParams const& params)
53 {
54         // Delegate to Libtorrent and return QTorrentHandle.
55         //std::auto_ptr<QTorrentHandle> handlePtr(new QTorrentHandle(btSession_.add_torrent(params)));
56         qtrapids::QTorrentHandle  handle = qtrapids::QTorrentHandle(btSession_.add_torrent(params));
57         return handle;
58 }
59
60
61 void QBittorrentSession::removeTorrent(qtrapids::QTorrentHandle const& handle)
62 {
63         btSession_.remove_torrent(handle.getHandle());
64 }
65
66
67 void QBittorrentSession::setUploadRateLimit(int rate)
68 {
69         btSession_.set_upload_rate_limit(rate);
70 }
71
72
73 void QBittorrentSession::setDownloadrateLimit(int rate)
74 {
75         btSession_.set_download_rate_limit(rate);
76 }
77
78
79 int QBittorrentSession::getUploadRateLimt() const
80 {
81         return btSession_.upload_rate_limit();
82 }
83
84
85 int QBittorrentSession::getDownloadRateLimit() const
86 {
87         return btSession_.download_rate_limit();
88 }
89
90
91
92
93
94 // ========================== SLOTS ==============================
95 /// @TODO This function is called when AlertWaiterThread emits alert()
96 /// If connection is direct, as it is now, we need to use QMutex here (if necessary?)
97 void QBittorrentSession::on_alert(Alert const *al)
98 //NOTE: al parameter not necessarily needed here, as we pop_alert() now!
99 {
100
101 #ifdef QTRAPIDS_DEBUG
102         //if (al)
103         //      qDebug() << "on_alert():" << QString::fromStdString(al->message());
104 #endif
105
106         std::auto_ptr<Alert> alertPtr = btSession_.pop_alert();
107         emit alert(alertPtr);
108 }
109
110 } //namespace qtrapids