Clean and order documentation in source files. Source ready to beta 2 release
[mdictionary] / src / plugins / stardict / StarDictPlugin.cpp
index 87928cb..67e2db3 100644 (file)
     Copyright 2010 Comarch S.A.
 
 *******************************************************************************/
+/*!
+     \file StarDictPlugin.cpp
+     \brief Implementation of stardict plugin's main class.
 
-/*! \file stardictplugin.cpp
+     \author Jakub Jaszczynski
 */
 
 #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 +41,23 @@ 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/xdxf.png");
-    _wordsCount = -1;
+    _icon = QIcon("/usr/share/mdictionary/stardict.png");
     stopped = false;
 
+    _settings->setValue("strip_accents","true");
+
     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);
 }
@@ -84,31 +90,37 @@ QString StarDictPlugin::type() const {
 
 
 QString StarDictPlugin::infoNote() const {
-    return _infoNote;
+    return _name;
 }
 
 
 QList<Translation*> StarDictPlugin::searchWordList(QString word, int limit) {
     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;
+    qint64 offset = 0,
+    len = 0;
+
+    if( word.indexOf("*")==-1 && word.indexOf("?")==-1 &&
+        word.indexOf("_")==-1 && word.indexOf("%")==-1)
+        word+="*";
     QRegExp keyword(word, Qt::CaseInsensitive, QRegExp::Wildcard);
 
-    int counter = 0;
-    while(counter < wordcount) {
+    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)
@@ -117,47 +129,135 @@ QList<Translation*> StarDictPlugin::searchWordList(QString word, int limit) {
             offset = reader->readInt64BigEndian();
         len = reader->readInt32BigEndian();
 
-        if(keyword.exactMatch(fkey)) {
+        if(keyword.exactMatch(fkey) ) {
             TranslationStarDict tran(fkey, infoNote(), this);
+  //        qDebug() << "off/len" << offset << len;
             int id = translations.indexOf(tran);
             if(id == -1) {
                 tran.add(offset, len);
                 translations.push_front(tran);
             } else
                 translations[id].add(offset, len);
+            counterLimit++;
         }
-
-
     }
     QList<Translation*> ret;
     QListIterator<TranslationStarDict> it(translations);
     while(it.hasNext())
         ret.push_back(new TranslationStarDict(it.next()));
+    return ret;
+}
 
 
+QByteArray StarDictPlugin::read(QByteArray::iterator it,
+        QByteArray::iterator end, int bytes) {
+    QByteArray ret;
+
+    if(bytes == 0 && it != end){
+        while(*it != '\0' && it != end)
+            ret.append(*it++);
+    }
+    else
+        for(int i = 0; i < bytes && it != end; i++)
+            ret.append(*it++);
     return ret;
 }
 
 
+QString StarDictPlugin::interpret(QByteArray::iterator it,
+        QByteArray::iterator end, QChar mode,QString key, bool) {
+    QString result;
+//    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 += "<t>" + QString::fromUtf8(read(it++, end)) + "</t>";
+    }
 
-QString StarDictPlugin::search(QString key) {
-    return "";
-}
+    if(mode ==  'g'){
+        /* delete "small" tag  from g translation*/
+        int indexOfSmall=result.indexOf("<small>");
+        while(indexOfSmall!=-1){
+            int indexOfEndSmall= result.indexOf("</small>");
+            if(indexOfEndSmall!=-1)
+                result.remove(indexOfSmall,indexOfEndSmall-indexOfSmall+8);
+            indexOfSmall=result.indexOf("<small>");
+        }
+    }
+    if(mode == 'x'){
+        result += QString::fromUtf8(read(it++, end));
+        result.replace("</k>","</key><t>");
+        result.replace("<k>","</t><key>");
+        int pos=result.indexOf("</t>");
+        if(pos!=-1)
+            result.remove(pos,4);
+        if(result.contains("<t>"))
+            result+="</t>";
+    }
 
-void StarDictPlugin::parseIfoFile()
-{
-    QFile file(settings()->value("path"));
-    if (!file.open(QIODevice::ReadOnly)) {
-        // TODO notification
-        return ;
+// W & P 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)
+ /*
     }
-    QTextStream in(&file);
-    while (!in.atEnd()) {
-        QString line = in.readLine();
-        QStringList list = line.split("=");
+    else if(mode == 'W') {
+        result += "<key>" + key + "</key>";
+        if(!last) {
+            QByteArray tmp ;
+            tmp.append(*(it++));
+            tmp.append(*(it++));
+            tmp.append(*(it++));
+            tmp.append(*(it));
+            result += read(it++, end, (qint32)qFromBigEndian(*(qint32*)tmp.data()));
+        } else
+            result += read(it++, end);
+    } else if(mode == 'P') {
+        result += "<key>" + key + "</key>";
+        if(!last) {
+            QByteArray tmp ;
+            tmp.append(*(it++));
+            tmp.append(*(it++));
+            tmp.append(*(it++));
+            tmp.append(*(it));
+            result += read(it++, end, (qint32)qFromBigEndian(*(qint32*)tmp.data()));
+        } else
+            result += read(it++, end);
+    } */
+    return result;
+}
+
 
-        settings()->setValue(list.at(0),list.at(1));
+QString StarDictPlugin::format(QByteArray raw, QString mode,QString key) {
+    QString result;
+    if(mode == "") {
+        for(QByteArray::iterator it = raw.begin(); it != raw.end(); it++) {
+            char tmp = *(++it);
+            result += interpret(--it, raw.end(), tmp, key);
+        }
+    } else {
+        QByteArray::iterator it = raw.begin();
+        foreach(QChar tmp, mode) {
+            result += interpret(it, raw.end(), tmp, key);
+        }
     }
+    return result;
+}
+
+
+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);
+    QString result= format(raw, settings()->value("sametypesequence"),key);
+
+    delete reader;
+    return result;
 }
 
 
@@ -177,19 +277,18 @@ CommonDictInterface* StarDictPlugin::getNew(const Settings *settings) const {
     connect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
             this, SIGNAL(notify(Notify::NotifyType,QString)));
 
-    ((StarDictDialog*)plugin->dictDialog())->setLastDialogParent(_dictDialog->lastDialogParent());
-
-
+    ((StarDictDialog*)plugin->dictDialog())->
+            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;
     }
