1 /*******************************************************************************
3 This file is part of mDictionary.
5 mDictionary is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 mDictionary is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with mDictionary. If not, see <http://www.gnu.org/licenses/>.
18 Copyright 2010 Comarch S.A.
20 *******************************************************************************/
22 \file XdxfDictDownloader.cpp
23 \author Mateusz Półrola <mateusz.polrola@comarch.com>
26 #include "XdxfDictDownloader.h"
27 #include "XdxfDictDownloadProgressDialog.h"
37 XdxfDictDownloader::XdxfDictDownloader(QObject *parent) :
41 manager = new QNetworkAccessManager(this);
43 connect(manager, SIGNAL(finished(QNetworkReply*)),
44 this, SLOT(dictListReceived(QNetworkReply*)));
45 connect(&http, SIGNAL(finished()),
46 this, SLOT(processFinished()));
47 connect(&http, SIGNAL(error(QString)),
48 this, SLOT(downloadingError(QString)));
49 connect(&http, SIGNAL(progress(qint64,qint64)),
50 this, SLOT(updateDownloadProgress(qint64,qint64)));
54 void XdxfDictDownloader::download(QWidget *parent) {
55 parentDialog = parent;
58 manager->get(QNetworkRequest(QUrl("http://xdxf.revdanica.com/down/")));
60 progressDialog = new XdxfDictDownloadProgressDialog(parent);
62 connect(progressDialog, SIGNAL(cancelDownloading()),
63 this, SLOT(breakDownloading()));
64 connect(this, SIGNAL(downloadProgress(float)),
65 progressDialog, SLOT(updateProgress(float)));
67 progressDialog->setText(tr("Downloading dictionaries list"));
68 progressDialog->show();
72 QString XdxfDictDownloader::downloadedFile() {
73 return _downloadedFile;
77 void XdxfDictDownloader::updateDownloadProgress(qint64 downloaded,
79 Q_EMIT downloadProgress(float(downloaded) / float(total));
83 void XdxfDictDownloader::downloadingError(QString error) {
85 Q_EMIT notify(Notify::Error, error);
89 void XdxfDictDownloader::breakDownloading() {
90 //if user cancel downloading we kill all running processes, hide progress dialog and set flag that user cancel downloading.
94 if(progressDialog && progressDialog->isVisible()) {
95 progressDialog->accept();
100 void XdxfDictDownloader::processFinished() {
101 //first check if user cancel downloading
104 if(!extract("/tmp/" + _fileName)) {
105 Q_EMIT notify(Notify::Error,
106 "Error while extracting dictionary archive");
113 void XdxfDictDownloader::downloadComplete() {
115 // Downloaded tar file name is different than extracted folder so we need
116 // some clean directory to identify extracted files
117 QDir dir("/tmp/mdict");
118 QString dictDirName = dir.entryList().at(2);
120 // Dict is in /tmp/mdict/<extracted directory>/dict.xdxf
121 QFile dictFile("/tmp/mdict/" + dictDirName + "/dict.xdxf");
122 dictFile.copy(QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf");
123 QFile::remove("/tmp/" + _fileName);
124 QFile::remove("/tmp/" + _fileName.replace(QRegExp(".bz2$"), ""));
126 _downloadedFile = QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf";
128 progressDialog->accept();
129 delete progressDialog;
132 emit fileDownloaded(_downloadedFile);
136 void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
137 progressDialog->accept();
140 if(reply->error() != QNetworkReply::NoError) {
141 Q_EMIT notify(Notify::Error, reply->errorString());
145 QString page(QString::fromUtf8(reply->readAll()));
147 // You can look at http://xdxf.revdanica.com/down/, we need to get table
148 // with dictionaries entries following regexp match its begining
149 QRegExp regOuter("<td>Icon</td><td>Name</td><td>Archive filename</td><td>Archive file size</td><td>Dict file size</td><td>Number of articles</td><td>From</td><td>To</td><td>Submitted by</td><td>Submition date</td></tr>(.*)</table>");
150 regOuter.setMinimal(true);
151 if(!regOuter.indexIn(page))
154 // Cutting each entry and creating coresponded DownloadDict object
155 page = regOuter.capturedTexts().at(1);
156 QRegExp regInner("<tr>.*</tr>");
157 regInner.setMinimal(true);
160 while ((pos = regInner.indexIn(page, pos)) != -1) {
161 DownloadDict temp = DownloadDict(regInner.cap(0));
162 if(!temp.fromLang().isEmpty())
164 pos += regInner.matchedLength();
167 XdxfDictSelectDialog selectDialog(dicts, parentDialog);
169 if(selectDialog.exec()==QDialog::Accepted) {
171 progressDialog->setText(tr("Downloading dictionary"));
172 progressDialog->show();
174 QString url = selectDialog.link();
176 _fileName = url.split('/').last();
179 clean.start("rm -rf /tmp/mdict");
180 clean.waitForFinished(-1);
181 clean.start("mkdir /tmp/mdict");
182 clean.waitForFinished(-1);
184 http.download(QUrl(url), "/tmp/" + _fileName);
188 bool XdxfDictDownloader::extract(QString file) {
190 FILE * archive = fopen(file.toStdString().c_str(), "rb");
194 BZFILE * afterbzFile = BZ2_bzReadOpen(&err, archive, 0, 0, 0, 0);
196 BZ2_bzReadClose(&err, afterbzFile);
200 FILE * tarfile = fopen(file.replace(QRegExp(".bz2$"), "").
201 toStdString().c_str(), "w");
205 while(err == BZ_OK) {
206 int len = BZ2_bzRead(&err, afterbzFile, buff, bufflen);
207 if(fwrite(buff, 1, len, tarfile) != len)
210 BZ2_bzReadClose(&err, afterbzFile);
217 char * tarfname = new char[file.replace(QRegExp(".bz2%"), "").size()+1];
218 strcpy(tarfname, file.replace(QRegExp(".bz2%"), "").toStdString().c_str());
220 err = tar_open(&t, tarfname, 0, O_RDONLY, 0, 0);
224 err = tar_extract_all(t, "/tmp/mdict/");
231 tar.start("tar -xvf " + file.replace(QRegExp(".bz2%"), "") + " -C /tmp/mdict");
232 tar.waitForFinished(-1);