[haiku-commits] haiku: hrev45007 - src/apps/webpositive

  • From: revol@xxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Fri, 14 Dec 2012 00:40:58 +0100 (CET)

hrev45007 adds 1 changeset to branch 'master'
old head: 4b84a0b5c8a8c87b2601a735d13cbf258a32fc92
new head: bafbb929015db8c950ae898e990d840c8236995f
overview: http://cgit.haiku-os.org/haiku/log/?qt=range&q=bafbb92+%5E4b84a0b

----------------------------------------------------------------------------

bafbb92: WebPositive: Smart URL handling improvements (GCI task)
  
   - Fixed: WebPositive now successfuly detects foreign protocols and
  launches their respective applications.
   - Improved: The decision whether to use a search engine or a DNS lookup
  for the text entered in the address bar, including for internationalized
  names (IDN) (though we do not handle them correctly later on yet).
   - TODO: escape the query string before passing it to webkit
  (for example for: "3+4")

                                       [ Tri-Edge AI <triedgeai@xxxxxxxxx> ]

----------------------------------------------------------------------------

Revision:    hrev45007
Commit:      bafbb929015db8c950ae898e990d840c8236995f
URL:         http://cgit.haiku-os.org/haiku/commit/?id=bafbb92
Author:      Tri-Edge AI <triedgeai@xxxxxxxxx>
Date:        Thu Dec 13 22:45:35 2012 UTC
Committer:   François Revol <revol@xxxxxxx>
Commit-Date: Thu Dec 13 23:40:03 2012 UTC

----------------------------------------------------------------------------

2 files changed, 90 insertions(+), 12 deletions(-)
src/apps/webpositive/BrowserWindow.cpp | 96 ++++++++++++++++++++++++++----
src/apps/webpositive/BrowserWindow.h   |  6 +-

----------------------------------------------------------------------------

diff --git a/src/apps/webpositive/BrowserWindow.cpp 
b/src/apps/webpositive/BrowserWindow.cpp
index 3e4da40..616334e 100644
--- a/src/apps/webpositive/BrowserWindow.cpp
+++ b/src/apps/webpositive/BrowserWindow.cpp
@@ -652,11 +652,10 @@ BrowserWindow::MessageReceived(BMessage* message)
                        BString url;
                        if (message->FindString("url", &url) != B_OK)
                                url = fURLInputGroup->Text();
+
                        _SetPageIcon(CurrentWebView(), NULL);
-                       BString newUrl = _SmartURLHandler(url);
-                       if (newUrl != url)
-                               fURLInputGroup->TextView()->SetText(newUrl);
-                       CurrentWebView()->LoadURL(newUrl.String());
+                       _SmartURLHandler(url);
+
                        break;
                }
                case GO_BACK:
@@ -2149,18 +2148,93 @@ BrowserWindow::_NewTabURL(bool isNewWindow) const
 }
 
 
-BString
-BrowserWindow::_SmartURLHandler(const BString& url) const
+void
+BrowserWindow::_VisitURL(const BString& url)
+{
+       //fURLInputGroup->TextView()->SetText(url);
+       CurrentWebView()->LoadURL(url.String());
+}
+
+
+void
+BrowserWindow::_VisitSearchEngine(const BString& search)
 {
-       BString result = url;
+       // TODO: Google Code-In Task to make default search
+       //                      engine modifiable from Settings? :)
+
+       BString engine = "http://www.google.com/search?q=";;
+       engine += search;
+               // WebKit takes care of URL encoding here.
+
+       _VisitURL(engine);
+}
+
+
+inline bool
+BrowserWindow::_IsValidDomainChar(char ch)
+{
+       // TODO: Currenlty, only a whitespace character
+       //                      breaks a domain name. It might be
+       //                      a good idea (or a bad one) to make
+       //                      character filtering based on the
+       //                      IDNA 2008 standard.
+
+       return ch != ' ';
+}
 
+
+void
+BrowserWindow::_SmartURLHandler(const BString& url)
+{
        // Only process if this doesn't look like a full URL (http:// or
        // file://, etc.)
-       if (url.FindFirst("://") == B_ERROR) {
-               if (url.FindFirst(".") == B_ERROR || url.FindFirst(" ") != 
B_ERROR)
-                       result.Prepend("http://www.google.com/search?q=";);
+
+       BString temp;
+       int32 at = url.FindFirst(":");
+
+       if (at != B_ERROR) {
+               BString proto;
+               url.CopyInto(proto, 0, at);
+
+               if (proto == "http" ||  proto == "https" ||     proto == "file")
+                       _VisitURL(url);
+               else {
+                       temp = "application/x-vnd.Be.URL.";
+                       temp += proto;
+
+                       char* argv[1] = { (char*)url.String() };
+
+                       if (be_roster->Launch(temp.String(), 1, argv) != B_OK)
+                               _VisitSearchEngine(url);
+               }
+       } else if (url == "localhost")
+               _VisitURL("http://localhost/";);
+       else {
+               const char* localhostPrefix = "localhost/";
+
+               if(url.Compare(localhostPrefix, strlen(localhostPrefix)) == 0)
+                       _VisitURL(url);
+               else {
+                       bool isURL = false;
+
+                       for (int32 i = 0; i < url.CountChars(); i++) {
+                               if (url[i] == '.')
+                                       isURL = true;
+                               else if (url[i] == '/')
+                                       break;
+                               else if (!_IsValidDomainChar(url[i])) {
+                                       isURL = false;
+
+                                       break;
+                               }
+                       }
+
+                       if (isURL)
+                               _VisitURL(url);
+                       else
+                               _VisitSearchEngine(url);
+               }
        }
-       return result;
 }
 
 
diff --git a/src/apps/webpositive/BrowserWindow.h 
b/src/apps/webpositive/BrowserWindow.h
index 36605aa..59a0982 100644
--- a/src/apps/webpositive/BrowserWindow.h
+++ b/src/apps/webpositive/BrowserWindow.h
@@ -190,7 +190,11 @@ private:
                        void                            
_InvokeButtonVisibly(BButton* button);
 
                        BString                         _NewTabURL(bool 
isNewWindow) const;
-                       BString                         _SmartURLHandler(const 
BString& url) const;
+
+                       void                            _VisitURL(const 
BString& url);
+                       void                            
_VisitSearchEngine(const BString& search);
+       inline  bool                            _IsValidDomainChar(char ch);
+                       void                            _SmartURLHandler(const 
BString& url);
 
                        void                            _HandlePageSourceResult(
                                                                        const 
BMessage* message);


Other related posts:

  • » [haiku-commits] haiku: hrev45007 - src/apps/webpositive - revol