*******************************************************************************/
-//Created by Mateusz Półrola
+/*!
+ \file DictsModel.cpp
+ \author Mateusz Półrola <mateusz.polrola@comarch.pl>
+*/
-#include "DictsModel.h"
DictsModel::DictsModel(QList<DownloadDict> dicts, QObject *parent) :
QAbstractItemModel(parent)
}
-int DictsModel::rowCount(const QModelIndex &parent) const {
+int DictsModel::rowCount(const QModelIndex &) const {
return dicts.count();
}
-int DictsModel::columnCount(const QModelIndex &parent) const {
+int DictsModel::columnCount(const QModelIndex &) const {
return 4;
}
case 2:
return dicts.at(index.row()).title();
case 3:
- return dicts.at(index.row()).size();
+ return QString::number(dicts.at(index.row()).size(), 'g', 2) +
+ QString (" MB");
}
}
return QVariant();
}
-QModelIndex DictsModel::index(int row, int column, const QModelIndex &parent) const {
+QModelIndex DictsModel::index(int row, int column, const QModelIndex &) const {
return createIndex(row, column, row);
}
-QModelIndex DictsModel::parent(const QModelIndex &child) const {
+QModelIndex DictsModel::parent(const QModelIndex &) const {
return QModelIndex();
}
-
-void DictsModel::sort(int column, Qt::SortOrder order) {
-
-}
*******************************************************************************/
-//Created by Mateusz Półrola
+/*!
+ \file DictsModel.h
+ \author Mateusz Półrola <mateusz.polrola@comarch.pl>
+*/
#ifndef DICTSMODEL_H
#define DICTSMODEL_H
#include <QAbstractItemModel>
#include "DownloadDict.h"
+
+/*!
+ Item model for informations about xdxf dictionaries.
+ Contains informations about languages of dictionary, it's name, size and
+ download link. Download link is stored as UserRole, and not displayed in views
+ using this model.
+*/
class DictsModel : public QAbstractItemModel
{
Q_OBJECT
public:
+ /*!
+ Constructor
+ \param dicts list of DownloadDict objects, each such object describes
+ one dictionary
+ */
DictsModel(QList<DownloadDict> dicts, QObject *parent);
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
- void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
-
private:
QList<DownloadDict> dicts;
};
QString from() { return _from;}
QString to() {return _to;}
- void setFrom(QString from) { _from = from; invalidateFilter(); qDebug()<<"asas";}
+ void setFrom(QString from) { _from = from; invalidateFilter(); }
void setTo(QString to) {_to = to; invalidateFilter();}
protected:
(_to.isEmpty() || sourceTo == _to));
}
+ bool lessThan(const QModelIndex &left, const QModelIndex &right) const {
+ if(sortColumn() == 3) {
+ QString l = left.model()->data(left).toString();
+ l.remove(" MB");
+
+ QString r = right.model()->data(right).toString();
+ r.remove(" MB");
+
+ float lNumber = l.toFloat();
+ float rNumber = r.toFloat();
+
+ return (lNumber < rNumber);
+ }
+ else
+ QSortFilterProxyModel::lessThan(left, right);
+ }
+
private:
QString _from;
QString _to;
#include <QRegExp>
#include <QStringList>
+#include <math.h>
class DownloadDict
{
_from = tmp.at(6);
_to = tmp.at(7);
_title = tmp.at(1);
- _size = tmp.at(3);
+ QString sizeStr = tmp.at(3);
+
+ _size = sizeStr.remove(',').toInt();
+
+ _size = _size / 1024 / 1024;
+
+ _size = round(_size*1000) / 1000;
+
QRegExp lreg("href=\"(.*)\"");
lreg.setMinimal(true);
lreg.indexIn(tmp.at(2));
QString fromLang() const {return _from;}
QString toLang() const {return _to;}
QString title() const {return _title;}
- QString size() const {return _size;}
+ float size() const {return _size;}
QString link() const {return _link;}
bool operator <(DownloadDict other) const {
}
private:
- QString _from, _to, _title, _link, _size;
+ QString _from, _to, _title, _link;
+ float _size;
};
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
#else
if(type==New) {
- infoLabel->setMinimumWidth(200);
+ infoLabel->setMinimumWidth(250);
setMinimumSize(sizeHint().width()*1.5, sizeHint().height()*1.2);
setMaximumSize(sizeHint().width()*1.7, sizeHint().height()*1.5);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setLayout(layout);
#ifndef Q_WS_MAEMO_5
- setMinimumSize(400,200);
+ setMinimumSize(450,200);
#else
setMinimumHeight(350);
#endif
*******************************************************************************/
/*! \file XdxfDictDialog.h
*/
-//Created by Mateusz Półrola
+
#ifndef XDXFDICTDIALOG_H
#define XDXFDICTDIALOG_H
#include "XdxfDictDownloadProgressDialog.h"
-XdxfDictDownloadProgressDialog::XdxfDictDownloadProgressDialog(QString title, QWidget*parent):
+XdxfDictDownloadProgressDialog::XdxfDictDownloadProgressDialog(QWidget*parent):
QDialog(parent) {
verticalLayout = new QVBoxLayout(this);
setLayout(verticalLayout);
- setWindowTitle(title);
downloadProgressBar = new QProgressBar(this);
downloadProgressBar->setMinimum(0);
downloadProgressBar->setMaximum(0);
downloadProgressBar->setTextVisible(true);
- // cancelButton = new QPushButton(tr("Cancel"),this);
+ cancelButton = new QPushButton(tr("Cancel"),this);
+
+ connect(cancelButton, SIGNAL(clicked()),
+ this, SIGNAL(cancelDownloading()));
#ifndef Q_WS_MAEMO_5
downloadLabel = new QLabel(this);
- downloadLabel->setText(title);
verticalLayout->addWidget(downloadLabel, 0, Qt::AlignCenter);
#endif
verticalLayout->addWidget(downloadProgressBar);
- //erticalLayout->addWidget(cancelButton);
+ verticalLayout->addWidget(cancelButton);
setModal(true);
+
+ #ifndef Q_WS_MAEMO_5
+ setMinimumWidth(350);
+ #endif
}
+void XdxfDictDownloadProgressDialog::setText(QString text) {
+ setWindowTitle(text);
+ #ifndef Q_WS_MAEMO_5
+ downloadLabel->setText(text);
+ #endif
+}
+
void XdxfDictDownloadProgressDialog::reject() {
return;
}
class XdxfDictDownloadProgressDialog : public QDialog {
Q_OBJECT
public:
- XdxfDictDownloadProgressDialog(QString title, QWidget *parent = 0);
+ XdxfDictDownloadProgressDialog(QWidget *parent = 0);
public Q_SLOTS:
void reject();
+ void setText(QString);
Q_SIGNALS:
//! signal emitted when user cancels downloading of a dictionary
parentDialog = 0;
process = new QProcess(this);
+
connect(process, SIGNAL(finished(int)),
this, SLOT(processFinished(int)));
+
+ progressDialog = 0;
+}
+
+XdxfDictDownloader::~XdxfDictDownloader() {
+
}
void XdxfDictDownloader::download(QWidget *parent) {
parentDialog = parent;
+ aborted = false;
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),
manager->get(QNetworkRequest(QUrl("http://xdxf.revdanica.com/down/")));
- progressDialog = new XdxfDictDownloadProgressDialog(tr("Downloading dictionaries list"), parent);
+ progressDialog = new XdxfDictDownloadProgressDialog(parent);
+
+ connect(progressDialog, SIGNAL(cancelDownloading()),
+ this, SLOT(breakDownloading()));
+ progressDialog->setText(tr("Downloading dictionaries list"));
progressDialog->show();
}
return _downloadedFile;
}
+
+void XdxfDictDownloader::breakDownloading() {
+ aborted = true;
+ if(process->state() != QProcess::NotRunning) {
+ process->kill();
+ }
+
+ if(progressDialog && progressDialog->isVisible()) {
+ progressDialog->accept();
+ }
+
+ Q_EMIT notify(Notify::Info, tr("Downloading canceled"));
+}
+
void XdxfDictDownloader::processFinished(int exitcode) {
+ if(aborted) return;
+ if(exitcode != 0) {
+ Q_EMIT notify(Notify::Error, tr("Error while downloading or processing dictionary"));
+ }
if(++currentCommand<commands.size()) {
process->start(commands[currentCommand]);
}
}
void XdxfDictDownloader::downloadComplete() {
+ if(aborted) return;
QDir dir("/tmp/mdict");
QString dictDirName = dir.entryList().at(2);
QFile dictFile("/tmp/mdict/" + dictDirName + "/dict.xdxf");
progressDialog->accept();
delete progressDialog;
+ progressDialog = 0;
+
emit fileDownloaded(_downloadedFile);
}
void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
+ if(aborted) return;
progressDialog->accept();
- delete progressDialog;
+
+ if(reply->error() != QNetworkReply::NoError) {
+ Q_EMIT notify(Notify::Error, reply->errorString());
+ return;
+ }
+
QString page(QString::fromUtf8(reply->readAll()));
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>");
regOuter.setMinimal(true);
QRegExp regInner("<tr>.*</tr>");
regInner.setMinimal(true);
int pos = 0;
- QStringList tmp;
+
while ((pos = regInner.indexIn(page, pos)) != -1) {
- dicts.append(DownloadDict(regInner.cap(0)));
+ DownloadDict temp = DownloadDict(regInner.cap(0));
+ if(!temp.fromLang().isEmpty())
+ dicts.append(temp);
pos += regInner.matchedLength();
}
if(selectDialog.exec()==QDialog::Accepted) {
- progressDialog = new XdxfDictDownloadProgressDialog(tr("Downloading dictionary"), parentDialog);
+ progressDialog->setText(tr("Downloading dictionary"));
progressDialog->show();
+
QString url = selectDialog.link();
_fileName = url.split('/').last();
#include <QNetworkReply>
#include <DownloadDict.h>
#include "XdxfDictDownloadProgressDialog.h"
+#include "../../include/Notify.h"
class XdxfDictDownloader : public QObject {
Q_OBJECT
public:
XdxfDictDownloader(QObject *parent = 0);
QString downloadedFile();
+ ~XdxfDictDownloader();
public Q_SLOTS:
void download(QWidget* parent);
Q_SIGNALS:
void fileDownloaded(QString);
+ void notify(Notify::NotifyType, QString);
private Q_SLOTS:
void dictListReceived(QNetworkReply*);
void processFinished(int);
+ void breakDownloading();
private:
void downloadComplete();
int currentCommand;
QProcess* process;
QString _fileName;
+ bool downloadError;
+ bool aborted;
};
#endif // XDXFDICTDOWNLOADER_H
QWidget *parent) :
QDialog(parent) {
+
+ setWindowTitle(tr("Select dictionary"));
+
layout = new QVBoxLayout;
setLayout(layout);
langFrom->setInsertPolicy(QComboBox::InsertAlphabetically);
langTo->setInsertPolicy(QComboBox::InsertAlphabetically);
- checkBoxLayout->addWidget(langFrom);
- checkBoxLayout->addWidget(langTo);
+ langFromLabel = new QLabel(tr("From "));
+ langToLabel = new QLabel(tr("To "));
+
+ checkBoxLayout->addWidget(langFromLabel);
+ checkBoxLayout->addWidget(langFrom, 10);
+ checkBoxLayout->addWidget(langToLabel);
+ checkBoxLayout->addWidget(langTo, 10);
model = new DictsModel(dicts, this);
treeView->setSortingEnabled(true);
treeView->sortByColumn(0, Qt::AscendingOrder);
- treeView->resizeColumnToContents(0);
- treeView->resizeColumnToContents(1);
- treeView->resizeColumnToContents(2);
- treeView->resizeColumnToContents(3);
+ treeView->setWordWrap(true);
+
+ #ifndef Q_WS_MAEMO_5
+ treeView->resizeColumnToContents(0);
+ treeView->resizeColumnToContents(1);
+ treeView->setColumnWidth(2, 300);
+ treeView->resizeColumnToContents(3);
+ #else
+ treeView->setColumnWidth(0, 150);
+ treeView->setColumnWidth(1, 150);
+ treeView->setColumnWidth(2, 300);
+ treeView->setColumnWidth(3, 150);
+ #endif
- // treeView->sortByColumn(0);
layout->addWidget(treeView);
this, SLOT(itemClicked(QModelIndex)));
#ifndef Q_WS_MAEMO_5
- setMinimumSize(400,200);
+ setMinimumSize(800,500);
#else
setMinimumHeight(350);
#endif
QHBoxLayout* checkBoxLayout;
QComboBox* langFrom;
QComboBox* langTo;
+ QLabel* langFromLabel;
+ QLabel* langToLabel;
private Q_SLOTS:
void initializeDicts();
XdxfDictDownloader XdxfPlugin::dictDownloader;
+bool XdxfPlugin::dictDownloaderInitialized = false;
+
+
XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
_langFrom(""), _langTo(""),_name(""), _infoNote("") {
this, SIGNAL(notify(Notify::NotifyType,QString)));
+ if(!dictDownloaderInitialized) {
+ connect(&dictDownloader, SIGNAL(notify(Notify::NotifyType,QString)),
+ this, SIGNAL(notify(Notify::NotifyType,QString)));
+ dictDownloaderInitialized = true;
+ }
+
+
_settings->setValue("type","xdxf");
_icon = QIcon("/usr/share/mdictionary/xdxf.png");
_wordsCount = -1;
private:
+ static bool dictDownloaderInitialized;
+
+
/*! \returns true or false depending on whether the dictionary is cached
or not
*/