File size: 4,838 Bytes
c7de05d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
###################################################
#
#  Copyright (C) 2008-2013 Mario Kemper <mario.kemper@gmail.com>
#
#  This file is part of Shutter.
#
#  Shutter is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3 of the License, or
#  (at your option) any later version.
#
#  Shutter is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with Shutter; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#

package Shutter::App::Environment;

#modules
#--------------------------------------
use 5.010;
use utf8;
use strict;
use warnings;

use Gtk3;
use Net::DBus;
use File::Glob qw/ bsd_glob /;
use File::Which;

#Glib
use Glib qw/TRUE FALSE/;

require Shutter::App::Directories;

use Exporter qw(import);
our @EXPORT_OK = qw(control_wm_settings init_unsaved_files init_debug_output);

#--------------------------------------

# Disable (mode 'start') or restore (mode 'stop') compiz focus prevention via DBus.
# Returns the previous focus_prevention_level, or -1 if compiz is not reachable.
sub control_wm_settings {
	my $mode          = shift;
	my $restore_value = shift;

	#compiz via dbus
	my $bus    = undef;
	my $compiz = undef;
	my $fpl    = undef;

	#disable focus_prevention
	my $curr_value = -1;

	#disable focus prevention when using compiz
	eval {
		$bus = Net::DBus->find;

		#Get a handle to the compiz service
		$compiz = $bus->get_service("org.freedesktop.compiz");

		#Get the relevant object
		$fpl = $compiz->get_object("/org/freedesktop/compiz/core/screen0/focus_prevention_level", "org.freedesktop.compiz");
	};
	if ($@) {
		warn "INFO: DBus connection to org.freedesktop.compiz failed --> skipping compiz related tasks\n\n";
		warn $@ . "\n\n";
		return $curr_value;
	}

	if (defined $fpl && $fpl) {
		eval {
			if ($mode eq 'start') {

				#save and return current value
				if (defined $fpl && $fpl) {
					$curr_value = $fpl->get;
				}
				if (defined $fpl && $fpl && $fpl->get != 0) {
					$fpl->set(0);
				}

				#re-enable focus prevention -> restore value
			} elsif ($mode eq 'stop') {
				if (defined $fpl && $fpl && defined $restore_value) {
					$fpl->set($restore_value);
				} elsif (defined $fpl && $fpl) {
					$fpl->set(1);
				}
			}
		};
		if ($@) {
			warn "ERROR: Unable to set/get focus_level_prevention --> skipping compiz related tasks\n\n";
			warn $@ . "\n\n";
		}
	}

	return $curr_value;
}

# Delete files in the 'unsaved' cache dir that are not part of the current session.
# $sc: Shutter::App::Common; $get_key_by_filename: coderef returning the session key for a file.
sub init_unsaved_files {
	my ($sc, $get_key_by_filename) = @_;

	#delete all files in this folder
	#except the ones that are in the current session
	my @unsaved_files = bsd_glob(Shutter::App::Directories::get_cache_dir() . "/*");
	foreach my $unsaved_file (@unsaved_files) {
		utf8::decode $unsaved_file;
		print $unsaved_file, " checking \n" if $sc->get_debug;
		unless ($get_key_by_filename->($unsaved_file)) {
			print $unsaved_file, " deleted \n" if $sc->get_debug;
			unlink $unsaved_file;
		}
	}
	return;
}

# Print system/library version information (used with --debug).
# $version, $rev: SHUTTER_VERSION and SHUTTER_REV from bin/shutter.
sub init_debug_output {
	my ($version, $rev) = @_;

	print "\nINFO: gathering system information...";
	print "\n";
	print "\n";
	print "Shutter ";
	print $version;
	print ' ';
	print $rev;
	print "\n";

	#kernel info
	if (File::Which::which('uname')) {
		print `uname -a`, "\n";
	}

	eval {
		open my $fh, '<', '/etc/os-release' or die;    ## no critic (InputOutput::RequireBriefOpen)
		my %map = map {
			chomp;
			my ($key, $value) = split /=/, $_, 2;
			$value =~ s/^(['"])(.*)\1$/$2/;
			($key, $value)
		} <$fh>;
		local $, = ' ';
		say grep { $_ } map { $map{$_} } qw/NAME VERSION_ID BUILD_ID/;
	};
	say "Cannot open /etc/os-release" if $@;

	printf "Glib %s \n", $Glib::VERSION;
	printf "Gtk3 %s \n", $Gtk3::VERSION;
	print "\n";

	# The version info stuff appeared in 1.040.
	print "Glib built for " . join(".", Glib->GET_VERSION_INFO) . ", running with " . join(".", Glib::major_version(), Glib::minor_version(), Glib::micro_version()) . "\n"
		if $Glib::VERSION >= 1.040;
	print "Gtk3 built for " . join(".", Gtk3->GET_VERSION_INFO) . ", running with " . join(".", Gtk3::major_version(), Gtk3::minor_version(), Gtk3::micro_version()) . "\n"
		if $Gtk3::VERSION >= 1.040;
	print "\n";

	return TRUE;
}

1;