@@ -206,13 +305,10 @@ Settings* StarDictPlugin::settings() {
 }
 
 
-bool StarDictPlugin::isCached() {
-    return false;
-}
-
-
-bool StarDictPlugin::setSettings(const Settings *settings) {
-    if(settings) {
+bool StarDictPlugin::setSettings(const Settings *sett) {
+    if(sett) {
+        foreach(QString key, sett->keys())
+            _settings->setValue(key, sett->value(key));
 
     } else
         return false;
@@ -222,16 +318,24 @@ bool StarDictPlugin::setSettings(const Settings *settings) {
 
 
 bool StarDictPlugin::getDictionaryInfo() {
-    QFile dictionaryFile(_settings->value("path"));
-    if(!QFile::exists(_settings->value("path"))
-                || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
-       Q_EMIT notify(Notify::Warning,
+    QFile file(settings()->value("ifoFileName"));
+    if(!QFile::exists(_settings->value("ifoFileName"))
+                || !file.open(QFile::ReadOnly | QFile::Text)) {
+        Q_EMIT notify(Notify::Warning,
                QString(tr("StarDict dictionary cannot be read from file")));
         qDebug()<<"Error: could not open the file";
         return false;
     }
-
-    return false;
+    QTextStream in(&file);
+    while (!in.atEnd()) {
+        QString line = in.readLine();
+        QStringList list = line.split("=");
+        if(list.size() == 2) {
+            settings()->setValue(list.at(0),list.at(1));
+        }
+    }
+    _name = settings()->value("bookname");
+    return true;
 }
 
 
@@ -239,16 +343,4 @@ QIcon* StarDictPlugin::icon() {
     return &_icon;
 }
 
-
-int StarDictPlugin::countWords() {
-    return 0;
-}
-
-
-
-void StarDictPlugin::clean() {
-
-}
-
-
 Q_EXPORT_PLUGIN2(stardict, StarDictPlugin)