Fix CID 1596595: Resource leak
[claws.git] / src / password_gtk.c
blob0170187bc462377812c7115a266f464ff0c4d8c4
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 2016-2021 the Claws Mail team and Andrej Kacian
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/>.
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #include "claws-features.h"
22 #endif
24 #ifndef PASSWORD_CRYPTO_OLD
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gtk/gtk.h>
30 #include "common/utils.h"
31 #include "gtk/manage_window.h"
32 #include "gtk/gtkutils.h"
33 #include "account.h"
34 #include "alertpanel.h"
35 #include "password.h"
36 #include "prefs_common.h"
38 static void entry_activated(GtkEntry *entry, gpointer user_data)
40 const gchar *text = gtk_entry_get_text(entry);
42 if (strlen(text) > 0)
43 gtk_widget_grab_focus(GTK_WIDGET(user_data));
46 struct _ctx {
47 gboolean done;
48 GtkWidget *dialog;
49 GtkWidget *entry_old;
50 GtkWidget *entry_new1;
51 GtkWidget *entry_new2;
54 static void ok_button_clicked(GtkButton *button, gpointer user_data)
56 struct _ctx *ctx = (struct _ctx *)user_data;
57 const gchar *old = NULL;
58 const gchar *new1 = gtk_entry_get_text(GTK_ENTRY(ctx->entry_new1));
59 const gchar *new2 = gtk_entry_get_text(GTK_ENTRY(ctx->entry_new2));
61 debug_print("OK button activated\n");
63 /* Now we check the new passphrase - same in both entries. */
64 if (strcmp(new1, new2)) {
65 debug_print("passphrases do not match\n");
66 alertpanel_warning(_("New passphrases do not match, try again."));
67 gtk_entry_set_text(GTK_ENTRY(ctx->entry_new1), "");
68 gtk_entry_set_text(GTK_ENTRY(ctx->entry_new2), "");
69 gtk_widget_grab_focus(ctx->entry_new1);
70 return;
73 /* If there is an existing primary passphrase, check for its correctness
74 * in entry_old. */
75 if (primary_passphrase_is_set()
76 && ((old = gtk_entry_get_text(GTK_ENTRY(ctx->entry_old))) == NULL
77 || strlen(old) == 0 || !primary_passphrase_is_correct(old))) {
78 debug_print("old passphrase incorrect\n");
79 alertpanel_warning(_("Incorrect old primary passphrase entered, try again."));
80 gtk_entry_set_text(GTK_ENTRY(ctx->entry_old), "");
81 gtk_widget_grab_focus(ctx->entry_old);
82 return;
85 primary_passphrase_change(old, new1);
87 ctx->done = TRUE;
88 gtk_widget_destroy(ctx->dialog);
89 ctx->dialog = NULL;
92 static void cancel_button_clicked(GtkButton *button, gpointer user_data)
94 struct _ctx *ctx = (struct _ctx *)user_data;
95 ctx->done = TRUE;
96 gtk_widget_destroy(ctx->dialog);
97 ctx->dialog = NULL;
100 static void dialog_destroy(GtkWidget *widget, gpointer user_data)
102 struct _ctx *ctx = (struct _ctx *)user_data;
103 ctx->done = TRUE;
104 ctx->dialog = NULL;
107 void primary_passphrase_change_dialog()
109 static PangoFontDescription *font_desc;
110 GtkWidget *dialog;
111 GtkWidget *vbox, *hbox;
112 GtkWidget *icon, *table, *label, *sep;
113 GtkWidget *msg_title;
114 GtkWidget *entry_old, *entry_new1, *entry_new2;
115 GtkWidget *ok_button, *cancel_button;
116 struct _ctx *ctx;
118 dialog = gtk_dialog_new();
120 gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
121 gtk_window_set_default_size(GTK_WINDOW(dialog), 375, 100);
122 gtk_window_set_title(GTK_WINDOW(dialog), "");
124 MANAGE_WINDOW_SIGNALS_CONNECT(dialog);
126 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
127 gtk_box_set_spacing(GTK_BOX(vbox), 14);
128 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 12);
129 gtk_container_set_border_width(GTK_CONTAINER(hbox), 5);
130 gtk_widget_show(hbox);
131 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
133 icon = gtk_image_new_from_icon_name("dialog-password",
134 GTK_ICON_SIZE_DIALOG);
135 gtk_widget_set_halign(icon, GTK_ALIGN_CENTER);
136 gtk_widget_set_valign(icon, GTK_ALIGN_START);
137 gtk_box_pack_start(GTK_BOX(hbox), icon, FALSE, FALSE, 0);
139 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 12);
140 gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
141 gtk_widget_show(vbox);
143 msg_title = gtk_label_new(_("Changing primary passphrase"));
144 gtk_label_set_xalign(GTK_LABEL(msg_title), 0.0);
145 gtk_label_set_justify(GTK_LABEL(msg_title), GTK_JUSTIFY_LEFT);
146 gtk_label_set_use_markup (GTK_LABEL (msg_title), TRUE);
147 gtk_box_pack_start(GTK_BOX(vbox), msg_title, FALSE, FALSE, 0);
148 gtk_label_set_line_wrap(GTK_LABEL(msg_title), TRUE);
149 if (!font_desc) {
150 gint size;
152 size = pango_font_description_get_size
153 (gtk_widget_get_style(msg_title)->font_desc);
154 font_desc = pango_font_description_new();
155 pango_font_description_set_weight
156 (font_desc, PANGO_WEIGHT_BOLD);
157 pango_font_description_set_size
158 (font_desc, size * PANGO_SCALE_LARGE);
160 if (font_desc)
161 gtk_widget_override_font(msg_title, font_desc);
163 label = gtk_label_new(
164 _("If a primary passphrase is currently active, it\n"
165 "needs to be entered.")
167 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
168 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
169 gtk_widget_show(label);
171 table = gtk_grid_new();
172 gtk_grid_set_row_spacing(GTK_GRID(table), 8);
173 gtk_grid_set_column_spacing(GTK_GRID(table), 8);
175 /* Old passphrase */
176 label = gtk_label_new(_("Old passphrase"));
177 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
178 gtk_grid_attach(GTK_GRID(table), label, 0, 0, 1, 1);
180 entry_old = gtk_entry_new();
181 gtk_entry_set_visibility(GTK_ENTRY(entry_old), FALSE);
182 gtk_grid_attach(GTK_GRID(table), entry_old, 1, 0, 1, 1);
183 gtk_widget_set_hexpand(entry_old, TRUE);
184 gtk_widget_set_halign(entry_old, GTK_ALIGN_FILL);
186 /* Separator */
187 sep = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
188 g_object_set (sep, "margin", 8, NULL);
189 gtk_grid_attach(GTK_GRID(table), sep, 0, 1, 2, 1);
191 /* New passphrase */
192 label = gtk_label_new(_("New passphrase"));
193 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
194 gtk_grid_attach(GTK_GRID(table), label, 0, 2, 1, 1);
196 entry_new1 = gtk_entry_new();
197 gtk_entry_set_visibility(GTK_ENTRY(entry_new1), FALSE);
198 gtk_grid_attach(GTK_GRID(table), entry_new1, 1, 2, 1, 1);
199 gtk_widget_set_hexpand(entry_new1, TRUE);
200 gtk_widget_set_halign(entry_new1, GTK_ALIGN_FILL);
202 /* New passphrase again */
203 label = gtk_label_new(_("Confirm passphrase"));
204 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
205 gtk_grid_attach(GTK_GRID(table), label, 0, 3, 1, 1);
207 entry_new2 = gtk_entry_new();
208 gtk_entry_set_visibility(GTK_ENTRY(entry_new2), FALSE);
209 gtk_grid_attach(GTK_GRID(table), entry_new2, 1, 3, 1, 1);
210 gtk_widget_set_hexpand(entry_new2, TRUE);
211 gtk_widget_set_halign(entry_new2, GTK_ALIGN_FILL);
213 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
215 cancel_button = gtk_dialog_add_button(GTK_DIALOG(dialog), _("_Cancel"),
216 GTK_RESPONSE_NONE);
217 ok_button = gtk_dialog_add_button(GTK_DIALOG(dialog),_("_OK"),
218 GTK_RESPONSE_NONE);
219 gtk_widget_grab_default(ok_button);
221 /* If no primary passphrase is set, disable the "old passphrase" entry */
222 if (!primary_passphrase_is_set())
223 gtk_widget_set_sensitive(entry_old, FALSE);
225 g_signal_connect(G_OBJECT(entry_old), "activate",
226 G_CALLBACK(entry_activated), entry_new1);
227 g_signal_connect(G_OBJECT(entry_new1), "activate",
228 G_CALLBACK(entry_activated), entry_new2);
229 gtk_entry_set_activates_default(GTK_ENTRY(entry_new2), TRUE);
231 ctx = g_new(struct _ctx, 1);
232 ctx->done = FALSE;
233 ctx->dialog = dialog;
234 ctx->entry_old = entry_old;
235 ctx->entry_new1 = entry_new1;
236 ctx->entry_new2 = entry_new2;
238 g_signal_connect(G_OBJECT(ok_button), "clicked",
239 G_CALLBACK(ok_button_clicked), ctx);
240 g_signal_connect(G_OBJECT(cancel_button), "clicked",
241 G_CALLBACK(cancel_button_clicked), ctx);
243 g_signal_connect(G_OBJECT(dialog), "destroy",
244 G_CALLBACK(dialog_destroy), ctx);
246 gtk_widget_show_all(gtk_dialog_get_content_area(GTK_DIALOG(dialog)));
247 gtk_window_present(GTK_WINDOW(dialog));
249 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
250 manage_window_set_transient(GTK_WINDOW(dialog));
252 while (!ctx->done)
253 gtk_main_iteration();
255 if (ctx->dialog != NULL)
256 gtk_widget_destroy(ctx->dialog);
258 GTK_EVENTS_FLUSH();
260 g_free(ctx);
263 #endif /* !PASSWORD_CRYPTO_OLD */