File size: 6,116 Bytes
39c802d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
###################################################
#
#  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::Session;

#Session state of the running Shutter instance.
#
#The object holds a REFERENCE to the session hash owned by bin/shutter
#(%session_screens), so code that still accesses that hash directly and
#code going through this object always see the same data.
#
#Objects that are created late or replaced during startup (the notebook,
#the gettext object) are passed as getter coderefs and resolved on use.

use 5.010;
use utf8;
use strict;
use warnings;

use Glib qw/ TRUE FALSE /;

#  Arguments (all named):
#    screens             - hashref, the session hash
#    notebook            - coderef returning the Gtk3::Notebook
#    gettext             - coderef returning the Locale::gettext object
#    show_status_message - coderef ($index, $message)
sub new {
	my ($class, %args) = @_;

	my $self = {
		_screens             => $args{screens} // {},
		_notebook            => $args{notebook},
		_gettext             => $args{gettext},
		_show_status_message => $args{show_status_message},
	};

	return bless $self, $class;
}

sub screens {
	my $self = shift;
	return $self->{_screens};
}

sub notebook {
	my $self = shift;
	return $self->{_notebook} ? $self->{_notebook}->() : undef;
}

sub gettext {
	my $self = shift;
	return $self->{_gettext} ? $self->{_gettext}->() : undef;
}

sub show_status_message {
	my ($self, @args) = @_;
	return $self->{_show_status_message}->(@args);
}

sub is_uri_in_session {
	my $self    = shift;
	my $giofile = shift;
	my $jump    = shift;

	return FALSE unless $giofile;

	my $session_screens = $self->{_screens};
	foreach my $key (keys %{$session_screens}) {
		if (exists $session_screens->{$key}->{'giofile'}) {
			if ($giofile->equal($session_screens->{$key}->{'giofile'})) {
				if (exists $session_screens->{$key}->{'tab_child'}) {
					if ($jump) {
						my $notebook = $self->notebook;
						$notebook->set_current_page($notebook->page_num($session_screens->{$key}->{'tab_child'}));
					}
					return TRUE;
				}
			}
		}
	}

	return FALSE;
}

sub screenshot_exists {
	my ($self, $key) = @_;

	my $session_screens = $self->{_screens};

	#check if file still exists
	unless ($session_screens->{$key}->{'giofile'}->query_exists) {
		$self->show_status_message(1, $session_screens->{$key}->{'long'} . " " . $self->gettext->get("not found"));
		return FALSE;
	}
	return TRUE;
}

sub get_key_by_filename {
	my $self     = shift;
	my $filename = shift;

	return unless $filename;

	my $session_screens = $self->{_screens};
	my $key             = undef;

	#and loop through hash to find the corresponding key
	foreach my $ckey (keys %{$session_screens}) {
		next unless exists $session_screens->{$ckey}->{'long'};

		#~ print "compare ".$session_screens->{$ckey}->{'long'}." - $filename\n";
		if ($session_screens->{$ckey}->{'long'} eq $filename) {
			$key = $ckey;
			last;
		}
	}

	return $key;
}

sub get_key_by_pubfile {
	my $self     = shift;
	my $filename = shift;

	return unless $filename;

	my $session_screens = $self->{_screens};
	my $key             = undef;

	#and loop through hash to find the corresponding key
	foreach my $ckey (keys %{$session_screens}) {
		next unless exists $session_screens->{$ckey}->{'links'};
		next
			unless exists $session_screens->{$ckey}->{'links'}->{'ubuntu-one'};
		next
			unless exists $session_screens->{$ckey}->{'links'}->{'ubuntu-one'}->{'pubfile'};

		#~ print "compare ".$session_screens->{$ckey}->{'links'}->{'ubuntu-one'}->{'pubfile'}." - $filename\n";
		if ($session_screens->{$ckey}->{'links'}->{'ubuntu-one'}->{'pubfile'} eq $filename) {
			$key = $ckey;
			last;
		}
	}

	return $key;
}

sub get_file_by_index {
	my $self  = shift;
	my $index = shift;

	return unless $index;

	#get current page
	my $curr_page = $self->notebook->get_nth_page($index);

	my $session_screens = $self->{_screens};
	my $key             = undef;

	#and loop through hash to find the corresponding key
	if ($curr_page) {
		foreach my $ckey (keys %{$session_screens}) {
			next unless exists $session_screens->{$ckey}->{'tab_child'};
			if ($session_screens->{$ckey}->{'tab_child'} == $curr_page) {
				$key = $ckey;
				last;
			}
		}
	}

	return $key;
}

sub get_total_size_of_session {
	my $self = shift;

	my $session_screens = $self->{_screens};
	my $total_size      = 0;
	foreach my $ckey (keys %{$session_screens}) {
		next unless $session_screens->{$ckey}->{'size'};
		$total_size += $session_screens->{$ckey}->{'size'};
	}
	return $total_size;
}

sub get_current_file {
	my $self = shift;

	#get current page
	my $notebook  = $self->notebook;
	my $curr_page = $notebook->get_nth_page($notebook->get_current_page);

	my $session_screens = $self->{_screens};
	my $key             = undef;

	#and loop through hash to find the corresponding key
	if ($curr_page) {
		foreach my $ckey (keys %{$session_screens}) {
			next unless (exists $session_screens->{$ckey}->{'tab_child'});
			if ($session_screens->{$ckey}->{'tab_child'} == $curr_page) {
				$key = $ckey;
				last;
			}
		}
	}

	return $key;
}

sub get_latest_tab_key {
	my $self = shift;

	my $max_key = 0;
	foreach my $key (keys %{$self->{_screens}}) {
		$key =~ /\[(\d+)\]/;
		$max_key = $1 if ($1 > $max_key);
	}
	return $max_key + 1;
}

1;