implement a lot's of function of googlePlugin
authorJakub Jaszczynski <j.j.jaszczynski@gmail.com>
Tue, 24 Aug 2010 10:15:18 +0000 (12:15 +0200)
committerJakub Jaszczynski <j.j.jaszczynski@gmail.com>
Tue, 24 Aug 2010 10:15:18 +0000 (12:15 +0200)
trunk/src/plugins/google/src/GooglePlugin.cpp
trunk/src/plugins/google/src/GooglePlugin.h
trunk/src/plugins/google/src/TranslationGoogle.cpp [new file with mode: 0644]
trunk/src/plugins/google/src/TranslationGoogle.h [new file with mode: 0644]
trunk/src/plugins/google/src/src.pro
trunk/src/plugins/google/tests/test.cpp
trunk/src/plugins/google/tests/tests.pro
trunk/src/plugins/xdxf/src/xdxfplugin.cpp

index c84e6b8..239ed9d 100644 (file)
 
 #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()
@@ -65,13 +67,29 @@ QString GooglePlugin::infoNote() const {
     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 {
@@ -80,6 +98,24 @@ 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 {
@@ -102,11 +138,36 @@ QIcon* GooglePlugin::icon() {
     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;
 }
index 272afd9..33e779c 100644 (file)
 #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
 {
@@ -58,6 +63,10 @@ public:
     //! 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
       */
@@ -88,7 +97,7 @@ public:
     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
@@ -101,6 +110,9 @@ public Q_SLOTS:
     //! stop current operation
     void stop();
 
+    void done();
+    void started(int);
+
 private:
     void initLanguages();
     QMap<QString, QString> languages;
@@ -121,6 +133,9 @@ private:
     QIcon _icon;
     Settings *_settings;
     bool stopped;
+    volatile bool wait;
+    QHttp *http;
+    GoogleDictDialog *_dictDialog;
 };
 
 #endif // GOOGLEPLUGIN_H
diff --git a/trunk/src/plugins/google/src/TranslationGoogle.cpp b/trunk/src/plugins/google/src/TranslationGoogle.cpp
new file mode 100644 (file)
index 0000000..22dbe2b
--- /dev/null
@@ -0,0 +1,40 @@
+#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;
+}
diff --git a/trunk/src/plugins/google/src/TranslationGoogle.h b/trunk/src/plugins/google/src/TranslationGoogle.h
new file mode 100644 (file)
index 0000000..57e938f
--- /dev/null
@@ -0,0 +1,49 @@
+#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
index 985b5e5..67790e3 100644 (file)
@@ -4,7 +4,7 @@
 #
 #-------------------------------------------------
 
-QT       += core xml gui sql
+QT       += core xml gui network
 
 
 TARGET = GooglePlugin
@@ -29,6 +29,8 @@ DESTDIR = $${MDICT_PLUGINSDIR}/$${MDICT_APPNAME}
 
 SOURCES +=  \
     GooglePlugin.cpp \
+    TranslationGoogle.cpp \
+    GoogleDictDialog.cpp
 
 HEADERS += \
     GooglePlugin.h \
@@ -36,6 +38,8 @@ HEADERS += \
     ../../../includes/translation.h \
     ../../../includes/settings.h \
     ../../../includes/CommonDictInterface.h \
+    TranslationGoogle.h \
+    GoogleDictDialog.h
 
 
 
@@ -61,4 +65,3 @@ unix {
   dicts.path = $$LIBDIR
 
 }
-
index d558d93..17cd944 100644 (file)
 
 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)
index 7a823d0..22afa83 100644 (file)
@@ -3,7 +3,7 @@
 ######################################################################
 
 CONFIG += qtestlib
-QT += sql
+QT += core gui network
 TARGET = GooglrPluginTests
 TEMPLATE = app
 INCLUDEPATH += .
@@ -15,9 +15,11 @@ HEADERS += test.h \
     ../../../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
index ae3549e..407b41a 100644 (file)
@@ -273,14 +273,12 @@ void XdxfPlugin::setPath(QString path){
 
 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);