File Coverage

blib/lib/UI/Dialog/Backend/Nautilus.pm
Criterion Covered Total %
statement 30 103 29.1
branch 1 36 2.7
condition 3 47 6.3
subroutine 9 18 50.0
pod 7 7 100.0
total 50 211 23.7


line stmt bran cond sub pod time code
1             package UI::Dialog::Backend::Nautilus;
2             ###############################################################################
3             # Copyright (C) 2004-2016 Kevin C. Krinke
4             #
5             # This library is free software; you can redistribute it and/or
6             # modify it under the terms of the GNU Lesser General Public
7             # License as published by the Free Software Foundation; either
8             # version 2.1 of the License, or (at your option) any later version.
9             #
10             # This library is distributed in the hope that it will be useful,
11             # but WITHOUT ANY WARRANTY; without even the implied warranty of
12             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13             # Lesser General Public License for more details.
14             #
15             # You should have received a copy of the GNU Lesser General Public
16             # License along with this library; if not, write to the Free Software
17             # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18             ###############################################################################
19 1     1   14421 use 5.006;
  1         4  
20 1     1   4 use strict;
  1         1  
  1         15  
21 1     1   4 use warnings;
  1         1  
  1         21  
22 1     1   3 use Carp;
  1         2  
  1         76  
23 1     1   369 use UI::Dialog::Backend;
  1         2  
  1         32  
24 1     1   372 use FileHandle;
  1         1670  
  1         4  
