File indexing completed on 2024-12-01 04:33:13
0001 /* This file is part of the KDE project 0002 0003 Copyright (C) 2007 Lukas Appelhans <l.appelhans@gmx.de> 0004 0005 This program is free software; you can redistribute it and/or 0006 modify it under the terms of the GNU General Public 0007 License as published by the Free Software Foundation; either 0008 version 2 of the License, or (at your option) any later version. 0009 */ 0010 #include "download.h" 0011 0012 #include <QDebug> 0013 #include <QFile> 0014 0015 #include <KIO/Global> 0016 0017 #include "kget_debug.h" 0018 0019 Download::Download(const QUrl &srcUrl, const QUrl &destUrl) 0020 : m_srcUrl(srcUrl) 0021 , m_destUrl(destUrl) 0022 { 0023 qCDebug(KGET_DEBUG) << "DownloadFile: " << m_srcUrl.url() << " to dest: " << m_destUrl.url(); 0024 m_copyJob = KIO::get(m_srcUrl, KIO::NoReload, KIO::HideProgressInfo); 0025 connect(m_copyJob, &KIO::TransferJob::data, this, &Download::slotData); 0026 connect(m_copyJob, &KJob::result, this, &Download::slotResult); 0027 } 0028 0029 Download::~Download() 0030 { 0031 } 0032 0033 void Download::slotData(KIO::Job *job, const QByteArray &data) 0034 { 0035 Q_UNUSED(job) 0036 qCDebug(KGET_DEBUG); 0037 /**if (data.size() == 0) 0038 { 0039 slotResult(job); 0040 return; 0041 }**/ 0042 m_data.append(data); 0043 } 0044 0045 void Download::slotResult(KJob *job) 0046 { 0047 qCDebug(KGET_DEBUG); 0048 switch (job->error()) { 0049 case 0: // The download has finished 0050 { 0051 qCDebug(KGET_DEBUG) << "Downloading successfully finished" << m_destUrl.url(); 0052 QFile torrentFile(m_destUrl.toLocalFile()); 0053 if (!torrentFile.open(QIODevice::WriteOnly | QIODevice::Text)) { } 0054 // TODO: Do a Message box here 0055 torrentFile.write(m_data); 0056 torrentFile.close(); 0057 Q_EMIT finishedSuccessfully(m_destUrl, m_data); 0058 m_data = nullptr; 0059 break; 0060 } 0061 case KIO::ERR_FILE_ALREADY_EXIST: { 0062 qCDebug(KGET_DEBUG) << "ERROR - File already exists"; 0063 QFile file(m_destUrl.toLocalFile()); 0064 Q_EMIT finishedSuccessfully(m_destUrl, file.readAll()); 0065 m_data = nullptr; 0066 break; 0067 } 0068 default: 0069 qCDebug(KGET_DEBUG) << "We are sorry to say you, that there were errors while downloading :("; 0070 m_data = nullptr; 0071 Q_EMIT finishedWithError(); 0072 break; 0073 } 0074 } 0075 0076 #include "moc_download.cpp"