Merge branch 'http' of ssh://drop.maemo.org/git/mdictionary into http
[mdictionary] / src / plugins / xdxf / XdxfDictDownloader.cpp
index a0f6376..d65fbe9 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,8 @@ void XdxfDictDownloader::processFinished(int exitcode) {
 }
 
 void XdxfDictDownloader::downloadComplete() {
+<<<<<<< HEAD
+    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");
@@ -78,13 +110,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()));
 
     // You can look at http://xdxf.revdanica.com/down/, we need to get table
@@ -99,9 +139,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();
     }
 
@@ -109,8 +151,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();