2
* evopedia: An offline Wikipedia reader.
4
* Copyright (C) 2010-2011 evopedia developers
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.
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.
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/>.
21
#include "downloadablearchive.h"
23
#include <QtWidgets/QFileDialog>
25
#include "evopediaapplication.h"
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)
31
this->language = language;
35
QString DownloadableArchive::askAndCreateDownloadDirectory()
37
ArchiveManager *am((static_cast<EvopediaApplication *>(qApp))->evopedia()->getArchiveManager());
39
QString baseDir = am->getArchivesBaseDir();
41
if (baseDir.isEmpty()) {
42
baseDir = QFileDialog::getExistingDirectory(0, tr("Select Base Download Directory For Archives"),
43
QString(), QFileDialog::ShowDirsOnly);
44
if (baseDir.isEmpty())
47
am->setArchivesBaseDir(baseDir);
50
QString downloadDirectory = QDir(baseDir).absolutePath() + "/" + QString("wikipedia_%1").arg(language);
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));
61
return downloadDirectory;
64
bool DownloadableArchive::startDownload()
66
downloadDirectory = askAndCreateDownloadDirectory();
68
/* TODO2 sanity check for language and date? */
69
torrentFile = QString("wikipedia_%1_%2.torrent").arg(language, date);
71
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
72
QObject::connect(manager, SIGNAL(finished(QNetworkReply* )),
73
this, SLOT(torrentDownloadFinished(QNetworkReply* )));
74
manager->get(QNetworkRequest(url));
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 */
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());
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);