Added notifications and canceling to xdxf download dialog
[mdictionary] / src / plugins / xdxf / XdxfDictDownloader.cpp
index d5ce72b..3a916cf 100644 (file)
@@ -31,14 +31,22 @@ XdxfDictDownloader::XdxfDictDownloader(QObject *parent) :
     parentDialog = 0;
     process = new QProcess(this);
 
+
     connect(process, SIGNAL(finished(int)),
             this, SLOT(processFinished(int)));
+
+    progressDialog = 0;
+}
+
+XdxfDictDownloader::~XdxfDictDownloader() {
+
 }
 
 
 
 void XdxfDictDownloader::download(QWidget *parent) {
     parentDialog = parent;
+    aborted = false;
     QNetworkAccessManager *manager = new QNetworkAccessManager(this);
 
     connect(manager, SIGNAL(finished(QNetworkReply*)),
@@ -46,7 +54,11 @@ void XdxfDictDownloader::download(QWidget *parent) {
 
     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 +66,25 @@ QString XdxfDictDownloader::downloadedFile() {
     return _downloadedFile;
 }
 
+
+void XdxfDictDownloader::breakDownloading() {
+    aborted = true;
+    if(process->state() != QProcess::NotRunning) {
+        process->kill();
+    }
+
+    if(progressDialog && progressDialog->isVisible()) {
+        progressDialog->accept();
+    }
+
+    Q_EMIT notify(Notify::Info, tr("Downloading canceled"));
+}
+
 void XdxfDictDownloader::processFinished(int exitcode) {
+    if(aborted) return;
+    if(exitcode != 0) {
+        Q_EMIT notify(Notify::Error, tr("Error while downloading or processing dictionary"));
+    }
     if(++currentCommand<commands.size()) {
         process->start(commands[currentCommand]);
     }
@@ -64,6 +94,7 @@ void XdxfDictDownloader::processFinished(int exitcode) {
 }
 
 void XdxfDictDownloader::downloadComplete() {
+    if(aborted) return;
     QDir dir("/tmp/mdict");
     QString dictDirName = dir.entryList().at(2);
     QFile dictFile("/tmp/mdict/" + dictDirName + "/dict.xdxf");
@@ -74,13 +105,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()));
     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>");
     regOuter.setMinimal(true);
@@ -91,9 +130,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();
     }
 
@@ -101,8 +142,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();