update copyright year
[claws.git] / src / password_gtk.c
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
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #include "claws-features.h"
22 #endif
23
24 #ifndef PASSWORD_CRYPTO_OLD
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gtk/gtk.h>
29
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"
37
38 static void entry_activated(GtkEntry *entry, gpointer user_data)
39 {
40         const gchar *text = gtk_entry_get_text(entry);
41
42         if (strlen(text) > 0)
43                 gtk_widget_grab_focus(GTK_WIDGET(user_data));
44 }
45
46 struct _ctx {
47         gboolean done;
48         GtkWidget *dialog;
49         GtkWidget *entry_old;
50         GtkWidget *entry_new1;
51         GtkWidget *entry_new2;
52 };
53
54 static void ok_button_clicked(GtkButton *button, gpointer user_data)
55 {
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));
60
61         debug_print("OK button activated\n");
62
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;
71         }
72
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;
83         }
84
85         primary_passphrase_change(old, new1);
86
87         ctx->done = TRUE;
88         gtk_widget_destroy(ctx->dialog);
89         ctx->dialog = NULL;
90 }
91
92 static void cancel_button_clicked(GtkButton *button, gpointer user_data)
93 {
94         struct _ctx *ctx = (struct _ctx *)user_data;
95         ctx->done = TRUE;
96         gtk_widget_destroy(ctx->dialog);
97         ctx->dialog = NULL;
98 }
99
100 static void dialog_destroy(GtkWidget *widget, gpointer user_data)
101 {
102         struct _ctx *ctx = (struct _ctx *)user_data;
103         ctx->done = TRUE;
104         ctx->dialog = NULL;
105 }
106
107 void primary_passphrase_change_dialog()
108 {
109         static PangoFontDescription *font_desc;
110         GtkWidget *dialog;
111         GtkWidget *vbox, *hbox;
112         GtkWidget *icon, *table, *label;
113         GtkWidget *msg_title;
114         GtkWidget *entry_old, *entry_new1, *entry_new2;
115         GtkWidget *ok_button, *cancel_button;
116         struct _ctx *ctx;
117
118         dialog = gtk_dialog_new();
119
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), "");
123
124         MANAGE_WINDOW_SIGNALS_CONNECT(dialog);
125
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);
132
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);
138
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);
142
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;
151
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);
159         }
160         if (font_desc)
161                 gtk_widget_override_font(msg_title, font_desc);
162
163         label = gtk_label_new(
164         _("If a primary passphrase is currently active, it\n"
165         "needs to be entered.")
166         );
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);
170
171         table = gtk_grid_new();
172
173         /* Old passphrase */
174         label = gtk_label_new(_("Old passphrase:"));
175         gtk_label_set_xalign(GTK_LABEL(label), 0.0);
176         gtk_grid_attach(GTK_GRID(table), label, 0, 0, 1, 1);
177
178         entry_old = gtk_entry_new();
179         gtk_entry_set_visibility(GTK_ENTRY(entry_old), FALSE);
180         gtk_grid_attach(GTK_GRID(table), entry_old, 1, 0, 1, 1);
181         gtk_widget_set_hexpand(entry_old, TRUE);
182         gtk_widget_set_halign(entry_old, GTK_ALIGN_FILL);
183
184         /* Separator */
185         gtk_grid_attach(GTK_GRID(table), gtk_separator_new(GTK_ORIENTATION_HORIZONTAL), 0, 1, 1, 1);
186
187         /* New passphrase */
188         label = gtk_label_new(_("New passphrase:"));
189         gtk_label_set_xalign(GTK_LABEL(label), 0.0);
190         gtk_grid_attach(GTK_GRID(table), label, 0, 2, 1, 1);
191
192         entry_new1 = gtk_entry_new();
193         gtk_entry_set_visibility(GTK_ENTRY(entry_new1), FALSE);
194         gtk_grid_attach(GTK_GRID(table), entry_new1, 1, 2, 1, 1);
195         gtk_widget_set_hexpand(entry_new1, TRUE);
196         gtk_widget_set_halign(entry_new1, GTK_ALIGN_FILL);
197
198         /* New passphrase again */
199         label = gtk_label_new(_("Confirm passphrase:"));
200         gtk_label_set_xalign(GTK_LABEL(label), 0.0);
201         gtk_grid_attach(GTK_GRID(table), label, 0, 3, 1, 1);
202
203         entry_new2 = gtk_entry_new();
204         gtk_entry_set_visibility(GTK_ENTRY(entry_new2), FALSE);
205         gtk_grid_attach(GTK_GRID(table), entry_new2, 1, 3, 1, 1);
206         gtk_widget_set_hexpand(entry_new2, TRUE);
207         gtk_widget_set_halign(entry_new2, GTK_ALIGN_FILL);
208
209         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
210
211         cancel_button = gtk_dialog_add_button(GTK_DIALOG(dialog), _("_Cancel"),
212                                               GTK_RESPONSE_NONE);
213         ok_button = gtk_dialog_add_button(GTK_DIALOG(dialog),_("_OK"),
214                                           GTK_RESPONSE_NONE);
215         gtk_widget_grab_default(ok_button);
216
217         /* If no primary passphrase is set, disable the "old passphrase" entry */
218         if (!primary_passphrase_is_set())
219                 gtk_widget_set_sensitive(entry_old, FALSE);
220
221         g_signal_connect(G_OBJECT(entry_old), "activate",
222                         G_CALLBACK(entry_activated), entry_new1);
223         g_signal_connect(G_OBJECT(entry_new1), "activate",
224                         G_CALLBACK(entry_activated), entry_new2);
225         gtk_entry_set_activates_default(GTK_ENTRY(entry_new2), TRUE);
226
227         ctx = g_new(struct _ctx, 1);
228         ctx->done = FALSE;
229         ctx->dialog = dialog;
230         ctx->entry_old = entry_old;
231         ctx->entry_new1 = entry_new1;
232         ctx->entry_new2 = entry_new2;
233
234         g_signal_connect(G_OBJECT(ok_button), "clicked",
235                         G_CALLBACK(ok_button_clicked), ctx);
236         g_signal_connect(G_OBJECT(cancel_button), "clicked",
237                         G_CALLBACK(cancel_button_clicked), ctx);
238
239         g_signal_connect(G_OBJECT(dialog), "destroy",
240                         G_CALLBACK(dialog_destroy), ctx);
241
242         gtk_widget_show_all(gtk_dialog_get_content_area(GTK_DIALOG(dialog)));
243         gtk_window_present(GTK_WINDOW(dialog));
244
245         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
246         manage_window_set_transient(GTK_WINDOW(dialog));
247
248         while (!ctx->done)
249                 gtk_main_iteration();
250
251         if (ctx->dialog != NULL)
252                 gtk_widget_destroy(ctx->dialog);
253
254         GTK_EVENTS_FLUSH();
255
256         g_free(ctx);
257 }
258
259 #endif /* !PASSWORD_CRYPTO_OLD */