677be7f7861e2d3eb2ca5ac4266a1d21f2435233
[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     process = new QProcess(this);
44     manager = new QNetworkAccessManager(this);
45
46     connect(manager, SIGNAL(finished(QNetworkReply*)),
47             this, SLOT(dictListReceived(QNetworkReply*)));
48
49     connect(process, SIGNAL(finished(int)),
50             this, SLOT(processFinished(int)));
51
52     progressDialog = 0;
53 }
54
55 void XdxfDictDownloader::download(QWidget *parent) {
56     parentDialog = parent;
57     aborted = false;
58
59     manager->get(QNetworkRequest(QUrl("http://xdxf.revdanica.com/down/")));
60
61     progressDialog = new XdxfDictDownloadProgressDialog(parent);
62
63     connect(progressDialog, SIGNAL(cancelDownloading()),
64             this, SLOT(breakDownloading()));
65     progressDialog->setText(tr("Downloading dictionaries list"));
66     progressDialog->show();
67 }
68
69 QString XdxfDictDownloader::downloadedFile() {
70     return _downloadedFile;
71 }
72
73
74 void XdxfDictDownloader::breakDownloading() {
75     //if user cancel downloading we kill all running processes, hide progress dialog and set flag that user cancel downloading.
76     aborted = true;
77     if(process->state() != QProcess::NotRunning) {
78         process->kill();
79     }
80
81     if(progressDialog && progressDialog->isVisible()) {
82         progressDialog->accept();
83     }
84
85 }
86
87 void XdxfDictDownloader::processFinished(int exitcode) {
88     //first check if user cancel downloading
89     if(aborted) return;
90
91     //if error of proces, notify user about this
92     if(exitcode != 0) {
93         Q_EMIT notify(Notify::Error, tr("Error while downloading or processing dictionary"));
94         breakDownloading();
95         return;
96     }
97     //if there are any left commands, execute next
98     if(++currentCommand<commands.size()) {
99         process->start(commands[currentCommand]);
100     }
101     else {
102         qDebug() << "EXTRACT" << extract("/tmp/" + _fileName);
103         downloadComplete();
104     }
105 }
106
107 void XdxfDictDownloader::downloadComplete() {
108     if(aborted) return;
109     // Downloaded tar file name is different than extracted folder so we need
110     // some clean directory to identify extracted files
111     QDir dir("/tmp/mdict");
112     QString dictDirName = dir.entryList().at(2);
113
114     // Dict is in /tmp/mdict/<extracted directory>/dict.xdxf
115     QFile dictFile("/tmp/mdict/" + dictDirName + "/dict.xdxf");
116     dictFile.copy(QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf");
117     QFile::remove("/tmp/" + _fileName);
118     QFile::remove("/tmp/" + _fileName.replace(QRegExp(".bz2$"), ""));
119
120     _downloadedFile = QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf";
121
122     progressDialog->accept();
123     delete progressDialog;
124     progressDialog = 0;
125
126     emit fileDownloaded(_downloadedFile);
127 }
128
129 void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
130
131     if(aborted) return;
132     progressDialog->accept();
133
134     if(reply->error() != QNetworkReply::NoError) {
135         Q_EMIT notify(Notify::Error, reply->errorString());
136         return;
137     }
138
139     QString page(QString::fromUtf8(reply->readAll()));
140
141     // You can look at http://xdxf.revdanica.com/down/, we need to get table
142     // with dictionaries entries following regexp match its begining
143     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>");
144     regOuter.setMinimal(true);
145     if(!regOuter.indexIn(page))
146         return;
147
148     // Cutting each entry and creating coresponded DownloadDict object
149     page = regOuter.capturedTexts().at(1);
150     QRegExp regInner("<tr>.*</tr>");
151     regInner.setMinimal(true);
152     int pos = 0;
153
154     while ((pos = regInner.indexIn(page, pos)) != -1) {
155         DownloadDict temp = DownloadDict(regInner.cap(0));
156         if(!temp.fromLang().isEmpty())
157             dicts.append(temp);
158         pos += regInner.matchedLength();
159     }
160
161     XdxfDictSelectDialog selectDialog(dicts, parentDialog);
162
163     if(selectDialog.exec()==QDialog::Accepted) {
164
165         progressDialog->setText(tr("Downloading dictionary"));
166         progressDialog->show();
167
168         QString url = selectDialog.link();
169
170         _fileName = url.split('/').last();
171
172         // Now its the tricky part ... its temporary (probably)
173         // We dont have any tar-dev and bz2-dev packages on maemo so we need
174         // to call commands via shell, each command from list is called after
175         // previous call returns 0
176
177         currentCommand = 0;
178         commands.clear();
179         commands.push_back("rm -rf /tmp/mdict");
180         commands.push_back("mkdir /tmp/mdict");
181
182         // Downloading xdxf dict from sourceforge is kind of complicated,
183         // there is a lot of redirection and QNetwork* is kind of lost, we
184         // tried to follow redirection (by hand) but we end up with some
185         // page and js scripts and thats all
186         // Maybe calling wget is not pretty one but its working!
187         commands.push_back("wget --quiet -P /tmp/ " + url);
188
189         //commands.push_back(QString("tar -xjvf /tmp/") + _fileName + QString(" -C /tmp/mdict"));
190
191         process->start(commands[0]);
192     }
193 }
194
195 bool XdxfDictDownloader::extract(QString file) {
196     // Extracting bz2
197     FILE * archive = fopen(file.toStdString().c_str(), "rb");
198     if (archive == 0)
199         return false;
200     int err;
201     BZFILE * afterbzFile = BZ2_bzReadOpen(&err, archive, 0, 0, 0, 0);
202     if(err != BZ_OK) {
203         BZ2_bzReadClose(&err, afterbzFile);
204         return false;
205     }
206
207     FILE * tarfile = fopen(file.replace(QRegExp(".bz2$"), "").
208             toStdString().c_str(), "w");
209
210     int bufflen = 100;
211     char buff[bufflen];
212     while(err == BZ_OK) {
213         int len = BZ2_bzRead(&err, afterbzFile, buff, bufflen);
214         if(fwrite(buff, 1, len, tarfile) != len)
215             return false;
216     }
217     BZ2_bzReadClose(&err, afterbzFile);
218     fclose(tarfile);
219     fclose(archive);
220
221     // Extracting tar
222     TAR *t;
223     char * tarfname = new char[file.replace(QRegExp(".bz2%"), "").size()+1];
224     strcpy(tarfname, file.replace(QRegExp(".bz2%"), "").toStdString().c_str());
225
226     err = tar_open(&t, tarfname, 0, O_RDONLY, 0, 0);
227     if(err == -1)
228         return false;
229
230     err = tar_extract_all(t, "/tmp/mdict/");
231     if(err == -1) {
232         return false;
233     }
234     tar_close(t);
235
236     return true;
237 }
238
239