Merge branch 'master' into google
[mdictionary] / trunk / src / plugins / xdxf / src / xdxfplugin.cpp
index b0c75d4..64d5fce 100644 (file)
 *******************************************************************************/
 
 /*! \file xdxfplugin.cpp
+\author Jakub Jaszczynski <j.j.jaszczynski@gmail.com>
 */
 
 #include "xdxfplugin.h"
 #include <QDebug>
+#include "../../../includes/Notify.h"
 
 XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
                     _langFrom(tr("")), _langTo(tr("")),_name(tr("")),
                     _type(tr("xdxf")), _infoNote(tr("")) {
     _wordsCount = -1;
     _settings = new Settings();
-    _dictDialog = new XdxfDictDialog(this, this);
-    cachingDialog = new XdxfCachingDialog(this);
+    _dictDialog = new XdxfDictDialog(this);
+    cachingDialog = new XdxfCachingDialog();
 
     connect(cachingDialog, SIGNAL(cancelCaching()),
             this, SLOT(stop()));
 
+    connect(this, SIGNAL(updateCachingProgress(int,int)),
+            cachingDialog, SLOT(updateCachingProgress(int,int)));
+
     _settings->setValue("type","xdxf");
 
     stopped = false;
 
     _icon = QIcon(":/icons/xdxf.png");
+    initAccents();
 }
 
