*******************************************************************************/
-//Created by Mateusz Półrola
+/*!
+ \file XdxfDictDownloader.cpp
+ \author Mateusz Półrola <mateusz.polrola@comarch.com>
+ */
#include "XdxfDictDownloader.h"
#include "XdxfDictDownloadProgressDialog.h"
QObject(parent) {
parentDialog = 0;
process = new QProcess(this);
+ manager = new QNetworkAccessManager(this);
+
+ connect(manager, SIGNAL(finished(QNetworkReply*)),
+ this, SLOT(dictListReceived(QNetworkReply*)));
connect(process, SIGNAL(finished(int)),
this, SLOT(processFinished(int)));
-}
-
+ progressDialog = 0;
+}
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()));
+ progressDialog->setText(tr("Downloading dictionaries list"));
progressDialog->show();
}
return _downloadedFile;
}
+
+void XdxfDictDownloader::breakDownloading() {
+ //if user cancel downloading we kill all running processes, hide progress dialog and set flag that user cancel downloading.
+ aborted = true;
+ if(process->state() != QProcess::NotRunning) {
+ process->kill();
+ }
+
+ if(progressDialog && progressDialog->isVisible()) {
+ progressDialog->accept();
+ }
+
+}
+
void XdxfDictDownloader::processFinished(int exitcode) {
+ //first check if user cancel downloading
+ if(aborted) return;
+
+ //if error of proces, notify user about this
+ if(exitcode != 0) {
+ Q_EMIT notify(Notify::Error, tr("Error while downloading or processing dictionary"));
+ breakDownloading();
+ return;
+ }
+ //if there are any left commands, execute next
if(++currentCommand<commands.size()) {
process->start(commands[currentCommand]);
}
}
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");
progressDialog->accept();
delete progressDialog;
+ progressDialog = 0;
+
emit fileDownloaded(_downloadedFile);
}
void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
+ if(aborted) return;
progressDialog->accept();
- delete progressDialog;
+
+ 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;
+
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();
}
if(selectDialog.exec()==QDialog::Accepted) {
- progressDialog = new XdxfDictDownloadProgressDialog(tr("Downloading dictionary"), parentDialog);
+ progressDialog->setText(tr("Downloading dictionary"));
progressDialog->show();
+
QString url = selectDialog.link();
_fileName = url.split('/').last();
// to call commands via shell, each command from list is called after
// previous call returns 0
+ currentCommand = 0;
commands.clear();
commands.push_back("rm -rf /tmp/mdict");
commands.push_back("mkdir /tmp/mdict");