Merge branch 'cache' of ssh://drop.maemo.org/git/mdictionary into cache
authorPiotrek <ppilar11@gmail.com>
Wed, 18 Aug 2010 09:46:16 +0000 (11:46 +0200)
committerPiotrek <ppilar11@gmail.com>
Wed, 18 Aug 2010 09:46:16 +0000 (11:46 +0200)
Conflicts:
trunk/src/plugins/xdxf/tests/test.cpp

1  2 
trunk/src/plugins/xdxf/src/xdxfplugin.cpp
trunk/src/plugins/xdxf/tests/test.cpp

@@@ -102,7 -102,6 +102,7 @@@ QList<Translation*> XdxfPlugin::searchW
          while(cur.next())
              translations.insert(new TranslationXdxf(cur.value(0).toString(),
                                                      _infoNote, this));
 +        db.close();
          return translations.toList();
  }
  
@@@ -123,17 -122,17 +123,17 @@@ QList<Translation*> XdxfPlugin::searchW
          return translations.toList();
      }
  
-     QXmlStreamReader dictionaryReader(&dictionaryFile);
+     QXmlStreamReader reader(&dictionaryFile);
      /*search words list*/
      QString a;
      int i=0;
-     while(!dictionaryReader.atEnd() && !stopped){
-         dictionaryReader.readNextStartElement();
-         if(dictionaryReader.name()=="ar"){
-             while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
-                 dictionaryReader.readNextStartElement();
-             if(!dictionaryReader.atEnd())
-                 a = dictionaryReader.readElementText();
+     while(!reader.atEnd() && !stopped){
+         reader.readNextStartElement();
+         if(reader.name()=="ar") {
+             while(reader.name()!="k" && !reader.atEnd())
+                 reader.readNextStartElement();
+             if(!reader.atEnd())
+                 a = reader.readElementText();
              if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
                  bool ok=true;
                  Translation *tran;
@@@ -181,7 -180,6 +181,7 @@@ QString XdxfPlugin::searchCache(QStrin
      cur.exec();
      if(cur.next())
          result = cur.value(0).toString();
 +    db.close();
      return result;
  
  }
@@@ -196,36 -194,40 +196,40 @@@ QString XdxfPlugin::searchFile(QString 
          qDebug()<<"Error: could not open file";
          return "";
      }
-     QXmlStreamReader dictionaryReader(&dictionaryFile);
+     QXmlStreamReader reader(&dictionaryFile);
  
  
      QString a;
  
      bool match =false;
      stopped = false;
-     while (!dictionaryReader.atEnd()&& !stopped) {
-         dictionaryReader.readNext();
-         if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
-             if(dictionaryReader.name()=="k") {
-                 a = dictionaryReader.readElementText();
+     while (!reader.atEnd()&& !stopped) {
+         reader.readNext();
+         if(reader.tokenType() == QXmlStreamReader::StartElement) {
+             if(reader.name()=="k") {
+                 a = reader.readElementText();
                  if(a==key)
                      match = true;
              }
          }
-         else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
-             if(match) {
-                 QString temp(dictionaryReader.text().toString());
-                 temp.replace("\n","");
-                 if(temp == ""){
-                     while(dictionaryReader.name()!="ar"&&
-                                 !dictionaryReader.atEnd()){
-                         dictionaryReader.readNext();
-                         temp+=dictionaryReader.text().toString();
-                     }
+         if(match) {
+             QString temp("");
+             while(reader.name()!="ar" && !reader.atEnd()) {
+                 if(reader.name()!="" && reader.name()!="k") {
+                     if(reader.tokenType()==QXmlStreamReader::EndElement)
+                         temp+=tr("</");
+                     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("\"");
+                     temp+=tr(">");
                  }
-                 resultString+=temp.replace("\n","")+"\n";
-                 match=false;
+                 temp+= reader.text().toString();
+                 reader.readNext();
              }
+             resultString+=tr("<t>") + temp.replace("\n","") + tr("</t>");
+             match=false;
          }
          this->thread()->yieldCurrentThread();
      }
@@@ -261,7 -263,6 +265,7 @@@ CommonDictInterface* XdxfPlugin::getNew
  
          plugin->db_name = plugin->_settings->value("type")
                 + plugin->_settings->value("path");
 +        if(!plugin->db.connectionName().isEmpty())
          plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
  
          if(settings->value("cached").isEmpty() &&
@@@ -326,20 -327,20 +330,20 @@@ void XdxfPlugin::getDictionaryInfo() 
          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();
+     QXmlStreamReader reader(&dictionaryFile);
+     reader.readNextStartElement();
+     if(reader.name()=="xdxf") {
+       if(reader.attributes().hasAttribute("lang_from"))
+         _langFrom = reader.attributes().value("lang_from").toString();
+       if(reader.attributes().hasAttribute("lang_to"))
+         _langTo = reader.attributes().value("lang_to").toString();
      }
-     dictionaryReader.readNextStartElement();
-     if(dictionaryReader.name()=="full_name")
-         _name=dictionaryReader.readElementText();
-     dictionaryReader.readNextStartElement();
-     if(dictionaryReader.name()=="description")
-         _infoNote=dictionaryReader.readElementText();
+     reader.readNextStartElement();
+     if(reader.name()=="full_name")
+         _name=reader.readElementText();
+     reader.readNextStartElement();
+     if(reader.name()=="description")
+         _infoNote=reader.readElementText();
  
      dictionaryFile.close();
  }
@@@ -450,31 -451,35 +454,35 @@@ bool XdxfPlugin::makeCache(QString dir
                  match = true;
              }
          }
-         else if(reader.tokenType() == QXmlStreamReader::Characters) {
-              if(match) {
-                 QString temp(reader.text().toString());
-                 temp.replace("\n","");
-                 if(temp == ""){
-                     while(reader.name()!="ar"&&
-                                 !reader.atEnd()){
-                         reader.readNext();
-                         temp+=reader.text().toString();
-                     }
-                 }
-                 match = false;
-                 cur.prepare("insert into dict values(?,?)");
-                 cur.addBindValue(a);
-                 cur.addBindValue(temp);
-                 cur.exec();
-                 counter++;
-                 int prog = counter*100/_wordsCount;
-                 if(prog % 5 == 0 && lastProg != prog) {
-                     Q_EMIT updateCachingProgress(prog,
-                                                  timer.restart());
-                     lastProg = prog;
+         if(match) {
+             QString temp("");
+             while(reader.name()!="ar" && !reader.atEnd()) {
+                 if(reader.name()!="" && reader.name()!="k") {
+                     if(reader.tokenType()==QXmlStreamReader::EndElement)
+                         temp+=tr("</");
+                     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("\"");
+                     temp+=tr(">");
                  }
+                 temp+= reader.text().toString();
+                 reader.readNext();
+             }
+             temp += tr("<t>") + temp.replace("\n","") + tr("</t>");
+             match=false;
+             cur.prepare("insert into dict values(?,?)");
+             cur.addBindValue(a);
+             cur.addBindValue(temp);
+             cur.exec();
+             counter++;
+             int prog = counter*100/_wordsCount;
+             if(prog % 5 == 0 && lastProg != prog) {
+                 Q_EMIT updateCachingProgress(prog,
+                                              timer.restart());
+                 lastProg = prog;
              }
          }
      }
  
      cachingDialog->setVisible(false);
  
      if(!cur.next() || countWords() != cur.value(0).toInt())
 +    {
 +        db.close();
          return false;
 +    }
      _settings->setValue("cache_path", cachePathN);
      _settings->setValue("cached", "true");
  
 +    db.close();
      return true;
  }
  
@@@ -42,9 -42,11 +42,11 @@@ void XdxfTest::search() 
      settings->setValue("path","../tests/dict.xdxf");
      CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
  
-     //xdxfPlugin->search("wino");
-     QCOMPARE(xdxfPlugin->search("."), QString("kropka\n"));
-     QCOMPARE(xdxfPlugin->search("1"), QString("one\n"));
+     xdxfPlugin->search("wino");
+     QCOMPARE(xdxfPlugin->search("."), QString("<t>kropka</t>"));
+     QCOMPARE(xdxfPlugin->search("1"), QString("<t>one</t>"));
+     QCOMPARE(xdxfPlugin->search("test"), QString("<t><c c=\"FF00FF\">kro</c>test01<pos>krowa</pos></t>"));
  }
  
  void XdxfTest::searchWordList() {
@@@ -56,7 -58,7 +58,7 @@@
      CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
      QList<Translation*> te = xdxfPlugin->searchWordList(".", 10);
      if(te.size()>0)
-         QCOMPARE(te.size(), 8);
+         QCOMPARE(te.size(), 9);
      QList<Translation*> te2 = xdxfPlugin->searchWordList("1",10);
      QCOMPARE(te2.size(), 5);
  
@@@ -67,7 -69,7 +69,7 @@@
          QCOMPARE(te4.at(0)->key(), QString("house"));
  
      QList<Translation*> te5 = xdxfPlugin->searchWordList("*");
-         QCOMPARE(te5.size(), 8);
+         QCOMPARE(te5.size(), 9);
  
      QList<Translation*> te6 = xdxfPlugin->searchWordList("*", 8);
          QCOMPARE(te6.size(), 8);
@@@ -87,6 -89,7 +89,6 @@@
      QList<Translation*> te11 = xdxfPlugin->searchWordList("h**?*?**e");
          QCOMPARE(te11.at(0)->key(), QString("house"));
  
 -
  }
  
  void XdxfTest::stop() {
      QFuture<QList<Translation*> > future = QtConcurrent::run(xdxfPlugin, &CommonDictInterface::searchWordList, string, 10);
      QList<Translation*> te5 = future.result();
  
-     QCOMPARE(te5.size(), 8);
+     QCOMPARE(te5.size(), 9);
  }