Another logic fix for handling master password change dialog.
[claws.git] / src / prefs_other.c
index 8ff9ff5695619536c95de6919e3adb870ce4a8d3..405a37406ed99fcbe1245cf32afd0c49e9b8820c 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2005-2012 Colin Leroy <colin@colino.net> & The Claws Mail Team
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 2005-2015 Colin Leroy and The Claws Mail Team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
- * 
  */
 
 #ifdef HAVE_CONFIG_H
-#  include "config.h"
+#include "config.h"
 #include "claws-features.h"
 #endif
 
 #include "gtk/gtkutils.h"
 #include "gtk/prefswindow.h"
 #include "combobox.h"
+#ifndef PASSWORD_CRYPTO_OLD
+#include "password.h"
+#include "password_gtk.h"
+#endif
 
 #include "manage_window.h"
 #ifdef HAVE_LIBETPAN
@@ -62,6 +65,9 @@ typedef struct _OtherPage
        GtkWidget *checkbtn_real_time_sync;
        GtkWidget *flush_metadata_faster_radiobtn;
        GtkWidget *flush_metadata_safer_radiobtn;
+#ifndef PASSWORD_CRYPTO_OLD
+       GtkWidget *checkbtn_use_password;
+#endif
 } OtherPage;
 
 static struct KeybindDialog {
@@ -78,6 +84,10 @@ static gboolean prefs_keybind_key_pressed    (GtkWidget      *widget,
                                                 gpointer        data);
 static void prefs_keybind_cancel               (void);
 static void prefs_keybind_apply_clicked                (GtkWidget      *widget);
+#ifndef PASSWORD_CRYPTO_OLD
+static void prefs_change_master_password(GtkButton *button, gpointer data);
+static void prefs_use_password_toggled(GtkToggleButton *button, gpointer data);
+#endif
 
 
 static void prefs_keybind_select(void)
@@ -466,6 +476,13 @@ static void prefs_other_create_widget(PrefsPage *_page, GtkWindow *window,
        GtkWidget *flush_metadata_faster_radiobtn;
        GtkWidget *flush_metadata_safer_radiobtn;
 
+#ifndef PASSWORD_CRYPTO_OLD
+       GtkWidget *vbox_password;
+       GtkWidget *frame_password;
+       GtkWidget *checkbtn_use_password;
+       GtkWidget *button_change_password;
+#endif
+
        gchar *shred_binary = NULL;
 
        vbox1 = gtk_vbox_new (FALSE, VSPACING);
@@ -555,7 +572,7 @@ static void prefs_other_create_widget(PrefsPage *_page, GtkWindow *window,
        gtk_widget_set_size_request (spinbtn_iotimeout, 64, -1);
        gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbtn_iotimeout), TRUE);
 
-       label_iotimeout = gtk_label_new (_("seconds"));
+       label_iotimeout = gtk_label_new (_("second(s)"));
        gtk_widget_show (label_iotimeout);
        gtk_box_pack_start (GTK_BOX (hbox1), label_iotimeout, FALSE, FALSE, 0);
 
@@ -586,6 +603,31 @@ static void prefs_other_create_widget(PrefsPage *_page, GtkWindow *window,
        PACK_CHECK_BUTTON (vbox2, checkbtn_real_time_sync,
                           _("Synchronise offline folders as soon as possible"));
 
+#ifndef PASSWORD_CRYPTO_OLD
+       vbox_password = gtkut_get_options_frame(vbox1, &frame_password, _("Master password"));
+
+       PACK_CHECK_BUTTON(vbox_password, checkbtn_use_password,
+                       _("Use a master password"));
+
+       CLAWS_SET_TIP(checkbtn_use_password,
+                       _("If checked, your saved account passwords will be protected "
+                               "by a master password. If no master password is set, "
+                               "you will be prompted to set one."));
+
+       button_change_password = gtk_button_new_with_label(
+                       _("Change master password"));
+       gtk_widget_show (button_change_password);
+       hbox1 = gtk_hbox_new (FALSE, 8);
+       gtk_widget_show (hbox1);
+       gtk_box_pack_start (GTK_BOX (vbox_password), hbox1, FALSE, FALSE, 0);
+       gtk_box_pack_start (GTK_BOX (hbox1), button_change_password,
+                       FALSE, FALSE, 0);
+       g_signal_connect (G_OBJECT (checkbtn_use_password), "toggled",
+                               G_CALLBACK (prefs_use_password_toggled), button_change_password);
+       g_signal_connect (G_OBJECT (button_change_password), "clicked",
+                         G_CALLBACK (prefs_change_master_password), NULL);
+#endif
+
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_addaddrbyclick), 
                prefs_common.add_address_by_click);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_confonexit), 
