From 2668d6425b3573368150c95c253c653d3362136f Mon Sep 17 00:00:00 2001
From: Furkan Uzumcu <furkanuzumcu@gmail.com>
Date: Fri, 21 Oct 2011 17:34:20 +0200
Subject: Enable url or text drops in the tabbar

Dropping urls in the tabbar loads them in new tabs, while dropping text
searches it on your default search engine.

REVIEW: 102811
REVIEWED-BY: adjam
---
 src/tabbar.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/tabbar.h   |  3 +++
 2 files changed, 53 insertions(+)

(limited to 'src')

diff --git a/src/tabbar.cpp b/src/tabbar.cpp
index ea01ef9b..74c339e0 100644
--- a/src/tabbar.cpp
+++ b/src/tabbar.cpp
@@ -43,6 +43,7 @@
 #include "websnap.h"
 #include "tabhighlighteffect.h"
 #include "tabpreviewpopup.h"
+#include "searchengine.h"
 
 // KDE Includes
 #include <KActionMenu>
@@ -88,6 +89,7 @@ TabBar::TabBar(QWidget *parent)
     setGraphicsEffect(m_tabHighlightEffect);
 
     setAnimatedTabHighlighting(ReKonfig::animatedTabHighlighting());
+    setAcceptDrops(true);
 }
 
 
@@ -479,3 +481,51 @@ void TabBar::setAnimatedTabHighlighting(bool enabled)
         }
     }
 }
+
+void TabBar::dropEvent(QDropEvent* event)
+{
+    if (event->mimeData()->hasUrls())
+    {
+        int urlCount = event->mimeData()->urls().count();
+        if (urlCount > 1)
+        {
+            Q_FOREACH (const QUrl url, event->mimeData()->urls())
+                rApp->loadUrl(url, Rekonq::NewTab);
+        }
+        else
+            rApp->loadUrl(event->mimeData()->urls().first(), Rekonq::NewFocusedTab);
+    }
+    else if (event->mimeData()->hasText())
+    {
+        //In case the text is a valid URL
+        if (isURLValid(event->mimeData()->text()))
+            rApp->loadUrl(KUrl(event->mimeData()->text()), Rekonq::NewFocusedTab);
+        else
+        {
+            KService::Ptr defaultSearchEngine = SearchEngine::defaultEngine();
+            if (defaultSearchEngine)
+                rApp->loadUrl(KUrl(SearchEngine::buildQuery(defaultSearchEngine, event->mimeData()->text())), Rekonq::NewFocusedTab);
+        }
+    }
+    KTabBar::dropEvent(event);
+}
+
+void TabBar::dragEnterEvent(QDragEnterEvent* event)
+{
+    if (event->mimeData()->hasUrls() || event->mimeData()->hasText())
+        event->acceptProposedAction();
+    else
+        KTabBar::dragEnterEvent(event);
+}
+
+bool TabBar::isURLValid(const QString &url)
+{
+    QString editedURL = url;
+    bool isValid = false;
+    if (editedURL.startsWith("http://") || editedURL.startsWith("https://") || editedURL.startsWith("ftp://"))
+        editedURL = editedURL.remove(QRegExp("(http|https|ftp)://"));
+    if (editedURL.contains('.') && editedURL.indexOf('.') > 0 && editedURL.indexOf('.') < editedURL.length() && !editedURL.trimmed().contains(" ")
+        && QUrl::fromUserInput(editedURL).isValid())
+        isValid = true;
+    return isValid;
+}
diff --git a/src/tabbar.h b/src/tabbar.h
index b4c816b7..1cb66592 100644
--- a/src/tabbar.h
+++ b/src/tabbar.h
@@ -88,6 +88,8 @@ protected:
     virtual void mousePressEvent(QMouseEvent *event);
 
     virtual void tabRemoved(int index);
+    void dropEvent(QDropEvent *event);
+    void dragEnterEvent(QDragEnterEvent *event);
 
 private slots:
     void cloneTab();
@@ -102,6 +104,7 @@ private slots:
     void showTabPreview();
 
     void removeAnimation(int index);
+    bool isURLValid(const QString &url);
 
 private:
     void setupHistoryActions();
-- 
cgit v1.2.1