Merge branch 'stardict' of ssh://drop.maemo.org/git/mdictionary into stardict
[mdictionary] / src / plugins / stardict / StarDictPlugin.cpp
index d6df778..c7a2557 100644 (file)
 
 #include "StarDictPlugin.h"
 
+/**/
 uint qHash(const TranslationStarDict &key) {
    return qHash(key.key());
 }
 
+
 StarDictPlugin::StarDictPlugin(QObject *parent) : CommonDictInterface(parent),
                     _langFrom(""), _langTo(""),_name(""), _infoNote("") {
     _settings = new Settings();
@@ -36,22 +38,20 @@ StarDictPlugin::StarDictPlugin(QObject *parent) : CommonDictInterface(parent),
     connect(_dictDialog, SIGNAL(notify(Notify::NotifyType,QString)),
             this, SIGNAL(notify(Notify::NotifyType,QString)));
 
-
     _settings->setValue("type","stardict");
     _icon = QIcon("/usr/share/mdictionary/stardict.png");
-    _wordsCount = -1;
     stopped = false;
 
     initAccents();
 }
 
+
 void StarDictPlugin::retranslate() {
     QString locale = QLocale::system().name();
-
     QTranslator *translator = new QTranslator(this);
 
-    if(!translator->load(":/xdxf/translations/" + locale)) {
-        translator->load(":/xdxf/translations/en_US");
+    if(!translator->load(":/stardict/translations/" + locale)) {
+        translator->load(":/stardict/translations/en_US");
     }
     QCoreApplication::installTranslator(translator);
 }
@@ -89,27 +89,31 @@ QString StarDictPlugin::infoNote() const {
 
 
 QList<Translation*> StarDictPlugin::searchWordList(QString word, int limit) {
-    qDebug() << "StarDict searachWordList" << word;
     QList<TranslationStarDict> translations;
     bool is32b = false;
-    if(settings()->value("idxoffsetbits") == "32" ||
-            settings()->value("idxoffsetbits") == "")
-       is32b = true;
-
     bool err = 0;
     int wordcount = settings()->value("wordcount").toInt(&err);
-    if(!err)
-        return QList<Translation*>();
-
+    int counter = 0;
+    int counterLimit = 0;
     QString idxPath = settings()->value("idxFileName");
     StarDictReader * reader = StarDictReaderFactory::createReader(idxPath);
     QString fkey;
     qint64 offset = 0, len = 0;
     QRegExp keyword(word, Qt::CaseInsensitive, QRegExp::Wildcard);
 
-    int counter = 0;
-    int counterLimit = 0;
-    while(counter < wordcount && (counterLimit<limit || limit==0)) {
+    if( word.indexOf("*")==-1 && word.indexOf("?")==-1 &&
+        word.indexOf("_")==-1 && word.indexOf("%")==-1)
+        word+="*";
+
+    if((settings()->value("idxoffsetbits") == "32") ||
+                         (settings()->value("idxoffsetbits") == ""))
+        is32b = true;
+
+    if(!err)
+        return QList<Translation*>();
+
+    stopped=false;
+    while(counter < wordcount && (counterLimit<limit || limit==0) && !stopped) {
         counter++;
         fkey = reader->readKeyword();
         if(is32b)
@@ -119,7 +123,6 @@ QList<Translation*> StarDictPlugin::searchWordList(QString word, int limit) {
         len = reader->readInt32BigEndian();
 
         if(keyword.exactMatch(fkey)) {
-            qDebug()<<"InfoNote"<<infoNote();
             TranslationStarDict tran(fkey, infoNote(), this);
             qDebug() << "off/len" << offset << len;
             int id = translations.indexOf(tran);
@@ -135,8 +138,6 @@ QList<Translation*> StarDictPlugin::searchWordList(QString word, int limit) {
     QListIterator<TranslationStarDict> it(translations);
     while(it.hasNext())
         ret.push_back(new TranslationStarDict(it.next()));
-
-
     return ret;
 }
 
@@ -148,8 +149,6 @@ QByteArray StarDictPlugin::read(QByteArray::iterator it,
     if(bytes == 0 && it != end){
         while(*it != '\0' && it != end)
             ret.append(*it++);
-        if(it == end) qDebug()<<"end";
-        else  qDebug()<<"000";
     }
     else
         for(int i = 0; i < bytes && it != end; i++)
@@ -161,34 +160,32 @@ QByteArray StarDictPlugin::read(QByteArray::iterator it,
 QString StarDictPlugin::interpret(QByteArray::iterator it,
         QByteArray::iterator end, QChar mode,QString key, bool last) {
     QString result;
-    if(mode == 'm'){
+    qDebug()<<"****** mode:     "<<mode;
+
+    if(mode == 'm' ||
+            mode == 'l' ||
+            mode == 'g' ||
+            mode == 't' ||
+            mode == 'y' ||
+            mode == 'k' ||
+            mode == 'w' ||
+            mode == 'h' ||
+            mode == 'r' ){
         result += "<key>" + key + "</key>";
-        result += QString::fromUtf8(read(it++, end));
+        result += "<t>" + QString::fromUtf8(read(it++, end)) + "</t>";
     }
-    else if(mode == 'l'){
-        result += "<key>" + key + "</key>";
-        result += QString::fromUtf8(read(it++, end));
-    }
-    else if(mode ==  'g'){
-        result += "<key>" + key + "</key>";
-        result += QString::fromUtf8(read(it++, end));
 
-        /* delete "small" tag  from translation*/
+    if(mode ==  'g'){
+        /* delete "small" tag  from g translation*/
         int indexOfSmall=result.indexOf("<small>");
         while(indexOfSmall!=-1){
-            qDebug()<<"przed"<<result;
             int indexOfEndSmall= result.indexOf("</small>");
             if(indexOfEndSmall!=-1)
                 result.remove(indexOfSmall,indexOfEndSmall-indexOfSmall+8);
             indexOfSmall=result.indexOf("<small>");
-            qDebug()<<"po"<<result;
         }
     }
-    else if(mode == 't'){
-        result += "<key>" + key + "</key>";
-        result += QString::fromUtf8(read(it++, end));
-    }
-    else if(mode == 'x'){
+    if(mode == 'x'){
         result += QString::fromUtf8(read(it++, end));
         result.replace("</k>","</key><t>");
         result.replace("<k>","</t><key>");
@@ -198,29 +195,9 @@ QString StarDictPlugin::interpret(QByteArray::iterator it,
         if(result.contains("<t>"))
             result+="</t>";
     }
-    else if(mode == 'y') {
-        result += "<key>" + key + "</key>";
-        result += QString::fromUtf8(read(it++, end));
-    }
-    else if(mode == 'k'){
-        result += "<key>" + key + "</key>";
-        result += QString::fromUtf8(read(it++, end));
-    }
-    else if(mode == 'w'){
-        result += "<key>" + key + "</key>";
-        result += QString::fromUtf8(read(it++, end));
-    }
-    else if(mode == 'h'){
-        result += "<key>" + key + "</key>";
-        result += QString::fromUtf8(read(it++, end));
-    }
-    else if(mode == 'r'){
-        result += "<key>" + key + "</key>";
-        result += QString::fromUtf8(read(it++, end));
-    }
 
 // Dont know whether mDictionary would ever handle binary stardict format
-//     to be honest dont see any kind of adventages (can't find any binary dict)
+// to be honest dont see any kind of adventages (can't find any binary dict)
  /*
     }
     else if(mode == 'W') {
@@ -251,6 +228,7 @@ QString StarDictPlugin::interpret(QByteArray::iterator it,
     return result;
 }
 
+
 QString StarDictPlugin::format(QByteArray raw, QString mode,QString key) {
     QString result;
     if(mode == "") {
@@ -268,20 +246,17 @@ QString StarDictPlugin::format(QByteArray raw, QString mode,QString key) {
 }
 
 
-
 QString StarDictPlugin::search(QString key, qint64 offset, qint32 len) {
     QString dictPath = settings()->value("dictFileName");
     StarDictReader *reader = StarDictReaderFactory::createReader(dictPath);
-
     QByteArray raw = reader->readString(offset, len);
-    qDebug()<<"mod"<<settings()->value("sametypesequence");
     QString result= format(raw, settings()->value("sametypesequence"),key);
+
     delete reader;
     return result;
 }
 
 
-
 void StarDictPlugin::stop() {
     stopped=true;
 }
@@ -302,14 +277,14 @@ CommonDictInterface* StarDictPlugin::getNew(const Settings *settings) const {
             setLastDialogParent(_dictDialog->lastDialogParent());
 
     if(settings && plugin->setSettings(settings)) {
-        disconnect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
-                this, SIGNAL(notify(Notify::NotifyType,QString)));
+        disconnect( plugin, SIGNAL(notify(Notify::NotifyType,QString)),
+                    this, SIGNAL(notify(Notify::NotifyType,QString)));
         plugin->getDictionaryInfo();
         return plugin;
     }
     else {
-        disconnect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
-                this, SIGNAL(notify(Notify::NotifyType,QString)));
+        disconnect( plugin, SIGNAL(notify(Notify::NotifyType,QString)),
+                    this, SIGNAL(notify(Notify::NotifyType,QString)));
         delete plugin;
         return 0;
     }
@@ -326,11 +301,6 @@ Settings* StarDictPlugin::settings() {
 }
 
 
-bool StarDictPlugin::isCached() {
-    return false;
-}
-
-
 bool StarDictPlugin::setSettings(const Settings *sett) {
     if(sett) {
         foreach(QString key, sett->keys())
@@ -347,12 +317,11 @@ bool StarDictPlugin::getDictionaryInfo() {
     QFile file(settings()->value("ifoFileName"));
     if(!QFile::exists(_settings->value("ifoFileName"))
                 || !file.open(QFile::ReadOnly | QFile::Text)) {
-       Q_EMIT notify(Notify::Warning,
+        Q_EMIT notify(Notify::Warning,
                QString(tr("StarDict dictionary cannot be read from file")));
         qDebug()<<"Error: could not open the file";
         return false;
     }
-
     QTextStream in(&file);
     while (!in.atEnd()) {
         QString line = in.readLine();
@@ -361,7 +330,6 @@ bool StarDictPlugin::getDictionaryInfo() {
             settings()->setValue(list.at(0),list.at(1));
         }
     }
-
     _name = settings()->value("bookname");
     return true;
 }
@@ -377,7 +345,6 @@ int StarDictPlugin::countWords() {
 }
 
 
-
 void StarDictPlugin::clean() {
 
 }