summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
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