- Licence text updated to only allow GPLv2
[qtrapids] / src / engine / QTorrentHandle.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 #include <QDebug>
19
20 #include "QTorrentHandle.h"
21
22 namespace qtrapids
23 {
24
25
26 QTorrentHandle::QTorrentHandle(libtorrent::torrent_handle handle) :
27                 torrentHandle_(handle)
28 {
29 }
30
31
32 QTorrentHandle::~QTorrentHandle()
33 {
34 }
35
36
37 TorrentStatus QTorrentHandle::status() const
38 {
39         return torrentHandle_.status();
40 }
41
42
43 TorrentInfo const& QTorrentHandle::getTorrentInfo() const
44 {
45         return torrentHandle_.get_torrent_info();
46 }
47
48
49 bool QTorrentHandle::isValid() const
50 {
51         return torrentHandle_.is_valid();
52 }
53
54
55 QString QTorrentHandle::name() const
56 {
57         return QString::fromStdString(torrentHandle_.name());
58 }
59
60 size_t QTorrentHandle::getTotalSize() const
61 {
62         TorrentInfo info = getTorrentInfo();
63         return static_cast<size_t> (info.total_size());
64 }
65
66
67 QTorrentHandle::State QTorrentHandle::state() const
68 {
69         TorrentStatus statusTmp = status();
70
71         switch (statusTmp.state) {
72         case TorrentStatus::queued_for_checking :
73                 return QTorrentHandle::QUEUED_FOR_CHECKING;
74         case TorrentStatus::checking_files :
75                 return QTorrentHandle::CHECKING_FILES;
76         case TorrentStatus::downloading_metadata :
77                 return QTorrentHandle::DOWNLOADING_METADATA;
78         case TorrentStatus::downloading :
79                 return QTorrentHandle::DOWNLOADING;
80         case TorrentStatus::finished :
81                 return QTorrentHandle::FINISHED;
82         case TorrentStatus::seeding :
83                 return QTorrentHandle::SEEDING;
84         case TorrentStatus::allocating :
85                 return QTorrentHandle::ALLOCATING;
86         default:
87                 return QTorrentHandle::UNSPECIFIED;
88         }
89
90 }
91
92
93 float QTorrentHandle::progress() const
94 {
95         TorrentStatus statusTmp = status();
96         return statusTmp.progress;
97 }
98
99 float QTorrentHandle::uploadRate() const
100 {
101         TorrentStatus statusTmp = status();
102         return statusTmp.upload_rate;
103 }
104
105
106 float QTorrentHandle::downloadRate() const
107 {
108         TorrentStatus statusTmp = status();
109         return statusTmp.download_rate;
110 }
111
112
113 qint32 QTorrentHandle::numSeeds() const
114 {
115         TorrentStatus statusTmp = status();
116         return statusTmp.list_seeds;
117 }
118
119
120 qint32 QTorrentHandle::numLeeches() const
121 {
122         TorrentStatus statusTmp = status();
123         return (statusTmp.list_peers - statusTmp.list_seeds);
124 }
125
126
127 qint32 QTorrentHandle::ratio() const
128 {
129         TorrentStatus statusTmp = status();
130         size_t ratio;
131         if (statusTmp.total_payload_download == 0) {
132                 ratio = 0;
133         } else {
134                 ratio = static_cast<size_t> (statusTmp.total_payload_upload / statusTmp.total_payload_download);
135         }
136
137         return ratio;
138 }
139
140
141 TorrentHandle QTorrentHandle::getHandle() const
142 {
143         return torrentHandle_;
144 }
145
146
147 bool QTorrentHandle::operator==(QTorrentHandle const& h) const
148 {
149         return torrentHandle_ == h.torrentHandle_;
150 }
151
152
153 bool QTorrentHandle::operator<(QTorrentHandle const& h) const
154 {
155         return torrentHandle_ < h.torrentHandle_;
156 }
157
158 } // namespace qtrapids
159
160