implement a lot's of function of googlePlugin
[mdictionary] / trunk / src / plugins / xdxf / src / xdxfplugin.cpp
index b734e26..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;
 }
@@ -115,7 +122,7 @@ QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
                         _infoNote, this));
         }
         db.close();
-       return translations.toList();
+    return translations.toList();
 }
 
 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
@@ -186,15 +193,14 @@ 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())
     while(cur.next())
-//      result = cur.value(0).toString();
         result += cur.value(0).toString();
+
     db.close();
+
     return result;
 
 }
@@ -204,13 +210,10 @@ QString XdxfPlugin::searchFile(QString key) {
     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;
@@ -242,7 +245,8 @@ QString XdxfPlugin::searchFile(QString key) {
             }
             if(temp.at(0)==QChar('\n'))
                 temp.remove(0,1);
-            resultString+=tr("<t>") + temp + tr("</t>");    //.replace("\n","")
+            resultString+=tr("<key>") + a +tr("</key>");
+            resultString+=tr("<t>") + temp + tr("</t>");
             match=false;
         }
         this->thread()->yieldCurrentThread();
@@ -271,23 +275,20 @@ 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() || settings->value("generateCache")=="true")
-            plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
+                         + 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;
 }
@@ -329,6 +330,7 @@ void XdxfPlugin::setSettings(Settings *settings) {
     else {
        _settings->setValue("cached", "false");
     }
+    delete settings;
 
     emit settingsChanged();
 }
@@ -357,12 +359,12 @@ void XdxfPlugin::getDictionaryInfo() {
 
     QString format = "png";
     QString initialPath = QDir::currentPath() + tr("/xdxf.") + format;
-//    qDebug()<<initialPath;
-//    qDebug()<<QPixmap(":/icons/xdxf.png").save(initialPath,format.toAscii());
-
-    _infoNote="<info path=\""+initialPath+"\">"+"\n" + _name + "(" + _type + ")"  + "</info>";
-
+//  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();
 }
@@ -486,7 +488,7 @@ bool XdxfPlugin::makeCache(QString dir) {
             }
             if(temp.at(0)==QChar('\n'))
                 temp.remove(0,1);
-            temp=tr("<t>") + temp+ tr("</t>");  //.replace("\n","")
+            temp=tr("<key>") + a + tr("</key>") + tr("<t>") + temp+ tr("</t>");
             match=false;
             cur.prepare("insert into dict values(?,?)");
             cur.addBindValue(a);
@@ -516,6 +518,7 @@ bool XdxfPlugin::makeCache(QString dir) {
     _settings->setValue("cache_path", cachePathN);
     _settings->setValue("cached", "true");
 
+
     db.close();
     return true;
 }