/*! /file AccentsNormalizer.h
\brief Common functionality for all classes that need to remove accents from strings
-
\author Bartosz Szatkowski <bulislaw@linux.com>
*/
#include <QString>
#include <QRegExp>
-
+//! Common functionality for all classes that need to remove accents from strings
class AccentsNormalizer {
protected:
QMap<QChar, QRegExp> letters;
QRegExp noLetter;
+ //! removes accents from letters in searched word (e.g. ą -> a, ł -> l)
QString removeAccents(QString string) {
string = string.toLower();
QString normalized = string.normalized(QString::NormalizationForm_KD);
return normalized;
}
+ //! initializes the list of pairs (letters, accents)
void initAccents() {
letters['a'] = QRegExp(QString::fromUtf8("[ÀàÁáÂÂâÃãÄäÅåæÆĀāĂ㥹ǠȀǡȁǢȂǣȃȦȧǺȺǻǼǍǽǎǞǟⱥ]"), Qt::CaseInsensitive);
letters['b'] = QRegExp(QString::fromUtf8("[ƀƁƂƃɃƄƅ]"), Qt::CaseInsensitive);
Q_OBJECT
public:
CommonDictInterface(QObject *parent = 0):QObject(parent) {}
-
virtual ~CommonDictInterface() {}
//! returns source language code iso 639-2
for adding new dictionary and changing plugin settings*/
virtual DictDialog* dictDialog() = 0;
-
//! returns new, clean copy of plugin with settings set as in Settings*
virtual CommonDictInterface* getNew(const Settings*) const = 0;
virtual QString search(QString key) = 0;
//! \returns unique value (unique for every dictionary, not plugin)
- virtual uint hash() const = 0;
+ uint hash() const {
+ return _hash;
+ }
//! sets unique value (unique for every dictionary, not plugin)
- virtual void setHash(uint) = 0;
+ void setHash(uint) {
+ this->_hash=_hash;
+ }
//! returns current plugin settings
virtual Settings* settings() = 0;
//! stops current operation
virtual void stop() = 0;
- virtual void retranslate() = 0;
+ //! loads translations for each plugin only once
+ virtual void retranslate() {}
Q_SIGNALS:
-
- //! emitted when dictionary is ready to use afer being loaded
- void loaded(CommonDictInterface*);
-
//! emitted after change in dictionary settings
void settingsChanged();
protected:
+//! removes accents from letters in searched word (e.g. ą -> a, ł -> l)
QString removeAccents(QString string) {
if(settings()->value("strip_accents") == "true")
return AccentsNormalizer::removeAccents(string);
return string;
}
+
+private:
+ uint _hash;
+
};
Q_DECLARE_INTERFACE(CommonDictInterface, "CommonDictInterface/0.1");
*******************************************************************************/
+/*! \file DictDialog.h
+ \brief Interface for plugin dialog provider
+ \author Mateusz Półrola <mateusz.polrola@comarch.com>
+*/
+
+
//Created by Mateusz Półrola
#ifndef DICTDIALOG_H
public:
GUIInterface(QWidget *parent = 0) :QMainWindow(parent) {}
-
virtual ~GUIInterface() {}
//! Returns all loaded dictionaries with information about that they are
It's important for plugin to store all information given to it in Settings.*/
class Settings {
public:
- Settings(){}
+ Settings() {}
+ ~Settings() {}
Settings(const Settings* set) {
_settings = QHash<QString, QString>(set->_settings);
}
- ~Settings(){}
/*! \returns value fo given key
- \param key
+ \param key name of given setting
+ \return value of given setting
*/
QString value(const QString key) const {
if(!_settings.contains(key)) {
return _settings[key];
}
- //! sets key to value
+ /*! sets key to value
+ \param key name of given setting
+ \param value of given setting
+ */
void setValue(const QString key, const QString value) {
_settings.insert(key, value);
}
+ //! \returns list of keys (QList<QString>)
QList<QString> keys() const {
return _settings.keys();
}
private:
+ //! keeps the settings
QHash<QString, QString> _settings;
};
public:
Translation () { _bookmark = 0;}
virtual ~Translation () {}
+
//! \return word to be translated
virtual QString key() const = 0;
+ //! compares two translations by key
bool operator==(Translation* translation) {
return this->key()==translation->key();
}
//! returns coresponding dict object
virtual uint dict() const {return 0;}
- protected:
- bool _bookmark;
+ protected:
+ //! indicates whether this translation is in bookmark
+ bool _bookmark;
};
\param word for which we want to find a translation */
void setKey(QString key) {
_key = key;
- };
+ }
//! sets information about dictionary
void setDictionaryInfo(QString dictionaryInfo) {
QStringList Bookmarks::search(QString word, QString dbName) {
- checkAndCreateDb();
QStringList result;
QSqlDatabase db = getDbCnx(dbName);
if(!db.isOpen() && !db.open()) {
_settings->setValue("connection_accepted","false");
}
+
bool GooglePlugin::isConnectionAccept() const {
if(_settings->value("connection_accepted")=="true")
return true;
}
-uint GooglePlugin::hash() const {
- return _hash;
-}
-
-
-void GooglePlugin::setHash(uint _hash) {
- this->_hash=_hash;
-}
-
-
Settings* GooglePlugin::settings() {
-/* Settings *returnSettings=new Settings;
- QStringList list = _settings->keys();
- foreach(QString key, list)
- returnSettings->setValue(key,_settings->value(key));
- return returnSettings;
-*/
return _settings;
}
//! returns information about dictionary in html (name, authors, etc)
QString infoNote() const;
+ //! sets the language to which the translation is done
void setLangTo(QString langTo);
+ //! sets the language from which the translation is done
void setLangFrom(QString langFrom);
/*! returns DictDialog object that creates dialogs
//! returns whether plugin can start searching
bool isAvailable() const;
+ //! sets if connection with Internet is possible
void setConnectionAccept(QString connectionAcepted);
+ //! returns the value of "connection_accepted" from settings
bool isConnectionAccept() 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;
-
- //! sets unique value (unique for every dictionary, not plugin)
- void setHash(uint);
-
//! returns current plugin settings
Settings* settings();
//! returns plugin icon
QIcon* icon();
+ //! returns empty translation object (to be fetched later) for given key
Translation* getTranslationFor(QString key);
+ //! initializes the list of available languages in Google translator
static QMap<QString, QString> initLanguages();
public slots:
//! stop current operation
void stop();
+ //! function called after the request from Google is returned
void done();
+ //! transforms Google format to String with translation
QString jsonParse(QString result);
+
+ //! sets information about dictionary
void getDictionaryInfo();
+ //! loads translations for each plugin only once
void retranslate();
private:
+ //! maps languages full names to two-letter acronyms
QMap<QString, QString> languages;
+
+ //! name of a dictionary
QString _name;
//! type of a dictionary
QString _type;
//! information about dictionary
QString _infoNote;
+ //! unique number of a dictionary assigned to it by backbone
uint _hash;
+ //! icon displayed during translations and when the dictionary is chosen
QIcon _icon;
Settings *_settings;
+ //! indicates if search is stopped
bool stopped;
bool _connectionAccept;
+ //! indicates if response from Google appeared
volatile bool wait;
QHttp *http;
GoogleDictDialog *_dictDialog;
QString langTo();
private slots:
+ //! assigns the language chosen from a list(langFromComboBox) to _langFrom
void activatedFrom(int);
+ //! assigns the language chosen from a list(langToComboBox) to _langTo
void activatedTo(int);
+ //! handles the "swap languages" button
void changeLangButtonClicked();
QLabel* connectInfoLabel;
QPushButton* saveButton;
QPushButton* changeLangButton;
-
-
QComboBox *langFromComboBox;
QComboBox *langToComboBox;
-
QVBoxLayout* verticalLayout;
QVBoxLayout* langLayout;
QHBoxLayout* langFromLayout;
QHBoxLayout* langToLayout;
QHBoxLayout* changelangLayout;
-
QString _langFrom;
QString _langTo;
-
-
-/* QPushButton* browseButton;
- QLabel* browseLabel;
- QCheckBox* cacheCheckBox;
-
- QHBoxLayout* browseLayout;
- QHBoxLayout* cacheLayout;
-*/
};
#endif // GOOGLESETTINGSDIALOG_H
QString _key;
QString _dictionaryInfo;
QString _trans;
- GooglePlugin *googlePlugin;
int _dictHash;
bool error;
-
+ GooglePlugin *googlePlugin;
};
#endif // TRANSLATIONGOOGLE_H
QString result("");
if(!xdxfPlugin)
return result;
- result=result + "<dict>" + "<info";
+
+ result=result + "<dict> <info path=\"/usr/share/mdictionary/xdxf.png\" ";
if(isBookmark())
- result+= " bookmark=\"true\" ";
+ result+= " bookmark=\"true\" > \n";
else
- result+= " bookmark=\"false\" ";
+ result+= " bookmark=\"false\" > \n";
result+= _dictionaryInfo + "</info>" + xdxfPlugin->search(_key) + "</dict>";
return result.replace("&","&");
}
}
-void XdxfCachingDialog::reject() {
- return;
-}
-
void XdxfCachingDialog::cancelButtonClicked(){
return;
}
explicit XdxfCachingDialog(XdxfPlugin *parent = 0);
Q_SIGNALS:
+ //! signal emitted when user cancels caching of a dictionary
void cancelCaching();
-public Q_SLOTS:
- void reject();
-
private Q_SLOTS:
void cancelButtonClicked();
void updateCachingProgress(int, int);
QProgressBar* cachingProgressBar;
QPushButton* cancelButton;
QVBoxLayout* verticalLayout;
+ //! used to check the progress of dictionary caching
QTime time;
};
if(!_dicitonaryFilePath.isEmpty()) {
accept();
}
- else {
- reject();
- }
}
QString XdxfLoadDialog::dicitonaryFilePath() {
//! Returns if user wants to cache dictionary
bool generateCache();
-signals:
-
-public slots:
-
private Q_SLOTS:
void selectFile();
void addDictionary();
Q_OBJECT
public:
explicit XdxfSettingsDialog(XdxfPlugin* plugin, QWidget *parent = 0);
-
+ //! \returns settings of a given plugin
static Settings* getSettings(XdxfPlugin* plugin, QWidget *parent);
- //! Returns dictionary file path chosen by user
+ //! \returns dictionary file path chosen by user
QString dicitonaryFilePath();
- //! Returns if user wants to cache dictionary
+ //! \returns if user wants to cache dictionary
bool generateCache();
private Q_SLOTS:
QString XdxfPlugin::infoNote() const {
- return _infoNote;
+ return _infoNote;
}
if(ok) { /*add key word to list*/
translations.insert(new TranslationXdxf(
cur.value(0).toString().toLower(),
- _infoNote, this));
+ _dictionaryInfo, this));
i++;
}
}
}
if(ok) { /*add key word to list*/
translations<<(new TranslationXdxf(readKey.toLower(),
- _infoNote,this));
+ _dictionaryInfo,this));
i++;
}
if(i>=limit && limit!=0)
}
-void XdxfPlugin::setHash(uint _hash) {
- this->_hash=_hash;
-}
-
-
-uint XdxfPlugin::hash() const {
- return _hash;
-}
-
-
Settings* XdxfPlugin::settings() {
-/*
- Settings *returnSettings=new Settings;
- QStringList list = _settings->keys();
- foreach(QString key, list)
- returnSettings->setValue(key,_settings->value(key));
- return returnSettings;
-*/
return _settings;
}
if(reader.name()=="description")
_infoNote=reader.readElementText();
- QString initialPath = "/usr/share/mdictionary/xdxf.png";
-
- _infoNote=" path=\""+initialPath+"\"> \n" + _name + " [" + _langFrom + "-"
+ _dictionaryInfo= _name + " [" + _langFrom + "-"
+ _langTo + "]";
+
dictionaryFile.close();
if(okFormat)
return true;
//! returns a description of a word given by a QString
QString search(QString key);
- //! returns a unique hash for a dictionary
- uint hash() const;
-
- //! sets unique value (unique for every dictionary, not plugin)
- void setHash(uint);
-
//! returns current plugin settings
Settings* settings();
//! stop current operation
void stop();
+ //! loads translations for each plugin only once
void retranslate();
private:
-/*! returns true or false depending on whether the dictionary is cached
- or not, not implemented yet
- */
- bool isCached();
-
-//! sets the path to dictionary file and adds it to settings
- // void setPath(QString);
+ /*! returns true or false depending on whether the dictionary is cached
+ or not, not implemented yet
+ */
+ bool isCached();
+ /*! searches for a list of words similar to a word in a database file
+ \param word key compared with keys in a database
+ \param limit limits the number of translations in returned list,
+ 0 means unlimited
+ \returns list of translations
+ */
QList<Translation*> searchWordListCache(QString word, int limit=0);
+ /*! searches for a list of words similar to a word in a xdxf file
+ \param word key compared with keys in a xdxf file
+ \param limit limits the number of translations in returned list,
+ 0 means unlimited
+ \returns list of translations
+ */
QList<Translation*> searchWordListFile(QString word, int limit=0);
+ /*! searches for a translation of a word which is exactly like a key
+ in xdxf file */
QString searchFile(QString key);
+ /*! searches for a translation of a word which is exactly like a key
+ in database file */
QString searchCache(QString key);
//! scans dictionary file to get information about it
bool getDictionaryInfo();
+ //! counts the keys in xdxf file
int countWords();
+ /*! transforms xdxf files to database files (caching operation)
+ \returns true on success, false on failure */
bool makeCache(QString dir);
//! language from which we translate
QString _name;
//! information about dictionary
QString _infoNote;
- //! path to dictionary file
- // QString path;
+
+ QString _dictionaryInfo;
+
+ //! unique number of a dictionary assigned to it by backbone
uint _hash;
+ //! icon displayed during translations and when the dictionary is chosen
QIcon _icon;
QSqlDatabase db;
QString db_name;
//! number of words in dictionary
long _wordsCount;
+ //! indicates if search is stopped
volatile bool stopped;
Settings *_settings;
XdxfDictDialog* _dictDialog;
QCOMPARE(plugin.infoNote(),QString(" [Polish-English] " ));
}
+void GoogleTest::langFrom() {
+ GooglePlugin plugin;
+ Settings *settings=new Settings;
+ settings->setValue("lang_to","pl");
+ settings->setValue("lang_from","en");
+ plugin.setSettings(settings);
+ delete settings;
+
+ QCOMPARE(plugin.langFrom(),QString("English"));
+ QCOMPARE(plugin.langTo(),QString("Polish"));
+ QCOMPARE(plugin.type(),QString("google"));
+}
+
+void GoogleTest::getNew() {
+ GooglePlugin plugin;
+ Settings *settings=new Settings;
+ settings->setValue("lang_to","pl");
+ settings->setValue("lang_from","en");
+ CommonDictInterface *test=plugin.getNew(settings);
+ delete settings;
+
+ QCOMPARE(test->langFrom(),QString("English"));
+ QCOMPARE(test->langTo(),QString("Polish"));
+ QCOMPARE(test->type(),QString("google"));
+
+ delete test;
+}
+
QTEST_MAIN(GoogleTest)
private slots:
void jsonParse();
void getDictionaryInfo();
+ void langFrom();
+ void getNew();
};
QCOMPARE(te11.at(0)->key(), QString("house"));
xdxfPlugin->clean();
+ QList<Translation*> te12 = xdxfPlugin->searchWordList("1",10);
+ QCOMPARE(te2.size(), 5);
+
delete xdxfPlugin;
}
delete settings;
QCOMPARE(xdxfPlugin->langFrom(), QString("ENG"));
+ QCOMPARE(xdxfPlugin->langTo(), QString("POL"));
+ QCOMPARE(xdxfPlugin->name(), QString("English-Polish dictionary"));
+ QCOMPARE(xdxfPlugin->infoNote(), QString("Copyright: http://www.freelang.net/dictionary/; Version: 1.0"));
+ QCOMPARE(xdxfPlugin->type(), QString("xdxf"));
xdxfPlugin->clean();
delete xdxfPlugin;
}
+void XdxfTest::isAvailable() {
+ XdxfPlugin xdxfPluginB(this);
+ Settings *settings=new Settings;
+ settings->setValue("path","../XdxfPluginTests/dict.xdxf");
+ CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
+ delete settings;
+
+ QCOMPARE(xdxfPlugin->isAvailable(), true);
+
+ xdxfPlugin->clean();
+ delete xdxfPlugin;
+}
+
+void XdxfTest::removeAccents() {
+ qDebug()<<"\n";
+ XdxfPluginSub xdxf;
+ xdxf.settings()->setValue(QString("strip_accents"), QString("true"));
+
+ QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("nóżka")), QString("nozka"));
+ QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("motor")), QString("motor"));
+ QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("nÓżKa")), QString("nozka"));
+ QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("ławka")), QString("lawka"));
+ QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("éàèùâêîôûëïüÿäöüç")), QString("eaeuaeioueiuyaouc"));
+ QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("íőűúó")), QString("iouuo"));
+ QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("-ę")), QString("-e"));
+ QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("\"e\"")), QString("\"e\""));
+ QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("'e'")), QString("'e'"));
+ QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("\\e")), QString("e"));
+ QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("\\")), QString(""));
+}
+
void XdxfTest::timeCache() {
qDebug()<<"\n";
QTime timer;
qDebug()<<"Error: could not open file";
return;
}
- if(!QFile::exists("../../../../../../dict.xdxf")) {
+ if(!QFile::exists("../../../dict.xdxf")) {
qDebug()<<"Error: could not open dict file";
return;
}
timer.start();
XdxfPlugin xdxfPluginB(this);
Settings *settings=new Settings;
- settings->setValue("path","../../../../../../dict.xdxf");
+ settings->setValue("path","../../../dict.xdxf");
settings->setValue("generateCache", "true");
CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
delete settings;
void XdxfTest::timeFile() {
qDebug()<<"\n";
QTime timer;
- QDate date;
QFile File("../XdxfPluginTests/time.xml");
if(!File.open(QFile::ReadWrite | QFile::Text)) {
qDebug()<<"Error: could not open file";
return;
}
- if(!QFile::exists("../../../../../../dict.xdxf")) {
+ if(!QFile::exists("../../../dict.xdxf")) {
qDebug()<<"Error: could not open dict file";
return;
}
XdxfPlugin xdxfPluginB(this);
Settings *settings=new Settings;
- if(!QFile::exists("../../../../../../dict.xdxf"))
+ if(!QFile::exists("../../../dict.xdxf"))
return;
- settings->setValue("path","../../../../../../dict.xdxf");
+ settings->setValue("path","../../../dict.xdxf");
CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
delete settings;
void XdxfTest::timeCacheNormalize() {
qDebug()<<"\n";
QTime timer;
- QDate date;
QFile File("../XdxfPluginTests/time.xml");
if(!File.open(QFile::ReadWrite | QFile::Text)) {
qDebug()<<"Error: could not open file";
return;
}
- if(!QFile::exists("../../../../../../dict.xdxf")) {
+ if(!QFile::exists("../../../dict.xdxf")) {
qDebug()<<"Error: could not open dict file";
return;
}
XdxfPlugin xdxfPluginB(this);
Settings *settings=new Settings;
- settings->setValue("path","../../../../../../dict.xdxf");
+ settings->setValue("path","../../../dict.xdxf");
settings->setValue("generateCache", "true");
settings->setValue("strip_accents", "true");
CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
delete xdxfPlugin;
}
-void XdxfTest::removeAccents() {
- qDebug()<<"\n";
- XdxfPluginSub xdxf;
- xdxf.settings()->setValue(QString("strip_accents"), QString("true"));
-
- QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("nóżka")), QString("nozka"));
- QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("motor")), QString("motor"));
- QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("nÓżKa")), QString("nozka"));
- QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("ławka")), QString("lawka"));
- QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("éàèùâêîôûëïüÿäöüç")), QString("eaeuaeioueiuyaouc"));
- QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("íőűúó")), QString("iouuo"));
- QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("-ę")), QString("-e"));
- QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("\"e\"")), QString("\"e\""));
- QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("'e'")), QString("'e'"));
- QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("\\e")), QString("e"));
- QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("\\")), QString(""));
-}
-
void XdxfTest::timeFileNormalize(){
qDebug()<<"\n";
QTime timer;
- QDate date;
QFile File("../XdxfPluginTests/time.xml");
if(!File.open(QFile::ReadWrite | QFile::Text)) {
qDebug()<<"Error: could not open file";
return;
}
- if(!QFile::exists("../../../../../../dict.xdxf")) {
+ if(!QFile::exists("../../../dict.xdxf")) {
qDebug()<<"Error: could not open dict file";
return;
}
XdxfPlugin xdxfPluginB(this);
Settings *settings=new Settings;
- settings->setValue("path","../../../../../../dict.xdxf");
+ settings->setValue("path","../../../dict.xdxf");
settings->setValue("strip_accents", "true");
CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
delete settings;
void searchCache();
void searchWordListCache();
void removeAccents();
+ void isAvailable();
void timeCache();
void timeFile();
void timeCacheNormalize();
QDialog* settingsDialog() {return 0;}
CommonDictInterface* getNew(const Settings *) const { return 0;}
bool isAvailable() const {return available;}
- uint hash() const { return _hash;}
- void setHash(uint h) {_hash = h;}
void stop() {stopped = 1;}
QIcon* icon(){return 0;}
QString search(QString key) {return "";}