Add comments and translations for xdxf downloading dialog
[mdictionary] / src / plugins / xdxf / XdxfDictDownloader.cpp
index b7acba9..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"
+#include <QDebug>
 
 
 XdxfDictDownloader::XdxfDictDownloader(QObject *parent) :
-    QObject(parent)
-{
-}
+    QObject(parent) {
+    parentDialog = 0;
+    process = new QProcess(this);
+    manager = new QNetworkAccessManager(this);
 
+    connect(manager, SIGNAL(finished(QNetworkReply*)),
+            this, SLOT(dictListReceived(QNetworkReply*)));
 
-void XdxfDictDownloader::download(QWidget *parent) {
+    connect(process, SIGNAL(finished(int)),
+            this, SLOT(processFinished(int)));
 
-    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
+    progressDialog = 0;
+}
 
-    connect(manager, SIGNAL(finished(QNetworkReply*)),
-            this, SLOT(dictListReceived(QNetworkReply*)));
+void XdxfDictDownloader::download(QWidget *parent) {
+    parentDialog = parent;
+    aborted = false;
 
     manager->get(QNetworkRequest(QUrl("http://xdxf.revdanica.com/down/")));
 
-    XdxfDictSelectDialog selectDialog(parent);
+    progressDialog = new XdxfDictDownloadProgressDialog(parent);
+
+    connect(progressDialog, SIGNAL(cancelDownloading()),
+            this, SLOT(breakDownloading()));
+    progressDialog->setText(tr("Downloading dictionaries list"));
+    progressDialog->show();
+}
 
-    selectDialog.exec();
+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]);
+    }
+    else {
+        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");
+    QString dictDirName = dir.entryList().at(2);
+
+    // Dict is in /tmp/mdict/<extracted directory>/dict.xdxf
+    QFile dictFile("/tmp/mdict/" + dictDirName + "/dict.xdxf");
+    dictFile.copy(QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf");
+    QFile::remove("/tmp/" + _fileName);
+
+    _downloadedFile = QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf";
+
+    progressDialog->accept();
+    delete progressDialog;
+    progressDialog = 0;
+
+    emit fileDownloaded(_downloadedFile);
+}
+
 void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
 
+    if(aborted) return;
+    progressDialog->accept();
+
+    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
+    // with dictionaries entries following regexp match its begining
+    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);
+    if(!regOuter.indexIn(page))
+        return;
+
+    // Cutting each entry and creating coresponded DownloadDict object
+    page = regOuter.capturedTexts().at(1);
+    QRegExp regInner("<tr>.*</tr>");
+    regInner.setMinimal(true);
+    int pos = 0;
+
+    while ((pos = regInner.indexIn(page, pos)) != -1) {
+        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->setText(tr("Downloading dictionary"));
+        progressDialog->show();
+
+        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
+
+        currentCommand = 0;
+        commands.clear();
+        commands.push_back("rm -rf /tmp/mdict");
+        commands.push_back("mkdir /tmp/mdict");
+
+        // 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"));
+
+        process->start(commands[0]);
+    }
 }