| 1 | package Gtk2::Ex::MindMapView::Content::Uri; | ||
| 2 | |||
| 3 | our $VERSION = '0.000001'; | ||
| 4 | |||
| 5 | use warnings; | ||
| 6 | use strict; | ||
| 7 | use Carp; | ||
| 8 | |||
| 9 | use List::Util; | ||
| 10 | |||
| 11 | use Gnome2::Canvas; | ||
| 12 | |||
| 13 | use Gtk2::Ex::MindMapView::ArgUtils; | ||
| 14 | use Gtk2::Ex::MindMapView::Content; | ||
| 15 | |||
| 16 | use base 'Gtk2::Ex::MindMapView::Content::EllipsisText'; | ||
| 17 | |||
| 18 | sub new | ||
| 19 | { | ||
| 20 | my $class = shift(@_); | ||
| 21 | |||
| 22 | my $self = $class->SUPER::new(@_); | ||
| 23 | |||
| 24 | my %attributes = @_; | ||
| 25 | |||
| 26 | args_valid(\%attributes, qw(group x y width height browser text uri text_color_gdk font_desc)); | ||
| 27 | |||
| 28 | args_required(\%attributes, qw(group browser text uri)); | ||
| 29 | |||
| 30 | $self->{image}->set(underline=>'single'); | ||
| 31 | |||
| 32 | $self->{image}->signal_connect(event=>\&_event_handler, $self); | ||
| 33 | |||
| 34 | return $self; | ||
| 35 | } | ||
| 36 | |||
| 37 | |||
| 38 | # $content->set(uri=>'http://...'); | ||
| 39 | |||
| 40 | sub set | ||
| 41 | { | ||
| 42 | my $self = shift(@_); | ||
| 43 | |||
| 44 | my %attributes = @_; | ||
| 45 | |||
| 46 | foreach my $param_name (keys %attributes) | ||
| 47 | { | ||
| 48 | my $value = $attributes{$param_name}; | ||
| 49 | |||
| 50 | if ($param_name eq 'uri') | ||
| 51 | { | ||
| 52 | $self->{uri} = $value; | ||
| 53 | |||
| 54 | next; | ||
| 55 | } | ||
| 56 | |||
| 57 | $self->SUPER::set($param_name=>$value); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | |||
| 62 | sub _event_handler | ||
| 63 | { | ||
| 64 | my ($item, $event, $self) = @_; | ||
| 65 | |||
| 66 | my $event_type = $event->type; | ||
| 67 | |||
| 68 | if ($event_type eq 'button-press') | ||
| 69 | { | ||
| 70 | my $uri = $self->{uri}; | ||
| 71 | |||
| 72 | if (!($uri =~ /^(?:(\w+):\/\/|mailto:)/)) | ||
| 73 | { | ||
| 74 | croak "Invalid uri in Gtk2::Ex::MindMapView::Content::Url. Uri is: $uri\n"; | ||
| 75 | } | ||
| 76 | |||
| 77 | my $protocol = $1; | ||
| 78 | |||
| 79 | if (!grep { $_ eq $protocol } qw( file http mailto)) | ||
| 80 | { | ||
| 81 | croak "Unknown uri in Gtk2::Ex::MindMapView::Content::Url. Uri is: $uri\n"; | ||
| 82 | } | ||
| 83 | |||
| 84 | $uri =~ s/^file:\/\/// if ($protocol eq 'file'); | ||
| 85 | |||
| 86 | my $command = $self->{browser}; | ||
| 87 | |||
| 88 | $command =~ s/\%s/$uri/; | ||
| 89 | |||
| 90 | unless (fork) | ||
| 91 | { # child process | ||
| 92 | exec "$command"; | ||
| 93 | |||
| 94 | exit 1; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | 1; # Magic true value required at end of module | ||
| 102 | __END__ |