code clean
[mdictionary] / src / plugins / stardict / StarDialog.cpp
index 9b4b001..baf11d3 100644 (file)
     Copyright 2010 Comarch S.A.
 
 *******************************************************************************/
+/*!
+    \file StarDialog.cpp
+    \brief Implementation of stardict plugin's dialogs.
 
-//Created by Mateusz Półrola
+    \author Mateusz Półrola <mateusz.polrola@gmail.com>
+*/
 
 #include "StarDialog.h"
 #include <QDebug>
 #include <QFile>
 
-StarDialog::StarDialog(StarDictPlugin *plugin,
-                       StarDialogType type,
-                       QWidget *parent) :
-    QDialog(parent) {
-    this->plugin = plugin;
-    this->type = type;
-
-    accentsToolTip = tr("Strip accents (searching takes more time, but spelling doesn't have to be exact)");
-
+StarDialog::StarDialog(StarDictPlugin *plugin, StarDialogType type,
+                       QWidget *parent) : QDialog(parent) {                                        
+   this->plugin = plugin;
+   this->type = type;
+
+#ifndef Q_WS_MAEMO_5
+    view= new QDeclarativeView();
+    view->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/StarDictDialog.qml"));
+    view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
+    view->setAlignment(Qt::AlignCenter);
+    view->show();
+
+    mainLayout = new QVBoxLayout;
+    mainLayout->addWidget(view);
+    setLayout(mainLayout);
+    view->setWindowTitle(tr("StarDict Settings"));
+
+    QGraphicsObject *rootObject = view->rootObject();
+
+    connect(this, SIGNAL(setInfo(QVariant)),
+           rootObject, SLOT(setInfo(QVariant)));
+    connect(this,SIGNAL(setButtonText(QVariant)),
+            rootObject, SLOT(setButtonText(QVariant)));
+    connect(this,SIGNAL(setNew(QVariant)),
+            rootObject, SLOT(setNew(QVariant)));
+    connect(this,SIGNAL(setPath(QVariant)),
+            rootObject, SLOT(setPath(QVariant)));
+
+    connect(rootObject, SIGNAL(saveButtonClicked()),
+            this, SLOT(accept()));
+    connect(rootObject, SIGNAL(browseButtonClicked()),
+            this, SLOT(selectFile()));
+    connect(rootObject, SIGNAL(heightChange(int)),
+            this, SLOT(heightChange(int)));
+
+#else
+   if(type == New)
+       connect(browseButton, SIGNAL(clicked()),this, SLOT(selectFile()));
+   connect(confirmButton, SIGNAL(clicked()),this, SLOT(accept()));
+#endif
     initializeUI();
+}
 
+void StarDialog::heightChange(int height){
+    if(height>sizeHint().height())
+        resize(sizeHint().width(),height);
+    lastHeight=height;
+}
 
-    connect(accentsCheckBox, SIGNAL(toggled(bool)),
-            this, SLOT(setAccents(bool)));
-
-    #ifdef Q_WS_MAEMO_5
-        connect(accentsInfoToolButton, SIGNAL(clicked()),
-
-    #endif
-
-    if(type == New) {
-        connect(browseButton, SIGNAL(clicked()),
-                this, SLOT(selectFile()));
+void StarDialog::resizeEvent(QResizeEvent *e){
+    QDialog::resizeEvent(e);
+    if(lastHeight>sizeHint().height()){
+        resize(sizeHint().width(),lastHeight);
     }
-
-    connect(confirmButton, SIGNAL(clicked()),
-            this, SLOT(accept()));
-
 }
 
-
 void StarDialog::initializeUI() {
+#ifndef Q_WS_MAEMO_5
+    if(type != New){
+        emit setNew(false);
+        QString info=tr("Plugin: ") + plugin->type().toUpper() +"\n" +
+            tr("Book name: ") + plugin->settings()->value("bookname") + "\n" +
+            tr("Version: ") + plugin->settings()->value("version") + "\n" +
+            tr("Word count: ") + plugin->settings()->value("wordcount") + "\n" +
+            tr("Author: ") + plugin->settings()->value("author") + "\n" +
+            tr("E-mail: ") + plugin->settings()->value("email") + "\n" +
+            tr("Website: ") + plugin->settings()->value("website") + "\n" +
+            tr("Description: ") + plugin->settings()->value("description")+"\n"+
+            tr("Date: ") + plugin->settings()->value("date");
+        emit setInfo(info);
+        emit setButtonText("Save settings");
+    }
+    else{
+        emit setNew(true);
+        emit setButtonText("Add");
+    }
+
+    if(!plugin)
+        _dictionaryFilePath = "";
+
+#else
     mainVerticalLayout = new QVBoxLayout;
     widget = new QWidget;
     widget->setLayout(mainVerticalLayout);
-
     infoLabel = new QLabel;
     infoLabel->setWordWrap(true);
+    QVBoxLayout* buttonLayout = new QVBoxLayout;
 
     if(type == New) {
-        setWindowTitle(tr("Add new StarDict dictionary"));
-
         browseLayout = new QHBoxLayout;
         browseButton = new QPushButton(tr("Browse"));
+        browseButton->setMaximumWidth(150);
         infoLabel->setText(tr("Dictionary file: not selected"));
-
+        setWindowTitle(tr("Add new StarDict dictionary"));
         browseLayout->addWidget(infoLabel, 0, Qt::AlignLeft);
+        browseLayout->addLayout(buttonLayout);
         browseLayout->addWidget(browseButton, 0, Qt::AlignRight);
-
         mainVerticalLayout->addLayout(browseLayout);
     }
     else {
         setWindowTitle(tr("StarDict Settings"));
-
         infoLabel->setText(tr("Plugin: ") + plugin->type().toUpper() +"\n" +
-                       tr("From: ") + plugin->langFrom() + "\n" +
-                       tr("To: ") + plugin->langTo() + "\n" +
-                       tr("Description: ") + plugin->name() + "\n" +
-                       plugin->infoNote());
+                tr("Book name: ") + plugin->settings()->value("bookname") 
+                        + "\n" +
+                tr("Version: ") + plugin->settings()->value("version") + "\n" +
+                tr("Word count: ") + plugin->settings()->value("wordcount") 
+                        + "\n" +
+                tr("Author: ") + plugin->settings()->value("author") + "\n" +
+                tr("E-mail: ") + plugin->settings()->value("email") + "\n" +
+                tr("Website: ") + plugin->settings()->value("website") + "\n" +
+                tr("Description: ") + plugin->settings()->value("description") 
+                        + "\n" +
+               tr("Date: ") + plugin->settings()->value("date"));
         mainVerticalLayout->addWidget(infoLabel);
     }
 
-    accentsLayout = new QHBoxLayout;
-    accentsCheckBox = new QCheckBox(tr("Strip accents"));
-    accentsCheckBox->setToolTip(accentsToolTip);
-    accentsLayout->addWidget(accentsCheckBox); 
-    #ifdef Q_WS_MAEMO_5
-        accentsInfoToolButton = new QToolButton;
-        accentsInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
-        accentsLayout->addWidget(accentsInfoToolButton);
-    #endif
-
-
-    mainVerticalLayout->addLayout(accentsLayout);
-
-
-    //load old setting if exists
-    if(!plugin) {
-        accentsCheckBox->setChecked(true);
-        accentsCheckBox->setEnabled(false);
-        _accents = true;
+    if(!plugin)
         _dictionaryFilePath = "";
-    }
-    else if(plugin && plugin->settings()->value("cached") == "true") {
-        accentsCheckBox->setChecked(true);
-        accentsCheckBox->setEnabled(false);
-        _accents = true;
-    }
-    else {
-        if(plugin->settings()->value("strip_accents") == "true") {
-            accentsCheckBox->setChecked(true);
-            _accents = true;
-        }
-        else {
-            accentsCheckBox->setChecked(false);
-            _accents = false;
-        }
-    }
 
     confirmButton = new QPushButton;
     mainVerticalLayout->addWidget(confirmButton);
-    if(type == New) {
+    if(type == New)
         confirmButton->setText(tr("Add"));
-    }
-    else {
+    else
         confirmButton->setText(tr("Save settings"));
-    }
 
     scrollArea = new QScrollArea;
     scrollArea->setWidget(widget);
@@ -141,8 +163,8 @@ void StarDialog::initializeUI() {
     #else
         if(type==New) {
             infoLabel->setMinimumWidth(200);
-            setMinimumSize(sizeHint().width()*1.5, sizeHint().height()*1.2);
-            setMaximumSize(sizeHint().width()*1.7, sizeHint().height()*1.5);
+//            setMinimumSize(sizeHint().width()*1.5, sizeHint().height()*1.2);
+//            setMaximumSize(sizeHint().width()*1.7, sizeHint().height()*1.5);
             scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
         }
     #endif
@@ -160,62 +182,69 @@ void StarDialog::initializeUI() {
     scrollArea->setLineWidth(0);
     scrollArea->setMidLineWidth(0);
     scrollArea->setFrameStyle(QFrame::NoFrame);
-
-
-}
-
-
-void StarDialog::setAccents(bool accents) {
-    _accents = accents;
+#endif
 }
 
 
 void StarDialog::selectFile() {
     QString fileName = QFileDialog::getOpenFileName(this,
-                                     tr("Select dictionary file"),
-                                     _dictionaryFilePath,
-                                     tr("StarDict Files (*.dict *dict.dz *.idx *idx.gz *.ifo)"),
-                                     NULL,
-                                     NULL);
-
+                     tr("Select dictionary file"),
+                     _dictionaryFilePath,
+                     tr("StarDict Files (*.dict *dict.dz *.idx *idx.gz *.ifo)"),
+                     NULL,
+                     NULL);
     if (!fileName.isEmpty()) {
+#ifndef Q_WS_MAEMO_5
+        emit setPath(tr("Dictionary file: %1").arg(fileName));
+#else
         infoLabel->setText(tr("Dictionary file: %1").arg(fileName));
+#endif
         _dictionaryFilePath = fileName;
-        if (_dictionaryFilePath.endsWith(".tar.bz2")){
-                _isCompressed = true;
-        }
-        else {
-                _isCompressed = false;
-        }
+        if (_dictionaryFilePath.endsWith(".tar.bz2"))
+            _isCompressed = true;
+        else
+            _isCompressed = false;
+#ifndef Q_WS_MAEMO_5
+        resize(view->sizeHint());
+
+#else
         updateGeometry();
+#endif
     }
 }
 
+
 void StarDialog::saveSettings() {
+
     _settings = new Settings;
-    if(plugin) {
+    if(plugin)
         foreach(QString key, plugin->settings()->keys())
             _settings->setValue(key, plugin->settings()->value(key));
-    }
-    //else {
+
+    if(_settings->value("path")=="")
         _settings->setValue("path", _dictionaryFilePath);
+
+    if(_settings->value("ifoFileName")=="")
         _settings->setValue("ifoFileName", _dictName + ".ifo");
+
+    if(_settings->value("idxFileName")=="")
         _settings->setValue("idxFileName", _dictName + ".idx");
-        _settings->setValue("dictFileName", _dictName + ".dict");
-        if (QFile::exists(_dictName + ".syn") == true) {
-                _settings->setValue("synFileName", _dictName + ".syn");
-        }
-    //}
-    if(_accents)
-        _settings->setValue("strip_accents", "true");
-    else
-        _settings->setValue("strip_accents", "false");
+
+    if(_settings->value("dictFileName")==""){
+        if (QFile::exists(_dictName + ".dict.dz") == true)
+            _settings->setValue("dictFileName", _dictName + ".dict.dz");
+        else
+            _settings->setValue("dictFileName", _dictName + ".dict");
+    }
+    if(_settings->value("synFileName")=="")
+        if (QFile::exists(_dictName + ".syn") == true)
+            _settings->setValue("synFileName", _dictName + ".syn");
 }
 
+
 void StarDialog::accept() {
     if(type == New && _dictionaryFilePath.isEmpty()) {
         Q_EMIT notify(Notify::Warning, tr("File path is not set"));
-
         return;
     }
 
@@ -228,30 +257,33 @@ void StarDialog::accept() {
     QDialog::accept();
 }
 
+
 bool StarDialog::checkFiles() {
-        if (!_isCompressed){
-               _dictName = _dictionaryFilePath.left(_dictionaryFilePath.lastIndexOf("."));
-               if (QFile::exists(_dictName + ".idx") == false && QFile::exists(_dictName + ".idx.gz") == false) {
-                       return false;
-               }
-               if (QFile::exists(_dictName + ".dict") == false && QFile::exists(_dictName + ".dict.dz") == false) {
-                       return false;
-               }
-               return true;
+    if (!_isCompressed) {
+        if (_dictionaryFilePath.right(2) == "dz") {
+            _dictName = _dictionaryFilePath.left(_dictionaryFilePath.lastIndexOf("."));
+            _dictName = _dictName.left(_dictName.lastIndexOf("."));
         }
-        else {
-                //TODO: untar files (?)
-                return false;
+        else{
+            _dictName = _dictionaryFilePath.left(_dictionaryFilePath.lastIndexOf("."));
         }
+
+        if (QFile::exists(_dictName + ".idx") == false
+                && QFile::exists(_dictName + ".idx.gz") == false) {
+            return false;
+        }
+        if (QFile::exists(_dictName + ".dict") == false
+                && QFile::exists(_dictName + ".dict.dz") == false) {
+            return false;
+        }
+        return true;
+    }
+    else
+        return false;
 }
 
+
 Settings* StarDialog::getSettings() {
     return _settings;
 }
 
-#ifdef Q_WS_MAEMO_5
-    void StarDialog::showAccentsInfo() {
-        Q_EMIT notify(Notify::Warning, accentsToolTip);
-    }
-#endif
-