Added download progress notification
[mdictionary] / src / plugins / xdxf / XdxfDictDownloader.cpp
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
5     mDictionary is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     mDictionary is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 /*!
23   \file XdxfDictDownloader.cpp
24   \author Mateusz Półrola <mateusz.polrola@comarch.com>
25   */
26
27 #include "XdxfDictDownloader.h"
28 #include "XdxfDictDownloadProgressDialog.h"
29 #include <QDebug>
30
31 #include <bzlib.h>
32 #include <libtar.h>
33 #include <stdio.h>
34 #include <fcntl.h>
35
36 typedef void BZFILE;
37
38
39
40 XdxfDictDownloader::XdxfDictDownloader(QObject *parent) :
41     QObject(parent) {
42     parentDialog = 0;
43     manager = new QNetworkAccessManager(this);
44
45     connect(manager, SIGNAL(finished(QNetworkReply*)),
46             this, SLOT(dictListReceived(QNetworkReply*)));
47
48
49     progressDialog = 0;
50     connect(&http, SIGNAL(finished()), this, SLOT(processFinished()));
51     connect(&http, SIGNAL(error(QString)),
52             this, SLOT(downloadingError(QString)));
53     connect(&http, SIGNAL(progress(qint64,qint64)),
54             this, SLOT(updateDownloadProgress(qint64,qint64)));
55
56 }
57
58 void XdxfDictDownloader::download(QWidget *parent) {
59     parentDialog = parent;
60     aborted = false;
61
62     manager->get(QNetworkRequest(QUrl("http://xdxf.revdanica.com/down/")));
63
64     progressDialog = new XdxfDictDownloadProgressDialog(parent);
65
66     connect(progressDialog, SIGNAL(cancelDownloading()),
67             this, SLOT(breakDownloading()));
68     connect(this, SIGNAL(downloadProgress(float)),
69             progressDialog, SLOT(updateProgress(float)));
70
71     progressDialog->setText(tr("Downloading dictionaries list"));
72     progressDialog->show();
73 }
74
75 QString XdxfDictDownloader::downloadedFile() {
76     return _downloadedFile;
77 }
78
79 void XdxfDictDownloader::updateDownloadProgress(qint64 downloaded,
80                                                 qint64 total)   {
81     Q_EMIT downloadProgress(float(downloaded) / float(total));
82 }
83
84 void XdxfDictDownloader::downloadingError(QString error) {
85     breakDownloading();
86     Q_EMIT notify(Notify::Error, error);
87 }
88
89 void XdxfDictDownloader::breakDownloading() {
90     //if user cancel downloading we kill all running processes, hide progress dialog and set flag that user cancel downloading.
91     aborted = true;
92     http.kill();
93
94     if(progressDialog && progressDialog->isVisible()) {
95         progressDialog->accept();
96     }
97
98 }
99
100 void XdxfDictDownloader::processFinished() {
101     //first check if user cancel downloading
102     if(aborted) return;
103
104     if(!extract("/tmp/" + _fileName)) {
105         Q_EMIT notify(Notify::Error,
106                 "Error while extracting dictionary archive");
107         return;
108     }
109     downloadComplete();
110 }
111
112 void XdxfDictDownloader::downloadComplete() {
113     if(aborted) return;
114     // Downloaded tar file name is different than extracted folder so we need
115     // some clean directory to identify extracted files
116     QDir dir("/tmp/mdict");
117     QString dictDirName = dir.entryList().at(2);
118
119     // Dict is in /tmp/mdict/<extracted directory>/dict.xdxf
120     QFile dictFile("/tmp/mdict/" + dictDirName + "/dict.xdxf");
121     dictFile.copy(QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf");
122     QFile::remove("/tmp/" + _fileName);
123     QFile::remove("/tmp/" + _fileName.replace(QRegExp(".bz2$"), ""));
124
125     _downloadedFile = QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf";
126
127     progressDialog->accept();
128     delete progressDialog;
129     progressDialog = 0;
130
131     emit fileDownloaded(_downloadedFile);
132 }
133
134 void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
135     progressDialog->accept();
136     if(aborted) return;
137
138
139     if(reply->error() != QNetworkReply::NoError) {
140         Q_EMIT notify(Notify::Error, reply->errorString());
141         return;
142     }
143
144     QString page(QString::fromUtf8(reply->readAll()));
145
146     // You can look at http://xdxf.revdanica.com/down/, we need to get table
147     // with dictionaries entries following regexp match its begining
148     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>");
149     regOuter.setMinimal(true);
150     if(!regOuter.indexIn(page))
151         return;
152
153     // Cutting each entry and creating coresponded DownloadDict object
154     page = regOuter.capturedTexts().at(1);
155     QRegExp regInner("<tr>.*</tr>");
156     regInner.setMinimal(true);
157     int pos = 0;
158
159     while ((pos = regInner.indexIn(page, pos)) != -1) {
160         DownloadDict temp = DownloadDict(regInner.cap(0));
161         if(!temp.fromLang().isEmpty())
162             dicts.append(temp);
163         pos += regInner.matchedLength();
164     }
165
166     XdxfDictSelectDialog selectDialog(dicts, parentDialog);
167
168     if(selectDialog.exec()==QDialog::Accepted) {
169
170         progressDialog->setText(tr("Downloading dictionary"));
171         progressDialog->show();
172
173         QString url = selectDialog.link();
174
175         _fileName = url.split('/').last();
176
177         QProcess clean;
178         clean.start("rm -rf /tmp/mdict");
179         clean.waitForFinished(-1);
180         clean.start("mkdir /tmp/mdict");
181         clean.waitForFinished(-1);
182
183         http.download(QUrl(url), "/tmp/" + _fileName);
184     }
185 }
186
187 bool XdxfDictDownloader::extract(QString file) {
188     // Extracting bz2
189     FILE * archive = fopen(file.toStdString().c_str(), "rb");
190     if (archive == 0)
191         return false;
192     int err;
193     BZFILE * afterbzFile = BZ2_bzReadOpen(&err, archive, 0, 0, 0, 0);
194     if(err != BZ_OK) {
195         BZ2_bzReadClose(&err, afterbzFile);
196         return false;
197     }
198
199     FILE * tarfile = fopen(file.replace(QRegExp(".bz2$"), "").
200             toStdString().c_str(), "w");
201
202     int bufflen = 100;
203     char buff[bufflen];
204     while(err == BZ_OK) {
205         int len = BZ2_bzRead(&err, afterbzFile, buff, bufflen);
206         if(fwrite(buff, 1, len, tarfile) != len)
207             return false;
208     }
209     BZ2_bzReadClose(&err, afterbzFile);
210     fclose(tarfile);
211     fclose(archive);
212
213     // Extracting tar
214     TAR *t;
215     char * tarfname = new char[file.replace(QRegExp(".bz2%"), "").size()+1];
216     strcpy(tarfname, file.replace(QRegExp(".bz2%"), "").toStdString().c_str());
217
218     err = tar_open(&t, tarfname, 0, O_RDONLY, 0, 0);
219     if(err == -1)
220         return false;
221
222     err = tar_extract_all(t, "/tmp/mdict/");
223     if(err == -1) {
224         return false;
225     }
226     tar_close(t);
227
228     return true;
229 }
230
231