5084f5d60ceec428f69e3c55c2e454276a4dc0cd
[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
40
41 QBittorrentSession::~QBittorrentSession()
42 {
43 }
44
45
46 qtrapids::QTorrentHandle
47 QBittorrentSession::addTorrent(AddTorrentParams const& params)
48 {
49         // Delegate to Libtorrent and return QTorrentHandle.
50         //std::auto_ptr<QTorrentHandle> handlePtr(new QTorrentHandle(btSession_.add_torrent(params)));
51         qtrapids::QTorrentHandle  handle = qtrapids::QTorrentHandle(btSession_.add_torrent(params));
52         return handle;
53 }
54
55
56 void QBittorrentSession::removeTorrent(qtrapids::QTorrentHandle const& handle)
57 {
58         btSession_.remove_torrent(handle.getHandle());
59 }
60
61
62 // ========================== SLOTS ==============================
63 /// @TODO This function is called when AlertWaiterThread emits alert()
64 /// If connection is direct, as it is now, we need to use QMutex here (if necessary?)
65 void QBittorrentSession::on_alert(Alert const *al)
66 //NOTE: al parameter not necessarily needed here, as we pop_alert() now!
67 {
68
69 #ifdef QTRAPIDS_DEBUG
70         if (al)
71                 qDebug() << "on_alert():" << QString::fromStdString(al->message());
72 #endif
73
74         std::auto_ptr<Alert> alertPtr = btSession_.pop_alert();
75         emit alert(alertPtr);
76 }
77
78 } //namespace qtrapids