Merge branch 'master' into google
[mdictionary] / trunk / src / plugins / xdxf / src / xdxfplugin.cpp
index 369a234..64d5fce 100644 (file)
@@ -20,6 +20,7 @@
 *******************************************************************************/
 
 /*! \file xdxfplugin.cpp
+\author Jakub Jaszczynski <j.j.jaszczynski@gmail.com>
 */
 
 #include "xdxfplugin.h"
@@ -50,38 +51,10 @@ XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
 
 
 
-XdxfPlugin::~XdxfPlugin()
-{
-//  QString connection(db.connectionName());
-//   db.close();
-//  QSqlDatabase::removeDatabase(connection);
-
+XdxfPlugin::~XdxfPlugin() {
     delete _settings;
 }
 
-void XdxfPlugin::initAccents() {
-    letters['a'] = QRegExp("[ÀàÁáÂÂâÃãÄäÅåæÆĀāĂ㥹]", Qt::CaseInsensitive);
-    letters['c'] = QRegExp("[ÇçÈçŒĆćĈĉĊċČč]", Qt::CaseInsensitive);
-    letters['d'] = QRegExp("[ÐĐđĎď]", Qt::CaseInsensitive);
-    letters['e'] = QRegExp("[ÈéèÉÊêËëĒēĔĕĖėĘęĚěē]", Qt::CaseInsensitive);
-    letters['f'] = QRegExp("[ſ]", Qt::CaseInsensitive);
-    letters['g'] = QRegExp("[ĠġĢģĜĝĞğ]", Qt::CaseInsensitive);
-    letters['h'] = QRegExp("[ħĤĥĦ]", Qt::CaseInsensitive);
-    letters['i'] = QRegExp("[ÌìÍíÎîÏïİijĨĩĪīĬĭĮį]", Qt::CaseInsensitive);
-    letters['j'] = QRegExp("[Ĵĵ]", Qt::CaseInsensitive);
-    letters['k'] = QRegExp("[Ķķĸ]", Qt::CaseInsensitive);
-    letters['l'] = QRegExp("[ŀŁłĹĺĻļĽľĿ]", Qt::CaseInsensitive);
-    letters['n'] = QRegExp("[ÑñŃńŅņŇňʼnŊŋ]", Qt::CaseInsensitive);
-    letters['o'] = QRegExp("[ÒòÓóÔÕõÖöØøŐőœŌōŎŏ]", Qt::CaseInsensitive);
-    letters['r'] = QRegExp("[ŕŖŖŗŘř]", Qt::CaseInsensitive);
-    letters['s'] = QRegExp("[ߊšŚśŜŝŞş]", Qt::CaseInsensitive);
-    letters['t'] = QRegExp("[ŢţŤťŦŧ]", Qt::CaseInsensitive);
-    letters['u'] = QRegExp("[ÙùÚúÛûÜüŰűŲųŨũŪūŬŮů]", Qt::CaseInsensitive);
-    letters['w'] = QRegExp("[Ŵŵ]", Qt::CaseInsensitive);
-    letters['y'] = QRegExp("[ÿÝýŶŷŸ]", Qt::CaseInsensitive);
-    letters['z'] = QRegExp("[ŹźŻżŽž]", Qt::CaseInsensitive);
-    noLetter = QRegExp("[^a-z ]", Qt::CaseInsensitive);
-}
 
 QString XdxfPlugin::langFrom() const {   
     return _langFrom;
@@ -96,7 +69,6 @@ QString XdxfPlugin::name() const {
 }
 
 QString XdxfPlugin::type() const {
-//    return _settings->value("type");
     return _type;
 }
 
@@ -322,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);
@@ -338,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();
@@ -348,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");
@@ -373,19 +354,27 @@ 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;
 
-    emit settingsChanged();
+    Q_EMIT settingsChanged();
 }
 
+
+
 void XdxfPlugin::getDictionaryInfo() {
     QFile dictionaryFile(path);
     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
@@ -413,28 +402,12 @@ void XdxfPlugin::getDictionaryInfo() {
     QString format = "png";
     QString initialPath = QDir::currentPath() + tr("/xdxf.") + format;
 
-    _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.toLower();
-    QString normalized = string.normalized(QString::NormalizationForm_KD);
-    foreach(QChar let, letters.keys())
-        normalized.replace(letters[let], QString(let));
-
-    normalized.replace(noLetter, "");
-    //qDebug() << "NORMALIZED: " << normalized;
-    return normalized;
-    return string;
-}
-
-
-
 QIcon* XdxfPlugin::icon() {
     return &_icon;
 }
@@ -489,6 +462,7 @@ bool XdxfPlugin::makeCache(QString dir) {
     }
     QXmlStreamReader reader(&dictionaryFile);
 
+
     db.setDatabaseName(cachePathN);
     if(!db.open()) {
         qDebug() << "Database error" << db.lastError().text() << endl;
@@ -515,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) {
@@ -556,7 +533,6 @@ bool XdxfPlugin::makeCache(QString dir) {
             counter++;
             int prog = counter*100/_wordsCount;
             if(prog % 5 == 0 && lastProg != prog) {
-                qDebug() << prog;
                 Q_EMIT updateCachingProgress(prog,
                                              timer.restart());
                 lastProg = prog;
@@ -566,6 +542,7 @@ bool XdxfPlugin::makeCache(QString dir) {
 
     cur.exec("END;");
     cur.exec("select count(*) from dict");
+    settings()->setValue("strip_accents", stripAcc);
 
     countWords();
     cachingDialog->setVisible(false);