~ubuntu-branches/ubuntu/lucid/horae/lucid

« back to all changes in this revision

Viewing changes to lib/WWWBrowser.pm

  • Committer: Bazaar Package Importer
  • Author(s): Carlo Segre
  • Date: 2009-07-04 11:16:53 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20090704111653-htsge5klkti12mco
Tags: 070-1
* New upstream release
* Upgrade to Standards-Version 3.8.2
* Restore Education top level menu item in *.desktop files
  NOTE: This is not the right solution but conforms to the freedesktop.org 
        standard. KDE and Gnome provide a "Science" top level menu through
        their own configuration files.  XFCE4 currently does not but can be
        configured to do so.  See README.Debian for this information.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env perl
2
 
# -*- perl -*-
3
 
 
4
 
#
5
 
# $Id: WWWBrowser.pm,v 2.23 2003/02/05 16:39:10 eserte Exp $
6
 
# Author: Slaven Rezic
7
 
#
8
 
# Modified somewhat by Bruce Ravel
9
 
#
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.
13
 
#
14
 
# Mail: slaven@rezic.de
15
 
# WWW:  http://www.rezic.de/eserte/
16
 
#
17
 
 
18
 
package WWWBrowser;
19
 
 
20
 
use strict;
21
 
use vars qw(@unix_browsers $VERSION $initialized $os $fork
22
 
            $got_from_config $ignore_config);
23
 
 
24
 
$VERSION = sprintf("%d.%02d", q$Revision: 2.23 $ =~ /(\d+)\.(\d+)/);
25
 
 
26
 
@unix_browsers = qw(_default_gnome _default_kde
27
 
                    opera iceweasel galeon epiphany firefox konqueror mozilla netscape Netscape kfmclient
28
 
                    w3m lynx
29
 
                    dillo mosaic Mosaic chimera arena tkweb
30
 
                    explorer) if !@unix_browsers;
31
 
 
32
 
init();
33
 
 
34
 
sub init {
35
 
    if (!$initialized) {
36
 
        if (!defined $main::os) {
37
 
            $os = ($^O eq 'MSWin32' ? 'win' : 'unix');
38
 
        } else {
39
 
            $os = $main::os;
40
 
        }
41
 
        if (!defined &main::status_message) {
42
 
            eval 'sub status_message { warn $_[0] }';
43
 
        } else {
44
 
            eval 'sub status_message { main::status_message(@_) }';
45
 
        }
46
 
        $fork = 1;
47
 
        $initialized++;
48
 
        get_from_config();
49
 
    }
50
 
}
51
 
 
52
 
sub start_browser {
53
 
    my $url = shift;
54
 
    my(%args) = @_;
55
 
 
56
 
    if ($os eq 'win') {
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");
61
 
            # otherwise croak
62
 
            if ($?/256 != 0) {
63
 
                status_message("Can't find HTML viewer.", "err");
64
 
                return 0;
65
 
            }
66
 
        }
67
 
        return 1;
68
 
    }
69
 
 
70
 
    my @browsers = @unix_browsers;
71
 
    if ($args{-browser}) {
72
 
        unshift @browsers, delete $args{-browser};
73
 
    }
74
 
 
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)) {
81
 
                        exec_bg($term,
82
 
                                ($term eq 'gnome_terminal' ? '-x' : '-e'),
83
 
                                $browser, $url);
84
 
                        return 1;
85
 
                    }
86
 
                }
87
 
            } else {
88
 
                # without X11: not in background!
89
 
                system($browser, $url);
90
 
                return 1;
91
 
            }
92
 
            next;
93
 
        }
94
 
 
95
 
        if ((!defined $ENV{DISPLAY} || $ENV{DISPLAY} eq '') &&
96
 
            $^O ne 'cygwin') {
97
 
            next;
98
 
        }
99
 
        # After this point only X11 browsers or cygwin as a special case
100
 
 
101
 
        my $url = $url;
102
 
        if ($browser eq '_default_gnome') {
103
 
            eval {
104
 
                my $cmdline = _get_cmdline_for_url_from_Gnome($url);
105
 
                exec_bg($cmdline);
106
 
                return 1;
107
 
            };
108
 
        } elsif ($browser eq '_default_kde') {
109
 
            # NYI
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);
123
 
            return 1 if (!$?)
124
 
        } elsif ($browser eq 'netscape') {
125
 
            if ($os eq 'unix') {
126
 
                my $lockfile = "$ENV{HOME}/.netscape/lock";
127
 
                if (-l $lockfile) {
128
 
                    my($host,$pid) = readlink($lockfile) =~ /^(.*):(\d+)$/;
129
 
                    # XXX check $host
130
 
                    # Check whether Netscape stills lives:
131
 
                    if (defined $pid && kill 0 => $pid) {
132
 
                        if ($args{-oldwindow}) {
133
 
                            exec_bg("netscape", "-remote", "openURL($url)");
134
 
                        } else {
135
 
                            exec_bg("netscape", "-remote", "openURL($url,new)");
136
 
                        }
137
 
                        # XXX further options: mailto(to-adresses)
138
 
                        # XXX check return code?
139
 
                        return 1;
140
 
                    }
141
 
                }
142
 
                exec_bg("netscape", $url);
143
 
                return 1;
144
 
            }
145
 
        } else {
146
 
            exec_bg($browser, $url);
147
 
            return 1;
148
 
        }
