From 3ed265b387092e2f112ea1c9933501b3bf64d551 Mon Sep 17 00:00:00 2001 From: Furkan Uzumcu Date: Sun, 18 Sep 2011 23:32:47 +0200 Subject: Change "Paste & Go" action's text to "Paste & Search" if the content of the clipboard is not an URL REVIEW: 102441 REVIEWED-BY: adjam --- src/urlbar/urlbar.cpp | 33 +++++++++++++++++++++++++++++++-- src/urlbar/urlbar.h | 2 ++ 2 files changed, 33 insertions(+), 2 deletions(-) (limited to 'src/urlbar') diff --git a/src/urlbar/urlbar.cpp b/src/urlbar/urlbar.cpp index e3f216d8..04e48d1b 100644 --- a/src/urlbar/urlbar.cpp +++ b/src/urlbar/urlbar.cpp @@ -46,6 +46,7 @@ #include "bookmarkwidget.h" #include "iconmanager.h" #include "favoritewidget.h" +#include "searchengine.h" // KDE Includes #include @@ -482,8 +483,17 @@ void UrlBar::contextMenuEvent(QContextMenuEvent* event) menu.addAction(a); // Paste & Go - a = new KAction(i18n("Paste && Go"), this); - connect(a, SIGNAL(triggered(bool)), this, SLOT(pasteAndGo())); + const QString clipboardText = rApp->clipboard()->text(); + if (isValidURL(clipboardText) || clipboardText.isEmpty()) + { + a = new KAction(i18n("Paste && Go"), this); + connect(a, SIGNAL(triggered(bool)), this, SLOT(pasteAndGo())); + } + else + { + a = new KAction(i18n("Paste && Search"), this); + connect(a, SIGNAL(triggered(bool)), this, SLOT(pasteAndSearch())); + } a->setEnabled(clipboardFilled); menu.addAction(a); @@ -504,6 +514,17 @@ void UrlBar::contextMenuEvent(QContextMenuEvent* event) } +bool UrlBar::isValidURL(QString url) +{ + bool isValid = false; + if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("ftp://")) + url = url.remove(QRegExp("(http|https|ftp)://")); + if (url.contains('.') && url.indexOf('.') > 0 && url.indexOf('.') < url.length() && !url.trimmed().contains(" ") && QUrl::fromUserInput(url).isValid()) + isValid = true; + return isValid; +} + + IconButton *UrlBar::addRightIcon(UrlBar::icon ic) { IconButton *rightIcon = new IconButton(this); @@ -696,6 +717,14 @@ void UrlBar::pasteAndGo() } +void UrlBar::pasteAndSearch() +{ + KService::Ptr defaultEngine = SearchEngine::defaultEngine(); + if (defaultEngine) + activated(KUrl(SearchEngine::buildQuery(defaultEngine, rApp->clipboard()->text()))); +} + + void UrlBar::delSlot() { del(); diff --git a/src/urlbar/urlbar.h b/src/urlbar/urlbar.h index 7059725b..a9ce301d 100644 --- a/src/urlbar/urlbar.h +++ b/src/urlbar/urlbar.h @@ -116,7 +116,9 @@ private Q_SLOTS: void refreshFavicon(); void pasteAndGo(); + void pasteAndSearch(); void delSlot(); + bool isValidURL(QString url); protected: void paintEvent(QPaintEvent *event); -- cgit v1.2.1