Merge branch 'cache' of ssh://drop.maemo.org/git/mdictionary into cache
authorMateusz Półrola <mateusz.polrola@comarch.pl>
Fri, 13 Aug 2010 08:22:25 +0000 (10:22 +0200)
committerMateusz Półrola <mateusz.polrola@comarch.pl>
Fri, 13 Aug 2010 08:22:25 +0000 (10:22 +0200)
1  2 
trunk/src/plugins/xdxf/src/xdxfplugin.cpp

@@@ -33,8 -33,6 +33,8 @@@ XdxfPlugin::XdxfPlugin(QObject *parent
      _wordsCount = -1;
      _settings = new Settings();
      _dictDialog = new XdxfDictDialog(this, this);
 +    cachingDialog = new XdxfCachingDialog(this);
 +
      _settings->setValue("type","xdxf");
      if(isCached())
          _settings->setValue("cached","true");
@@@ -76,12 -74,37 +76,37 @@@ QList<Translation*> XdxfPlugin::searchW
  
  QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
  
+     qDebug() << "search cache";
+     QSet<Translation*> translations;
+     QString cacheFilePath = _settings->value("cache_path");
+     QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
+         db.setDatabaseName(cacheFilePath);
+         if(!db.open()) {
+             qDebug() << "Database error" << endl;
+             return searchWordListFile(word, limit);
+         }
  
+         stopped = false;
+         word = removeAccents(word);
+         if(word.indexOf("*")==-1)
+             word+="%";
+         word = word.replace("*", "%");
+         QSqlQuery cur;
+         cur.prepare("select word from dict where word like ? limit ?");
+         cur.addBindValue(word);
+         cur.addBindValue(limit);
+         cur.exec();
+         while(cur.next())
+             translations.insert(new TranslationXdxf(cur.value(0).toString(),
+                                                     _infoNote, this));
+         return translations.toList();
  }
  
  
  
  QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
+     qDebug() << "search file";
      QSet<Translation*> translations;
      QFile dictionaryFile(path);
  
@@@ -198,7 -221,8 +223,8 @@@ CommonDictInterface* XdxfPlugin::getNew
          QStringList list = settings->keys();
          foreach(QString key, list)
              plugin->settings()->setValue(key, settings->value(key));
-         plugin->makeCache("");
+         if(plugin->settings()->value("cached") != "true")
+             plugin->makeCache("");
      }
      return  plugin;
  }
@@@ -306,7 -330,6 +332,7 @@@ int XdxfPlugin::countWords() 
  
  
  bool XdxfPlugin::makeCache(QString dir) {
 +    cachingDialog->setVisible(true);
      QFileInfo dictFileN(_settings->value("path"));
      QString cachePathN;
      cachePathN = dictFileN.dir().absolutePath() + "/"
      counter=0;
      while (!reader.atEnd()) {
  
 +        QCoreApplication::processEvents();
 +        //usleep(50);
          reader.readNext();
  
          if(reader.tokenType() == QXmlStreamReader::StartElement) {
                  counter++;
                  int prog = counter*100/_wordsCount;
                  if(prog % 5 == 0)
 -                    Q_EMIT update(prog);
 +                    Q_EMIT updateCachingProgress(prog);
              }
  
          }
      }
  
 +    cachingDialog->setVisible(false);
 +
      qDebug()<<counter;
      cur.exec("END;");
      cur.exec("select count(*) from dict");
          qDebug() << "ŻLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE";
          return false;
      }
+     _settings->setValue("cache_path", cachePathN);
+     _settings->setValue("cached", "true");
      return true;
  }