Merge branch 'xdxf'
authorJakub Jaszczynski <j.j.jaszczynski@gmail.com>
Fri, 6 Aug 2010 06:37:32 +0000 (08:37 +0200)
committerJakub Jaszczynski <j.j.jaszczynski@gmail.com>
Fri, 6 Aug 2010 06:37:32 +0000 (08:37 +0200)
Conflicts:
trunk/src/base/gui/MainWindow.cpp
trunk/src/base/gui/TranslationWidget.cpp
trunk/src/plugins/xdxf/src/xdxfplugin.cpp

1  2 
trunk/src/base/gui/TranslationWidget.cpp
trunk/src/plugins/xdxf/src/TranslationXdxf.cpp
trunk/src/plugins/xdxf/src/TranslationXdxf.h
trunk/src/plugins/xdxf/src/xdxfplugin.cpp
trunk/src/plugins/xdxf/src/xdxfplugin.h

@@@ -29,18 -29,9 +29,19 @@@ TranslationWidget::TranslationWidget(Ba
  
      this->backbone = backbone;
  
 -    //setAttribute(Qt::WA_Maemo5StackedWindow);
++
 +    #ifdef Q_WS_MAEMO_5
 +        setAttribute(Qt::WA_Maemo5StackedWindow);
 +    #endif
      setWindowFlags(windowFlags() | Qt::Window);
  
 +    setContextMenuPolicy(Qt::CustomContextMenu);
 +
 +    setReadOnly(true);
 +
 +    connect(this, SIGNAL(customContextMenuRequested(QPoint)),
 +            this, SLOT(showContextMenu(QPoint)));
 +
      initializeUI();
  }
  
index c2bb1a7,0000000..0c3fcfb
mode 100644,000000..100644
--- /dev/null
@@@ -1,30 -1,0 +1,51 @@@
++/*******************************************************************************
++
++    This file is part of mDictionary.
++
++    mDictionary is free software: you can redistribute it and/or modify
++    it under the terms of the GNU General Public License as published by
++    the Free Software Foundation, either version 3 of the License, or
++    (at your option) any later version.
++
++    mDictionary is distributed in the hope that it will be useful,
++    but WITHOUT ANY WARRANTY; without even the implied warranty of
++    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
++    GNU General Public License for more details.
++
++    You should have received a copy of the GNU General Public License
++    along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
++
++    Copyright 2010 Comarch S.A.
++
++*******************************************************************************/
++
 +#include "TranslationXdxf.h"
 +
 +
 +TranslationXdxf::TranslationXdxf() {
 +}
 +
 +TranslationXdxf::TranslationXdxf(QString _key, QString _dictionaryInfo, XdxfPlugin *xdxfPlugin): _key(_key),_dictionaryInfo(_dictionaryInfo) {
 +    this->xdxfPlugin=xdxfPlugin;
 +}
 +
 +QString TranslationXdxf::key() const {
 +    return _key;
 +}
 +
 +QString TranslationXdxf::dictionaryInfo() const {
 +    return _dictionaryInfo;
 +}
 +
 +QString TranslationXdxf::toHtml() const {
 +    return xdxfPlugin->search(_key);
 +}
 +
 +void TranslationXdxf::setKey(QString _key) {
 +    this->_key=_key;
 +}
 +
 +void TranslationXdxf::setDictionaryInfo(QString _dictionaryInfo) {
 +    this->_dictionaryInfo=_dictionaryInfo;
 +}
 +
