aa1f1056f2cb5b806a6d447035f456cd8a89c585
[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
52                 if (!ta->handle.is_valid()) {
53                     qDebug() << "handle is invalid";
54                     return;
55                 }
56
57                 TorrentHandle handle(ta->handle);
58                 TorrentState state;
59
60                 state.hash = Hash2QStr(handle.hash());
61                 state.state = handle.state();
62                 state.progress = handle.progress() * torrent_progress_max;
63                 state.down_rate = handle.downloadRate();
64                 state.up_rate = handle.uploadRate();
65                 state.seeds = handle.numSeeds();
66                 state.leeches = handle.numLeeches();
67             
68                 ParamsMap_t params;
69                 emit alert(state, params);
70             }
71
72         }
73     }
74
75     void TorrentSession::getState()
76     {
77         torrents_t::const_iterator p;
78         for (p = torrents_.constBegin(); p != torrents_.constEnd(); ++p) {
79             TorrentHandlePtr handle = *p;
80             TorrentState state;
81             QString hash = Hash2QStr(handle->hash());
82             
83             state.hash = hash;
84             state.name = handle->name();
85             state.state = handle->state();
86             state.progress = handle->progress() * torrent_progress_max;
87             state.down_rate = handle->downloadRate();
88             state.up_rate = handle->uploadRate();
89             state.seeds = handle->numSeeds();
90             state.leeches = handle->numLeeches();
91             state.total_size = handle->getTotalSize();
92
93             emit alert(state, ParamsMap_t());
94         }
95     }
96
97     void TorrentSession::addTorrent(const QString &path, const QString &save_path
98                                     , qtrapids::ParamsMap_t other_params)
99     {
100         add_torrent_params_t addParams;
101         QFile torrent_file(path);
102         if (!torrent_file.exists()) {
103             qWarning() << "Torrent file " << path << "doesn't exist";
104             return;
105         }
106
107         qDebug() << "addTorrent: " << path << " save to " << save_path;
108         boost::intrusive_ptr<libtorrent::torrent_info> tiTmp
109             = new libtorrent::torrent_info
110             (boost::filesystem::path(path.toStdString()));
111         addParams.ti = tiTmp;
112
113         // save_path is the only mandatory parameter, rest are optional.
114         addParams.save_path = boost::filesystem::path(save_path.toStdString());
115         //addParams.storage_mode = libtorrent::storage_mode_allocate;
116
117         TorrentHandlePtr handle(new TorrentHandle(btSession_.add_torrent(addParams)));
118         QString hash = Hash2QStr(handle->hash());
119
120         TorrentState state;
121
122         state.hash = hash;
123         state.name = handle->name();
124         state.state = handle->state();
125         state.progress = handle->progress() * torrent_progress_max;
126         state.down_rate = handle->downloadRate();
127         state.up_rate = handle->uploadRate();
128         state.seeds = handle->numSeeds();
129         state.leeches = handle->numLeeches();
130         state.total_size = handle->getTotalSize();
131
132         torrents_[hash] = handle;
133
134         emit alert(state, ParamsMap_t());
135     }
136
137     void TorrentSession::removeTorrent(const QString &hash)
138     {
139         torrents_t::iterator p = torrents_.find(hash);
140         if (p == torrents_.end()) {
141             qDebug() << "Invalid request to remove torrent with hash " << hash;
142             return;
143         }
144         try {
145             btSession_.remove_torrent(p.value()->getHandle());
146         } catch (torrent_exception_t e) {
147             qDebug() << // e.what()
148                 "exception catched"
149                 ;
150         }
151     }
152
153
154 } // namespace qtrapids