Thursday, May 18, 2006

wxWidgets code to launch browser

In wxWidgets, which is a wonderful multi-platform GUI C++ library loaded with really coool helper functions which works in Windows, Linux and OSX (and more), has a method called LaunchDefaultBrowser, which has lots of issues with MIME types, blah, blah.

So, as I'm sick of LaunchDefaultBrowser, which seems to always call epiphany browser under GNU/Linux, I decided to write a function to call a browser according to certain needs/parameters. So,

This function searches the PATH environment variable for this commands (according to no articular preference):

firefox
firefox-bin
mozilla
mozilla-bin
opera
konqueror
epiphany

It can probably be enhanced by some checks, but here you are:

void MainFrame::ExecuteURL(const wxString &link)
{

// variable declarations
wxArrayString browsers;
wxPathList path_list;
bool BrowserWasFound = false;
unsigned int i = 0;
wxString path;

// Add directories to wxPathList's search path from PATH environment variable
path_list.AddEnvList(wxT("PATH"));

// Add browsers filenames. First item = most priority
browsers.Add(wxT("firefox"));
browsers.Add(wxT("firefox-bin"));
browsers.Add(wxT("mozilla"));
browsers.Add(wxT("mozilla-bin"));
browsers.Add(wxT("opera"));
browsers.Add(wxT("konqueror"));
browsers.Add(wxT("epiphany"));

for (i = 0; i < browsers.GetCount(); i++) {
path = path_list.FindAbsoluteValidPath(browsers[i]);
if (path.IsEmpty()) {
continue;
} else {
BrowserWasFound = true;
break;
}
}

browsers.Clear();

if (BrowserWasFound) {
path += wxT(" ");
path += link;
::wxExecute(path);
} else {
wxMessageBox(wxT("No browser has been found."),MessageBoxHeader);
}
}

Hope it gets to be useful!

Labels:

Slashdot   Liked it? Submit this post to Slashdot!
posted by Arturo 'Buanzo' Busleiman @ 7:07 AM  
2 comments

2 Comments:

At 7:39 PM, Blogger Roy Evangelista said...

OK men it works...

 
At 7:42 PM, Blogger Roy Evangelista said...

OK men i tested it....and it works.

 

Post a Comment

<< Home