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.
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>
24 #include <peer/authenticationmonitor.h>
25 #include <btdownload.h>
29 #include <KIconLoader>
30 #include <KStandardDirs>
34 #include <QDomElement>
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
),
42 m_dlLimit(BittorrentSettings::downloadLimit()),
43 m_ulLimit(BittorrentSettings::uploadLimit()),
48 if (m_source
.url().isEmpty())
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
)));
66 void BTTransfer::init(KUrl src
)
68 if (src
!= m_source
&& !src
.isEmpty())
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
79 kDebug(5001) << "Trying to set port to" << BittorrentSettings::port() + i
;
80 bt::Globals::instance().initServer(BittorrentSettings::port() + i
);
82 }while (!bt::Globals::instance().getServer().isOK() && i
< 10);
84 if (!bt::Globals::instance().getServer().isOK())
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/");
99 m_tmp
= KStandardDirs::locateLocal("appdata", "tmp/");
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
;
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()
137 bool BTTransfer::isResumable() const
143 void BTTransfer::start()
149 kDebug(5001) << "Going to download that stuff :-0";
150 kDebug(5001) << "Here we are";
152 kDebug(5001) << "Got started??";
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()
171 setStatus(Job::Stopped
, i18n("Stopped"), SmallIcon("process-stop"));
172 setTransferChange(Tc_Status
, true);
176 void BTTransfer::update()
182 kDebug(5001) << "Update torrent";
183 bt::UpdateCurrentTime();
184 bt::AuthenticationMonitor::instance().update();
185 kDebug(5001) << "Ignore this ;)";
187 kDebug(5001) << "Hhmpf";
189 m_percent
= percent();
191 setTransferChange(Tc_ProcessedSize
| Tc_Speed
| Tc_Percent
, true);
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
)
213 bt::Globals::instance().getServer().changePort(port
);
216 void BTTransfer::slotDownloadFinished(bt::TorrentInterface
* ti
)
220 setStatus(Job::Finished
, i18n("Finished"), SmallIcon("ok"));
221 setTransferChange(Tc_Status
, true);
224 void BTTransfer::setTrafficLimits(int ulLimit
, int dlLimit
)
230 torrent
->setTrafficLimits(ulLimit
* 1000, dlLimit
* 1000);
235 /**Property-Functions**/
236 KUrl::List
BTTransfer::trackersList() const
242 const KUrl::List trackers
= torrent
->getTrackersList()->getTrackerURLs();
246 int BTTransfer::dlRate() const
252 return torrent
->getStats().download_rate
;
255 int BTTransfer::ulRate() const
261 return torrent
->getStats().upload_rate
;
264 int BTTransfer::totalSize() const
270 return torrent
->getStats().total_bytes_to_download
;
273 int BTTransfer::sessionBytesDownloaded() const
279 return torrent
->getStats().session_bytes_downloaded
;
282 int BTTransfer::sessionBytesUploaded() const
288 return torrent
->getStats().session_bytes_uploaded
;
291 int BTTransfer::chunksTotal() const
297 return torrent
->getTorrent().getNumChunks();
300 int BTTransfer::chunksDownloaded() const
306 return torrent
->downloadedChunksBitSet().numOnBits();
309 int BTTransfer::chunksExcluded() const
315 return torrent
->excludedChunksBitSet().numOnBits();
318 int BTTransfer::chunksLeft() const
324 return chunksTotal() - chunksDownloaded();
327 int BTTransfer::seedsConnected() const
333 return torrent
->getStats().seeders_connected_to
;
336 int BTTransfer::seedsDisconnected() const
342 return torrent
->getStats().seeders_total
;
345 int BTTransfer::leechesConnected() const
351 return torrent
->getStats().leechers_connected_to
;
354 int BTTransfer::leechesDisconnected() const
360 return torrent
->getStats().leechers_total
;
363 int BTTransfer::elapsedTime() const
369 return torrent
->getRunningTimeDL();
372 int BTTransfer::remainingTime() const
378 return torrent
->getETA();
381 int BTTransfer::ulLimit() const
388 int BTTransfer::dlLimit() const
395 bt::TorrentControl
* BTTransfer::torrentControl()
401 int BTTransfer::percent() const
407 return ((float) chunksDownloaded() / (float) chunksTotal()) * 100;
410 bool BTTransfer::ready()
416 #include "bttransfer.moc"