b2fb6b82fbef5920ee615296676dc2e86a558b79
[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;
100         GtkWidget *msg_title, *msg_label;
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         msg_label = gtk_label_new("<some useful text goes here>");
151         gtk_misc_set_alignment(GTK_MISC(msg_label), 0, 0.5);
152         gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_LEFT);
153         gtk_box_pack_start(GTK_BOX(vbox), msg_label, FALSE, FALSE, 0);
154         gtk_widget_show(msg_label);
155
156         entry_new1 = gtk_entry_new();
157         gtk_entry_set_visibility(GTK_ENTRY(entry_new1), FALSE);
158         gtk_box_pack_start(GTK_BOX(vbox), entry_new1, FALSE, FALSE, 0);
159
160         entry_new2 = gtk_entry_new();
161         gtk_entry_set_visibility(GTK_ENTRY(entry_new2), FALSE);
162         gtk_box_pack_start(GTK_BOX(vbox), entry_new2, FALSE, FALSE, 0);
163
164         gtkut_stock_button_set_create(&confirm_area,
165                         &cancel_button, GTK_STOCK_CANCEL,
166                         &ok_button, GTK_STOCK_OK,
167                         NULL, NULL);
168
169         gtk_box_pack_end(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(dialog))),
170                         confirm_area, FALSE, FALSE, 0);
171         gtk_container_set_border_width(GTK_CONTAINER(confirm_area), 5);
172
173         gtk_widget_grab_default(ok_button);
174
175         g_signal_connect(G_OBJECT(entry_new1), "activate",
176                         G_CALLBACK(entry_new1_activated), entry_new2);
177         gtk_entry_set_activates_default(GTK_ENTRY(entry_new2), TRUE);
178
179         ctx = g_new(struct _ctx, 1);
180         ctx->done = FALSE;
181         ctx->dialog = dialog;
182         ctx->entry_new1 = entry_new1;
183         ctx->entry_new2 = entry_new2;
184
185         g_signal_connect(G_OBJECT(ok_button), "clicked",
186                         G_CALLBACK(ok_button_clicked), ctx);
187         g_signal_connect(G_OBJECT(cancel_button), "clicked",
188                         G_CALLBACK(cancel_button_clicked), ctx);
189
190         g_signal_connect(G_OBJECT(dialog), "destroy",
191                         G_CALLBACK(dialog_destroy), ctx);
192
193         gtk_widget_show_all(gtk_dialog_get_content_area(GTK_DIALOG(dialog)));
194         gtk_window_present(GTK_WINDOW(dialog));
195
196         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
197         manage_window_set_transient(GTK_WINDOW(dialog));
198
199         while (!ctx->done)
200                 gtk_main_iteration();
201
202         manage_window_focus_out(dialog, NULL, NULL);
203
204         if (ctx->dialog != NULL)
205                 gtk_widget_destroy(ctx->dialog);
206
207         GTK_EVENTS_FLUSH();
208
209         g_free(ctx);
210 }
211
212 #endif /* !PASSWORD_CRYPTO_OLD */