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 \brief XdxfDictDownloader is responsible for getting dict list from XDXF website
24 and other actions necessary to download and add dictionary
26 \author Mateusz Półrola <mateusz.polrola@comarch.com>
29 #include "XdxfDictDownloader.h"
30 #include "XdxfDictDownloadProgressDialog.h"
45 XdxfDictDownloader::XdxfDictDownloader(QObject *parent) :
49 manager = new QNetworkAccessManager(this);
51 connect(manager, SIGNAL(finished(QNetworkReply*)),
52 this, SLOT(dictListReceived(QNetworkReply*)));
53 connect(&http, SIGNAL(finished()),
54 this, SLOT(processFinished()));
55 connect(&http, SIGNAL(error(QString)),
56 this, SLOT(downloadingError(QString)));
57 connect(&http, SIGNAL(progress(qint64,qint64)),
58 this, SLOT(updateDownloadProgress(qint64,qint64)));
62 void XdxfDictDownloader::download(QWidget *parent) {
63 parentDialog = parent;
66 manager->get(QNetworkRequest(QUrl("http://xdxf.revdanica.com/down/")));
68 progressDialog = new XdxfDictDownloadProgressDialog(parent);
70 connect(progressDialog, SIGNAL(cancelDownloading()),
71 this, SLOT(breakDownloading()));
72 connect(this, SIGNAL(downloadProgress(float)),
73 progressDialog, SLOT(updateProgress(float)));
74 progressDialog->setText(tr("Downloading dictionaries list"));
75 progressDialog->show();
79 QString XdxfDictDownloader::downloadedFile() {
80 return _downloadedFile;
84 void XdxfDictDownloader::updateDownloadProgress(qint64 downloaded,
86 Q_EMIT downloadProgress(float(downloaded) / float(total));
90 void XdxfDictDownloader::downloadingError(QString error) {
92 Q_EMIT notify(Notify::Error, error);
96 void XdxfDictDownloader::breakDownloading() {
97 //if user cancel downloading we kill all running processes, hide progress dialog and set flag that user cancel downloading.
100 if(progressDialog && progressDialog->isVisible()) {
101 progressDialog->accept();
106 void XdxfDictDownloader::processFinished() {
107 //first check if user cancel downloading
110 if(!extract("/tmp/" + _fileName)) {
111 Q_EMIT notify(Notify::Error,
112 "Error while extracting dictionary archive");
119 void XdxfDictDownloader::downloadComplete() {
121 // Downloaded tar file name is different than extracted folder so we need
122 // some clean directory to identify extracted files
123 QDir dir("/tmp/mdict");
124 QString dictDirName = dir.entryList().at(2);
126 // Dict is in /tmp/mdict/<extracted directory>/dict.xdxf
127 QFile dictFile("/tmp/mdict/" + dictDirName + "/dict.xdxf");
128 dictFile.copy(QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf");
129 QFile::remove("/tmp/" + _fileName);
130 QFile::remove("/tmp/" + _fileName.replace(QRegExp(".bz2$"), ""));
132 _downloadedFile = QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf";
134 progressDialog->accept();
135 delete progressDialog;
138 emit fileDownloaded(_downloadedFile);
142 void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
143 progressDialog->accept();
146 if(reply->error() != QNetworkReply::NoError) {
147 Q_EMIT notify(Notify::Error, reply->errorString());
151 QString page(QString::fromUtf8(reply->readAll()));
153 // You can look at http://xdxf.revdanica.com/down/, we need to get table
154 // with dictionaries entries following regexp match its begining
155 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>");
156 regOuter.setMinimal(true);
157 if(!regOuter.indexIn(page))
160 // Cutting each entry and creating coresponded DownloadDict object
161 page = regOuter.capturedTexts().at(1);
162 QRegExp regInner("<tr>.*</tr>");
163 regInner.setMinimal(true);
166 while ((pos = regInner.indexIn(page, pos)) != -1) {
167 DownloadDict temp = DownloadDict(regInner.cap(0));
168 if(!temp.fromLang().isEmpty())
170 pos += regInner.matchedLength();
173 XdxfDictSelectDialog selectDialog(dicts, parentDialog);
175 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);