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 process = new QProcess(this);
44 manager = new QNetworkAccessManager(this);
46 connect(manager, SIGNAL(finished(QNetworkReply*)),
47 this, SLOT(dictListReceived(QNetworkReply*)));
49 connect(process, SIGNAL(finished(int)),
50 this, SLOT(processFinished(int)));
53 connect(&http, SIGNAL(finished()), this, SLOT(processFinished()));
56 void XdxfDictDownloader::download(QWidget *parent) {
57 parentDialog = parent;
60 manager->get(QNetworkRequest(QUrl("http://xdxf.revdanica.com/down/")));
62 progressDialog = new XdxfDictDownloadProgressDialog(parent);
64 connect(progressDialog, SIGNAL(cancelDownloading()),
65 this, SLOT(breakDownloading()));
66 progressDialog->setText(tr("Downloading dictionaries list"));
67 progressDialog->show();
70 QString XdxfDictDownloader::downloadedFile() {
71 return _downloadedFile;
75 void XdxfDictDownloader::breakDownloading() {
76 //if user cancel downloading we kill all running processes, hide progress dialog and set flag that user cancel downloading.
78 if(process->state() != QProcess::NotRunning) {
82 if(progressDialog && progressDialog->isVisible()) {
83 progressDialog->accept();
88 void XdxfDictDownloader::processFinished() {
89 //first check if user cancel downloading
92 if(!extract("/tmp/" + _fileName)) {
93 Q_EMIT notify(Notify::Error,
94 "Error while extracting dictionary archive");
100 void XdxfDictDownloader::downloadComplete() {
102 // Downloaded tar file name is different than extracted folder so we need
103 // some clean directory to identify extracted files
104 QDir dir("/tmp/mdict");
105 QString dictDirName = dir.entryList().at(2);
107 // Dict is in /tmp/mdict/<extracted directory>/dict.xdxf
108 QFile dictFile("/tmp/mdict/" + dictDirName + "/dict.xdxf");
109 dictFile.copy(QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf");
110 QFile::remove("/tmp/" + _fileName);
111 QFile::remove("/tmp/" + _fileName.replace(QRegExp(".bz2$"), ""));
113 _downloadedFile = QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf";
115 progressDialog->accept();
116 delete progressDialog;
119 emit fileDownloaded(_downloadedFile);
122 void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
125 progressDialog->accept();
127 if(reply->error() != QNetworkReply::NoError) {
128 Q_EMIT notify(Notify::Error, reply->errorString());
132 QString page(QString::fromUtf8(reply->readAll()));
134 // You can look at http://xdxf.revdanica.com/down/, we need to get table
135 // with dictionaries entries following regexp match its begining
136 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>");
137 regOuter.setMinimal(true);
138 if(!regOuter.indexIn(page))
141 // Cutting each entry and creating coresponded DownloadDict object
142 page = regOuter.capturedTexts().at(1);
143 QRegExp regInner("<tr>.*</tr>");
144 regInner.setMinimal(true);
147 while ((pos = regInner.indexIn(page, pos)) != -1) {
148 DownloadDict temp = DownloadDict(regInner.cap(0));
149 if(!temp.fromLang().isEmpty())
151 pos += regInner.matchedLength();
154 XdxfDictSelectDialog selectDialog(dicts, parentDialog);
156 if(selectDialog.exec()==QDialog::Accepted) {
158 progressDialog->setText(tr("Downloading dictionary"));
159 progressDialog->show();
161 QString url = selectDialog.link();
163 _fileName = url.split('/').last();
166 clean.start("rm -rf /tmp/mdict");
167 clean.waitForFinished(-1);
168 clean.start("mkdir /tmp/mdict");
169 clean.waitForFinished(-1);
171 http.download(QUrl(url), "/tmp/" + _fileName);
175 bool XdxfDictDownloader::extract(QString file) {
177 FILE * archive = fopen(file.toStdString().c_str(), "rb");
181 BZFILE * afterbzFile = BZ2_bzReadOpen(&err, archive, 0, 0, 0, 0);
183 BZ2_bzReadClose(&err, afterbzFile);
187 FILE * tarfile = fopen(file.replace(QRegExp(".bz2$"), "").
188 toStdString().c_str(), "w");
192 while(err == BZ_OK) {
193 int len = BZ2_bzRead(&err, afterbzFile, buff, bufflen);
194 if(fwrite(buff, 1, len, tarfile) != len)
197 BZ2_bzReadClose(&err, afterbzFile);
203 char * tarfname = new char[file.replace(QRegExp(".bz2%"), "").size()+1];
204 strcpy(tarfname, file.replace(QRegExp(".bz2%"), "").toStdString().c_str());
206 err = tar_open(&t, tarfname, 0, O_RDONLY, 0, 0);
210 err = tar_extract_all(t, "/tmp/mdict/");