initial message templates support
[claws.git] / src / sigstatus.c
blobb2b48715eac1e2b15f2a6ff3431c31061b1fdc27
1 /* sigstatus.h - GTK+ based signature status display
2 * Copyright (C) 2001 Werner Koch (dd9jn)
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.
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.
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.
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
23 #if USE_GPGME
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>
34 #include "intl.h"
35 #include "gtkutils.h"
36 #include "utils.h"
37 #include "sigstatus.h"
39 /* remove the window after 30 seconds to avoid cluttering the deskop
40 * with too many of them */
41 #define MY_TIMEOUT (30*1000)
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;
53 static void do_destroy(GpgmegtkSigStatus hd)
55 if (!hd->running) {
56 if (hd->mainwindow) {
57 gtk_widget_destroy ( hd->mainwindow );
58 hd->mainwindow = NULL;
60 if (hd->timeout_id_valid) {
61 gtk_timeout_remove(hd->timeout_id);
62 hd->timeout_id_valid = 0;
64 if (hd->destroy_pending)
65 g_free(hd);
69 static void okay_cb(GtkWidget *widget, gpointer data)
71 GpgmegtkSigStatus hd = data;
73 hd->running = 0;
74 do_destroy(hd);
77 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
79 GpgmegtkSigStatus hd = data;
81 hd->running = 0;
82 do_destroy(hd);
84 return TRUE;
87 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
89 GpgmegtkSigStatus hd = data;
91 if (event && event->keyval == GDK_Escape) {
92 hd->running = 0;
93 do_destroy(hd);
97 GpgmegtkSigStatus gpgmegtk_sig_status_create(void)
99 GtkWidget *window;
100 GtkWidget *vbox;
101 GtkWidget *hbox;
102 GtkWidget *label;
103 GtkWidget *okay_btn;
104 GtkWidget *okay_area;
105 GpgmegtkSigStatus hd;
107 hd = g_malloc0(sizeof *hd);
108 hd->running = 1;
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, FALSE, 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);
121 vbox = gtk_vbox_new(FALSE, 8);
122 gtk_container_add(GTK_CONTAINER(window), vbox);
123 gtk_widget_show(vbox);
125 hbox = gtk_hbox_new(FALSE, 0);
126 gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 8);
127 gtk_widget_show(hbox);
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);
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);
141 gtk_widget_show_all(window);
143 while (gtk_events_pending())
144 gtk_main_iteration();
146 return hd;
149 static gint timeout_cb(gpointer data)
151 GpgmegtkSigStatus hd = data;
153 hd->running = 0;
154 hd->timeout_id_valid = 0;
155 do_destroy(hd);
157 return FALSE;
160 void gpgmegtk_sig_status_destroy(GpgmegtkSigStatus hd)
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;
169 do_destroy(hd);
173 /* Fixme: remove status and get it from the context */
174 void gpgmegtk_sig_status_update(GpgmegtkSigStatus hd, GpgmeCtx ctx)
176 gint idx;
177 time_t created;
178 GpgmeSigStat status;
179 gchar *text = NULL;
181 if (!hd || !hd->running || !ctx)
182 return;
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;
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 = "[?]";
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);
206 gtk_label_set_text(GTK_LABEL(hd->label), text );
207 g_free (text);
209 while (gtk_events_pending())
210 gtk_main_iteration();
213 const char *gpgmegtk_sig_status_to_string(GpgmeSigStat status)
215 const char *result = "?";
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_BAD:
228 result = _("BAD signature");
229 break;
230 case GPGME_SIG_STAT_NOKEY:
231 result = _("No public key to verify the signature");
232 break;
233 case GPGME_SIG_STAT_ERROR:
234 result = _("Error verifying the signature");
235 break;
236 default:
237 break;
240 return result;
243 #endif /* USE_GPGME */