Skip to content

Fix #557: Add Base64 export #609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 121 additions & 77 deletions bin/shutter
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ use Proc::Simple;
#sort lexically, but sort numeral parts numerically
use Sort::Naturally;

#for Base64 export
use MIME::Base64 ('encode_base64');

use English;

sub escape_string {
Expand Down Expand Up @@ -544,6 +547,7 @@ sub STARTUP {
$sm->{_menuitem_save_as}->signal_connect('activate', \&evt_save_as, 'menu_save_as');

#~ $sm->{_menuitem_export_svg}->signal_connect( 'activate', \&evt_save_as, 'menu_export_svg' );
$sm->{_menuitem_export_b64}->signal_connect('activate', \&evt_save_as, 'menu_export_b64');
$sm->{_menuitem_export_pdf}->signal_connect('activate', \&evt_save_as, 'menu_export_pdf');
$sm->{_menuitem_export_pscript}->signal_connect('activate', \&evt_save_as, 'menu_export_ps');
$sm->{_menuitem_print}->signal_connect('activate', \&fct_print, 'menu_print');
Expand Down Expand Up @@ -3542,6 +3546,7 @@ sub STARTUP {
$sm->{_menuitem_save_as}->set_sensitive($n_items);

#~ $sm->{_menuitem_export_svg}->set_sensitive($n_items);
$sm->{_menuitem_export_b64}->set_sensitive($n_items);
$sm->{_menuitem_export_pdf}->set_sensitive($n_items);
$sm->{_menuitem_export_pscript}->set_sensitive($n_items);
$sm->{_menuitem_pagesetup}->set_sensitive($n_items);
Expand Down Expand Up @@ -3789,6 +3794,8 @@ sub STARTUP {
my $rfiletype = undef;
if ($data eq 'menu_export_svg') {
$rfiletype = 'svg';
} elsif ($data eq 'menu_export_b64') {
$rfiletype = 'b64';
} elsif ($data eq 'menu_export_ps') {
$rfiletype = 'ps';
} elsif ($data eq 'menu_export_pdf') {
Expand Down Expand Up @@ -9630,7 +9637,11 @@ sub STARTUP {
#change extension related to the requested filetype
if (defined $rfiletype && defined $rfilename) {
my ($short, $folder, $ext) = fileparse($rfilename, qr/\.[^.]*/);
$fs->set_current_name($short . "." . $rfiletype);
if ($rfiletype eq 'b64') {
$fs->set_current_name($short . $ext . "." . $rfiletype);
} else {
$fs->set_current_name($short . "." . $rfiletype);
}
}

my $extra_hbox = Gtk3::HBox->new;
Expand All @@ -9654,6 +9665,11 @@ sub STARTUP {
$combobox_save_as_type->insert_text($counter, "ps - PostScript");
$combobox_save_as_type->set_active(0);

} elsif (defined $rfiletype && $rfiletype eq 'b64') {

$combobox_save_as_type->insert_text($counter, "b64 - Base64");
$combobox_save_as_type->set_active(0);

#images
} else {

Expand Down Expand Up @@ -9762,54 +9778,70 @@ sub STARTUP {

unless ($shf->file_exists($filename)) {

#get pixbuf from param
my $pixbuf = $rpixbuf;
unless ($pixbuf) {
if ($rfiletype eq 'b64') {

open (IMAGE, $rfilename) || die "$!";
binmode(IMAGE);
local $/;
my $file_contents = <IMAGE>;
close IMAGE;
open (B64, ">$filename") || die $!;
print B64 encode_base64($file_contents);
close B64;
print "Exported file $filename, b64\n";

} else {

#get pixbuf from param
my $pixbuf = $rpixbuf;
unless ($pixbuf) {

#or load pixbuf from existing file
$pixbuf = $lp_ne->load($rfilename);
}
#or load pixbuf from existing file
$pixbuf = $lp_ne->load($rfilename);
}

#save as (pixbuf, new_filename, filetype, quality - auto here, old_filename)
if ($sp->save_pixbuf_to_file($pixbuf, $filename, $choosen_format, $rquality)) {
#save as (pixbuf, new_filename, filetype, quality - auto here, old_filename)
if ($sp->save_pixbuf_to_file($pixbuf, $filename, $choosen_format, $rquality)) {

if ($key) {
if ($key) {

#do not try to update when exporting to pdf or ps
unless (defined $rfiletype
&& ($rfiletype eq 'pdf' || $rfiletype eq 'ps'))
{
#do not try to update when exporting to pdf or ps
unless (defined $rfiletype
&& ($rfiletype eq 'pdf' || $rfiletype eq 'ps'))
{

#cancel handle
if (exists $session_screens{$key}->{'handle'}) {
#cancel handle
if (exists $session_screens{$key}->{'handle'}) {

$session_screens{$key}->{'handle'}->cancel;
}
if (fct_update_tab($key, undef, Glib::IO::File::new_for_path($filename), FALSE, 'clear')) {
$session_screens{$key}->{'handle'}->cancel;
}
if (fct_update_tab($key, undef, Glib::IO::File::new_for_path($filename), FALSE, 'clear')) {

#setup a new filemonitor, so we get noticed if the file changed
fct_add_file_monitor($key);
#setup a new filemonitor, so we get noticed if the file changed
fct_add_file_monitor($key);

fct_show_status_message(1, "$session_screens{ $key }->{ 'long' } " . $d->get("saved"));
}
fct_show_status_message(1, "$session_screens{ $key }->{ 'long' } " . $d->get("saved"));
}

} else {
if ($shf->file_exists($filename)) {
fct_show_status_message(1, "$filename " . $d->get("saved"));
} else {
if ($shf->file_exists($filename)) {
fct_show_status_message(1, "$filename " . $d->get("saved"));
}
}

}

}
#successfully saved
$fs->destroy();
return $filename;

#successfully saved
$fs->destroy();
return $filename;
} else {

} else {
#error while saving
$fs->destroy();
return FALSE;

#error while saving
$fs->destroy();
return FALSE;
}

}

Expand All @@ -9827,68 +9859,80 @@ sub STARTUP {
);

if ($response == 40) {
if ($rfiletype eq 'b64') {

open (IMAGE, $rfilename) || die "$!";
binmode(IMAGE);
local $/;
my $file_contents = <IMAGE>;
close IMAGE;
open (B64, ">$filename") || die $!;
print B64 encode_base64($file_contents);
close B64;
print "Exported file $filename, b64\n";

} else {
#get pixbuf from param
my $pixbuf = $rpixbuf;
unless ($pixbuf) {

#get pixbuf from param
my $pixbuf = $rpixbuf;
unless ($pixbuf) {

#or load pixbuf from existing file
$pixbuf = $lp_ne->load($rfilename);
}

if ($sp->save_pixbuf_to_file($pixbuf, $filename, $choosen_format, $rquality)) {
#or load pixbuf from existing file
$pixbuf = $lp_ne->load($rfilename);
}

if ($key) {
if ($sp->save_pixbuf_to_file($pixbuf, $filename, $choosen_format, $rquality)) {

#do not try to update when exporting to pdf
unless (defined $rfiletype
&& ($rfiletype eq 'pdf' || $rfiletype eq 'ps'))
{
if ($key) {

#cancel handle
if (exists $session_screens{$key}->{'handle'}) {
#do not try to update when exporting to pdf
unless (defined $rfiletype
&& ($rfiletype eq 'pdf' || $rfiletype eq 'ps'))
{

$session_screens{$key}->{'handle'}->cancel;
}
#cancel handle
if (exists $session_screens{$key}->{'handle'}) {

if (fct_update_tab($key, undef, Glib::IO::File::new_for_path($filename), FALSE, 'clear')) {
$session_screens{$key}->{'handle'}->cancel;
}

#setup a new filemonitor, so we get noticed if the file changed
fct_add_file_monitor($key);
if (fct_update_tab($key, undef, Glib::IO::File::new_for_path($filename), FALSE, 'clear')) {

#maybe file is in session as well, need to set the handler again ;-)
foreach my $searchkey (keys %session_screens) {
next if $key eq $searchkey;
if ($session_screens{$searchkey}->{'long'} eq $filename) {
$session_screens{$searchkey}->{'changed'} = TRUE;
fct_update_tab($searchkey, undef, undef, FALSE, 'clear');
#setup a new filemonitor, so we get noticed if the file changed
fct_add_file_monitor($key);

#maybe file is in session as well, need to set the handler again ;-)
foreach my $searchkey (keys %session_screens) {
next if $key eq $searchkey;
if ($session_screens{$searchkey}->{'long'} eq $filename) {
$session_screens{$searchkey}->{'changed'} = TRUE;
fct_update_tab($searchkey, undef, undef, FALSE, 'clear');
}
}
}

fct_show_status_message(1, "$session_screens{ $key }->{ 'long' } " . $d->get("saved"));
fct_show_status_message(1, "$session_screens{ $key }->{ 'long' } " . $d->get("saved"));

}
}

} else {
if ($shf->file_exists($filename)) {
fct_show_status_message(1, "$filename " . $d->get("saved"));
} else {
if ($shf->file_exists($filename)) {
fct_show_status_message(1, "$filename " . $d->get("saved"));
}
}
}

} #end if $key
} #end if $key

#successfully saved
$fs->destroy();
return $filename;
#successfully saved
$fs->destroy();
return $filename;

} else {
} else {

#error while saving
$fs->destroy();
return FALSE;
#error while saving
$fs->destroy();
return FALSE;

}
}

} else {

#user cancelled overwrite
Expand Down
5 changes: 5 additions & 0 deletions share/shutter/resources/modules/Shutter/App/Menu.pm
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ sub fct_ret_file_menu {
#~ $self->{_menuitem_export_svg}->set_sensitive(FALSE);
#~ $self->{_menuitem_export_svg}->add_accelerator( 'activate', $accel_group, $self->{_shf}->accel('<Shift><Alt>G'), qw/visible/ );
#~ $self->{_menu_file}->append( $self->{_menuitem_export_svg} );

$self->{_menuitem_export_b64} = Gtk3::ImageMenuItem->new_with_mnemonic($d->get('Export to Base64...'));
$self->{_menuitem_export_b64}->set_sensitive(FALSE);
$self->{_menuitem_export_b64}->add_accelerator('activate', $accel_group, $self->{_shf}->accel('<Shift><Alt>B'), qw/visible/);
$self->{_menu_file}->append($self->{_menuitem_export_b64});

$self->{_menuitem_export_pdf} = Gtk3::ImageMenuItem->new_with_mnemonic($d->get('E_xport to PDF...'));
$self->{_menuitem_export_pdf}->set_sensitive(FALSE);
Expand Down