Code formatting/indentation unified in trunk
[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     {
75     case TorrentStatus::queued_for_checking :
76         return QTorrentHandle::QUEUED_FOR_CHECKING;
77     case TorrentStatus::checking_files :
78         return QTorrentHandle::CHECKING_FILES;
79     case TorrentStatus::downloading_metadata :
80         return QTorrentHandle::DOWNLOADING_METADATA;
81     case TorrentStatus::downloading :
82         return QTorrentHandle::DOWNLOADING;
83     case TorrentStatus::finished :
84         return QTorrentHandle::FINISHED;
85     case TorrentStatus::seeding :
86         return QTorrentHandle::SEEDING;
87     case TorrentStatus::allocating :
88         return QTorrentHandle::ALLOCATING;
89     default:
90         return QTorrentHandle::UNSPECIFIED;
91     }
92
93 }
94
95
96 float QTorrentHandle::progress() const
97 {
98     TorrentStatus statusTmp = status();
99     return statusTmp.progress;
100 }
101
102 float QTorrentHandle::uploadRate() const
103 {
104     TorrentStatus statusTmp = status();
105     return statusTmp.upload_rate;
106 }
107
108
109 float QTorrentHandle::downloadRate() const
110 {
111     TorrentStatus statusTmp = status();
112     return statusTmp.download_rate;
113 }
114
115
116 qint32 QTorrentHandle::numSeeds() const
117 {
118     TorrentStatus statusTmp = status();
119     return statusTmp.list_seeds;
120 }
121
122
123 qint32 QTorrentHandle::numLeeches() const
124 {
125     TorrentStatus statusTmp = status();
126     return (statusTmp.list_peers - statusTmp.list_seeds);
127 }
128
129
130 qint32 QTorrentHandle::ratio() const
131 {
132     TorrentStatus statusTmp = status();
133     size_t ratio;
134     if (statusTmp.total_payload_download == 0)
135     {
136         ratio = 0;
137     }
138     else
139     {
140         ratio = static_cast<size_t> (statusTmp.total_payload_upload / statusTmp.total_payload_download);
141     }
142
143     return ratio;
144 }
145
146
147 TorrentHandle QTorrentHandle::getHandle() const
148 {
149     return torrentHandle_;
150 }
151
152
153 bool QTorrentHandle::operator==(QTorrentHandle const& h) const
154 {
155     return torrentHandle_ == h.torrentHandle_;
156 }
157
158
159 bool QTorrentHandle::operator<(QTorrentHandle const& h) const
160 {
161     return torrentHandle_ < h.torrentHandle_;
162 }
163
164 } // namespace qtrapids
165
166