More dbg-output and small fixes...
[kdenetwork.git] / kget / transfer-plugins / bittorrent / bttransfer.cpp
blob7a89e8ec1c49c24d596f853a6b61620178112af4
1 /* This file is part of the KDE project
3 Copyright (C) 2007 Lukas Appelhans <l.appelhans@gmx.de>
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9 */
11 #include "bttransfer.h"
12 #include "bittorrentsettings.h"
14 #include "core/kget.h"
16 #include <torrent/torrent.h>
17 #include <peer/peermanager.h>
18 #include <util/error.h>
19 #include <torrent/globals.h>
20 #include <torrent/server.h>
21 #include <util/constants.h>
22 #include <util/functions.h>
23 #include <util/log.h>
24 #include <peer/authenticationmonitor.h>
25 #include <btdownload.h>
27 #include <KDebug>
28 #include <KLocale>
29 #include <KIconLoader>
30 #include <KStandardDirs>
31 #include <KUrl>
33 #include <QFile>
34 #include <QDomElement>
35 #include <QFileInfo>
37 BTTransfer::BTTransfer(TransferGroup* parent, TransferFactory* factory,
38 Scheduler* scheduler, const KUrl& src, const KUrl& dest,
39 const QDomElement * e)
40 : Transfer(parent, factory, scheduler, src, dest, e),
41 m_tmp(0),
42 m_dlLimit(BittorrentSettings::downloadLimit()),
43 m_ulLimit(BittorrentSettings::uploadLimit()),
44 torrent(0),
45 m_ready(false)
47 kDebug(5001);
48 if (m_source.url().isEmpty())
49 return;
51 if (!m_source.isLocalFile())
53 kDebug(5001) << m_dest.path();
54 BTDownload *download = new BTDownload(m_source);
56 setStatus(Job::Stopped, i18n("Downloading Torrent-File.."), SmallIcon("document-save"));
57 setTransferChange(Tc_Status, true);
59 m_source = KStandardDirs::locateLocal("appdata", "tmp/") + m_source.fileName();
60 connect(download, SIGNAL(finishedSuccessfully(KUrl)), SLOT(init(KUrl)));
62 else
63 init();
66 void BTTransfer::init(KUrl src)
68 if (src != m_source && !src.isEmpty())
69 m_source = src;
71 setStatus(Job::Stopped, i18n("Stopped"), SmallIcon("process-stop"));
72 setTransferChange(Tc_Status, true);
74 bt::InitLog(KStandardDirs::locateLocal("appdata", "torrentlog.log"));//initialize the torrent-log
76 bt::Uint16 i = 0;
79 kDebug(5001) << "Trying to set port to" << BittorrentSettings::port() + i;
80 bt::Globals::instance().initServer(BittorrentSettings::port() + i);
81 i++;
82 }while (!bt::Globals::instance().getServer().isOK() && i < 10);
84 if (!bt::Globals::instance().getServer().isOK())
85 return;
87 try
89 torrent = new bt::TorrentControl();
91 if (!BittorrentSettings::tmpDir().isEmpty())
93 m_tmp = BittorrentSettings::tmpDir();
94 kDebug(5001) << "Trying to set" << m_tmp << " as tmpDir";
95 if (!QFileInfo(m_tmp).isDir())
96 m_tmp = KStandardDirs::locateLocal("appdata", "tmp/");
98 else
99 m_tmp = KStandardDirs::locateLocal("appdata", "tmp/");
101 m_ready = true;
103 torrent->init(0, m_source.url().remove("file://"), m_tmp + m_source.fileName().remove(".torrent"),
104 m_dest.directory().remove("file://"), 0);
106 if (torrent->getStats().multi_file_torrent)
107 m_dest = torrent->getStats().output_path;
108 else
109 m_dest = torrent->getDataDir() + torrent->getStats().torrent_name;
111 torrent->createFiles();
113 torrent->setPreallocateDiskSpace(BittorrentSettings::preAlloc());
114 //torrent->setMaxShareRatio(1); //TODO: Make configurable...
115 kDebug(5001) << "Source:" << m_source.url();
116 kDebug(5001) << "Dest:" << m_dest.url();
117 kDebug(5001) << "Temp:" << m_tmp;
119 connect(torrent, SIGNAL(stoppedByError(bt::TorrentInterface*, QString)), SLOT(slotStoppedByError(bt::TorrentInterface*, QString)));
120 connect(torrent, SIGNAL(finished(bt::TorrentInterface*)), this, SLOT(slotDownloadFinished(bt::TorrentInterface* )));
121 //FIXME connect(tc,SIGNAL(corruptedDataFound( bt::TorrentInterface* )), this, SLOT(emitCorruptedData( bt::TorrentInterface* )));
123 catch (bt::Error &err)
125 kDebug(5001) << err.toString();
127 connect(&timer, SIGNAL(timeout()), SLOT(update()));
130 BTTransfer::~BTTransfer()
132 kDebug(5001);
133 if (torrent)
134 delete torrent;
137 bool BTTransfer::isResumable() const
139 kDebug(5001);
140 return true;
143 void BTTransfer::start()
145 kDebug(5001);
147 if (m_ready)
149 kDebug(5001) << "Going to download that stuff :-0";
150 kDebug(5001) << "Here we are";
151 torrent->start();
152 kDebug(5001) << "Got started??";
153 timer.start(250);
154 setStatus(Job::Running, i18n("Downloading.."), SmallIcon("media-playback-start"));
155 kDebug(5001) << "Jepp, it does";
156 m_totalSize = totalSize();
157 setTransferChange(Tc_Status | Tc_TrackersList | Tc_TotalSize, true);
158 kDebug(5001) << "Completely";
159 setTrafficLimits(m_ulLimit, m_dlLimit);
163 void BTTransfer::stop()
165 kDebug(5001);
166 if (m_ready)
168 torrent->stop(true);
169 m_speed = 0;
170 timer.stop();
171 setStatus(Job::Stopped, i18n("Stopped"), SmallIcon("process-stop"));
172 setTransferChange(Tc_Status, true);
176 void BTTransfer::update()
178 kDebug(5001);
180 if (torrent)
182 kDebug(5001) << "Update torrent";
183 bt::UpdateCurrentTime();
184 bt::AuthenticationMonitor::instance().update();
185 kDebug(5001) << "Ignore this ;)";
186 torrent->update();
187 kDebug(5001) << "Hhmpf";
188 m_speed = dlRate();
189 m_percent = percent();
191 setTransferChange(Tc_ProcessedSize | Tc_Speed | Tc_Percent, true);
193 else
194 timer.stop();
197 void BTTransfer::save(QDomElement e) // krazy:exclude=passbyvalue
201 void BTTransfer::load(const QDomElement &e)
205 void BTTransfer::slotStoppedByError(bt::TorrentInterface* error, QString errormsg)
207 kDebug(5001) << errormsg;
210 void BTTransfer::setPort(int port)
212 kDebug(5001);
213 bt::Globals::instance().getServer().changePort(port);
216 void BTTransfer::slotDownloadFinished(bt::TorrentInterface* ti)
218 kDebug(5001);
219 timer.stop();
220 setStatus(Job::Finished, i18n("Finished"), SmallIcon("ok"));
221 setTransferChange(Tc_Status, true);
224 void BTTransfer::setTrafficLimits(int ulLimit, int dlLimit)
226 kDebug(5001);
227 if (!torrent)
228 return;
230 torrent->setTrafficLimits(ulLimit * 1000, dlLimit * 1000);
231 m_dlLimit = dlLimit;
232 m_ulLimit = ulLimit;
235 /**Property-Functions**/
236 KUrl::List BTTransfer::trackersList() const
238 kDebug(5001);
239 if (!torrent)
240 return KUrl::List();
242 const KUrl::List trackers = torrent->getTrackersList()->getTrackerURLs();
243 return trackers;
246 int BTTransfer::dlRate() const
248 kDebug(5001);
249 if (!torrent)
250 return -1;
252 return torrent->getStats().download_rate;
255 int BTTransfer::ulRate() const
257 kDebug(5001);
258 if (!torrent)
259 return -1;
261 return torrent->getStats().upload_rate;
264 int BTTransfer::totalSize() const
266 kDebug(5001);
267 if (!torrent)
268 return -1;
270 return torrent->getStats().total_bytes_to_download;
273 int BTTransfer::sessionBytesDownloaded() const
275 kDebug(5001);
276 if (!torrent)
277 return -1;
279 return torrent->getStats().session_bytes_downloaded;
282 int BTTransfer::sessionBytesUploaded() const
284 kDebug(5001);
285 if (!torrent)
286 return -1;
288 return torrent->getStats().session_bytes_uploaded;
291 int BTTransfer::chunksTotal() const
293 kDebug(5001);
294 if (!torrent)
295 return -1;
297 return torrent->getTorrent().getNumChunks();
300 int BTTransfer::chunksDownloaded() const
302 kDebug(5001);
303 if (!torrent)
304 return -1;
306 return torrent->downloadedChunksBitSet().numOnBits();
309 int BTTransfer::chunksExcluded() const
311 kDebug(5001);
312 if (!torrent)
313 return -1;
315 return torrent->excludedChunksBitSet().numOnBits();
318 int BTTransfer::chunksLeft() const
320 kDebug(5001);
321 if (!torrent)
322 return -1;
324 return chunksTotal() - chunksDownloaded();
327 int BTTransfer::seedsConnected() const
329 kDebug(5001);
330 if (!torrent)
331 return -1;
333 return torrent->getStats().seeders_connected_to;
336 int BTTransfer::seedsDisconnected() const
338 kDebug(5001);
339 if (!torrent)
340 return -1;
342 return torrent->getStats().seeders_total;
345 int BTTransfer::leechesConnected() const
347 kDebug(5001);
348 if (!torrent)
349 return -1;
351 return torrent->getStats().leechers_connected_to;
354 int BTTransfer::leechesDisconnected() const
356 kDebug(5001);
357 if (!torrent)
358 return -1;
360 return torrent->getStats().leechers_total;
363 int BTTransfer::elapsedTime() const
365 if (!torrent)
366 return -1;
368 kDebug(5001);
369 return torrent->getRunningTimeDL();
372 int BTTransfer::remainingTime() const
374 if (!torrent)
375 return -1;
377 kDebug(5001);
378 return torrent->getETA();
381 int BTTransfer::ulLimit() const
383 kDebug(5001);
384 if (!torrent)
385 return -1;
388 int BTTransfer::dlLimit() const
390 kDebug(5001);
391 if (!torrent)
392 return -1;
395 bt::TorrentControl * BTTransfer::torrentControl()
397 kDebug(5001);
398 return torrent;
401 int BTTransfer::percent() const
403 kDebug(5001);
404 if (!torrent)
405 return -1;
407 return ((float) chunksDownloaded() / (float) chunksTotal()) * 100;
410 bool BTTransfer::ready()
412 kDebug(5001);
413 return m_ready;
416 #include "bttransfer.moc"