~patrick-hetu/+junk/evopedia-app

« back to all changes in this revision

Viewing changes to src/downloadablearchive.cpp

  • Committer: Patrick Hetu
  • Date: 2013-07-22 02:35:22 UTC
  • Revision ID: patrick.hetu@gmail.com-20130722023522-jrfmj5s6eb3mfdv8
initial test

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * evopedia: An offline Wikipedia reader.
 
3
 *
 
4
 * Copyright (C) 2010-2011 evopedia developers
 
5
 *
 
6
 * This program is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation, either version 3 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 *
 
19
 */
 
20
 
 
21
#include "downloadablearchive.h"
 
22
 
 
23
#include <QtWidgets/QFileDialog>
 
24
 
 
25
#include "evopediaapplication.h"
 
26
 
 
27
DownloadableArchive::DownloadableArchive(const QString &language, const QString &date,
 
28
                                         const QUrl &url, const QString &size, QObject *parent) :
 
29
    Archive(parent), url(url), size(size)
 
30
{
 
31
    this->language = language;
 
32
    this->date = date;
 
33
}
 
34
 
 
35
QString DownloadableArchive::askAndCreateDownloadDirectory()
 
36
{
 
37
    ArchiveManager *am((static_cast<EvopediaApplication *>(qApp))->evopedia()->getArchiveManager());
 
38
    const QDir d;
 
39
    QString baseDir = am->getArchivesBaseDir();
 
40
 
 
41
    if (baseDir.isEmpty()) {
 
42
        baseDir = QFileDialog::getExistingDirectory(0, tr("Select Base Download Directory For Archives"),
 
43
                                                        QString(), QFileDialog::ShowDirsOnly);
 
44
        if (baseDir.isEmpty())
 
45
            return QString();
 
46
        else
 
47
            am->setArchivesBaseDir(baseDir);
 
48
    }
 
49
 
 
50
    QString downloadDirectory = QDir(baseDir).absolutePath() + "/" + QString("wikipedia_%1").arg(language);
 
51
 
 
52
    if (!QDir(downloadDirectory).exists()) {
 
53
        if (!QDir().mkpath(downloadDirectory)) {
 
54
            QMessageBox::critical(0, tr("Error Downloading Torrent"),
 
55
                                  tr("Unable to create directory %1.")
 
56
                                  .arg(downloadDirectory));
 
57
            return QString();
 
58
        }
 
59
    }
 
60
 
 
61
    return downloadDirectory;
 
62
}
 
63
 
 
64
bool DownloadableArchive::startDownload()
 
65
{
 
66
    downloadDirectory = askAndCreateDownloadDirectory();
 
67
 
 
68
    /* TODO2 sanity check for language and date? */
 
69
    torrentFile = QString("wikipedia_%1_%2.torrent").arg(language, date);
 
70
 
 
71
    QNetworkAccessManager* manager = new QNetworkAccessManager(this);
 
72
    QObject::connect(manager, SIGNAL(finished(QNetworkReply* )),
 
73
                     this, SLOT(torrentDownloadFinished(QNetworkReply* )));
 
74
    manager->get(QNetworkRequest(url));
 
75
 
 
76
    /* TODO1 indicator (progress bar, throbber) while file is downloaded */
 
77
    /* TODO1 it could be possible that we have to move this code to
 
78
       PartialArchive in order to achive that */
 
79
    return true;
 
80
}
 
81
 
 
82
/* TODO1 download error handler */
 
83
void DownloadableArchive::torrentDownloadFinished(QNetworkReply* reply) {
 
84
    QFile f(downloadDirectory + "/" + torrentFile);
 
85
    f.open(QIODevice::WriteOnly);
 
86
    f.write(reply->readAll());
 
87
    f.close();
 
88
 
 
89
    ArchiveManager *am((static_cast<EvopediaApplication *>(qApp))->evopedia()->getArchiveManager());
 
90
    PartialArchive *a = new PartialArchive(language, date, size,
 
91
                                           torrentFile, downloadDirectory);
 
92
    am->exchangeArchives(this, a);
 
93
 
 
94
    a->startDownload();
 
95
}