summaryrefslogtreecommitdiffstats
path: root/bin/openpgp-applet
blob: 02cceea95e4deec696a23c72ba5a9e887912ce36 (plain)
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
#!/usr/bin/perl

=head1 NAME

OpenPGP_Applet - GNOME applet for OpenPGP text encryption

=head1 VERSION

Version 1.0

=head1 DESCRIPTION

OpenPGP Applet allows encryption and decryption of the clipboard's content with
a symmetric cipher using a passphrase. This is a graphical frontend on top of
GnuPG.

Asymmetric decryption and clipboard verification are also supported.

=head1 PREREQUISITES

OpenPGP Applet does not handle passphrase input. Since it also does not
offer terminal interaction unless explicitly run from there, it relies
in practice on some kind of GnuPG agent such as pinentry, Seahorse 2.x
or GNOME keyring 3.x to manage passphrase input.

=head1 SEE ALSO

User documentation, with screenshots, can be found on https://tails.boum.org/doc/encryption_and_privacy/gpgapplet/

=head1 AUTHOR

Tails developers <tails@boum.org>

=head1 LICENCE

This program is free software; you can redistribute it and/or modify it under the terms of either:

a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or

b) the "Artistic License" which comes with Perl.

Pixmaps and icons are licensed under the terms of Creative Commons ShareAlike 2.0 (CC-BY-SA-2.0)

See README and LICENSE for details.

=cut

use strict;
use warnings FATAL => 'all';
use 5.10.0;

our $VERSION = 1.0;

my $DEBUG = $ENV{'DEBUG'};

use Glib qw{TRUE FALSE};
use Gtk3 qw{-init};
use Gtk3::SimpleList;

use Encode qw{decode encode find_encoding};
use English;
use Crypt::OpenPGP_Applet::GnuPG::Interface;
use IO::Handle;
use I18N::Langinfo qw{langinfo CODESET};
use List::MoreUtils qw{none};
use DateTime;
use File::ShareDir;
use IO::Select;
use Errno qw(EPIPE);

use Locale::TextDomain ("OpenPGP_Applet");
use POSIX;
setlocale(LC_MESSAGES, "");


=head1 GLOBALS

=cut

use constant C_SELECT      => 0;
use constant C_NAME        => 1;
use constant C_KEYID       => 2;
use constant C_STATUS      => 3;
use constant C_FINGERPRINT => 4;
use constant C_USERIDS     => 5;
use constant C_TRUSTED     => 6;
use constant VISIBLE_COLS  => (C_NAME, C_KEYID, C_STATUS);
use constant HIDDEN_COLS   => (C_FINGERPRINT, C_USERIDS, C_TRUSTED);

use constant COMBO_NAME        => 0;
use constant COMBO_KEYID       => 1;
use constant COMBO_FINGERPRINT => 2;
use constant COMBO_ROLE        => 3;

my $gnupg         = Crypt::OpenPGP_Applet::GnuPG::Interface->new();
$gnupg->call('gpg2');
my $codeset       = langinfo(CODESET());
my $encoding      = find_encoding($codeset);
my $main_window   = Gtk3::Window->new();
my $icon_factory  = Gtk3::IconFactory->new();
# Set always_trust since GnuPG otherwise will fail if the key's
# trust hasn't been set.
my %gnupg_options = (armor => 1, always_trust => 0, meta_interactive => 0);

my $pgp_encrypted_msg = {
    type   => 'message',
    header => '-----BEGIN PGP MESSAGE-----',
    footer => '-----END PGP MESSAGE-----'
};
my $pgp_signed_msg = {
    type   => 'signed',
    header => '-----BEGIN PGP SIGNED MESSAGE-----',
    middle => '-----BEGIN PGP SIGNATURE-----',
    footer => '-----END PGP SIGNATURE-----'
};
my @pgp_headers = ($pgp_encrypted_msg, $pgp_signed_msg);

=head1 MAIN

=cut

my $statusicon = build_statusicon();
$statusicon->set_visible(TRUE);
init_freshest_clipboard();
init_icons_stock($icon_factory);
detect_received(freshest_clipboard());
Gtk3->main;


=head1 FUNCTIONS

=cut

sub all_clipboards {
    map {
        Gtk3::Clipboard::get($_)
    } (
        Gtk3::Gdk::Atom::intern('CLIPBOARD', Glib::FALSE),
        Gtk3::Gdk::Atom::intern('PRIMARY', Glib::FALSE)
    );
}

{
    my $freshest_clipboard;

    sub init_freshest_clipboard {
        $freshest_clipboard = Gtk3::Clipboard::get(Gtk3::Gdk::Atom::intern('CLIPBOARD', Glib::FALSE));
    }

    sub freshest_clipboard {
        return $freshest_clipboard;
    }

    sub set_freshest_clipboard {
        $freshest_clipboard = shift;
    }
}

sub app_exit {
    my $parent = shift;
    my $dialog = Gtk3::MessageDialog->new($parent, [qw/modal destroy-with-parent/],
                                   'warning',
                                   'yes-no',
                                   $encoding->decode(__("You are about to exit OpenPGP Applet. Are you sure?")));

    $dialog->set_default_response('no');
    Gtk3->main_quit if ($dialog->run eq 'yes');

    $dialog->destroy;
}

