fix a bug in convenction
[mdictionary] / trunk / src / plugins / xdxf / src / xdxfplugin.cpp
index e18474b..4d3aa55 100644 (file)
@@ -32,14 +32,17 @@ XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
                     _type(tr("xdxf")), _infoNote(tr("")) {
     _settings = new Settings();
     _dictDialog = new XdxfDictDialog(this, this);
-    _settings->setValue("Type","xdxf");
+    _settings->setValue("type","xdxf");
     if(isCached())
-        _settings->setValue("Cached","true");
+        _settings->setValue("cached","true");
     else
-        _settings->setValue("Cached","false");
+        _settings->setValue("cached","false");
+
+    _wordsCount = 0;
 
-    setPath("/usr/lib/mdictionary/dict.xdxf");
     stopped = false;
+
+    _icon = QIcon(":/icons/xdxf.png");
 }
 
 QString XdxfPlugin::langFrom() const {   
@@ -67,6 +70,8 @@ QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
     QSet<Translation*> translations;
     QFile dictionaryFile(path);
 
+    word = removeAccents(word);
+
     stopped = false;
     if(word.indexOf("*")==-1)
         word+="*";
@@ -78,22 +83,7 @@ QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
         return translations.toList();
     }
 
-    /*read information about dictionary*/
     QXmlStreamReader dictionaryReader(&dictionaryFile);
-    dictionaryReader.readNextStartElement();
-    if(dictionaryReader.name()=="xdxf") {
-      if(dictionaryReader.attributes().hasAttribute("lang_from"))
-        _langFrom = dictionaryReader.attributes().value("lang_from").toString();
-      if(dictionaryReader.attributes().hasAttribute("lang_to"))
-        _langTo = dictionaryReader.attributes().value("lang_to").toString();
-    }
-    dictionaryReader.readNextStartElement();
-    if(dictionaryReader.name()=="full_name")
-        _name=dictionaryReader.readElementText();
-    dictionaryReader.readNextStartElement();
-    if(dictionaryReader.name()=="description")
-        _infoNote=dictionaryReader.readElementText();
-
     /*search words list*/
     QString a;
     int i=0;
@@ -104,11 +94,10 @@ QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
                 dictionaryReader.readNextStartElement();
             if(!dictionaryReader.atEnd())
                 a = dictionaryReader.readElementText();
-            if(regWord.exactMatch(a) && (i<limit || limit==0)) {
+            if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
                 bool ok=true;
                 Translation *tran;
-                foreach(tran,translations)
-                {
+                foreach(tran,translations) {
                     if(tran->key()==a)
                         ok=false;  /*if key word is in the dictionary more that one */
                 }
@@ -182,13 +171,18 @@ DictDialog* XdxfPlugin::dictDialog() {
 void XdxfPlugin::setPath(QString path){
     this->path=path;
     _settings->setValue("path",path);
+    getDictionaryInfo();
 }
 
 
 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
     XdxfPlugin *plugin = new XdxfPlugin();
-    if(settings)
+    if(settings){
         plugin->setPath(settings->value("path"));
+        QStringList list = settings->keys();
+        foreach(QString key, list)
+            plugin->settings()->setValue(key, settings->value(key));
+    }
     return  plugin;
 }
 
@@ -196,13 +190,11 @@ bool XdxfPlugin::isAvailable() const {
     return true;
 }
 
-void XdxfPlugin::setHash(uint _hash)
-{
+void XdxfPlugin::setHash(uint _hash) {
     this->_hash=_hash;
 }
 
-uint XdxfPlugin::hash() const
-{
+uint XdxfPlugin::hash() const {
    return _hash;
 }
 
@@ -210,9 +202,71 @@ Settings* XdxfPlugin::settings() {
     return _settings;
 }
 
-bool XdxfPlugin::isCached()
-{
+bool XdxfPlugin::isCached() {
     return false;
 }
 
+void XdxfPlugin::setSettings(Settings *settings) {
+    _settings = settings;
+    setPath(_settings->value("path"));
+    emit settingsChanged();
+}
+
+
+void XdxfPlugin::getDictionaryInfo() {
+    QFile dictionaryFile(path);
+    if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
+        qDebug()<<"Error: could not open file";
+        return;
+    }
+
+    QXmlStreamReader dictionaryReader(&dictionaryFile);
+    dictionaryReader.readNextStartElement();
+    if(dictionaryReader.name()=="xdxf") {
+      if(dictionaryReader.attributes().hasAttribute("lang_from"))
+        _langFrom = dictionaryReader.attributes().value("lang_from").toString();
+      if(dictionaryReader.attributes().hasAttribute("lang_to"))
+        _langTo = dictionaryReader.attributes().value("lang_to").toString();
+    }
+    dictionaryReader.readNextStartElement();
+    if(dictionaryReader.name()=="full_name")
+        _name=dictionaryReader.readElementText();
+    dictionaryReader.readNextStartElement();
+    if(dictionaryReader.name()=="description")
+        _infoNote=dictionaryReader.readElementText();
+
+    /*dictionaryFile.seek(0);
+
+    long wordsCount = 0;
+
+    QString line;
+    while(!dictionaryFile.atEnd()) {
+        line = dictionaryFile.readLine();
+        if(line.contains("<ar>")) {
+            wordsCount++;
+        }
+    }*/
+
+    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.remove(i,1);
+        }
+    }
+    return normalized;
+}
+
+QIcon* XdxfPlugin::icon() {
+    return &_icon;
+}
+
 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)