From 0e21b478636fac986a5e2276662da6a6e1692f9e Mon Sep 17 00:00:00 2001
From: Andrei Karas <akaras@inbox.ru>
Date: Mon, 18 Feb 2013 15:34:03 +0300
Subject: Add support for help seach links.

To add link to search text: /url ?text
Also add support for links in format: help://topic
---
 src/commands.cpp                    |  2 +-
 src/gui/helpwindow.cpp              |  9 ++++--
 src/gui/helpwindow.h                |  2 +-
 src/gui/widgets/itemlinkhandler.cpp | 59 ++++++++++++++++++++++++-------------
 4 files changed, 46 insertions(+), 26 deletions(-)

(limited to 'src')

diff --git a/src/commands.cpp b/src/commands.cpp
index 73a9f40ce..b085cbd7c 100644
--- a/src/commands.cpp
+++ b/src/commands.cpp
@@ -1038,7 +1038,7 @@ impHandler(url)
     if (tab)
     {
         std::string url = args;
-        if (!strStartWith(url, "http"))
+        if (!strStartWith(url, "http") && !strStartWith(url, "?"))
             url = "http://" + url;
         std::string str = strprintf("[@@%s |%s@@]", url.c_str(), args.c_str());
         outStringNormal(tab, str, str);
diff --git a/src/gui/helpwindow.cpp b/src/gui/helpwindow.cpp
index c035b2a24..712d9fa6a 100644
--- a/src/gui/helpwindow.cpp
+++ b/src/gui/helpwindow.cpp
@@ -36,6 +36,7 @@
 
 #include "utils/gettext.h"
 #include "utils/langs.h"
+#include "utils/paths.h"
 #include "utils/process.h"
 
 #include "utils/translation/podict.h"
@@ -101,18 +102,19 @@ void HelpWindow::handleLink(const std::string &link,
 
 void HelpWindow::loadHelp(const std::string &helpFile)
 {
+    if (!checkPath(helpFile))
+        return;
     mBrowserBox->clearRows();
-
     loadFile("header");
     loadFile(helpFile);
     loadFile("footer");
-
     mScrollArea->setVerticalScrollAmount(0);
     setVisible(true);
 }
 
-void HelpWindow::loadFile(const std::string &file)
+void HelpWindow::loadFile(std::string file)
 {
+    trim(file);
     std::string helpPath = branding.getStringValue("helpPath");
     if (helpPath.empty())
         helpPath = paths.getStringValue("help");
@@ -144,6 +146,7 @@ void HelpWindow::loadTags()
 void HelpWindow::search(const std::string &text0)
 {
     std::string text = text0;
+    trim(text);
     toLower(text);
     if (mTagFileMap.find(text) == mTagFileMap.end())
     {
diff --git a/src/gui/helpwindow.h b/src/gui/helpwindow.h
index 998164b62..4e27a61e2 100644
--- a/src/gui/helpwindow.h
+++ b/src/gui/helpwindow.h
@@ -79,7 +79,7 @@ class HelpWindow final : public Window, public LinkHandler,
     private:
         void loadTags();
 
-        void loadFile(const std::string &file);
+        void loadFile(std::string file);
 
         BrowserBox *mBrowserBox;
         ScrollArea *mScrollArea;
diff --git a/src/gui/widgets/itemlinkhandler.cpp b/src/gui/widgets/itemlinkhandler.cpp
index 24b487f53..e708eb507 100644
--- a/src/gui/widgets/itemlinkhandler.cpp
+++ b/src/gui/widgets/itemlinkhandler.cpp
@@ -25,6 +25,7 @@
 #include "item.h"
 
 #include "gui/confirmdialog.h"
+#include "gui/helpwindow.h"
 #include "gui/itempopup.h"
 #include "gui/viewport.h"
 
@@ -64,7 +65,43 @@ ItemLinkHandler::~ItemLinkHandler()
 void ItemLinkHandler::handleLink(const std::string &link,
                                  gcn::MouseEvent *event)
 {
-    if (!strStartWith(link, "http://") && !strStartWith(link, "https://"))
+    if (strStartWith(link, "http://") || strStartWith(link, "https://"))
+    {
+        if (!event)
+            return;
+        std::string url = link;
+        replaceAll(url, " ", "");
+        listener.url = url;
+        const int button = event->getButton();
+        if (button == gcn::MouseInput::LEFT)
+        {
+            ConfirmDialog *const confirmDlg = new ConfirmDialog(
+                _("Open url"), url, false, true);
+            confirmDlg->addActionListener(&listener); 
+        }
+        else if (button == gcn::MouseInput::RIGHT)
+        {
+            if (viewport)
+                viewport->showLinkPopup(url);
+        }
+    }
+    else if (!link.empty() && link[0] == '?')
+    {
+        if (helpWindow)
+        {
+            helpWindow->search(link.substr(1));
+            helpWindow->requestMoveToTop();
+        }
+    }
+    else if (strStartWith(link, "help://"))
+    {
+        if (helpWindow)
+        {
+            helpWindow->loadHelp(link.substr(7));
+            helpWindow->requestMoveToTop();
+        }
+    }
+    else
     {
         if (!mItemPopup)
             return;
@@ -91,24 +128,4 @@ void ItemLinkHandler::handleLink(const std::string &link,
             }
         }
     }
-    else
-    {
-        if (!event)
-            return;
-        std::string url = link;
-        replaceAll(url, " ", "");
-        listener.url = url;
-        const int button = event->getButton();
-        if (button == gcn::MouseInput::LEFT)
-        {
-            ConfirmDialog *const confirmDlg = new ConfirmDialog(
-                _("Open url"), url, false, true);
-            confirmDlg->addActionListener(&listener); 
-        }
-        else if (button == gcn::MouseInput::RIGHT)
-        {
-            if (viewport)
-                viewport->showLinkPopup(url);
-        }
-    }
 }
-- 
cgit v1.2.3-70-g09d2