| 1 | package Gtk2::Ex::FileLocator::Helper; | ||
| 2 | use strict; | ||
| 3 | no warnings; | ||
| 4 | |||
| 5 | use Gtk2; | ||
| 6 | use Glib qw(TRUE FALSE); | ||
| 7 | use Gnome2::VFS -init; | ||
| 8 | |||
| 9 | use CGI; | ||
| 10 | use Unicode::MapUTF8; | ||
| 11 | |||
| 12 | use base Exporter::; | ||
| 13 | |||
| 14 | our @EXPORT = qw( | ||
| 15 | image_set_file_icon | ||
| 16 | file_open | ||
| 17 | string_shell_escape | ||
| 18 | string_shell_unescape | ||
| 19 | string_shell_complete | ||
| 20 | ); | ||
| 21 | |||
| 22 | sub image_set_file_icon { | ||
| 23 | my ( $image, $filename, $iconsize ) = @_; | ||
| 24 | |||
| 25 | if ( -e $filename ) { | ||
| 26 | my $theme = Gnome2::IconTheme->new(); | ||
| 27 | my ($icon_name) = $theme->lookup_sync( undef, $filename, undef, "none" ); | ||
| 28 | my ($icon) = $theme->lookup_icon( $icon_name, $iconsize ); | ||
| 29 | |||
| 30 | my $icontheme = Gtk2::IconTheme->get_default; | ||
| 31 | my $pixbuf = $icontheme->load_icon( $icon_name, $iconsize, 'force-svg' ); | ||
| 32 | |||
| 33 | $image->set_from_pixbuf($pixbuf); | ||
| 34 | } else { | ||
| 35 | $image->set_from_stock( 'gtk-none', 'small-toolbar' ); | ||
| 36 | } | ||
| 37 | |||
| 38 | return $image; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub file_open { | ||
| 42 | my $filename = shift; | ||
| 43 | |||
| 44 | my ( $result, $info ) = Gnome2::VFS->get_file_info( $filename, 'get-mime-type' ); | ||
| 45 | if ( $result eq "ok" ) { | ||
| 46 | my $mimeType = Gnome2::VFS::Mime::Type->new( $info->get_mime_type ); | ||
| 47 | my $application = $mimeType->get_default_application; | ||
| 48 | return system sprintf "$application->{command} %s >/dev/null 2>&1 &", string_shell_escape($filename); | ||
| 49 | } else { | ||
| 50 | #warn "result: $result"; | ||
| 51 | } | ||
| 52 | |||
| 53 | return; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub string_shell_escape { | ||
| 57 | my $string = shift; | ||
| 58 | $string =~ s/([\#\'\&\|\s\<\>\{\}\(\)\?\*\!\[\]])/\\$1/sg; | ||
| 59 | $string =~ s/([`"])/?/sg; | ||
| 60 | return $string; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub string_shell_unescape { | ||
| 64 | my $string = shift; | ||
| 65 | $string = CGI::unescape($string); | ||
| 66 | $string = Unicode::MapUTF8::from_utf8( { -string => $string, -charset => 'ISO-8859-1' } ); | ||
| 67 | return $string; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub string_shell_complete { | ||
| 71 | my $chunk = string_shell_escape(shift); | ||
| 72 | my @pattern = map { "$_/$chunk" } "/bin", split ":", $ENV{PATH}; | ||
| 73 | |||
| 74 | chdir $ENV{HOME}; | ||
| 75 | |||
| 76 | my $match = ""; | ||
| 77 | |||
| 78 | foreach my $pattern ( $chunk, @pattern ) { | ||
| 79 | if ( -e $pattern ) { | ||
| 80 | $match = $pattern; | ||
| 81 | goto MATCH; | ||
| 82 | } | ||
| 83 | |||
| 84 | #printf "%s\n", $pattern; | ||
| 85 | |||
| 86 | # file match | ||
| 87 | foreach (`ls -C1 $pattern* >/dev/null 2>&1`) { | ||
| 88 | chomp; | ||
| 89 | last if !$_ or $_ eq '.'; | ||
| 90 | #printf "%s\n", join "\n", $_; | ||
| 91 | if ($_) { | ||
| 92 | $match = string_shell_unescape($_); | ||
| 93 | goto MATCH; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | # directory match | ||
| 98 | foreach (`ls -d -C1 $pattern* 2>/dev/null`) { | ||
| 99 | chomp; | ||
| 100 | s/:$//o; | ||
| 101 | if (-e) { | ||
| 102 | $match = string_shell_unescape( -d $_ ? "$_/" : $_ ); | ||
| 103 | goto MATCH; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | MATCH: | ||
| 109 | $match = "$ENV{HOME}/$match" if $match and $match !~ m|^/|o; | ||
| 110 | $match .= "/" if -d $match; | ||
| 111 | $match =~ s|/+|/|sgo; | ||
| 112 | |||
| 113 | return $match || $chunk; | ||
| 114 | } | ||
| 115 | |||
| 116 | 1; | ||
| 117 | __END__ |