index 0c65eb4,0000000..a691d9a
mode 100644,000000..100644
--- /dev/null
@@@ -1,39 -1,0 +1,60 @@@
++/*******************************************************************************
++
++    This file is part of mDictionary.
++
++    mDictionary is free software: you can redistribute it and/or modify
++    it under the terms of the GNU General Public License as published by
++    the Free Software Foundation, either version 3 of the License, or
++    (at your option) any later version.
++
++    mDictionary is distributed in the hope that it will be useful,
++    but WITHOUT ANY WARRANTY; without even the implied warranty of
++    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
++    GNU General Public License for more details.
++
++    You should have received a copy of the GNU General Public License
++    along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
++
++    Copyright 2010 Comarch S.A.
++
++*******************************************************************************/
++
 +#ifndef TRANSLATIONXDXF_H
 +#define TRANSLATIONXDXF_H
 +
 +#include <QString>
 +#include "../../../includes/translation.h"
 +#include "xdxfplugin.h"
 +
 +class TranslationXdxf : public Translation
 +{
 +public:
 +    TranslationXdxf();
 +    TranslationXdxf(QString _key,QString _dictionaryInfo, XdxfPlugin *xdxfPlugin);
 +
 +    //! \return word to be translated
 +    QString key() const;
 +
 +    //! \returns dictionary information (plugin name, languages, <logo> etc)\
 +    //!    to be displayed in translation table header
 +    QString dictionaryInfo() const;
 +
 +    //! \return parsed raw format into html
 +    QString toHtml() const;
 +
 +    //! sets the word for which we want to find a translation
 +    void setKey(QString);
 +
 +    //! sets information about dictionary
 +    void setDictionaryInfo(QString);
 +
 +
 +private:
 +    QString _key;
 +    QString _dictionaryInfo;
 +    XdxfPlugin *xdxfPlugin;
 +
 +};
 +
 +#endif // TRANSLATIONXDXF_H
 +
index c1c161f,0000000..98cf0d3
mode 100644,000000..100644
--- /dev/null
@@@ -1,146 -1,0 +1,210 @@@
++/*******************************************************************************
++
++    This file is part of mDictionary.
++
++    mDictionary is free software: you can redistribute it and/or modify
++    it under the terms of the GNU General Public License as published by
++    the Free Software Foundation, either version 3 of the License, or
++    (at your option) any later version.
++
++    mDictionary is distributed in the hope that it will be useful,
++    but WITHOUT ANY WARRANTY; without even the implied warranty of
++    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
++    GNU General Public License for more details.
++
++    You should have received a copy of the GNU General Public License
++    along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
++
++    Copyright 2010 Comarch S.A.
++
++*******************************************************************************/
++
 +#include "xdxfplugin.h"
 +#include <QDebug>
 +#include <QFile>
 +#include <QXmlStreamReader>
 +#include <QtPlugin>
 +#include "TranslationXdxf.h"
 +#include "../../../includes/settings.h"
 +
 +XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
 +                    _langFrom(tr("")), _langTo(tr("")),_name(tr("")),
 +                    _type(tr("xdxf")), _infoNote(tr("")) {
-     path="dict.xdxf";
-     stopped = false;
 +    _settings = new Settings();
 +    _dictDialog = new XdxfDictDialog(this);
++    _settings->setValue("type","xdxf");
++    if(isCached())
++        _settings->setValue("Cached","true");
++    else
++        _settings->setValue("Cached","false");
++    setPath("dict.xdxf");
++    stopped = false;
++
 +}
 +
- QString XdxfPlugin::langFrom() const {
-     return  _langFrom;
++QString XdxfPlugin::langFrom() const {   
++    return _langFrom;
 +}
 +
 +QString XdxfPlugin::langTo() const {
 +    return  _langTo;
 +}
 +
 +QString XdxfPlugin::name() const {
 +    return  _name;
 +}
 +
 +QString XdxfPlugin::type() const {
++//    return _settings->value("type");
 +    return _type;
 +}
 +
 +QString XdxfPlugin::infoNote() const {
 +    return  _infoNote;
 +}
 +
- QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
++QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) { 
++    QSet<Translation*> translations;
++    QFile dictionaryFile(path);
++
 +    stopped = false;
++    if(word.indexOf("*")==-1)
++        word+="*";
 +    QRegExp regWord(word);
 +    regWord.setCaseSensitivity(Qt::CaseInsensitive);
 +    regWord.setPatternSyntax(QRegExp::Wildcard);
-     QList<Translation*> translations;
-     QFile dictionaryFile(path);
 +    if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
 +        qDebug()<<"Error: could not open file";
-         return translations;
++        return translations.toList();
 +    }
