QSqlQuery cur(db);
if(limit !=0)
- cur.prepare("select word from dict where word like ? or normalized 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 ? or normalized like ?");
+ cur.prepare("select word from dict where word like ? or normalized "
+ "like ?");
cur.addBindValue(word);
cur.addBindValue(word);
if(limit !=0)
return translations.toList();
}
+
QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
QSet<Translation*> translations;
-
QFile dictionaryFile(_settings->value("path"));
-
word = word.toLower();
-
stopped = false;
+
QRegExp regWord(word);
regWord.setCaseSensitivity(Qt::CaseInsensitive);
regWord.setPatternSyntax(QRegExp::Wildcard);
+
+ /*check xdxf file exist*/
if(!QFile::exists(_settings->value("path"))
|| !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
qDebug()<<"Error: could not open file";
}
QXmlStreamReader reader(&dictionaryFile);
- /*search words list*/
- QString a;
+ QString readKey;
int i=0;
+
+ /*search words list*/
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(a) || regWord.exactMatch(removeAccents(a))) &&
- (i<limit || limit==0)) {
+ readKey = reader.readElementText();
+ if((regWord.exactMatch(readKey)
+ || regWord.exactMatch(removeAccents(readKey)))
+ && (i<limit || limit==0)) {
bool ok=true;
Translation *tran;
foreach(tran,translations) {
- if(tran->key().toLower()==a.toLower())
- ok=false; /*if key word is in the dictionary more that one */
+ if(tran->key().toLower()==readKey.toLower())
+ ok=false; /*if key is in the dictionary more that one */
}
if(ok) { /*add key word to list*/
- translations<<(new TranslationXdxf(a.toLower(),
- _infoNote,this));
+ translations<<(new TranslationXdxf(readKey.toLower(),
+ _infoNote,this));
i++;
}
if(i>=limit && limit!=0)
}
+
QString XdxfPlugin::searchFile(QString key) {
- key = key.toLower();
QFile dictionaryFile(_settings->value("path"));
QString resultString("");
+ key = key.toLower();
+
+ /*check xdxf file exist*/
if(!QFile::exists(_settings->value("path"))
|| !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
Q_EMIT notify(Notify::Warning,
qDebug()<<"Error: could not open file";
return "";
}
- QXmlStreamReader reader(&dictionaryFile);
- QString a;
+ QXmlStreamReader reader(&dictionaryFile);
+ QString readKey;
bool match =false;
stopped = false;
+
+ /*search translations for word*/
while (!reader.atEnd()&& !stopped) {
reader.readNext();
if(reader.tokenType() == QXmlStreamReader::StartElement) {
if(reader.name()=="k") {
- a = reader.readElementText();
- if(a.toLower()==key.toLower())
+ readKey = reader.readElementText();
+ if(readKey.toLower()==key.toLower())
match = true;
}
}
}
if(temp.at(0)==QChar('\n'))
temp.remove(0,1);
- resultString+="<key>" + a +"</key>";
+ resultString+="<key>" + readKey +"</key>";
resultString+="<t>" + temp + "</t>";
match=false;
}
}
stopped=false;
dictionaryFile.close();
-
return resultString;
}
bool XdxfPlugin::makeCache(QString) {
cachingDialog->setVisible(true);
QCoreApplication::processEvents();
- stopped = false;
QFileInfo dictFileN(_settings->value("path"));
QString cachePathN;
+ stopped = false;
+
+ /*create cache file name*/
int i=0;
do {
cachePathN = QDir::homePath() + "/.mdictionary/"
- + dictFileN.completeBaseName()+"."+QString::number(i)+ ".cache";
+ + dictFileN.completeBaseName()+"."
+ +QString::number(i) + ".cache";
i++;
} while(QFile::exists(cachePathN));
+ /*checke errors (File open and db open)*/
QFile dictionaryFile(dictFileN.filePath());
if (!QFile::exists(_settings->value("path"))
|| !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
return 0;
}
QXmlStreamReader reader(&dictionaryFile);
-
-
db.setDatabaseName(cachePathN);
if(!db.open()) {
qDebug() << "Database error" << db.lastError().text() << endl;
"You may want to recache.").arg(name())));
return false;
}
+
+ /*inicial sqlQuery*/
QCoreApplication::processEvents();
QSqlQuery cur(db);
cur.exec("PRAGMA synchronous = 0");
int counter = 0;
cur.exec("BEGIN;");
- QString a;
+ QString readKey;
bool match = false;
QTime timer;
timer.start();
countWords();
-
int lastProg = -1;
-
- QString stripAcc = settings()->value("strip_accents");
settings()->setValue("strip_accents", "true");
-
-
counter=0;
- while (!reader.atEnd() && !stopped) {
+ /*add all words to db*/
+ while (!reader.atEnd() && !stopped) {
QCoreApplication::processEvents();
reader.readNext();
-
if(reader.tokenType() == QXmlStreamReader::StartElement) {
if(reader.name()=="k"){
- a = reader.readElementText();
+ readKey = reader.readElementText();
match = true;
}
}
if(reader.tokenType()==QXmlStreamReader::StartElement)
temp+="<";
temp+=reader.name().toString();
- if(reader.name().toString()=="c" && reader.tokenType()==QXmlStreamReader::StartElement)
- temp= temp + " c=\"" + reader.attributes().value("c").toString() + "\"";
+ if(reader.name().toString()=="c"
+ && reader.tokenType()==QXmlStreamReader::StartElement) {
+ temp= temp + " c=\""
+ + reader.attributes().value("c").toString()
+ + "\"";
+ }
temp+=">";
}
- temp+= reader.text().toString().replace("<","<").replace(">",">");;
+ temp+= reader.text().toString().replace("<","<").replace(">"
+ ,">");
reader.readNext();
}
if(temp.at(0)==QChar('\n'))
temp.remove(0,1);
- temp="<key>" + a + "</key>" + "<t>" + temp+ "</t>";
+ temp="<key>" + readKey + "</key>" + "<t>" + temp+ "</t>";
match=false;
cur.prepare("insert into dict values(?,?,?)");
- cur.addBindValue(a);
- cur.addBindValue(removeAccents(a));
+ cur.addBindValue(readKey);
+ cur.addBindValue(removeAccents(readKey));
cur.addBindValue(temp);
cur.exec();
counter++;
int prog = counter*100/_wordsCount;
if(prog % 5 == 0 && lastProg != prog) {
- Q_EMIT updateCachingProgress(prog,
- timer.restart());
+ Q_EMIT updateCachingProgress(prog,timer.restart());
lastProg = prog;
}
}
}
-
cur.exec("END;");
cur.exec("select count(*) from dict");
-
- countWords();
cachingDialog->setVisible(false);
+ /*checke errors (wrong number of added words)*/
+ countWords();
if(!cur.next() || countWords() != cur.value(0).toInt()) {
Q_EMIT updateCachingProgress(100, timer.restart());
Q_EMIT notify(Notify::Warning,
db.close();
return false;
}
+
_settings->setValue("cache_path", cachePathN);
_settings->setValue("cached", "true");
-
db.close();
return true;
}