Extract session lookups into Shutter::App::Session
Browse filesMove fct_is_uri_in_session, fct_screenshot_exists, fct_get_key_by_*,
fct_get_file_by_index, fct_get_total_size_of_session, fct_get_current_file
and fct_get_latest_tab_key into an OO Shutter::App::Session class. A
file-scoped $session holds a reference to %session_screens (so subs still
using the hash directly see the same data) plus getter coderefs for
$notebook and $d; the original fct_* names remain as one-line wrappers.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- bin/shutter +15 -133
- share/shutter/resources/modules/Shutter/App/Session.pm +240 -0
- t/Shutter/App/15_session.t +143 -0
bin/shutter
CHANGED
|
@@ -5405,27 +5405,15 @@ modes are currently experimental, your desktop environment might fall back to an
|
|
| 5405 |
return TRUE;
|
| 5406 |
}
|
| 5407 |
|
| 5408 |
-
|
| 5409 |
-
|
| 5410 |
-
|
| 5411 |
-
|
| 5412 |
-
|
| 5413 |
-
|
| 5414 |
-
|
| 5415 |
-
if (exists $session_screens{$key}->{'giofile'}) {
|
| 5416 |
-
if ($giofile->equal($session_screens{$key}->{'giofile'})) {
|
| 5417 |
-
if (exists $session_screens{$key}->{'tab_child'}) {
|
| 5418 |
-
if ($jump) {
|
| 5419 |
-
$notebook->set_current_page($notebook->page_num($session_screens{$key}->{'tab_child'}));
|
| 5420 |
-
}
|
| 5421 |
-
return TRUE;
|
| 5422 |
-
}
|
| 5423 |
-
}
|
| 5424 |
-
}
|
| 5425 |
-
}
|
| 5426 |
|
| 5427 |
-
|
| 5428 |
-
}
|
| 5429 |
|
| 5430 |
sub fct_toggle_status_throbber {
|
| 5431 |
my $status = shift;
|
|
@@ -5512,16 +5500,7 @@ modes are currently experimental, your desktop environment might fall back to an
|
|
| 5512 |
return TRUE;
|
| 5513 |
}
|
| 5514 |
|
| 5515 |
-
sub fct_screenshot_exists {
|
| 5516 |
-
my ($key) = @_;
|
| 5517 |
-
|
| 5518 |
-
#check if file still exists
|
| 5519 |
-
unless ($session_screens{$key}->{'giofile'}->query_exists) {
|
| 5520 |
-
fct_show_status_message(1, $session_screens{$key}->{'long'} . " " . $d->get("not found"));
|
| 5521 |
-
return FALSE;
|
| 5522 |
-
}
|
| 5523 |
-
return TRUE;
|
| 5524 |
-
}
|
| 5525 |
|
| 5526 |
sub fct_trash {
|
| 5527 |
my $key = shift;
|
|
@@ -7412,105 +7391,15 @@ modes are currently experimental, your desktop environment might fall back to an
|
|
| 7412 |
return TRUE;
|
| 7413 |
}
|
| 7414 |
|
| 7415 |
-
sub fct_get_key_by_filename {
|
| 7416 |
-
my $filename = shift;
|
| 7417 |
|
| 7418 |
-
|
| 7419 |
|
| 7420 |
-
|
| 7421 |
-
|
| 7422 |
-
#and loop through hash to find the corresponding key
|
| 7423 |
-
foreach my $ckey (keys %session_screens) {
|
| 7424 |
-
next unless exists $session_screens{$ckey}->{'long'};
|
| 7425 |
-
|
| 7426 |
-
#~ print "compare ".$session_screens{$ckey}->{'long'}." - $filename\n";
|
| 7427 |
-
if ($session_screens{$ckey}->{'long'} eq $filename) {
|
| 7428 |
-
$key = $ckey;
|
| 7429 |
-
last;
|
| 7430 |
-
}
|
| 7431 |
-
}
|
| 7432 |
-
|
| 7433 |
-
return $key;
|
| 7434 |
-
}
|
| 7435 |
-
|
| 7436 |
-
sub fct_get_key_by_pubfile {
|
| 7437 |
-
my $filename = shift;
|
| 7438 |
|
| 7439 |
-
|
| 7440 |
-
|
| 7441 |
-
my $key = undef;
|
| 7442 |
|
| 7443 |
-
|
| 7444 |
-
foreach my $ckey (keys %session_screens) {
|
| 7445 |
-
next unless exists $session_screens{$ckey}->{'links'};
|
| 7446 |
-
next
|
| 7447 |
-
unless exists $session_screens{$ckey}->{'links'}->{'ubuntu-one'};
|
| 7448 |
-
next
|
| 7449 |
-
unless exists $session_screens{$ckey}->{'links'}->{'ubuntu-one'}->{'pubfile'};
|
| 7450 |
-
|
| 7451 |
-
#~ print "compare ".$session_screens{$ckey}->{'links'}->{'ubuntu-one'}->{'pubfile'}." - $filename\n";
|
| 7452 |
-
if ($session_screens{$ckey}->{'links'}->{'ubuntu-one'}->{'pubfile'} eq $filename) {
|
| 7453 |
-
$key = $ckey;
|
| 7454 |
-
last;
|
| 7455 |
-
}
|
| 7456 |
-
}
|
| 7457 |
-
|
| 7458 |
-
return $key;
|
| 7459 |
-
}
|
| 7460 |
-
|
| 7461 |
-
sub fct_get_file_by_index {
|
| 7462 |
-
my $index = shift;
|
| 7463 |
-
|
| 7464 |
-
return unless $index;
|
| 7465 |
-
|
| 7466 |
-
#get current page
|
| 7467 |
-
my $curr_page = $notebook->get_nth_page($index);
|
| 7468 |
-
|
| 7469 |
-
my $key = undef;
|
| 7470 |
-
|
| 7471 |
-
#and loop through hash to find the corresponding key
|
| 7472 |
-
if ($curr_page) {
|
| 7473 |
-
foreach my $ckey (keys %session_screens) {
|
| 7474 |
-
next unless exists $session_screens{$ckey}->{'tab_child'};
|
| 7475 |
-
if ($session_screens{$ckey}->{'tab_child'} == $curr_page) {
|
| 7476 |
-
$key = $ckey;
|
| 7477 |
-
last;
|
| 7478 |
-
}
|
| 7479 |
-
}
|
| 7480 |
-
}
|
| 7481 |
-
|
| 7482 |
-
return $key;
|
| 7483 |
-
}
|
| 7484 |
-
|
| 7485 |
-
sub fct_get_total_size_of_session {
|
| 7486 |
-
my $total_size = 0;
|
| 7487 |
-
foreach my $ckey (keys %session_screens) {
|
| 7488 |
-
next unless $session_screens{$ckey}->{'size'};
|
| 7489 |
-
$total_size += $session_screens{$ckey}->{'size'};
|
| 7490 |
-
}
|
| 7491 |
-
return $total_size;
|
| 7492 |
-
}
|
| 7493 |
-
|
| 7494 |
-
sub fct_get_current_file {
|
| 7495 |
-
|
| 7496 |
-
#get current page
|
| 7497 |
-
my $curr_page = $notebook->get_nth_page($notebook->get_current_page);
|
| 7498 |
-
|
| 7499 |
-
my $key = undef;
|
| 7500 |
-
|
| 7501 |
-
#and loop through hash to find the corresponding key
|
| 7502 |
-
if ($curr_page) {
|
| 7503 |
-
foreach my $ckey (keys %session_screens) {
|
| 7504 |
-
next unless (exists $session_screens{$ckey}->{'tab_child'});
|
| 7505 |
-
if ($session_screens{$ckey}->{'tab_child'} == $curr_page) {
|
| 7506 |
-
$key = $ckey;
|
| 7507 |
-
last;
|
| 7508 |
-
}
|
| 7509 |
-
}
|
| 7510 |
-
}
|
| 7511 |
-
|
| 7512 |
-
return $key;
|
| 7513 |
-
}
|
| 7514 |
|
| 7515 |
sub fct_update_profile_selectors {
|
| 7516 |
my ($combobox_settings_profiles, $current_profiles_ref, $recur_widget) = @_;
|
|
@@ -8058,14 +7947,7 @@ modes are currently experimental, your desktop environment might fall back to an
|
|
| 8058 |
|
| 8059 |
}
|
| 8060 |
|
| 8061 |
-
sub fct_get_latest_tab_key {
|
| 8062 |
-
my $max_key = 0;
|
| 8063 |
-
foreach my $key (keys %session_screens) {
|
| 8064 |
-
$key =~ /\[(\d+)\]/;
|
| 8065 |
-
$max_key = $1 if ($1 > $max_key);
|
| 8066 |
-
}
|
| 8067 |
-
return $max_key + 1;
|
| 8068 |
-
}
|
| 8069 |
|
| 8070 |
sub fct_imagemagick_perform {
|
| 8071 |
my ($function, $file, $data) = @_;
|
|
|
|
| 5405 |
return TRUE;
|
| 5406 |
}
|
| 5407 |
|
| 5408 |
+
require Shutter::App::Session;
|
| 5409 |
+
my $session = Shutter::App::Session->new(
|
| 5410 |
+
screens => \%session_screens,
|
| 5411 |
+
notebook => sub { return $notebook },
|
| 5412 |
+
gettext => sub { return $d },
|
| 5413 |
+
show_status_message => sub { return fct_show_status_message(@_) },
|
| 5414 |
+
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5415 |
|
| 5416 |
+
sub fct_is_uri_in_session { return $session->is_uri_in_session(@_) }
|
|
|
|
| 5417 |
|
| 5418 |
sub fct_toggle_status_throbber {
|
| 5419 |
my $status = shift;
|
|
|
|
| 5500 |
return TRUE;
|
| 5501 |
}
|
| 5502 |
|
| 5503 |
+
sub fct_screenshot_exists { return $session->screenshot_exists(@_) }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5504 |
|
| 5505 |
sub fct_trash {
|
| 5506 |
my $key = shift;
|
|
|
|
| 7391 |
return TRUE;
|
| 7392 |
}
|
| 7393 |
|
| 7394 |
+
sub fct_get_key_by_filename { return $session->get_key_by_filename(@_) }
|
|
|
|
| 7395 |
|
| 7396 |
+
sub fct_get_key_by_pubfile { return $session->get_key_by_pubfile(@_) }
|
| 7397 |
|
| 7398 |
+
sub fct_get_file_by_index { return $session->get_file_by_index(@_) }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7399 |
|
| 7400 |
+
sub fct_get_total_size_of_session { return $session->get_total_size_of_session(@_) }
|
|
|
|
|
|
|
| 7401 |
|
| 7402 |
+
sub fct_get_current_file { return $session->get_current_file(@_) }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7403 |
|
| 7404 |
sub fct_update_profile_selectors {
|
| 7405 |
my ($combobox_settings_profiles, $current_profiles_ref, $recur_widget) = @_;
|
|
|
|
| 7947 |
|
| 7948 |
}
|
| 7949 |
|
| 7950 |
+
sub fct_get_latest_tab_key { return $session->get_latest_tab_key(@_) }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7951 |
|
| 7952 |
sub fct_imagemagick_perform {
|
| 7953 |
my ($function, $file, $data) = @_;
|
share/shutter/resources/modules/Shutter/App/Session.pm
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
###################################################
|
| 2 |
+
#
|
| 3 |
+
# Copyright (C) 2008-2013 Mario Kemper <mario.kemper@gmail.com>
|
| 4 |
+
#
|
| 5 |
+
# This file is part of Shutter.
|
| 6 |
+
#
|
| 7 |
+
# Shutter is free software; you can redistribute it and/or modify
|
| 8 |
+
# it under the terms of the GNU General Public License as published by
|
| 9 |
+
# the Free Software Foundation; either version 3 of the License, or
|
| 10 |
+
# (at your option) any later version.
|
| 11 |
+
#
|
| 12 |
+
# Shutter is distributed in the hope that it will be useful,
|
| 13 |
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 14 |
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 15 |
+
# GNU General Public License for more details.
|
| 16 |
+
#
|
| 17 |
+
# You should have received a copy of the GNU General Public License
|
| 18 |
+
# along with Shutter; if not, write to the Free Software
|
| 19 |
+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
| 20 |
+
#
|
| 21 |
+
###################################################
|
| 22 |
+
|
| 23 |
+
package Shutter::App::Session;
|
| 24 |
+
|
| 25 |
+
#Session state of the running Shutter instance.
|
| 26 |
+
#
|
| 27 |
+
#The object holds a REFERENCE to the session hash owned by bin/shutter
|
| 28 |
+
#(%session_screens), so code that still accesses that hash directly and
|
| 29 |
+
#code going through this object always see the same data.
|
| 30 |
+
#
|
| 31 |
+
#Objects that are created late or replaced during startup (the notebook,
|
| 32 |
+
#the gettext object) are passed as getter coderefs and resolved on use.
|
| 33 |
+
|
| 34 |
+
use 5.010;
|
| 35 |
+
use utf8;
|
| 36 |
+
use strict;
|
| 37 |
+
use warnings;
|
| 38 |
+
|
| 39 |
+
use Glib qw/ TRUE FALSE /;
|
| 40 |
+
|
| 41 |
+
# Arguments (all named):
|
| 42 |
+
# screens - hashref, the session hash
|
| 43 |
+
# notebook - coderef returning the Gtk3::Notebook
|
| 44 |
+
# gettext - coderef returning the Locale::gettext object
|
| 45 |
+
# show_status_message - coderef ($index, $message)
|
| 46 |
+
sub new {
|
| 47 |
+
my ($class, %args) = @_;
|
| 48 |
+
|
| 49 |
+
my $self = {
|
| 50 |
+
_screens => $args{screens} // {},
|
| 51 |
+
_notebook => $args{notebook},
|
| 52 |
+
_gettext => $args{gettext},
|
| 53 |
+
_show_status_message => $args{show_status_message},
|
| 54 |
+
};
|
| 55 |
+
|
| 56 |
+
return bless $self, $class;
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
sub screens {
|
| 60 |
+
my $self = shift;
|
| 61 |
+
return $self->{_screens};
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
sub notebook {
|
| 65 |
+
my $self = shift;
|
| 66 |
+
return $self->{_notebook} ? $self->{_notebook}->() : undef;
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
sub gettext {
|
| 70 |
+
my $self = shift;
|
| 71 |
+
return $self->{_gettext} ? $self->{_gettext}->() : undef;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
sub show_status_message {
|
| 75 |
+
my ($self, @args) = @_;
|
| 76 |
+
return $self->{_show_status_message}->(@args);
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
sub is_uri_in_session {
|
| 80 |
+
my $self = shift;
|
| 81 |
+
my $giofile = shift;
|
| 82 |
+
my $jump = shift;
|
| 83 |
+
|
| 84 |
+
return FALSE unless $giofile;
|
| 85 |
+
|
| 86 |
+
my $session_screens = $self->{_screens};
|
| 87 |
+
foreach my $key (keys %{$session_screens}) {
|
| 88 |
+
if (exists $session_screens->{$key}->{'giofile'}) {
|
| 89 |
+
if ($giofile->equal($session_screens->{$key}->{'giofile'})) {
|
| 90 |
+
if (exists $session_screens->{$key}->{'tab_child'}) {
|
| 91 |
+
if ($jump) {
|
| 92 |
+
my $notebook = $self->notebook;
|
| 93 |
+
$notebook->set_current_page($notebook->page_num($session_screens->{$key}->{'tab_child'}));
|
| 94 |
+
}
|
| 95 |
+
return TRUE;
|
| 96 |
+
}
|
| 97 |
+
}
|
| 98 |
+
}
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
return FALSE;
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
sub screenshot_exists {
|
| 105 |
+
my ($self, $key) = @_;
|
| 106 |
+
|
| 107 |
+
my $session_screens = $self->{_screens};
|
| 108 |
+
|
| 109 |
+
#check if file still exists
|
| 110 |
+
unless ($session_screens->{$key}->{'giofile'}->query_exists) {
|
| 111 |
+
$self->show_status_message(1, $session_screens->{$key}->{'long'} . " " . $self->gettext->get("not found"));
|
| 112 |
+
return FALSE;
|
| 113 |
+
}
|
| 114 |
+
return TRUE;
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
sub get_key_by_filename {
|
| 118 |
+
my $self = shift;
|
| 119 |
+
my $filename = shift;
|
| 120 |
+
|
| 121 |
+
return unless $filename;
|
| 122 |
+
|
| 123 |
+
my $session_screens = $self->{_screens};
|
| 124 |
+
my $key = undef;
|
| 125 |
+
|
| 126 |
+
#and loop through hash to find the corresponding key
|
| 127 |
+
foreach my $ckey (keys %{$session_screens}) {
|
| 128 |
+
next unless exists $session_screens->{$ckey}->{'long'};
|
| 129 |
+
|
| 130 |
+
#~ print "compare ".$session_screens->{$ckey}->{'long'}." - $filename\n";
|
| 131 |
+
if ($session_screens->{$ckey}->{'long'} eq $filename) {
|
| 132 |
+
$key = $ckey;
|
| 133 |
+
last;
|
| 134 |
+
}
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
return $key;
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
sub get_key_by_pubfile {
|
| 141 |
+
my $self = shift;
|
| 142 |
+
my $filename = shift;
|
| 143 |
+
|
| 144 |
+
return unless $filename;
|
| 145 |
+
|
| 146 |
+
my $session_screens = $self->{_screens};
|
| 147 |
+
my $key = undef;
|
| 148 |
+
|
| 149 |
+
#and loop through hash to find the corresponding key
|
| 150 |
+
foreach my $ckey (keys %{$session_screens}) {
|
| 151 |
+
next unless exists $session_screens->{$ckey}->{'links'};
|
| 152 |
+
next
|
| 153 |
+
unless exists $session_screens->{$ckey}->{'links'}->{'ubuntu-one'};
|
| 154 |
+
next
|
| 155 |
+
unless exists $session_screens->{$ckey}->{'links'}->{'ubuntu-one'}->{'pubfile'};
|
| 156 |
+
|
| 157 |
+
#~ print "compare ".$session_screens->{$ckey}->{'links'}->{'ubuntu-one'}->{'pubfile'}." - $filename\n";
|
| 158 |
+
if ($session_screens->{$ckey}->{'links'}->{'ubuntu-one'}->{'pubfile'} eq $filename) {
|
| 159 |
+
$key = $ckey;
|
| 160 |
+
last;
|
| 161 |
+
}
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
return $key;
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
sub get_file_by_index {
|
| 168 |
+
my $self = shift;
|
| 169 |
+
my $index = shift;
|
| 170 |
+
|
| 171 |
+
return unless $index;
|
| 172 |
+
|
| 173 |
+
#get current page
|
| 174 |
+
my $curr_page = $self->notebook->get_nth_page($index);
|
| 175 |
+
|
| 176 |
+
my $session_screens = $self->{_screens};
|
| 177 |
+
my $key = undef;
|
| 178 |
+
|
| 179 |
+
#and loop through hash to find the corresponding key
|
| 180 |
+
if ($curr_page) {
|
| 181 |
+
foreach my $ckey (keys %{$session_screens}) {
|
| 182 |
+
next unless exists $session_screens->{$ckey}->{'tab_child'};
|
| 183 |
+
if ($session_screens->{$ckey}->{'tab_child'} == $curr_page) {
|
| 184 |
+
$key = $ckey;
|
| 185 |
+
last;
|
| 186 |
+
}
|
| 187 |
+
}
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
return $key;
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
sub get_total_size_of_session {
|
| 194 |
+
my $self = shift;
|
| 195 |
+
|
| 196 |
+
my $session_screens = $self->{_screens};
|
| 197 |
+
my $total_size = 0;
|
| 198 |
+
foreach my $ckey (keys %{$session_screens}) {
|
| 199 |
+
next unless $session_screens->{$ckey}->{'size'};
|
| 200 |
+
$total_size += $session_screens->{$ckey}->{'size'};
|
| 201 |
+
}
|
| 202 |
+
return $total_size;
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
sub get_current_file {
|
| 206 |
+
my $self = shift;
|
| 207 |
+
|
| 208 |
+
#get current page
|
| 209 |
+
my $notebook = $self->notebook;
|
| 210 |
+
my $curr_page = $notebook->get_nth_page($notebook->get_current_page);
|
| 211 |
+
|
| 212 |
+
my $session_screens = $self->{_screens};
|
| 213 |
+
my $key = undef;
|
| 214 |
+
|
| 215 |
+
#and loop through hash to find the corresponding key
|
| 216 |
+
if ($curr_page) {
|
| 217 |
+
foreach my $ckey (keys %{$session_screens}) {
|
| 218 |
+
next unless (exists $session_screens->{$ckey}->{'tab_child'});
|
| 219 |
+
if ($session_screens->{$ckey}->{'tab_child'} == $curr_page) {
|
| 220 |
+
$key = $ckey;
|
| 221 |
+
last;
|
| 222 |
+
}
|
| 223 |
+
}
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
return $key;
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
sub get_latest_tab_key {
|
| 230 |
+
my $self = shift;
|
| 231 |
+
|
| 232 |
+
my $max_key = 0;
|
| 233 |
+
foreach my $key (keys %{$self->{_screens}}) {
|
| 234 |
+
$key =~ /\[(\d+)\]/;
|
| 235 |
+
$max_key = $1 if ($1 > $max_key);
|
| 236 |
+
}
|
| 237 |
+
return $max_key + 1;
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
1;
|
t/Shutter/App/15_session.t
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/perl
|
| 2 |
+
use 5.010;
|
| 3 |
+
use strict;
|
| 4 |
+
use warnings;
|
| 5 |
+
|
| 6 |
+
use Test::More;
|
| 7 |
+
use Test::MockModule;
|
| 8 |
+
use Glib qw/ TRUE FALSE /;
|
| 9 |
+
|
| 10 |
+
require_ok("Shutter::App::Session");
|
| 11 |
+
|
| 12 |
+
can_ok(
|
| 13 |
+
"Shutter::App::Session", qw/
|
| 14 |
+
new screens notebook gettext show_status_message
|
| 15 |
+
is_uri_in_session screenshot_exists get_key_by_filename get_key_by_pubfile
|
| 16 |
+
get_file_by_index get_total_size_of_session get_current_file get_latest_tab_key
|
| 17 |
+
/
|
| 18 |
+
);
|
| 19 |
+
|
| 20 |
+
# minimal stand-ins for Glib::IO::File, Gtk3::Notebook and Locale::gettext
|
| 21 |
+
my $mock_file = Test::MockModule->new("Local::File", no_auto => 1);
|
| 22 |
+
$mock_file->define(new => sub { my ($class, %a) = @_; return bless {%a}, $class });
|
| 23 |
+
$mock_file->define(equal => sub { my ($self, $other) = @_; return $self->{path} eq $other->{path} });
|
| 24 |
+
$mock_file->define(query_exists => sub { my $self = shift; return $self->{exists} });
|
| 25 |
+
|
| 26 |
+
my $mock_notebook = Test::MockModule->new("Local::Notebook", no_auto => 1);
|
| 27 |
+
$mock_notebook->define(new => sub { my ($class, @pages) = @_; return bless {pages => [@pages], current => 0}, $class });
|
| 28 |
+
$mock_notebook->define(get_nth_page => sub { my ($self, $n) = @_; return $self->{pages}->[$n] });
|
| 29 |
+
$mock_notebook->define(get_current_page => sub { my $self = shift; return $self->{current} });
|
| 30 |
+
$mock_notebook->define(set_current_page => sub { my ($self, $n) = @_; $self->{current} = $n; return });
|
| 31 |
+
$mock_notebook->define(
|
| 32 |
+
page_num => sub {
|
| 33 |
+
my ($self, $child) = @_;
|
| 34 |
+
for my $i (0 .. $#{$self->{pages}}) {
|
| 35 |
+
return $i if $self->{pages}->[$i] == $child;
|
| 36 |
+
}
|
| 37 |
+
return -1;
|
| 38 |
+
});
|
| 39 |
+
|
| 40 |
+
my $mock_gettext = Test::MockModule->new("Local::Gettext", no_auto => 1);
|
| 41 |
+
$mock_gettext->define(new => sub { return bless {}, shift });
|
| 42 |
+
$mock_gettext->define(get => sub { my ($self, $s) = @_; return "<$s>" });
|
| 43 |
+
|
| 44 |
+
my ($child0, $child1, $child2) = map { bless {id => $_}, "Local::Child" } 0 .. 2;
|
| 45 |
+
my $notebook = Local::Notebook->new($child0, $child1, $child2);
|
| 46 |
+
my @status;
|
| 47 |
+
|
| 48 |
+
my %session_screens = (
|
| 49 |
+
"[1]" => {
|
| 50 |
+
long => "/tmp/a.png",
|
| 51 |
+
giofile => Local::File->new(path => "/tmp/a.png", exists => TRUE),
|
| 52 |
+
tab_child => $child1,
|
| 53 |
+
size => 100,
|
| 54 |
+
links => {"ubuntu-one" => {pubfile => "http://example.com/a"}},
|
| 55 |
+
},
|
| 56 |
+
"[3]" => {
|
| 57 |
+
long => "/tmp/b.png",
|
| 58 |
+
giofile => Local::File->new(path => "/tmp/b.png", exists => FALSE),
|
| 59 |
+
tab_child => $child2,
|
| 60 |
+
size => 250,
|
| 61 |
+
},
|
| 62 |
+
);
|
| 63 |
+
|
| 64 |
+
my $session = Shutter::App::Session->new(
|
| 65 |
+
screens => \%session_screens,
|
| 66 |
+
notebook => sub { return $notebook },
|
| 67 |
+
gettext => sub { return Local::Gettext->new },
|
| 68 |
+
show_status_message => sub { push @status, [@_]; return },
|
| 69 |
+
);
|
| 70 |
+
|
| 71 |
+
isa_ok($session, "Shutter::App::Session");
|
| 72 |
+
|
| 73 |
+
subtest "shares the hash by reference" => sub {
|
| 74 |
+
is($session->screens, \%session_screens, "same hashref");
|
| 75 |
+
$session_screens{"[7]"} = {long => "/tmp/c.png", size => 1};
|
| 76 |
+
is($session->get_key_by_filename("/tmp/c.png"), "[7]", "sees keys added to the original hash");
|
| 77 |
+
is($session->get_latest_tab_key, 8, "latest tab key follows");
|
| 78 |
+
delete $session_screens{"[7]"};
|
| 79 |
+
is($session->get_key_by_filename("/tmp/c.png"), undef, "sees deletions");
|
| 80 |
+
};
|
| 81 |
+
|
| 82 |
+
subtest "get_key_by_filename" => sub {
|
| 83 |
+
is($session->get_key_by_filename("/tmp/a.png"), "[1]", "found a");
|
| 84 |
+
is($session->get_key_by_filename("/tmp/b.png"), "[3]", "found b");
|
| 85 |
+
is($session->get_key_by_filename("/tmp/nope"), undef, "unknown");
|
| 86 |
+
is($session->get_key_by_filename(undef), undef, "undef");
|
| 87 |
+
is(scalar(() = $session->get_key_by_filename("")), 0, "empty list for false filename");
|
| 88 |
+
};
|
| 89 |
+
|
| 90 |
+
subtest "get_key_by_pubfile" => sub {
|
| 91 |
+
is($session->get_key_by_pubfile("http://example.com/a"), "[1]", "found");
|
| 92 |
+
is($session->get_key_by_pubfile("http://example.com/b"), undef, "unknown");
|
| 93 |
+
is($session->get_key_by_pubfile(undef), undef, "undef");
|
| 94 |
+
};
|
| 95 |
+
|
| 96 |
+
subtest "get_total_size_of_session" => sub {
|
| 97 |
+
is($session->get_total_size_of_session, 350, "sum of sizes");
|
| 98 |
+
is(Shutter::App::Session->new(screens => {})->get_total_size_of_session, 0, "empty session");
|
| 99 |
+
};
|
| 100 |
+
|
| 101 |
+
subtest "get_latest_tab_key" => sub {
|
| 102 |
+
is($session->get_latest_tab_key, 4, "max + 1");
|
| 103 |
+
is(Shutter::App::Session->new(screens => {})->get_latest_tab_key, 1, "empty session");
|
| 104 |
+
};
|
| 105 |
+
|
| 106 |
+
subtest "notebook lookups" => sub {
|
| 107 |
+
$notebook->{current} = 2;
|
| 108 |
+
is($session->get_current_file, "[3]", "current page -> key");
|
| 109 |
+
$notebook->{current} = 0;
|
| 110 |
+
is($session->get_current_file, undef, "page without screenshot");
|
| 111 |
+
is($session->get_file_by_index(1), "[1]", "index 1");
|
| 112 |
+
is($session->get_file_by_index(2), "[3]", "index 2");
|
| 113 |
+
is($session->get_file_by_index(0), undef, "index 0 is treated as no index");
|
| 114 |
+
is($session->get_file_by_index(9), undef, "out of range");
|
| 115 |
+
};
|
| 116 |
+
|
| 117 |
+
subtest "notebook getter is resolved lazily" => sub {
|
| 118 |
+
my $nb;
|
| 119 |
+
my $late = Shutter::App::Session->new(screens => \%session_screens, notebook => sub { return $nb });
|
| 120 |
+
is($late->notebook, undef, "not yet created");
|
| 121 |
+
$nb = $notebook;
|
| 122 |
+
is($late->notebook, $notebook, "picked up once created");
|
| 123 |
+
};
|
| 124 |
+
|
| 125 |
+
subtest "is_uri_in_session" => sub {
|
| 126 |
+
$notebook->{current} = 0;
|
| 127 |
+
is($session->is_uri_in_session(undef), FALSE, "undef file");
|
| 128 |
+
is($session->is_uri_in_session(Local::File->new(path => "/tmp/x.png")), FALSE, "not in session");
|
| 129 |
+
is($session->is_uri_in_session(Local::File->new(path => "/tmp/b.png")), TRUE, "in session");
|
| 130 |
+
is($notebook->{current}, 0, "no jump without flag");
|
| 131 |
+
is($session->is_uri_in_session(Local::File->new(path => "/tmp/b.png"), TRUE), TRUE, "in session, jump");
|
| 132 |
+
is($notebook->{current}, 2, "jumped to tab");
|
| 133 |
+
};
|
| 134 |
+
|
| 135 |
+
subtest "screenshot_exists" => sub {
|
| 136 |
+
@status = ();
|
| 137 |
+
is($session->screenshot_exists("[1]"), TRUE, "existing file");
|
| 138 |
+
is(scalar @status, 0, "no status message");
|
| 139 |
+
is($session->screenshot_exists("[3]"), FALSE, "missing file");
|
| 140 |
+
is_deeply(\@status, [[1, "/tmp/b.png <not found>"]], "status message shown");
|
| 141 |
+
};
|
| 142 |
+
|
| 143 |
+
done_testing();
|