Skip to content

Commit

Permalink
WebPositive: Smart URL handling improvements (GCI task)
Browse files Browse the repository at this point in the history
 - 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")
  • Loading branch information
Tri-Edge AI authored and mmuman committed Dec 13, 2012
1 parent 4b84a0b commit bafbb92
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 12 deletions.
96 changes: 85 additions & 11 deletions src/apps/webpositive/BrowserWindow.cpp
Expand Up @@ -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:
Expand Down Expand Up @@ -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;
}


Expand Down
6 changes: 5 additions & 1 deletion src/apps/webpositive/BrowserWindow.h
Expand Up @@ -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);
Expand Down

0 comments on commit bafbb92

Please sign in to comment.