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