++
++    /*read information about dictionary*/
 +    QXmlStreamReader dictionaryReader(&dictionaryFile);
 +    dictionaryReader.readNextStartElement();
 +    if(dictionaryReader.name()=="xdxf") {
 +      if(dictionaryReader.attributes().hasAttribute("lang_from"))
 +        _langFrom = dictionaryReader.attributes().value("lang_from").toString();
 +      if(dictionaryReader.attributes().hasAttribute("lang_to"))
 +        _langTo = dictionaryReader.attributes().value("lang_to").toString();
 +    }
 +    dictionaryReader.readNextStartElement();
 +    if(dictionaryReader.name()=="full_name")
 +        _name=dictionaryReader.readElementText();
 +    dictionaryReader.readNextStartElement();
 +    if(dictionaryReader.name()=="description")
 +        _infoNote=dictionaryReader.readElementText();
++
++    /*search words list*/
 +    QString a;
 +    int i=0;
 +    while(!dictionaryReader.atEnd() && !stopped){
 +        dictionaryReader.readNextStartElement();
 +        if(dictionaryReader.name()=="ar"){
 +            while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
 +                dictionaryReader.readNextStartElement();
-           a = dictionaryReader.readElementText();
-           if(regWord.exactMatch(a) && i<limit) {
-                 translations.append(new TranslationXdxf(a,_infoNote,this));
++            if(!dictionaryReader.atEnd())
++                a = dictionaryReader.readElementText();
++            if(regWord.exactMatch(a) && i<limit) {
++                bool ok=true;
++                Translation *tran;
++                foreach(tran,translations)
++                {
++                    if(tran->key()==a)
++                        ok=false;  /*if key word is in the dictionary more that one */
++                }
++                if(ok)  /*add key word to list*/
++                    translations<<(new TranslationXdxf(a,_infoNote,this));
 +                i++;
-                 if(i>=limit)
++                if(i>=limit && limit!=0)
 +                    break;
 +            }
 +        }
 +    }
 +    stopped=false;
 +    dictionaryFile.close();
-     return translations;
++    return translations.toList();
 +}
 +
 +QString XdxfPlugin::search(QString key) {
 +    QFile dictionaryFile(path);
++    QString resultString("");
 +    if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
 +        qDebug()<<"Error: could not open file";
 +        return "";
 +    }
 +    QXmlStreamReader dictionaryReader(&dictionaryFile);
 +
 +    QString a;
 +    bool match =false;
 +    while (!dictionaryReader.atEnd()) {
 +        dictionaryReader.readNext();
 +        if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
 +            if(dictionaryReader.name()=="k") {
 +                a = dictionaryReader.readElementText();
 +                if(a==key)
 +                    match = true;
 +            }
 +        }
 +        else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters)  {
 +            if(match) {
-               QString temp(dictionaryReader.text().toString().replace("\n",""));
-               dictionaryFile.close();
-               return temp;
++                QString temp(dictionaryReader.text().toString());
++                temp.replace("\n","");
++                if(temp == ""){
++                    while(dictionaryReader.name()!="ar" && !dictionaryReader.atEnd()){
++                        dictionaryReader.readNext();
++                        temp+=dictionaryReader.text().toString();
++                    }
++                }
++                resultString+=temp.replace("\n","");
++                match=false;
 +            }
 +        }
 +    }
-     return "";
++    dictionaryFile.close();
++    return resultString;
 +}
 +
 +void XdxfPlugin::stop() {
 +    stopped=true;
 +}
 +
 +DictDialog* XdxfPlugin::dictDialog() {
 +     return _dictDialog;
 +}
 +
++void XdxfPlugin::setPath(QString path){
++    this->path=path;
++    _settings->setValue("path",path);
++}
++
 +
