download.cpp 2.17 KB
Newer Older
1 2 3 4 5 6 7 8 9
/* This file is part of the KDE project

   Copyright (C) 2007 Lukas Appelhans <l.appelhans@gmx.de>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
*/
10
#include "download.h"
11 12 13

#include <QFile>

14
#include "kget_debug.h"
15
#include <QDebug>
16

Lukas Appelhans's avatar
Lukas Appelhans committed
17
Download::Download(const QUrl &srcUrl, const QUrl &destUrl)
18
  : m_srcUrl(srcUrl),
19
    m_destUrl(destUrl)
20
{
21
    qCDebug(KGET_DEBUG) << "DownloadFile: " << m_srcUrl.url() << " to dest: " << m_destUrl.url();
22
    m_copyJob = KIO::get(m_srcUrl, KIO::NoReload, KIO::HideProgressInfo);
23 24
    connect(m_copyJob, &KIO::TransferJob::data, this, &Download::slotData);
    connect(m_copyJob, &KJob::result, this, &Download::slotResult);
25 26
}

27 28 29 30
Download::~Download()
{
}

31
void Download::slotData(KIO::Job *job, const QByteArray& data)
32
{
Matthias Fuchs's avatar
Matthias Fuchs committed
33
    Q_UNUSED(job)
34
    qCDebug(KGET_DEBUG);
35 36 37 38 39 40 41 42
    /**if (data.size() == 0)
    {
        slotResult(job);
        return;
    }**/
    m_data.append(data);
}

43
void Download::slotResult(KJob * job)
44
{
45
    qCDebug(KGET_DEBUG);
46 47 48 49
    switch (job->error())
    {
        case 0://The download has finished
        {
50
            qCDebug(KGET_DEBUG) << "Downloading successfully finished" << m_destUrl.url();
51
            QFile torrentFile(m_destUrl.toLocalFile());
52 53
            if (!torrentFile.open(QIODevice::WriteOnly | QIODevice::Text)) {}
                //TODO: Do a Message box here
54 55
            torrentFile.write(m_data);
            torrentFile.close();
56
            emit finishedSuccessfully(m_destUrl, m_data);
57 58 59 60
            m_data = 0;
            break;
        }
        case KIO::ERR_FILE_ALREADY_EXIST:
61
        {
62
            qCDebug(KGET_DEBUG) << "ERROR - File already exists";
63
            QFile file(m_destUrl.toLocalFile());
64
            emit finishedSuccessfully(m_destUrl, file.readAll());
65
            m_data = 0;
66
            break;
67
        }
68
        default:
69
            qCDebug(KGET_DEBUG) << "We are sorry to say you, that there were errors while downloading :(";
70 71 72 73 74 75
            m_data = 0;
            emit finishedWithError();
            break;
    }
}