aboutsummaryrefslogtreecommitdiffstats
path: root/smime/gui/e-cert-selector.c
blob: 0ac946b5b808675ede2a33f3b1cebd34e220c861 (plain) (blame)
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
/*
 *
 * 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:
 *      Michael Zucchi <notzed@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib/gi18n.h>

#include "nss.h"
#include "pk11func.h"
#include "certdb.h"
#include "cert.h"

#include <glade/glade.h>

#include "e-cert-selector.h"

#include "e-util/e-util-private.h"

struct _ECertSelectorPrivate {
    CERTCertList *certlist;

    GtkWidget *combobox, *description;
};

enum {
    ECS_SELECTED,
    ECS_LAST_SIGNAL
};

static guint ecs_signals[ECS_LAST_SIGNAL];

G_DEFINE_TYPE (ECertSelector, e_cert_selector, GTK_TYPE_DIALOG)

/* (this is what mozilla shows)
Issued to:
  Subject: E=notzed@ximian.com, CN=notzed@ximian.com, O=My Company Ltd, L=Adelaide, ST=SA, C=AU
  Serial Number: 03
  Valid from 23/10/03 06:35:29 to 22/10/04 06:35:29
  Purposes: Sign,Encrypt
Issued by:
  Subject: E=notzed@ximian.com, O=company, L=there, ST=Here, C=AU
*/

static CERTCertListNode *
ecs_find_current(ECertSelector *ecs)
{
    struct _ECertSelectorPrivate *p = ecs->priv;
    CERTCertListNode *node;
    int n;

    if (p->certlist == NULL || CERT_LIST_EMPTY(p->certlist))
        return NULL;

    n = gtk_combo_box_get_active (GTK_COMBO_BOX (p->combobox));
    node = CERT_LIST_HEAD(p->certlist);
    while (n>0 && !CERT_LIST_END(node, p->certlist)) {
        n--;
        node = CERT_LIST_NEXT(node);
    }

    g_return_val_if_fail (!CERT_LIST_END(node, p->certlist), NULL);

    return node;
}

static void
e_cert_selector_response(GtkDialog *dialog, gint button)
{
    CERTCertListNode *node;

    switch (button) {
    case GTK_RESPONSE_OK:
        node = ecs_find_current((ECertSelector *)dialog);
        break;
    default:
        node = NULL;
        break;
    }

    g_signal_emit(dialog, ecs_signals[ECS_SELECTED], 0, node?node->cert->nickname:NULL);
}

static void
ecs_cert_changed(GtkWidget *w, ECertSelector *ecs)
{
    struct _ECertSelectorPrivate *p = ecs->priv;
    CERTCertListNode *node;
    GtkTextBuffer *buffer;
    GString *text;

    text = g_string_new("");
    node = ecs_find_current(ecs);
    if (node) {
        /* FIXME: add serial no, validity date, uses */
        g_string_append_printf(text, _("Issued to:\n  Subject: %s\n"), node->cert->subjectName);
        g_string_append_printf(text, _("Issued by:\n  Subject: %s\n"), node->cert->issuerName);
    }

    buffer = gtk_text_view_get_buffer((GtkTextView *)p->description);
    gtk_text_buffer_set_text(buffer, text->str, text->len);
    g_string_free(text, TRUE);
}

/**
 * e_cert_selector_new:
 * @type:
 * @currentid:
 *
 * Create a new ECertSelector dialog.  @type specifies which type of cert to
 * be selected, E_CERT_SELECTOR_SIGNER for signing certs, and
 * E_CERT_SELECTOR_RECIPIENT for encrypting certs.
 *
 * @currentid is the nickname of the cert currently selected for this user.
 *
 * You only need to connect to a single signal "selected" which will
 * be called with either a NULL nickname if cancelled, or the newly
 * selected nickname otherwise.
 *
 * Return value: A dialogue to be shown.
 **/
