implement a lot's of function of googlePlugin
[mdictionary] / trunk / src / plugins / xdxf / src / xdxfplugin.cpp
index 73c7e5f..407b41a 100644 (file)
 
 *******************************************************************************/
 
+/*! \file xdxfplugin.cpp
+*/
+
 #include "xdxfplugin.h"
 #include <QDebug>
-#include <QFile>
-#include <QXmlStreamReader>
-#include <QtPlugin>
-#include "TranslationXdxf.h"
-#include "../../../includes/settings.h"
 
 XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
                     _langFrom(tr("")), _langTo(tr("")),_name(tr("")),
@@ -45,6 +43,15 @@ XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
     _icon = QIcon(":/icons/xdxf.png");
 }
 
+XdxfPlugin::~XdxfPlugin()
+{
+//  QString connection(db.connectionName());
+//   db.close();
+//  QSqlDatabase::removeDatabase(connection);
+
+    delete _settings;
+}
+
 QString XdxfPlugin::langFrom() const {   
     return _langFrom;
 }
@@ -87,31 +94,42 @@ QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
         }
 
         stopped = false;
-        if(word.indexOf("*")==-1 && word.indexOf("?")== 0)
-            word+="%";
+        word = word.toLower();
         word = word.replace("*", "%");
         word = word.replace("?", "_");
         word = removeAccents(word);
-        qDebug() << word;
+        //qDebug() << word;
 
         QSqlQuery cur(db);
-        cur.prepare("select word from dict where word like ? limit ?");
+        if(limit !=0)
+            cur.prepare("select word from dict where word like ? limit ?");
+        else
+            cur.prepare("select word from dict where word like ?");
         cur.addBindValue(word);
-        cur.addBindValue(limit);
+        if(limit !=0)
+            cur.addBindValue(limit);
         cur.exec();
-        while(cur.next())
-            translations.insert(new TranslationXdxf(cur.value(0).toString(),
-                                                    _infoNote, this));
+        while(cur.next()){
+            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(),
+                        _infoNote, this));
+        }
         db.close();
-        return translations.toList();
+    return translations.toList();
 }
 
-
-
 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
     QSet<Translation*> translations;
     QFile dictionaryFile(path);
 
+    word = word.toLower();
     word = removeAccents(word);
 
     stopped = false;
@@ -138,11 +156,12 @@ QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
                 bool ok=true;
                 Translation *tran;
                 foreach(tran,translations) {
-                    if(tran->key()==a)
+                    if(tran->key().toLower()==a.toLower())
                         ok=false;  /*if key word is in the dictionary more that one */
                 }
                 if(ok)  /*add key word to list*/
-                    translations<<(new TranslationXdxf(a,_infoNote,this));
+                    translations<<(new TranslationXdxf(a.toLower(),
+                                _infoNote,this));
                 i++;
                 if(i>=limit && limit!=0)
                     break;
@@ -162,12 +181,11 @@ QString XdxfPlugin::search(QString key) {
     return searchFile(key);
 }
 
-
-
 QString XdxfPlugin::searchCache(QString key) {
-    QString result;
+    QString result("");
     QString cacheFilePath = _settings->value("cache_path");
     db.setDatabaseName(cacheFilePath);
+    key = key.toLower();
 
     if(!db.open()) {
         qDebug() << "Database error" << db.lastError().text() << endl;
@@ -175,29 +193,27 @@ QString XdxfPlugin::searchCache(QString key) {
     }
 
     QSqlQuery cur(db);
-    cur.prepare("select translation from dict where word like ? limit 1");
+    cur.prepare("select translation from dict where word like ?");
     cur.addBindValue(key);
     cur.exec();
-    if(cur.next())
-        result = cur.value(0).toString();
+    while(cur.next())
+        result += cur.value(0).toString();
+
     db.close();
+
     return result;
 
 }
 
-
-
-
 QString XdxfPlugin::searchFile(QString key) {
+    key = key.toLower();
     QFile dictionaryFile(path);
     QString resultString("");
     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
-        qDebug()<<"Error: could not open file";
+        qDebug()<<"Error: could not open file when search";
         return "";
     }
     QXmlStreamReader reader(&dictionaryFile);
-
-
     QString a;
 
     bool match =false;
@@ -207,7 +223,7 @@ QString XdxfPlugin::searchFile(QString key) {
         if(reader.tokenType() == QXmlStreamReader::StartElement) {
             if(reader.name()=="k") {
                 a = reader.readElementText();
-                if(a==key)
+                if(a.toLower()==key.toLower())
                     match = true;
             }
         }
@@ -224,10 +240,13 @@ QString XdxfPlugin::searchFile(QString key) {
                        temp= temp + tr(" c=\"") + reader.attributes().value(tr("c")).toString() + tr("\"");
                     temp+=tr(">");
                 }
-                temp+= reader.text().toString();
+                temp+= reader.text().toString().replace("<","&lt;").replace(">","&gt;");
                 reader.readNext();
             }
-            resultString+=tr("<t>") + temp.replace("\n","") + tr("</t>");
+            if(temp.at(0)==QChar('\n'))
+                temp.remove(0,1);
+            resultString+=tr("<key>") + a +tr("</key>");
+            resultString+=tr("<t>") + temp + tr("</t>");
             match=false;
         }
         this->thread()->yieldCurrentThread();
