File google-suggest.diff of Package kde4-konqueror-plugins

--- konq-plugins/searchbar/searchbar.cpp	2008/04/14 11:54:37	1.1
+++ konq-plugins/searchbar/searchbar.cpp	2008/04/14 13:15:13
@@ -1,4 +1,6 @@
 /* This file is part of the KDE project
+   Copyright (C) 2005 by Tobi Vollebregt <tobivollebregt@gmail.com>
+   Copyright (C) 2004 by Vinay Khaitan <vkhaitan@iitk.ac.in>
    Copyright (C) 2004 Arend van Beelen jr. <arend@auton.nl>
 
    This program is free software; you can redistribute it and/or
@@ -47,6 +49,12 @@
 //Added by qt3to4:
 #include <QPixmap>
 #include <QMouseEvent>
+#include <QAbstractItemView>
+#include <kselectaction.h>
+
+#include <kprotocolinfo.h>
+#include <kio/job.h>
+
 #include "searchbar.h"
 
 typedef KGenericFactory<SearchBarPlugin> SearchBarPluginFactory;
@@ -60,15 +68,19 @@
   m_searchCombo(0),
   m_searchMode(UseSearchProvider),
   m_urlEnterLock(false),
-  m_process(0)
+  m_process(0),
+  m_gsTimer(this)
+//  m_googleMode(GoogleOnly)
 {
 	m_searchCombo = new SearchBarCombo(0L, "search combo");
 	m_searchCombo->setDuplicatesEnabled(false);
 	m_searchCombo->setMaxCount(5);
 	m_searchCombo->setFixedWidth(180);
 	m_searchCombo->lineEdit()->installEventFilter(this);
+	m_searchCombo->view()->setFocusProxy(m_searchCombo);
 
 	m_popupMenu = 0;
+	m_googleMenu = 0;
 
 	m_searchComboAction = actionCollection()->addAction("toolbar_search_bar");
 	m_searchComboAction->setText(i18n("Search Bar"));
@@ -101,6 +113,11 @@
 		                 SLOT  (partChanged      (KParts::Part*)));
 		partChanged(partMan->activePart());
 	}
+
+	connect(this, SIGNAL(gsCompleteDelayed()), SLOT(gsStartDelay()));
+	connect(&m_gsTimer, SIGNAL(timeout()), SLOT(gsMakeCompletionList()));
+	connect(m_searchCombo->view(), SIGNAL(highlighted(const QString&)), SLOT(gsSetCompletedText(const QString&)));
+	connect(m_searchCombo, SIGNAL(activated(const QString&)), SLOT(gsPutTextInBox(const QString&)));
 }
 
 SearchBarPlugin::~SearchBarPlugin()
@@ -108,6 +125,7 @@
 	KConfigGroup config(KGlobal::config(), "SearchBar");
 	config.writeEntry("Mode", (int) m_searchMode);
 	config.writeEntry("CurrentEngine", m_currentEngine);
+//	config.writeEntry("GoogleSuggestMode", m_googleMode);
 
 	delete m_searchCombo;
 	m_searchCombo = 0L;
@@ -127,6 +145,14 @@
 	if( o==m_searchCombo->lineEdit() && e->type() == QEvent::KeyPress )
 	{
 		QKeyEvent *k = (QKeyEvent *)e;
+		QString text = k->text();
+		if(!text.isEmpty())
+		{
+			if(k->key() != Qt::Key_Return && k->key() != Qt::Key_Enter && k->key() != Qt::Key_Escape)
+			{
+				emit gsCompleteDelayed();
+			}
+		}
 		if(k->state() & Qt::ControlModifier)
 		{
 			if(k->key()==Qt::Key_Down)
@@ -140,6 +166,36 @@
 				return true;
 			}
 		}
+		else
+		{
+			if (k->key() == Qt::Key_Up || k->key() == Qt::Key_Down)
+			{
+				if(m_searchCombo->view()->isVisible())
+				{
+					qApp->sendEvent(m_searchCombo->view(), e);
+					return true;
+				}
+			}
+		}
+		if (k->key() == Qt::Key_Enter || k->key() == Qt::Key_Return)
+		{
+			/*- Fix a bug which caused the searchbar to search for the completed
+			    input instead of the literal input when enter was pressed and
+			    the listbox was visible.
+			if(m_searchCombo->listBox()->isVisible())
+			{
+				qApp->sendEvent(m_searchCombo->listBox(),e);
+			}*/
+		}
+		if (k->key() == Qt::Key_Escape)
+		{
+			m_searchCombo->view()->hide();
+			if (m_searchCombo->lineEdit()->hasSelectedText())
+			{
+				m_searchCombo->lineEdit()->setText(m_searchCombo->currentText().left(m_searchCombo->lineEdit()->selectionStart()));
+			}
+			m_gsTimer.stop();
+		}
 	}
 	return false;
 }
