views:

560

answers:

4

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);

    }
+5  A: 

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.

John Millikin
so would I just change the program name from "/usr/bin/firefox" to "xdg-open"?
KPexEA
Change it to /usr/bin/xdg-open
John Millikin
A: 

If you don't want to involve additional applications, just use the built-in remote control commands of firefox. E.g:

firefox -remote 'openurl(http://stackoverflow.com)'

Se detailed usage at http://www.mozilla.org/unix/remote.html

matli
A: 

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".

ephemient
A: 

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.

oliver