GtkWidget *
e_cert_selector_new(int type, const char *currentid)
{
    ECertSelector *ecs;
    struct _ECertSelectorPrivate *p;
    SECCertUsage usage;
    CERTCertList *certlist;
    CERTCertListNode *node;
    GladeXML *gui;
    GtkWidget *w;
    int n=0, active=0;
    char *gladefile;

    ecs = g_object_new(e_cert_selector_get_type(), NULL);
    p = ecs->priv;

    gladefile = g_build_filename(EVOLUTION_GLADEDIR,
                     "smime-ui.glade",
                     NULL);
    gui = glade_xml_new(gladefile, "cert_selector_vbox", NULL);
    g_free (gladefile);

    p->combobox = glade_xml_get_widget(gui, "cert_combobox");
    p->description = glade_xml_get_widget(gui, "cert_description");

    w = glade_xml_get_widget(gui, "cert_selector_vbox");
    gtk_box_pack_start((GtkBox *)((GtkDialog *)ecs)->vbox, w, TRUE, TRUE, 3);
    gtk_window_set_title(GTK_WINDOW(ecs), _("Select certificate"));

    switch (type) {
    case E_CERT_SELECTOR_SIGNER:
    default:
        usage = certUsageEmailSigner;
        break;
    case E_CERT_SELECTOR_RECIPIENT:
        usage = certUsageEmailRecipient;
        break;
    }

    gtk_list_store_clear (GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (p->combobox))));

    certlist = CERT_FindUserCertsByUsage(CERT_GetDefaultCertDB(), usage, FALSE, TRUE, NULL);
    ecs->priv->certlist = certlist;
    if (certlist != NULL) {
        node = CERT_LIST_HEAD(certlist);
        while (!CERT_LIST_END(node, certlist)) {
            if (node->cert->nickname || node->cert->emailAddr) {
                gtk_combo_box_append_text (GTK_COMBO_BOX (p->combobox), node->cert->nickname?node->cert->nickname:node->cert->emailAddr);

                if (currentid != NULL
                    && ((node->cert->nickname != NULL && strcmp(node->cert->nickname, currentid) == 0)
                    || (node->cert->emailAddr != NULL && strcmp(node->cert->emailAddr, currentid) == 0)))
                    active = n;

                n++;
            }

            node = CERT_LIST_NEXT(node);
        }
    }

    gtk_combo_box_set_active (GTK_COMBO_BOX (p->combobox), active);

    g_signal_connect (p->combobox, "changed", G_CALLBACK(ecs_cert_changed), ecs);

    g_object_unref(gui);

    ecs_cert_changed(p->combobox, ecs);

    return GTK_WIDGET(ecs);
}

static void
e_cert_selector_init(ECertSelector *ecs)
{
    gtk_dialog_add_buttons((GtkDialog *)ecs,
                   GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                   GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);

    ecs->priv = g_malloc0(sizeof(*ecs->priv));
}

static void
e_cert_selector_finalise(GObject *o)
{
    ECertSelector *ecs = (ECertSelector *)o;

    if (ecs->priv->certlist)
        CERT_DestroyCertList(ecs->priv->certlist);

    g_free(ecs->priv);

    ((GObjectClass *)e_cert_selector_parent_class)->finalize(o);
}

static void
e_cert_selector_class_init(ECertSelectorClass *klass)
{
    ((GObjectClass *)klass)->finalize = e_cert_selector_finalise;
    ((GtkDialogClass *)klass)->response = e_cert_selector_response;

    ecs_signals[ECS_SELECTED] =
        g_signal_new("selected",
                 G_OBJECT_CLASS_TYPE(klass),
                 G_SIGNAL_RUN_LAST,
                 G_STRUCT_OFFSET(ECertSelectorClass, selected),
                 NULL, NULL,
                 g_cclosure_marshal_VOID__POINTER,
                 G_TYPE_NONE, 1, G_TYPE_POINTER);
}