diff --git a/src/apps/webpositive/BrowserWindow.cpp b/src/apps/webpositive/BrowserWindow.cpp index 3e4da40d185..616334e70d6 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 36605aaaebf..59a0982af49 100644 --- a/src/apps/webpositive/BrowserWindow.h +++ b/src/apps/webpositive/BrowserWindow.h @@ -190,7 +190,11 @@ class BrowserWindow : public BWebWindow { 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);