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