ErrorTextDialog/SaveDialog.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« ErrorTextDialog.pm ↑ Summary TextView/FollowAppend.pm »
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 <http://www.gnu.org/licenses/>.
17  
18  
19 package Gtk2::Ex::ErrorTextDialog::SaveDialog;
20 1 ▶use 5.008001; # for utf8::is_utf8()
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 0 ▶use Gtk2;
Subroutine BEGIN: called CC=1 SCAR=2.6
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__
« ErrorTextDialog.pm ↑ Summary TextView/FollowAppend.pm »