claws-mail/src/sigstatus.c

250 lines
5.8 KiB
C
Raw Normal View History

2001-04-27 22:27:24 +02:00
/* sigstatus.h - GTK+ based signature status display
* Copyright (C) 2001 Werner Koch (dd9jn)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#if USE_GPGME
#include <glib.h>
#include <gtk/gtkwindow.h>
#include <gtk/gtkvbox.h>
#include <gtk/gtkhbox.h>
#include <gtk/gtklabel.h>
#include <gtk/gtkbutton.h>
#include <gdk/gdkkeysyms.h>
#include <gpgme.h>
#include "intl.h"
#include "gtkutils.h"
#include "utils.h"
#include "sigstatus.h"
/* remove the window after 30 seconds to avoid cluttering the deskop
* with too many of them */
#define MY_TIMEOUT (30*1000)
struct gpgmegtk_sig_status_s {
2001-05-28 20:38:15 +02:00
GtkWidget *mainwindow;
GtkWidget *label;
gint running;
gint destroy_pending;
guint timeout_id;
gint timeout_id_valid;
2001-04-27 22:27:24 +02:00
};
static void do_destroy(GpgmegtkSigStatus hd)
{
2001-05-28 20:38:15 +02:00
if (!hd->running) {
if (hd->mainwindow) {
gtk_widget_destroy ( hd->mainwindow );
hd->mainwindow = NULL;
}
if (hd->timeout_id_valid) {
gtk_timeout_remove(hd->timeout_id);
hd->timeout_id_valid = 0;
}
2002-08-06 11:34:13 +02:00
if (hd->destroy_pending)
2001-05-28 20:38:15 +02:00
g_free(hd);
}
2001-04-27 22:27:24 +02:00
}
static void okay_cb(GtkWidget *widget, gpointer data)
{
2002-08-06 11:34:13 +02:00
GpgmegtkSigStatus hd = data;
2001-04-27 22:27:24 +02:00
2002-08-06 11:34:13 +02:00
hd->running = 0;
do_destroy(hd);
2001-04-27 22:27:24 +02:00
}
2001-05-08 00:13:03 +02:00
static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
{
2002-08-06 11:34:13 +02:00
GpgmegtkSigStatus hd = data;
2001-05-08 00:13:03 +02:00
2002-08-06 11:34:13 +02:00
hd->running = 0;
do_destroy(hd);
2001-05-08 00:13:03 +02:00
return TRUE;
}
2001-04-27 22:27:24 +02:00
static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
{
2002-08-06 11:34:13 +02:00
GpgmegtkSigStatus hd = data;
2001-04-27 22:27:24 +02:00
2002-08-06 11:34:13 +02:00
if (event && event->keyval == GDK_Escape) {
hd->running = 0;
do_destroy(hd);
}
2001-04-27 22:27:24 +02:00
}
2001-05-28 20:38:15 +02:00
GpgmegtkSigStatus gpgmegtk_sig_status_create(void)
2001-04-27 22:27:24 +02:00
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *hbox;
GtkWidget *label;
GtkWidget *okay_btn;
GtkWidget *okay_area;
2002-08-06 11:34:13 +02:00
GpgmegtkSigStatus hd;
2001-04-27 22:27:24 +02:00
2002-08-06 11:34:13 +02:00
hd = g_malloc0(sizeof *hd);
hd->running = 1;
2001-04-27 22:27:24 +02:00
window = gtk_window_new(GTK_WINDOW_DIALOG);
2002-08-06 11:34:13 +02:00
hd->mainwindow = window;
2001-04-27 22:27:24 +02:00
gtk_widget_set_usize(window, 400, -1);
gtk_container_set_border_width(GTK_CONTAINER(window), 8);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
2002-08-06 11:34:13 +02:00
gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, FALSE);
gtk_signal_connect(GTK_OBJECT(window), "delete_event",
GTK_SIGNAL_FUNC(delete_event), hd);
gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
GTK_SIGNAL_FUNC(key_pressed), hd);
2001-04-27 22:27:24 +02:00
vbox = gtk_vbox_new(FALSE, 8);
gtk_container_add(GTK_CONTAINER(window), vbox);
gtk_widget_show(vbox);
hbox = gtk_hbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 8);
gtk_widget_show(hbox);
label = gtk_label_new(_("Checking signature"));
2002-08-06 11:34:13 +02:00
hd->label = label;
2001-04-27 22:27:24 +02:00
gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 8);
gtk_widget_show(label);
2001-05-28 20:38:15 +02:00
gtkut_button_set_create(&okay_area, &okay_btn, _("OK"),
2001-04-27 22:27:24 +02:00
NULL, NULL, NULL, NULL);
gtk_box_pack_end(GTK_BOX(vbox), okay_area, FALSE, FALSE, 0);
gtk_widget_grab_default(okay_btn);
2002-08-06 11:34:13 +02:00
gtk_signal_connect(GTK_OBJECT(okay_btn), "clicked",
GTK_SIGNAL_FUNC(okay_cb), hd);
2001-04-27 22:27:24 +02:00
gtk_widget_show_all(window);
2002-08-06 11:34:13 +02:00
while (gtk_events_pending())
gtk_main_iteration();
2001-04-27 22:27:24 +02:00
2002-08-06 11:34:13 +02:00
return hd;
2001-04-27 22:27:24 +02:00
}
static gint timeout_cb(gpointer data)
{
2002-08-06 11:34:13 +02:00
GpgmegtkSigStatus hd = data;
2001-04-27 22:27:24 +02:00
2002-08-06 11:34:13 +02:00
hd->running = 0;
hd->timeout_id_valid = 0;
do_destroy(hd);
2001-05-28 20:38:15 +02:00
2002-08-06 11:34:13 +02:00
return FALSE;
2001-04-27 22:27:24 +02:00
}
void gpgmegtk_sig_status_destroy(GpgmegtkSigStatus hd)
{
2002-08-06 11:34:13 +02:00
if (hd) {
hd->destroy_pending = 1;
if (hd->running && !hd->timeout_id_valid) {
hd->timeout_id = gtk_timeout_add(MY_TIMEOUT,
timeout_cb, hd);
hd->timeout_id_valid = 1;
}
do_destroy(hd);
}
2001-04-27 22:27:24 +02:00
}
/* Fixme: remove status and get it from the context */
void gpgmegtk_sig_status_update(GpgmegtkSigStatus hd, GpgmeCtx ctx)
{
2002-08-06 11:34:13 +02:00
gint idx;
time_t created;
GpgmeSigStat status;
gchar *text = NULL;
2001-04-27 22:27:24 +02:00
2002-08-06 11:34:13 +02:00
if (!hd || !hd->running || !ctx)
return;
2001-04-27 22:27:24 +02:00
2001-05-28 20:38:15 +02:00
for (idx = 0; gpgme_get_sig_status(ctx, idx, &status, &created);
idx++) {
2002-08-06 11:34:13 +02:00
gchar *tmp;
const gchar *userid;
GpgmeKey key = NULL;
2001-04-27 22:27:24 +02:00
2002-08-06 11:34:13 +02:00
if (!gpgme_get_sig_key (ctx, idx, &key)) {
2001-05-28 20:38:15 +02:00
userid = gpgme_key_get_string_attr
(key, GPGME_ATTR_USERID, NULL, 0);
2002-08-06 11:34:13 +02:00
} else
userid = "[?]";
tmp = g_strdup_printf(_("%s%s%s from \"%s\""),
text ? text : "",
text ? "\n" : "",
gpgmegtk_sig_status_to_string(status),
userid);
g_free (text);
text = tmp;
gpgme_key_unref (key);
}
gtk_label_set_text(GTK_LABEL(hd->label), text);
g_free(text);
while (gtk_events_pending())
gtk_main_iteration();
2001-04-27 22:27:24 +02:00
}
const char *gpgmegtk_sig_status_to_string(GpgmeSigStat status)
{
2002-08-06 11:34:13 +02:00
const char *result = "?";
switch (status) {
case GPGME_SIG_STAT_NONE:
result = _("Oops: Signature not verified");
break;
case GPGME_SIG_STAT_NOSIG:
result = _("No signature found");
break;
case GPGME_SIG_STAT_GOOD:
result = _("Good signature");
break;
case GPGME_SIG_STAT_GOOD_EXP:
result = _("Good signature but it has expired");
break;
case GPGME_SIG_STAT_GOOD_EXPKEY:
result = _("Good signature but the key has expired");
break;
2002-08-06 11:34:13 +02:00
case GPGME_SIG_STAT_BAD:
result = _("BAD signature");
break;
case GPGME_SIG_STAT_NOKEY:
result = _("No public key to verify the signature");
break;
case GPGME_SIG_STAT_ERROR:
result = _("Error verifying the signature");
break;
2001-05-28 20:38:15 +02:00
default:
break;
2002-08-06 11:34:13 +02:00
}
2001-04-27 22:27:24 +02:00
2002-08-06 11:34:13 +02:00
return result;
2001-04-27 22:27:24 +02:00
}
#endif /* USE_GPGME */