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