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
|
/*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) version 3.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the program; if not, see <http://www.gnu.org/licenses/>
*
*
* Authors:
* Chris Toshok <toshok@ximian.com>
*
* Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include "ca-trust-dialog.h"
#include "cert-trust-dialog.h"
#include "certificate-manager.h"
#include "certificate-viewer.h"
#include "e-cert.h"
#include "e-cert-trust.h"
#include "e-cert-db.h"
#include "nss.h"
#include <cms.h>
#include <cert.h>
#include <certdb.h>
#include <pkcs11.h>
#include <pk11func.h>
#include "shell/e-shell.h"
#include "e-util/e-dialog-utils.h"
#include "e-util/e-util.h"
#include "e-util/e-util-private.h"
#include "widgets/misc/e-preferences-window.h"
G_DEFINE_TYPE (ECertManagerConfig, e_cert_manager_config, GTK_TYPE_BOX);
enum {
PROP_0,
PROP_PREFERENCES_WINDOW
};
#define ECMC_TREE_VIEW(o) ecmc->priv->o->treeview
#define PAGE_TREE_VIEW(o) o->treeview
typedef struct {
GType type;
const gchar *column_title;
const gchar * (*get_cert_data_func) (ECert *cert); /* Prototype to e_cert_get_ * functions */
gboolean visible; /* Default visibility of column */
} CertTreeColumn;
static CertTreeColumn yourcerts_columns[] = {
{ G_TYPE_STRING, N_("Certificate Name"), e_cert_get_cn, TRUE },
{ G_TYPE_STRING, N_("Issued To Organization"), e_cert_get_org, FALSE },
{ G_TYPE_STRING, N_("Issued To Organizational Unit"), e_cert_get_org_unit, FALSE },
{ G_TYPE_STRING, N_("Serial Number"), e_cert_get_serial_number, TRUE },
{ G_TYPE_STRING, N_("Purposes"), e_cert_get_usage, TRUE },
{ G_TYPE_STRING, N_("Issued By"), e_cert_get_issuer_cn, TRUE },
{ G_TYPE_STRING, N_("Issued By Organization"), e_cert_get_issuer_org, FALSE },
{ G_TYPE_STRING, N_("Issued By Organizational Unit"), e_cert_get_issuer_org_unit, FALSE },
{ G_TYPE_STRING, N_("Issued"), e_cert_get_issued_on, FALSE },
{ G_TYPE_STRING, N_("Expires"), e_cert_get_expires_on, TRUE },
{ G_TYPE_STRING, N_("SHA1 Fingerprint"), e_cert_get_sha1_fingerprint, FALSE },
{ G_TYPE_STRING, N_("MD5 Fingerprint"), e_cert_get_md5_fingerprint, FALSE },
{ G_TYPE_OBJECT, NULL, NULL, FALSE } /* Hidden column for ECert * object */
};
static const gchar* yourcerts_mime_types[] = { "application/x-x509-user-cert", "application/x-pkcs12", NULL };
static CertTreeColumn contactcerts_columns[] = {
{ G_TYPE_STRING, N_("Certificate Name"), e_cert_get_cn, TRUE },
{ G_TYPE_STRING, N_("Email Address"), e_cert_get_email, TRUE },
{ G_TYPE_STRING, N_("Issued To Organization"), e_cert_get_org, FALSE },
{ G_TYPE_STRING, N_("Issued To Organizational Unit"), e_cert_get_org_unit, FALSE },
{ G_TYPE_STRING, N_("Serial Number"), e_cert_get_serial_number, TRUE },
{ G_TYPE_STRING, N_("Purposes"), e_cert_get_usage, TRUE },
{ G_TYPE_STRING, N_("Issued By"), e_cert_get_issuer_cn, TRUE },
{ G_TYPE_STRING, N_("Issued By Organization"), e_cert_get_issuer_org, FALSE },
{ G_TYPE_STRING, N_("Issued By Organizational Unit"), e_cert_get_issuer_org_unit, FALSE },
{ G_TYPE_STRING, N_("Issued"), e_cert_get_issued_on, FALSE },
{ G_TYPE_STRING, N_("Expires"), e_cert_get_expires_on, TRUE },
{ G_TYPE_STRING, N_("SHA1 Fingerprint"), e_cert_get_sha1_fingerprint, FALSE },
{ G_TYPE_STRING, N_("MD5 Fingerprint"), e_cert_get_md5_fingerprint, FALSE },
{ G_TYPE_OBJECT, NULL, NULL, FALSE }
};
static const gchar* contactcerts_mime_types[] = { "application/x-x509-email-cert", "application/x-x509-ca-cert", NULL };
static CertTreeColumn authoritycerts_columns[] = {
{ G_TYPE_STRING, N_("Certificate Name"), e_cert_get_cn, TRUE },
{ G_TYPE_STRING, N_("Email Address"), e_cert_get_email, TRUE },
{ G_TYPE_STRING, N_("Serial Number"), e_cert_get_serial_number, TRUE },
{ G_TYPE_STRING, N_("Purposes"), e_cert_get_usage, TRUE },
{ G_TYPE_STRING, N_("Issued By"), e_cert_get_issuer_cn, FALSE },
{ G_TYPE_STRING, N_("Issued By Organization"), e_cert_get_issuer_org, FALSE },
{ G_TYPE_STRING, N_("Issued By Organizational Unit"), e_cert_get_issuer_org_unit, FALSE },
{ G_TYPE_STRING, N_("Issued"), e_cert_get_issued_on, FALSE },
{ G_TYPE_STRING, N_("Expires"), e_cert_get_expires_on, TRUE },
{ G_TYPE_STRING, N_("SHA1 Fingerprint"), e_cert_get_sha1_fingerprint, FALSE },
{ G_TYPE_STRING, N_("MD5 Fingerprint"), e_cert_get_md5_fingerprint, FALSE },
{ G_TYPE_OBJECT, NULL, NULL, FALSE }
};
static const gchar* authoritycerts_mime_types[] = { "application/x-x509-ca-cert", NULL };
typedef struct {
GtkTreeView *treeview;
GtkTreeStore *treemodel;
GtkTreeModel *streemodel;
GHashTable *root_hash;
GtkMenu *popup_menu;
GtkWidget *view_button;
GtkWidget *edit_button;
GtkWidget *backup_button;
GtkWidget *backup_all_button;
GtkWidget *import_button;
GtkWidget *delete_button;
CertTreeColumn *columns;
gint columns_count;
ECertType cert_type;
const gchar *cert_filter_name;
const gchar **cert_mime_types;
} CertPage;
struct _ECertManagerConfigPrivate {
GtkBuilder *builder;
EPreferencesWindow *pref_window;
CertPage *yourcerts_page;
CertPage *contactcerts_page;
CertPage *authoritycerts_page;
};
static void view_cert (GtkWidget *button, CertPage *cp);
static void edit_cert (GtkWidget *button, CertPage *cp);
static void delete_cert (GtkWidget *button, CertPage *cp);
static void import_cert (GtkWidget *button, CertPage *cp);
static void load_certs (CertPage *cp);
static void unload_certs (CertPage *cp);
static void
save_treeview_state (GtkTreeView *treeview)
{
GKeyFile *keyfile;
GtkTreeModel *model;
GtkTreeSortable *sortable;
GtkSortType sort_type;
gint columns_count;
gint i = 0;
gint *list;
gchar *cfg_file, *data;
const gchar *tree_name;
g_return_if_fail (treeview && GTK_IS_TREE_VIEW (treeview));
model = gtk_tree_view_get_model (treeview);
g_return_if_fail (model && GTK_IS_TREE_MODEL_SORT (model));
keyfile = g_key_file_new ();
cfg_file = g_build_filename (e_get_user_config_dir (), "cert_trees.ini", NULL);
g_key_file_load_from_file (keyfile, cfg_file, 0, NULL);
tree_name = gtk_widget_get_name (GTK_WIDGET (treeview));
sortable = GTK_TREE_SORTABLE (model);
columns_count = gtk_tree_model_get_n_columns (model) - 1; /* Ignore the last column - the ECert * holder */
list = g_new0 (gint, columns_count);
for (i = 0; i < columns_count; i++) {
GtkTreeViewColumn *column = gtk_tree_view_get_column (treeview, i);
if (gtk_tree_view_column_get_visible (column)) {
list[gtk_tree_view_column_get_sort_column_id (column)] = gtk_tree_view_column_get_width (column);
} else {
list[gtk_tree_view_column_get_sort_column_id (column)] = 0;
}
}
g_key_file_set_integer_list (keyfile, tree_name, "columns", list, columns_count);
g_free (list);
list = g_new0 (gint, columns_count);
for (i = 0; i < columns_count; i++) {
GtkTreeViewColumn *column = gtk_tree_view_get_column (treeview, i);
list[i] = gtk_tree_view_column_get_sort_column_id (column);
}
g_key_file_set_integer_list (keyfile, tree_name, "columns-order", list, columns_count);
g_free (list);
gtk_tree_sortable_get_sort_column_id (sortable, &i, &sort_type);
g_key_file_set_integer (keyfile, tree_name, "sort-column", i);
g_key_file_set_integer (keyfile, tree_name, "sort-order", sort_type);
data = g_key_file_to_data (keyfile, NULL, NULL);
g_file_set_contents (cfg_file, data, -1, NULL);
g_free (data);
g_free (cfg_file);
g_key_file_free (keyfile);
}
static void
load_treeview_state (GtkTreeView *treeview)
{
GKeyFile *keyfile;
gint i, *list;
gsize length;
GtkTreeSortable *sortable;
GtkTreeModel *model;
gchar *cfg_file;
const gchar *tree_name;
g_return_if_fail (treeview && GTK_IS_TREE_VIEW (treeview));
keyfile = g_key_file_new ();
cfg_file = g_build_filename (e_get_user_config_dir (), "cert_trees.ini", NULL);
if (!g_key_file_load_from_file (keyfile, cfg_file, 0, NULL)) {
g_key_file_free (keyfile);
g_free (cfg_file);
return;
}
model = GTK_TREE_MODEL (gtk_tree_view_get_model (treeview));
tree_name = gtk_widget_get_name (GTK_WIDGET (treeview));
list = g_key_file_get_integer_list (keyfile, tree_name, "columns", &length, NULL);
if (list) {
if (length != (gtk_tree_model_get_n_columns (model) - 1)) {
g_debug ("%s: Unexpected number of columns in config file", G_STRFUNC);
g_free (list);
goto exit;
}
for (i = 0; i < length; i++) {
GtkTreeViewColumn *column = gtk_tree_view_get_column (treeview, i);
if (list[i]) {
gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
gtk_tree_view_column_set_fixed_width (column, list[i]);
gtk_tree_view_column_set_visible (column, TRUE);
} else {
gtk_tree_view_column_set_visible (column, FALSE);
}
}
g_free (list);
}
list = g_key_file_get_integer_list (keyfile, tree_name, "columns-order", &length, NULL);
if (list) {
GList *columns = gtk_tree_view_get_columns (treeview);
if (length != g_list_length (columns)) {
g_debug ("%s: Unexpected number of columns in config file", G_STRFUNC);
g_free (list);
goto exit;
}
for (i = (length - 1); i >= 0; i--) {
if ((list[i] >= 0) && (list[i] < length)) {
GtkTreeViewColumn *column = g_list_nth (columns, list[i])->data;
gtk_tree_view_move_column_after (treeview, column, NULL);
} else {
g_warning ("%s: Invalid column number", G_STRFUNC);
}
}
g_free (list);
g_list_free (columns);
}
sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (treeview));
gtk_tree_sortable_set_sort_column_id (sortable,
g_key_file_get_integer (keyfile, tree_name, "sort-column", 0),
g_key_file_get_integer (keyfile, tree_name, "sort-order", GTK_SORT_ASCENDING));
exit:
g_free (cfg_file);
g_key_file_free (keyfile);
}
static void
report_and_free_error (CertPage *cp,
const gchar *where,
GError *error)
{
g_return_if_fail (cp != NULL);
e_notice (gtk_widget_get_toplevel (GTK_WIDGET (cp->treeview)),
GTK_MESSAGE_ERROR, "%s: %s", where, error ? error->message : _("Unknown error"));
if (error)
g_error_free (error);
}
static gboolean
treeview_header_clicked (GtkWidget *widget,
GdkEventButton *event,
gpointer user_data)
{
GtkMenu *menu = user_data;
if (event->button != 3)
return FALSE;
gtk_widget_show_all (GTK_WIDGET (menu));
gtk_menu_popup (menu, NULL, NULL, NULL, NULL, event->button, event->time);
return TRUE;
}
static void
header_popup_item_toggled (GtkCheckMenuItem *item,
gpointer user_data)
{
GtkTreeViewColumn *column = user_data;
gtk_tree_view_column_set_visible (column,
gtk_check_menu_item_get_active (item));
}
static void
treeview_column_visibility_changed (GtkTreeViewColumn *column,
GParamSpec *pspec,
gpointer user_data)
{
GtkCheckMenuItem *menu_item = user_data;
gtk_check_menu_item_set_active (menu_item,
gtk_tree_view_column_get_visible (column));
}
static void
treeview_selection_changed (GtkTreeSelection *selection,
CertPage *cp)
{
GtkTreeIter iter;
gboolean cert_selected = FALSE;
GtkTreeModel *model;
if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
ECert *cert;
gtk_tree_model_get (model, &iter,
cp->columns_count - 1, &cert,
-1);
if (cert) {
cert_selected = TRUE;
g_object_unref (cert);
}
}
if (cp->delete_button)
gtk_widget_set_sensitive (cp->delete_button, cert_selected);
if (cp->edit_button)
gtk_widget_set_sensitive (cp->edit_button, cert_selected);
if (cp->view_button)
gtk_widget_set_sensitive (cp->view_button, cert_selected);
}
static void
treeview_add_column (CertPage *cp,
gint column_index)
{
GtkCellRenderer *cell;
GtkTreeViewColumn *column;
GtkWidget *header, *item;
if (cp->columns[column_index].type != G_TYPE_STRING)
return;
cell = gtk_cell_renderer_text_new ();
g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
column = gtk_tree_view_column_new_with_attributes (
cp->columns[column_index].column_title, cell,
"text", column_index, NULL);
gtk_tree_view_column_set_resizable (column, TRUE);
gtk_tree_view_column_set_reorderable (column, TRUE);
gtk_tree_view_column_set_sort_column_id (column, column_index);
gtk_tree_view_column_set_visible (column, cp->columns[column_index].visible);
gtk_tree_view_append_column (cp->treeview, column);
header = gtk_tree_view_column_get_button (column);
g_signal_connect (header, "button-release-event",
G_CALLBACK (treeview_header_clicked), cp->popup_menu);
/* The first column should not be concealable so there's no point in displaying
* it in the popup menu */
if (column_index == 0)
return;
/* Add item to header popup */
item = gtk_check_menu_item_new_with_label (cp->columns[column_index].column_title);
gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), cp->columns[column_index].visible);
gtk_menu_attach (cp->popup_menu, item, 0, 1, column_index - 1, column_index);
g_signal_connect (item, "toggled",
G_CALLBACK (header_popup_item_toggled), column);
g_signal_connect (column, "notify::visible",
G_CALLBACK (treeview_column_visibility_changed), item);
}
struct find_cert_data {
ECert *cert;
GtkTreePath *path;
CertPage *cp;
};
static gboolean
find_cert_cb (GtkTreeModel *model,
GtkTreePath *path,
GtkTreeIter *iter,
gpointer data)
{
struct find_cert_data *fcd = data;
ECert *cert = NULL;
g_return_val_if_fail (model != NULL, TRUE);
g_return_val_if_fail (iter != NULL, TRUE);
g_return_val_if_fail (data != NULL, TRUE);
/* Get the certificate object from model */
gtk_tree_model_get (model, iter, (fcd->cp->columns_count - 1), &cert, -1);
if (cert && g_strcmp0 (e_cert_get_serial_number (cert), e_cert_get_serial_number (fcd->cert)) == 0
&& g_strcmp0 (e_cert_get_subject_name (cert), e_cert_get_subject_name (fcd->cert)) == 0
&& g_strcmp0 (e_cert_get_sha1_fingerprint (cert), e_cert_get_sha1_fingerprint (fcd->cert)) == 0
&& g_strcmp0 (e_cert_get_md5_fingerprint (cert), e_cert_get_md5_fingerprint (fcd->cert)) == 0) {
fcd->path = gtk_tree_path_copy (path);
}
return fcd->path != NULL;
}
static void
select_certificate (CertPage *cp,
ECert *cert)
{
GtkTreeModel *model;
GtkTreeSelection *selection;
struct find_cert_data fcd;
g_return_if_fail (cp != NULL);
g_return_if_fail (cert != NULL);
g_return_if_fail (E_IS_CERT (cert));
model = gtk_tree_view_get_model (cp->treeview);
g_return_if_fail (model != NULL);
fcd.cp = cp;
fcd.cert = cert;
gtk_tree_model_foreach (model, find_cert_cb, &fcd);
if (fcd.path) {
gtk_tree_view_expand_to_path (cp->treeview, fcd.path);
selection = gtk_tree_view_get_selection (cp->treeview);
gtk_tree_selection_select_path (selection, fcd.path);
gtk_tree_view_scroll_to_cell (cp->treeview, fcd.path, NULL, FALSE, 0.0, 0.0);
gtk_tree_path_free (fcd.path);
}
}
static void
view_cert (GtkWidget *button,
CertPage *cp)
{
GtkTreeIter iter;
if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (cp->treeview), NULL, &iter)) {
ECert *cert;
gtk_tree_model_get (GTK_TREE_MODEL (cp->streemodel), &iter,
cp->columns_count - 1, &cert,
-1);
if (cert) {
GtkWidget *dialog = certificate_viewer_show (cert);
g_signal_connect (dialog, "response",
G_CALLBACK (gtk_widget_destroy), NULL);
gtk_widget_show (dialog);
}
}
}
static void
edit_cert (GtkWidget *button,
CertPage *cp)
{
GtkTreeIter iter;
if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (cp->treeview), NULL, &iter)) {
ECert *cert;
gtk_tree_model_get (GTK_TREE_MODEL (cp->streemodel), &iter,
cp->columns_count - 1, &cert,
-1);
if (cert) {
GtkWidget *dialog;
CERTCertificate *icert = e_cert_get_internal_cert (cert);
switch (cp->cert_type) {
case E_CERT_CA:
dialog = ca_trust_dialog_show (cert, FALSE);
ca_trust_dialog_set_trust (dialog,
e_cert_trust_has_trusted_ca (icert->trust, TRUE, FALSE, FALSE),
e_cert_trust_has_trusted_ca (icert->trust, FALSE, TRUE, FALSE),
e_cert_trust_has_trusted_ca (icert->trust, FALSE, FALSE, TRUE));
break;
case E_CERT_CONTACT:
dialog = cert_trust_dialog_show (cert);
break;
default:
/* Other cert types cannot be edited */
return;
}
if ((gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK) &&
(cp->cert_type == E_CERT_CA)) {
gboolean trust_ssl, trust_email, trust_objsign;
CERTCertTrust trust;
ca_trust_dialog_get_trust (dialog,
&trust_ssl, &trust_email, &trust_objsign);
e_cert_trust_init (&trust);
e_cert_trust_set_valid_ca (&trust);
e_cert_trust_add_ca_trust (&trust,
trust_ssl, trust_email, trust_objsign);
e_cert_db_change_cert_trust (icert, &trust);
}
gtk_widget_destroy (dialog);
}
}
}
static void
import_cert (GtkWidget *button,
CertPage *cp)
{
GtkWidget *filesel;
GtkFileFilter *filter;
gint i;
filesel = gtk_file_chooser_dialog_new (_("Select a certificate to import..."),
NULL, GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL);
gtk_dialog_set_default_response (GTK_DIALOG (filesel), GTK_RESPONSE_OK);
filter = gtk_file_filter_new ();
gtk_file_filter_set_name (filter, cp->cert_filter_name);
for (i = 0; cp->cert_mime_types[i] != NULL; i++) {
gtk_file_filter_add_mime_type (filter, cp->cert_mime_types[i]);
}
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (filesel), filter);
filter = gtk_file_filter_new ();
gtk_file_filter_set_name (filter, _("All files"));
gtk_file_filter_add_pattern (filter, "*");
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (filesel), filter);
if (gtk_dialog_run (GTK_DIALOG (filesel)) == GTK_RESPONSE_OK) {
gchar *filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (filesel));
GSList *imported_certs = NULL;
GError *error = NULL;
gboolean import;
/* destroy dialog to get rid of it in the GUI */
gtk_widget_destroy (filesel);
switch (cp->cert_type) {
case E_CERT_USER:
import = e_cert_db_import_pkcs12_file (e_cert_db_peek (), filename, &error);
break;
case E_CERT_CONTACT:
case E_CERT_CA:
import = e_cert_db_import_certs_from_file (e_cert_db_peek (), filename,
cp->cert_type, &imported_certs, &error);
break;
default:
g_free (filename);
return;
}
if (import) {
unload_certs (cp);
load_certs (cp);
if (imported_certs)
select_certificate (cp, imported_certs->data);
} else {
report_and_free_error (cp, _("Failed to import certificate"), error);
}
g_slist_foreach (imported_certs, (GFunc) g_object_unref, NULL);
g_slist_free (imported_certs);
g_free (filename);
} else
gtk_widget_destroy (filesel);
}
static void
delete_cert (GtkWidget *button,
CertPage *cp)
{
GtkTreeIter iter;
if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (cp->treeview), NULL, &iter)) {
ECert *cert;
gtk_tree_model_get (GTK_TREE_MODEL (cp->streemodel), &iter,
cp->columns_count - 1, &cert,
-1);
if (cert && e_cert_db_delete_cert (e_cert_db_peek (), cert)) {
GtkTreeIter child_iter;
gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (cp->streemodel), &child_iter, &iter);
gtk_tree_store_remove (cp->treemodel, &child_iter);
/* we need two unrefs here, one to unref the
* gtk_tree_model_get above, and one to unref
* the initial ref when we created the cert
* and added it to the tree */
g_object_unref (cert);
g_object_unref (cert);
}
}
}
static void
add_cert (CertPage *cp,
ECert *cert)
{
GtkTreeIter iter;
GtkTreeIter *parent_iter = NULL;
const gchar *organization = e_cert_get_org (cert);
GtkTreeModel *model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (cp->streemodel));
gint i;
if (organization) {
parent_iter = g_hash_table_lookup (cp->root_hash, organization);
if (!parent_iter) {
/* create a new toplevel node */
gtk_tree_store_append (GTK_TREE_STORE (model), &iter, NULL);
gtk_tree_store_set (GTK_TREE_STORE (model), &iter,
0, organization, -1);
/* now copy it off into parent_iter and insert it into
* the hashtable */
parent_iter = gtk_tree_iter_copy (&iter);
g_hash_table_insert (cp->root_hash, g_strdup (organization), parent_iter);
}
}
gtk_tree_store_append (GTK_TREE_STORE (model), &iter, parent_iter);
for (i = 0; i < cp->columns_count; i++) {
const gchar * (*get_cert_data_func) (ECert *cert);
/* When e_cert_get_cn() is empty, use _get_nickname() */
if ((cp->columns[i].get_cert_data_func == e_cert_get_cn) && (!e_cert_get_cn (cert))) {
get_cert_data_func = e_cert_get_nickname;
} else {
get_cert_data_func = cp->columns[i].get_cert_data_func;
}
if (cp->columns[i].type == G_TYPE_STRING) {
gtk_tree_store_set (GTK_TREE_STORE (model), &iter,
i, get_cert_data_func (cert), -1);
} else if (cp->columns[i].type == G_TYPE_OBJECT) {
gtk_tree_store_set (GTK_TREE_STORE (model), &iter,
i, cert, -1);
}
}
}
static void
unload_certs (CertPage *cp)
{
GtkTreeStore *treemodel;
GType types[cp->columns_count];
gint i;
g_return_if_fail (cp != NULL);
for (i = 0; i < cp->columns_count; i++)
types[i] = cp->columns[i].type;
treemodel = gtk_tree_store_newv (cp->columns_count, types);
if (cp->streemodel)
g_object_unref (cp->streemodel);
cp->streemodel = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (treemodel));
g_object_unref (treemodel);
gtk_tree_view_set_model (cp->treeview, cp->streemodel);
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (cp->streemodel), 0, GTK_SORT_ASCENDING);
if (cp->root_hash)
g_hash_table_destroy (cp->root_hash);
cp->root_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
(GDestroyNotify) g_free,
(GDestroyNotify) gtk_tree_iter_free);
}
static void
load_certs (CertPage *cp)
{
CERTCertList *certList;
CERTCertListNode *node;
g_return_if_fail (cp != NULL);
certList = PK11_ListCerts (PK11CertListUnique, NULL);
for (node = CERT_LIST_HEAD (certList);
!CERT_LIST_END (node, certList);
node = CERT_LIST_NEXT (node)) {
ECert *cert = e_cert_new (CERT_DupCertificate ((CERTCertificate *) node->cert));
ECertType ct = e_cert_get_cert_type (cert);
/* show everything else in a contact tab */
if (ct == cp->cert_type || (cp->cert_type == E_CERT_CONTACT && ct != E_CERT_CA && ct != E_CERT_USER)) {
add_cert (cp, cert);
} else {
g_object_unref (cert);
}
}
CERT_DestroyCertList (certList);
}
static gboolean
populate_ui (ECertManagerConfig *ecmc)
{
/* This is an idle callback. */
ECertManagerConfigPrivate *priv = ecmc->priv;
unload_certs (priv->yourcerts_page);
load_certs (priv->yourcerts_page);
unload_certs (priv->contactcerts_page);
load_certs (priv->contactcerts_page);
unload_certs (priv->authoritycerts_page);
load_certs (priv->authoritycerts_page);
/* expand all three trees */
gtk_tree_view_expand_all (ECMC_TREE_VIEW (yourcerts_page));
gtk_tree_view_expand_all (ECMC_TREE_VIEW (contactcerts_page));
gtk_tree_view_expand_all (ECMC_TREE_VIEW (authoritycerts_page));
/* Now load settings of each treeview */
load_treeview_state (ECMC_TREE_VIEW (yourcerts_page));
load_treeview_state (ECMC_TREE_VIEW (contactcerts_page));
load_treeview_state (ECMC_TREE_VIEW (authoritycerts_page));
return FALSE;
}
static void
initialize_ui (CertPage *cp)
{
GtkTreeSelection *selection;
gint i;
cp->popup_menu = GTK_MENU (gtk_menu_new ());
/* Add columns to treeview */
for (i = 0; i < cp->columns_count; i++)
treeview_add_column (cp, i);
selection = gtk_tree_view_get_selection (cp->treeview);
g_signal_connect (selection, "changed",
G_CALLBACK (treeview_selection_changed), cp);
if (cp->import_button)
g_signal_connect (cp->import_button, "clicked",
G_CALLBACK (import_cert), cp);
if (cp->edit_button)
g_signal_connect (cp->edit_button, "clicked",
G_CALLBACK (edit_cert), cp);
if (cp->delete_button)
g_signal_connect (cp->delete_button, "clicked",
G_CALLBACK (delete_cert), cp);
if (cp->view_button)
g_signal_connect (cp->view_button, "clicked",
G_CALLBACK (view_cert), cp);
}
static void
cert_manager_config_window_hide (ECertManagerConfig *ecmc,
EPreferencesWindow *epw)
{
g_return_if_fail (ecmc);
save_treeview_state (ECMC_TREE_VIEW (yourcerts_page));
save_treeview_state (ECMC_TREE_VIEW (contactcerts_page));
save_treeview_state (ECMC_TREE_VIEW (authoritycerts_page));
}
static void
free_cert (GtkTreeModel *model,
GtkTreePath *path,
GtkTreeIter *iter,
gpointer user_data)
{
CertPage *cp = user_data;
ECert *cert;
gtk_tree_model_get (model, iter, cp->columns_count - 1, &cert, -1);
/* Double unref: one for gtk_tree_model_get() and one for e_cert_new() */
g_object_unref (cert);
g_object_unref (cert);
}
static void
cert_page_free (CertPage *cp)
{
if (!cp)
return;
/* Free all certificates in treeviews */
if (cp->treemodel) {
gtk_tree_model_foreach (GTK_TREE_MODEL (cp->treemodel),
(GtkTreeModelForeachFunc) free_cert, cp);
}
/* Free streemodel */
if (cp->streemodel) {
g_object_unref (cp->streemodel);
cp->streemodel = NULL;
}
if (cp->root_hash) {
g_hash_table_unref (cp->root_hash);
cp->root_hash = NULL;
}
g_free (cp);
}
static void
cert_manager_config_dispose (GObject *object)
{
ECertManagerConfig *ecmc = E_CERT_MANAGER_CONFIG (object);
if (ecmc->priv->yourcerts_page) {
cert_page_free (ecmc->priv->yourcerts_page);
ecmc->priv->yourcerts_page = NULL;
}
if (ecmc->priv->contactcerts_page) {
cert_page_free (ecmc->priv->contactcerts_page);
ecmc->priv->contactcerts_page = NULL;
}
if (ecmc->priv->authoritycerts_page) {
cert_page_free (ecmc->priv->authoritycerts_page);
ecmc->priv->authoritycerts_page = NULL;
}
if (ecmc->priv->builder) {
g_object_unref (ecmc->priv->builder);
ecmc->priv->builder = NULL;
}
if (ecmc->priv->pref_window) {
g_signal_handlers_disconnect_matched (ecmc->priv->pref_window, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, ecmc);
ecmc->priv->pref_window = NULL;
}
G_OBJECT_CLASS (e_cert_manager_config_parent_class)->dispose (object);
}
static void
cert_manager_config_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
ECertManagerConfig *ecmc = E_CERT_MANAGER_CONFIG (object);
switch (property_id) {
case PROP_PREFERENCES_WINDOW:
ecmc->priv->pref_window = g_value_get_object (value);
/* When the preferences window is "closed" (= hidden), save
* state of all treeviews. */
g_signal_connect_swapped (ecmc->priv->pref_window, "hide",
G_CALLBACK (cert_manager_config_window_hide), ecmc);
return;
}
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
static void
e_cert_manager_config_class_init (ECertManagerConfigClass *class)
{
GObjectClass *object_class;
g_type_class_add_private (class, sizeof (ECertManagerConfigPrivate));
object_class = G_OBJECT_CLASS (class);
object_class->set_property = cert_manager_config_set_property;
object_class->dispose = cert_manager_config_dispose;
g_object_class_install_property (
object_class,
PROP_PREFERENCES_WINDOW,
g_param_spec_object (
"preferences-window",
NULL,
NULL,
E_TYPE_PREFERENCES_WINDOW,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
}
static void
e_cert_manager_config_init (ECertManagerConfig *ecmc)
{
ECertManagerConfigPrivate *priv;
GtkWidget *parent, *widget;
CertPage *cp;
priv = G_TYPE_INSTANCE_GET_PRIVATE (
ecmc, E_TYPE_CERT_MANAGER_CONFIG, ECertManagerConfigPrivate);
ecmc->priv = priv;
/* We need to peek the db here to make sure it (and NSS) are fully initialized. */
e_cert_db_peek ();
priv->builder = gtk_builder_new ();
e_load_ui_builder_definition (priv->builder, "smime-ui.ui");
cp = g_new0 (CertPage, 1);
priv->yourcerts_page = cp;
cp->treeview = GTK_TREE_VIEW (e_builder_get_widget (priv->builder, "yourcerts-treeview"));
cp->streemodel = NULL;
cp->treemodel = NULL;
cp->view_button = e_builder_get_widget (priv->builder, "your-view-button");
cp->backup_button = e_builder_get_widget (priv->builder, "your-backup-button");
cp->backup_all_button = e_builder_get_widget (priv->builder, "your-backup-all-button");
cp->edit_button = NULL;
cp->import_button = e_builder_get_widget (priv->builder, "your-import-button");
cp->delete_button = e_builder_get_widget (priv->builder, "your-delete-button");
cp->columns = yourcerts_columns;
cp->columns_count = G_N_ELEMENTS (yourcerts_columns);
cp->cert_type = E_CERT_USER;
cp->cert_filter_name = _("All PKCS12 files");
cp->cert_mime_types = yourcerts_mime_types;
initialize_ui (cp);
cp = g_new0 (CertPage, 1);
priv->contactcerts_page = cp;
cp->treeview = GTK_TREE_VIEW (e_builder_get_widget (priv->builder, "contactcerts-treeview"));
cp->streemodel = NULL;
cp->treemodel = NULL;
cp->view_button = e_builder_get_widget (priv->builder, "contact-view-button");
cp->backup_button = NULL;
cp->backup_all_button = NULL;
cp->edit_button = e_builder_get_widget (priv->builder, "contact-edit-button");
cp->import_button = e_builder_get_widget (priv->builder, "contact-import-button");
cp->delete_button = e_builder_get_widget (priv->builder, "contact-delete-button");
cp->columns = contactcerts_columns;
cp->columns_count = G_N_ELEMENTS (contactcerts_columns);
cp->cert_type = E_CERT_CONTACT;
cp->cert_filter_name = _("All email certificate files");
cp->cert_mime_types = contactcerts_mime_types;
initialize_ui (cp);
cp = g_new0 (CertPage, 1);
priv->authoritycerts_page = cp;
cp->treeview = GTK_TREE_VIEW (e_builder_get_widget (priv->builder, "authoritycerts-treeview"));
cp->streemodel = NULL;
cp->treemodel = NULL;
cp->view_button = e_builder_get_widget (priv->builder, "authority-view-button");
cp->backup_button = NULL;
cp->backup_all_button = NULL;
cp->edit_button = e_builder_get_widget (priv->builder, "authority-edit-button");
cp->import_button = e_builder_get_widget (priv->builder, "authority-import-button");
cp->delete_button = e_builder_get_widget (priv->builder, "authority-delete-button");
cp->columns = authoritycerts_columns;
cp->columns_count = G_N_ELEMENTS (authoritycerts_columns);
cp->cert_type = E_CERT_CA;
cp->cert_filter_name = _("All CA certificate files");
cp->cert_mime_types = authoritycerts_mime_types;
initialize_ui (cp);
/* Run this in an idle callback so Evolution has a chance to
* fully initialize itself and start its main loop before we
* load certificates, since doing so may trigger a password
* dialog, and dialogs require a main loop. */
g_idle_add ((GSourceFunc) populate_ui, ecmc);
/* Disconnect cert-manager-notebook from it's window and attach it
* to this ECertManagerConfig */
widget = e_builder_get_widget (priv->builder, "cert-manager-notebook");
parent = gtk_widget_get_parent (widget);
gtk_container_remove (GTK_CONTAINER (parent), widget);
gtk_box_pack_start (GTK_BOX (ecmc), widget, TRUE, TRUE, 0);
gtk_widget_show_all (widget);
/* FIXME: remove when implemented */
gtk_widget_set_sensitive (priv->yourcerts_page->backup_button, FALSE);
gtk_widget_set_sensitive (priv->yourcerts_page->backup_all_button, FALSE);
}
GtkWidget *
e_cert_manager_config_new (EPreferencesWindow *window)
{
ECertManagerConfig *ecmc;
ecmc = g_object_new (E_TYPE_CERT_MANAGER_CONFIG, "preferences-window", window, NULL);
return GTK_WIDGET (ecmc);
}
|