sub build_statusicon {
    my $icon = Gtk3::StatusIcon->new;
    $icon->set_visible(FALSE);
    $icon->set_from_icon_name('seahorse');
    $icon->set_tooltip_text($encoding->decode(__("OpenPGP encryption applet")));

    my $menu   = Gtk3::Menu->new;
    my $mexit  = Gtk3::MenuItem->new_with_mnemonic($encoding->decode(__("Exit")));
    $mexit->signal_connect('activate' => sub { app_exit($main_window); });
    my $mabout = Gtk3::MenuItem->new_with_mnemonic($encoding->decode(__("About")));
    $mabout->signal_connect('activate' => sub { Gtk3->show_about_dialog(
        $main_window,
        'program-name' => 'OpenPGP Applet',
        'license'      => q{This program is free software; you can redistribute it and/or modify it under the terms of either:

a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or

b) the "Artistic License" which comes with Perl.

The pixmaps and icons are licensed using the Creative Common 1.0 Universal (CC0). Humans may want to refer to http://creativecommons.org/publicdomain/zero/1.0/

Please see README and LICENSE files distributed along this program for detail.
},
        'wrap-license' => 1,
        'website'      => 'https://tails.boum.org/',
    )});
    $menu->append($mabout);
    $menu->append(Gtk3::SeparatorMenuItem->new);
    $menu->append($mexit);

    $icon->signal_connect('popup-menu', sub {
        my $ticon = shift;
        my $event = shift;
        my $time = shift;
        $menu->show_all;
        $menu->popup(undef, undef, undef, undef, $event, $time);
    });

    $icon->signal_connect('button-press-event' => sub {
        my $ticon = shift;
        my $event = shift;
        return unless $event->button == 1;
        our $action_menu = build_action_menu();
        $action_menu->show_all;
        $action_menu->popup(undef, undef, undef, undef, $event->button, $event->time);
    });

    foreach (all_clipboards()) {
        $_->signal_connect("owner-change" => sub {
            my $clipboard = shift;
            my $event     = shift;
            handle_clipboard_owner_change($clipboard);
        });
    }

    return $icon;
}

sub build_action_menu {
    my $action_menu = Gtk3::Menu->new;

    my $text_type = detect_text_type(get_validated_clipboard_text());

    if ($text_type eq 'text' or $text_type eq 'none') {
        my $msymencrypt    = Gtk3::MenuItem->new_with_mnemonic($encoding->decode(__("Encrypt Clipboard with _Passphrase")));
        $msymencrypt->signal_connect('activate' => sub { operate_on_clipboard(\&symmetric_encrypt, ['text']); });
        $action_menu->append($msymencrypt);
        my $msignencrypt    = Gtk3::MenuItem->new_with_mnemonic($encoding->decode(__("Sign/Encrypt Clipboard with Public _Keys")));
        $msignencrypt->signal_connect('activate' => sub { operate_on_clipboard(\&public_crypto, ['text']); });
        $action_menu->append($msignencrypt);
    }
    if ($text_type eq 'message' or $text_type eq 'signed') {
        my $mdecryptver = Gtk3::MenuItem->new_with_mnemonic($encoding->decode(__("_Decrypt/Verify Clipboard")));
        $mdecryptver->signal_connect('activate' => sub { operate_on_clipboard(\&decrypt_verify, ['message', 'signed']); });
        $action_menu->append($mdecryptver);
    }
    my $mmanage = Gtk3::MenuItem->new_with_mnemonic($encoding->decode(__("_Manage Keys")));
    $mmanage->signal_connect('activate' => sub { manage_keys(); });
    $action_menu->append($mmanage);

    my $mtexteditor = Gtk3::MenuItem->new_with_mnemonic($encoding->decode(__("_Open Text Editor")));
    $mtexteditor->signal_connect('activate' => sub { open_text_editor(); });
    $action_menu->append($mtexteditor);

    return $action_menu;
}

sub manage_keys {
    system("seahorse &");
}

sub open_text_editor {
    system("gnome-text-editor &");
}

sub all_text_types {
    map { $_->{type} } @pgp_headers;
}

sub detect_text_type {
    my $text = shift;

    unless (defined $text && length($text)) {
        return 'none';
    }

    foreach (@pgp_headers) {
        my $header = $_->{header};
        my $footer = $_->{footer};
        return $_->{type} if $text =~ m{$header.*$footer}ms;
    }

    return 'text';
}

sub text_is_of_type {
    my $text        = shift;
    my @valid_types = @_;

    my $text_type = detect_text_type($text);
    if (none { $_ eq $text_type } @valid_types) {
        return (
            0,
            $encoding->decode(
                __("The clipboard does not contain valid input data."))
        );
    }
    return (1);
}

sub get_validated_clipboard_text {
    my $args = shift;
    my @valid_types;

    if (exists $args->{valid_types} && defined $args->{valid_types}) {
        @valid_types = @{ $args->{valid_types} };
    }
    else {
        @valid_types = all_text_types();
    }

    my $clipboard = freshest_clipboard();
    # FIXME-GTK3 - still true with gtk3 ?
    # Note: according to the GTK documentation, the wait_for_text method
    # is supposed to always returns UTF-8. But it seems like the Perl
    # bindings decode it and we get a string of chars instead of bytes.
    my $content = $clipboard->wait_for_text;
    my ($is_valid, $reason) = text_is_of_type($content, @valid_types);
    return ($content) if $is_valid;
    return (0, $reason);
}

