File: HELPERS.pm

package info (click to toggle)
tinyca 0.7.5-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 792 kB
  • ctags: 185
  • sloc: perl: 8,671; makefile: 57; sh: 11
file content (479 lines) | stat: -rw-r--r-- 11,820 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# Copyright (c) Stephan Martin <sm@sm-zone.net>
#
# $Id: HELPERS.pm,v 1.6 2006/06/28 21:50:42 sm Exp $
# 
# This program 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 2 of the License, or
# (at your option) any later version.
# 
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.

use strict;
package GUI::HELPERS;

use POSIX;

#
#  Error message box, kills application
#
sub print_error {
   my ($t, $ext) = @_;
   
   my ($box, $button, $dbutton, $expander, $text, $scrolled, $buffer);

   $button = Gtk2::Button->new_from_stock('gtk-ok');
   $button->signal_connect('clicked', sub { HELPERS::exit_clean(1) });
   $button->can_default(1);

   $box = Gtk2::MessageDialog->new(
         undef, [qw/destroy-with-parent modal/], 'error', 'none', $t);
   $box->set_default_size(600, 0);
   $box->set_resizable(1);

   if(defined($ext)) {
      $buffer = Gtk2::TextBuffer->new();
      $buffer->set_text($ext);

      $text = Gtk2::TextView->new_with_buffer($buffer);
      $text->set_editable(0);
      $text->set_wrap_mode('word');

      $scrolled = Gtk2::ScrolledWindow->new(undef, undef);
      $scrolled->set_policy('never', 'automatic');
      $scrolled->set_shadow_type('etched-in');
      $scrolled->add($text);

      $expander = Gtk2::Expander->new(_("Command Details"));
      $expander->add($scrolled);
      $box->vbox->add($expander);
   }

   $box->add_action_widget($button, 0);

   $box->show_all();
}

#
#  Warning message box
#
sub print_warning {
   my ($t, $ext) = @_;

   my ($box, $button, $dbutton, $expander, $text, $scrolled, $buffer);

   $button = Gtk2::Button->new_from_stock('gtk-ok');
   $button->signal_connect('clicked', sub { $box->destroy() });
   $button->can_default(1);

   $box = Gtk2::MessageDialog->new(
         undef, [qw/destroy-with-parent modal/], 'warning', 'none', $t);
   $box->set_default_size(600, 0);
   $box->set_resizable(1);

   if(defined($ext)) {
      $buffer = Gtk2::TextBuffer->new();
      $buffer->set_text($ext);

      $text = Gtk2::TextView->new_with_buffer($buffer);
      $text->set_editable(0);
      $text->set_wrap_mode('word');

      $scrolled = Gtk2::ScrolledWindow->new(undef, undef);
      $scrolled->set_policy('never', 'automatic');
      $scrolled->set_shadow_type('etched-in');
      $scrolled->add($text);

      $expander = Gtk2::Expander->new(_("Command Details"));
      $expander->add($scrolled);
      $box->vbox->add($expander);
   }
   $box->add_action_widget($button, 0);

   $box->show_all();

   return;
}

#
#  Info message box
#
sub print_info {
   my ($t, $ext) = @_;

   my ($box, $button, $dbutton, $buffer, $text, $scrolled, $expander);

   $button = Gtk2::Button->new_from_stock('gtk-ok');
   $button->signal_connect('clicked', sub { $box->destroy() });
   $button->can_default(1);

   $box = Gtk2::MessageDialog->new(
         undef, [qw/destroy-with-parent modal/], 'info', 'none', $t);
   $box->set_default_size(600, 0);
   $box->set_resizable(1);

   if(defined($ext)) {
      $buffer = Gtk2::TextBuffer->new();
      $buffer->set_text($ext);

      $text = Gtk2::TextView->new_with_buffer($buffer);
      $text->set_editable(0);
      $text->set_wrap_mode('word');

      $scrolled = Gtk2::ScrolledWindow->new(undef, undef);
      $scrolled->set_policy('never', 'automatic');
      $scrolled->set_shadow_type('etched-in');
      $scrolled->add($text);

      $expander = Gtk2::Expander->new(_("Command Details"));
      $expander->add($scrolled);
      $box->vbox->add($expander);
   }
   $box->add_action_widget($button, 0);

   $box->show_all();

   return;
}

