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)));
55 void XdxfDictDownloader::download(QWidget *parent) {
56 parentDialog = parent;
59 manager->get(QNetworkRequest(QUrl("http://xdxf.revdanica.com/down/")));
61 progressDialog = new XdxfDictDownloadProgressDialog(parent);
63 connect(progressDialog, SIGNAL(cancelDownloading()),
64 this, SLOT(breakDownloading()));
65 progressDialog->setText(tr("Downloading dictionaries list"));
66 progressDialog->show();
69 QString XdxfDictDownloader::downloadedFile() {
70 return _downloadedFile;
74 void XdxfDictDownloader::downloadingError(QString error) {
76 Q_EMIT notify(Notify::Error, error);
79 void XdxfDictDownloader::breakDownloading() {
80 //if user cancel downloading we kill all running processes, hide progress dialog and set flag that user cancel downloading.
84 if(progressDialog && progressDialog->isVisible()) {
85 progressDialog->accept();
90 void XdxfDictDownloader::processFinished() {
91 //first check if user cancel downloading
94 if(!extract("/tmp/" + _fileName)) {
95 Q_EMIT notify(Notify::Error,
96 "Error while extracting dictionary archive");
102 void XdxfDictDownloader::downloadComplete() {
104 // Downloaded tar file name is different than extracted folder so we need
105 // some clean directory to identify extracted files
106 QDir dir("/tmp/mdict");
107 QString dictDirName = dir.entryList().at(2);
109 // Dict is in /tmp/mdict/<extracted directory>/dict.xdxf
110 QFile dictFile("/tmp/mdict/" + dictDirName + "/dict.xdxf");
111 dictFile.copy(QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf");
112 QFile::remove("/tmp/" + _fileName);
113 QFile::remove("/tmp/" + _fileName.replace(QRegExp(".bz2$"), ""));
115 _downloadedFile = QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf";
117 progressDialog->accept();
118 delete progressDialog;
121 emit fileDownloaded(_downloadedFile);
124 void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
125 progressDialog->accept();
129 if(reply->error() != QNetworkReply::NoError) {
130 Q_EMIT notify(Notify::Error, reply->errorString());
134 QString page(QString::fromUtf8(reply->readAll()));
136 // You can look at http://xdxf.revdanica.com/down/, we need to get table
137 // with dictionaries entries following regexp match its begining
138 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>");
139 regOuter.setMinimal(true);
140 if(!regOuter.indexIn(page))
143 // Cutting each entry and creating coresponded DownloadDict object
144 page = regOuter.capturedTexts().at(1);
145 QRegExp regInner("<tr>.*</tr>");
146 regInner.setMinimal(true);
149 while ((pos = regInner.indexIn(page, pos)) != -1) {
150 DownloadDict temp = DownloadDict(regInner.cap(0));
151 if(!temp.fromLang().isEmpty())
153 pos += regInner.matchedLength();
156 XdxfDictSelectDialog selectDialog(dicts, parentDialog);
158 if(selectDialog.exec()==QDialog::Accepted) {
160 progressDialog->setText(tr("Downloading dictionary"));
161 progressDialog->show();
163 QString url = selectDialog.link();
165 _fileName = url.split('/').last();
168 clean.start("rm -rf /tmp/mdict");
169 clean.waitForFinished(-1);
170 clean.start("mkdir /tmp/mdict");
171 clean.waitForFinished(-1);
173 http.download(QUrl(url), "/tmp/" + _fileName);
177 bool XdxfDictDownloader::extract(QString file) {
179 FILE * archive = fopen(file.toStdString().c_str(), "rb");
183 BZFILE * afterbzFile = BZ2_bzReadOpen(&err, archive, 0, 0, 0, 0);
185 BZ2_bzReadClose(&err, afterbzFile);
189 FILE * tarfile = fopen(file.replace(QRegExp(".bz2$"), "").
190 toStdString().c_str(), "w");
194 while(err == BZ_OK) {
195 int len = BZ2_bzRead(&err, afterbzFile, buff, bufflen);
196 if(fwrite(buff, 1, len, tarfile) != len)
199 BZ2_bzReadClose(&err, afterbzFile);
205 char * tarfname = new char[file.replace(QRegExp(".bz2%"), "").size()+1];
206 strcpy(tarfname, file.replace(QRegExp(".bz2%"), "").toStdString().c_str());
208 err = tar_open(&t, tarfname, 0, O_RDONLY, 0, 0);
212 err = tar_extract_all(t, "/tmp/mdict/");