kget
btdownload.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "btdownload.h"
00011
00012 #include <QFile>
00013 #include <QFileInfo>
00014
00015 #include <KDebug>
00016 #include <KStandardDirs>
00017
00018 BTDownload::BTDownload(const KUrl &srcUrl)
00019 : m_srcUrl(srcUrl),
00020 m_destUrl(KStandardDirs::locateLocal("appdata", "tmp/"))
00021 {
00022 kDebug(5001) << "DownloadFile:" << m_srcUrl.url();
00023 KIO::TransferJob *m_copyJob = KIO::get(m_srcUrl, KIO::NoReload, KIO::HideProgressInfo);
00024 connect(m_copyJob, SIGNAL(data(KIO::Job*,const QByteArray &)), SLOT(slotData(KIO::Job*, const QByteArray&)));
00025 connect(m_copyJob, SIGNAL(result(KJob *)), SLOT(slotResult(KJob *)));
00026 connect(m_copyJob, SIGNAL(finished()), SLOT(setTorrentFileDownloaded()));
00027 }
00028
00029 void BTDownload::slotData(KIO::Job *job, const QByteArray& data)
00030 {
00031 kDebug(5001);
00037 m_data.append(data);
00038 }
00039
00040 void BTDownload::slotResult(KJob * job)
00041 {
00042 kDebug(5001);
00043 switch (job->error())
00044 {
00045 case 0:
00046 {
00047 kDebug(5001) << "Downloading successfully finished" << m_destUrl.url().remove("file://") + m_srcUrl.fileName();
00048 QFile torrentFile(m_destUrl.url().remove("file://") + m_srcUrl.fileName());
00049 if (!torrentFile.open(QIODevice::WriteOnly | QIODevice::Text))
00050 kDebug(5001) << "Thanks uwolfer";
00051 torrentFile.write(m_data);
00052 torrentFile.close();
00053 kDebug(5001) << m_data;
00054 m_data = 0;
00055 m_destUrl = QFileInfo(torrentFile).absoluteFilePath();
00056 kDebug(5001) << m_destUrl;
00057 emit finishedSuccessfully(m_destUrl);
00058 break;
00059 }
00060 case KIO::ERR_FILE_ALREADY_EXIST:
00061 kDebug(5001) << "ERROR - File already exists";
00062 m_data = 0;
00063 emit finishedSuccessfully(m_destUrl);
00064 default:
00065 kDebug(5001) << "That sucks";
00066 m_data = 0;
00067 emit finishedWithError();
00068 break;
00069 }
00070 }
00071