File Coverage

blib/lib/Gtk2/Ex/ErrorTextDialog/SaveDialog.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             # Copyright 2009, 2010, 2011, 2013 Kevin Ryde
2              
3             # This file is part of Gtk2-Ex-ErrorTextDialog.
4             #
5             # Gtk2-Ex-ErrorTextDialog is free software; you can redistribute it and/or
6             # modify it under the terms of the GNU General Public License as published
7             # by the Free Software Foundation; either version 3, or (at your option) any
8             # later version.
9             #
10             # Gtk2-Ex-ErrorTextDialog is distributed in the hope that it will be useful,
11             # but WITHOUT ANY WARRANTY; without even the implied warranty of
12             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13             # Public License for more details.
14             #
15             # You should have received a copy of the GNU General Public License along
16             # with Gtk2-Ex-ErrorTextDialog. If not, see .
17              
18              
19             package Gtk2::Ex::ErrorTextDialog::SaveDialog;
20 1     1   813 use 5.008001; # for utf8::is_utf8()
  1         3  
  1         39  
21 1     1   4 use strict;
  1         2  
  1         26  
22 1     1   4 use warnings;
  1         2  
  1         22  
23 1     1   1928 use Gtk2;
  0            
  0            
24             use Locale::TextDomain ('Gtk2-Ex-ErrorTextDialog');
25             use Gtk2::Ex::ErrorTextDialog; # for TextDomain utf8 setups
26              
27             our $VERSION = 11;
28              
29             use Glib::Object::Subclass
30             'Gtk2::FileChooserDialog',
31             signals => { delete_event => \&Gtk2::Widget::hide_on_delete };
32              
33             # GtkFileChooserDialog dispatches properties to the GtkFileChooserWidget,
34             # but it's not ready to do so until after its "constructor()" code.
35             # INIT_INSTANCE runs before that (under g_type_create_instance()).
36             #
37             # This subclassed new() is a workaround, but one which of course is not run
38             # by non-perl GObject constructors like the GtkBuilder mechanism.
39             #
40             sub new {
41             my $class = shift;
42             my $self = $class->SUPER::new (@_);
43             $self->set_action ('save');
44              
45             # new in gtk 2.8
46             if ($self->can('set_do_overwrite_confirmation')) {
47             $self->set_do_overwrite_confirmation (1);
48             }
49              
50             # default filename based on the program name in $0, without directory or
51             # any .pl suffix (g_get_prgname() is not wrapped, perl $0 being adequate)
52             {
53             my $filename = 'errors.utf8.txt';
54             require File::Basename;
55             my $prgname = File::Basename::basename ($0, '.pl');
56             if ($prgname ne '') {
57             $filename = Glib::filename_display_name($prgname) . "-$filename";
58             }
59             $self->set_current_name ($filename);
60             }
61              
62             return $self;
63             }
64              
65             sub INIT_INSTANCE {
66             my ($self) = @_;
67              
68             $self->set_destroy_with_parent (1);
69              
70             { my $title = __('Save Errors');
71             if (defined (my $appname = Glib::get_application_name())) {
72             $title = "$appname: $title";
73             }
74             $self->set_title ($title);
75             }
76              
77             $self->add_buttons ('gtk-save' => 'accept',
78             'gtk-cancel' => 'cancel');
79              
80             # connect to self instead of a class handler since as of Gtk2-Perl 1.200 a
81             # Gtk2::Dialog class handler for 'response' is called with response IDs as
82             # numbers, not enum strings like 'accept'
83             $self->signal_connect (response => \&_do_response);
84              
85             my $label = Gtk2::Label->new
86             (__('Save error messages to a file (with UTF-8 encoding)'));
87             $label->show;
88             my $vbox = $self->vbox;
89             $vbox->pack_start ($label, 0,0,0);
90             $vbox->reorder_child ($label, 0); # at the top of the dialog
91             }
92              
93             sub _do_response {
94             my ($self, $response) = @_;
95             ### ErrorText-SaveDialog response: $response
96              
97             if ($response eq 'accept') {
98             $self->save;
99              
100             } elsif ($response eq 'cancel') {
101             # raise 'close' as per a keyboard Esc to close, which defaults to
102             # raising 'delete-event', which in turn defaults to a destroy
103             $self->signal_emit ('close');
104             }
105             }
106              
107             sub save {
108             my ($self) = @_;
109             my $error_dialog = $self->get_transient_for;
110             my $filename = $self->get_filename;
111              
112             # Gtk2-Perl 1.200 $chooser->get_filename gives back wide chars (where it
113             # almost certainly should be bytes)
114             if (utf8::is_utf8($filename)) {
115             $filename = Glib->filename_from_unicode ($filename);
116             }
117             $self->hide;
118             _save_to_filename ($error_dialog, $filename);
119             }
120              
121             # The die() message here might be an unholy amalgam of filename charset
122             # $filename, and locale charset $!. It probably occurs in many other
123             # libraries too, and you're probably asking for trouble if your filename and
124             # locale charsets are different, so leave it as just this simple combination
125             # for now.
126             #
127             sub _save_to_filename {
128             my ($error_dialog, $filename) = @_;
129             my $text = $error_dialog->get_text;
130             ### ErrorText-SaveDialog _save_to_filename()
131             ### $filename
132             ### $text
133             ### text utf8: utf8::is_utf8($text)
134             ### text utf8 valid: utf8::valid($text)
135              
136             my $out;
137             (open $out, '>:utf8', $filename
138             and print $out $text
139             and close $out)
140             or die "Cannot write file $filename: $!";
141             }
142              
143             1;
144             __END__