- CommonDictInterface* XdxfPlugin::getNew(const Settings*) const {
-   return new XdxfPlugin();
++CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
++    XdxfPlugin *plugin = new XdxfPlugin();
++    plugin->setPath(settings->value("path"));
++    return  new XdxfPlugin();//plugin;
 +}
 +
 +bool XdxfPlugin::isAvailable() const {
 +    return true;
 +}
 +
 +void XdxfPlugin::setHash(uint _hash)
 +{
 +    this->_hash=_hash;
 +}
 +
 +uint XdxfPlugin::hash() const
 +{
 +   return _hash;
 +}
 +
 +Settings* XdxfPlugin::settings() {
 +    return _settings;
 +}
 +
++bool XdxfPlugin::isCached()
++{
++    return false;
++}
++
 +Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)
index f1f1e1a,0000000..ce2dbca
mode 100644,000000..100644
--- /dev/null
@@@ -1,90 -1,0 +1,112 @@@
++/*******************************************************************************
++
++    This file is part of mDictionary.
++
++    mDictionary is free software: you can redistribute it and/or modify
++    it under the terms of the GNU General Public License as published by
++    the Free Software Foundation, either version 3 of the License, or
++    (at your option) any later version.
++
++    mDictionary is distributed in the hope that it will be useful,
++    but WITHOUT ANY WARRANTY; without even the implied warranty of
++    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
++    GNU General Public License for more details.
++
++    You should have received a copy of the GNU General Public License
++    along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
++
++    Copyright 2010 Comarch S.A.
++
++*******************************************************************************/
++
 +#ifndef XDXFPLUGIN_H
 +#define XDXFPLUGIN_H
 +
 +#include "../../../includes/CommonDictInterface.h"
 +#include <QObject>
 +#include <QDialog>
 +#include <QRegExp>
 +#include <QTime>
 +#include "XdxfDictDialog.h"
 +
 +class TranslationXdxf;
 +
 +class XdxfPlugin : public CommonDictInterface
 +{
 +    Q_OBJECT
 +    Q_INTERFACES(CommonDictInterface)
 +public:
 +    XdxfPlugin(QObject *parent=0);
 +
 +    //! returns source language code iso 639-2
 +    QString langFrom() const;
 +
 +    //! returns destination language code iso 639-2
 +    QString langTo() const;
 +
 +    //! returns dictionary name (like "old english" or so)
 +    QString name() const;
 +
 +    //! returns dictionary type (xdxf, google translate, etc)
 +    QString type() const;
 +
 +    //! returns information about dictionary in html (name, authors, etc)
 +    QString infoNote() const;
 +
 +    /*! returns DictDialog object that creates dialogs
 +        for adding new dictionary and change plugin settings
 +      */
 +    DictDialog* dictDialog();
 +
 +
 +    //! returns new, clean copy of plugin with setting set as in Settings*
 +    CommonDictInterface* getNew(const Settings*) const;
 +
 +    //! returns whether plugin can start searching
 +    bool isAvailable() const;
 +
 +    //! returns a description of a word given by a QString
 +    QString search(QString key);
 +
 +    //! returns a unique hash for a dictionary
 +    uint hash() const;
 +
 +    //! set unique value (unique for every dictionary not plugin)
 +    void setHash(uint);
 +
 +    //! returns current plugin settings
 +    Settings* settings();
 +
 +public Q_SLOTS:
 +    /*! performs search in dictionary
 +      \param  word word to search in dictionary
 +      \param limit limit on number of results
 +
 +      After finishing search it has to emit
 +      \see CommonDictInterface:finalTranslation  finalTranslation
 +    */
 +    QList<Translation*> searchWordList(QString word, int limit);
 +
 +    //! stop current operation
 +    void stop();
 +
 +private:
 +    bool isCached();
++    void setPath(QString);
 +    QString _langFrom;
 +    QString _langTo;
 +    QString _name;
 +    QString _type;
 +    QString _infoNote;
 +    QDialog *_loadDialog;
 +    QDialog *_settingsDialog;
 +    QString path;
 +    uint _hash;
 +    bool stopped;   /*volatile*/
 +    Settings *_settings;
 +    XdxfDictDialog* _dictDialog;
 +};
 +
 +#endif // XDXFPLUGIN_H
 +
 +