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)));
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::breakDownloading() {
75 //if user cancel downloading we kill all running processes, hide progress dialog and set flag that user cancel downloading.
77 if(process->state() != QProcess::NotRunning) {
81 if(progressDialog && progressDialog->isVisible()) {
82 progressDialog->accept();
87 void XdxfDictDownloader::processFinished(int exitcode) {
88 //first check if user cancel downloading
91 //if error of proces, notify user about this
93 Q_EMIT notify(Notify::Error, tr("Error while downloading or processing dictionary"));
97 //if there are any left commands, execute next
98 if(++currentCommand<commands.size()) {
99 process->start(commands[currentCommand]);
102 qDebug() << "EXTRACT" << extract("/tmp/" + _fileName);
107 void XdxfDictDownloader::downloadComplete() {
109 // Downloaded tar file name is different than extracted folder so we need
110 // some clean directory to identify extracted files
111 QDir dir("/tmp/mdict");
112 QString dictDirName = dir.entryList().at(2);
114 // Dict is in /tmp/mdict/<extracted directory>/dict.xdxf
115 QFile dictFile("/tmp/mdict/" + dictDirName + "/dict.xdxf");
116 dictFile.copy(QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf");
117 QFile::remove("/tmp/" + _fileName);
118 QFile::remove("/tmp/" + _fileName.replace(QRegExp(".bz2$"), ""));
120 _downloadedFile = QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf";
122 progressDialog->accept();
123 delete progressDialog;
126 emit fileDownloaded(_downloadedFile);
129 void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
132 progressDialog->accept();
134 if(reply->error() != QNetworkReply::NoError) {
135 Q_EMIT notify(Notify::Error, reply->errorString());
139 QString page(QString::fromUtf8(reply->readAll()));
141 // You can look at http://xdxf.revdanica.com/down/, we need to get table
142 // with dictionaries entries following regexp match its begining
143 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>");
144 regOuter.setMinimal(true);
145 if(!regOuter.indexIn(page))
148 // Cutting each entry and creating coresponded DownloadDict object
149 page = regOuter.capturedTexts().at(1);
150 QRegExp regInner("<tr>.*</tr>");
151 regInner.setMinimal(true);
154 while ((pos = regInner.indexIn(page, pos)) != -1) {
155 DownloadDict temp = DownloadDict(regInner.cap(0));
156 if(!temp.fromLang().isEmpty())
158 pos += regInner.matchedLength();
161 XdxfDictSelectDialog selectDialog(dicts, parentDialog);
163 if(selectDialog.exec()==QDialog::Accepted) {
165 progressDialog->setText(tr("Downloading dictionary"));
166 progressDialog->show();
168 QString url = selectDialog.link();
170 _fileName = url.split('/').last();
172 // Now its the tricky part ... its temporary (probably)
173 // We dont have any tar-dev and bz2-dev packages on maemo so we need
174 // to call commands via shell, each command from list is called after
175 // previous call returns 0
179 commands.push_back("rm -rf /tmp/mdict");
180 commands.push_back("mkdir /tmp/mdict");
182 // Downloading xdxf dict from sourceforge is kind of complicated,
183 // there is a lot of redirection and QNetwork* is kind of lost, we
184 // tried to follow redirection (by hand) but we end up with some
185 // page and js scripts and thats all
186 // Maybe calling wget is not pretty one but its working!
187 commands.push_back("wget --quiet -P /tmp/ " + url);
189 //commands.push_back(QString("tar -xjvf /tmp/") + _fileName + QString(" -C /tmp/mdict"));
191 process->start(commands[0]);
195 bool XdxfDictDownloader::extract(QString file) {
197 FILE * archive = fopen(file.toStdString().c_str(), "rb");
201 BZFILE * afterbzFile = BZ2_bzReadOpen(&err, archive, 0, 0, 0, 0);
203 BZ2_bzReadClose(&err, afterbzFile);
207 FILE * tarfile = fopen(file.replace(QRegExp(".bz2$"), "").
208 toStdString().c_str(), "w");
212 while(err == BZ_OK) {
213 int len = BZ2_bzRead(&err, afterbzFile, buff, bufflen);
214 if(fwrite(buff, 1, len, tarfile) != len)
217 BZ2_bzReadClose(&err, afterbzFile);
223 char * tarfname = new char[file.replace(QRegExp(".bz2%"), "").size()+1];
224 strcpy(tarfname, file.replace(QRegExp(".bz2%"), "").toStdString().c_str());
226 err = tar_open(&t, tarfname, 0, O_RDONLY, 0, 0);
230 err = tar_extract_all(t, "/tmp/mdict/");