Added downloads of xdxf dicts
[mdictionary] / src / plugins / xdxf / XdxfDictDownloader.cpp
index 918d533..d5ce72b 100644 (file)
 XdxfDictDownloader::XdxfDictDownloader(QObject *parent) :
     QObject(parent) {
     parentDialog = 0;
+    process = new QProcess(this);
+
+    connect(process, SIGNAL(finished(int)),
+            this, SLOT(processFinished(int)));
 }
 
 
@@ -41,11 +45,42 @@ void XdxfDictDownloader::download(QWidget *parent) {
             this, SLOT(dictListReceived(QNetworkReply*)));
 
     manager->get(QNetworkRequest(QUrl("http://xdxf.revdanica.com/down/")));
+
+    progressDialog = new XdxfDictDownloadProgressDialog(tr("Downloading dictionaries list"), parent);
+    progressDialog->show();
+}
+
+QString XdxfDictDownloader::downloadedFile() {
+    return _downloadedFile;
+}
+
+void XdxfDictDownloader::processFinished(int exitcode) {
+    if(++currentCommand<commands.size()) {
+        process->start(commands[currentCommand]);
+    }
+    else {
+        downloadComplete();
+    }
 }
 
+void XdxfDictDownloader::downloadComplete() {
+    QDir dir("/tmp/mdict");
+    QString dictDirName = dir.entryList().at(2);
+    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;
+    emit fileDownloaded(_downloadedFile);
+}
 
 void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
 
+    progressDialog->accept();
+    delete progressDialog;
     QString page(QString::fromUtf8(reply->readAll()));
     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);
@@ -64,10 +99,20 @@ void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
 
     XdxfDictSelectDialog selectDialog(dicts, parentDialog);
 
-    if(selectDialog.exec()==QDialog::accept()) {
+    if(selectDialog.exec()==QDialog::Accepted) {
+
+        progressDialog = new XdxfDictDownloadProgressDialog(tr("Downloading dictionary"), parentDialog);
+        progressDialog->show();
         QString url = selectDialog.link();
 
+        _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"));
 
+        process->start(commands[0]);
     }
 }