################################################### # # Copyright (C) 2008-2013 Mario Kemper # # 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;