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