add getTranslationFor function for googlePlugin
authorJakub Jaszczynski <j.j.jaszczynski@gmail.com>
Tue, 24 Aug 2010 10:18:15 +0000 (12:18 +0200)
committerJakub Jaszczynski <j.j.jaszczynski@gmail.com>
Tue, 24 Aug 2010 10:18:15 +0000 (12:18 +0200)
trunk/src/plugins/google/src/GooglePlugin.cpp
trunk/src/plugins/google/src/GooglePlugin.h
trunk/src/plugins/google/src/TranslationGoogle.cpp
trunk/src/plugins/google/src/TranslationGoogle.h

index 239ed9d..98e174b 100644 (file)
@@ -43,8 +43,8 @@ GooglePlugin::GooglePlugin(QObject *parent): CommonDictInterface(parent),
     connect(http, SIGNAL(done(bool)), this, SLOT(done()));
 }
 
-GooglePlugin::~GooglePlugin()
-{
+GooglePlugin::~GooglePlugin() {
+    delete _settings;
 }
 
 QString GooglePlugin::langFrom() const {
@@ -79,43 +79,13 @@ 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 {
    return true;
 }
 
-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;
+QString GooglePlugin::search(QString key) {
+    /*function is not used on this plagin */
+    return QString("");
 }
 
 uint GooglePlugin::hash() const {
@@ -130,12 +100,35 @@ Settings* GooglePlugin::settings() {
     return _settings;
 }
 
-void GooglePlugin::setSettings(Settings*)
+QIcon* GooglePlugin::icon() {
+    return &_icon;
+}
+
+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));
+    }
+    delete settings;
+    return plugin;
 }
 
-QIcon* GooglePlugin::icon() {
-    return &_icon;
+void GooglePlugin::setSettings(Settings* 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));
+    delete settings;
+}
+
+Translation* getTranslationFor(QString key) {
+    return searchWordList(key);
 }
 
 QList<Translation*> GooglePlugin::searchWordList(QString word, int limit) {
@@ -155,7 +148,10 @@ QList<Translation*> GooglePlugin::searchWordList(QString word, int limit) {
     http->request(head, data);
     while(wait);
 
-    translations<<(new TranslationGoogle(word,_infoNote,this));
+    QString text = QString::fromUtf8(http->readAll());
+    text=tr("<key>") + key + tr("</key>") + tr("<t>") + text + tr("</t>");
+
+    translations<<(new TranslationGoogle(word,text,_infoNote,this));
     return translations;
 }
 
@@ -167,7 +163,6 @@ void GooglePlugin::started(int a) {
     qDebug()<<"test";
 }
 
-
 void GooglePlugin::stop() {
     stopped=true;
 }
@@ -282,4 +277,5 @@ void GooglePlugin::initLanguages() {
     languages["YORUBA"] = "yo";
     languages["UNKNOWN"] = "";
 }
+
 Q_EXPORT_PLUGIN2(google, GooglePlugin)
index 33e779c..a4ba857 100644 (file)
@@ -96,6 +96,8 @@ public:
     //! returns plugin icon
     QIcon* icon();
 
+    Translation* getTranslationFor(QString key);
+
 
 public slots:
     /*! performs search in dictionary
index 22dbe2b..4ccd0c6 100644 (file)
@@ -4,10 +4,9 @@ TranslationGoogle::TranslationGoogle()
 {
 }
 
-TranslationGoogle::TranslationGoogle(QString _key,QString _dictionaryInfo,
-                     GooglePlugin *googlePlugin): _key(_key),
-                     _dictionaryInfo(_dictionaryInfo) {
-
+TranslationGoogle::TranslationGoogle(QString _key,QString _trans,
+                    QString _dictionaryInfo,GooglePlugin *googlePlugin):
+                    _key(_key),_trans(_trans),_dictionaryInfo(_dictionaryInfo) {
     this->googlePlugin=googlePlugin;
     if(googlePlugin)
         _dictHash = googlePlugin->hash();
@@ -27,7 +26,7 @@ QString TranslationGoogle::dictionaryInfo() const {
 QString TranslationGoogle::toHtml() const {
     QString result("");
     result+="<dict> <info bookmark=\"false\">GOOGLE TRANSLATOR </info>";
-    result+=googlePlugin->search(_key) +"</dict>";
+    result+=_trans +"</dict>";
     return result;
 }
 
index 57e938f..e486d71 100644 (file)
@@ -11,7 +11,7 @@ class TranslationGoogle : public Translation
 public:
     TranslationGoogle();
 
-    TranslationGoogle(QString _key,QString _dictionaryInfo, GooglePlugin *googlePlugin);
+    TranslationGoogle(QString _key,QString _trans,QString _dictionaryInfo, GooglePlugin *googlePlugin);
 
     //! \return word to be translated
     QString key() const;
@@ -41,6 +41,7 @@ public:
 private:
     QString _key;
     QString _dictionaryInfo;
+    QString _trans;
     GooglePlugin *googlePlugin;
     int _dictHash;