@@ -609,6 +651,13 @@ static void prefs_other_create_widget(PrefsPage *_page, GtkWindow *window,
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_real_time_sync), 
                prefs_common.real_time_sync);
 
+#ifndef PASSWORD_CRYPTO_OLD
+       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_use_password),
+               prefs_common.use_master_password);
+       gtk_widget_set_sensitive(button_change_password,
+                       prefs_common.use_master_password);
+#endif
+
        prefs_other->checkbtn_addaddrbyclick = checkbtn_addaddrbyclick;
        prefs_other->checkbtn_confonexit = checkbtn_confonexit;
        prefs_other->checkbtn_cleanonexit = checkbtn_cleanonexit;
@@ -621,6 +670,9 @@ static void prefs_other_create_widget(PrefsPage *_page, GtkWindow *window,
        prefs_other->checkbtn_real_time_sync = checkbtn_real_time_sync;
        prefs_other->flush_metadata_safer_radiobtn = flush_metadata_safer_radiobtn;
        prefs_other->flush_metadata_faster_radiobtn = flush_metadata_faster_radiobtn;
+#ifndef PASSWORD_CRYPTO_OLD
+       prefs_other->checkbtn_use_password = checkbtn_use_password;
+#endif
        prefs_other->page.widget = vbox1;
 }
 
@@ -655,7 +707,43 @@ static void prefs_other_save(PrefsPage *_page)
                        GTK_TOGGLE_BUTTON(page->checkbtn_use_shred)); 
        prefs_common.real_time_sync = 
                gtk_toggle_button_get_active(
-                       GTK_TOGGLE_BUTTON(page->checkbtn_real_time_sync)); 
+                       GTK_TOGGLE_BUTTON(page->checkbtn_real_time_sync));
+
+#ifndef PASSWORD_CRYPTO_OLD
+       /* If we're disabling use of master password, we need to reencrypt
+        * all account passwords with hardcoded key. */
+       if (!gtk_toggle_button_get_active(
+                       GTK_TOGGLE_BUTTON(page->checkbtn_use_password))
+                       && master_password_is_set()) {
+               master_password_change(NULL);
+
+               /* In case user did not finish the password change process
+                * (e.g. did not enter a correct current master password),
+                * we need to enable the "use master password" checkbox again,
+                * since the old master password is still valid. */
+               if (master_password_is_set()) {
+                       gtk_toggle_button_set_active(
+                               GTK_TOGGLE_BUTTON(page->checkbtn_use_password), TRUE);
+               }
+       }
+
+       if (gtk_toggle_button_get_active(
+                       GTK_TOGGLE_BUTTON(page->checkbtn_use_password))
+                       && !master_password_is_set()) {
+               master_password_change_dialog();
+
+               /* In case user cancelled the password change dialog, we need
+                * to disable the "use master password" checkbox. */
+               if (!master_password_is_set()) {
+                       gtk_toggle_button_set_active(
+                               GTK_TOGGLE_BUTTON(page->checkbtn_use_password), FALSE);
+               }
+       }
+
+       prefs_common.use_master_password =
+               gtk_toggle_button_get_active(
+                       GTK_TOGGLE_BUTTON(page->checkbtn_use_password));
+#endif
 
        gtk_can_change_accels = gtk_toggle_button_get_active(
                GTK_TOGGLE_BUTTON(page->checkbtn_gtk_can_change_accels));
@@ -710,3 +798,20 @@ void prefs_other_done(void)
        prefs_gtk_unregister_page((PrefsPage *) prefs_other);
        g_free(prefs_other);
 }
+
+#ifndef PASSWORD_CRYPTO_OLD
+void prefs_change_master_password(GtkButton *button, gpointer data)
+{
+       /* Call the password change dialog */
+       master_password_change_dialog();
+}
+
+void prefs_use_password_toggled(GtkToggleButton *chkbtn, gpointer data)
+{
+       GtkWidget *button = GTK_WIDGET(data);
+       gboolean active = gtk_toggle_button_get_active(chkbtn);
+
+       if (!active)
+               gtk_widget_set_sensitive(button, active);
+}
+#endif