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