- Torrent addign and removal functional
[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 QString QTorrentHandle::state() const
65 {
66         return GetStatusString(status());
67 }
68
69 float QTorrentHandle::progress() const
70 {
71         TorrentStatus statusTmp = status();
72         return statusTmp.progress;
73 }
74
75 float QTorrentHandle::uploadRate() const
76 {
77         TorrentStatus statusTmp = status();
78         return statusTmp.upload_rate;
79 }
80
81
82 float QTorrentHandle::downloadRate() const
83 {
84         TorrentStatus statusTmp = status();
85         return statusTmp.download_rate;
86 }
87
88
89 qint32 QTorrentHandle::numSeeds() const
90 {
91         TorrentStatus statusTmp = status();
92         return statusTmp.list_seeds;
93 }
94
95
96 qint32 QTorrentHandle::numLeeches() const
97 {
98         TorrentStatus statusTmp = status();
99         return (statusTmp.list_peers - statusTmp.list_seeds);
100 }
101
102
103 qint32 QTorrentHandle::ratio() const
104 {
105         TorrentStatus statusTmp = status();
106         size_t ratio; 
107         if (statusTmp.total_payload_download == 0) {
108                 ratio = 0;
109         } else {
110                 ratio = static_cast<size_t> (statusTmp.total_payload_upload / statusTmp.total_payload_download);
111         }
112         
113         return ratio;
114 }
115
116
117 TorrentHandle QTorrentHandle::getHandle() const
118 {
119         return torrentHandle_;
120 }
121
122
123 bool QTorrentHandle::operator==(QTorrentHandle const& h) const
124 {
125         return torrentHandle_ == h.torrentHandle_;
126 }
127
128
129 bool QTorrentHandle::operator<(QTorrentHandle const& h) const
130 {
131         return torrentHandle_ < h.torrentHandle_;
132 }
133
134
135
136 QString QTorrentHandle::GetStatusString(TorrentStatus const& status) const
137 {
138         switch (status.state) {
139                 case TorrentStatus::queued_for_checking :
140                         return "Queued";
141                 case TorrentStatus::checking_files :
142                         return "Checking";
143                 case TorrentStatus::downloading_metadata :
144                         return "DL meta";
145                 case TorrentStatus::downloading :
146                         return "DL";
147                 case TorrentStatus::finished :
148                         return "Finished";
149                 case TorrentStatus::seeding :
150                         return "Seeding"; 
151                 case TorrentStatus::allocating :
152                         return "Allocating";
153                 default:
154                         return "N/A";
155         }
156 }
157
158
159