Repair problem with build app on scratchbox. Add rules to build deb package with...
[mdictionary] / src / plugins / xdxf / XdxfDictDownloader.cpp
index 677be7f..887fdf5 100644 (file)
 #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>
 
@@ -40,16 +44,19 @@ typedef void BZFILE;
 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;
+    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) {
@@ -62,6 +69,9 @@ 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();
 }
@@ -70,13 +80,20 @@ 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();
@@ -84,24 +101,16 @@ void XdxfDictDownloader::breakDownloading() {
 
 }
 
-void XdxfDictDownloader::processFinished(int exitcode) {
+void XdxfDictDownloader::processFinished() {
     //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();
+    if(!extract("/tmp/" + _fileName)) {
+        Q_EMIT notify(Notify::Error,
+                "Error while extracting dictionary archive");
         return;
     }
-    //if there are any left commands, execute next
-    if(++currentCommand<commands.size()) {
-        process->start(commands[currentCommand]);
-    }
-    else {
-        qDebug() << "EXTRACT" << extract("/tmp/" + _fileName);
-        downloadComplete();
-    }
+    downloadComplete();
 }
 
 void XdxfDictDownloader::downloadComplete() {
@@ -127,9 +136,9 @@ void XdxfDictDownloader::downloadComplete() {
 }
 
 void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
-
-    if(aborted) return;
     progressDialog->accept();
+    if(aborted) return;
+
 
     if(reply->error() != QNetworkReply::NoError) {
         Q_EMIT notify(Notify::Error, reply->errorString());
@@ -169,26 +178,13 @@ void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
 
         _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"));
+        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);
     }
 }
 
@@ -219,6 +215,7 @@ bool XdxfDictDownloader::extract(QString file) {
     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());
@@ -232,6 +229,11 @@ bool XdxfDictDownloader::extract(QString file) {
         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;
 }