summaryrefslogtreecommitdiff
path: root/file_download.cpp
blob: add1207f96e2a9854c38b64b359686c6247b7299 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "file_download.h"

file_download::file_download()
{

}

int file_download::file_download::download_file(QString url, QString save_location)
{
    /*
     * this function will download a given url/file to a location
     * no file location will be interpreted as . (current directory)
     */
    qDebug() << "downloading to " + save_location + " from " + url;
    if (QFile::exists(save_location))
        QFile::remove(save_location);
    QNetworkReply *reply = http.get(QNetworkRequest(url));
    QObject::connect(reply, SIGNAL(finished()),&loop, SLOT(quit()));
    loop.exec();
    QFile file(save_location);
    file.open(QIODevice::WriteOnly);
    file.write(reply->readAll());
    delete reply;
    if (QFile::exists(save_location) && file.size() > 0)
        return 1; /* file downloaded - could just be a blank file at this point */
    else
    {
        msgbox.info_box("Unable to download file", "Notice");
        return 0; /* file did not download */
    }
}