Merge branch 'stardict' of ssh://drop.maemo.org/git/mdictionary into stardict
[mdictionary] / src / plugins / xdxf / XdxfDictDownloader.cpp
index 3a916cf..1b65408 100644 (file)
     Copyright 2010 Comarch S.A.
 
 *******************************************************************************/
-
-//Created by Mateusz Półrola
+/*!
+    \file XdxfDictDownloader.cpp
+    \author Mateusz Półrola <mateusz.polrola@comarch.com>
+*/
 
 #include "XdxfDictDownloader.h"
 #include "XdxfDictDownloadProgressDialog.h"
 #include <QDebug>
+#include <QProcess>
+#include <bzlib.h>
+#ifndef Q_WS_MAEMO_5
+#include <libtar.h>
+#endif
+#include <stdio.h>
+#include <fcntl.h>
 
+typedef void BZFILE;
 
 XdxfDictDownloader::XdxfDictDownloader(QObject *parent) :
     QObject(parent) {
     parentDialog = 0;
-    process = new QProcess(this);
-
-
-    connect(process, SIGNAL(finished(int)),
-            this, SLOT(processFinished(int)));
-
     progressDialog = 0;
-}
-
-XdxfDictDownloader::~XdxfDictDownloader() {
+    manager = new QNetworkAccessManager(this);
 
+    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;
     aborted = false;
-    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
-
-    connect(manager, SIGNAL(finished(QNetworkReply*)),
-            this, SLOT(dictListReceived(QNetworkReply*)));
 
     manager->get(QNetworkRequest(QUrl("http://xdxf.revdanica.com/down/")));
 
@@ -58,48 +63,67 @@ void XdxfDictDownloader::download(QWidget *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::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;
-    if(process->state() != QProcess::NotRunning) {
-        process->kill();
-    }
+    http.kill();
 
     if(progressDialog && progressDialog->isVisible()) {
         progressDialog->accept();
     }
-
-    Q_EMIT notify(Notify::Info, tr("Downloading canceled"));
 }
 
-void XdxfDictDownloader::processFinished(int exitcode) {
+
+void XdxfDictDownloader::processFinished() {
+    //first check if user cancel downloading
     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]);
-    }
-    else {
-        downloadComplete();
+
+    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");
     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);
+    QFile::remove("/tmp/" + _fileName.replace(QRegExp(".bz2$"), ""));
 
     _downloadedFile = QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf";
 
@@ -110,22 +134,26 @@ void XdxfDictDownloader::downloadComplete() {
     emit fileDownloaded(_downloadedFile);
 }
 
-void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
 
-    if(aborted) return;
+void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
     progressDialog->accept();
-
+    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
+    // 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);
@@ -149,12 +177,64 @@ void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
 
         _fileName = url.split('/').last();
 
-        commands.clear();
-        commands.push_back("rm -rf /tmp/mdict");
-        commands.push_back("mkdir /tmp/mdict");
-        commands.push_back("wget --quiet -P /tmp/ " + url);
-        commands.push_back(QString("tar -xjvf /tmp/") + _fileName + QString(" -C /tmp/mdict"));
+        QProcess clean;
+        clean.start("rm -rf /tmp/mdict");
+        clean.waitForFinished(-1);
+        clean.start("mkdir /tmp/mdict");
+        clean.waitForFinished(-1);
 
-        process->start(commands[0]);
+        http.download(QUrl(url), "/tmp/" + _fileName);
     }
 }
+
+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;
+    }
+
+    FILE * tarfile = fopen(file.replace(QRegExp(".bz2$"), "").
+            toStdString().c_str(), "w");
+
+    int bufflen = 100;
+    char buff[bufflen];
+    while(err == BZ_OK) {
+        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;
+
+    err = tar_extract_all(t, "/tmp/mdict/");
+    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;
+}
+
+