Add comments and translations for xdxf downloading dialog
[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
32 XdxfDictDownloader::XdxfDictDownloader(QObject *parent) :
33     QObject(parent) {
34     parentDialog = 0;
35     process = new QProcess(this);
36     manager = new QNetworkAccessManager(this);
37
38     connect(manager, SIGNAL(finished(QNetworkReply*)),
39             this, SLOT(dictListReceived(QNetworkReply*)));
40
41     connect(process, SIGNAL(finished(int)),
42             this, SLOT(processFinished(int)));
43
44     progressDialog = 0;
45 }
46
47 void XdxfDictDownloader::download(QWidget *parent) {
48     parentDialog = parent;
49     aborted = false;
50
51     manager->get(QNetworkRequest(QUrl("http://xdxf.revdanica.com/down/")));
52
53     progressDialog = new XdxfDictDownloadProgressDialog(parent);
54
55     connect(progressDialog, SIGNAL(cancelDownloading()),
56             this, SLOT(breakDownloading()));
57     progressDialog->setText(tr("Downloading dictionaries list"));
58     progressDialog->show();
59 }
60
61 QString XdxfDictDownloader::downloadedFile() {
62     return _downloadedFile;
63 }
64
65
66 void XdxfDictDownloader::breakDownloading() {
67     //if user cancel downloading we kill all running processes, hide progress dialog and set flag that user cancel downloading.
68     aborted = true;
69     if(process->state() != QProcess::NotRunning) {
70         process->kill();
71     }
72
73     if(progressDialog && progressDialog->isVisible()) {
74         progressDialog->accept();
75     }
76
77 }
78
79 void XdxfDictDownloader::processFinished(int exitcode) {
80     //first check if user cancel downloading
81     if(aborted) return;
82
83     //if error of proces, notify user about this
84     if(exitcode != 0) {
85         Q_EMIT notify(Notify::Error, tr("Error while downloading or processing dictionary"));
86         breakDownloading();
87         return;
88     }
89     //if there are any left commands, execute next
90     if(++currentCommand<commands.size()) {
91         process->start(commands[currentCommand]);
92     }
93     else {
94         downloadComplete();
95     }
96 }
97
98 void XdxfDictDownloader::downloadComplete() {
99     if(aborted) return;
100     // Downloaded tar file name is different than extracted folder so we need
101     // some clean directory to identify extracted files
102     QDir dir("/tmp/mdict");
103     QString dictDirName = dir.entryList().at(2);
104
105     // Dict is in /tmp/mdict/<extracted directory>/dict.xdxf
106     QFile dictFile("/tmp/mdict/" + dictDirName + "/dict.xdxf");
107     dictFile.copy(QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf");
108     QFile::remove("/tmp/" + _fileName);
109
110     _downloadedFile = QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf";
111
112     progressDialog->accept();
113     delete progressDialog;
114     progressDialog = 0;
115
116     emit fileDownloaded(_downloadedFile);
117 }
118
119 void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
120
121     if(aborted) return;
122     progressDialog->accept();
123
124     if(reply->error() != QNetworkReply::NoError) {
125         Q_EMIT notify(Notify::Error, reply->errorString());
126         return;
127     }
128
129     QString page(QString::fromUtf8(reply->readAll()));
130
131     // You can look at http://xdxf.revdanica.com/down/, we need to get table
132     // with dictionaries entries following regexp match its begining
133     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>");
134     regOuter.setMinimal(true);
135     if(!regOuter.indexIn(page))
136         return;
137
138     // Cutting each entry and creating coresponded DownloadDict object
139     page = regOuter.capturedTexts().at(1);
140     QRegExp regInner("<tr>.*</tr>");
141     regInner.setMinimal(true);
142     int pos = 0;
143
144     while ((pos = regInner.indexIn(page, pos)) != -1) {
145         DownloadDict temp = DownloadDict(regInner.cap(0));
146         if(!temp.fromLang().isEmpty())
147             dicts.append(temp);
148         pos += regInner.matchedLength();
149     }
150
151     XdxfDictSelectDialog selectDialog(dicts, parentDialog);
152
153     if(selectDialog.exec()==QDialog::Accepted) {
154
155         progressDialog->setText(tr("Downloading dictionary"));
156         progressDialog->show();
157
158         QString url = selectDialog.link();
159
160         _fileName = url.split('/').last();
161
162         // Now its the tricky part ... its temporary (probably)
163         // We dont have any tar-dev and bz2-dev packages on maemo so we need
164         // to call commands via shell, each command from list is called after
165         // previous call returns 0
166
167         currentCommand = 0;
168         commands.clear();
169         commands.push_back("rm -rf /tmp/mdict");
170         commands.push_back("mkdir /tmp/mdict");
171
172         // Downloading xdxf dict from sourceforge is kind of complicated,
173         // there is a lot of redirection and QNetwork* is kind of lost, we
174         // tried to follow redirection (by hand) but we end up with some
175         // page and js scripts and thats all
176         // Maybe calling wget is not pretty one but its working!
177         commands.push_back("wget --quiet -P /tmp/ " + url);
178         commands.push_back(QString("tar -xjvf /tmp/") + _fileName + QString(" -C /tmp/mdict"));
179
180         process->start(commands[0]);
181     }
182 }