Statistics
| Revision:

root / src / sigstatus.c @ 322

History | View | Annotate | Download (6.8 kB)

1 1 hiro
/* sigstatus.h - GTK+ based signature status display
2 1 hiro
 *      Copyright (C) 2001 Werner Koch (dd9jn)
3 1 hiro
 *
4 1 hiro
 * This program is free software; you can redistribute it and/or modify
5 1 hiro
 * it under the terms of the GNU General Public License as published by
6 1 hiro
 * the Free Software Foundation; either version 2 of the License, or
7 1 hiro
 * (at your option) any later version.
8 1 hiro
 *
9 1 hiro
 * This program is distributed in the hope that it will be useful,
10 1 hiro
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 1 hiro
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 1 hiro
 * GNU General Public License for more details.
13 1 hiro
 *
14 1 hiro
 * You should have received a copy of the GNU General Public License
15 1 hiro
 * along with this program; if not, write to the Free Software
16 1 hiro
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 1 hiro
 */
18 1 hiro
19 1 hiro
#ifdef HAVE_CONFIG_H
20 1 hiro
#  include "config.h"
21 1 hiro
#endif
22 1 hiro
23 1 hiro
#if USE_GPGME
24 1 hiro
25 1 hiro
#include <glib.h>
26 92 hiro
#include <glib/gi18n.h>
27 140 hiro
#include <gtk/gtkdialog.h>
28 1 hiro
#include <gtk/gtkvbox.h>
29 1 hiro
#include <gtk/gtkhbox.h>
30 1 hiro
#include <gtk/gtklabel.h>
31 1 hiro
#include <gtk/gtkbutton.h>
32 36 hiro
#include <gtk/gtkstock.h>
33 1 hiro
#include <gdk/gdkkeysyms.h>
34 1 hiro
#include <gpgme.h>
35 1 hiro
36 1 hiro
#include "gtkutils.h"
37 1 hiro
#include "utils.h"
38 1 hiro
#include "sigstatus.h"
39 1 hiro
40 1 hiro
/* remove the window after 30 seconds to avoid cluttering the deskop
41 1 hiro
 * with too many of them */
42 1 hiro
#define MY_TIMEOUT (30*1000)
43 1 hiro
44 1 hiro
struct gpgmegtk_sig_status_s {
45 1 hiro
        GtkWidget *mainwindow;
46 1 hiro
        GtkWidget *label;
47 1 hiro
        gint running;
48 1 hiro
        gint destroy_pending;
49 1 hiro
        guint timeout_id;
50 1 hiro
        gint timeout_id_valid;
51 1 hiro
};
52 1 hiro
53 1 hiro
54 1 hiro
static void do_destroy(GpgmegtkSigStatus hd)
55 1 hiro
{
56 1 hiro
        if (!hd->running) {
57 140 hiro
                if (hd->timeout_id_valid) {
58 140 hiro
                        gtk_timeout_remove(hd->timeout_id);
59 140 hiro
                        hd->timeout_id_valid = 0;
60 140 hiro
                }
61 1 hiro
                if (hd->mainwindow) {
62 1 hiro
                        gtk_widget_destroy ( hd->mainwindow );
63 1 hiro
                        hd->mainwindow = NULL;
64 1 hiro
                }
65 1 hiro
                if (hd->destroy_pending)
66 140 hiro
                        g_free(hd);
67 1 hiro
        }
68 1 hiro
}
69 1 hiro
70 1 hiro
static void okay_cb(GtkWidget *widget, gpointer data)
71 1 hiro
{
72 1 hiro
        GpgmegtkSigStatus hd = data;
73 1 hiro
74 1 hiro
        hd->running = 0;
75 1 hiro
        do_destroy(hd);
76 1 hiro
}
77 1 hiro
78 140 hiro
static gboolean delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
79 1 hiro
{
80 1 hiro
        GpgmegtkSigStatus hd = data;
81 1 hiro
82 1 hiro
        hd->running = 0;
83 1 hiro
        do_destroy(hd);
84 1 hiro
85 1 hiro
        return TRUE;
86 1 hiro
}
87 1 hiro
88 1 hiro
static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
89 1 hiro
{
90 1 hiro
        GpgmegtkSigStatus hd = data;
91 1 hiro
92 1 hiro
        if (event && event->keyval == GDK_Escape) {
93 1 hiro
                hd->running = 0;
94 1 hiro
                do_destroy(hd);
95 140 hiro
                return TRUE;
96 1 hiro
        }
97 1 hiro
        return FALSE;
98 1 hiro
}
99 1 hiro
100 1 hiro
GpgmegtkSigStatus gpgmegtk_sig_status_create(void)
101 1 hiro
{
102 1 hiro
        GtkWidget *window;
103 1 hiro
        GtkWidget *vbox;
104 1 hiro
        GtkWidget *hbox;
105 1 hiro
        GtkWidget *label;
106 1 hiro
        GtkWidget *okay_btn;
107 1 hiro
        GtkWidget *okay_area;
108 1 hiro
        GpgmegtkSigStatus hd;
109 1 hiro
110 1 hiro
        hd = g_malloc0(sizeof *hd);
111 1 hiro
        hd->running = 1;
112 1 hiro
113 140 hiro
        window = gtk_dialog_new();
114 1 hiro
        hd->mainwindow = window;
115 1 hiro
        gtk_widget_set_size_request(window, 400, -1);
116 140 hiro
        gtk_window_set_title(GTK_WINDOW(window), _("Signature check result"));
117 140 hiro
        gtk_window_set_policy(GTK_WINDOW(window), FALSE, FALSE, FALSE);
118 1 hiro
        gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
119 140 hiro
        gtk_dialog_set_has_separator(GTK_DIALOG(window), FALSE);
120 1 hiro
        g_signal_connect(G_OBJECT(window), "delete_event",
121 1 hiro
                         G_CALLBACK(delete_event), hd);
122 1 hiro
        g_signal_connect(G_OBJECT(window), "key_press_event",
123 1 hiro
                         G_CALLBACK(key_pressed), hd);
124 1 hiro
125 1 hiro
        vbox = gtk_vbox_new(FALSE, 8);
126 140 hiro
        gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
127 140 hiro
        gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
128 140 hiro
                           vbox, TRUE, TRUE, 0);
