change comment's and fix bug (xslt transform)
[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     \file XdxfDictDownloader.cpp
23     \author Mateusz Półrola <mateusz.polrola@comarch.com>
24 */
25
26 #include "XdxfDictDownloader.h"
27 #include "XdxfDictDownloadProgressDialog.h"
28 #include <QDebug>
29 #include <QProcess>
30 #include <bzlib.h>
31 #include <stdio.h>
32 #include <fcntl.h>
33
34 #ifndef Q_WS_MAEMO_5
35     #include <libtar.h>
36 #endif
37
38
39 typedef void BZFILE;
40
41
42 XdxfDictDownloader::XdxfDictDownloader(QObject *parent) :
43     QObject(parent) {
44     parentDialog = 0;
45     progressDialog = 0;
46     manager = new QNetworkAccessManager(this);
47
48     connect(manager, SIGNAL(finished(QNetworkReply*)),
49             this, SLOT(dictListReceived(QNetworkReply*)));
50     connect(&http, SIGNAL(finished()),
51             this, SLOT(processFinished()));
52     connect(&http, SIGNAL(error(QString)),
53             this, SLOT(downloadingError(QString)));
54     connect(&http, SIGNAL(progress(qint64,qint64)),
55             this, SLOT(updateDownloadProgress(qint64,qint64)));
56 }
57
58
59 void XdxfDictDownloader::download(QWidget *parent) {
60     parentDialog = parent;
61     aborted = false;
62
63     manager->get(QNetworkRequest(QUrl("http://xdxf.revdanica.com/down/")));
64
65     progressDialog = new XdxfDictDownloadProgressDialog(parent);
66
67     connect(progressDialog, SIGNAL(cancelDownloading()),
68             this, SLOT(breakDownloading()));
69     connect(this, SIGNAL(downloadProgress(float)),
70             progressDialog, SLOT(updateProgress(float)));
71
72     progressDialog->setText(tr("Downloading dictionaries list"));
73     progressDialog->show();
74 }
75
76
77 QString XdxfDictDownloader::downloadedFile() {
78     return _downloadedFile;
79 }
80
81
82 void XdxfDictDownloader::updateDownloadProgress(qint64 downloaded,
83                                                 qint64 total)   {
84     Q_EMIT downloadProgress(float(downloaded) / float(total));
85 }
86
87
88 void XdxfDictDownloader::downloadingError(QString error) {
89     breakDownloading();
90     Q_EMIT notify(Notify::Error, error);
91 }
92
93
94 void XdxfDictDownloader::breakDownloading() {
95     //if user cancel downloading we kill all running processes, hide progress dialog and set flag that user cancel downloading.
96     aborted = true;
97     http.kill();
98
99     if(progressDialog && progressDialog->isVisible()) {
100         progressDialog->accept();
101     }
102 }
103
104
105 void XdxfDictDownloader::processFinished() {
106     //first check if user cancel downloading
107     if(aborted) return;
108
109     if(!extract("/tmp/" + _fileName)) {
110         Q_EMIT notify(Notify::Error,
111                 "Error while extracting dictionary archive");
112         return;
113     }
114     downloadComplete();
115 }
116
117
118 void XdxfDictDownloader::downloadComplete() {
119     if(aborted) return;
120     // Downloaded tar file name is different than extracted folder so we need
121     // some clean directory to identify extracted files
122     QDir dir("/tmp/mdict");
123     QString dictDirName = dir.entryList().at(2);
124
125     // Dict is in /tmp/mdict/<extracted directory>/dict.xdxf
126     QFile dictFile("/tmp/mdict/" + dictDirName + "/dict.xdxf");
127     dictFile.copy(QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf");
128     QFile::remove("/tmp/" + _fileName);
129     QFile::remove("/tmp/" + _fileName.replace(QRegExp(".bz2$"), ""));
130
131     _downloadedFile = QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf";
132
133     progressDialog->accept();
134     delete progressDialog;
135     progressDialog = 0;
136
137     emit fileDownloaded(_downloadedFile);
138 }
139
140
141 void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
142     progressDialog->accept();
143     if(aborted)
144         return;
145     if(reply->error() != QNetworkReply::NoError) {
146         Q_EMIT notify(Notify::Error, reply->errorString());
147         return;
148     }
149
150     QString page(QString::fromUtf8(reply->readAll()));
151
152     // You can look at http://xdxf.revdanica.com/down/, we need to get table
153     // with dictionaries entries following regexp match its begining
154     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>");
155     regOuter.setMinimal(true);
156     if(!regOuter.indexIn(page))
157         return;
158
159     // Cutting each entry and creating coresponded DownloadDict object
160     page = regOuter.capturedTexts().at(1);
161     QRegExp regInner("<tr>.*</tr>");
162     regInner.setMinimal(true);
163     int pos = 0;
164
165     while ((pos = regInner.indexIn(page, pos)) != -1) {
166         DownloadDict temp = DownloadDict(regInner.cap(0));
167         if(!temp.fromLang().isEmpty())
168             dicts.append(temp);
169         pos += regInner.matchedLength();
170     }
171
172     XdxfDictSelectDialog selectDialog(dicts, parentDialog);
173
174     if(selectDialog.exec()==QDialog::Accepted) {
175
176         progressDialog->setText(tr("Downloading dictionary"));
177         progressDialog->show();
178
179         QString url = selectDialog.link();
180         _fileName = url.split('/').last();
181
182         QProcess clean;
183         clean.start("rm -rf /tmp/mdict");
184         clean.waitForFinished(-1);
185         clean.start("mkdir /tmp/mdict");
186         clean.waitForFinished(-1);
187
188         http.download(QUrl(url), "/tmp/" + _fileName);
189     }
190 }
191
192 bool XdxfDictDownloader::extract(QString file) {
193     // Extracting bz2
194     FILE * archive = fopen(file.toStdString().c_str(), "rb");
195     if (archive == 0)
196         return false;
197     int err;
198     BZFILE * afterbzFile = BZ2_bzReadOpen(&err, archive, 0, 0, 0, 0);
199     if(err != BZ_OK) {
200         BZ2_bzReadClose(&err, afterbzFile);
201         return false;
202     }
203
204     FILE * tarfile = fopen(file.replace(QRegExp(".bz2$"), "").
205             toStdString().c_str(), "w");
206
207     int bufflen = 100;
208     char buff[bufflen];
209     while(err == BZ_OK) {
210         unsigned int len = BZ2_bzRead(&err, afterbzFile, buff, bufflen);
211         if(fwrite(buff, 1, len, tarfile) != len)
212             return false;
213     }
214     BZ2_bzReadClose(&err, afterbzFile);
215     fclose(tarfile);
216     fclose(archive);
217
218     // Extracting tar
219     #ifndef Q_WS_MAEMO_5
220     TAR *t;
221     char * tarfname = new char[file.replace(QRegExp(".bz2%"), "").size()+1];
222     strcpy(tarfname, file.replace(QRegExp(".bz2%"), "").toStdString().c_str());
223
224     err = tar_open(&t, tarfname, 0, O_RDONLY, 0, 0);
225     if(err == -1)
226         return false;
227
228     char text[]={"/tmp/mdict/"};
229     err = tar_extract_all(t,text);
230     if(err == -1) {
231         return false;
232     }
233     tar_close(t);
234     #else
235     QProcess tar;
236     tar.start("tar -xvf " + file.replace(QRegExp(".bz2%"), "") + " -C /tmp/mdict");
237     tar.waitForFinished(-1);
238     #endif
239
240     return true;
241 }
242
243