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