fix alt folder concat, undefined properties in checkboxes warning
[clawsker.git] / clawsker
1 #!/usr/bin/perl -w
2 #
3 # $Id$
4 #
5
6 =pod
7
8 =head1 NAME
9
10 Clawsker - A Claws Mail Tweaker
11
12 =head1 SYNOPSIS
13
14 clawsker [options]
15
16 =head1 DESCRIPTION
17
18 Clawsker is an applet to edit the so called Claws Mail hidden preferences.
19
20 Claws Mail is a fast, lightweight and feature-rich MUA with a high number 
21 of configurable options. To keep the binary small and fast some of these 
22 preferences which are not widely used are not provided with a graphical
23 interface for inspection and/or modification.
24
25 Users wanting to edit such preferences had to face raw editing of their 
26 configuration files, now you can do it with a convenient GTK2 interface
27 using Clawsker.
28
29 =head1 OPTIONS
30
31 --help
32         Shows a brief help screen.
33
34 --verbose
35         Tell more on the standard output.
36
37 --alternate-config-dir <dir>
38         Uses <dir> as Claws Mail configuration dir.
39
40 --clawsrc <file>
41         Uses <file> as Claws Mail resource configuration file. This sets
42         the full file name overriding any previous setting.
43
44 Multiple options are allowed, although only the last one has effect. Weird
45 option specifications may produce weird results (but otherwise correct).
46         
47 =head1 LIMITATIONS
48
49 A running Claws Mail cannot be detected if using the --clawsrc option because
50 the directory is not assumed to be a Claws Mail configuration dir. If that is 
51 the case use the --alternate-config-dir option instead.
52
53 =head1 AUTHOR
54
55 Ricardo Mones E<lt>ricardo@mones.orgE<gt>
56
57 =head1 LICENSE
58
59 Copyright (c) 2007-2008 by Ricardo Mones Lastra
60
61 This program is free software: you can redistribute it and/or modify
62 it under the terms of the GNU General Public License as published by
63 the Free Software Foundation, either version 3 of the License, or
64 (at your option) any later version.
65
66 This program is distributed in the hope that it will be useful,
67 but WITHOUT ANY WARRANTY; without even the implied warranty of
68 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
69 GNU General Public License for more details.
70
71 You should have received a copy of the GNU General Public License
72 along with this program.  If not, see E<lt>http://www.gnu.org/licenses/E<gt>.
73
74 =cut
75
76 use strict;
77 use encoding 'utf8';
78 use Glib qw(TRUE FALSE);
79 use Gtk2 -init;
80 use POSIX qw(setlocale);
81 use Locale::gettext;
82
83 my $NAME = 'clawsker';
84 my $PREFIX = '@PREFIX@';
85 my $LIBDIR = '@LIBDIR@';
86 my $VERSION = '@VERSION@';
87 my $VERBOSE = FALSE;
88 my $main_window = undef;
89
90 my $locale = (defined($ENV{LC_MESSAGES}) ? $ENV{LC_MESSAGES} : $ENV{LANG});
91 $locale = "C" unless defined($locale);
92 setlocale (LC_ALL, $locale);
93 bindtextdomain ($NAME, sprintf ('%s/share/locale', $PREFIX));
94 textdomain ($NAME);
95
96 my $SHOWHINTS = FALSE;
97 $SHOWHINTS = TRUE if ($Gtk2::VERSION >= 1.040 and Gtk2->CHECK_VERSION (2, 12, 0));
98
99 sub _ {
100     my $str = shift;
101     my %par = @_;
102     my $xla = gettext ($str);
103     if (scalar(keys(%par)) > 0) {
104         foreach my $key (keys %par) {
105             $xla =~ s/\{$key\}/$par{$key}/g;
106         }
107     }
108     return $xla;
109 }
110
111 # default messages
112 %xl::s = (
113     win_title => _('Claws Mail Hidden Preferences'),
114     about => _('About...'),
115     about_title => _('Clawsker :: A Claws Mail Tweaker'),
116     about_license => _('License:'),
117     about_version => _('Version:'),
118
119     tab_colours => _('Colours'),
120     tab_behaviour => _('Behaviour'),
121     tab_gui => _('GUI'),
122     tab_other => _('Other'),
123
124     ab_frame => _('Addressbook'),
125     mem_frame => _('Memory'),
126     msgview_frame => _('Message view'),
127     log_frame => _('Log window'),
128     dnd_frame => _('Drag \'n\' drop'),
129     ssl_frame => _('Secure Sockets Layer'),
130     msgs_frame => _('Messages'),
131     stripes_frame => _('Coloured stripes'),
132     sbar_frame => _('Scroll bars'),
133     mlist_frame => _('Message list'),
134     netm_frame => _('NetworkManager'),
135
136     l_oth_use_dlg => _('Use detached address book edit dialogue'),
137     h_oth_use_dlg => _('If true use a separate dialogue to edit a person\'s details. Otherwise will use a form embedded in the address book\'s main window.'),
138     l_oth_max_use => _('Maximum memory for message cache (Kb)'),
139     h_oth_max_use => _('The maximum amount of memory to use to cache messages, in kB.'),
140     l_oth_min_time => _('Minimun time for cache elements (minutes)'),
141     h_oth_min_time => _('The minimum time in minutes to keep a cache in memory. Caches more recent than this time will not be freed, even if the memory usage is too high.'),
142     l_oth_use_netm => _('Use NetworkManager'),
143     h_oth_use_netm => _('Use NetworkManager to switch offline automatically.'),
144     
145     l_gui_b_unread => _('Show unread messages with bold font'),
146     h_gui_b_unread => _('Show unread messages in the Message List using a bold font.'),
147     l_gui_no_markup => _('Don\'t use markup in compose window'),
148     h_gui_no_markup => _('Don\'t use bold and italic text in Compose dialogue\'s account selector.'),
149     l_gui_dot_lines => _('Use dotted lines in tree views'),
150     h_gui_dot_lines => _('Use the old dotted line look in the main window tree views (folder and message lists) instead of the modern lineless look.'),
151     l_gui_h_scroll => _('Enable horizontal scrollbar'),
152     h_gui_h_scroll => _('Enable the horizontal scrollbar in the Message List.'),
153     l_gui_swp_from => _('Dislay To header in From field in Sent folder'),
154     h_gui_swp_from => _('Display the sender\'s email address in the To column of the Sent folder instead of the recipient\'s.'),
155     l_gui_v_scroll => _('Folder list scrollbar behaviour'),
156     h_gui_v_scroll => _('Specify the policy of vertical scrollbar of Folder List: show always, automatic or hide always.'),
157     l_gui_v_scroll_show => _('Show always'),
158     l_gui_v_scroll_auto => _('Automatic'),
159     l_gui_v_scroll_hide => _('Hide always'),
160     l_gui_strip_off => _('Striped lines contrast'),
161     h_gui_strip_off => _('Specify the value to use when creating alternately coloured lines in tree view components. The smaller the value, the less visible the difference in the alternating colours of the lines.'),
162     l_gui_cursor_v => _('Show Cursor in message view'),
163     h_gui_cursor_v => _('Display the cursor in the message view.'),
164     l_gui_toolbar_d => _('Detachable toolbars'),
165     h_gui_toolbar_d => _('Show handles in the toolbars.'),
166     l_gui_strip_all => _('Use stripes in all tree view components'),
167     h_gui_strip_all => _('Enable alternately coloured lines in all tree view components.'),
168     l_gui_strip_sum => _('Use stripes in Folder List and Message List'),
169     h_gui_strip_sum => _('Enable alternately coloured lines in message list and folder list'),
170     l_gui_two_line_v => _('2 lines per Message List item in 3-column layout'),
171     h_gui_two_line_v => _('Spread Message List information over two lines when using the three column mode'),
172
173     l_beh_hover_t => _('Drag \'n\' drop hover timeout (ms)'),
174     h_beh_hover_t => _('Time in milliseconds that will cause a folder tree to expand when the mouse cursor is held over it during drag and drop.'),
175     l_beh_dangerous => _('Don\'t confirm deletions (dangerous!)'),
176     h_beh_dangerous => _('Don\'t ask for confirmation before definitive deletion of emails.'),
177     l_beh_flowed => _('Respect format=flowed in messages'),
178     h_beh_flowed => _('Respect format=flowed on text/plain message parts. This will cause some mails to have long lines, but will fix some URLs that would otherwise be wrapped.'),
179     l_beh_parts_rw => _('Allow writable temporary files'),
180     h_beh_parts_rw => _('Saves temporary files when opening attachment with write bit set.'),
181     l_beh_skip_ssl => _('Don\'t check SSL certificates'),
182     h_beh_skip_ssl => _('Disables the verification of SSL certificates.'),
183     l_beh_up_step => _('Progress bar update step (items)'),
184     h_beh_up_step => _('Update stepping in progress bars.'),
185     l_beh_thread_a => _('Maximum age when threading by subject (days)'),
186     h_beh_thread_a => _('Number of days to include a message in a thread when using "Thread using subject in addition to standard headers".'),
187     l_beh_unsafe_ssl => _('Allow unsafe SSL certificates'),
188     h_beh_unsafe_ssl => _('Allows Claws Mail to remember multiple SSL certificates for a given server/port.'),
189     l_beh_use_utf8 => _('Force UTF-8 for broken mails'),
190     h_beh_use_utf8 => _('Use UTF-8 encoding for broken mails instead of current locale.'),
191     l_beh_warn_dnd => _('Warn on drag \'n\' drop'),
192     h_beh_warn_dnd => _('Display a confirmation dialogue on drag \'n\' drop of folders.'),
193     l_beh_out_ascii => _('Ougoing messages fallback to ASCII'),
194     h_beh_out_ascii => _('If content allows, ASCII will be used to encode outgoing messages, otherwise the user-defined encoding is enforced always.'),
195
196     l_col_emphasis => _('X-Mailer header'),
197     h_col_emphasis => _('The colour used for the X-Mailer line when its value is Claws Mail.'),
198     l_col_log_err => _('Error messages'),
199     h_col_log_err => _('Colour for error messages in log window.'),
200     l_col_log_in => _('Server messages'),
201     h_col_log_in => _('Colour for input messages in log window.'),
202     l_col_log_msg => _('Standard messages'),
203     h_col_log_msg => _('Colour for messages in log window.'),
204     l_col_log_out => _('Client messages'),
205     h_col_log_out => _('Colour for output messages in log window.'),
206     l_col_log_warn => _('Warnings'),
207     h_col_log_warn => _('Colour for warning messages in log window.'),
208
209     e_error => _('Error: '),
210     e_noclawsrc => _('resource file for Claws Mail was not found.'),
211     e_running => _('seems Claws Mail is currently running, close it first.'),
212     e_requireddir => _('option requires a directory name.'),
213     e_requiredfile => _('option requires a file name.'),
214     e_notadir => _('specified name is not a directory or does not exist.'),
215     e_notafile => _('specified name is not a file or does not exist.'),
216 );
217
218 # all preferences read by load_preferences
219 my %PREFS = ();
220 # values of all preferences handled by clawsker
221 my %HPVALUE = ();
222 # default config dir and file name
223 my $ALTCONFIGDIR = FALSE;
224 my $CONFIGDIR = $ENV{HOME} . '/.claws-mail/';
225 my $CONFIGRC = 'clawsrc';
226
227 # index constants for preference arrays
228 use constant NAME  => 0; # the name on the rc file
229 use constant LABEL => 1; # the label on the GUI
230 use constant DESC  => 2; # the description for the hint/help
231 use constant TYPE  => 3; # data type: bool, int, float, string, color
232 use constant CMVER => 4; # lowest Claws Mail version the feature exists
233 use constant CMDEF => 5; # default value for the preference in Claws Mail
234 use constant GUI   => 6; # GUI element
235
236 # constants for GUI spacing
237 use constant HBOX_SPC => 5;
238 use constant FRAME_SPC => 2;
239 use constant PAGE_SPC => 5;
240
241 # data handlers and auxiliar functions
242
243 sub handle_bool_value {
244     my ($widget, $event, $dataref) = @_;
245     $$dataref = ($widget->get_active ())? '1': '0';
246 }
247
248 sub handle_int_value {
249     my ($widget, $event, $dataref) = @_;
250     $_ = $widget->get_text ();
251     s/^\s+//;
252     s/\s+$//;
253     if (/^[0-9]+$/) {
254         $$dataref = $_;
255         $widget->set_text ($_);
256     }
257     else {
258         $widget->set_text ($$dataref);
259     }
260 }
261
262 sub handle_string_value {
263     my ($widget, $event, $dataref) = @_;
264     $$dataref = $widget->get_text ();
265 }
266
267 sub gdk_color_from_str {
268     my ($str) = @_;
269     my ($rr, $gg, $bb) = (0, 0 ,0);
270     $_ = uc ($str);
271     if (/\#([A-F0-9][A-F0-9])([A-F0-9][A-F0-9])([A-F0-9][A-F0-9])/) {
272         $rr = hex($1) * 256;
273         $gg = hex($2) * 256; 
274         $bb = hex($3) * 256;
275     }
276     my $color = Gtk2::Gdk::Color->new ($rr, $gg, $bb);
277     return $color;
278 }
279
280 sub str_from_gdk_color {
281     my ($color) = @_;
282     my $rr = $color->red / 256;
283     my $gg = $color->green / 256;
284     my $bb = $color->blue / 256;
285     my $str = sprintf ("#%.2x%.2x%.2x", $rr, $gg, $bb);
286     return $str;
287 }
288
289 sub handle_color_value {
290     my ($widget, $event, $dataref) = @_;
291     my $newcol = $widget->get_color;
292     $$dataref = &str_from_gdk_color ($newcol);
293 }
294
295 sub handle_selection_value {
296     my ($widget, $event, $dataref) = @_;
297     $$dataref = $widget->get_active;
298 }
299
300 sub get_rc_filename {
301     return $CONFIGDIR . $CONFIGRC;
302 }
303
304 sub set_rc_filename {
305     my ($fullname) = @_;
306     my @parts = split ('/', $fullname);
307     $CONFIGRC = $parts[$#parts];
308     $parts[$#parts] = '';
309     $CONFIGDIR = join ('/', @parts);
310 }
311
312 sub log_message {
313     my ($mesg, $fatal) = @_;
314     if (defined($fatal) && $fatal eq 'die') {
315         die "$NAME: $mesg\n";
316     }
317     if ($VERBOSE) {
318         print "$NAME: $mesg\n";
319     }
320 }
321
322 sub error_dialog {
323     my ($emsg) = @_;
324     my $markup = "<span weight=\"bold\" size=\"large\">" . $emsg . "</span>";
325     my $errordlg = Gtk2::MessageDialog->new_with_markup ($main_window, 'modal', 'error', 'cancel', $markup);
326     $errordlg->set_title (_('Error message'));
327     $errordlg->run;
328     $errordlg->destroy;
329 }
330
331 sub check_claws_not_running() {
332     my $socket = (not $ALTCONFIGDIR)? "/tmp/": $CONFIGDIR;
333     $socket .= "claws-mail-$<";
334     -S $socket and do {
335         my $emsg = "$xl::s{e_error}$xl::s{e_running}";
336         log_message ($emsg);
337         error_dialog ($emsg);
338         return FALSE;
339      };
340
341      return TRUE;
342 }
343
344 sub check_rc_file() {
345     my ($rcfile) = @_;
346     (defined($rcfile) && -f $rcfile) or do {
347        my $emsg = "$xl::s{e_error}$xl::s{e_noclawsrc}\n";
348         log_message ($emsg);
349         error_dialog ($emsg);
350         return FALSE;
351     };
352
353     return TRUE;
354 }
355
356 sub set_widget_hint() {
357     if ($SHOWHINTS) {
358         my ($wdgt, $hint) = @_;    
359         $wdgt->set_tooltip_text ($hint);
360         $wdgt->set_has_tooltip (TRUE);
361     }
362 }
363
364 # graphic element creation 
365
366 sub new_check_button_for {
367     my ($hash, $key) = @_;
368     my $name = $$hash{$key}[NAME];
369     my $label = $$hash{$key}[LABEL];
370     #
371     my $hbox = Gtk2::HBox->new (FALSE, 5);
372     my $cb = Gtk2::CheckButton->new ($label);
373     $$hash{$key}[GUI] = $cb;
374     if (defined ($HPVALUE{$name})) {
375         $cb->set_active ($HPVALUE{$name} eq '1');
376     }
377     $cb->signal_connect (clicked => sub {
378             my ($w, $e) = @_;
379             &handle_bool_value($w, $e, \$HPVALUE{$name});
380         });
381     &set_widget_hint ($cb, $$hash{$key}[DESC]);
382     $hbox->pack_start ($cb, FALSE, FALSE, HBOX_SPC);
383     #
384     return $hbox;
385 }
386
387 sub new_text_box_for {
388     my ($hash, $key) = @_;
389     my $name = $$hash{$key}[NAME];
390     my $label = $$hash{$key}[LABEL];
391     #
392     my $hbox = Gtk2::HBox->new (FALSE, 5);
393     my $glabel = Gtk2::Label->new ($label);
394     my $gentry = Gtk2::Entry->new;
395     $gentry->set_width_chars (8);
396     $$hash{$key}[GUI] = $gentry;
397     $gentry->set_text($HPVALUE{$name});
398     $gentry->signal_connect(changed => sub {
399             my ($w, $e) = @_;
400             &handle_int_value($w, $e, \$HPVALUE{$name}); # FIXME int only
401         });
402     &set_widget_hint ($gentry, $$hash{$key}[DESC]);
403     $hbox->pack_start ($glabel, FALSE, FALSE, HBOX_SPC);
404     $hbox->pack_start ($gentry, FALSE, FALSE, HBOX_SPC);
405     #
406     return $hbox;
407 }
408
409 sub new_color_button_for {
410     my ($hash, $key) = @_;
411     my $name = $$hash{$key}[NAME];
412     my $label = $$hash{$key}[LABEL];
413     #
414     my $col = &gdk_color_from_str ($HPVALUE{$name});
415     my $hbox = Gtk2::HBox->new (FALSE, 5);
416     my $glabel = Gtk2::Label->new ($label);
417     my $button = Gtk2::ColorButton->new_with_color ($col);
418     $$hash{$key}[GUI] = $button;
419     $button->set_title ($label);
420     $button->set_relief ('none');
421     $button->signal_connect ('color-set' => sub {
422             my ($w, $e) = @_;
423             &handle_color_value($w, $e, \$HPVALUE{$name}); 
424         });
425     &set_widget_hint ($button, $$hash{$key}[DESC]);
426     $hbox->pack_start ($button, FALSE, FALSE, HBOX_SPC);
427     $hbox->pack_start ($glabel, FALSE, FALSE, HBOX_SPC);
428     #
429     return $hbox;
430 }
431
432 sub new_selection_box_for {
433     my ($hash, $key) = @_;
434     my $name = $$hash{$key}[NAME];
435     my $label = $$hash{$key}[LABEL];
436     #
437     my $hbox = Gtk2::HBox->new (FALSE, 5);
438     my $glabel = Gtk2::Label->new ($label);
439     my $combo = Gtk2::ComboBox->new_text;
440     $$hash{$key}[GUI] = $combo;
441     my @options = split (';', $$hash{$key}[TYPE]);
442     foreach my $opt (@options) {
443         my ($index, $textkey) = split ('=', $opt);
444         $combo->insert_text ($index, $xl::s{$textkey});
445     }
446     $combo->signal_connect ('changed' => sub {
447             my ($w, $e) = @_;
448             &handle_selection_value($w, $e, \$HPVALUE{$name});
449         });
450     $combo->set_active ($HPVALUE{$name});
451     &set_widget_hint ($combo, $$hash{$key}[DESC]);
452     $hbox->pack_start ($glabel, FALSE, FALSE, HBOX_SPC);
453     $hbox->pack_start ($combo, FALSE, FALSE, HBOX_SPC);
454     #
455     return $hbox;
456 }
457
458 # preference maps and corresponding page creation subs
459
460 %pr::oth = ( # other preferences
461     use_dlg => [ 
462         'addressbook_use_editaddress_dialog',
463         $xl::s{l_oth_use_dlg},
464         $xl::s{h_oth_use_dlg},
465         'bool',
466         '2.7.0',
467         '0',
468         undef,
469     ],
470     max_use => [
471         'cache_max_mem_usage',
472         $xl::s{l_oth_max_use},
473         $xl::s{h_oth_max_use},
474         'int',
475         '0.0.0',
476         '4096',
477         undef,
478     ],
479     min_time => [
480         'cache_min_keep_time',
481         $xl::s{l_oth_min_time},
482         $xl::s{h_oth_min_time},
483         'int',
484         '0.0.0',
485         '15',
486         undef,
487     ],
488     use_netm => [
489         'use_networkmanager',
490         $xl::s{l_oth_use_netm},
491         $xl::s{h_oth_use_netm},
492         'bool',
493         '3.3.1',
494         '1',
495         undef,
496     ],
497 );
498
499 sub new_other_page() {
500     my $of = Gtk2::VBox->new (FALSE, 5);
501     $of->set_border_width (PAGE_SPC);
502
503     my $ab_frame = Gtk2::Frame->new ($xl::s{ab_frame}); 
504     my $cb_use_dlg = &new_check_button_for(\%pr::oth, 'use_dlg');
505     my $vb1 = Gtk2::VBox->new (FALSE, 5);
506     $vb1->set_border_width (PAGE_SPC); 
507     $vb1->pack_start ($cb_use_dlg, FALSE, FALSE, 0);
508     $ab_frame->add ($vb1);
509
510     my $mem_frame = Gtk2::Frame->new ($xl::s{mem_frame}); 
511     my $tb_max_use = &new_text_box_for(\%pr::oth, 'max_use');
512     my $tb_min_time = &new_text_box_for(\%pr::oth, 'min_time');
513     my $vb2 = Gtk2::VBox->new (FALSE, 5);
514     $vb2->set_border_width (PAGE_SPC); 
515     $vb2->pack_start ($tb_max_use, FALSE, FALSE, 0);
516     $vb2->pack_start ($tb_min_time, FALSE, FALSE, 0);
517     $mem_frame->add ($vb2);
518
519     my $netm_frame = Gtk2::Frame->new ($xl::s{netm_frame});
520     my $cb_use_netm = &new_check_button_for(\%pr::oth, 'use_netm');
521     my $vb3 = Gtk2::VBox->new (FALSE, 5);
522     $vb3->set_border_width (PAGE_SPC);
523     $vb3->pack_start ($cb_use_netm, FALSE, FALSE, 0);
524     $netm_frame->add ($vb3);
525
526     $of->pack_start ($ab_frame, FALSE, FALSE, FRAME_SPC);
527     $of->pack_start ($mem_frame, FALSE, FALSE, FRAME_SPC);
528     $of->pack_start ($netm_frame, FALSE, FALSE, FRAME_SPC);
529
530     return $of;
531 }
532
533 %pr::gui = ( # gui bells and whistles
534     b_unread => [ 
535         'bold_unread',
536         $xl::s{l_gui_b_unread},
537         $xl::s{h_gui_b_unread},
538         'bool',
539         '0.0.0',
540         '1',
541         undef,
542     ],
543     no_markup => [
544         'compose_no_markup',
545         $xl::s{l_gui_no_markup},
546         $xl::s{h_gui_no_markup},
547         'bool',
548         '0.0.0',
549         '0',
550         undef,
551     ],
552     dot_lines => [
553         'enable_dotted_lines',
554         $xl::s{l_gui_dot_lines},
555         $xl::s{h_gui_dot_lines},
556         'bool',
557         '0.0.0',
558         '0',
559         undef,
560     ],
561     h_scroll => [
562         'enable_hscrollbar',
563         $xl::s{l_gui_h_scroll},
564         $xl::s{h_gui_h_scroll},
565         'bool',
566         '0.0.0',
567         '1',
568         undef,
569     ],
570     swp_from => [
571         'enable_swap_from',
572         $xl::s{l_gui_swp_from},
573         $xl::s{h_gui_swp_from},
574         'bool',
575         '0.0.0',
576         '0',
577         undef,
578     ],
579     v_scroll => [
580         'folderview_vscrollbar_policy',
581         $xl::s{l_gui_v_scroll},
582         $xl::s{h_gui_v_scroll},
583         '0=l_gui_v_scroll_show;1=l_gui_v_scroll_auto;2=l_gui_v_scroll_hide',
584         '0.0.0',
585         '0',
586         undef,
587     ],
588     strip_off => [
589         'stripes_color_offset',
590         $xl::s{l_gui_strip_off},
591         $xl::s{h_gui_strip_off},
592         'int',
593         '0.0.0',
594         '4000',
595         undef,
596     ],
597     cursor_v => [
598         'textview_cursor_visible',
599         $xl::s{l_gui_cursor_v},
600         $xl::s{h_gui_cursor_v},
601         'bool',
602         '0.0.0',
603         '0',
604         undef,
605     ],
606     toolbar_d => [
607         'toolbar_detachable',
608         $xl::s{l_gui_toolbar_d},
609         $xl::s{h_gui_toolbar_d},
610         'bool',
611         '0.0.0',
612         '0',
613         undef,
614     ],
615     strip_all => [
616         'use_stripes_everywhere',
617         $xl::s{l_gui_strip_all},
618         $xl::s{h_gui_strip_all},
619         'bool',
620         '0.0.0',
621         '1',
622         undef,
623     ],
624     strip_sum => [
625         'use_stripes_in_summaries',
626         $xl::s{l_gui_strip_sum},
627         $xl::s{h_gui_strip_sum},
628         'bool',
629         '0.0.0',
630         '1',
631         undef,
632     ],
633     two_linev => [
634         'two_line_vertical',
635         $xl::s{l_gui_two_line_v},
636         $xl::s{h_gui_two_line_v},
637         'bool',
638         '3.4.0.7',
639         '0',
640         undef,
641     ],
642 );
643
644 sub new_gui_page() {
645     my $gf = Gtk2::VBox->new (FALSE, 5);
646     $gf->set_border_width (PAGE_SPC);
647
648     my $stripes_frame = Gtk2::Frame->new ($xl::s{stripes_frame});
649     my $cb_strip_all = &new_check_button_for (\%pr::gui, 'strip_all');
650     my $cb_strip_sum = &new_check_button_for (\%pr::gui, 'strip_sum');
651     my $tb_strip_off = &new_text_box_for (\%pr::gui, 'strip_off');
652     my $vb1 = Gtk2::VBox->new (FALSE, 5);
653     $vb1->set_border_width (PAGE_SPC);
654     $vb1->pack_start ($cb_strip_all, FALSE, FALSE, 0);
655     $vb1->pack_start ($cb_strip_sum, FALSE, FALSE, 0);
656     $vb1->pack_start ($tb_strip_off, FALSE, FALSE, 0);
657     $stripes_frame->add ($vb1);
658
659     my $mlist_frame = Gtk2::Frame->new ($xl::s{mlist_frame});
660     my $cb_b_unread = &new_check_button_for (\%pr::gui, 'b_unread');
661     my $cb_swp_from = &new_check_button_for (\%pr::gui, 'swp_from');
662     my $vb3 = Gtk2::VBox->new (FALSE, 5);
663     $vb3->set_border_width (PAGE_SPC);
664     $vb3->pack_start ($cb_b_unread, FALSE, FALSE, 0);
665     $vb3->pack_start ($cb_swp_from, FALSE, FALSE, 0);
666     $mlist_frame->add ($vb3);
667
668     my $sbar_frame = Gtk2::Frame->new ($xl::s{sbar_frame});
669     my $cb_h_scroll = &new_check_button_for (\%pr::gui, 'h_scroll');
670     my $sb_v_scroll = &new_selection_box_for (\%pr::gui, 'v_scroll');
671     my $vb2 = Gtk2::VBox->new (FALSE, 5);
672     $vb2->set_border_width (PAGE_SPC);
673     $vb2->pack_start ($cb_h_scroll, FALSE, FALSE, 0);
674     $vb2->pack_start ($sb_v_scroll, FALSE, FALSE, 0);
675     $sbar_frame->add ($vb2);
676
677     my $cb_no_markup = &new_check_button_for (\%pr::gui, 'no_markup'); 
678     my $cb_dot_lines = &new_check_button_for (\%pr::gui, 'dot_lines'); 
679     my $cb_cursor_v = &new_check_button_for (\%pr::gui, 'cursor_v');
680     my $cb_toolbar_d = &new_check_button_for (\%pr::gui, 'toolbar_d');
681     my $cb_two_linev = &new_check_button_for (\%pr::gui, 'two_linev');
682
683     $gf->pack_start ($stripes_frame, FALSE, FALSE, FRAME_SPC);
684     $gf->pack_start ($mlist_frame, FALSE, FALSE, FRAME_SPC);
685     $gf->pack_start ($cb_no_markup, FALSE, FALSE, 0);
686     $gf->pack_start ($cb_dot_lines, FALSE, FALSE, 0);
687     $gf->pack_start ($cb_cursor_v, FALSE, FALSE, 0);
688     $gf->pack_start ($cb_toolbar_d, FALSE, FALSE, 0);
689     $gf->pack_start ($cb_two_linev, FALSE, FALSE, 0);
690     $gf->pack_start ($sbar_frame, FALSE, FALSE, FRAME_SPC);
691
692     return $gf;
693 }
694
695 %pr::beh = ( # tweak some behaviour
696     hover_t => [
697         'hover_timeout',
698         $xl::s{l_beh_hover_t},
699         $xl::s{h_beh_hover_t},
700         'int',
701         '0.0.0',
702         '500',
703         undef,
704     ],
705     dangerous => [
706         'live_dangerously',
707         $xl::s{l_beh_dangerous},
708         $xl::s{h_beh_dangerous},
709         'bool',
710         '0.0.0',
711         '0',
712         undef,
713     ],
714     flowed => [
715         'respect_flowed_format',
716         $xl::s{l_beh_flowed},
717         $xl::s{h_beh_flowed},
718         'bool',
719         '0.0.0',
720         '0',
721         undef,
722     ],
723     parts_rw => [
724         'save_parts_readwrite',
725         $xl::s{l_beh_parts_rw},
726         $xl::s{h_beh_parts_rw},
727         'bool',
728         '0.0.0',
729         '0',
730         undef,
731     ],
732     skip_ssl => [
733         'skip_ssl_cert_check',
734         $xl::s{l_beh_skip_ssl},
735         $xl::s{h_beh_skip_ssl},
736         'bool',
737         '0.0.0',
738         '0',
739         undef,
740     ],
741     up_step => [
742         'statusbar_update_step',
743         $xl::s{l_beh_up_step},
744         $xl::s{h_beh_up_step},
745         'int',
746         '0.0.0',
747         '10',
748         undef,
749     ],
750     thread_a => [
751         'thread_by_subject_max_age',
752         $xl::s{l_beh_thread_a},
753         $xl::s{h_beh_thread_a},
754         'int',
755         '0.0.0',
756         '10',
757         undef,
758     ],
759     unsafe_ssl => [
760         'unsafe_ssl_certs',
761         $xl::s{l_beh_unsafe_ssl},
762         $xl::s{h_beh_unsafe_ssl},
763         'bool',
764         '0.0.0',
765         '0',
766         undef,
767     ],
768     use_utf8 => [
769         'utf8_instead_of_locale_for_broken_mail',
770         $xl::s{l_beh_use_utf8},
771         $xl::s{h_beh_use_utf8},
772         'bool',
773         '0.0.0',
774         '0',
775         undef,
776     ],
777     warn_dnd => [
778         'warn_dnd',
779         $xl::s{l_beh_warn_dnd},
780         $xl::s{h_beh_warn_dnd},
781         'bool',
782         '0.0.0',
783         '1',
784         undef,
785     ],
786     out_ascii => [
787         'outgoing_fallback_to_ascii',
788         $xl::s{l_beh_out_ascii},
789         $xl::s{h_beh_out_ascii},
790         'bool',
791         '3.4.0.37',
792         '1',
793         undef,
794     ],
795 );
796
797 sub new_behaviour_page() {
798     my $bf = Gtk2::VBox->new (FALSE, 5);
799     $bf->set_border_width (PAGE_SPC);
800
801     my $dnd_frame = Gtk2::Frame->new ($xl::s{dnd_frame});
802     my $tb_hoover_t = &new_text_box_for (\%pr::beh, 'hover_t');
803     my $cb_warn_dnd = &new_check_button_for (\%pr::beh, 'warn_dnd');
804     my $vb1 = Gtk2::VBox->new (FALSE, 5);
805     $vb1->set_border_width (PAGE_SPC);
806     $vb1->pack_start ($cb_warn_dnd, FALSE, FALSE, 0);
807     $vb1->pack_start ($tb_hoover_t, FALSE, FALSE, 0);
808     $dnd_frame->add ($vb1);
809
810     my $ssl_frame = Gtk2::Frame->new ($xl::s{ssl_frame});
811     my $cb_skip_ssl = &new_check_button_for (\%pr::beh, 'skip_ssl');
812     my $cb_unsafe_ssl = &new_check_button_for (\%pr::beh, 'unsafe_ssl');
813     my $hb1 = Gtk2::HBox->new (FALSE, 5);
814     $hb1->set_border_width (PAGE_SPC);
815     $hb1->pack_start ($cb_skip_ssl, FALSE, FALSE, 0);
816     $hb1->pack_start ($cb_unsafe_ssl, FALSE, FALSE, 0);
817     $ssl_frame->add ($hb1);
818
819     my $tb_up_step = &new_text_box_for (\%pr::beh, 'up_step');
820     my $tb_thread_a = &new_text_box_for (\%pr::beh, 'thread_a');
821
822     my $msgs_frame = Gtk2::Frame->new ($xl::s{msgs_frame});
823     my $cb_flowed = &new_check_button_for (\%pr::beh, 'flowed');
824     my $cb_parts_rw = &new_check_button_for (\%pr::beh, 'parts_rw');
825     my $cb_use_utf8 = &new_check_button_for (\%pr::beh, 'use_utf8');
826     my $cb_dangerous = &new_check_button_for (\%pr::beh, 'dangerous');
827     my $cb_out_ascii = &new_check_button_for (\%pr::beh, 'out_ascii');
828     my $vb2 = Gtk2::VBox->new (FALSE, 5);    
829     $vb2->set_border_width (PAGE_SPC);
830     $vb2->pack_start ($cb_flowed, FALSE, FALSE, 0);
831     $vb2->pack_start ($cb_parts_rw, FALSE, FALSE, 0);
832     $vb2->pack_start ($cb_use_utf8, FALSE, FALSE, 0);
833     $vb2->pack_start ($cb_dangerous, FALSE, FALSE, 0);
834     $vb2->pack_start ($cb_out_ascii, FALSE, FALSE, 0);
835     $msgs_frame->add ($vb2);
836
837     $bf->pack_start ($dnd_frame, FALSE, FALSE, FRAME_SPC);
838     $bf->pack_start ($ssl_frame, FALSE, FALSE, FRAME_SPC);
839     $bf->pack_start ($tb_up_step, FALSE, FALSE, 0);
840     $bf->pack_start ($tb_thread_a, FALSE, FALSE, 0);
841     $bf->pack_start ($msgs_frame, FALSE, FALSE, FRAME_SPC);
842
843     return $bf;
844 }
845
846 %pr::col = ( # a variety of colours
847     emphasis => [
848         'emphasis_color',
849         $xl::s{l_col_emphasis},
850         $xl::s{h_col_emphasis},
851         'color',
852         '0.0.0',
853         '#0000cf',
854         undef,
855     ],
856     log_err => [
857         'log_error_color',
858         $xl::s{l_col_log_err},
859         $xl::s{h_col_log_err},
860         'color',
861         '0.0.0',
862         '#af0000',
863         undef,
864     ],
865     log_in => [
866         'log_in_color',
867         $xl::s{l_col_log_in},
868         $xl::s{h_col_log_in},
869         'color',
870         '0.0.0',
871         '#000000',
872         undef,
873     ],
874     log_msg => [
875         'log_msg_color',
876         $xl::s{l_col_log_msg},
877         $xl::s{h_col_log_msg},
878         'color',
879         '0.0.0',
880         '#00af00',
881         undef,
882     ],
883     log_out => [
884         'log_out_color',
885         $xl::s{l_col_log_out},
886         $xl::s{h_col_log_out},
887         'color',
888         '0.0.0',
889         '#0000ef',
890         undef,
891     ],
892     log_warn => [
893         'log_warn_color',
894         $xl::s{l_col_log_warn},
895         $xl::s{h_col_log_warn},
896         'color',
897         '0.0.0',
898         '#af0000',
899         undef,
900     ],
901 );
902
903 sub new_colours_page() {
904     my $cf = Gtk2::VBox->new (FALSE, 5);
905     $cf->set_border_width (PAGE_SPC);
906
907     my $msgview_frame = Gtk2::Frame->new ($xl::s{msgview_frame}); 
908     my $cb_emphasis = &new_color_button_for (\%pr::col, 'emphasis');
909     my $vb1 = Gtk2::VBox->new (FALSE, 5);
910     $vb1->set_border_width (PAGE_SPC);
911     $vb1->pack_start ($cb_emphasis, FALSE, FALSE, 0);
912     $msgview_frame->add ($vb1);
913     
914     my $log_frame = Gtk2::Frame->new ($xl::s{log_frame}); 
915     my $cb_log_err = &new_color_button_for (\%pr::col, 'log_err');
916     my $cb_log_in = &new_color_button_for (\%pr::col, 'log_in');
917     my $cb_log_msg = &new_color_button_for (\%pr::col, 'log_msg');
918     my $cb_log_out = &new_color_button_for (\%pr::col, 'log_out');
919     my $cb_log_warn = &new_color_button_for (\%pr::col, 'log_warn');
920     my $vb2 = Gtk2::VBox->new (FALSE, 5);
921     $vb2->set_border_width (PAGE_SPC);
922     $vb2->pack_start ($cb_log_err, FALSE, FALSE, 0);
923     $vb2->pack_start ($cb_log_in, FALSE, FALSE, 0);
924     $vb2->pack_start ($cb_log_msg, FALSE, FALSE, 0);
925     $vb2->pack_start ($cb_log_out, FALSE, FALSE, 0);
926     $vb2->pack_start ($cb_log_warn, FALSE, FALSE, 0);
927     $log_frame->add ($vb2);
928
929     $cf->pack_start ($msgview_frame, FALSE, FALSE, 0);
930     $cf->pack_start ($log_frame, FALSE, FALSE, 0);
931
932     return $cf;
933 }
934
935 # the command line help
936 sub print_help() {
937     my $line = '-' x length ($xl::s{about_title}) . "\n";
938     print $line;
939     print $xl::s{about_title} . "\n";
940     print $xl::s{about_version} . " $VERSION\n";
941     print "Perl-GLib " . $Glib::VERSION;
942     # version info stuff appeared in 1.040
943     if ($Glib::VERSION >= 1.040) {
944         print _(", built for ") . join(".", Glib->GET_VERSION_INFO) 
945               . _(", running with ") . join(".", &Glib::major_version, 
946                   &Glib::minor_version, &Glib::micro_version);
947     }
948     print "\n";
949     print "Perl-GTK2 " . $Gtk2::VERSION;
950     if ($Gtk2::VERSION >= 1.040) {
951         print _(", built for ") . join(".", Gtk2->GET_VERSION_INFO)
952               . _(", running with ") . join(".", &Gtk2::major_version, 
953                   &Gtk2::minor_version, &Gtk2::micro_version);
954     }
955     print "\n";
956     print $line;
957     print _("Syntax:\n");
958     print _("    clawsker [options]\n");
959     print _("Options:\n");
960     print _("    --help                         Prints this help screen.\n");
961     print _("    --verbose                      More messages on standard output.\n");
962     print _("    --alternate-config-dir <dir>   Uses <dir> as Claws Mail config dir.\n");
963     print _("    --clawsrc <file>               Uses <file> as full resource name.\n");
964 }
965
966 # handle errors which don't allow to run
967 sub command_line_fatal() {
968     my $reason = shift;
969     my $emsg = $xl::s{e_error} . $reason;
970     error_dialog ($emsg);
971     log_message ("$emsg", 'die');
972 }
973
974 # parse the command line
975 sub parse_command_line() {
976     my $arg = 0;
977     while (defined($ARGV[$arg])) {
978         for ($ARGV[$arg]) {
979             /--help/ && do { 
980                 &print_help; 
981                 return FALSE;
982             };
983             /--verbose/ && do {
984                 $VERBOSE = TRUE;
985                 last;
986             };
987             /--alternate-config-dir/ && do {
988                 ++$arg;
989                 &command_line_fatal ($xl::s{e_requireddir})
990                     unless defined($ARGV[$arg]);
991                 &command_line_fatal ($xl::s{e_notadir})
992                     unless -d $ARGV[$arg];
993                 $CONFIGDIR = $ARGV[$arg];
994                 $CONFIGDIR .= "/" 
995                     unless ($CONFIGDIR =~ /.*\/$/);
996                 $ALTCONFIGDIR = TRUE;
997                 last;
998             };
999             /--clawsrc/ && do {
1000                 ++$arg;
1001                 &command_line_fatal($xl::s{e_requiredfile}) 
1002                     unless defined($ARGV[$arg]);
1003                 &command_line_fatal($xl::s{e_notafile}) 
1004                     unless -f $ARGV[$arg];
1005                 &set_rc_filename ($ARGV[$arg]);
1006                 last;
1007             };
1008             /.*/ && &command_line_fatal (
1009                         _("unknown option '{opt}'.\n", opt => $ARGV[$arg]));
1010         }
1011         ++$arg;
1012     }
1013     # eveything continues...
1014     return TRUE;
1015 }
1016
1017 # update the hidden preferences status from loaded values
1018 sub init_hidden_preferences() {
1019     foreach my $hash (\%pr::beh, \%pr::col, \%pr::gui, \%pr::oth) {
1020         foreach my $key (keys %$hash) { 
1021             $HPVALUE{${$hash}{$key}[NAME]} = $PREFS{${$hash}{$key}[NAME]};
1022         }
1023     }
1024     return TRUE;
1025 }
1026
1027 # load current status from disc
1028 sub load_preferences() {
1029     my $rc = &get_rc_filename;
1030     &log_message ("Loading preferences from $rc\n");
1031     return FALSE unless &check_rc_file ($rc);
1032     return FALSE unless &check_claws_not_running;
1033     open (RCF, "<$rc");
1034     while (<RCF>) {
1035         chomp;
1036         if (/^([8a-z_]+)=(.*)$/) {
1037             $PREFS{$1} = "$2";
1038         }
1039     }
1040     close (RCF);
1041     return TRUE;
1042 }
1043
1044 # save current preferences to disc
1045 sub save_preferences() {
1046     my $rc = &get_rc_filename;
1047     &log_message ("Saving preferences to $rc\n");
1048     return FALSE unless &check_rc_file ($rc);
1049     return FALSE unless &check_claws_not_running;
1050     my $rcbak = "$rc.backup";
1051     rename ($rc, $rcbak);
1052     open (RCF, ">$rc");
1053     open (RCB, "<$rcbak");
1054     while (<RCB>) {
1055         chomp;
1056         if (/^([8a-z_]+)=(.*)$/) {
1057             if (defined($HPVALUE{$1})) {
1058                 print RCF $1 . "=" . $HPVALUE{$1} . "\n";
1059             }
1060             else {
1061                 print RCF $_ . "\n";
1062             }
1063         }
1064         else {
1065             print RCF $_ . "\n";
1066         }
1067     }
1068     close (RCB);
1069     close (RCF);
1070     return TRUE;
1071 }
1072
1073 # create notebook
1074 sub new_notebook() {
1075     my $nb = Gtk2::Notebook->new;
1076     # 
1077     $nb->append_page (&new_behaviour_page, $xl::s{tab_behaviour});
1078     $nb->append_page (&new_colours_page, $xl::s{tab_colours});
1079     $nb->append_page (&new_gui_page, $xl::s{tab_gui});
1080     $nb->append_page (&new_other_page, $xl::s{tab_other});
1081
1082     return $nb;
1083 }
1084
1085 # create an about dialog
1086 sub new_about_dialog() {
1087     my ($parent) = @_;
1088     my $title = $xl::s{about_title};
1089     my $lic = $xl::s{about_license};
1090     my $vers = $xl::s{about_version} . " $VERSION";
1091     my $license = 
1092 "This program is free software: you can redistribute it and/or modify
1093 it under the terms of the GNU General Public License as published by
1094 the Free Software Foundation, either version 3 of the License, or
1095 (at your option) any later version.
1096
1097 This program is distributed in the hope that it will be useful,
1098 but WITHOUT ANY WARRANTY; without even the implied warranty of
1099 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1100 GNU General Public License for more details.
1101
1102 You should have received a copy of the GNU General Public License
1103 along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.";
1104     my $year = "2007";
1105     my $holder = "Ricardo Mones &lt;ricardo\@mones.org&gt;";
1106
1107     my $dialog = Gtk2::MessageDialog->new_with_markup ($parent, 
1108                     [qw/modal destroy-with-parent/], 
1109                     'info', 'close', 
1110                     "<span size=\"x-large\" weight=\"bold\">$title</span>\n"
1111                     . "<span size=\"large\">$vers</span>\n"
1112                     . "<span>Copyright $year by $holder</span>\n\n"
1113                     . "<span size=\"large\">$lic</span>\n\n"
1114                     . "<span size=\"small\">$license</span>");
1115     $dialog->set_title ($xl::s{about});
1116     #
1117     return $dialog;
1118 }
1119
1120 # create buttons box
1121 sub new_button_box() {
1122     my ($parent, $adlg) = @_;
1123     my $b_about = Gtk2::Button->new_from_stock ('gtk-about');
1124     my $b_exit = Gtk2::Button->new_from_stock ('gtk-quit');
1125     my $b_apply = Gtk2::Button->new_from_stock ('gtk-apply');
1126     # disable button until is really implemented
1127     # my $b_undo = Gtk2::Button->new_from_stock ('gtk-undo');
1128     my $hbox = Gtk2::HBox->new (FALSE, 5);
1129     # signal handlers 
1130     $b_exit->signal_connect (clicked => sub { Gtk2->main_quit });
1131     $b_apply->signal_connect (clicked => sub { &save_preferences ($parent) });
1132     # $b_undo->signal_connect (clicked => sub { &undo_current_changes });
1133     $b_about->signal_connect (clicked => sub { $adlg->run; $adlg->hide });
1134     # package them
1135     $hbox->pack_end ($b_apply, FALSE, FALSE, 0);
1136     $hbox->pack_end ($b_exit, FALSE, FALSE, 0);
1137     # $hbox->pack_end ($b_undo, FALSE, FALSE, 0);
1138     $hbox->pack_start ($b_about, FALSE, FALSE, 0);
1139     #
1140     return $hbox;
1141 }
1142
1143 # initialise
1144 $main_window = Gtk2::Window->new ('toplevel');
1145 exit unless &parse_command_line;
1146 exit unless &load_preferences;
1147 exit unless &init_hidden_preferences;
1148 # create main GUI
1149 my $box = Gtk2::VBox->new (FALSE, 5);
1150 $box->set_border_width(3);
1151 my $about = &new_about_dialog;
1152 $box->pack_start (&new_notebook, FALSE, FALSE, 0);
1153 $box->pack_end (&new_button_box ($main_window, $about), FALSE, FALSE, 0);
1154 $main_window->signal_connect (delete_event => sub { Gtk2->main_quit });
1155 $main_window->set_title ($xl::s{win_title});
1156 $main_window->add ($box);
1157 $main_window->show_all;
1158 Gtk2->main;
1159