@@ -204,11 +260,16 @@
 	setIcon();
 }
 
-void SearchBarPlugin::startSearch(const QString &search)
+void SearchBarPlugin::startSearch(const QString &_search)
 {
-	if(m_urlEnterLock || search.isEmpty() || !m_part)
+	if(m_urlEnterLock || _search.isEmpty() || !m_part)
 		return;
 
+	m_gsTimer.stop();
+	m_searchCombo->view()->hide();
+
+	QString search = _search.section('(', 0, 0).stripWhiteSpace();
+
 	if(m_searchMode == FindInThisPage)
 	{
 		m_part->findText(search, 0);
@@ -357,10 +418,18 @@
 		}
 
 		m_popupMenu->addSeparator();
+/*		m_googleMenu = new KSelectAction(KIcon("ktip"), i18n("Use Google Suggest"), m_popupMenu);
+		connect(m_googleMenu, SIGNAL(triggered(int)), SLOT(selectGoogleSuggestMode()));
+		QStringList google_modes;
+		google_modes << i18n("For Google Only") << i18n("For All Searches") << i18n("Never");
+		m_googleMenu->setItems(google_modes);
+		m_googleMenu->plug(m_popupMenu);
+*/
 		m_popupMenu->insertItem(SmallIcon("enhanced_browsing"), i18n("Select Search Engines..."),
 			this, SLOT(selectSearchEngines()), 0, 1000);
 		connect(m_popupMenu, SIGNAL(activated(int)), SLOT(useSearchProvider(int)));
 	}
+//	m_googleMenu->setCurrentItem(m_googleMode);
 	m_popupMenu->popup(m_searchCombo->mapToGlobal(QPoint(0, m_searchCombo->height() + 1)), 0);
 }
 
@@ -442,6 +511,7 @@
 	config = KConfigGroup( KGlobal::config(), "SearchBar");
 	m_searchMode = (SearchModes) config.readEntry("Mode", (int) UseSearchProvider);
 	m_currentEngine = config.readEntry("CurrentEngine", engine);
+//	m_googleMode=(GoogleMode)config.readEntry("GoogleSuggestMode", GoogleOnly);
 
 	if ( m_currentEngine.isEmpty() )
 	    m_currentEngine = "google";
@@ -554,4 +624,116 @@
 	}
 }
 
