Merge branch 'stardict' of ssh://drop.maemo.org/git/mdictionary into stardict
[mdictionary] / src / plugins / xdxf / XdxfDictDownloader.cpp
index 6e96c98..1b65408 100644 (file)
     Copyright 2010 Comarch S.A.
 
 *******************************************************************************/
-
 /*!
-  \file XdxfDictDownloader.cpp
-  \author Mateusz Półrola <mateusz.polrola@comarch.com>
-  */
+    \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;
+    progressDialog = 0;
     manager = new QNetworkAccessManager(this);
 
     connect(manager, SIGNAL(finished(QNetworkReply*)),
             this, SLOT(dictListReceived(QNetworkReply*)));
-
-
-    progressDialog = 0;
-    connect(&http, SIGNAL(finished()), this, SLOT(processFinished()));
+    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;
@@ -60,15 +63,31 @@ 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;
@@ -77,9 +96,9 @@ void XdxfDictDownloader::breakDownloading() {
     if(progressDialog && progressDialog->isVisible()) {
         progressDialog->accept();
     }
-
 }
 
+
 void XdxfDictDownloader::processFinished() {
     //first check if user cancel downloading
     if(aborted) return;
@@ -92,6 +111,7 @@ void XdxfDictDownloader::processFinished() {
     downloadComplete();
 }
 
+
 void XdxfDictDownloader::downloadComplete() {
     if(aborted) return;
     // Downloaded tar file name is different than extracted folder so we need
@@ -114,11 +134,11 @@ 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;
@@ -194,6 +214,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());
@@ -207,6 +228,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;
 }