File indexing completed on 2023-10-01 08:39:26

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 <QFile>
0013 
0014 #include "kget_debug.h"
0015 #include <QDebug>
0016 
0017 Download::Download(const QUrl &srcUrl, const QUrl &destUrl)
0018     : m_srcUrl(srcUrl)
0019     , m_destUrl(destUrl)
0020 {
0021     qCDebug(KGET_DEBUG) << "DownloadFile: " << m_srcUrl.url() << " to dest: " << m_destUrl.url();
0022     m_copyJob = KIO::get(m_srcUrl, KIO::NoReload, KIO::HideProgressInfo);
0023     connect(m_copyJob, &KIO::TransferJob::data, this, &Download::slotData);
0024     connect(m_copyJob, &KJob::result, this, &Download::slotResult);
0025 }
0026 
0027 Download::~Download()
0028 {
0029 }
0030 
0031 void Download::slotData(KIO::Job *job, const QByteArray &data)
0032 {
0033     Q_UNUSED(job)
0034     qCDebug(KGET_DEBUG);
0035     /**if (data.size() == 0)
0036     {
0037         slotResult(job);
0038         return;
0039     }**/
0040     m_data.append(data);
0041 }
0042 
0043 void Download::slotResult(KJob *job)
0044 {
0045     qCDebug(KGET_DEBUG);
0046     switch (job->error()) {
0047     case 0: // The download has finished
0048     {
0049         qCDebug(KGET_DEBUG) << "Downloading successfully finished" << m_destUrl.url();
0050         QFile torrentFile(m_destUrl.toLocalFile());
0051         if (!torrentFile.open(QIODevice::WriteOnly | QIODevice::Text)) { }
0052         // TODO: Do a Message box here
0053         torrentFile.write(m_data);
0054         torrentFile.close();
0055         Q_EMIT finishedSuccessfully(m_destUrl, m_data);
0056         m_data = nullptr;
0057         break;
0058     }
0059     case KIO::ERR_FILE_ALREADY_EXIST: {
0060         qCDebug(KGET_DEBUG) << "ERROR - File already exists";
0061         QFile file(m_destUrl.toLocalFile());
0062         Q_EMIT finishedSuccessfully(m_destUrl, file.readAll());
0063         m_data = nullptr;
0064         break;
0065     }
0066     default:
0067         qCDebug(KGET_DEBUG) << "We are sorry to say you, that there were errors while downloading :(";
0068         m_data = nullptr;
0069         Q_EMIT finishedWithError();
0070         break;
0071     }
0072 }
0073 
0074 #include "moc_download.cpp"