+// Google Suggest code
+
+void SearchBarPlugin::selectGoogleSuggestMode()
+{
+//	m_googleMode = (GoogleMode)m_googleMenu->currentItem();
+//	KConfigGroup config(KGlobal::config(), "SearchBar");
+//	config.writeEntry("GoogleSuggestMode", m_googleMode);
+}
+
+// adapted and modified by Tobi Vollebregt
+// original code from Googlebar by Vinay Khaitan
+
+void SearchBarPlugin::gsStartDelay()
+{
+	m_gsTimer.stop();
+	m_searchCombo->view()->hide();
+	// FIXME: make configurable
+	m_gsTimer.start(500, true);
+}
+
+void SearchBarPlugin::gsMakeCompletionList()
+{
+kDebug() << "*** gsMakeCompletionList";
+//	if ((m_googleMode==GoogleOnly && m_currentEngine != "google") || m_googleMode==Never)
+//		return;
+
+	if (!m_searchCombo->currentText().isEmpty())
+	{
+				KIO::TransferJob* tj =
+				KIO::get(KUrl("http://www.google.com/complete/search?hl=en&js=true&qu=" + m_searchCombo->currentText()), KIO::NoReload, KIO::HideProgressInfo);
+		connect(tj, SIGNAL(data(KIO::Job*, const QByteArray&)), this, SLOT(gsDataArrived(KIO::Job*, const QByteArray&)));
+		connect(tj, SIGNAL(result(KIO::Job*)), this, SLOT(gsJobFinished(KIO::Job*)));
+	}
+}
+
+void SearchBarPlugin::gsDataArrived(KIO::Job*, const QByteArray& data)
+{
+	m_gsData += QString::fromUtf8(data.data());
+}
+
+static QString reformatNumber(const QString& number)
+{
+	static const char suffix[] = { 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' };
+	QString s = number.stripWhiteSpace();
+	uint c = 0;
+	for (int i = s.length() - 1; i > 0 && s[i] == '0'; --i) ++c;
+	c /= 3;
+	if (c >= sizeof(suffix)/sizeof(suffix[0]))
+		c = sizeof(suffix)/sizeof(suffix[0]) - 1;
+	s = s.left(s.length() - c * 3) + suffix[c];
+	return s;
+}
+
+void SearchBarPlugin::gsJobFinished(KIO::Job* job)
+{
+kDebug() << "*** gsJobFinished";
+	if (((KIO::TransferJob*)job)->error() == 0)
+	{
+		QString temp;
+		temp = m_gsData.mid(m_gsData.find('(') + 1, m_gsData.findRev(')') - m_gsData.find('(') - 1);
+		temp = temp.mid(temp.find('(') + 1, temp.find(')') - temp.find('(') - 1);
+		temp.remove('"');
+		QStringList compList1 = QStringList::split(',', temp);
+		temp = m_gsData.mid(m_gsData.find(')') + 1, m_gsData.findRev(')') - m_gsData.find('(') - 1);
+		temp = temp.mid(temp.find('(') + 1, temp.find(')') - temp.find('(') - 1);
+		temp.remove('"');
+		temp.remove(',');
+		temp.remove('s');
+		QStringList compList2 = QStringList::split("reult", temp);
+		QStringList finalList;
+		for(uint j = 0; j < compList1.count(); j++)
+		{
+			if (/*m_googleMode!=ForAll ||*/ m_currentEngine == "google")
+				finalList.append(compList1[j].stripWhiteSpace() + " (" + reformatNumber(compList2[j]) + ")");
+			else
+				finalList.append(compList1[j].stripWhiteSpace());
+		}
+		//store text so that we can restore it if it gets erased after GS returns no results
+		temp = m_searchCombo->currentText();
+//		m_searchCombo->view()->clear();
+//		m_searchCombo->view()->insertStringList(finalList);
+		m_searchCombo->setIcon(m_searchIcon);
+		//restore text
+		m_searchCombo->lineEdit()->setText(temp);
+		if (finalList.count() != 0 && !m_gsTimer.isActive())
+		{
+			m_searchCombo->popup();
+		}
+	}
+	m_gsData = "";
+}
+
+void SearchBarPlugin::gsSetCompletedText(const QString& text)
+{
+	QString currentText;
+	if (m_searchCombo->lineEdit()->hasSelectedText())
+		currentText = m_searchCombo->currentText().left(m_searchCombo->lineEdit()->selectionStart());
+	else
+		currentText = m_searchCombo->currentText();
+	if (currentText == text.left(currentText.length()))
+	{
+		m_searchCombo->lineEdit()->setText(text.left(text.find('(') - 1));
+		m_searchCombo->lineEdit()->setCursorPosition(currentText.length());
+		m_searchCombo->lineEdit()->setSelection(currentText.length(), m_searchCombo->currentText().length() - currentText.length());
+	}
+}
+
+void SearchBarPlugin::gsPutTextInBox(const QString& text)
+{
+	m_searchCombo->lineEdit()->setText(text.section('(', 0, 0).stripWhiteSpace());
+}
+
 #include "searchbar.moc"
--- konq-plugins/searchbar/searchbar.h	2008/04/14 11:54:36	1.1
+++ konq-plugins/searchbar/searchbar.h	2008/04/14 12:54:49
@@ -1,4 +1,6 @@
 /* This file is part of the KDE project
+   Copyright (C) 2005 by Tobi Vollebregt <tobivollebregt@gmail.com>
+   Copyright (C) 2004 by Vinay Khaitan <vkhaitan@iitk.ac.in>
    Copyright (C) 2004 Arend van Beelen jr. <arend@auton.nl>
 
    This program is free software; you can redistribute it and/or
@@ -35,6 +37,9 @@
 
 class KHTMLPart;
 class Q3PopupMenu;
+class QTimer;
+class KSelectAction;
+
 /**
  * Combo box which catches mouse clicks on the pixmap.
  */
@@ -151,6 +156,22 @@
 		void updateComboVisibility();
 
 		void focusSearchbar();
+
+		// Google Suggest private slots
+		void selectGoogleSuggestMode();
+		void gsStartDelay();
+		void gsMakeCompletionList();
+		void gsDataArrived(KIO::Job*, const QByteArray& data);
+		void gsJobFinished(KIO::Job* job);
+		void gsSetCompletedText(const QString& text);
+		void gsPutTextInBox(const QString& text);
+
+	signals:
+
+		// Google Suggest signals
+
+		void gsCompleteDelayed();
+
 	private:
 		void nextSearchEntry();
 		void previousSearchEntry();
@@ -159,6 +180,7 @@
 		SearchBarCombo        *m_searchCombo;
                 QAction         *m_searchComboAction;
 		Q3PopupMenu            *m_popupMenu;
+		KSelectAction         *m_googleMenu;
 		QPixmap                m_searchIcon;
 		SearchModes            m_searchMode;
 		QString                m_providerName;
@@ -166,6 +188,12 @@
 		QString                m_currentEngine;
 		QStringList            m_searchEngines;
 		KProcess              *m_process;
+
+		// Google Suggest private members
+		QTimer                 m_gsTimer;
+		QString                m_gsData;
+//		enum GoogleMode        {GoogleOnly,ForAll,Never};
+//		GoogleMode             m_googleMode;
 };
 
 #endif // SEARCHBAR_PLUGIN
openSUSE Build Service is sponsored by