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