Add comments and translations for xdxf downloading dialog
[mdictionary] / src / plugins / xdxf / XdxfDictDownloader.cpp
index a0f6376..2b62ff6 100644 (file)
 
 *******************************************************************************/
 
-//Created by Mateusz Półrola
+/*!
+  \file XdxfDictDownloader.cpp
+  \author Mateusz Półrola <mateusz.polrola@comarch.com>
+  */
 
 #include "XdxfDictDownloader.h"
 #include "XdxfDictDownloadProgressDialog.h"
@@ -30,23 +33,28 @@ XdxfDictDownloader::XdxfDictDownloader(QObject *parent) :
     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();
 }
 
@@ -54,7 +62,31 @@ QString XdxfDictDownloader::downloadedFile() {
     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]);
     }
@@ -64,6 +96,7 @@ void XdxfDictDownloader::processFinished(int exitcode) {
 }
 
 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");
@@ -78,13 +111,21 @@ void XdxfDictDownloader::downloadComplete() {
 
     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
@@ -99,9 +140,11 @@ void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
     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();
     }
 
@@ -109,8 +152,9 @@ void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
 
     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();
@@ -120,6 +164,7 @@ void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
         // 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");