sub set_clipboards_text {
    my $text = shift;
    my $encoded_text = $encoding->encode($text);

    foreach (all_clipboards()) {
        $_->set_text($encoded_text,-1);
    }
}

sub get_status {
    my $code = shift;
    my $status;
    my $trusted;
    # Below taken from doc/DETAILS in GnuPG's sources
    SWITCH: 
    for ($code){
        if ($_ eq "o") { $trusted = FALSE;
                   $status = $encoding->decode(__("Unknown Trust")); last SWITCH; }
        if ($_ eq "-") { $trusted = FALSE;
                    $status = $encoding->decode(__("Unknown Trust")); last SWITCH; }
        if ($_ eq "q") { $trusted = FALSE;
                    $status = $encoding->decode(__("Unknown Trust")); last SWITCH; }
        if ($_ eq "m") { $trusted = FALSE;
                    $status = $encoding->decode(__("Marginal Trust")); last SWITCH; }
        if ($_ eq "f") { $trusted = TRUE;
                    $status = $encoding->decode(__("Full Trust")); last SWITCH; }
        if ($_ eq "u") { $trusted = TRUE;
                    $status = $encoding->decode(__("Ultimate Trust")); last SWITCH; }
    	return;
    }
    return ($status, $trusted);
}

sub get_private_key_status {
    my $key = shift;

    my $fingerprint = $encoding->decode($key->fingerprint->as_hex_string());
    my $pubkey = ($gnupg->get_public_keys_light($fingerprint))[0]; # ignore collisions

    # a valid key may lack signing capabilities
    return unless $pubkey->usage_flags =~ m/S/;

    my $validity = $pubkey->user_ids->[0]->validity;
    return get_status($validity);
}

sub get_public_key_status {
    my $key = shift;

    # a valid key may lack encryption capabilities
    return unless $key->usage_flags =~ m/E/;

    my $validity = $key->user_ids->[0]->validity;
    return get_status($validity);
}

sub create_key_row {
    my $key = shift;

    my ($status, $trusted) = (ref($key) eq "GnuPG::SecretKey") ?
        get_private_key_status($key)
      : get_public_key_status($key);
    # no status implies expired, revoked, etc. keys, which we don't want to list
    return if !defined $status;

    my $name    = $encoding->decode($key->user_ids->[0]->as_string);
    my $userids = join("\n", map { my $a = $_->as_string; my $b = $encoding->decode("$a"); my $c ="x" . "$b" }
                                 $key->user_ids);
    my $keyid   = $encoding->decode($key->short_hex_id);

    my $fingerprint = $encoding->decode($key->fingerprint->as_hex_string());
    # Gtk3::SimpleList encodes these strings itself.
    return [FALSE, $name, $keyid, $status, $fingerprint, $userids, $trusted];
}

