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"
42 XdxfDictDownloader::XdxfDictDownloader(QObject *parent) :
45 manager = new QNetworkAccessManager(this);
47 connect(manager, SIGNAL(finished(QNetworkReply*)),
48 this, SLOT(dictListReceived(QNetworkReply*)));
52 connect(&http, SIGNAL(finished()), this, SLOT(processFinished()));
53 connect(&http, SIGNAL(error(QString)),
54 this, SLOT(downloadingError(QString)));
55 connect(&http, SIGNAL(progress(qint64,qint64)),
56 this, SLOT(updateDownloadProgress(qint64,qint64)));
60 void XdxfDictDownloader::download(QWidget *parent) {
61 parentDialog = parent;
64 manager->get(QNetworkRequest(QUrl("http://xdxf.revdanica.com/down/")));
66 progressDialog = new XdxfDictDownloadProgressDialog(parent);
68 connect(progressDialog, SIGNAL(cancelDownloading()),
69 this, SLOT(breakDownloading()));
70 connect(this, SIGNAL(downloadProgress(float)),
71 progressDialog, SLOT(updateProgress(float)));
73 progressDialog->setText(tr("Downloading dictionaries list"));
74 progressDialog->show();
77 QString XdxfDictDownloader::downloadedFile() {
78 return _downloadedFile;
81 void XdxfDictDownloader::updateDownloadProgress(qint64 downloaded,
83 Q_EMIT downloadProgress(float(downloaded) / float(total));
86 void XdxfDictDownloader::downloadingError(QString error) {
88 Q_EMIT notify(Notify::Error, error);
91 void XdxfDictDownloader::breakDownloading() {
92 //if user cancel downloading we kill all running processes, hide progress dialog and set flag that user cancel downloading.
96 if(progressDialog && progressDialog->isVisible()) {
97 progressDialog->accept();
102 void XdxfDictDownloader::processFinished() {
103 //first check if user cancel downloading
106 if(!extract("/tmp/" + _fileName)) {
107 Q_EMIT notify(Notify::Error,
108 "Error while extracting dictionary archive");
114 void XdxfDictDownloader::downloadComplete() {
116 // Downloaded tar file name is different than extracted folder so we need
117 // some clean directory to identify extracted files
118 QDir dir("/tmp/mdict");
119 QString dictDirName = dir.entryList().at(2);
121 // Dict is in /tmp/mdict/<extracted directory>/dict.xdxf
122 QFile dictFile("/tmp/mdict/" + dictDirName + "/dict.xdxf");
123 dictFile.copy(QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf");
124 QFile::remove("/tmp/" + _fileName);
125 QFile::remove("/tmp/" + _fileName.replace(QRegExp(".bz2$"), ""));
127 _downloadedFile = QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf";
129 progressDialog->accept();
130 delete progressDialog;
133 emit fileDownloaded(_downloadedFile);
136 void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
137 progressDialog->accept();
141 if(reply->error() != QNetworkReply::NoError) {
142 Q_EMIT notify(Notify::Error, reply->errorString());
146 QString page(QString::fromUtf8(reply->readAll()));
148 // You can look at http://xdxf.revdanica.com/down/, we need to get table
149 // with dictionaries entries following regexp match its begining
150 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>");
151 regOuter.setMinimal(true);
152 if(!regOuter.indexIn(page))
155 // Cutting each entry and creating coresponded DownloadDict object
156 page = regOuter.capturedTexts().at(1);
157 QRegExp regInner("<tr>.*</tr>");
158 regInner.setMinimal(true);
161 while ((pos = regInner.indexIn(page, pos)) != -1) {
162 DownloadDict temp = DownloadDict(regInner.cap(0));
163 if(!temp.fromLang().isEmpty())
165 pos += regInner.matchedLength();
168 XdxfDictSelectDialog selectDialog(dicts, parentDialog);
170 if(selectDialog.exec()==QDialog::Accepted) {
172 progressDialog->setText(tr("Downloading dictionary"));
173 progressDialog->show();
175 QString url = selectDialog.link();
177 _fileName = url.split('/').last();
180 clean.start("rm -rf /tmp/mdict");
181 clean.waitForFinished(-1);
182 clean.start("mkdir /tmp/mdict");
183 clean.waitForFinished(-1);
185 http.download(QUrl(url), "/tmp/" + _fileName);
189 bool XdxfDictDownloader::extract(QString file) {
191 FILE * archive = fopen(file.toStdString().c_str(), "rb");
195 BZFILE * afterbzFile = BZ2_bzReadOpen(&err, archive, 0, 0, 0, 0);
197 BZ2_bzReadClose(&err, afterbzFile);
201 FILE * tarfile = fopen(file.replace(QRegExp(".bz2$"), "").
202 toStdString().c_str(), "w");
206 while(err == BZ_OK) {
207 int len = BZ2_bzRead(&err, afterbzFile, buff, bufflen);
208 if(fwrite(buff, 1, len, tarfile) != len)
211 BZ2_bzReadClose(&err, afterbzFile);
218 char * tarfname = new char[file.replace(QRegExp(".bz2%"), "").size()+1];
219 strcpy(tarfname, file.replace(QRegExp(".bz2%"), "").toStdString().c_str());
221 err = tar_open(&t, tarfname, 0, O_RDONLY, 0, 0);
225 err = tar_extract_all(t, "/tmp/mdict/");
232 tar.start("tar -xvf " + file.replace(QRegExp(".bz2%"), "") + " -C /tmp/mdict");
233 tar.waitForFinished(-1);