5
# $Id: WWWBrowser.pm,v 2.23 2003/02/05 16:39:10 eserte Exp $
8
# Modified somewhat by Bruce Ravel
10
# Copyright (C) 1999,2000,2001,2003 Slaven Rezic. All rights reserved.
11
# This package is free software; you can redistribute it and/or
12
# modify it under the same terms as Perl itself.
14
# Mail: slaven@rezic.de
15
# WWW: http://www.rezic.de/eserte/
21
use vars qw(@unix_browsers $VERSION $initialized $os $fork
22
$got_from_config $ignore_config);
24
$VERSION = sprintf("%d.%02d", q$Revision: 2.23 $ =~ /(\d+)\.(\d+)/);
26
@unix_browsers = qw(_default_gnome _default_kde
27
opera iceweasel galeon epiphany firefox konqueror mozilla netscape Netscape kfmclient
29
dillo mosaic Mosaic chimera arena tkweb
30
explorer) if !@unix_browsers;
36
if (!defined $main::os) {
37
$os = ($^O eq 'MSWin32' ? 'win' : 'unix');
41
if (!defined &main::status_message) {
42
eval 'sub status_message { warn $_[0] }';
44
eval 'sub status_message { main::status_message(@_) }';
57
if (!eval 'require Win32Util;
58
Win32Util::start_html_viewer($url)') {
59
# if this fails, just try to start explorer
60
system("start explorer $url");
63
status_message("Can't find HTML viewer.", "err");
70
my @browsers = @unix_browsers;
71
if ($args{-browser}) {
72
unshift @browsers, delete $args{-browser};
75
foreach my $browser (@browsers) {
76
next if (!is_in_path($browser));
77
if ($browser =~ /^(lynx|w3m)$/) { # text-orientierte Browser
78
if (defined $ENV{DISPLAY} && $ENV{DISPLAY} ne "") {
79
foreach my $term (qw(xterm kvt gnome-terminal)) {
80
if (is_in_path($term)) {
82
($term eq 'gnome_terminal' ? '-x' : '-e'),
88
# without X11: not in background!
89
system($browser, $url);
95
if ((!defined $ENV{DISPLAY} || $ENV{DISPLAY} eq '') &&
99
# After this point only X11 browsers or cygwin as a special case
102
if ($browser eq '_default_gnome') {
104
my $cmdline = _get_cmdline_for_url_from_Gnome($url);
108
} elsif ($browser eq '_default_kde') {
110
} elsif ($browser eq 'konqueror') {
111
return 1 if open_in_konqueror($url, %args);
112
} elsif ($browser eq 'galeon') {
113
return 1 if open_in_galeon($url, %args);
114
} elsif ($browser eq 'mozilla') {
115
return 1 if open_in_mozilla($url, %args);
116
} elsif ($browser =~ /^mosaic$/i &&
117
$url =~ /^file:/ && $url !~ m|file://|) {
118
$url =~ s|file:/|file://localhost/|;
119
} elsif ($browser eq 'kfmclient') {
120
# kfmclient loads kfm, which loads and displays all KDE icons
121
# on the desktop, even if KDE is not running at all.
122
exec_bg("kfmclient", "openURL", $url);
124
} elsif ($browser eq 'netscape') {
126
my $lockfile = "$ENV{HOME}/.netscape/lock";
128
my($host,$pid) = readlink($lockfile) =~ /^(.*):(\d+)$/;
130
# Check whether Netscape stills lives:
131
if (defined $pid && kill 0 => $pid) {
132
if ($args{-oldwindow}) {
133
exec_bg("netscape", "-remote", "openURL($url)");
135
exec_bg("netscape", "-remote", "openURL($url,new)");
137
# XXX further options: mailto(to-adresses)
138
# XXX check return code?
142
exec_bg("netscape", $url);
146
exec_bg($browser, $url);
151
status_message("Can't find HTML viewer.", "err");
156
sub open_in_konqueror {
159
if (is_in_path("dcop") && is_in_path("konqueror")) {
161
# first try old window (if requested)
162
if ($args{-oldwindow}) {
164
foreach my $l (split /\n/, `dcop konqueror KonquerorIface getWindows`) {
165
if ($l =~ /(konqueror-mainwindow\#\d+)/) {
171
if (defined $konq_name) {
172
system(qw/dcop konqueror/, $konq_name, qw/openURL/, $url);
173
return 1 if ($?/256 == 0);
177
# then try to send to running konqueror process:
178
system(qw/dcop konqueror KonquerorIface openBrowserWindow/, $url);
179
return 1 if ($?/256 == 0);
181
# otherwise start a new konqueror
182
exec_bg("konqueror", $url);
183
return 1; # if ($?/256 == 0);
191
if (is_in_path("galeon")) {
193
$url = _guess_and_expand_url($url) if $args{-expandurl};
195
# first try old window (if requested)
196
if ($args{-oldwindow}) {
197
system("galeon", "-x", $url);
198
return 1 if ($?/256 == 0);
201
exec_bg("galeon", "-n", $url);
202
return 1 if ($?/256 == 0);
208
sub open_in_mozilla {
211
if (is_in_path("mozilla")) {
212
if ($args{-oldwindow}) {
213
system("mozilla", "-remote", "openURL($url)");
215
system("mozilla", "-remote", "openURL($url,new-tab)");
217
return 1 if ($?/256 == 0);
219
# otherwise start a new mozilla process
220
exec_bg("mozilla", $url);
221
return 1; # if ($?/256 == 0);
230
if (!$fork || fork == 0) {
232
die "Can't exec @cmd: $!";
237
system(join(" ", @cmd) . ($fork ? "&" : ""));
241
sub _get_cmdline_for_url_from_Gnome {
243
(my $url_scheme = $url) =~ s/^([^:]+).*/$1/; # use URI.pm?
247
if (open(GNOME, "$ENV{HOME}/.gnome/Gnome")) {
252
} elsif (defined $curr_section && $curr_section eq 'URL Handlers' && /^(default|\Q$url_scheme\E)-show=(.*)/) {
253
if ($1 eq 'default') {
254
$default_cmdline = $2;
262
if (!defined $cmdline) {
263
$cmdline = $default_cmdline;
265
if (!defined $cmdline) {
266
die "Can't find command for scheme $url_scheme";
268
$cmdline =~ s/%s/$url/g;
272
# XXX document get_from_config, $ignore_config, ~/.wwwbrowser
273
sub get_from_config {
274
if (!$got_from_config && !$ignore_config && $ENV{HOME} && open(CFG, "$ENV{HOME}/.wwwbrowser")) {
282
unshift @unix_browsers, @browser;
286
sub _guess_and_expand_url {
288
if ($url =~ m|^[a-z]+://|) {
290
} elsif ($url =~ m|^www|) {
292
} elsif ($url =~ m|^ftp|) {
300
# REPO NAME file_name_is_absolute /home/e/eserte/src/repository
301
# REPO MD5 89d0fdf16d11771f0f6e82c7d0ebf3a8
303
if (eval { require File::Spec; defined &File::Spec::file_name_is_absolute }) {
304
*file_name_is_absolute = \&File::Spec::file_name_is_absolute;
306
*file_name_is_absolute = sub {
309
if ($^O eq 'MSWin32') {
310
$r = ($file =~ m;^([a-z]:(/|\\)|\\\\|//);i);
312
$r = ($file =~ m|^/|);
321
# REPO NAME is_in_path /home/e/eserte/src/repository
322
# REPO MD5 81c0124cc2f424c6acc9713c27b9a484
325
return $prog if (file_name_is_absolute($prog) and -f $prog and -x $prog);
327
my $sep = $Config::Config{'path_sep'} || ':';
328
foreach (split(/$sep/o, $ENV{PATH})) {
329
if ($^O eq 'MSWin32') {
330
# maybe use $ENV{PATHEXT} like maybe_command in ExtUtils/MM_Win32.pm?
332
if (-x "$_\\$prog.bat" ||
333
-x "$_\\$prog.com" ||
334
-x "$_\\$prog.exe" ||
337
return "$_/$prog" if (-x "$_/$prog" && !-d "$_/$prog");
350
WWWBrowser - platform independent mean to start a WWW browser
355
WWWBrowser::start_browser($url, -oldwindow => 1);
359
=head2 start_browser($url [, %args])
361
Start a web browser with the specified URL. The process is started in
364
The following optional parameters are recognized:
368
=item -oldwindow => $bool
370
Normally, the URL is loaded into a new window, if possible. With
371
C<-oldwindow> set to a false window, C<WWWBrowser> will try to re-use
374
=item -browser => $browser
376
Use (preferebly) the named browser C<$browser>. See L</CONFIGURATION>
377
for a some browser specialities. This option will only work for unix.
383
For unix, the global variable C<@WWWBrowser::unix_browsers> can be set
384
to a list of preferred web browsers. The following browsers are
391
Text oriented browsers, which are opened in an C<xterm>, C<kvt> or
392
C<gnome-terminal> (if running under X11). If not running under X11,
393
then no background process is started.
397
Use C<openURL> method of kfm.
401
Use C<-remote> option to re-use a running netscape process, if
406
Look into the C<~/.gnome/Gnome> configuration file for the right browser.
414
The following variables can be defined globally in the B<main>
421
Short name of operating system (C<win>, C<mac> or C<unix>).
423
=item C<&status_messages>
425
Error handling function (instead of default C<warn>).
431
For Windows, the L<Win32Util|Win32Util> module should be installed in
436
Slaven Rezic <slaven@rezic.de>
440
Copyright (c) 1999,2000,2001,2003 Slaven Rezic. All rights reserved.
441
This module is free software; you can redistribute it and/or modify
442
it under the same terms as Perl itself.
446
L<Win32Util|Win32Util>.