#
# create standard dialog box
#
sub dialog_box {
   my ($title, $text, $button1, $button2) = @_;

   my $box = Gtk2::Dialog->new($title, undef, ["destroy-with-parent"]);

   $box->add_action_widget($button1, 0);

   if(defined($button2)) {
      $box->add_action_widget($button2, 0);
      $box->action_area->set_layout('spread');
   }

   if(defined($text)) {
      my $label = create_label($text, 'center', 0, 1);
      $box->vbox->pack_start($label, 0, 0, 0);
   }

   $box->signal_connect(response => sub { $box->destroy });

   return($box);
}

#
# create standard label 
#
sub create_label {
   my ($text, $mode, $wrap, $bold) = @_;

   $text = "<b>$text</b>" if($bold);

   my $label = Gtk2::Label->new($text);

   $label->set_justify($mode); 
   if($mode eq 'center') { 
      $label->set_alignment(0.5, 0.5); 
   }elsif($mode eq 'left') { 
      $label->set_alignment(0, 0); 
   }elsif($mode eq 'right') { 
      $label->set_alignment(1, 1); 
   } 
   
   $label->set_line_wrap($wrap); 
   
   $label->set_markup($text) if($bold);
   
   return($label);
}

#
# write two labels to table
#
sub label_to_table {
   my ($key, $val, $table, $row, $mode, $wrap, $bold) = @_;

   my ($label, $entry);

   $label = create_label($key, $mode, $wrap, $bold);
   $label->set_padding(20, 0);
   $table->attach_defaults($label, 0, 1, $row, $row+1);

   $label = create_label($val, $mode, $wrap, $bold);
   $label->set_padding(20, 0);
   $table->attach_defaults($label, 1, 2, $row, $row+1);

   $row++;
   $table->resize($row, 2);

   return($row);
}

#
# write label and entry to table
#
sub entry_to_table {
   my ($text, $var, $table, $row, $visibility, $box) = @_;

   my ($label, $entry);

   $label = create_label($text, 'left', 0, 0);
   $table->attach_defaults($label, 0, 1, $row, $row+1);

   $entry = Gtk2::Entry->new();
   $entry->set_text($$var) if(defined($$var));

   $table->attach_defaults($entry, 1, 2, $row, $row+1);
   $entry->signal_connect('changed' =>
         sub {GUI::CALLBACK::entry_to_var($entry, $entry, $var, $box)} );
   $entry->set_visibility($visibility);

   return($entry);
}

#
# sort the table by the clicked column
#
sub sort_clist {
   my ($clist, $col) = @_;

   $clist->set_sort_column($col);
   $clist->sort();

   return(1);
}

sub create_activity_bar {
   my ($t) = @_;

   my($box, $bar);

   $box = Gtk2::MessageDialog->new(
      undef, [qw/destroy-with-parent modal/], 'info', 'none', $t);

   $bar = Gtk2::ProgressBar->new();
   $bar->pulse();
   $bar->set_pulse_step(0.1);

   $box->vbox->add($bar);

   $box->show_all();

   return($box, $bar);
}

#
# set curser busy
#
sub set_cursor {
   my $main = shift;
   my $busy = shift;

   if($busy) {
      $main->{'rootwin'}->set_cursor($main->{'busycursor'});
   } else {
      $main->{'rootwin'}->set_cursor($main->{'cursor'});
   }
   while(Gtk2->events_pending) {
      Gtk2->main_iteration;
   }
}

#
# call file chooser
#
sub browse_file {
   my($title, $entry, $mode) = @_;

   my($file_chooser, $filename, $filter);

   $file_chooser = Gtk2::FileChooserDialog->new ($title, undef, $mode, 
         'gtk-cancel' => 'cancel', 
         'gtk-ok' => 'ok'); 

   $file_chooser->add_shortcut_folder ('/tmp');

   if($mode eq 'open') {
      $filter = Gtk2::FileFilter->new();
      $filter->set_name(_("Request Files (*.pem, *.der, *.req)"));
      $filter->add_pattern("*.pem");
      $filter->add_pattern("*.der");
      $filter->add_pattern("*.req");
      $file_chooser->add_filter($filter);

      $filter = Gtk2::FileFilter->new();
      $filter->set_name(_("All Files (*.*)"));
      $filter->add_pattern("*");
      $file_chooser->add_filter($filter);
   }

   if ('ok' eq $file_chooser->run) {
      $filename = $file_chooser->get_filename();
      $entry->set_text($filename);
   }

   $file_chooser->destroy();
}

