update team list
[claws.git] / src / password_gtk.c
blob2764fbe4468b2a9a2dc749364e984a24f88836e2
1 /*
2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2016 The Claws Mail Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #include "claws-features.h"
23 #endif
25 #ifndef PASSWORD_CRYPTO_OLD
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
31 #include "common/utils.h"
32 #include "gtk/manage_window.h"
33 #include "gtk/gtkutils.h"
34 #include "account.h"
35 #include "alertpanel.h"
36 #include "password.h"
37 #include "prefs_common.h"
39 static void entry_activated(GtkEntry *entry, gpointer user_data)
41 const gchar *text = gtk_entry_get_text(entry);
43 if (strlen(text) > 0)
44 gtk_widget_grab_focus(GTK_WIDGET(user_data));
47 struct _ctx {
48 gboolean done;
49 GtkWidget *dialog;
50 GtkWidget *entry_old;
51 GtkWidget *entry_new1;
52 GtkWidget *entry_new2;
55 static void ok_button_clicked(GtkButton *button, gpointer user_data)
57 struct _ctx *ctx = (struct _ctx *)user_data;
58 const gchar *old = NULL;
59 const gchar *new1 = gtk_entry_get_text(GTK_ENTRY(ctx->entry_new1));
60 const gchar *new2 = gtk_entry_get_text(GTK_ENTRY(ctx->entry_new2));
62 debug_print("OK button activated\n");
64 /* Now we check the new passphrase - same in both entries. */
65 if (strcmp(new1, new2)) {
66 debug_print("passphrases do not match\n");
67 alertpanel_warning(_("New passphrases do not match, try again."));
68 gtk_entry_set_text(GTK_ENTRY(ctx->entry_new1), "");
69 gtk_entry_set_text(GTK_ENTRY(ctx->entry_new2), "");
70 gtk_widget_grab_focus(ctx->entry_new1);
71 return;
74 /* If there is an existing master passphrase, check for its correctness
75 * in entry_old. */
76 if (master_passphrase_is_set()
77 && ((old = gtk_entry_get_text(GTK_ENTRY(ctx->entry_old))) == NULL
78 || strlen(old) == 0 || !master_passphrase_is_correct(old))) {
79 debug_print("old passphrase incorrect\n");
80 alertpanel_warning(_("Incorrect old master passphrase entered, try again."));
81 gtk_entry_set_text(GTK_ENTRY(ctx->entry_old), "");
82 gtk_widget_grab_focus(ctx->entry_old);
83 return;
86 master_passphrase_change(old, new1);
88 ctx->done = TRUE;
89 gtk_widget_destroy(ctx->dialog);
90 ctx->dialog = NULL;
93 static void cancel_button_clicked(GtkButton *button, gpointer user_data)
95 struct _ctx *ctx = (struct _ctx *)user_data;
96 ctx->done = TRUE;
97 gtk_widget_destroy(ctx->dialog);
98 ctx->dialog = NULL;
101 static void dialog_destroy(GtkWidget *widget, gpointer user_data)
103 struct _ctx *ctx = (struct _ctx *)user_data;
104 ctx->done = TRUE;
105 ctx->dialog = NULL;
108 void master_passphrase_change_dialog()
110 static PangoFontDescription *font_desc;
111 GtkWidget *dialog;
112 GtkWidget *vbox, *hbox;
113 GtkWidget *icon, *table, *label;
114 GtkWidget *msg_title;
115 GtkWidget *entry_old, *entry_new1, *entry_new2;
116 GtkWidget *confirm_area;
117 GtkWidget *ok_button, *cancel_button;
118 struct _ctx *ctx;
120 dialog = gtk_dialog_new();
122 gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
123 gtk_window_set_default_size(GTK_WINDOW(dialog), 375, 100);
124 gtk_window_set_title(GTK_WINDOW(dialog), "");
126 MANAGE_WINDOW_SIGNALS_CONNECT(dialog);
128 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
129 gtk_box_set_spacing(GTK_BOX(vbox), 14);
130 hbox = gtk_hbox_new(FALSE, 12);
131 gtk_container_set_border_width(GTK_CONTAINER(hbox), 5);
132 gtk_widget_show(hbox);
133 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
135 icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_AUTHENTICATION,
136 GTK_ICON_SIZE_DIALOG);
137 gtk_misc_set_alignment(GTK_MISC(icon), 0.5, 0.0);
138 gtk_box_pack_start(GTK_BOX(hbox), icon, FALSE, FALSE, 0);
140 vbox = gtk_vbox_new(FALSE, 12);
141 gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
142 gtk_widget_show(vbox);
144 msg_title = gtk_label_new(_("Changing master passphrase"));
145 gtk_misc_set_alignment(GTK_MISC(msg_title), 0, 0.5);
146 gtk_label_set_justify(GTK_LABEL(msg_title), GTK_JUSTIFY_LEFT);
147 gtk_label_set_use_markup (GTK_LABEL (msg_title), TRUE);
148 gtk_box_pack_start(GTK_BOX(vbox), msg_title, FALSE, FALSE, 0);
149 gtk_label_set_line_wrap(GTK_LABEL(msg_title), TRUE);
150 if (!font_desc) {
151 gint size;
153 size = pango_font_description_get_size
154 (gtk_widget_get_style(msg_title)->font_desc);
155 font_desc = pango_font_description_new();
156 pango_font_description_set_weight
157 (font_desc, PANGO_WEIGHT_BOLD);
158 pango_font_description_set_size
159 (font_desc, size * PANGO_SCALE_LARGE);
161 if (font_desc)
162 gtk_widget_modify_font(msg_title, font_desc);
164 label = gtk_label_new(
165 _("If a master passphrase is currently active, it\n"
166 "needs to be entered.")
168 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
169 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
170 gtk_widget_show(label);
172 table = gtk_table_new(4, 2, FALSE);
174 /* Old passphrase */
175 label = gtk_label_new(_("Old passphrase:"));
176 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
177 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1,
178 GTK_EXPAND | GTK_FILL, 0, 0, 0);
180 entry_old = gtk_entry_new();
181 gtk_entry_set_visibility(GTK_ENTRY(entry_old), FALSE);
182 gtk_table_attach(GTK_TABLE(table), entry_old, 1, 2, 0, 1,
183 GTK_FILL | GTK_EXPAND, 0, 0, 0);
185 /* Separator */
186 gtk_table_attach(GTK_TABLE(table),
187 gtk_hseparator_new(), 0, 2, 1, 2,
188 GTK_FILL | GTK_EXPAND, 0, 0, 5);
190 /* New passphrase */
191 label = gtk_label_new(_("New passphrase:"));
192 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
193 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 2, 3,
194 GTK_EXPAND | GTK_FILL, 0, 0, 0);
196 entry_new1 = gtk_entry_new();
197 gtk_entry_set_visibility(GTK_ENTRY(entry_new1), FALSE);
198 gtk_table_attach(GTK_TABLE(table), entry_new1, 1, 2, 2, 3,
199 GTK_FILL | GTK_EXPAND, 0, 0, 0);
201 /* New passphrase again */
202 label = gtk_label_new(_("Confirm passphrase:"));
203 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
204 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 3, 4,
205 GTK_EXPAND | GTK_FILL, 0, 0, 0);
207 entry_new2 = gtk_entry_new();
208 gtk_entry_set_visibility(GTK_ENTRY(entry_new2), FALSE);
209 gtk_table_attach(GTK_TABLE(table), entry_new2, 1, 2, 3, 4,
210 GTK_FILL | GTK_EXPAND, 0, 0, 0);
212 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
214 /* Dialog buttons */
215 gtkut_stock_button_set_create(&confirm_area,
216 &cancel_button, GTK_STOCK_CANCEL,
217 &ok_button, GTK_STOCK_OK,
218 NULL, NULL);
220 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(dialog))),
221 confirm_area, FALSE, FALSE, 0);
222 gtk_container_set_border_width(GTK_CONTAINER(confirm_area), 5);
224 gtk_widget_grab_default(ok_button);
226 /* If no master passphrase is set, disable the "old passphrase" entry */
227 if (!master_passphrase_is_set())
228 gtk_widget_set_sensitive(entry_old, FALSE);
230 g_signal_connect(G_OBJECT(entry_old), "activate",
231 G_CALLBACK(entry_activated), entry_new1);
232 g_signal_connect(G_OBJECT(entry_new1), "activate",
233 G_CALLBACK(entry_activated), entry_new2);
234 gtk_entry_set_activates_default(GTK_ENTRY(entry_new2), TRUE);
236 ctx = g_new(struct _ctx, 1);
237 ctx->done = FALSE;
238 ctx->dialog = dialog;
239 ctx->entry_old = entry_old;
240 ctx->entry_new1 = entry_new1;
241 ctx->entry_new2 = entry_new2;
243 g_signal_connect(G_OBJECT(ok_button), "clicked",
244 G_CALLBACK(ok_button_clicked), ctx);
245 g_signal_connect(G_OBJECT(cancel_button), "clicked",
246 G_CALLBACK(cancel_button_clicked), ctx);
248 g_signal_connect(G_OBJECT(dialog), "destroy",
249 G_CALLBACK(dialog_destroy), ctx);
251 gtk_widget_show_all(gtk_dialog_get_content_area(GTK_DIALOG(dialog)));
252 gtk_window_present(GTK_WINDOW(dialog));
254 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
255 manage_window_set_transient(GTK_WINDOW(dialog));
257 while (!ctx->done)
258 gtk_main_iteration();
260 if (ctx->dialog != NULL)
261 gtk_widget_destroy(ctx->dialog);
263 GTK_EVENTS_FLUSH();
265 g_free(ctx);
268 #endif /* !PASSWORD_CRYPTO_OLD */