25              
26             BEGIN {
27 1     1   242 use vars qw( $VERSION @ISA );
  1         1  
  1         50  
28 1     1   7 @ISA = qw( UI::Dialog::Backend );
29 1         770 $VERSION = '1.21';
30             }
31              
32             sub new {
33 1     1 1 711 my $proto = shift();
34 1   33     6 my $class = ref($proto) || $proto;
35 1         2 my $self = {};
36 1         1 bless($self, $class);
37 1   33     19 $self->{'_opts'}->{'bin'} ||= $self->_find_bin('nautilus');
38 1   33     7 $self->{'_opts'}->{'bin'} ||= $self->_find_bin('caja');
39 1 50       8 unless (-x $self->{'_opts'}->{'bin'}) {
40 1         203 croak("the nautilus binary could not be found at: ".$self->{'_opts'}->{'bin'});
41             }
42 0           return($self);
43             }
44              
45             #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
46             #: Internal Methods
47             #:
48              
49             sub _debug {
50 0     0     my $self = shift();
51 0   0       my $mesg = shift() || 'unknown msg';
52 0           my $logfile = '/tmp/nautilus_debug.tmp';
53 0 0         if (open(NAUTILUSLOGFILE,">>".$logfile)) {
54 0           print NAUTILUSLOGFILE $mesg."\n";
55 0           close(NAUTILUSLOGFILE);
56             }
57             }
58             sub _is_env {
59 0     0     my $self = shift();
60             return(1)
61             unless not $ENV{'NAUTILUS_SCRIPT_SELECTED_FILE_PATHS'}
62             and not $ENV{'NAUTILUS_SCRIPT_SELECTED_URIS'}
63             and not $ENV{'NAUTILUS_SCRIPT_CURRENT_URI'}
64 0 0 0       and not $ENV{'NAUTILUS_SCRIPT_WINDOW_GEOMETRY'};
      0        
      0        
65 0           return(0);
66             }
67              
68             #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
69             #: Public (Nautilus Shell Script) Methods
70             #:
71              
72             #: Thanks to URI::Escape. Because it's not part of the core Perl, we need to
73             #: include it here. UI::Dialog shouldn't force other dependancies. This version
74             #: is modified to strip the prefixing protocol indicator.
75             sub uri_unescape {
76             # Note from RFC1630: "Sequences which start with a percent sign
77             # but are not followed by two hexadecimal characters are reserved
78             # for future extension"
79 0     0 1   my $self = shift();
80 0           my $str = shift();
81 0 0 0       if (@_ && wantarray) {
82             # not executed for the common case of a single argument
83 0           my @str = ($str, @_); # need to copy
84 0           foreach (@str) {
85 0           s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
  0            
86 0           s!^\w+\://!!;
87             }
88 0           return(@str);
89             }
90 0 0         $str =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg if defined $str;
  0            
91 0           $str =~ s!^\w+\://!!;
92 0           return($str);
93             }
94              
95             #NAUTILUS_SCRIPT_SELECTED_FILE_PATHS: newline-delimited paths for selected files (only if local)
96             sub paths {
97 0     0 1   my $self = shift();
98 0 0         if ($self->_is_env()) {
99             return(split(/\n/,$ENV{'NAUTILUS_SCRIPT_SELECTED_FILE_PATHS'}))
100 0 0         unless not $ENV{'NAUTILUS_SCRIPT_SELECTED_FILE_PATHS'};
101 0           my @paths = ();
102 0           foreach my $uri ($self->uris()) {
103 0           my $path = $uri;
104 0           my $desktop = $self->_get_desktop_dir();
105 0           $path =~ s!^x\-nautilus\-desktop\:///trash!$ENV{'HOME'}/.Trash!;
106 0           $path =~ s!^x\-nautilus\-desktop\://!$desktop!;
107 0           push(@paths,$self->uri_unescape($path));
108             }
109 0           return(@paths);
110 0           } else { return(0); }
111             }
112              
113             #NAUTILUS_SCRIPT_SELECTED_URIS: newline-delimited URIs for selected files
114             sub uris {
115 0     0 1   my $self = shift();
116 0 0         if ($self->_is_env()) {
117 0           return(split(/\n/,$ENV{'NAUTILUS_SCRIPT_SELECTED_URIS'}));
118 0           } else { return(0); }
119             }
120              
121             #NAUTILUS_SCRIPT_CURRENT_URI: URI for current location
122             sub path {
123 0     0 1   my $self = shift();
124 0 0         return('error') unless $self->_is_env();
125 0   0       my $URI = $ENV{'NAUTILUS_SCRIPT_CURRENT_URI'} || '';
126 0           my $desktop = $self->_get_desktop_dir();
127 0           $URI =~ s!^x\-nautilus\-desktop\:///trash!$ENV{'HOME'}/.Trash!;
128 0           $URI =~ s!^x\-nautilus\-desktop\://!$desktop!;
129 0   0       return(($self->uri_unescape($URI)||$URI));
130             }
131              
132             #NAUTILUS_SCRIPT_CURRENT_URI: URI for current location
133             sub uri {
134 0     0 1   my $self = shift();
135 0 0         return($ENV{'NAUTILUS_SCRIPT_CURRENT_URI'}) if $self->_is_env();
136 0           return(0);
137             }
138              
139             #NAUTILUS_SCRIPT_WINDOW_GEOMETRY: position and size of current window
140             sub geometry {
141 0     0 1   my $self = shift();
142 0 0         if ($self->_is_env()) {
143             #: Width, Height, X, Y
144 0 0         return($1,$2,$3,$4) if $ENV{'NAUTILUS_SCRIPT_WINDOW_GEOMETRY'} =~ /(\d+)x(\d+)\+(\d+)\+(\d+)/;
145 0           } else { return(0,0,0,0); }
146             }
147              
148             sub _get_desktop_dir {
149 0     0     my $self = shift();
150             # Figure out the user's home directory
151 0           my @user = getpwuid($>);
152 0           my $home = undef;
153 0 0 0       if (@user > 7 && -d $user[7]) {
154 0           $home = $user[7];
155             }
156             # check for XDG paths to home directory
157 0   0       my $xdg_desktop_dir = $ENV{XDG_DESKTOP_DIR} || undef;
158 0 0 0       if (defined $xdg_desktop_dir && -d $xdg_desktop_dir) {
159 0           return $xdg_desktop_dir;
160             }
161 0   0       my $xdg_config_home = $ENV{XDG_CONFIG_HOME} || $home.'/.config';
162 0 0 0       if (-d $xdg_config_home && -f $xdg_config_home.'/user-dirs.dirs') {
163 0           foreach (`. "${xdg_config_home}/user-dirs.dirs"; env`) {
164 0           chomp;
165 0 0         next unless /=/;
166 0           my ($var, $value) = split(/=/, $_);
167 0           $ENV{$var} = $value;
168             }
169             }
170 0   0       $xdg_desktop_dir = $ENV{XDG_DESKTOP_DIR} || undef;
171 0 0 0       if (defined $xdg_desktop_dir && -d $xdg_desktop_dir) {
172 0           return $xdg_desktop_dir;
173             }
174 0 0         return $home."/Desktop" if -d $home."/Desktop";
175             # finally defaulting to just home
176 0           return $home;
177             }
178              
179             1;