Dialog/Backend/Nautilus.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 22 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 177 CC 17 SCAR 25.6
CC
17
Cov
0%
_get_desktop_dir
42.0
uri_unescape
12.0
paths
12.0
CRAP 12.9
1 package UI::Dialog::Backend::Nautilus;
2 ###############################################################################
3 # Copyright (C) 2004-2016 Kevin C. Krinke <kevin@krinke.ca>
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 use 5.006;
20 use strict;
21 use warnings;
22 use Carp;
23  
24  
25 #
26 # Please read the POD for copyright and licensing issues.
27 #
28  
29 BEGIN {
30 use vars qw($VERSION);
31 $VERSION = '1.14';
32 }
33  
34 sub new {
35 my $proto = shift();
36 my $class = ref($proto) || $proto;
37 my $self = {};
38 bless($self, $class);
39 return($self);
40 }
41  
42 #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
43 #: Internal Methods
44 #:
45  
46 sub _debug {
47 my $self = shift();
48 my $mesg = shift() || 'unknown msg';
49 my $logfile = '/tmp/nautilus_debug.tmp';
50 if (open(NAUTILUSLOGFILE,">>".$logfile)) {
51 print NAUTILUSLOGFILE $mesg."\n";
52 close(NAUTILUSLOGFILE);
53 }
54 }
55 sub _is_env {
56 my $self = shift();
57 return(1)
58 unless not $ENV{'NAUTILUS_SCRIPT_SELECTED_FILE_PATHS'}
59 and not $ENV{'NAUTILUS_SCRIPT_SELECTED_URIS'}
60 and not $ENV{'NAUTILUS_SCRIPT_CURRENT_URI'}
61 and not $ENV{'NAUTILUS_SCRIPT_WINDOW_GEOMETRY'};
62 return(0);
63 }
64  
65 #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
66 #: Public (Nautilus Shell Script) Methods
67 #:
68  
69 #: Thanks to URI::Escape. Because it's not part of the core Perl, we need to
70 #: include it here. UI::Dialog shouldn't force other dependancies. This version
71 #: is modified to strip the prefixing protocol indicator.
72 sub uri_unescape {
73 # Note from RFC1630: "Sequences which start with a percent sign
74 # but are not followed by two hexadecimal characters are reserved
75 # for future extension"
76 my $self = shift();
77 my $str = shift();
78 if (@_ && wantarray) {
79 # not executed for the common case of a single argument
80 my @str = ($str, @_); # need to copy
81 foreach (@str) {
82 s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
83 s!^\w+\://!!;
84 }
85 return(@str);
86 }
87 $str =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg if defined $str;
88 $str =~ s!^\w+\://!!;
89 return($str);
90 }
91  
92 #NAUTILUS_SCRIPT_SELECTED_FILE_PATHS: newline-delimited paths for selected files (only if local)
93 sub paths {
94 my $self = shift();
95 if ($self->_is_env()) {
96 return(split(/\n/,$ENV{'NAUTILUS_SCRIPT_SELECTED_FILE_PATHS'}))
97 unless not $ENV{'NAUTILUS_SCRIPT_SELECTED_FILE_PATHS'};
98 my @paths = ();
99 foreach my $uri ($self->uris()) {
100 my $path = $uri;
101 my $desktop = $self->_get_desktop_dir();
102 $path =~ s!^x\-nautilus\-desktop\:///trash!$ENV{'HOME'}/.Trash!;
103 $path =~ s!^x\-nautilus\-desktop\://!$desktop!;
104 push(@paths,$self->uri_unescape($path));
105 }
106 return(@paths);
107 } else { return(0); }
108 }
109  
110 #NAUTILUS_SCRIPT_SELECTED_URIS: newline-delimited URIs for selected files
111 sub uris {
112 my $self = shift();
113 if ($self->_is_env()) {
114 return(split(/\n/,$ENV{'NAUTILUS_SCRIPT_SELECTED_URIS'}));
115 } else { return(0); }
116 }
117  
118 #NAUTILUS_SCRIPT_CURRENT_URI: URI for current location
119 sub path {
120 my $self = shift();
121 return('error') unless $self->_is_env();
122 my $URI = $ENV{'NAUTILUS_SCRIPT_CURRENT_URI'} || '';
123 my $desktop = $self->_get_desktop_dir();
124 $URI =~ s!^x\-nautilus\-desktop\:///trash!$ENV{'HOME'}/.Trash!;
125 $URI =~ s!^x\-nautilus\-desktop\://!$desktop!;
126 return(($self->uri_unescape($URI)||$URI));
127 }
128  
129 #NAUTILUS_SCRIPT_CURRENT_URI: URI for current location
130 sub uri {
131 my $self = shift();
132 return($ENV{'NAUTILUS_SCRIPT_CURRENT_URI'}) if $self->_is_env();
133 return(0);
134 }
135  
136 #NAUTILUS_SCRIPT_WINDOW_GEOMETRY: position and size of current window
137 sub geometry {
138 my $self = shift();
139 if ($self->_is_env()) {
140 #: Width, Height, X, Y
141 return($1,$2,$3,$4) if $ENV{'NAUTILUS_SCRIPT_WINDOW_GEOMETRY'} =~ /(\d+)x(\d+)\+(\d+)\+(\d+)/;
142 } else { return(0,0,0,0); }
143 }
144  
145 sub _get_desktop_dir {
146 my $self = shift();
147 my $desktop_dir = $ENV{'HOME'} . "/Desktop";
148 if ( eval { require Gnome2::GConf; 1; } ) {
149 use Gnome2::GConf;
150 my $gconf = Gnome2::GConf::Client->get_default();
151 $desktop_dir = $ENV{'HOME'}
152 if $gconf->get_bool( '/apps/nautilus/preferences/desktop_is_home_dir' );
153 } else {
154 my $gconf_xml = $ENV{'HOME'} . '/.gconf/apps/nautilus/preferences/%gconf.xml';
155 if ( -r $gconf_xml ) {
156 if ( open( GCONF, "<" . $gconf_xml ) ) {
157 my $RAW = undef;
158 {
159 local $/;
160 $RAW = <GCONF>;
161 }
162 close( GCONF );
163 # <entry name="desktop_is_home_dir" mtime="1090894369" type="bool" value="true">
164 if ( $RAW =~ m!\s+[^"]+\"desktop_is_home_dir\"[^"]+\"\d*\"[^"]+\"bool\"\svalue=\"false\"\>! ) {
165 $desktop_dir = $ENV{'HOME'};
166 }
167 }
168 }
169 }
170 return( $desktop_dir );
171 }
172  
173 1;