Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch web page from my application in Linux

Tags:

c++

c

browser

linux

I have an application that launches a webpage in the "current" browser when the user selects it. This part of my app works fine in the Windows version but I can't figure out how to do this in Linux build.

Right now the Linux version is hardcoded for Firefox in a specific directory and runs a new instance of it each time and doesn't show the URL that I pass in. I would like it to NOT launch a new version each time but just open a new page in the current open one if it is already running.

For windows I use:

ShellExecute(NULL,"open",filename,NULL,NULL,SW_SHOWNORMAL);

For Linux I currently use:

pid_t pid;
char *args[2];

char *prog=0;
char firefox[]={"/usr/bin/firefox"};

if(strstri(filename,".html"))
    prog=firefox;

if(prog)
{
    args[0]=(char *)filename;
    args[1]=0;
    pid=fork();
    if(!pid)
        execvp(prog,args);

}
like image 515
KPexEA Avatar asked Oct 01 '08 02:10

KPexEA


People also ask

How do I open a URL in Ubuntu terminal?

xdg-open opens a file or URL in the user's preferred application. If a URL is provided the URL will be opened in the user's preferred web browser. gnome-open works as well as xdg-open but neither knows what to do with naked domain. So gnome-open http://askubuntu.com works but not gnome-open askubuntu.com .


3 Answers

If you're writing this for modern distros, you can use xdg-open:

$ xdg-open http://google.com/

If you're on an older version you'll have to use a desktop-specific command like gnome-open or exo-open.

like image 178
John Millikin Avatar answered Oct 17 '22 21:10

John Millikin


xdg-open is the new standard, and you should use it when possible. However, if the distro is more than a few years old, it may not exist, and alternative mechanisms include $BROWSER (older attempted standard), gnome-open (Gnome), kfmclient exec (KDE), exo-open (Xfce), or parsing mailcap yourself (the text/html handler will be likely be a browser).

That being said, most applications don't bother with that much work -- if they're built for a particular environment, they use that environment's launch mechanisms. For example, Gnome has gnome_url_show, KDE has KRun, most terminal programs (for example, mutt) parse mailcap, etc. Hardcoding a browser and allowing the distributor or user to override the default is common too.

I don't suggest hardcoding this, but if you really want to open a new tab in Firefox, you can use "firefox -new-tab $URL".

like image 2
ephemient Avatar answered Oct 17 '22 20:10

ephemient


A note for xdg-open: check http://portland.freedesktop.org/wiki/ , section "Using Xdg-utils"; it states that you can include the xdg-open script in your own application and use that as fallback in case the target system doesn't have xdg-open already installed.

like image 1
oliver Avatar answered Oct 17 '22 21:10

oliver