sub make_pub_key_tree {
    my $pub_keys_ref = shift;

    my $treestore = Gtk3::TreeStore->new(,
        'Glib::Boolean',       # C_SELECT
        'Glib::String',        # C_NAME
        'Glib::String',        # C_KEYID
        'Glib::String',        # C_STATUS
        'Glib::String',        # C_FINGERPRINT
        'Glib::String',        # C_USERIDS
        'Glib::Boolean'        # C_TRUSTED
    );
    for my $key (@{$pub_key_ref}) {
        my $iter = $treestore->append();
        my $iter = $treestore->insert_with_values(
            $iter,
            C_NAME,         $key[C_NAME],
            C_KEYID,        $key[C_KEYID],
            C_STATUS,       $key[C_STATUS],
            C_FINGERPRINT,  $key[C_FINGERPRINT],
            C_USERIDS,      $key[C_USERIDS],
            C_TRUSTED,      $key[C_TRUSTED]
            );
    }


    my $tree = Gtk3::SimpleList->new (
        ""                                    => 'bool', # C_SELECT
        $encoding->decode(__("Name"))    => 'text', # C_NAME
        $encoding->decode(__("Key ID"))  => 'text', # C_KEYID
        $encoding->decode(__("Status"))  => 'text', # C_STATUS
        ""                                    => 'text', # C_FINGERPRINT
        ""                                    => 'text', # C_USERIDS
        ""                                    => 'bool'  # C_TRUSTED
        );
    foreach my $i (VISIBLE_COLS) {
        my $col = $list->get_column($i);
        $col->set_max_width(400);
        $col->set_resizable(TRUE);
        $col->set_sort_column_id($i);
    }
    foreach my $i (HIDDEN_COLS) {
        $list->get_column($i)->set_visible(FALSE);
    }
    $tree->set_search_column(C_NAME);
    $list->get_selection->set_mode('single');
    $list->get_selection->unselect_all;
    # Initially sort by name (couldn't find a cleaner way)
    $list->get_column(C_NAME)->signal_emit('clicked');

    # If we used Gtk3::TreeView instead of Gtk3::SimpleList we could
    # show all user ids directly in the list, but we make it simple
    # for us and instead show them in the tooltip.
    $list->set_has_tooltip(TRUE);
    $list->signal_connect('query-tooltip' => sub {
        my ($wx, $wy, $tooltip) = ($_[1], $_[2], $_[4]);
        my ($x, $y) = $list->convert_widget_to_bin_window_coords($wx, $wy);
        my $row = $list->get_path_at_pos($x, $y);
        return FALSE unless defined $row;
        my $fingerprint =
            join(" ", (${$list->{data}}[$row][C_FINGERPRINT] =~ m/..../g));
        my $fingerprint_label = $encoding->decode(__("Fingerprint:"));
        my $uids = "${$list->{data}}[$row][C_USERIDS]";
        my $uids_label = $encoding->decode(
            __n("User ID:", "User IDs:", ($uids =~ tr/\n//) + 1));
        my $text = sprintf("%s\n%s\n%s\n%s", $uids_label, $uids,
                           $fingerprint_label, $fingerprint);
        $tooltip->set_text("$text");
        return TRUE;
    });

    $list->signal_connect('row-activated' => sub {
        # Since we use 'single' selection mode, there can only be one
        my $index = ($list->get_selected_indices)[0];
        my $old_val = $list->{data}->[$index]->[C_SELECT];
        $list->{data}->[$index]->[C_SELECT] = !$old_val;
    });

    push @{$list->{data}},
        grep { $_ } map { create_key_row($_) } @{$pub_keys_ref};

    $list->select(0);

    return $list;
}

sub make_pub_key_list {
    my $pub_keys_ref = shift;

    my $list = Gtk3::SimpleList->new (
        ""                                    => 'bool', # C_SELECT
        $encoding->decode(__("Name"))    => 'text', # C_NAME
        $encoding->decode(__("Key ID"))  => 'text', # C_KEYID
        $encoding->decode(__("Status"))  => 'text', # C_STATUS
        ""                                    => 'text', # C_FINGERPRINT
        ""                                    => 'text', # C_USERIDS
        ""                                    => 'bool'  # C_TRUSTED
        );
    foreach my $i (VISIBLE_COLS) {
        my $col = $list->get_column($i);
        $col->set_max_width(400);
        $col->set_resizable(TRUE);
        $col->set_sort_column_id($i);
    }
    foreach my $i (HIDDEN_COLS) {
        $list->get_column($i)->set_visible(FALSE);
    }
    $list->set_search_column(C_NAME);
    $list->get_selection->set_mode('single');
    $list->get_selection->unselect_all;
    # Initially sort by name (couldn't find a cleaner way)
    $list->get_column(C_NAME)->signal_emit('clicked');

    # If we used Gtk3::TreeView instead of Gtk3::SimpleList we could
    # show all user ids directly in the list, but we make it simple
    # for us and instead show them in the tooltip.
    $list->set_has_tooltip(TRUE);
    $list->signal_connect('query-tooltip' => sub {
        my ($wx, $wy, $tooltip) = ($_[1], $_[2], $_[4]);
        my ($x, $y) = $list->convert_widget_to_bin_window_coords($wx, $wy);
        my $row = $list->get_path_at_pos($x, $y);
        return FALSE unless defined $row;
        my $fingerprint =
            join(" ", (${$list->{data}}[$row][C_FINGERPRINT] =~ m/..../g));
        my $fingerprint_label = $encoding->decode(__("Fingerprint:"));
        my $uids = "${$list->{data}}[$row][C_USERIDS]";
        my $uids_label = $encoding->decode(
            __n("User ID:", "User IDs:", ($uids =~ tr/\n//) + 1));
        my $text = sprintf("%s\n%s\n%s\n%s", $uids_label, $uids,
                           $fingerprint_label, $fingerprint);
        $tooltip->set_text("$text");
        return TRUE;
    });

    $list->signal_connect('row-activated' => sub {
        # Since we use 'single' selection mode, there can only be one
        my $index = ($list->get_selected_indices)[0];
        my $old_val = $list->{data}->[$index]->[C_SELECT];
        $list->{data}->[$index]->[C_SELECT] = !$old_val;
    });

    push @{$list->{data}},
        grep { $_ } map { create_key_row($_) } @{$pub_keys_ref};

    $list->select(0);

    return $list;
}

sub make_priv_key_combo {
    my $priv_keys_ref = shift;

    my $list_store = Gtk3::ListStore->new(
        qw/Glib::String Glib::String Glib::String Glib::String/);
    my $iter = $list_store->append;
    $list_store->set ($iter,
                      COMBO_NAME, $encoding->decode(__("None (Don't sign)")),
                      COMBO_KEYID, "",
                      COMBO_FINGERPRINT, "",
                      COMBO_ROLE, "none");
    $iter = $list_store->append;
    $list_store->set ($iter,
                      COMBO_NAME, "",
                      COMBO_KEYID, "",
                      COMBO_FINGERPRINT, "",
                      COMBO_ROLE, "separator");
    foreach my $key (@{$priv_keys_ref}) {
        my $row = create_key_row($key);
        next unless $row; # skip keys without signing capability
        $iter = $list_store->append;
        $list_store->set ($iter,
                          COMBO_NAME, "$row->[C_NAME]",
                          COMBO_KEYID, "($row->[C_KEYID])",
                          COMBO_FINGERPRINT, "$row->[C_FINGERPRINT]",
                          COMBO_ROLE, "");
    }

    my $sorted_list = Gtk3::TreeModelSort->new_with_model($list_store);
    $sorted_list->set_default_sort_func(sub {
        my ($model, $iter1, $iter2, $data) = @_;
        my $name1 = $model->get($iter1, COMBO_NAME);
        my $name2 = $model->get($iter2, COMBO_NAME);
        my $role1 = $model->get($iter1, COMBO_ROLE);
        my $role2 = $model->get($iter2, COMBO_ROLE);

        if ($role1 eq "none") {
            return -1;
        } elsif ($role2 eq "none") {
            return 1;
        } elsif ($role1 eq "separator") {
            return -1;
        } elsif ($role2 eq "separator") {
            return 1;
        } else {
            return (lc $name1 cmp lc $name2);
        }
                                });

    my $combo = Gtk3::ComboBox->new_with_model($sorted_list);
    my $renderer = Gtk3::CellRendererText->new();
    $combo->pack_start($renderer, FALSE);
    $combo->add_attribute($renderer, 'text', COMBO_NAME);
    $renderer = Gtk3::CellRendererText->new();
    $combo->pack_start($renderer, FALSE);
    $combo->add_attribute($renderer, 'text', COMBO_KEYID);
    $combo->set_row_separator_func( sub {
        my ($model, $iter, $data) = @_;
        return TRUE if ($model->get($iter, COMBO_ROLE) eq "separator");
                                        });
    $combo->set_active(0);

    return $combo;
}

sub choose_keys {
    my $priv_keys_ref = shift;
    my $pub_keys_ref = shift;

    my $pub_key_label = Gtk3::Label->new(
        $encoding->decode(__("Select recipients:")));

    my $pub_key_list = make_pub_key_list($pub_keys_ref);
    my $pub_key_list_scroll = Gtk3::ScrolledWindow->new;
    $pub_key_list_scroll->set_policy('automatic', 'always');
    $pub_key_list_scroll->add($pub_key_list);

    my $hide_recipients_checkbox = Gtk3::CheckButton->new(
        $encoding->decode(__("Hide recipients")));
    $hide_recipients_checkbox->set_has_tooltip(TRUE);
    $hide_recipients_checkbox->set_tooltip_text(
        $encoding->decode(__("Hide the user IDs of all recipients of " .
                                  "an encrypted message. Otherwise anyone " .
                                  "that sees the encrypted message can see " .
                                  "who the recipients are.")));

    my $priv_key_label = Gtk3::Label->new(
        $encoding->decode(__("Sign message as:")));

    my $priv_key_combo = make_priv_key_combo($priv_keys_ref);

    my $dialog = Gtk3::Dialog->new($encoding->decode(__("Choose keys")),
                                   $main_window, 'destroy-with-parent',
                                   'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok' );
    $dialog->set_default_size(650,500);
    $dialog->set_default_response('ok');
    my $vbox = $dialog->get_content_area;
    $vbox->pack_start($pub_key_label, FALSE, FALSE, 5);
    $vbox->pack_start($pub_key_list_scroll, TRUE, TRUE, 0);
    my $hbox = Gtk3::HBox->new;
    $hbox->pack_start($priv_key_label, FALSE, FALSE, 0);
    $hbox->pack_start($priv_key_combo, TRUE, TRUE, 0);
    $vbox->pack_start($hbox, FALSE, FALSE, 5);
    $vbox->pack_start($hide_recipients_checkbox, FALSE, FALSE, 0);

    $pub_key_list->grab_focus;
    $dialog->show_all;

    $dialog->signal_connect('key-press-event' => sub {
        my $event = $_[1];
        return unless $event->keyval == Gtk3::Gdk::KEY_Return;
        $dialog->response('ok');
        return 1;
    });

    while ($dialog->run eq 'ok') {
        my @recipients;
        my $signer;
        my $always_trust = 0;

        # Get signing key, if any
        my $priv_key_combo_model = $priv_key_combo->get_model;
        my $priv_key_iter = $priv_key_combo->get_active_iter;
        $signer = $priv_key_combo_model->get($priv_key_iter, COMBO_FINGERPRINT);

        # Get public keys, if any
        my @list_selection = grep { $_->[C_SELECT] } @{$pub_key_list->{data}};
        if (@list_selection) {
            my @unauth = grep { ! $_->[C_TRUSTED] } @list_selection;
            if (@unauth) {
                my $title = $encoding->decode(
                    __("Do you trust these keys?")
                                              );
                my $warning = $encoding->decode(__n(
                    "The following selected key is not fully trusted:",
                    "The following selected keys are not fully trusted:",
                    scalar @unauth
                                                ));
                my $msg = sprintf("%s\n", $warning);
                foreach my $key (@unauth) {
                    # Each key will be listed RTL *or* LTR depending on the
                    # direction of the first character of the name. This
                    # unfortunately causes mixing of LTR and RTL in the
                    # same list. A potential FIXME would be to display this
                    # with a custom windows using SimpleList for the keys.
                    # Also note that everything in $key (which originates
                    # from $pub_key_list) already has been decoded, so we
                    # don't have to do it again here.
                    my $key_name = "$key->[C_NAME] ($key->[C_KEYID])";
                    $msg = sprintf("%s%s\n", $msg, $key_name);
                }
                my $question = $encoding->decode(__n(
                    "Do you trust this key enough to use it anyway?",
                    "Do you trust these keys enough to use them anyway?",
                    scalar @unauth
                                                 ));
                $msg = sprintf("%s%s", $msg, $question);
                next unless display_question($dialog, $title, $msg);
                $always_trust = 1;
            }
            @recipients = map { $_->[C_FINGERPRINT] } @list_selection;
        }

        if (!@recipients && !$signer) {
            display_error($dialog,
                          $encoding->decode(__("No keys selected")),
                          $encoding->decode(__(
                              "You must select a private key to sign the " .
                              "message, or some public keys to encrypt the " .
                              "message, or both."
                                            )));
            next;
        }

        $dialog->destroy;
        return {
            always_trust => $always_trust,
            hide_recipients => $hide_recipients_checkbox->get_active,
            signer => $signer,
            recipients => \@recipients,
        };
    }
    $dialog->destroy;
    return ();
}

sub public_crypto {
    my $args    = shift;
    my $handles = $args->{handles};

    my @priv_keys = $gnupg->get_secret_keys_light();
    my @pub_keys = $gnupg->get_public_keys_light();

    if (@priv_keys == 0 && @pub_keys == 0) {
        display_error($main_window,
                      $encoding->decode(__("No keys available")),
                      $encoding->decode(__(
                          "You need a private key to sign messages or a " .
                          "public key to encrypt messages."
                                       )));
        return 0;
    }

    my $chosen = choose_keys(\@priv_keys, \@pub_keys);
    my $signer = $chosen->{signer};
    my $recipients_ref = $chosen->{recipients};
    my @recipients; @recipients = @{$recipients_ref} if defined $recipients_ref;
    my $always_trust = $chosen->{always_trust};
    my $hide_recipients = $chosen->{hide_recipients};

    $gnupg->options->always_trust($always_trust);
    $gnupg->options->clear_extra_args;
    $gnupg->options->clear_meta_signing_key_id;
    $gnupg->options->clear_recipients();

    if ($signer) {
        $gnupg->options->meta_signing_key_id($signer);
    }

    if (@recipients) {
        if ($hide_recipients) {
            # Since gpg's --no-throw-keyids seems to be broken (it doesn't
            # work via the CLI either) we can't just push it to extra_args :/.
            foreach my $recipient (@recipients) {
                $gnupg->options->push_extra_args('--hidden-recipient',
                                                 $recipient);
            }
        } else {
            $gnupg->options->push_recipients(@recipients);
        }
    }

    my $result = 0;

    if ($signer && !@recipients) {
        $result = $gnupg->clearsign(handles => $handles);
    } elsif (@recipients && !$signer) {
        $result = $gnupg->encrypt(handles => $handles);
    } elsif ($signer && @recipients) {
        $result = $gnupg->sign_and_encrypt(handles => $handles);
    }

    $gnupg->options->always_trust(0);
    $gnupg->options->clear_extra_args;
    $gnupg->options->clear_meta_signing_key_id;
    $gnupg->options->clear_recipients();

    return $result;
}

sub symmetric_encrypt {
    my $args    = shift;
    my $handles = $args->{handles};

    return $gnupg->encrypt_symmetrically(handles => $handles);
}

sub decrypt_verify {
    my $args    = shift;
    my $handles = $args->{handles};
    my $input   = $args->{input};

    my $text_type = detect_text_type($input);
    return
        $text_type eq 'message'
      ? $gnupg->decrypt(handles => $handles)
      : $gnupg->verify(handles => $handles);
}

sub gpg_operate_on_text {
    my $operation = shift;
    my $text      = shift;

    $gnupg->options->hash_init(%gnupg_options);
    my $in_h    = IO::Handle->new();
    my $err_h   = IO::Handle->new();
    my $out_h   = IO::Handle->new();


    my $handles = GnuPG::Handles->new(
        stdin => $in_h,
        stderr => $err_h,
        stdout => $out_h
    );


    my $args = {
        handles => $handles,
        input   => $text,
        in_h    => $in_h,
        err_h   => $err_h,
        out_h   => $out_h,
    };

    my $pid = $operation->($args) or return;

    my $read = _gpg_communicate([$out_h,$err_h],
                                [$in_h],
                                # We assume the sender/recipient uses the same charset as us :/
                                # PGP/MIME was invented for a reason.
                                { $in_h => $encoding->encode($text) });

    my @raw_stderr = split(/^/m, $read->{$err_h});
    my @raw_stdout = split(/^/m, $read->{$out_h});

    waitpid $pid, 0; # Clean up the finished GnuPG process.

    my $std_err = $encoding->decode(join('', @raw_stderr));
    my $std_out = $encoding->decode(join('', @raw_stdout));

    if ($CHILD_ERROR == 0) {
        if ($operation eq \&decrypt_verify) {
            my $msg;
            if ($text =~ m/$pgp_signed_msg->{header}/) {
                $msg = $text;
                $msg =~ s/^.*$pgp_signed_msg->{header}\nHash: [^\n]*\n\n//m;
                $msg =~ s/^$pgp_signed_msg->{middle}.*//ms;
            } else {
                $msg = $std_out;
            }
            display_output($msg, $std_err);
        } else {
            set_clipboards_text($std_out);
        }
    }
    else {
        display_error(
            $main_window,
            $encoding->decode(__("GnuPG error")),
            $std_out . "\n\n" . $std_err
        );
        return;
    }

    return;
}

sub operate_on_clipboard {
    my $operation   = shift;
    my $valid_types = shift;

    my ($text, $clip_error) = get_validated_clipboard_text(
        { valid_types => $valid_types }
    );

    if (defined $clip_error) {
        display_error(
            $main_window,
            $clip_error, # already translated and decoded
            $encoding->decode(__("Therefore the operation cannot be " .
                                      "performed."))
        );
        return;
    }

    gpg_operate_on_text($operation, $text);
}

sub display_error {
    my $parent = shift;
    my $title = shift;
    my $msg   = shift;

    my $dialog = Gtk3::MessageDialog->new(
        $parent, 'destroy-with-parent', 'error', 'ok',
        $title
    );
    $dialog->set('secondary_text' => $msg);
    $dialog->signal_connect(
        response => sub { my $self = shift; $self->destroy; }
    );
    $dialog->set_position('center');
    $dialog->run;
    $dialog->destroy;

    return 1;
}

sub display_question {
    my $parent = shift;
    my $title = shift;
    my $msg   = shift;

    my $dialog = Gtk3::MessageDialog->new(
        $parent, 'destroy-with-parent', 'question', 'yes-no', $title);
    $dialog->set('secondary_text' => $msg);
    $dialog->set_position('center');
    my $answer = $dialog->run;
    $dialog->destroy;
    return $answer eq 'yes' ? TRUE : FALSE;
}

# FIXME: let window grow depending on output text size
sub display_output {
    my $std_out = shift;
    my $std_err = shift;

    my $dialog = Gtk3::MessageDialog->new(
        $main_window, 'destroy-with-parent', 'info', 'ok',
        $encoding->decode(__("GnuPG results"))
    );
    my $my_width_request = 800;
    my $my_height_request = 600;
    # TRANSLATORS: GnuPG stdout (encrypted or decrypted message)
    $dialog->set('secondary_text' => sprintf($encoding->decode(
        __("Output of GnuPG:")
    )));

    my $msg_area = $dialog->get_content_area;

    my $outbuf = Gtk3::TextBuffer->new();
    $outbuf->set_text($std_out);
    my $text_desc = Pango::FontDescription->new;
    $text_desc->set_family('Monospace');
    my $textview_out = Gtk3::TextView->new_with_buffer($outbuf);
    $textview_out->set_editable(FALSE);
    $textview_out->set_cursor_visible(FALSE);
    $textview_out->set_left_margin(10);
    $textview_out->set_right_margin(10);
    $textview_out->set_wrap_mode('word');
    $textview_out->modify_font($text_desc);
    my $scrolled_win_out = Gtk3::ScrolledWindow->new;
    $scrolled_win_out->set_policy('automatic', 'automatic');
    $scrolled_win_out->add($textview_out);
    $msg_area->pack_start($scrolled_win_out, TRUE, TRUE, 0);

    if (defined $std_err && length($std_err)) {
        my $std_err_title = Gtk3::Label->new(
            # TRANSLATORS: GnuPG stderr (other informational messages)
            $encoding->decode(
               __("Other messages provided by GnuPG:")
            ));
        $std_err_title->set_alignment(0, 0);
        $std_err_title->set_padding(10, 0);
        $msg_area->pack_start($std_err_title, FALSE, FALSE, 0);
        my $std_err_buf = Gtk3::TextBuffer->new();
        $std_err_buf->set_text($std_err);
        my $textview_err = Gtk3::TextView->new_with_buffer($std_err_buf);
        $textview_err->set_editable(FALSE);
        $textview_err->set_cursor_visible(FALSE);
        $textview_err->set_left_margin(10);
        $textview_err->set_right_margin(10);
        $textview_err->set_wrap_mode('word');
        $textview_err->modify_font($text_desc);
        my $scrolled_win_err = Gtk3::ScrolledWindow->new;
        $scrolled_win_err->set_policy('automatic', 'automatic');
        $scrolled_win_err->add($textview_err);
        $scrolled_win_err->set_size_request(-1, $my_height_request/5);
        $msg_area->pack_start($scrolled_win_err, FALSE, FALSE, 0);
    }

    $dialog->signal_connect(
        response => sub { my $self = shift; $self->destroy; }
    );
    my $screen_width = $dialog->get_screen()->get_width();
    my $screen_height = $dialog->get_screen()->get_height();
    if ($screen_width > $my_width_request || $screen_height > $my_height_request) {
        $dialog->set_size_request($my_width_request, $my_height_request);
    } else {
        $dialog->maximize();
    }
    $dialog->set_resizable(TRUE);
    $dialog->set_position('center');
    $dialog->show_all;

    return 1;
}

# interleave reads and writes from gpg process
# stolen from Mail::GnuPG
# input parameters:
# $rhandles - array ref with a list of file handles for reading
# $whandles - array ref with a list of file handles for writing
# $wbuf_of - hash ref indexed by the stringified handles
# containing the data to write
# return value:
# $rbuf_of - hash ref indexed by the stringified handles
# containing the data that has been read
#
# read and write errors due to EPIPE (gpg exit) are skipped silently on the
# assumption that gpg will explain the problem on the error handle
#
# other errors cause a non-fatal warning, processing continues on the rest
# of the file handles
#
# NOTE: all the handles get closed inside this function

sub _gpg_communicate {
    my $blocksize = 2048;
    my ($rhandles, $whandles, $wbuf_of) = @_;
    my $rbuf_of = {};

    # the current write offsets, again indexed by the stringified handle
    my $woffset_of;

    my $reader = IO::Select->new;
    for (@$rhandles) {
        $reader->add($_);
        $rbuf_of->{$_} = '';
    }

    my $writer = IO::Select->new;
    for (@$whandles) {
        die("no data supplied for handle " . fileno($_)) if !exists $wbuf_of->{$_};
        if ($wbuf_of->{$_}) {
            $writer->add($_);
        } else { # nothing to write
            close $_;
        }
    }

    # we'll handle EPIPE explicitly below
    local $SIG{PIPE} = 'IGNORE';

    while ($reader->handles || $writer->handles) {
        my @ready = IO::Select->select($reader, $writer, undef, undef);
        if (!@ready) {
            die("error doing select: $!");
        }
        my ($rready, $wready, $eready) = @ready;
        if (@$eready) {
            die("select returned an unexpected exception handle, this shouldn't happen");
        }
        for my $rhandle (@$rready) {
            my $n = fileno($rhandle);
            my $count = sysread($rhandle, $rbuf_of->{$rhandle},
            $blocksize, length($rbuf_of->{$rhandle}));
            warn("read $count bytes from handle $n") if $DEBUG;
            if (!defined $count) { # read error
                if ($!{EPIPE}) {
                    warn("read failure (gpg exited?) from handle $n: $!")
                        if $DEBUG;
                } else {
                    warn("read failure from handle $n: $!");
                }
                $reader->remove($rhandle);
                close $rhandle;
                next;
            }
            if ($count == 0) { # EOF
                warn("read done from handle $n") if $DEBUG;
                $reader->remove($rhandle);
                close $rhandle;
                next;
            }
        }
        for my $whandle (@$wready) {
            my $n = fileno($whandle);
            $woffset_of->{$whandle} = 0 if !exists $woffset_of->{$whandle};
            my $count = syswrite($whandle, $wbuf_of->{$whandle},
                                 $blocksize, $woffset_of->{$whandle});
            if (!defined $count) {
                if ($!{EPIPE}) { # write error
                    warn("write failure (gpg exited?) from handle $n: $!")
                        if $DEBUG;
                } else {
                    warn("write failure from handle $n: $!");
                }
                $writer->remove($whandle);
                close $whandle;
                next;
            }
            warn("wrote $count bytes to handle $n") if $DEBUG;
            $woffset_of->{$whandle} += $count;
            if ($woffset_of->{$whandle} >= length($wbuf_of->{$whandle})) {
                warn("write done to handle $n") if $DEBUG;
                $writer->remove($whandle);
                close $whandle;
                next;
            }
        }
    }
    return $rbuf_of;
}

sub update_icon {
    my $text_type = shift;

    $statusicon->set_from_stock("OpenPGP_Applet-${text_type}");
}

sub detect_received {
    my $clipboard = shift;

    update_icon(detect_text_type(get_validated_clipboard_text()));
}

sub handle_clipboard_owner_change {
    my $clipboard = shift;

    # Each time the applet is used, we receive an owner-change signal for an
    # empty PRIMARY clipboard. We don't want to swich from a valid clipboard to
    # an empty one.
    my $content = $clipboard->wait_for_text;
    if (defined $content && length $content) {
        set_freshest_clipboard($clipboard);
    }
    detect_received($clipboard);
}

# pixmaps base dir is provided by File::ShareDir;
sub make_icon_source {
    my $icon = shift;
    my $base = shift;
    my $ext  = shift;
    my $size = shift;

    my $pixmapdir = File::ShareDir::dist_dir('OpenPGP_Applet') . "/pixmaps";
    my $filename = "$pixmapdir/$base/$icon.$ext";
    my $source = Gtk3::IconSource->new();
    $source->set_filename($filename);
    $source->set_direction_wildcarded(1);
    $source->set_state_wildcarded(1);
    if (defined $size) {
        $source->set_size_wildcarded(0);
        $source->set_size(Gtk3::IconSize::from_name($size));
    } else {
        $source->set_size_wildcarded(1);
    }

    return $source;
}

sub init_icons_stock {
    my $factory = shift;

    $factory->add_default;
    my @stock_ids = map { "OpenPGP_Applet-$_" } qw{ message none signed text };

    foreach my $stock_id (@stock_ids) {
        my $iconset = Gtk3::IconSet->new();
        $iconset->add_source(make_icon_source($stock_id, "22x22",    "png", 'gtk-button'));
        $iconset->add_source(make_icon_source($stock_id, "22x22",    "png", 'gtk-menu'));
        $iconset->add_source(make_icon_source($stock_id, "22x22",    "png", 'gtk-large-toolbar'));
        $iconset->add_source(make_icon_source($stock_id, "22x22",    "png", 'gtk-small-toolbar'));
        $iconset->add_source(make_icon_source($stock_id, "48x48",    "png", 'gtk-dialog'));
        $iconset->add_source(make_icon_source($stock_id, "scalable", "svg"));
        $factory->add($stock_id, $iconset);
    }
}