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