@@ -252,28 +271,24 @@ void XdxfPlugin::setPath(QString path){
     //getDictionaryInfo();
 }
 
-
 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
     XdxfPlugin *plugin = new XdxfPlugin();
     if(settings){
         plugin->setPath(settings->value("path"));
-
         QStringList list = settings->keys();
         foreach(QString key, list)
             plugin->settings()->setValue(key, settings->value(key));
 
-
         plugin->db_name = plugin->_settings->value("type")
-               + plugin->_settings->value("path");
-        if(!plugin->db.connectionName().isEmpty())
+                         + plugin->_settings->value("path");
         plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
 
         if(settings->value("cached").isEmpty() &&
            settings->value("generateCache") == "true") {
             plugin->makeCache("");
         }
+        delete settings;
     }
-
     plugin->getDictionaryInfo();
     return  plugin;
 }
@@ -315,11 +330,11 @@ void XdxfPlugin::setSettings(Settings *settings) {
     else {
        _settings->setValue("cached", "false");
     }
+    delete settings;
 
     emit settingsChanged();
 }
 
-
 void XdxfPlugin::getDictionaryInfo() {
     QFile dictionaryFile(path);
     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
@@ -342,13 +357,19 @@ void XdxfPlugin::getDictionaryInfo() {
     if(reader.name()=="description")
         _infoNote=reader.readElementText();
 
-    _infoNote="<info path=\""+path+"\">"+"\n" + _name + "(" + _type + ")"  + "</info>";
+    QString format = "png";
+    QString initialPath = QDir::currentPath() + tr("/xdxf.") + format;
+//  qDebug()<<initialPath;
+//  QPixmap test(":/icons/xdxf.png");
+//  qDebug()<<QPixmap(test).save(initialPath,format.toAscii());
+//  qDebug()<<QPixmap("/home/jakub/star.jpg").save(initialPath,format.toAscii());
+
+    _infoNote="path=\""+initialPath+"\">"+"\n" + _name + " [" + _langFrom + "-" + _langTo + "] "+ "(" + _type + ")";
 
     dictionaryFile.close();
 }
 
 QString XdxfPlugin::removeAccents(QString string) {
-
     string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
     QString normalized = string.normalized(QString::NormalizationForm_D);
     normalized = normalized;
@@ -396,8 +417,6 @@ int XdxfPlugin::countWords() {
     return wordsCount;
 }
 
-
-
 bool XdxfPlugin::makeCache(QString dir) {
     cachingDialog->setVisible(true);
     QCoreApplication::processEvents();
@@ -464,10 +483,12 @@ bool XdxfPlugin::makeCache(QString dir) {
                        temp= temp + tr(" c=\"") + reader.attributes().value(tr("c")).toString() + tr("\"");
                     temp+=tr(">");
                 }
-                temp+= reader.text().toString();
+                temp+= reader.text().toString().replace("<","&lt;").replace(">","&gt;");;
                 reader.readNext();
             }
-            temp += tr("<t>") + temp.replace("\n","") + tr("</t>");
+            if(temp.at(0)==QChar('\n'))
+                temp.remove(0,1);
+            temp=tr("<key>") + a + tr("</key>") + tr("<t>") + temp+ tr("</t>");
             match=false;
             cur.prepare("insert into dict values(?,?)");
             cur.addBindValue(a);
@@ -497,9 +518,9 @@ bool XdxfPlugin::makeCache(QString dir) {
     _settings->setValue("cache_path", cachePathN);
     _settings->setValue("cached", "true");
 
+
     db.close();
     return true;
 }
 
-
 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)