- Added daemon start functionality
[qtrapids] / src / server / TorrentHandle.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
21 #include "TorrentHandle.hpp"
22 #include <QDebug>
23
24
25 namespace qtrapids
26 {
27
28 TorrentHandle::TorrentHandle(libtorrent::torrent_handle handle) :
29                 torrentHandle_(handle)
30 {
31 }
32
33
34 TorrentHandle::~TorrentHandle()
35 {
36 }
37
38
39 TorrentStatus_t TorrentHandle::status() const
40 {
41         return torrentHandle_.status();
42 }
43
44
45 torrent_info_cref TorrentHandle::getTorrentInfo() const
46 {
47         return torrentHandle_.get_torrent_info();
48 }
49
50
51 bool TorrentHandle::isValid() const
52 {
53         return torrentHandle_.is_valid();
54 }
55
56
57 QString TorrentHandle::name() const
58 {
59         return QString::fromStdString(torrentHandle_.name());
60 }
61
62 size_t TorrentHandle::getTotalSize() const
63 {
64         torrent_info_cref info = getTorrentInfo();
65         return static_cast<size_t> (info.total_size());
66 }
67
68 size_t TorrentHandle::getTotalDone() const
69 {
70         TorrentStatus_t statusTmp = status();
71         return static_cast<size_t> (statusTmp.total_done);
72 }
73
74 TorrentStatus::Id TorrentHandle::state() const
75 {
76         TorrentStatus::Id s = (TorrentStatus::Id)(status().state);
77         return ( (s < TorrentStatus::UNSPECIFIED)
78                  ? s : TorrentStatus::UNSPECIFIED );
79 }
80
81
82 float TorrentHandle::progress() const
83 {
84         TorrentStatus_t statusTmp = status();
85         return statusTmp.progress;
86 }
87
88 float TorrentHandle::uploadRate() const
89 {
90         TorrentStatus_t statusTmp = status();
91         return statusTmp.upload_rate;
92 }
93
94
95 float TorrentHandle::downloadRate() const
96 {
97         TorrentStatus_t statusTmp = status();
98         return statusTmp.download_rate;
99 }
100
101
102 qint32 TorrentHandle::numSeeds() const
103 {
104         TorrentStatus_t statusTmp = status();
105         return statusTmp.list_seeds;
106 }
107
108
109 qint32 TorrentHandle::numLeeches() const
110 {
111         TorrentStatus_t statusTmp = status();
112         return (statusTmp.list_peers - statusTmp.list_seeds);
113 }
114
115
116 qint32 TorrentHandle::ratio() const
117 {
118         TorrentStatus_t statusTmp = status();
119         size_t ratio;
120         if (statusTmp.total_payload_download == 0) {
121                 ratio = 0;
122         } else {
123                 ratio = static_cast<size_t> (statusTmp.total_payload_upload / statusTmp.total_payload_download);
124         }
125
126         return ratio;
127 }
128
129
130 torrent_handle_t TorrentHandle::getHandle() const
131 {
132         return torrentHandle_;
133 }
134
135
136 bool TorrentHandle::operator==(TorrentHandle const& h) const
137 {
138         return torrentHandle_ == h.torrentHandle_;
139 }
140
141
142 bool TorrentHandle::operator<(TorrentHandle const& h) const
143 {
144         return torrentHandle_ < h.torrentHandle_;
145 }
146
147
148 }