#
# set text in statusbar
#
sub set_status {
   my ($main, $t) = @_;

   $main->{'bar'}->pop($main->{'lastid'}) if(defined($main->{'lastid'}));
   $main->{'lastid'} = $main->{'bar'}->get_context_id('gargs');
   $main->{'bar'}->push($main->{'lastid'}, $t);
}

1

__END__

=head1 NAME

GUI::HELPERS - helper functions for TinyCA, doing small jobs related to the
GUI

=head1 SYNOPSIS

 use GUI::HELPERS; 

 GUI::HELPERS::print_info($text, $ext);
 GUI::HELPERS::print_warning($text, $ext);
 GUI::HELPERS::print_error($text, $ext);
 GUI::HELPERS::sort_clist($clist, $col);
 GUI::HELPERS::set_cursor($main, $busy);
 GUI::HELPERS::browse_file($main, $entry, $mode);
 GUI::HELPERS::set_status($main, $text);

 $box   = GUI::HELPERS::dialog_box(
       $title, $text, $button1, $button2);
 $label = GUI::HELPERS::create_label(
       $text, $mode, $wrap, $bold);
 $row   = GUI::HELPERS::label_to_table(
       $key, $val, $table, $row, $mode, $wrap, $bold);
 $entry = GUI::HELPERS::entry_to_table(
       $text, $var, $table, $row, $visibility, $box);

=head1 DESCRIPTION

GUI::HELPERS.pm is a library, containing some useful functions used by other
TinyCA2 modules. All functions are related to the GUI.

=head2 GUI::HELPERS::print_info($text, $ext);

=over 1

creates an Gtk2::MessageDialog of the type info. The string given in $text is
shown as message, the (multiline) string $ext is available through the
"Details" Button.

=back

=head2 GUI::HELPERS::print_warning($text, $ext);

=over 1

is identically with GUI::HELPERS::print_warning(), only the
Gtk2::MessageDialog is of type warning.

=back

=head2 GUI::HELPERS::print_error($text, $ext);

=over 1

is identically with GUI::HELPERS::print_info(), only the Gtk2::MessageDialogog
is of type error and the program will shut down after closing the message.

=back

=head2 GUI::HELPERS::sort_clist($clist, $col);

=over 1

sorts the clist with the values from the given column $col.
   
=back

=head2 GUI::HELPERS::dialog_box($title, $text, $button1, $button2);

=over 1

returns the reference to a new window of type Gtk2::Dialog. $title and
$button1 must be given.  $text and $button2 are optional arguments and can be
undef.

=back

=head2 GUI::HELPERS::create_label($text, $mode, $wrap, $bold);

=over 1

returns the reference to a new Gtk2::Label. $mode can be "center", "left" or
"right". $wrap and $bold are boolean values.

=back

=head2 GUI::HELPERS::label_to_table($key, $val, $table, $row, $mode, $wrap, $bold);

=over 1

adds a new row to $table. The new row is appended at $row and has two columns:
the first will contain a label with the content of string $k, the second the
content of string $v. $mode, $wrap, $bold are the arguments for
GUI::HELPERS::create_label(), mentioned above. 
The function returns the number of the next free row in the table.

=back

=head2 GUI::HELPERS::entry_to_table($text, $var, $table, $row, $visibility, $box);

=over 1

adds a new row to $table. The new row is appended at $row and has two columns:
the first will contain a label with the content of the string $text, the
second one will contain a textentry Gtk2::Entry, associated with the variable
$var. $visibility controls, if the entered text will be displayed or not
(passwords).
The function returns the reference to the new created entry.

=back

=head2 GUI::HELPERS::set_cursor($main, $busy);

=over 1

sets the actual cursor to busy or back to normal. The value of $busy is
boolean.
This functions returns nothing;

=back

=head2 GUI::HELPERS::browse_file($main, $entry, $mode);

=over 1

opens a FileChooser dialog to select files or directories. $entry is a
reference to the variable, where the selected path shall be stored. If $mode
is set to "open", then only files with appropriate suffixes are displyed.

=back

=head2 GUI::HELPERS::set_status($main, $text);

=over 1

sets the text in $text to the statusbar at the bottom of the window.

=back

=cut