fix some bugs
authorjakub <jakub.jaszczynski@comarch.com>
Fri, 12 Nov 2010 09:52:29 +0000 (10:52 +0100)
committerjakub <jakub.jaszczynski@comarch.com>
Fri, 12 Nov 2010 09:52:29 +0000 (10:52 +0100)
src/mdictionary/gui/TranslationWidget.cpp
src/plugins/google/GooglePlugin.cpp
src/plugins/stardict/CompressedReader.cpp
src/plugins/stardict/StarDialog.cpp
src/plugins/stardict/StarDictDialog.cpp
src/plugins/stardict/StarDictPlugin.cpp
src/plugins/stardict/TranslationStarDict.cpp
src/plugins/stardict/UncompressedReader.cpp

index cf796c7..15859a3 100644 (file)
@@ -84,11 +84,11 @@ void TranslationWidget::show(QStringList translations) {
     trans=QString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") + QString("\n <ar>")
            + trans + QString("\n </ar>");
 
-    //qDebug()<<"\n\n\n\n\n\n\n\n\n before"<<trans;
     trans=checkTranslation(trans);
-    //qDebug()<<"\n\n after"<<trans;
     trans=XslConversion(trans);
 
+    qDebug()<<"\n\n trans"<<trans;
+
     QString head = "<html><head>";
     head += "<link rel=\"stylesheet\" type=\"text/css\" " ;
     head += "href=\"/usr/share/mdictionary/css/" "style.css";
@@ -119,6 +119,8 @@ QString TranslationWidget::checkTranslation(QString trans){
     /*check all "&"*/
     int pos=0;
     int pos2;
+    int count=0;
+
     while(pos!=-1){
         pos=trans.indexOf("&",pos+1);
         pos2=trans.indexOf("amp;",pos);
@@ -138,38 +140,26 @@ QString TranslationWidget::checkTranslation(QString trans){
             continue;
         if(pos!=-1)
             trans.insert(pos+1,"amp;");
-        if(pos>40)
-            while(1);
     }
 
-    /*check all "<" and ">" */
+
+    /*check all "<" and ">" in key */
     pos=0;
-    int count=0;
-    while(pos!=-1){
-        pos=trans.indexOf("<",pos+1);
-        pos2=trans.indexOf(">",pos+1);
-        if(pos!=-1 && pos2!=-1){
-            if(trans.at(pos2-1)!='/' && trans.at(pos+1)!='/')
-                count++;
-            if(trans.at(pos+1)=='/')
-                count--;
-        }
- /*     else if(pos==-1 && pos2!=-1){
-            qDebug()<<"pos=-1";
-            trans.remove(pos2,1);
+    count=0;
+    while(pos<trans.size()){
+        if(pos==trans.indexOf("<key>",pos)){
+            qDebug()<<"key";
+            count++;
+            pos+=4;
         }
-        else if(pos2==-1 && pos!=-1){
-            qDebug()<<"pos2=-1";
+        else if(pos==trans.indexOf("</key>",pos))
+            count--;
+        else if(count==1 && (trans.at(pos)=='<' ||  trans.at(pos)=='>' ))
             trans.remove(pos,1);
-        }
-        else {
-            qDebug()<<"pos i pos2 = -1";
-            break;
-        }
-        qDebug()<<"count"<<count;
-*/
+        pos++;
     }
 
+
     /*check all """ and "'" */
     pos=0;
     count=0;
index 38aac6c..d5e0683 100644 (file)
@@ -174,8 +174,11 @@ QList<Translation*> GooglePlugin::searchWordList(QString word, int ) {
     QList<Translation*> translations;
     if(isAvailable()) {
         QString error("");
-        word.replace("*","");   /*remove wildcard*/
-        word.replace("?","");
+        char wrongChar[]={"*?&<>"};
+        for(unsigned int a=0;a<sizeof(wrongChar);a++){    /*remove wrong char*/
+            word.replace(wrongChar[a],"");
+        }
+
         QString url=QString("/translate_a/t?client=t&sl=%1&tl=%2").arg(
                                               _settings->value("lang_from"),
                                               _settings->value("lang_to"));
index d142934..33a9744 100644 (file)
@@ -53,40 +53,44 @@ void CompressedReader::close() {
 
 
 QChar CompressedReader::readChar() {
-    char c[2]={0};
+    char c[4]={0};
+    QString cha;
+
     gzread(_file, c, 1);
-    QString cha(QString::fromUtf8(c));
-    if(!cha.at(0).isLetter() && c[0]!=0){
-        gzread(_file, c, 1);
-        cha=QString::fromUtf8(c);
+    if(((unsigned char)c[0])>127){
+        gzread(_file, c+1, 1);
+        if(((unsigned char)c[1])>127 && ((unsigned char)c[1])<192){
+            gzread(_file, c+2, 1);
+            if(((unsigned char)c[2])>127 && ((unsigned char)c[2])<192)
+                gzread(_file, c+3, 1);
+        }
     }
+
+    cha=QString::fromUtf8(c);
     return cha.at(0);
 }
 
 qint32 CompressedReader::readInt32BigEndian() {
     qint32 value;
     gzread(_file, (void*)(&value), 4);
-
     return qFromBigEndian(value);
 }
 
 qint64 CompressedReader::readInt64BigEndian() {
     qint64 value;
     gzread(_file, (void*)(&value), 8);
-
     return value;
 }
 
 QString CompressedReader::readKeyword() {
     QString result;
     QChar c;
-    c = readChar();
 
+    c = readChar();
     while(c != '\0') {
         result += c;
         c = readChar();
     }
-
     return result;
 }
 
index 6f18d02..139c8bd 100644 (file)
@@ -204,21 +204,21 @@ void StarDialog::saveSettings() {
         foreach(QString key, plugin->settings()->keys())
             _settings->setValue(key, plugin->settings()->value(key));
     }
-    //else {
-        _settings->setValue("path", _dictionaryFilePath);
-        _settings->setValue("ifoFileName", _dictName + ".ifo");
-        _settings->setValue("idxFileName", _dictName + ".idx");
-        if (QFile::exists(_dictName + ".dict.dz") == true) {
-                _settings->setValue("dictFileName", _dictName + ".dict.dz");
-        }
-        else {
-                _settings->setValue("dictFileName", _dictName + ".dict");
-        }
 
-        if (QFile::exists(_dictName + ".syn") == true) {
-                _settings->setValue("synFileName", _dictName + ".syn");
-        }
-    //}
+    _settings->setValue("path", _dictionaryFilePath);
+    _settings->setValue("ifoFileName", _dictName + ".ifo");
+    _settings->setValue("idxFileName", _dictName + ".idx");
+    if (QFile::exists(_dictName + ".dict.dz") == true) {
+            _settings->setValue("dictFileName", _dictName + ".dict.dz");
+    }
+    else {
+            _settings->setValue("dictFileName", _dictName + ".dict");
+    }
+
+    if (QFile::exists(_dictName + ".syn") == true) {
+            _settings->setValue("synFileName", _dictName + ".syn");
+    }
+
     if(_accents)
         _settings->setValue("strip_accents", "true");
     else
index d6dd535..f2c0f94 100644 (file)
@@ -44,7 +44,6 @@ void StarDictDialog::setLastDialogParent(QWidget *w) {
 
 Settings* StarDictDialog::addNewDictionary(QWidget *parent) {
     StarDialog d(0, StarDialog::New, parent);
-
     setLastDialogParent(parent);
 
     connect(&d, SIGNAL(notify(Notify::NotifyType,QString)),
@@ -58,7 +57,6 @@ Settings* StarDictDialog::addNewDictionary(QWidget *parent) {
 
 void StarDictDialog::changeSettings(QWidget * parent) {
     StarDialog d(plugin, StarDialog::Change, parent);
-
     setLastDialogParent(parent);
 
     connect(&d, SIGNAL(notify(Notify::NotifyType,QString)),
index bf75e6a..b12f843 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,7 +38,6 @@ 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;
@@ -45,13 +46,13 @@ StarDictPlugin::StarDictPlugin(QObject *parent) : CommonDictInterface(parent),
     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(":/stardicts/translations/en_US");
     }
     QCoreApplication::installTranslator(translator);
 }
@@ -89,26 +90,29 @@ 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;
+    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++;
@@ -120,7 +124,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);
@@ -136,8 +139,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;
 }
 
@@ -149,8 +150,10 @@ 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";
+        if(it == end)
+            qDebug()<<"end";
+        else
+            qDebug()<<"000";
     }
     else
         for(int i = 0; i < bytes && it != end; i++)
@@ -219,8 +222,9 @@ QString StarDictPlugin::interpret(QByteArray::iterator it,
         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 +255,7 @@ QString StarDictPlugin::interpret(QByteArray::iterator it,
     return result;
 }
 
+
 QString StarDictPlugin::format(QByteArray raw, QString mode,QString key) {
     QString result;
     if(mode == "") {
@@ -272,18 +277,15 @@ 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() {
-    qDebug()<<"zatrzymanie programu";
     stopped=true;
 }
 
@@ -303,14 +305,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;
     }
@@ -348,12 +350,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();
@@ -362,7 +363,6 @@ bool StarDictPlugin::getDictionaryInfo() {
             settings()->setValue(list.at(0),list.at(1));
         }
     }
-
     _name = settings()->value("bookname");
     return true;
 }
index 8ffd51e..18c6744 100644 (file)
@@ -76,10 +76,9 @@ QString TranslationStarDict::toXml() const {
             else
                 result+= _dictionaryInfo +"</info>";
 
-            /*przeformatowanie zwracanego stringu na oczekiwany format*/
+            /*conwert returned string to XML format*/
             result+=starDictPlugin->search(_key, offsets.at(i), lengths.at(i));
             result+= "</dict>";
-            //qDebug()<<"trans"<<result;
    }
    return result;
 }
index d4370c5..8bf95b7 100644 (file)
@@ -27,13 +27,20 @@ void UncompressedReader::close() {
 }
 
 QChar UncompressedReader::readChar() {
-    char c[2]={0};
+    char c[4]={0};
+    QString cha;
+
     _stream.readRawData(c,1);
-    QString cha(QString::fromUtf8(c));
-    if(!cha.at(0).isLetter() && c[0]!=0){
+    if(((unsigned char)c[0])>127){
         _stream.readRawData(c+1,1);
-        cha=QString::fromUtf8(c);
+        if(((unsigned char)c[1])>127 && ((unsigned char)c[1])<192){
+            _stream.readRawData(c+2,1);
+            if(((unsigned char)c[2])>127 && ((unsigned char)c[2])<192)
+                _stream.readRawData(c+3,1);
+        }
     }
+
+    cha=QString::fromUtf8(c);
     return cha.at(0);
 }
 
@@ -68,7 +75,6 @@ qint32 UncompressedReader::readInt32BigEndian() {
     qint32 value;
     _stream>>value;
     _stream.setByteOrder(QDataStream::LittleEndian);
-
     return value;
 }