-XdxfPlugin::~XdxfPlugin()
-{
-//  QString connection(db.connectionName());
-//   db.close();
-//  QSqlDatabase::removeDatabase(connection);
 
+
+XdxfPlugin::~XdxfPlugin() {
     delete _settings;
 }
 
+
 QString XdxfPlugin::langFrom() const {   
     return _langFrom;
 }
@@ -65,7 +69,6 @@ QString XdxfPlugin::name() const {
 }
 
 QString XdxfPlugin::type() const {
-//    return _settings->value("type");
     return _type;
 }
 
@@ -74,7 +77,6 @@ QString XdxfPlugin::infoNote() const {
 }
 
 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
-    //if(_settings->value("cached") == "true")
     if(word.indexOf("*")==-1 && word.indexOf("?")==-1 && word.indexOf("_")==-1
        && word.indexOf("%")==-1)
         word+="*";
@@ -90,6 +92,9 @@ QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
         db.setDatabaseName(cacheFilePath);
         if(!db.open()) {
             qDebug() << "Database error" << db.lastError().text() << endl;
+            Q_EMIT notify(Notify::Warning, QString("Cache database cannot be "
+                    "opened for %1 dictionary. Searching in xdxf file. "
+                    "You may want to recache.").arg(name()));
             return searchWordListFile(word, limit);
         }
 
@@ -97,19 +102,20 @@ QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
         word = word.toLower();
         word = word.replace("*", "%");
         word = word.replace("?", "_");
-        word = removeAccents(word);
-        //qDebug() << word;
 
         QSqlQuery cur(db);
         if(limit !=0)
-            cur.prepare("select word from dict where word like ? limit ?");
+            cur.prepare("select word from dict where word like ? or normalized like ? limit ?");
         else
-            cur.prepare("select word from dict where word like ?");
+            cur.prepare("select word from dict where word like ? or normalized like ?");
+        cur.addBindValue(word);
         cur.addBindValue(word);
         if(limit !=0)
             cur.addBindValue(limit);
         cur.exec();
+        bool in = false;
         while(cur.next()){
+            in = true;
             bool ok=true;
             Translation *tran;
             foreach(tran,translations) {
@@ -126,11 +132,13 @@ QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
 }
 
 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
+    QTime time;
+    time.start();
     QSet<Translation*> translations;
     QFile dictionaryFile(path);
 
     word = word.toLower();
-    word = removeAccents(word);
+    //word = removeAccents(word);
 
     stopped = false;
     QRegExp regWord(word);
@@ -138,12 +146,15 @@ QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
     regWord.setPatternSyntax(QRegExp::Wildcard);
     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
         qDebug()<<"Error: could not open file";
+        Q_EMIT notify(Notify::Warning,
+                QString("Xdxf file cannot be read for %1").arg(name()));
         return translations.toList();
     }
 
     QXmlStreamReader reader(&dictionaryFile);
     /*search words list*/
     QString a;
+
     int i=0;
     while(!reader.atEnd() && !stopped){
         reader.readNextStartElement();
@@ -152,7 +163,8 @@ QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
                 reader.readNextStartElement();
             if(!reader.atEnd())
                 a = reader.readElementText();
-            if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
+            if((regWord.exactMatch(a) || regWord.exactMatch(removeAccents(a))) &&
+                    (i<limit || limit==0)) {
                 bool ok=true;
                 Translation *tran;
                 foreach(tran,translations) {
@@ -171,6 +183,7 @@ QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
     }
     stopped=false;
     dictionaryFile.close();
+    qDebug() << time.elapsed();
     return translations.toList();
 }
 
@@ -189,6 +202,9 @@ QString XdxfPlugin::searchCache(QString key) {
 
     if(!db.open()) {
         qDebug() << "Database error" << db.lastError().text() << endl;
+        Q_EMIT notify(Notify::Warning, QString("Cache database cannot be "
+                "opened for %1 dictionary. Searching in xdxf file. "
+                "You may want to recache.").arg(name()));
         return searchFile(key);
     }
 
@@ -210,7 +226,9 @@ QString XdxfPlugin::searchFile(QString key) {
     QFile dictionaryFile(path);
     QString resultString("");
     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
-        qDebug()<<"Error: could not open file when search";
+        Q_EMIT notify(Notify::Warning,
+                QString("Xdxf file cannot be read for %1").arg(name()));
+        qDebug()<<"Error: could not open file";
         return "";
     }
     QXmlStreamReader reader(&dictionaryFile);
@@ -236,11 +254,14 @@ QString XdxfPlugin::searchFile(QString key) {
                     if(reader.tokenType()==QXmlStreamReader::StartElement)
                         temp+=tr("<");
                     temp+=reader.name().toString();
-                    if(reader.name().toString()=="c" && reader.tokenType()==QXmlStreamReader::StartElement)
-                       temp= temp + tr(" c=\"") + reader.attributes().value(tr("c")).toString() + tr("\"");
+                    if(reader.name().toString()=="c" &&
+                            reader.tokenType()==QXmlStreamReader::StartElement)
+                       temp= temp + tr(" c=\"") + reader.attributes().
+                               value(tr("c")).toString() + tr("\"");
                     temp+=tr(">");
                 }
-                temp+= reader.text().toString().replace("<","&lt;").replace(">","&gt;");
+                temp+= reader.text().toString().replace("<","&lt;").
+                        replace(">","&gt;");
                 reader.readNext();
             }
             if(temp.at(0)==QChar('\n'))
@@ -273,14 +294,10 @@ void XdxfPlugin::setPath(QString path){
 
 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
     XdxfPlugin *plugin = new XdxfPlugin();
-    static int a=0;
     if(settings){
         plugin->setPath(settings->value("path"));
         QStringList list = settings->keys();
-        foreach(QString key, list)
-            plugin->settings()->setValue(key, settings->value(key));
 
-        a=a+1;
         plugin->db_name = plugin->_settings->value("type")
                          + plugin->_settings->value("path");
         plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
@@ -289,6 +306,9 @@ CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
            settings->value("generateCache") == "true") {
             plugin->makeCache("");
         }
+        foreach(QString key, list)
+           if(key != "generateCache")
+                plugin->settings()->setValue(key, settings->value(key));
         delete settings;
     }
     plugin->getDictionaryInfo();
@@ -299,24 +319,34 @@ bool XdxfPlugin::isAvailable() const {
     return true;
 }
 
+
+
 void XdxfPlugin::setHash(uint _hash) {
     this->_hash=_hash;
 }
 
+
+
 uint XdxfPlugin::hash() const {
    return _hash;
 }
 
+
+
 Settings* XdxfPlugin::settings() {
     return _settings;
 }
 
+
+
 bool XdxfPlugin::isCached() {
     if(_settings->value("cached") == "true")
         return true;
     return false;
 }
 
+
+
 void XdxfPlugin::setSettings(Settings *settings) {
 
     QString oldPath = _settings->value("path");
@@ -324,22 +354,32 @@ void XdxfPlugin::setSettings(Settings *settings) {
         setPath(settings->value("path"));
     }
 
+   foreach(QString key, settings->keys())
+       if(key != "generateCache")
+           _settings->setValue(key, settings->value(key));
+
     if((_settings->value("cached") == "false" ||
-        _settings->value("cached").isEmpty()) &&
-       settings->value("generateCache") == "true") {
+            _settings->value("cached").isEmpty()) &&
+            settings->value("generateCache") == "true") {
         makeCache("");
     }
-    else {
+    else if (settings->value("generateCache") != "true") {
        _settings->setValue("cached", "false");
     }
+
+
     delete settings;
 
     Q_EMIT settingsChanged();
 }
 
+
+
 void XdxfPlugin::getDictionaryInfo() {
     QFile dictionaryFile(path);
     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
+       Q_EMIT notify(Notify::Warning,
+               QString("Xdxf file cannot be read dictionary"));
         qDebug()<<"Error: could not open file";
         return;
     }
@@ -361,33 +401,12 @@ void XdxfPlugin::getDictionaryInfo() {
 
     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 + ")";
 
+    _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;
-    for(int i=0; i<normalized.size(); i++) {
-        if( !normalized[i].isLetterOrNumber() &&
-            !normalized[i].isSpace() &&
-            !normalized[i].isDigit() &&
-            normalized[i] != '*' &&
-            normalized[i] != '%' &&
-            normalized[i] != '_' &&
-            normalized[i] != '?' ) {
-            normalized.remove(i,1);
-        }
-    }
-    return normalized;
-}
+
 
 QIcon* XdxfPlugin::icon() {
     return &_icon;
@@ -399,6 +418,9 @@ int XdxfPlugin::countWords() {
 
     QFile dictionaryFile(path);
     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
+        Q_EMIT notify(Notify::Warning,
+                QString("Xdxf file cannot be read for %1 dictionary")
+                .arg(name()));
         qDebug()<<"Error: could not open file";
         return -1;
     }
@@ -432,14 +454,22 @@ bool XdxfPlugin::makeCache(QString dir) {
 
 
     if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
+        Q_EMIT updateCachingProgress(100, 0);
+        Q_EMIT notify(Notify::Warning,
+                QString("Xdxf file cannot be read for %1 dictionary")
+                .arg(name()));
         return 0;
     }
-
     QXmlStreamReader reader(&dictionaryFile);
 
+
     db.setDatabaseName(cachePathN);
     if(!db.open()) {
         qDebug() << "Database error" << db.lastError().text() << endl;
+        Q_EMIT updateCachingProgress(100, 0);
+        Q_EMIT notify(Notify::Warning, QString("Cache database cannot be "
+                "opened for %1 dictionary. Searching in xdxf file. "
+                "You may want to recache.").arg(name()));
         return false;
     }
     QCoreApplication::processEvents();
@@ -447,7 +477,7 @@ bool XdxfPlugin::makeCache(QString dir) {
     cur.exec("PRAGMA synchronous = 0");
     cur.exec("drop table dict");
     QCoreApplication::processEvents();
-    cur.exec("create table dict(word text ,translation text)");
+    cur.exec("create table dict(word text, normalized text ,translation text)");
     int counter = 0;
     cur.exec("BEGIN;");
 
@@ -459,6 +489,9 @@ bool XdxfPlugin::makeCache(QString dir) {
 
     int lastProg = -1;
 
+    QString stripAcc = settings()->value("strip_accents");
+    settings()->setValue("strip_accents", "true");
+
 
     counter=0;
     while (!reader.atEnd() && !stopped) {
@@ -492,8 +525,9 @@ bool XdxfPlugin::makeCache(QString dir) {
                 temp.remove(0,1);
             temp=tr("<key>") + a + tr("</key>") + tr("<t>") + temp+ tr("</t>");
             match=false;
-            cur.prepare("insert into dict values(?,?)");
+            cur.prepare("insert into dict values(?,?,?)");
             cur.addBindValue(a);
+            cur.addBindValue(removeAccents(a));
             cur.addBindValue(temp);
             cur.exec();
             counter++;
@@ -508,12 +542,16 @@ bool XdxfPlugin::makeCache(QString dir) {
 
     cur.exec("END;");
     cur.exec("select count(*) from dict");
+    settings()->setValue("strip_accents", stripAcc);
 
     countWords();
     cachingDialog->setVisible(false);
 
     if(!cur.next() || countWords() != cur.value(0).toInt())
     {
+        Q_EMIT updateCachingProgress(100, timer.restart());
+        Q_EMIT notify(Notify::Warning,
+                QString("Database caching error, please try againg."));
         db.close();
         return false;
     }