1 /*******************************************************************************
3 This file is part of mDictionary.
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.
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.
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/>.
18 Copyright 2010 Comarch S.A.
20 *******************************************************************************/
23 \file XdxfDictDownloader.cpp
24 \author Mateusz Półrola <mateusz.polrola@comarch.com>
27 #include "XdxfDictDownloader.h"
28 #include "XdxfDictDownloadProgressDialog.h"
32 XdxfDictDownloader::XdxfDictDownloader(QObject *parent) :
35 process = new QProcess(this);
36 manager = new QNetworkAccessManager(this);
38 connect(manager, SIGNAL(finished(QNetworkReply*)),
39 this, SLOT(dictListReceived(QNetworkReply*)));
41 connect(process, SIGNAL(finished(int)),
42 this, SLOT(processFinished(int)));
47 void XdxfDictDownloader::download(QWidget *parent) {
48 parentDialog = parent;
51 manager->get(QNetworkRequest(QUrl("http://xdxf.revdanica.com/down/")));
53 progressDialog = new XdxfDictDownloadProgressDialog(parent);
55 connect(progressDialog, SIGNAL(cancelDownloading()),
56 this, SLOT(breakDownloading()));
57 progressDialog->setText(tr("Downloading dictionaries list"));
58 progressDialog->show();
61 QString XdxfDictDownloader::downloadedFile() {
62 return _downloadedFile;
66 void XdxfDictDownloader::breakDownloading() {
67 //if user cancel downloading we kill all running processes, hide progress dialog and set flag that user cancel downloading.
69 if(process->state() != QProcess::NotRunning) {
73 if(progressDialog && progressDialog->isVisible()) {
74 progressDialog->accept();
79 void XdxfDictDownloader::processFinished(int exitcode) {
80 //first check if user cancel downloading
83 //if error of proces, notify user about this
85 Q_EMIT notify(Notify::Error, tr("Error while downloading or processing dictionary"));
89 //if there are any left commands, execute next
90 if(++currentCommand<commands.size()) {
91 process->start(commands[currentCommand]);
98 void XdxfDictDownloader::downloadComplete() {
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);
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);
110 _downloadedFile = QDir::homePath() + "/.mdictionary/" + dictDirName + ".xdxf";
112 progressDialog->accept();
113 delete progressDialog;
116 emit fileDownloaded(_downloadedFile);
119 void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
122 progressDialog->accept();
124 if(reply->error() != QNetworkReply::NoError) {
125 Q_EMIT notify(Notify::Error, reply->errorString());
129 QString page(QString::fromUtf8(reply->readAll()));
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))
138 // Cutting each entry and creating coresponded DownloadDict object
139 page = regOuter.capturedTexts().at(1);
140 QRegExp regInner("<tr>.*</tr>");
141 regInner.setMinimal(true);
144 while ((pos = regInner.indexIn(page, pos)) != -1) {
145 DownloadDict temp = DownloadDict(regInner.cap(0));
146 if(!temp.fromLang().isEmpty())
148 pos += regInner.matchedLength();
151 XdxfDictSelectDialog selectDialog(dicts, parentDialog);
153 if(selectDialog.exec()==QDialog::Accepted) {
155 progressDialog->setText(tr("Downloading dictionary"));
156 progressDialog->show();
158 QString url = selectDialog.link();
160 _fileName = url.split('/').last();
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
169 commands.push_back("rm -rf /tmp/mdict");
170 commands.push_back("mkdir /tmp/mdict");
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"));
180 process->start(commands[0]);