Solved

opening a web page

Posted on 2004-09-02
9
265 Views
Last Modified: 2010-04-22
how do i detect the default browser and then load a html page with it ?
i think it's easy to write a script to do this, but i don't know.

anyway, how do i do it in c++ code ? or how do i make a script and then run the script from my c++ prog ?

thanks
0
Comment
Question by:hapciu
  • 4
  • 3
9 Comments
 
LVL 2

Expert Comment

by:gysbert1
ID: 11987322
Unfortunately, in Linux this is not all that easy since it depends on which desktop environment you're using, if you're even in X windows at all. For example, you could be running the script from a console, where the best effort bet might be to use Lynx. In KDE and Gnome (arguably the two most popular desktop environments), as far as I know the way the default browser is configured is completely different (Linux itself does not have a default central configuration like the Windows registry, which is advantageous in some ways, but also disadvantageous in other ways), so your problem might expand to:
- checking whether or not you are running under X windows (could perhaps just test for the presence of the DISPLAY environment variable with getenv or similar)
- checking which desktop environment is being used
- for gnome I don't know, but in KDE the configs are kept somewhere under ~/.kde/share/ , you could experiment by changing the default browser setting in your KDE control panel to some special string like abc123, and then doing a
grep -ir abc123 .
in the ~/.kde/share directory to see where it is stored. You could probably do something similar for gnome.
Once you've figured out what the default browser is, you'd also have to know how to open a file with that browser, i.e. which command line parameters to use. For example, if using mozilla, you cannot just pass the filename to the browser app on the command line because it will try to open a new mozilla session, which may or may not work depending on whether the user already has mozilla open or not, you need to pass special arguments to tell mozilla to open the page in the existing browser session, similar options exist for konqueror and opera and firefox, although they're mostly smarter about re-using the existing session by default.
Hope some of this helps!
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 11999720
are you talking about "web browsers"?
then you should think about JavaScript code in your HTML page, or use a CGI script which detects the browser by reading the User-Agent header from the HTTP request
0
 

Author Comment

by:hapciu
ID: 12022492
no, what I am actually talking about is the "run command" dialog.....
in kde, in the start menu there is a "run command" option. if you click it it opens a dialog with a text field... now that dialog is really smart... if you type an executable - it runs it, if you type a webpage - it opens the default browser and loads the page, if you type a music file - it opens the default music player and plays it, etc.

all I want is to write a c++ function that opens a web page .... you give it the address of the webpage as a parameter and it starts the default browser and opens that page.

gysbert1: you may be right, but then again, I hope there is an easier way to acomplish this :)


ps: in windows there is a function called shellExec(...) that does exactly this - default action on a given file. i'm looking for a similar one in Linux

thank you for your answers
0
 
LVL 2

Accepted Solution

by:
gysbert1 earned 60 total points
ID: 12023794
hapciu: I don't know offhand whether there is such a function in linux. As I mentioned, linux separates the GUI (Xwindows) from the OS, which has certain disadvantages compared to Windows, at least in terms of graphical integration. FWIW IMHO I think Mac OS X has managed to find a much better balance between good OS and good GUI environment than either Windows or Linux. Windows (still) has a unstable/insecure OS but a pretty good GUI, Linux is excellent in terms of OS stability and security, but lacks a bit of compatibility (drivers) and GUI integration, Mac OS X seems to just shine in all areas.

Anyway, back to the topic at hand. There *might* be a Xwindows API call to do something like shellExec, which *might* be something that hooks into each window manager or desktop environment being used, in which case you might not have to manually detect various things like that. I don't know offhand of such a call though, I would hope that something like that exists though. I have a feeling that it doesn't, the kind of functionality of clicking on certain file types to launch them with certain predefined commands in a special way is something I first started seeing with KDE / Gnome when they first started becoming popular some years ago.

If your program is a GUI app under linux, you could always give it its own config option in a dialog somewhere to specify the browser to use (for a non-GUI app you could still have a config file in a file such as ~/.<yourapp>/<yourapp>.cfg or whatever). Later if you do figure out how to find the default browser in a portable generic way, you could just autopopulate that setting instead of making the user configure it the first time the app is run.

I notice on my environment that there is a variable called WINDOWMANAGER, which for me is set to /usr/X11R6/bin/kde. I'm not sure if this is mandatorily set by Xwindows or something, but you might be able to use that to at least detect what WM the app is running under, and perhaps assume semi intelligent default settings for the user initial browser config dialog, e.g. if in KDE, assume konqueror and populate with the default kde konqueror path (whatever that is), if in gnome assume gecko (or whatever they use), or look to see if mozilla or firefox or something is installed (try shelling 'which mozilla' or 'which firefox' etc. to see if its on the path).

Please let us know if you do find an easier way!
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 51

Expert Comment

by:ahoffmann
ID: 12023979
system("/path/to/your/browser URL");

assuming that URL is a parameter somehow, you need to compute the full string for system() first using your args array or whatever.

You also may check exec*() functions, see  man execl
0
 
LVL 2

Expert Comment

by:gysbert1
ID: 12024096
I think hapciu's problem is finding out what the value of /path/to/your/browser needs to be to match the user's default browser setting.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 12025371
>  I think hapciu's problem is finding out what the value of /path/to/your/browser
difficult in C/C++, either go through all dirs in PATH environment variable and check if it exists their, or use
"which browser" popen() call to get the path back (not very reliable)-:
0
 
LVL 2

Expert Comment

by:gysbert1
ID: 12025473
I suspect the problem is not just the path, but what the actual value of "browser" is, so even "which browser" wouldn't work. I think he wants to know whether "browser" is konqueror, opera, firefox, mozilla, gecko etc. If one knows that, then "which browser" will probably do the trick, in fact it might not even be necessary since doing a "system" should respect the user's path when locating the binary.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now