149
 
    }
150
 
 
151
 
    status_message("Can't find HTML viewer.", "err");
152
 
 
153
 
    return 0;
154
 
}
155
 
 
156
 
sub open_in_konqueror {
157
 
    my $url = shift;
158
 
    my(%args) = @_;
159
 
    if (is_in_path("dcop") && is_in_path("konqueror")) {
160
 
 
161
 
        # first try old window (if requested)
162
 
        if ($args{-oldwindow}) {
163
 
            my $konq_name;
164
 
            foreach my $l (split /\n/, `dcop konqueror KonquerorIface getWindows`) {
165
 
                if ($l =~ /(konqueror-mainwindow\#\d+)/) {
166
 
                    $konq_name = $1;
167
 
                    last;
168
 
                }
169
 
            }
170
 
 
171
 
            if (defined $konq_name) {
172
 
                system(qw/dcop konqueror/, $konq_name, qw/openURL/, $url);
173
 
                return 1 if ($?/256 == 0);
174
 
            }
175
 
        }
176
 
 
177
 
        # then try to send to running konqueror process:
178
 
        system(qw/dcop konqueror KonquerorIface openBrowserWindow/, $url);
179
 
        return 1 if ($?/256 == 0);
180
 
 
181
 
        # otherwise start a new konqueror
182
 
        exec_bg("konqueror", $url);
183
 
        return 1; # if ($?/256 == 0);
184
 
    }
185
 
    0;
186
 
}
187
 
 
188
 
sub open_in_galeon {
189
 
    my $url = shift;
190
 
    my(%args) = @_;
191
 
    if (is_in_path("galeon")) {
192
 
 
193
 
        $url = _guess_and_expand_url($url) if $args{-expandurl};
194
 
 
195
 
        # first try old window (if requested)
196
 
        if ($args{-oldwindow}) {
197
 
            system("galeon", "-x", $url);
198
 
            return 1 if ($?/256 == 0);
199
 
        }
200
 
 
201
 
        exec_bg("galeon", "-n", $url);
202
 
        return 1 if ($?/256 == 0);
203
 
        return 0;
204
 
    }
205
 
    0;
206
 
}
207
 
 
208
 
sub open_in_mozilla {
209
 
    my $url = shift;
210
 
    my(%args) = @_;
211
 
    if (is_in_path("mozilla")) {
212
 
        if ($args{-oldwindow}) {
213
 
            system("mozilla", "-remote", "openURL($url)");
214
 
        } else {
215
 
            system("mozilla", "-remote", "openURL($url,new-tab)");
216
 
        }
217
 
        return 1 if ($?/256 == 0);
218
 
 
219
 
        # otherwise start a new mozilla process
220
 
        exec_bg("mozilla", $url);
221
 
        return 1; # if ($?/256 == 0);
222
 
    }
223
 
    0;
224
 
}
225
 
 
226
 
sub exec_bg {
227
 
    my(@cmd) = @_;
228
 
    if ($os eq 'unix') {
229
 
        eval {
230
 
            if (!$fork || fork == 0) {
231
 
                exec @cmd;
232
 
                die "Can't exec @cmd: $!";
233
 
            }
234
 
        };
235
 
    } else {
236
 
        # XXX use Spawn
237
 
        system(join(" ", @cmd) . ($fork ? "&" : ""));
238
 
    }
239
 
}
240
 
 
241
 
sub _get_cmdline_for_url_from_Gnome {
242
 
    my($url) = @_;
243
 
    (my $url_scheme = $url) =~ s/^([^:]+).*/$1/; # use URI.pm?
244
 
    my $curr_section;
245
 
    my $default_cmdline;
246
 
    my $cmdline;
247
 
    if (open(GNOME, "$ENV{HOME}/.gnome/Gnome")) {
248
 
        while(<GNOME>) {
249
 
            chomp;
250
 
            if (/^\[(.*)\]/) {
251
 
                $curr_section = $1;
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;
255
 
                } else {
256
 
                    $cmdline = $2;
257
 
                }
258
 
            }
259
 
        }
260
 
        close GNOME;
261
 
    }
262
 
    if (!defined $cmdline) {
263
 
        $cmdline = $default_cmdline;
264
 
    }
265
 
    if (!defined $cmdline) {
266
 
        die "Can't find command for scheme $url_scheme";
267
 
    }
268
 
    $cmdline =~ s/%s/$url/g;
269
 
    $cmdline;
270
 
}
271
 
 
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")) {
275
 
        my @browser;
276
 
        while(<CFG>) {
277
 
            chomp;
278
 
            push @browser, $_;
279
 
        }
280
 
        close CFG;
281
 
        $got_from_config++;
