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 *******************************************************************************/
23 \file XdxfDictDownloader.cpp
24 \author Mateusz Półrola <mateusz.polrola@comarch.com>
27 #include "XdxfDictDownloader.h"
28 #include "XdxfDictDownloadProgressDialog.h"
40 XdxfDictDownloader::XdxfDictDownloader(QObject *parent) :
43 manager = new QNetworkAccessManager(this);
45 connect(manager, SIGNAL(finished(QNetworkReply*)),
46 this, SLOT(dictListReceived(QNetworkReply*)));
50 connect(&http, SIGNAL(finished()), this, SLOT(processFinished()));
51 connect(&http, SIGNAL(error(QString)),
52 this, SLOT(downloadingError(QString)));
53 connect(&http, SIGNAL(progress(qint64,qint64)),
54 this, SLOT(updateDownloadProgress(qint64,qint64)));
58 void XdxfDictDownloader::download(QWidget *parent) {
59 parentDialog = parent;
62 manager->get(QNetworkRequest(QUrl("http://xdxf.revdanica.com/down/")));
64 progressDialog = new XdxfDictDownloadProgressDialog(parent);
66 connect(progressDialog, SIGNAL(cancelDownloading()),
67 this, SLOT(breakDownloading()));
68 connect(this, SIGNAL(downloadProgress(float)),
69 progressDialog, SLOT(updateProgress(float)));
71 progressDialog->setText(tr("Downloading dictionaries list"));
72 progressDialog->show();
75 QString XdxfDictDownloader::downloadedFile() {
76 return _downloadedFile;
79 void XdxfDictDownloader::updateDownloadProgress(qint64 downloaded,
81 Q_EMIT downloadProgress(float(downloaded) / float(total));
84 void XdxfDictDownloader::downloadingError(QString error) {
86 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");
112 void XdxfDictDownloader::downloadComplete() {
114 // Downloaded tar file name is different than extracted folder so we need
115 // some clean directory to identify extracted files
116 QDir dir("/tmp/mdict");
117 QString dictDirName = dir.entryList().at(2);
119 // Dict is in /tmp/mdict/<extracted directory>/dict.xdxf
120 QFile dictFile("/tmp/mdict/" + dictDirName + "/dict.xdxf");
121 dictFile.copy(QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf");
122 QFile::remove("/tmp/" + _fileName);
123 QFile::remove("/tmp/" + _fileName.replace(QRegExp(".bz2$"), ""));
125 _downloadedFile = QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf";
127 progressDialog->accept();
128 delete progressDialog;
131 emit fileDownloaded(_downloadedFile);
134 void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
135 progressDialog->accept();
139 if(reply->error() != QNetworkReply::NoError) {
140 Q_EMIT notify(Notify::Error, reply->errorString());
144 QString page(QString::fromUtf8(reply->readAll()));
146 // You can look at http://xdxf.revdanica.com/down/, we need to get table
147 // with dictionaries entries following regexp match its begining
148 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>");
149 regOuter.setMinimal(true);
150 if(!regOuter.indexIn(page))
153 // Cutting each entry and creating coresponded DownloadDict object
154 page = regOuter.capturedTexts().at(1);
155 QRegExp regInner("<tr>.*</tr>");
156 regInner.setMinimal(true);
159 while ((pos = regInner.indexIn(page, pos)) != -1) {
160 DownloadDict temp = DownloadDict(regInner.cap(0));
161 if(!temp.fromLang().isEmpty())
163 pos += regInner.matchedLength();
166 XdxfDictSelectDialog selectDialog(dicts, parentDialog);
168 if(selectDialog.exec()==QDialog::Accepted) {
170 progressDialog->setText(tr("Downloading dictionary"));
171 progressDialog->show();
173 QString url = selectDialog.link();
175 _fileName = url.split('/').last();
178 clean.start("rm -rf /tmp/mdict");
179 clean.waitForFinished(-1);
180 clean.start("mkdir /tmp/mdict");
181 clean.waitForFinished(-1);
183 http.download(QUrl(url), "/tmp/" + _fileName);
187 bool XdxfDictDownloader::extract(QString file) {
189 FILE * archive = fopen(file.toStdString().c_str(), "rb");
193 BZFILE * afterbzFile = BZ2_bzReadOpen(&err, archive, 0, 0, 0, 0);
195 BZ2_bzReadClose(&err, afterbzFile);
199 FILE * tarfile = fopen(file.replace(QRegExp(".bz2$"), "").
200 toStdString().c_str(), "w");
204 while(err == BZ_OK) {
205 int len = BZ2_bzRead(&err, afterbzFile, buff, bufflen);
206 if(fwrite(buff, 1, len, tarfile) != len)
209 BZ2_bzReadClose(&err, afterbzFile);
215 char * tarfname = new char[file.replace(QRegExp(".bz2%"), "").size()+1];
216 strcpy(tarfname, file.replace(QRegExp(".bz2%"), "").toStdString().c_str());
218 err = tar_open(&t, tarfname, 0, O_RDONLY, 0, 0);
222 err = tar_extract_all(t, "/tmp/mdict/");