sync with sylpheed 0.4.66cvs6
[claws.git] / src / passphrase.c
1 /* passphrase.c - GTK+ based passphrase callback
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 #include <string.h>
24 #include <glib.h>
25 #include <gdk/gdkkeysyms.h>
26 #include <gdk/gdkx.h>  /* GDK_DISPLAY() */
27 #include <gtk/gtkmain.h>
28 #include <gtk/gtkwidget.h>
29 #include <gtk/gtkwindow.h>
30 #include <gtk/gtkvbox.h>
31 #include <gtk/gtktable.h>
32 #include <gtk/gtklabel.h>
33 #include <gtk/gtkentry.h>
34 #include <gtk/gtkhbbox.h>
35 #include <gtk/gtkbutton.h>
36 #include <gtk/gtkfilesel.h>
37 #include <gtk/gtksignal.h>
38
39 #include "intl.h"
40 #include "passphrase.h"
41
42
43 static int grab_all = 0;
44
45 static gboolean pass_ack;
46
47 static void passphrase_ok_cb(GtkWidget *widget, gpointer data);
48 static void passphrase_cancel_cb(GtkWidget *widget, gpointer data);
49 static gint passphrase_deleted(GtkWidget *widget, GdkEventAny *event,
50                                gpointer data);
51 static void passphrase_key_pressed(GtkWidget *widget, GdkEventKey *event,
52                                    gpointer data);
53 static GtkWidget *create_description (const gchar *desc);
54
55 void
56 gpgmegtk_set_passphrase_grab (gint yes)
57 {
58     grab_all = yes;
59 }
60
61 gchar *
62 gpgmegtk_passphrase_mbox (const gchar *desc)
63 {
64     gchar *the_passphrase = NULL;
65     GtkWidget *vbox;
66     GtkWidget *table;
67     GtkWidget *pass_label;
68     GtkWidget *confirm_box;
69     GtkWidget *window;
70     GtkWidget *pass_entry;
71     GtkWidget *ok_button;
72     GtkWidget *cancel_button;
73
74     window = gtk_window_new(GTK_WINDOW_DIALOG);
75     gtk_window_set_title(GTK_WINDOW(window), _("Passphrase"));
76     gtk_widget_set_usize(window, 450, -1);
77     gtk_container_set_border_width(GTK_CONTAINER(window), 4);
78     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
79     gtk_window_set_modal(GTK_WINDOW(window), TRUE);
80     gtk_window_set_policy(GTK_WINDOW(window), FALSE, FALSE, FALSE);
81     gtk_signal_connect(GTK_OBJECT(window), "delete_event",
82                        GTK_SIGNAL_FUNC(passphrase_deleted), NULL);
83     gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
84                        GTK_SIGNAL_FUNC(passphrase_key_pressed), NULL);
85
86     vbox = gtk_vbox_new(FALSE, 8);
87     gtk_container_add(GTK_CONTAINER(window), vbox);
88
89     if (desc) {
90         GtkWidget *label = create_description (desc);
91         gtk_box_pack_start (GTK_BOX(vbox), label, TRUE, TRUE, 0);
92     }
93
94     table = gtk_table_new(2, 2, FALSE);
95     gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
96     gtk_container_set_border_width(GTK_CONTAINER(table), 8);
97     gtk_table_set_row_spacings(GTK_TABLE(table), 12);
98     gtk_table_set_col_spacings(GTK_TABLE(table), 8);
99
100
101     pass_label = gtk_label_new("");
102     gtk_table_attach (GTK_TABLE(table), pass_label, 0, 1, 0, 1,
103                       GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
104     gtk_misc_set_alignment (GTK_MISC (pass_label), 1, 0.5);
105
106     pass_entry = gtk_entry_new();
107     gtk_table_attach (GTK_TABLE(table), pass_entry, 1, 2, 0, 1,
108                       GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
109     gtk_entry_set_visibility (GTK_ENTRY(pass_entry), FALSE);
110     gtk_widget_grab_focus (pass_entry);
111
112
113     confirm_box = gtk_hbutton_box_new ();
114     gtk_button_box_set_layout (GTK_BUTTON_BOX(confirm_box), GTK_BUTTONBOX_END);
115     gtk_button_box_set_spacing (GTK_BUTTON_BOX(confirm_box), 5);
116
117     ok_button = gtk_button_new_with_label (_("OK"));
118     GTK_WIDGET_SET_FLAGS (ok_button, GTK_CAN_DEFAULT);
119     gtk_box_pack_start (GTK_BOX(confirm_box), ok_button, TRUE, TRUE, 0);
120
121     cancel_button = gtk_button_new_with_label (_("Cancel"));
122     GTK_WIDGET_SET_FLAGS (cancel_button, GTK_CAN_DEFAULT);
123     gtk_box_pack_start(GTK_BOX(confirm_box), cancel_button, TRUE, TRUE, 0);
124
125     gtk_box_pack_end(GTK_BOX(vbox), confirm_box, FALSE, FALSE, 0);
126     gtk_widget_grab_default (ok_button);
127
128     gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
129                        GTK_SIGNAL_FUNC(passphrase_ok_cb), NULL);
130     gtk_signal_connect(GTK_OBJECT(pass_entry), "activate",
131                        GTK_SIGNAL_FUNC(passphrase_ok_cb), NULL);
132     gtk_signal_connect(GTK_OBJECT(cancel_button), "clicked",
133                        GTK_SIGNAL_FUNC(passphrase_cancel_cb), NULL);
134
135     if (grab_all)
136         gtk_object_set (GTK_OBJECT(window), "type", GTK_WINDOW_POPUP, NULL);
137     gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
138     if (grab_all)   
139         gtk_window_set_policy (GTK_WINDOW(window), FALSE, FALSE, TRUE);
140     
141     gtk_widget_show_all(window);
142
143     if (grab_all) {
144         XGrabServer(GDK_DISPLAY());
145         if ( gdk_pointer_grab ( window->window, TRUE, 0,
146                                 NULL, NULL, GDK_CURRENT_TIME)) {
147             XUngrabServer ( GDK_DISPLAY() );
148             g_message ("OOPS: Could not grab mouse\n");
149             gtk_widget_destroy (window);
150             return NULL;
151         }
152         if ( gdk_keyboard_grab( window->window, FALSE, GDK_CURRENT_TIME )) {
153             gdk_pointer_ungrab (GDK_CURRENT_TIME);
154             XUngrabServer ( GDK_DISPLAY() );
155             g_message ("OOPS: Could not grab keyboard\n");
156             gtk_widget_destroy (window);
157             return NULL;
158         }
159     }
160
161     gtk_main();
162
163     if (grab_all) {
164         XUngrabServer (GDK_DISPLAY());
165         gdk_pointer_ungrab (GDK_CURRENT_TIME);
166         gdk_keyboard_ungrab (GDK_CURRENT_TIME);
167         gdk_flush();
168     }
169
170     if (pass_ack) {
171         the_passphrase = gtk_entry_get_text(GTK_ENTRY(pass_entry));
172         if (the_passphrase) /* Hmmm: Do we really need this? */
173             the_passphrase = g_strdup (the_passphrase);
174     }
175     gtk_widget_destroy (window);
176
177     return the_passphrase;
178 }
179
180
181 static void 
182 passphrase_ok_cb(GtkWidget *widget, gpointer data)
183 {
184     pass_ack = TRUE;
185     gtk_main_quit();
186 }
187
188 static void 
189 passphrase_cancel_cb(GtkWidget *widget, gpointer data)
190 {
191     pass_ack = FALSE;
192     gtk_main_quit();
193 }
194
195
196 static gint
197 passphrase_deleted(GtkWidget *widget, GdkEventAny *event, gpointer data)
198 {
199     passphrase_cancel_cb(NULL, NULL);
200     return TRUE;
201 }
202
203
204 static void 
205 passphrase_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
206 {
207     if (event && event->keyval == GDK_Escape)
208         passphrase_cancel_cb(NULL, NULL);
209 }
210
211 static gint 
212 linelen (const gchar *s)
213 {
214     gint i;
215
216     for (i = 0; *s && *s != '\n'; s++, i++)
217         ;
218
219     return i;
220 }
221
222 static GtkWidget *
223 create_description (const gchar *desc)
224 {
225     const gchar *cmd=NULL, *uid=NULL, *info=NULL;
226     gchar *buf;
227     GtkWidget *label;
228
229     cmd = desc;
230     uid = strchr (cmd, '\n');
231     if (uid) {
232         info = strchr (++uid, '\n');
233         if (info )
234             info++;
235     }
236
237     if (!uid)
238         uid = _("[no user id]");
239     if (!info)
240         info = "";
241
242     buf = g_strdup_printf (_("%sPlease enter the passphrase for:\n\n"
243                            "  %.*s  \n"
244                            "(%.*s)\n"),
245                            !strncmp (cmd, "TRY_AGAIN", 9 ) ?
246                            _("Bad passphrase! Try again...\n\n") : "",
247                            linelen (uid), uid, linelen (info), info);
248
249     label = gtk_label_new (buf);
250     g_free (buf);
251
252     return label;
253 }