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