486848dc3659f1fbbfcfa7c30f3c34f66d0b74dd
[qtrapids] / src / server / TorrentSession.cpp
1 #include "TorrentSession.hpp"
2 #include "TorrentHandle.hpp"
3 #include "AlertWaiterThread.hpp"
4
5
6 namespace qtrapids
7 {
8
9
10
11     TorrentSession::TorrentSession(QObject *parent, QSettings *settings)
12         : QObject(parent)
13         , btSession_()
14                 , alertWaiter_(new AlertWaiterThread(&btSession_, this))
15     {
16         qDBusRegisterMetaType<qtrapids::TorrentState>();
17         qDBusRegisterMetaType<qtrapids::ParamsMap_t>();
18         new QtRapidsServer(this);
19
20         QDBusConnection dbus = QDBusConnection::sessionBus();
21         dbus.registerObject("/qtrapids", this);
22         dbus.registerService("com.ixonos.qtrapids");
23
24         alertWaiter_->allAlerts();
25         connect(alertWaiter_, SIGNAL(alert()), this, SLOT(on_alert()));
26         alertWaiter_->start();
27
28
29     }
30
31     void TorrentSession::on_alert()
32     //NOTE: al parameter not necessarily needed here, as we pop_alert() now!
33     {
34
35         //qDebug() << "QBittorrentSession:on_alert(" << al << ")";
36         //      if (al)
37         //              qDebug() << "on_alert():" << QString::fromStdString(al->message());
38
39         std::auto_ptr<alert_t> alertPtr = btSession_.pop_alert();
40
41         if (alertPtr.get() != NULL) {
42
43             torrent_alert_t *ta = dynamic_cast<torrent_alert_t*> (alertPtr.get());
44
45             qDebug()
46                                 << "QBittorrentSession::on_alert(): "
47                                 << QString::fromStdString(alertPtr->message());
48
49
50             if (ta) {
51                 TorrentHandle handle(ta->handle);
52                 TorrentState state;
53
54                 state.hash = Hash2QStr(handle.hash());
55                 state.state = handle.state();
56                 state.progress = handle.progress() * torrent_progress_max;
57                 state.down_rate = handle.downloadRate();
58                 state.up_rate = handle.uploadRate();
59                 state.seeds = handle.numSeeds();
60                 state.leeches = handle.numLeeches();
61             
62                 ParamsMap_t params;
63                 emit alert(state, params);
64             }
65
66         }
67     }
68
69     void TorrentSession::getState()
70     {
71         torrents_t::const_iterator p;
72         for (p = torrents_.constBegin(); p != torrents_.constEnd(); ++p) {
73             TorrentHandlePtr handle = *p;
74             TorrentState state;
75             QString hash = Hash2QStr(handle->hash());
76             
77             state.hash = hash;
78             state.name = handle->name();
79             state.state = handle->state();
80             state.progress = handle->progress() * torrent_progress_max;
81             state.down_rate = handle->downloadRate();
82             state.up_rate = handle->uploadRate();
83             state.seeds = handle->numSeeds();
84             state.leeches = handle->numLeeches();
85             state.total_size = handle->getTotalSize();
86
87             emit alert(state, ParamsMap_t());
88         }
89     }
90
91     void TorrentSession::addTorrent(const QString &path, const QString &save_path
92                                     , qtrapids::ParamsMap_t other_params)
93     {
94         add_torrent_params_t addParams;
95         QFile torrent_file(path);
96         if (!torrent_file.exists()) {
97             qWarning() << "Torrent file " << path << "doesn't exist";
98             return;
99         }
100
101         qDebug() << "addTorrent: " << path << " save to " << save_path;
102         boost::intrusive_ptr<libtorrent::torrent_info> tiTmp
103             = new libtorrent::torrent_info
104             (boost::filesystem::path(path.toStdString()));
105         addParams.ti = tiTmp;
106
107         // save_path is the only mandatory parameter, rest are optional.
108         addParams.save_path = boost::filesystem::path(save_path.toStdString());
109         //addParams.storage_mode = libtorrent::storage_mode_allocate;
110
111         TorrentHandlePtr handle(new TorrentHandle(btSession_.add_torrent(addParams)));
112         QString hash = Hash2QStr(handle->hash());
113
114         TorrentState state;
115
116         state.hash = hash;
117         state.name = handle->name();
118         state.state = handle->state();
119         state.progress = handle->progress() * torrent_progress_max;
120         state.down_rate = handle->downloadRate();
121         state.up_rate = handle->uploadRate();
122         state.seeds = handle->numSeeds();
123         state.leeches = handle->numLeeches();
124         state.total_size = handle->getTotalSize();
125
126         torrents_[hash] = handle;
127
128         emit alert(state, ParamsMap_t());
129     }
130
131     void TorrentSession::removeTorrent(const QString &hash)
132     {
133         torrents_t::iterator p = torrents_.find(hash);
134         if (p == torrents_.end()) {
135             qDebug() << "Invalid request to remove torrent with hash " << hash;
136             return;
137         }
138         try {
139             btSession_.remove_torrent(p.value()->getHandle());
140         } catch (torrent_exception_t e) {
141             qDebug() << // e.what()
142                 "exception catched"
143                 ;
144         }
145     }
146
147
148 } // namespace qtrapids