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