filtering dialog box and some changes
[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
44 struct gpgmegtk_sig_status_s {
45         GtkWidget *mainwindow;
46         GtkWidget *label;
47         int running;
48         int destroy_pending;
49         guint timeout_id;
50         int timeout_id_valid;
51 };
52
53
54 static void do_destroy(GpgmegtkSigStatus hd)
55 {
56         if (!hd->running ) {
57                 if (hd->mainwindow) {
58                         gtk_widget_destroy ( hd->mainwindow );
59                         hd->mainwindow = NULL;
60                 }
61                 if (hd->timeout_id_valid) {
62                         gtk_timeout_remove(hd->timeout_id);
63                         hd->timeout_id_valid = 0;
64                 }
65                 if(hd->destroy_pending) 
66                         g_free(hd);
67         }
68 }
69
70
71 static void okay_cb(GtkWidget *widget, gpointer data)
72 {
73         GpgmegtkSigStatus hd = data;
74
75         hd->running = 0;
76         do_destroy(hd);
77 }
78
79
80 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
81 {
82         GpgmegtkSigStatus hd = data;
83
84         hd->running = 0;
85         do_destroy(hd);
86
87         return TRUE;
88 }
89
90
91 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
92 {
93         GpgmegtkSigStatus hd = data;
94
95         if (event && event->keyval == GDK_Escape) {
96                 hd->running = 0;
97                 do_destroy(hd);
98         }
99 }
100
101
102 GpgmegtkSigStatus gpgmegtk_sig_status_create()
103 {
104         GtkWidget *window;
105         GtkWidget *vbox;
106         GtkWidget *hbox;
107         GtkWidget *label;
108         GtkWidget *okay_btn;
109         GtkWidget *okay_area;
110         GpgmegtkSigStatus hd;
111
112         hd = g_malloc0(sizeof *hd);
113         hd->running = 1;
114
115         window = gtk_window_new(GTK_WINDOW_DIALOG);
116         hd->mainwindow = window;
117         gtk_widget_set_usize(window, 400, -1);
118         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
119         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
120         gtk_window_set_policy(GTK_WINDOW(window), FALSE, FALSE, FALSE);
121         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
122                            GTK_SIGNAL_FUNC(delete_event), hd);
123         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
124                            GTK_SIGNAL_FUNC(key_pressed), hd);
125
126         vbox = gtk_vbox_new(FALSE, 8);
127         gtk_container_add(GTK_CONTAINER(window), vbox);
128         gtk_widget_show(vbox);
129
130         hbox = gtk_hbox_new(FALSE, 0);
131         gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 8);
132         gtk_widget_show(hbox);
133
134         label = gtk_label_new(_("Checking signature"));
135         hd->label = label;
136         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 8);
137         gtk_widget_show(label);
138
139         gtkut_button_set_create(&okay_area, &okay_btn, _("Okay"),
140                                 NULL, NULL, NULL, NULL);
141         gtk_box_pack_end(GTK_BOX(vbox), okay_area, FALSE, FALSE, 0);
142         gtk_widget_grab_default(okay_btn);
143         gtk_signal_connect(GTK_OBJECT(okay_btn), "clicked",
144                            GTK_SIGNAL_FUNC(okay_cb), hd);
145
146         gtk_widget_show_all(window);
147
148         while (gtk_events_pending())
149             gtk_main_iteration();
150
151         return hd;
152 }
153
154 static gint timeout_cb(gpointer data)
155 {
156     GpgmegtkSigStatus hd = data;
157
158     hd->running = 0;
159     hd->timeout_id_valid = 0;
160     do_destroy(hd);
161     return FALSE;
162 }
163
164 void gpgmegtk_sig_status_destroy(GpgmegtkSigStatus hd)
165 {
166         if( hd ) {
167                 hd->destroy_pending = 1;
168                 if (hd->running && !hd->timeout_id_valid) {
169                     hd->timeout_id = gtk_timeout_add(MY_TIMEOUT,
170                                                      timeout_cb, hd);
171                     hd->timeout_id_valid = 1;
172                 }
173                 do_destroy(hd);
174         }
175 }
176
177
178 /* Fixme: remove status and get it from the context */
179 void gpgmegtk_sig_status_update(GpgmegtkSigStatus hd, GpgmeCtx ctx)
180 {
181     int idx;
182     time_t created;
183     GpgmeSigStat status;
184     char *text = NULL;
185
186     if (!hd || !hd->running || !ctx)
187         return;
188
189     for (idx=0; gpgme_get_sig_status(ctx, idx, &status, &created); idx++ ) {
190         char *tmp;
191         const char *userid;
192         GpgmeKey key = NULL;
193
194         if ( !gpgme_get_sig_key (ctx, idx, &key) ) {
195             userid = gpgme_key_get_string_attr (key, GPGME_ATTR_USERID,
196                                                 NULL, 0);
197         }
198         else
199             userid = "[?]";
200
201         tmp = g_strdup_printf ( "%s%s%s from \"%s\"",
202                                 text? text:"",
203                                 text? "\n":"",
204                                 gpgmegtk_sig_status_to_string(status),
205                                 userid );
206         g_free (text);
207         text = tmp;
208         gpgme_key_unref (key);
209     }
210
211     gtk_label_set_text(GTK_LABEL(hd->label), text ); 
212     g_free (text);
213
214     while (gtk_events_pending())
215         gtk_main_iteration();
216 }
217
218
219 const char *gpgmegtk_sig_status_to_string(GpgmeSigStat status)
220 {
221         const char *result = "?";
222
223         switch ( status ) {
224             case GPGME_SIG_STAT_NONE:
225                 result = _("Oops: Signature not verified");
226                 break;
227             case GPGME_SIG_STAT_NOSIG:
228                 result = _("No signature found");
229                 break;
230             case GPGME_SIG_STAT_GOOD:
231                 result = _("Good signature");
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         }
243
244         return result;
245 }
246
247 #endif /* USE_GPGME */