fix bug (duplication of dictionarys in xdxf downloader)
[mdictionary] / src / plugins / xdxf / xdxfplugin.cpp
index 7e4550c..e0ca9de 100644 (file)
     Copyright 2010 Comarch S.A.
 
 *******************************************************************************/
-
-/*! \file xdxfplugin.cpp
-\author Jakub Jaszczynski <j.j.jaszczynski@gmail.com>
+/*!
+    \file xdxfplugin.cpp
+    \author Jakub Jaszczynski <j.j.jaszczynski@gmail.com>
 */
 
 #include "xdxfplugin.h"
 #include <QDebug>
 #include "../../include/Notify.h"
+#include "DownloadDict.h"
+#include "XdxfDictDownloader.h"
+
+XdxfDictDownloader XdxfPlugin::dictDownloader;
+
+
+bool XdxfPlugin::dictDownloaderInitialized = false;
+
 
 XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
                     _langFrom(""), _langTo(""),_name(""), _infoNote("") {
@@ -35,6 +43,11 @@ XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
     connect(_dictDialog, SIGNAL(notify(Notify::NotifyType,QString)),
             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");
@@ -44,9 +57,9 @@ XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
     initAccents();
 }
 
+
 void XdxfPlugin::retranslate() {
     QString locale = QLocale::system().name();
-
     QTranslator *translator = new QTranslator(this);
 
     if(!translator->load(":/xdxf/translations/" + locale)) {
@@ -99,7 +112,6 @@ QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
 
 
 QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
-    int i=0;
     QSet<Translation*> translations;
     QString cacheFilePath = _settings->value("cache_path");
 
@@ -112,6 +124,7 @@ QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
         _settings->setValue("cached","false");
         return searchWordListFile(word, limit);
     }
+
     stopped = false;
     word = word.toLower();
     word = word.replace("*", "%");
@@ -130,21 +143,10 @@ QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
         cur.addBindValue(limit);
     cur.exec();
 
-    bool in = false;
-    while(cur.next() && (i<limit || limit==0 ) ) {
-        in = true;
-        bool ok=true;
-        Translation *tran;
-        foreach(tran,translations) {
-            if(tran->key().toLower()==cur.value(0).toString().toLower())
-                    ok=false;
-        }
-        if(ok) {  /*add key word to list*/
-            translations.insert(new TranslationXdxf(
-                    cur.value(0).toString().toLower(),
-                    _dictionaryInfo, this));
-            i++;
-        }
+    while(cur.next() && (translations.size()<limit || limit==0)) {
+       translations.insert(new TranslationXdxf(
+            cur.value(0).toString(),
+            _dictionaryInfo, this));
     }
     db.close();
     return translations.toList();
@@ -184,19 +186,10 @@ QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
                 readKey = reader.readElementText();
             if((regWord.exactMatch(readKey)
                     || regWord.exactMatch(removeAccents(readKey)))
-                    && (i<limit || limit==0)) {
-                bool ok=true;
-                Translation *tran;
-                foreach(tran,translations) {
-                    if(tran->key().toLower()==readKey.toLower())
-                        ok=false; /*if key is in the dictionary more that one */
-                }
-                if(ok) {  /*add key word to list*/
-                    translations<<(new TranslationXdxf(readKey.toLower(),
-                                    _dictionaryInfo,this));
-                    i++;
-                }
-                if(i>=limit && limit!=0)
+                    && (i<limit || limit==0) && !reader.atEnd())  {
+                translations<<(new TranslationXdxf(readKey.toLower(),
+                               _dictionaryInfo,this));
+                if(translations.size()==limit && limit!=0)
                     break;
             }
         }
@@ -237,11 +230,9 @@ QString XdxfPlugin::searchCache(QString key) {
     cur.exec();
     while(cur.next())
         result += cur.value(0).toString();
-
     db.close();
 
     return result;
-
 }
 
 
@@ -308,7 +299,6 @@ QString XdxfPlugin::searchFile(QString key) {
 
 
 void XdxfPlugin::stop() {
-   //qDebug()<<"stop";
     stopped=true;
 }
 
@@ -326,10 +316,7 @@ CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
 
     ((XdxfDictDialog*)plugin->dictDialog())->setLastDialogParent(_dictDialog->lastDialogParent());
 
-
-
     if(settings && plugin->setSettings(settings)) {
-
         disconnect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
                 this, SIGNAL(notify(Notify::NotifyType,QString)));
         return plugin;
@@ -375,10 +362,9 @@ bool XdxfPlugin::setSettings(const Settings *settings) {
         foreach(QString key, _settings->keys())
             oldSettings->setValue(key, _settings->value(key));
 
-        foreach(QString key, settings->keys()) {
+        foreach(QString key, settings->keys())
            if(key != "generateCache")
                _settings->setValue(key, settings->value(key));
-        }
 
         if(!getDictionaryInfo()) {
             Q_EMIT notify(Notify::Warning,
@@ -479,7 +465,6 @@ int XdxfPlugin::countWords() {
     }
 
     dictionaryFile.seek(0);
-
     long wordsCount = 0;
 
     QString line;
@@ -496,18 +481,14 @@ int XdxfPlugin::countWords() {
 
 
 bool XdxfPlugin::makeCache(QString) {
-
     XdxfCachingDialog d(_dictDialog->lastDialogParent());
 
-//    qDebug()<<_dictDialog->lastDialogParent();
-
     connect(&d, SIGNAL(cancelCaching()),
             this, SLOT(stop()));
     connect(this, SIGNAL(updateCachingProgress(int,int)),
             &d, SLOT(updateCachingProgress(int,int)));
 
     d.show();
-
     QCoreApplication::processEvents();
     QFileInfo dictFileN(_settings->value("path"));
     QString cachePathN;
@@ -567,7 +548,6 @@ bool XdxfPlugin::makeCache(QString) {
 
     /*add all words to db*/
     while (!reader.atEnd() && !stopped) {
-
         QCoreApplication::processEvents();
         reader.readNext();
         if(reader.tokenType() == QXmlStreamReader::StartElement) {
@@ -602,8 +582,8 @@ bool XdxfPlugin::makeCache(QString) {
             temp="<key>" + readKey + "</key>" + "<t>" + temp+ "</t>";
             match=false;
             cur.prepare("insert into dict values(?,?,?)");
-            cur.addBindValue(readKey);
-            cur.addBindValue(removeAccents(readKey));
+            cur.addBindValue(readKey.toLower());
+            cur.addBindValue(removeAccents(readKey).toLower());
             cur.addBindValue(temp);
             cur.exec();
             counter++;
@@ -642,6 +622,7 @@ bool XdxfPlugin::makeCache(QString) {
     return true;
 }
 
+
 void XdxfPlugin::clean() {
     if(QFile::exists(_settings->value("cache_path"))) {
         QFile(_settings->value("cache_path")).remove();