282
 
        unshift @unix_browsers, @browser;
283
 
    }
284
 
}
285
 
 
286
 
sub _guess_and_expand_url {
287
 
    my $url = shift;
288
 
    if ($url =~ m|^[a-z]+://|) {
289
 
        $url;
290
 
    } elsif ($url =~ m|^www|) {
291
 
        "http://$url";
292
 
    } elsif ($url =~ m|^ftp|) {
293
 
        "ftp://$url";
294
 
    } else {
295
 
        $url;
296
 
    }
297
 
}
298
 
 
299
 
# REPO BEGIN
300
 
# REPO NAME file_name_is_absolute /home/e/eserte/src/repository 
301
 
# REPO MD5 89d0fdf16d11771f0f6e82c7d0ebf3a8
302
 
BEGIN {
303
 
    if (eval { require File::Spec; defined &File::Spec::file_name_is_absolute }) {
304
 
        *file_name_is_absolute = \&File::Spec::file_name_is_absolute;
305
 
    } else {
306
 
        *file_name_is_absolute = sub {
307
 
            my $file = shift;
308
 
            my $r;
309
 
            if ($^O eq 'MSWin32') {
310
 
                $r = ($file =~ m;^([a-z]:(/|\\)|\\\\|//);i);
311
 
            } else {
312
 
                $r = ($file =~ m|^/|);
313
 
            }
314
 
            $r;
315
 
        };
316
 
    }
317
 
}
318
 
# REPO END
319
 
 
320
 
# REPO BEGIN
321
 
# REPO NAME is_in_path /home/e/eserte/src/repository 
322
 
# REPO MD5 81c0124cc2f424c6acc9713c27b9a484
323
 
sub is_in_path {
324
 
    my($prog) = @_;
325
 
    return $prog if (file_name_is_absolute($prog) and -f $prog and -x $prog);
326
 
    require Config;
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?
331
 
            return "$_\\$prog"
332
 
                if (-x "$_\\$prog.bat" ||
333
 
                    -x "$_\\$prog.com" ||
334
 
                    -x "$_\\$prog.exe" ||
335
 
                    -x "$_\\$prog.cmd");
336
 
        } else {
337
 
            return "$_/$prog" if (-x "$_/$prog" && !-d "$_/$prog");
338
 
        }
339
 
    }
340
 
    undef;
341
 
}
342
 
# REPO END
343
 
 
344
 
1;
345
 
 
346
 
__END__
347
 
 
348
 
=head1 NAME
349
 
 
350
 
WWWBrowser - platform independent mean to start a WWW browser
351
 
 
352
 
=head1 SYNOPSIS
353
 
 
354
 
    use WWWBrowser;
355
 
    WWWBrowser::start_browser($url, -oldwindow => 1);
356
 
 
357
 
=head1 DESCRIPTION
358
 
 
359
 
=head2 start_browser($url [, %args])
360
 
 
361
 
Start a web browser with the specified URL. The process is started in
362
 
background.
363
 
 
364
 
The following optional parameters are recognized:
365
 
 
366
 
=over 4
367
 
 
368
 
=item -oldwindow => $bool
369
 
 
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
372
 
a browser window.
373
 
 
374
 
=item -browser => $browser
375
 
 
376
 
Use (preferebly) the named browser C<$browser>. See L</CONFIGURATION>
377
 
for a some browser specialities. This option will only work for unix.
378
 
 
379
 
=back
380
 
 
381
 
=head1 CONFIGURATION
382
 
 
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
385
 
handled specially:
386
 
 
387
 
=over 4
388
 
 
389
 
=item lynx, w3m
390
 
 
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.
394
 
 
395
 
=item kfmclient
396
 
 
397
 
Use C<openURL> method of kfm.
398
 
 
399
 
=item netscape
400
 
 
401
 
Use C<-remote> option to re-use a running netscape process, if
402
 
possible.
403
 
 
404
 
=item _default_gnome
405
 
 
406
 
Look into the C<~/.gnome/Gnome> configuration file for the right browser.
407
 
 
408
 
=item _default_kde
409
 
 
410
 
NYI.
411
 
 
412
 
=back
413
 
 
414
 
The following variables can be defined globally in the B<main>
415
 
package:
416
 
 
417
 
=over 4
418
 
 
419
 
=item C<$os>
420
 
 
421
 
Short name of operating system (C<win>, C<mac> or C<unix>).
422
 
 
423
 
=item C<&status_messages>
424
 
 
425
 
Error handling function (instead of default C<warn>).
426
 
 
427
 
=back
428
 
 
429
 
=head1 REQUIREMENTS
430
 
 
431
 
For Windows, the L<Win32Util|Win32Util> module should be installed in
432
 
the path.
433
 
 
434
 
=head1 AUTHOR
435
 
 
436
 
Slaven Rezic <slaven@rezic.de>
437
 
 
438
 
=head1 COPYRIGHT
439
 
 
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.
443
 
 
444
 
=head1 SEE ALSO
445
 
 
446
 
L<Win32Util|Win32Util>.