#include "GooglePlugin.h"
#include <QDebug>
+#include "GoogleDictDialog.h"S
-#include <QHttp>
GooglePlugin::GooglePlugin(QObject *parent): CommonDictInterface(parent),
_langFrom(tr("")), _langTo(tr("")),_name(tr("")),
_type(tr("google")), _infoNote(tr("")) {
stopped = false;
+ _settings = new Settings();
+ _dictDialog = new GoogleDictDialog(this,this);
+
//_icon = QIcon(":/icon/google.png");
initLanguages();
-
+ _settings->setValue("type","google");
http = new QHttp(this);
connect(http, SIGNAL(done(bool)), this, SLOT(done()));
-
}
GooglePlugin::~GooglePlugin()
return _infoNote;
}
-DictDialog* GooglePlugin::dictDialog()
-{
+void GooglePlugin::setLangTo(QString langTo){
+ _langTo=langTo;
}
-CommonDictInterface* GooglePlugin::getNew(const Settings*) const
-{
+void GooglePlugin::setLangFrom(QString langFrom){
+ _langFrom=langFrom;
+}
+
+DictDialog* GooglePlugin::dictDialog() {
+ return _dictDialog;
+}
+CommonDictInterface* GooglePlugin::getNew(const Settings* settings) const
+{
+ GooglePlugin *plugin = new GooglePlugin();
+ if(settings) {
+ plugin->setLangFrom(settings->value("langFrom"));
+ plugin->setLangTo(settings->value("langTo"));
+ QStringList list = settings->keys();
+ foreach(QString key, list)
+ plugin->settings()->setValue(key, settings->value(key));
+ }
+ return plugin;
}
bool GooglePlugin::isAvailable() const {
QString GooglePlugin::search(QString key)
{
+ QString url = QString("/translate_a/t?client=t&sl=%1+&tl=%2").arg(_langFrom,_langTo);
+ QHttpRequestHeader head = QHttpRequestHeader("POST", url, 1,1);
+ head.setValue("Host", "www.google.pl");
+ head.setValue("User-Agent", "Mozilla/5.0");
+ head.setValue("Accept-Encoding", "deflate");
+ head.setContentLength(key.length());
+ head.setValue("Connection", "Close");
+
+ QByteArray data("text=");
+ data.append(key.toUtf8());
+ http->setHost("www.google.pl");
+ wait=true;
+ http->request(head, data);
+ while(wait);
+
+ QString text = QString::fromUtf8(http->readAll());
+ text=tr("<key>") + key + tr("</key>") + tr("<t>") + text + tr("</t>");
+ return text;
}
uint GooglePlugin::hash() const {
return &_icon;
}
-QList<Translation*> GooglePlugin::searchWordList(QString word, int limit)
-{
+QList<Translation*> GooglePlugin::searchWordList(QString word, int limit) {
+ QList<Translation*> translations;
+ QString url = QString("/translate_a/t?client=t&sl=%1+&tl=%2").arg(_langFrom,_langTo);
+ QHttpRequestHeader head = QHttpRequestHeader("POST", url, 1,1);
+ head.setValue("Host", "www.google.pl");
+ head.setValue("User-Agent", "Mozilla/5.0");
+ head.setValue("Accept-Encoding", "deflate");
+ head.setContentLength(word.length());
+ head.setValue("Connection", "Close");
+
+ QByteArray data("text=");
+ data.append(word.toUtf8());
+ http->setHost("www.google.pl");
+ wait=true;
+ http->request(head, data);
+ while(wait);
+
+ translations<<(new TranslationGoogle(word,_infoNote,this));
+ return translations;
+}
+void GooglePlugin::done() {
+ wait=false;
}
+void GooglePlugin::started(int a) {
+ qDebug()<<"test";
+}
+
+
void GooglePlugin::stop() {
stopped=true;
}
#include <QDialog>
#include <QtPlugin>
#include <QIcon>
+#include <QtNetwork>
#include "../../../includes/CommonDictInterface.h"
#include "../../../includes/settings.h"
+#include "../../../includes/DictDialog.h"
+#include "TranslationGoogle.h"
+#include "GoogleDictDialog.h"
+class GoogleDictDialog;
class GooglePlugin : public CommonDictInterface
{
//! returns information about dictionary in html (name, authors, etc)
QString infoNote() const;
+ void setLangTo(QString langTo);
+
+ void setLangFrom(QString langFrom);
+
/*! returns DictDialog object that creates dialogs
for adding new dictionary and change plugin tings
*/
QIcon* icon();
-public Q_SLOTS:
+public slots:
/*! performs search in dictionary
\param word word to search in dictionary
\param limit limit on number of results
//! stop current operation
void stop();
+ void done();
+ void started(int);
+
private:
void initLanguages();
QMap<QString, QString> languages;
QIcon _icon;
Settings *_settings;
bool stopped;
+ volatile bool wait;
+ QHttp *http;
+ GoogleDictDialog *_dictDialog;
};
#endif // GOOGLEPLUGIN_H
--- /dev/null
+#include "TranslationGoogle.h"
+
+TranslationGoogle::TranslationGoogle()
+{
+}
+
+TranslationGoogle::TranslationGoogle(QString _key,QString _dictionaryInfo,
+ GooglePlugin *googlePlugin): _key(_key),
+ _dictionaryInfo(_dictionaryInfo) {
+
+ this->googlePlugin=googlePlugin;
+ if(googlePlugin)
+ _dictHash = googlePlugin->hash();
+
+}
+
+QString TranslationGoogle::key() const{
+ return _key;
+}
+
+
+QString TranslationGoogle::dictionaryInfo() const {
+ return _dictionaryInfo;
+}
+
+
+QString TranslationGoogle::toHtml() const {
+ QString result("");
+ result+="<dict> <info bookmark=\"false\">GOOGLE TRANSLATOR </info>";
+ result+=googlePlugin->search(_key) +"</dict>";
+ return result;
+}
+
+void TranslationGoogle::setKey(QString) {
+ this->_key=_key;
+}
+
+void TranslationGoogle::setDictionaryInfo(QString){
+ this->_dictionaryInfo=_dictionaryInfo;
+}
--- /dev/null
+#ifndef TRANSLATIONGOOGLE_H
+#define TRANSLATIONGOOGLE_H
+
+#include "../../../includes/translation.h"
+#include "GooglePlugin.h"
+
+class GooglePlugin;
+
+class TranslationGoogle : public Translation
+{
+public:
+ TranslationGoogle();
+
+ TranslationGoogle(QString _key,QString _dictionaryInfo, GooglePlugin *googlePlugin);
+
+ //! \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
+ \param word for which we want to find a translation */
+ void setKey(QString);
+
+ //! sets information about dictionary
+ void setDictionaryInfo(QString);
+
+ //! \retrun whether given translation is taken from bookmarks
+ bool isBookmark() const {
+ return _bookmark;
+ }
+
+ //! returns coresponding dict object
+ uint dict() const {return _dictHash;}
+
+private:
+ QString _key;
+ QString _dictionaryInfo;
+ GooglePlugin *googlePlugin;
+ int _dictHash;
+
+};
+
+#endif // TRANSLATIONGOOGLE_H
#
#-------------------------------------------------
-QT += core xml gui sql
+QT += core xml gui network
TARGET = GooglePlugin
SOURCES += \
GooglePlugin.cpp \
+ TranslationGoogle.cpp \
+ GoogleDictDialog.cpp
HEADERS += \
GooglePlugin.h \
../../../includes/translation.h \
../../../includes/settings.h \
../../../includes/CommonDictInterface.h \
+ TranslationGoogle.h \
+ GoogleDictDialog.h
dicts.path = $$LIBDIR
}
-
void GoogleTest::getNew() {
-GooglePlugin test;
+ GooglePlugin googlePlugin(this);
+ Settings *settings=new Settings;
+ settings->setValue("langFrom","pl");
+ settings->setValue("langTo","en");
+
+
+ CommonDictInterface *googlePlugin2 = googlePlugin.getNew(settings);
+ QSignalSpy signa( googlePlugin2, SIGNAL( done() ) );
+ QList<Translation*> te=googlePlugin2->searchWordList("kot",8);
+
+
+
+ while(1)
+ qDebug()<<signa.count();
}
QTEST_MAIN(GoogleTest)
######################################################################
CONFIG += qtestlib
-QT += sql
+QT += core gui network
TARGET = GooglrPluginTests
TEMPLATE = app
INCLUDEPATH += .
../../../includes/settings.h \
../../../includes/DictDialog.h \
../../../includes/CommonDictInterface.h \
+ ../src/TranslationGoogle.h
SOURCES += test.cpp \
../src/GooglePlugin.cpp \
+ ../src/TranslationGoogle.cpp
check.target = check
check.commands = ./GooglePluginTests
CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
XdxfPlugin *plugin = new XdxfPlugin();
- static int a=0;
if(settings){
plugin->setPath(settings->value("path"));
QStringList list = settings->keys();
foreach(QString key, list)
plugin->settings()->setValue(key, settings->value(key));
- a=a+1;
plugin->db_name = plugin->_settings->value("type")
+ plugin->_settings->value("path");
plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);