Made the gnutls password encryption work on Win32.
[claws.git] / src / password_gtk.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2016 The Claws Mail Team
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
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #ifndef PASSWORD_CRYPTO_OLD
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30
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"
38
39 static void entry_new1_activated(GtkEntry *entry, gpointer user_data)
40 {
41         const gchar *text = gtk_entry_get_text(entry);
42
43         if (strlen(text) > 0)
44                 gtk_widget_grab_focus(GTK_WIDGET(user_data));
45 }
46
47 struct _ctx {
48         gboolean done;
49         GtkWidget *dialog;
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 *new1 = gtk_entry_get_text(GTK_ENTRY(ctx->entry_new1));
58         const gchar *new2 = gtk_entry_get_text(GTK_ENTRY(ctx->entry_new2));
59
60         debug_print("OK button activated\n");
61
62         /* Now we check the new password - same in both entries. */
63         if (strcmp(new1, new2)) {
64                 debug_print("passwords do not match\n");
65                 alertpanel_warning(_("New passwords do not match, try again."));
66                 gtk_entry_set_text(GTK_ENTRY(ctx->entry_new1), "");
67                 gtk_entry_set_text(GTK_ENTRY(ctx->entry_new2), "");
68                 gtk_widget_grab_focus(ctx->entry_new1);
69                 return;
70         }
71
72         master_password_change(new1);
73
74         ctx->done = TRUE;
75         gtk_widget_destroy(ctx->dialog);
76         ctx->dialog = NULL;
77 }
78
79 static void cancel_button_clicked(GtkButton *button, gpointer user_data)
80 {
81         struct _ctx *ctx = (struct _ctx *)user_data;
82         ctx->done = TRUE;
83         gtk_widget_destroy(ctx->dialog);
84         ctx->dialog = NULL;
85 }
86
87 static void dialog_destroy(GtkWidget *widget, gpointer user_data)
88 {
89         struct _ctx *ctx = (struct _ctx *)user_data;
90         ctx->done = TRUE;
91         ctx->dialog = NULL;
92 }
93
94 void master_password_change_dialog()
95 {
96         static PangoFontDescription *font_desc;
97         GtkWidget *dialog;
98         GtkWidget *vbox, *hbox;
99         GtkWidget *icon, *table, *label;
100         GtkWidget *msg_title;
101         GtkWidget *entry_new1, *entry_new2;
102         GtkWidget *confirm_area;
103         GtkWidget *ok_button, *cancel_button;
104         struct _ctx *ctx;
105
106         dialog = gtk_dialog_new();
107
108         gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
109         gtk_window_set_default_size(GTK_WINDOW(dialog), 375, 100);
110         gtk_window_set_title(GTK_WINDOW(dialog), "");
111
112         MANAGE_WINDOW_SIGNALS_CONNECT(dialog);
113
114         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
115         gtk_box_set_spacing(GTK_BOX(vbox), 14);
116         hbox = gtk_hbox_new(FALSE, 12);
117         gtk_container_set_border_width(GTK_CONTAINER(hbox), 5);
118         gtk_widget_show(hbox);
119         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
120
121         icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_AUTHENTICATION,
122                         GTK_ICON_SIZE_DIALOG);
123         gtk_misc_set_alignment(GTK_MISC(icon), 0.5, 0.0);
124         gtk_box_pack_start(GTK_BOX(hbox), icon, FALSE, FALSE, 0);
125
126         vbox = gtk_vbox_new(FALSE, 12);
127         gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
128         gtk_widget_show(vbox);
129
130         msg_title = gtk_label_new(_("Changing master password"));
131         gtk_misc_set_alignment(GTK_MISC(msg_title), 0, 0.5);
132         gtk_label_set_justify(GTK_LABEL(msg_title), GTK_JUSTIFY_LEFT);
133         gtk_label_set_use_markup (GTK_LABEL (msg_title), TRUE);
134         gtk_box_pack_start(GTK_BOX(vbox), msg_title, FALSE, FALSE, 0);
135         gtk_label_set_line_wrap(GTK_LABEL(msg_title), TRUE);
136         if (!font_desc) {
137                 gint size;
138
139                 size = pango_font_description_get_size
140                         (gtk_widget_get_style(msg_title)->font_desc);
141                 font_desc = pango_font_description_new();
142                 pango_font_description_set_weight
143                         (font_desc, PANGO_WEIGHT_BOLD);
144                 pango_font_description_set_size
145                         (font_desc, size * PANGO_SCALE_LARGE);
146         }
147         if (font_desc)
148                 gtk_widget_modify_font(msg_title, font_desc);
149
150         label = gtk_label_new(
151         _("If a master password is currently active the\n"
152         "current password is required to change password.\n"
153         "After pressing the 'Ok' button you will be prompted.")
154         );
155         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
156         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
157         gtk_widget_show(label);
158
159         table = gtk_table_new(2, 2, FALSE);
160
161         label = gtk_label_new(_("New password:"));
162         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
163         gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1,
164                         GTK_EXPAND | GTK_FILL, 0, 0, 0);
165
166         entry_new1 = gtk_entry_new();
167         gtk_entry_set_visibility(GTK_ENTRY(entry_new1), FALSE);
168         gtk_table_attach(GTK_TABLE(table), entry_new1, 1, 2, 0, 1,
169                         GTK_FILL | GTK_EXPAND, 0, 0, 0);
170
171         label = gtk_label_new(_("Confirm password:"));
172         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
173         gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2,
174                         GTK_EXPAND | GTK_FILL, 0, 0, 0);
175
176         entry_new2 = gtk_entry_new();
177         gtk_entry_set_visibility(GTK_ENTRY(entry_new2), FALSE);
178         gtk_table_attach(GTK_TABLE(table), entry_new2, 1, 2, 1, 2,
179                         GTK_FILL | GTK_EXPAND, 0, 0, 0);
180
181         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
182
183         gtkut_stock_button_set_create(&confirm_area,
184                         &cancel_button, GTK_STOCK_CANCEL,
185                         &ok_button, GTK_STOCK_OK,
186                         NULL, NULL);
187
188         gtk_box_pack_end(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(dialog))),
189                         confirm_area, FALSE, FALSE, 0);
190         gtk_container_set_border_width(GTK_CONTAINER(confirm_area), 5);
191
192         gtk_widget_grab_default(ok_button);
193
194         g_signal_connect(G_OBJECT(entry_new1), "activate",
195                         G_CALLBACK(entry_new1_activated), entry_new2);
196         gtk_entry_set_activates_default(GTK_ENTRY(entry_new2), TRUE);
197
198         ctx = g_new(struct _ctx, 1);
199         ctx->done = FALSE;
200         ctx->dialog = dialog;
201         ctx->entry_new1 = entry_new1;
202         ctx->entry_new2 = entry_new2;
203
204         g_signal_connect(G_OBJECT(ok_button), "clicked",
205                         G_CALLBACK(ok_button_clicked), ctx);
206         g_signal_connect(G_OBJECT(cancel_button), "clicked",
207                         G_CALLBACK(cancel_button_clicked), ctx);
208
209         g_signal_connect(G_OBJECT(dialog), "destroy",
210                         G_CALLBACK(dialog_destroy), ctx);
211
212         gtk_widget_show_all(gtk_dialog_get_content_area(GTK_DIALOG(dialog)));
213         gtk_window_present(GTK_WINDOW(dialog));
214
215         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
216         manage_window_set_transient(GTK_WINDOW(dialog));
217
218         while (!ctx->done)
219                 gtk_main_iteration();
220
221         if (ctx->dialog != NULL)
222                 gtk_widget_destroy(ctx->dialog);
223
224         GTK_EVENTS_FLUSH();
225
226         g_free(ctx);
227 }
228
229 #endif /* !PASSWORD_CRYPTO_OLD */