129 1 hiro
        gtk_widget_show(vbox);
130 1 hiro
131 1 hiro
        hbox = gtk_hbox_new(FALSE, 0);
132 140 hiro
        gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
133 1 hiro
        gtk_widget_show(hbox);
134 1 hiro
135 1 hiro
        label = gtk_label_new(_("Checking signature"));
136 1 hiro
        hd->label = label;
137 1 hiro
        gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 8);
138 140 hiro
        gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
139 140 hiro
        gtk_label_set_selectable(GTK_LABEL(label), TRUE);
140 1 hiro
        gtk_widget_show(label);
141 1 hiro
142 31 hiro
        gtkut_stock_button_set_create(&okay_area, &okay_btn, GTK_STOCK_OK,
143 31 hiro
                                      NULL, NULL, NULL, NULL);
144 140 hiro
        gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window)->action_area),
145 140 hiro
                         okay_area, FALSE, FALSE, 0);
146 140 hiro
        gtk_container_set_border_width(GTK_CONTAINER(okay_area), 5);
147 1 hiro
        gtk_widget_grab_default(okay_btn);
148 140 hiro
        gtk_widget_grab_focus(okay_btn);
149 1 hiro
        g_signal_connect(G_OBJECT(okay_btn), "clicked",
150 1 hiro
                         G_CALLBACK(okay_cb), hd);
151 1 hiro
152 1 hiro
        gtk_widget_show_all(window);
153 1 hiro
154 1 hiro
        while (gtk_events_pending())
155 1 hiro
                gtk_main_iteration();
156 1 hiro
157 1 hiro
        return hd;
