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"
42 XdxfDictDownloader::XdxfDictDownloader(QObject *parent) :
46 manager = new QNetworkAccessManager(this);
48 connect(manager, SIGNAL(finished(QNetworkReply*)),
49 this, SLOT(dictListReceived(QNetworkReply*)));
50 connect(&http, SIGNAL(finished()),
51 this, SLOT(processFinished()));
52 connect(&http, SIGNAL(error(QString)),
53 this, SLOT(downloadingError(QString)));
54 connect(&http, SIGNAL(progress(qint64,qint64)),
55 this, SLOT(updateDownloadProgress(qint64,qint64)));
59 void XdxfDictDownloader::download(QWidget *parent) {
60 parentDialog = parent;
63 manager->get(QNetworkRequest(QUrl("http://xdxf.revdanica.com/down/")));
65 progressDialog = new XdxfDictDownloadProgressDialog(parent);
67 connect(progressDialog, SIGNAL(cancelDownloading()),
68 this, SLOT(breakDownloading()));
69 connect(this, SIGNAL(downloadProgress(float)),
70 progressDialog, SLOT(updateProgress(float)));
72 progressDialog->setText(tr("Downloading dictionaries list"));
73 progressDialog->show();
77 QString XdxfDictDownloader::downloadedFile() {
78 return _downloadedFile;
82 void XdxfDictDownloader::updateDownloadProgress(qint64 downloaded,
84 Q_EMIT downloadProgress(float(downloaded) / float(total));
88 void XdxfDictDownloader::downloadingError(QString error) {
90 Q_EMIT notify(Notify::Error, error);
94 void XdxfDictDownloader::breakDownloading() {
95 //if user cancel downloading we kill all running processes, hide progress dialog and set flag that user cancel downloading.
99 if(progressDialog && progressDialog->isVisible()) {
100 progressDialog->accept();
105 void XdxfDictDownloader::processFinished() {
106 //first check if user cancel downloading
109 if(!extract("/tmp/" + _fileName)) {
110 Q_EMIT notify(Notify::Error,
111 "Error while extracting dictionary archive");
118 void XdxfDictDownloader::downloadComplete() {
120 // Downloaded tar file name is different than extracted folder so we need
121 // some clean directory to identify extracted files
122 QDir dir("/tmp/mdict");
123 QString dictDirName = dir.entryList().at(2);
125 // Dict is in /tmp/mdict/<extracted directory>/dict.xdxf
126 QFile dictFile("/tmp/mdict/" + dictDirName + "/dict.xdxf");
127 dictFile.copy(QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf");
128 QFile::remove("/tmp/" + _fileName);
129 QFile::remove("/tmp/" + _fileName.replace(QRegExp(".bz2$"), ""));
131 _downloadedFile = QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf";
133 progressDialog->accept();
134 delete progressDialog;
137 emit fileDownloaded(_downloadedFile);
141 void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
142 progressDialog->accept();
145 if(reply->error() != QNetworkReply::NoError) {
146 Q_EMIT notify(Notify::Error, reply->errorString());
150 QString page(QString::fromUtf8(reply->readAll()));
152 // You can look at http://xdxf.revdanica.com/down/, we need to get table
153 // with dictionaries entries following regexp match its begining
154 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>");
155 regOuter.setMinimal(true);
156 if(!regOuter.indexIn(page))
159 // Cutting each entry and creating coresponded DownloadDict object
160 page = regOuter.capturedTexts().at(1);
161 QRegExp regInner("<tr>.*</tr>");
162 regInner.setMinimal(true);
165 while ((pos = regInner.indexIn(page, pos)) != -1) {
166 DownloadDict temp = DownloadDict(regInner.cap(0));
167 if(!temp.fromLang().isEmpty())
169 pos += regInner.matchedLength();
172 XdxfDictSelectDialog selectDialog(dicts, parentDialog);
174 if(selectDialog.exec()==QDialog::Accepted) {
176 progressDialog->setText(tr("Downloading dictionary"));
177 progressDialog->show();
179 QString url = selectDialog.link();
180 _fileName = url.split('/').last();
183 clean.start("rm -rf /tmp/mdict");
184 clean.waitForFinished(-1);
185 clean.start("mkdir /tmp/mdict");
186 clean.waitForFinished(-1);
188 http.download(QUrl(url), "/tmp/" + _fileName);
192 bool XdxfDictDownloader::extract(QString file) {
194 FILE * archive = fopen(file.toStdString().c_str(), "rb");
198 BZFILE * afterbzFile = BZ2_bzReadOpen(&err, archive, 0, 0, 0, 0);
200 BZ2_bzReadClose(&err, afterbzFile);
204 FILE * tarfile = fopen(file.replace(QRegExp(".bz2$"), "").
205 toStdString().c_str(), "w");
209 while(err == BZ_OK) {
210 unsigned int len = BZ2_bzRead(&err, afterbzFile, buff, bufflen);
211 if(fwrite(buff, 1, len, tarfile) != len)
214 BZ2_bzReadClose(&err, afterbzFile);
221 char * tarfname = new char[file.replace(QRegExp(".bz2%"), "").size()+1];
222 strcpy(tarfname, file.replace(QRegExp(".bz2%"), "").toStdString().c_str());
224 err = tar_open(&t, tarfname, 0, O_RDONLY, 0, 0);
228 char text[]={"/tmp/mdict/"};
229 err = tar_extract_all(t,text);
236 tar.start("tar -xvf " + file.replace(QRegExp(".bz2%"), "") + " -C /tmp/mdict");
237 tar.waitForFinished(-1);