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