Added build script of Debian
[qtrapids] / src / server / TorrentHandle.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by Ixonos Plc   *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; version 2 of the License.               *
7  *                                                                         *
8  *   This program is distributed in the hope that it will be useful,       *
9  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
10  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
11  *   GNU General Public License for more details.                          *
12  *                                                                         *
13  *   You should have received a copy of the GNU General Public License     *
14  *   along with this program; if not, write to the                         *
15  *   Free Software Foundation, Inc.,                                       *
16  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
17  ***************************************************************************/
18
19 #include "TorrentHandle.hpp"
20 #include <QDebug>
21
22
23 namespace qtrapids
24 {
25
26 TorrentHandle::TorrentHandle(libtorrent::torrent_handle handle) :
27                 torrentHandle_(handle)
28 {
29 }
30
31
32 TorrentHandle::~TorrentHandle()
33 {
34 }
35
36
37 TorrentStatus_t TorrentHandle::status() const
38 {
39         return torrentHandle_.status();
40 }
41
42
43 torrent_info_cref TorrentHandle::getTorrentInfo() const
44 {
45         return torrentHandle_.get_torrent_info();
46 }
47
48
49 bool TorrentHandle::isValid() const
50 {
51         return torrentHandle_.is_valid();
52 }
53
54
55 QString TorrentHandle::name() const
56 {
57         return QString::fromStdString(torrentHandle_.name());
58 }
59
60 size_t TorrentHandle::getTotalSize() const
61 {
62         torrent_info_cref info = getTorrentInfo();
63         return static_cast<size_t> (info.total_size());
64 }
65
66 size_t TorrentHandle::getTotalDone() const
67 {
68         TorrentStatus_t statusTmp = status();
69         return static_cast<size_t> (statusTmp.total_done);
70 }
71
72 TorrentStatus::Id TorrentHandle::state() const
73 {
74         TorrentStatus::Id s = (TorrentStatus::Id)(status().state);
75         return ( (s < TorrentStatus::UNSPECIFIED)
76                  ? s : TorrentStatus::UNSPECIFIED );
77 }
78
79
80 float TorrentHandle::progress() const
81 {
82         TorrentStatus_t statusTmp = status();
83         return statusTmp.progress;
84 }
85
86 float TorrentHandle::uploadRate() const
87 {
88         TorrentStatus_t statusTmp = status();
89         return statusTmp.upload_rate;
90 }
91
92
93 float TorrentHandle::downloadRate() const
94 {
95         TorrentStatus_t statusTmp = status();
96         return statusTmp.download_rate;
97 }
98
99
100 qint32 TorrentHandle::numSeeds() const
101 {
102         TorrentStatus_t statusTmp = status();
103         return statusTmp.list_seeds;
104 }
105
106
107 qint32 TorrentHandle::numLeeches() const
108 {
109         TorrentStatus_t statusTmp = status();
110         return (statusTmp.list_peers - statusTmp.list_seeds);
111 }
112
113
114 qint32 TorrentHandle::ratio() const
115 {
116         TorrentStatus_t statusTmp = status();
117         size_t ratio;
118         if (statusTmp.total_payload_download == 0) {
119                 ratio = 0;
120         } else {
121                 ratio = static_cast<size_t> (statusTmp.total_payload_upload / statusTmp.total_payload_download);
122         }
123
124         return ratio;
125 }
126
127
128 torrent_handle_t TorrentHandle::getHandle() const
129 {
130         return torrentHandle_;
131 }
132
133
134 bool TorrentHandle::operator==(TorrentHandle const& h) const
135 {
136         return torrentHandle_ == h.torrentHandle_;
137 }
138
139
140 bool TorrentHandle::operator<(TorrentHandle const& h) const
141 {
142         return torrentHandle_ < h.torrentHandle_;
143 }
144
145
146 }