158 1 hiro
}
159 1 hiro
160 1 hiro
static gint timeout_cb(gpointer data)
161 1 hiro
{
162 1 hiro
        GpgmegtkSigStatus hd = data;
163 1 hiro
164 1 hiro
        hd->running = 0;
165 1 hiro
        hd->timeout_id_valid = 0;
166 1 hiro
        do_destroy(hd);
167 1 hiro
168 1 hiro
        return FALSE;
169 1 hiro
}
170 1 hiro
171 1 hiro
void gpgmegtk_sig_status_destroy(GpgmegtkSigStatus hd)
172 1 hiro
{
173 1 hiro
        if (hd) {
174 1 hiro
                hd->destroy_pending = 1;
175 1 hiro
                if (hd->running && !hd->timeout_id_valid) {
176 1 hiro
                        hd->timeout_id = gtk_timeout_add(MY_TIMEOUT,
177 1 hiro
                                                         timeout_cb, hd);
178 1 hiro
                        hd->timeout_id_valid = 1;
179 1 hiro
                }
180 1 hiro
                do_destroy(hd);
181 1 hiro
        }
182 1 hiro
}
183 1 hiro
184 56 hiro
void gpgmegtk_sig_status_update(GpgmegtkSigStatus hd, gpgme_ctx_t ctx)
185 1 hiro
{
186 56 hiro
        gpgme_verify_result_t result;
187 56 hiro
        gpgme_signature_t sig;
188 1 hiro
        gchar *text = NULL;
189 1 hiro
190 1 hiro
        if (!hd || !hd->running || !ctx)
191 1 hiro
                return;
192 56 hiro
        result = gpgme_op_verify_result(ctx);
193 56 hiro
        sig = result->signatures;
194 56 hiro
        while (sig) {
195 1 hiro
                gchar *tmp;
196 1 hiro
                const gchar *userid;
197 56 hiro
                gpgme_key_t key = NULL;
198 1 hiro
199 56 hiro
                if (!gpgme_get_key(ctx, sig->fpr, &key, 0)) {
200 56 hiro
                        userid = key->uids->uid;
201 56 hiro
                } else {
202 1 hiro
                        userid = "[?]";
203 56 hiro
                }
204 140 hiro
                tmp = g_strdup_printf
205 140 hiro
                        (_("%s%s%s from \"%s\""),
206 140 hiro
                         text ? text : "", text ? "\n" : "",
207 140 hiro
                         gpgmegtk_sig_status_to_string(sig, FALSE), userid);
208 1 hiro
                g_free (text);
209 1 hiro
                text = tmp;
210 1 hiro
                gpgme_key_unref (key);
211 56 hiro
                sig = sig->next;
212 1 hiro
        }
213 1 hiro
        gtk_label_set_text(GTK_LABEL(hd->label), text);
214 1 hiro
        g_free(text);
215 1 hiro
216 1 hiro
        while (gtk_events_pending())
217 1 hiro
                gtk_main_iteration();
218 1 hiro
}
219 1 hiro
220 56 hiro
const gchar *gpgmegtk_sig_status_to_string(gpgme_signature_t signature,
221 140 hiro
                                           gboolean use_name)
222 1 hiro
{
223 56 hiro
        const gchar *result = "?";
224 140 hiro
225 216 hiro
        g_return_val_if_fail(signature != NULL, result);
226 216 hiro
227 140 hiro
        switch (gpg_err_code(signature->status)) {
228 140 hiro
        case GPG_ERR_NO_DATA:
229 1 hiro
                result = _("No signature found");
230 1 hiro
                break;
231 140 hiro
        case GPG_ERR_NO_ERROR:
232 140 hiro
                switch (signature->validity) {
233 140 hiro
                case GPGME_VALIDITY_ULTIMATE:
234 140 hiro
                case GPGME_VALIDITY_FULL:
235 140 hiro
                case GPGME_VALIDITY_MARGINAL:
236 140 hiro
                        result = use_name ? _("Good signature from \"%s\"") :
237 140 hiro
                                _("Good signature");
238 140 hiro
                        break;
239 140 hiro
                default:
240 140 hiro
                        result = use_name ?
241 140 hiro
                                _("Valid signature but the key for \"%s\" is not trusted") :
242 140 hiro
                                _("Valid signature (untrusted key)");
243 140 hiro
                        break;
244 140 hiro
                }
245 1 hiro
                break;
246 140 hiro
        case GPG_ERR_SIG_EXPIRED:
247 140 hiro
                result = use_name ? _("Signature valid but expired for \"%s\"") :
248 140 hiro
                        _("Signature valid but expired");
249 140 hiro
                break;
250 140 hiro
        case GPG_ERR_KEY_EXPIRED:
251 140 hiro
                result = use_name ? _("Signature valid but the signing key for \"%s\" has expired") :
252 140 hiro
                        _("Signature valid but the signing key has expired");
253 140 hiro
                break;
254 140 hiro
        case GPG_ERR_CERT_REVOKED:
255 140 hiro
                result = use_name ? _("Signature valid but the signing key for \"%s\" has been revoked") :
256 140 hiro
                        _("Signature valid but the signing key has been revoked");
257 140 hiro
                break;
258 140 hiro
        case GPG_ERR_BAD_SIGNATURE:
259 56 hiro
                result = use_name ? _("BAD signature from \"%s\"") :
260 140 hiro
                        _("BAD signature");
261 1 hiro
                break;
262 140 hiro
        case GPG_ERR_NO_PUBKEY:
263 1 hiro
                result = _("No public key to verify the signature");
264 1 hiro
                break;
265 1 hiro
        default:
266 140 hiro
                result = _("Error verifying the signature");
267 1 hiro
                break;
268 1 hiro
        }
269 1 hiro
270 1 hiro
        return result;
271 1 hiro
}
272 1 hiro
273 1 hiro
#endif /* USE_GPGME */