summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrea Diamantini <adjam7@gmail.com>2009-03-24 11:04:50 +0100
committerAndrea Diamantini <adjam7@gmail.com>2009-03-24 11:04:50 +0100
commitc77009fa70847fc12e1b0dbb9826513b22d27e12 (patch)
tree49e92ee7d42a18096c6703705b4188c0c13b0ffc /src
parentupdated TODO (diff)
downloadrekonq-c77009fa70847fc12e1b0dbb9826513b22d27e12.tar.xz
Added Google Suggest. Thanks, Ariyagit status
Diffstat (limited to 'src')
-rw-r--r--src/searchbar.cpp54
-rw-r--r--src/searchbar.h18
2 files changed, 62 insertions, 10 deletions
diff --git a/src/searchbar.cpp b/src/searchbar.cpp
index 7d599bfb..80b231ac 100644
--- a/src/searchbar.cpp
+++ b/src/searchbar.cpp
@@ -33,6 +33,7 @@
// Qt Includes
#include <QtCore>
#include <QtGui>
+#include <QtNetwork>
SearchBar::SearchBar(QWidget *parent) :
@@ -50,12 +51,20 @@ SearchBar::SearchBar(QWidget *parent) :
setClearButtonShown( true );
- QPalette p;
- p.setColor( QPalette::Text , Qt::lightGray );
- setPalette( p );
- setText( i18n("Search..") );
+ // better solution than using QPalette(s)..
+ setClickMessage( i18n("Search..") );
+
+ // setting QNetworkAccessManager..
+ netMan = new QNetworkAccessManager(this);
+ connect(netMan, SIGNAL(finished(QNetworkReply*)), this, SLOT(handleNetworkData(QNetworkReply*)));
- connect( this, SIGNAL( returnPressed() ) , this , SLOT( searchNow() ) );
+ timer = new QTimer(this);
+ timer->setSingleShot(true);
+ timer->setInterval(300);
+ connect(timer, SIGNAL(timeout()), SLOT(autoSuggest()));
+ connect(this, SIGNAL(textEdited(QString)), timer, SLOT(start()));
+
+ connect(this, SIGNAL( returnPressed() ) , this , SLOT( searchNow() ) );
}
@@ -66,6 +75,7 @@ SearchBar::~SearchBar()
void SearchBar::searchNow()
{
+ timer->stop();
QString searchText = text();
KUrl url(QLatin1String("http://www.google.com/search"));
@@ -87,3 +97,37 @@ void SearchBar::focusInEvent(QFocusEvent *event)
clear();
}
+
+void SearchBar::autoSuggest()
+{
+ QString str = text();
+ QString url = QString("http://google.com/complete/search?output=toolbar&q=%1").arg(str);
+ netMan->get(QNetworkRequest(QString(url)));
+}
+
+
+void SearchBar::handleNetworkData(QNetworkReply *networkReply)
+{
+ QUrl url = networkReply->url();
+ if (!networkReply->error())
+ {
+ QStringList choices;
+
+ QString response(networkReply->readAll());
+ QXmlStreamReader xml(response);
+ while (!xml.atEnd())
+ {
+ xml.readNext();
+ if (xml.tokenType() == QXmlStreamReader::StartElement)
+ if (xml.name() == "suggestion")
+ {
+ QStringRef str = xml.attributes().value("data");
+ choices << str.toString();
+ }
+ }
+
+ setCompletedItems(choices, true);
+ }
+
+ networkReply->deleteLater();
+} \ No newline at end of file
diff --git a/src/searchbar.h b/src/searchbar.h
index 363ab35b..5ec81885 100644
--- a/src/searchbar.h
+++ b/src/searchbar.h
@@ -28,7 +28,9 @@
// Forward Declarations
class KUrl;
class QFocusEvent;
-
+class QTimer;
+class QNetworkAccessManager;
+class QNetworkReply;
/**
* This class defines an internet search bar.
@@ -42,10 +44,13 @@ public:
~SearchBar();
public slots:
-/**
- * Use this slot to perform one search in one search engine
- *
- */
+ void autoSuggest();
+ void handleNetworkData(QNetworkReply *networkReply);
+
+ /**
+ * Use this slot to perform one search in one search engine
+ *
+ */
void searchNow();
protected:
@@ -54,6 +59,9 @@ protected:
signals:
void search(const KUrl &url);
+private:
+ QTimer *timer;
+ QNetworkAccessManager *netMan;
};
#endif