Dialog box with settings for StarDict.
[mdictionary] / src / plugins / stardict / StarDialog.cpp
index e4ca1bf..9d32f9b 100644 (file)
 
 //Created by Mateusz Półrola
 
-#include "XdxfDialog.h"
+#include "StarDialog.h"
 #include <QDebug>
+#include <QFile>
 
-XdxfDialog::XdxfDialog(XdxfPlugin *plugin,
-                       XdxfDialogType type,
+StarDialog::StarDialog(StarDictPlugin *plugin,
+                       StarDialogType type,
                        QWidget *parent) :
     QDialog(parent) {
     this->plugin = plugin;
     this->type = type;
 
-
-    cacheToolTip = tr("Optimize for quicker searches (may take some time)");
     accentsToolTip = tr("Strip accents (searching takes more time, but spelling doesn't have to be exact)");
 
     initializeUI();
 
-    connect(cacheCheckBox, SIGNAL(toggled(bool)),
-            this, SLOT(setGenerateCache(bool)));
 
     connect(accentsCheckBox, SIGNAL(toggled(bool)),
             this, SLOT(setAccents(bool)));
 
     #ifdef Q_WS_MAEMO_5
         connect(accentsInfoToolButton, SIGNAL(clicked()),
-                 this, SLOT(showAccentsInfo()));
 
-        connect(cacheInfoToolButton, SIGNAL(clicked()),
-                 this, SLOT(showCacheInfo()));
     #endif
 
     if(type == New) {
@@ -62,7 +56,7 @@ XdxfDialog::XdxfDialog(XdxfPlugin *plugin,
 }
 
 
-void XdxfDialog::initializeUI() {
+void StarDialog::initializeUI() {
     mainVerticalLayout = new QVBoxLayout;
     widget = new QWidget;
     widget->setLayout(mainVerticalLayout);
@@ -71,7 +65,7 @@ void XdxfDialog::initializeUI() {
     infoLabel->setWordWrap(true);
 
     if(type == New) {
-        setWindowTitle(tr("Add new XDXF dictionary"));
+        setWindowTitle(tr("Add new StarDict dictionary"));
 
         browseLayout = new QHBoxLayout;
         browseButton = new QPushButton(tr("Browse"));
@@ -83,13 +77,17 @@ void XdxfDialog::initializeUI() {
         mainVerticalLayout->addLayout(browseLayout);
     }
     else {
-        setWindowTitle(tr("XDXF Settings"));
+        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);
     }
 
@@ -103,40 +101,23 @@ void XdxfDialog::initializeUI() {
         accentsLayout->addWidget(accentsInfoToolButton);
     #endif
 
-    cacheLayout = new QHBoxLayout;
-    cacheCheckBox = new QCheckBox(tr("Optimize"));
-    cacheCheckBox->setToolTip(cacheToolTip);
-    cacheLayout->addWidget(cacheCheckBox);
-    #ifdef Q_WS_MAEMO_5
-        cacheInfoToolButton = new QToolButton;
-        cacheInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
-        cacheLayout->addWidget(cacheInfoToolButton);
-    #endif
 
-    mainVerticalLayout->addLayout(cacheLayout);
     mainVerticalLayout->addLayout(accentsLayout);
 
 
     //load old setting if exists
     if(!plugin) {
-        cacheCheckBox->setChecked(true);
         accentsCheckBox->setChecked(true);
         accentsCheckBox->setEnabled(false);
-        _generateCache = true;
         _accents = true;
         _dictionaryFilePath = "";
     }
     else if(plugin && plugin->settings()->value("cached") == "true") {
-        cacheCheckBox->setChecked(true);
         accentsCheckBox->setChecked(true);
         accentsCheckBox->setEnabled(false);
-        _generateCache = true;
         _accents = true;
     }
     else {
-        cacheCheckBox->setChecked(false);
-        _generateCache = false;
-
         if(plugin->settings()->value("strip_accents") == "true") {
             accentsCheckBox->setChecked(true);
             _accents = true;
@@ -188,39 +169,33 @@ void XdxfDialog::initializeUI() {
 }
 
 
-void XdxfDialog::setAccents(bool accents) {
+void StarDialog::setAccents(bool accents) {
     _accents = accents;
 }
 
-void XdxfDialog::setGenerateCache(bool generate) {
-    _generateCache = generate;
-
-    if(generate) {
-        _lastAccents = _accents;
-        accentsCheckBox->setChecked(true);
-    }
-    else
-        accentsCheckBox->setChecked(_lastAccents);
-
-    accentsCheckBox->setEnabled(!generate);
-}
 
-void XdxfDialog::selectFile() {
+void StarDialog::selectFile() {
     QString fileName = QFileDialog::getOpenFileName(this,
                                      tr("Select dictionary file"),
                                      _dictionaryFilePath,
-                                     tr("XDXF Files (*.xdxf)"),
+                                     tr("StarDict Files (*.dict *dict.dz *.idx *idx.gz *.ifo)"),
                                      NULL,
                                      NULL);
 
     if (!fileName.isEmpty()) {
         infoLabel->setText(tr("Dictionary file: %1").arg(fileName));
         _dictionaryFilePath = fileName;
+        if (_dictionaryFilePath.endsWith(".tar.bz2")){
+                _isCompressed = true;
+        }
+        else {
+                _isCompressed = false;
+        }
         updateGeometry();
     }
 }
 
-void XdxfDialog::saveSettings() {
+void StarDialog::saveSettings() {
     _settings = new Settings;
     if(plugin) {
         foreach(QString key, plugin->settings()->keys())
@@ -228,40 +203,58 @@ void XdxfDialog::saveSettings() {
     }
     else {
         _settings->setValue("path", _dictionaryFilePath);
+        _settings->setValue("ifoFileName", _dictName + ".ifo");
+        _settings->setValue("idxFileName", _dictName + ".idx");
+        _settings->setValue("dictFileName", _dictName + ".dict");
+        if (QFile::exists(_dictName + ".syn") == true) {
+                _settings->setValue("synFileName", _dictName + ".syn");
+        }
     }
-
-    if(_generateCache)
-        _settings->setValue("generateCache", "true");
-    else
-        _settings->setValue("generateCache", "false");
-
     if(_accents)
         _settings->setValue("strip_accents", "true");
     else
         _settings->setValue("strip_accents", "false");
 }
 
-void XdxfDialog::accept() {
+void StarDialog::accept() {
     if(type == New && _dictionaryFilePath.isEmpty()) {
         Q_EMIT notify(Notify::Warning, tr("File path is not set"));
 
         return;
     }
 
+    if(type == New && !_dictionaryFilePath.isEmpty() && !checkFiles()) {
+        Q_EMIT notify(Notify::Warning, tr("Dictionary files are not complete"));
+        return;
+    }
+
     saveSettings();
     QDialog::accept();
 }
 
-Settings* XdxfDialog::getSettings() {
+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;
+        }
+        else {
+                //TODO: untar files (?)
+                return false;
+        }
+}
+
+Settings* StarDialog::getSettings() {
     return _settings;
 }
 
 #ifdef Q_WS_MAEMO_5
-    void XdxfDialog::showCacheInfo() {
-        Q_EMIT notify(Notify::Warning, cacheToolTip);
-    }
-
-    void XdxfDialog::showAccentsInfo() {
+    void StarDialog::showAccentsInfo() {
         Q_EMIT notify(Notify::Warning, accentsToolTip);
     }
 #endif