Added notifications and canceling to xdxf download dialog
authorMateusz Półrola <mateusz.polrola@comarch.pl>
Fri, 1 Oct 2010 08:42:51 +0000 (10:42 +0200)
committerMateusz Półrola <mateusz.polrola@comarch.pl>
Fri, 1 Oct 2010 08:42:51 +0000 (10:42 +0200)
14 files changed:
src/plugins/xdxf/DictsModel.cpp
src/plugins/xdxf/DictsModel.h
src/plugins/xdxf/DictsProxyModel.h
src/plugins/xdxf/DownloadDict.h
src/plugins/xdxf/XdxfDialog.cpp
src/plugins/xdxf/XdxfDictDialog.h
src/plugins/xdxf/XdxfDictDownloadProgressDialog.cpp
src/plugins/xdxf/XdxfDictDownloadProgressDialog.h
src/plugins/xdxf/XdxfDictDownloader.cpp
src/plugins/xdxf/XdxfDictDownloader.h
src/plugins/xdxf/XdxfDictSelectDialog.cpp
src/plugins/xdxf/XdxfDictSelectDialog.h
src/plugins/xdxf/xdxfplugin.cpp
src/plugins/xdxf/xdxfplugin.h

index 7947874..c92ed57 100644 (file)
 
 *******************************************************************************/
 
-//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)
@@ -32,11 +34,11 @@ DictsModel::DictsModel(QList<DownloadDict> dicts, QObject *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;
 }
 
@@ -57,7 +59,8 @@ QVariant DictsModel::data(const QModelIndex &index, int role) const
         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");
         }
     }
 
@@ -88,14 +91,10 @@ QVariant DictsModel::headerData(int section, Qt::Orientation orientation,
     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) {
-
-}
index 69711ba..ad83848 100644 (file)
 
 *******************************************************************************/
 
-//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;
@@ -43,8 +58,6 @@ public:
     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;
 };
index 4192f87..79804a7 100644 (file)
@@ -38,7 +38,7 @@ public:
     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:
@@ -52,6 +52,23 @@ 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;
index c56a769..f565f96 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <QRegExp>
 #include <QStringList>
+#include <math.h>
 
 class DownloadDict
 {
@@ -19,7 +20,14 @@ public:
         _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));
@@ -29,7 +37,7 @@ public:
     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 {
@@ -40,7 +48,8 @@ public:
     }
 
 private:
-    QString _from, _to, _title, _link, _size;
+    QString _from, _to, _title, _link;
+    float _size;
 };
 
 
index da304b7..e660d8b 100644 (file)
@@ -185,7 +185,7 @@ void XdxfDialog::initializeUI() {
         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);
@@ -197,7 +197,7 @@ void XdxfDialog::initializeUI() {
     setLayout(layout);
 
     #ifndef Q_WS_MAEMO_5
-        setMinimumSize(400,200);
+        setMinimumSize(450,200);
     #else
         setMinimumHeight(350);
     #endif
index 50b8ac3..c34f211 100644 (file)
@@ -20,7 +20,7 @@
 *******************************************************************************/
 /*! \file XdxfDictDialog.h
 */
-//Created by Mateusz Półrola
+
 
 #ifndef XDXFDICTDIALOG_H
 #define XDXFDICTDIALOG_H
index f4962ad..8a34fce 100644 (file)
 
 #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;
 }
index 05c10e6..18be373 100644 (file)
 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
index d5ce72b..3a916cf 100644 (file)
@@ -31,14 +31,22 @@ XdxfDictDownloader::XdxfDictDownloader(QObject *parent) :
     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*)),
@@ -46,7 +54,11 @@ void XdxfDictDownloader::download(QWidget *parent) {
 
     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();
 }
 
@@ -54,7 +66,25 @@ QString XdxfDictDownloader::downloadedFile() {
     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]);
     }
@@ -64,6 +94,7 @@ void XdxfDictDownloader::processFinished(int exitcode) {
 }
 
 void XdxfDictDownloader::downloadComplete() {
+    if(aborted) return;
     QDir dir("/tmp/mdict");
     QString dictDirName = dir.entryList().at(2);
     QFile dictFile("/tmp/mdict/" + dictDirName + "/dict.xdxf");
@@ -74,13 +105,21 @@ void XdxfDictDownloader::downloadComplete() {
 
     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);
@@ -91,9 +130,11 @@ void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
     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();
     }
 
@@ -101,8 +142,9 @@ void XdxfDictDownloader::dictListReceived(QNetworkReply *reply) {
 
     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();
index 3f16509..fbb10c0 100644 (file)
 #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();
@@ -58,6 +62,8 @@ private:
     int currentCommand;
     QProcess* process;
     QString _fileName;
+    bool downloadError;
+    bool aborted;
 };
 
 #endif // XDXFDICTDOWNLOADER_H
index 9fc1968..db6505c 100644 (file)
@@ -27,6 +27,9 @@ XdxfDictSelectDialog::XdxfDictSelectDialog(QList<DownloadDict> dicts,
                                            QWidget *parent) :
     QDialog(parent) {
 
+
+    setWindowTitle(tr("Select dictionary"));
+
     layout = new QVBoxLayout;
     setLayout(layout);
 
@@ -39,8 +42,13 @@ XdxfDictSelectDialog::XdxfDictSelectDialog(QList<DownloadDict> dicts,
     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);
 
@@ -57,12 +65,20 @@ XdxfDictSelectDialog::XdxfDictSelectDialog(QList<DownloadDict> dicts,
     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);
 
@@ -77,7 +93,7 @@ XdxfDictSelectDialog::XdxfDictSelectDialog(QList<DownloadDict> dicts,
             this, SLOT(itemClicked(QModelIndex)));
 
     #ifndef Q_WS_MAEMO_5
-        setMinimumSize(400,200);
+        setMinimumSize(800,500);
     #else
         setMinimumHeight(350);
     #endif
index 2392351..5f6dfcc 100644 (file)
@@ -45,6 +45,8 @@ private:
     QHBoxLayout* checkBoxLayout;
     QComboBox* langFrom;
     QComboBox* langTo;
+    QLabel* langFromLabel;
+    QLabel* langToLabel;
 
 private Q_SLOTS:
     void initializeDicts();
index 05d1e91..6a79bb1 100644 (file)
@@ -31,6 +31,9 @@
 
 XdxfDictDownloader XdxfPlugin::dictDownloader;
 
+bool XdxfPlugin::dictDownloaderInitialized = false;
+
+
 XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
                     _langFrom(""), _langTo(""),_name(""), _infoNote("") {
 
@@ -45,6 +48,13 @@ XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
             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;
index 38ab6e6..ebdcf8d 100644 (file)
@@ -128,6 +128,9 @@ Q_SIGNALS:
 
 private:
 
+    static bool dictDownloaderInitialized;
+
+
     /*! \returns true or false depending on whether the dictionary is cached
         or not
      */