Copyright 2010 Comarch S.A.
*******************************************************************************/
+/*!
+ \file XdxfDictDownloader.cpp
+ \brief XdxfDictDownloader is responsible for getting dict list from XDXF website
+ and other actions necessary to download and add dictionary
-//Created by Mateusz Półrola
+ \author Mateusz Półrola <mateusz.polrola@comarch.com>
+*/
#include "XdxfDictDownloader.h"
#include "XdxfDictDownloadProgressDialog.h"
#include <QDebug>
+#include <QProcess>
+#include <bzlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+
+#ifndef Q_WS_MAEMO_5
+ #include <libtar.h>
+#endif
+
+
+typedef void BZFILE;
XdxfDictDownloader::XdxfDictDownloader(QObject *parent) :
QObject(parent) {
parentDialog = 0;
- process = new QProcess(this);
+ progressDialog = 0;
+ manager = new QNetworkAccessManager(this);
- connect(process, SIGNAL(finished(int)),
- this, SLOT(processFinished(int)));
+ connect(manager, SIGNAL(finished(QNetworkReply*)),
+ this, SLOT(dictListReceived(QNetworkReply*)));
+ connect(&http, SIGNAL(finished()),
+ this, SLOT(processFinished()));
+ connect(&http, SIGNAL(error(QString)),
+ this, SLOT(downloadingError(QString)));
+ connect(&http, SIGNAL(progress(qint64,qint64)),
+ this, SLOT(updateDownloadProgress(qint64,qint64)));
}
-
void XdxfDictDownloader::download(QWidget *parent) {
parentDialog = parent;
- QNetworkAccessManager *manager = new QNetworkAccessManager(this);
-
- connect(manager, SIGNAL(finished(QNetworkReply*)),
- this, SLOT(dictListReceived(QNetworkReply*)));
+ aborted = false;
manager->get(QNetworkRequest(QUrl("http://xdxf.revdanica.com/down/")));
- progressDialog = new XdxfDictDownloadProgressDialog(tr("Downloading dictionaries list"), parent);
+ progressDialog = new XdxfDictDownloadProgressDialog(parent);
+
+ connect(progressDialog, SIGNAL(cancelDownloading()),
+ this, SLOT(breakDownloading()));
+ connect(this, SIGNAL(downloadProgress(float)),
+ progressDialog, SLOT(updateProgress(float)));
+ progressDialog->setText(tr("Downloading dictionaries list"));
progressDialog->show();
}
+
QString XdxfDictDownloader::downloadedFile() {
return _downloadedFile;
}
-void XdxfDictDownloader::processFinished(int exitcode) {
- if(++currentCommand<commands.size()) {
- process->start(commands[currentCommand]);
+
+void XdxfDictDownloader::updateDownloadProgress(qint64 downloaded,
+ qint64 total) {
+ Q_EMIT downloadProgress(float(downloaded) / float(total));
+}
+
+
+void XdxfDictDownloader::downloadingError(QString error) {
+ breakDownloading();
+ Q_EMIT notify(Notify::Error, error);
+}
+
+
+void XdxfDictDownloader::breakDownloading() {
+ //if user cancel downloading we kill all running processes, hide progress dialog and set flag that user cancel downloading.
+ aborted = true;
+ http.kill();
+ if(progressDialog && progressDialog->isVisible()) {
+ progressDialog->accept();
}
- else {
- downloadComplete();
+}
+
+
+void XdxfDictDownloader::processFinished() {
+ //first check if user cancel downloading
+ if(aborted) return;
+
+ if(!extract("/tmp/" + _fileName)) {
+ Q_EMIT notify(Notify::Error,
+ "Error while extracting dictionary archive");
+ return;
}
+ downloadComplete();
}
+
void XdxfDictDownloader::downloadComplete() {
+ if(aborted) return;
// Downloaded tar file name is different than extracted folder so we need
// some clean directory to identify extracted files
QDir dir("/tmp/mdict");
QFile dictFile("/tmp/mdict/" + dictDirName + "/dict.xdxf");
dictFile.copy(QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf");
QFile::remove("/tmp/" + _fileName);
+ QFile::remove("/tmp/" + _fileName.replace(QRegExp(".bz2$"), ""));
_downloadedFile = QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf";
progressDialog->accept();
delete progressDialog;
+ progressDialog = 0;
+
emit fileDownloaded(_downloadedFile);
}
-void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
+void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
progressDialog->accept();
- delete progressDialog;
+ if(aborted)
+ return;
+ if(reply->error() != QNetworkReply::NoError) {
+ Q_EMIT notify(Notify::Error, reply->errorString());
+ return;
+ }
+
QString page(QString::fromUtf8(reply->readAll()));
// You can look at http://xdxf.revdanica.com/down/, we need to get table
QRegExp regInner("<tr>.*</tr>");
regInner.setMinimal(true);
int pos = 0;
- QStringList tmp;
+ dicts.clear();
while ((pos = regInner.indexIn(page, pos)) != -1) {
- dicts.append(DownloadDict(regInner.cap(0)));
+ DownloadDict temp = DownloadDict(regInner.cap(0));
+ if(!temp.fromLang().isEmpty())
+ dicts.append(temp);
pos += regInner.matchedLength();
}
XdxfDictSelectDialog selectDialog(dicts, parentDialog);
if(selectDialog.exec()==QDialog::Accepted) {
-
- progressDialog = new XdxfDictDownloadProgressDialog(tr("Downloading dictionary"), parentDialog);
+ progressDialog->setText(tr("Downloading dictionary"));
progressDialog->show();
- QString url = selectDialog.link();
+ QString url = selectDialog.link();
_fileName = url.split('/').last();
- // Now its the tricky part ... its temporary (probably)
- // We dont have any tar-dev and bz2-dev packages on maemo so we need
- // to call commands via shell, each command from list is called after
- // previous call returns 0
+ QProcess clean;
+ clean.start("rm -rf /tmp/mdict");
+ clean.waitForFinished(-1);
+ clean.start("mkdir /tmp/mdict");
+ clean.waitForFinished(-1);
+
+ http.download(QUrl(url), "/tmp/" + _fileName);
+ }
+}
- commands.clear();
- commands.push_back("rm -rf /tmp/mdict");
- commands.push_back("mkdir /tmp/mdict");
+bool XdxfDictDownloader::extract(QString file) {
+ // Extracting bz2
+ FILE * archive = fopen(file.toStdString().c_str(), "rb");
+ if (archive == 0)
+ return false;
+ int err;
+ BZFILE * afterbzFile = BZ2_bzReadOpen(&err, archive, 0, 0, 0, 0);
+ if(err != BZ_OK) {
+ BZ2_bzReadClose(&err, afterbzFile);
+ return false;
+ }
- // Downloading xdxf dict from sourceforge is kind of complicated,
- // there is a lot of redirection and QNetwork* is kind of lost, we
- // tried to follow redirection (by hand) but we end up with some
- // page and js scripts and thats all
- // Maybe calling wget is not pretty one but its working!
- commands.push_back("wget --quiet -P /tmp/ " + url);
- commands.push_back(QString("tar -xjvf /tmp/") + _fileName + QString(" -C /tmp/mdict"));
+ FILE * tarfile = fopen(file.replace(QRegExp(".bz2$"), "").
+ toStdString().c_str(), "w");
- process->start(commands[0]);
+ int bufflen = 100;
+ char buff[bufflen];
+ while(err == BZ_OK) {
+ unsigned int len = BZ2_bzRead(&err, afterbzFile, buff, bufflen);
+ if(fwrite(buff, 1, len, tarfile) != len)
+ return false;
+ }
+ BZ2_bzReadClose(&err, afterbzFile);
+ fclose(tarfile);
+ fclose(archive);
+
+ // Extracting tar
+ #ifndef Q_WS_MAEMO_5
+ TAR *t;
+ char * tarfname = new char[file.replace(QRegExp(".bz2%"), "").size()+1];
+ strcpy(tarfname, file.replace(QRegExp(".bz2%"), "").toStdString().c_str());
+
+ err = tar_open(&t, tarfname, 0, O_RDONLY, 0, 0);
+ if(err == -1)
+ return false;
+
+ char text[]={"/tmp/mdict/"};
+ err = tar_extract_all(t,text);
+ if(err == -1) {
+ return false;
}
+ tar_close(t);
+ #else
+ QProcess tar;
+ tar.start("tar -xvf " + file.replace(QRegExp(".bz2%"), "") + " -C /tmp/mdict");
+ tar.waitForFinished(-1);
+ #endif
+
+ return true;
}
+
+
+