Another logic fix for handling master password change dialog.
[claws.git] / src / prefs_other.c
index f67fe550540c74a874278886abf524c86f5bf014..405a37406ed99fcbe1245cf32afd0c49e9b8820c 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2005-2011 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 "defs.h"
 #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
@@ -61,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 {
@@ -77,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)
@@ -159,7 +170,7 @@ static void prefs_keybind_select(void)
 static gboolean prefs_keybind_key_pressed(GtkWidget *widget, GdkEventKey *event,
                                          gpointer data)
 {
-       if (event && event->keyval == GDK_Escape)
+       if (event && event->keyval == GDK_KEY_Escape)
                prefs_keybind_cancel();
        return FALSE;
 }
@@ -193,6 +204,10 @@ static void prefs_keybind_apply(struct KeyBind keybind[], gint num)
                const gchar *accel_key
                        = keybind[i].accel_key ? keybind[i].accel_key : "";
                gtk_accelerator_parse(accel_key, &key, &mods);
+               if (key == 0 && mods == 0) {
+                       g_message("Failed parsing accelerator '%s' for path '%s'\n",
+                                 accel_key, keybind[i].accel_path);
+               }
                gtk_accel_map_change_entry(keybind[i].accel_path,
                                           key, mods, TRUE);
        }
@@ -221,11 +236,11 @@ static void prefs_keybind_apply_clicked(GtkWidget *widget)
 
                {"<Actions>/Menu/View/ShowHide/MessageView",            "V"},
                {"<Actions>/Menu/View/ThreadView",                      "<control>T"},
-               {"<Actions>/Menu/View/GoTo/Prev",                       "P"},
-               {"<Actions>/Menu/View/GoTo/Next",                       "N"},
-               {"<Actions>/Menu/View/GoTo/PrevUnread",                 "<shift>P"},
-               {"<Actions>/Menu/View/GoTo/NextUnread",                 "<shift>N"},
-               {"<Actions>/Menu/View/GoTo/OtherFolder",                "G"},
+               {"<Actions>/Menu/View/Goto/Prev",                       "P"},
+               {"<Actions>/Menu/View/Goto/Next",                       "N"},
+               {"<Actions>/Menu/View/Goto/PrevUnread",                 "<shift>P"},
+               {"<Actions>/Menu/View/Goto/NextUnread",                 "<shift>N"},
+               {"<Actions>/Menu/View/Goto/OtherFolder",                "G"},
                {"<Actions>/Menu/View/OpenNewWindow",                   "<control><alt>N"},
                {"<Actions>/Menu/View/MessageSource",                   "<control>U"},
                {"<Actions>/Menu/View/AllHeaders",                      "<control>H"},
@@ -301,11 +316,11 @@ static void prefs_keybind_apply_clicked(GtkWidget *widget)
 
                {"<Actions>/Menu/View/ShowHide/MessageView",            ""},
                {"<Actions>/Menu/View/ThreadView",                      "<shift>T"},
-               {"<Actions>/Menu/View/GoTo/Prev",                       "P"},
-               {"<Actions>/Menu/View/GoTo/Next",                       "N"},
-               {"<Actions>/Menu/View/GoTo/PrevUnread",                 "<shift>P"},
-               {"<Actions>/Menu/View/GoTo/NextUnread",                 "<shift>N"},
-               {"<Actions>/Menu/View/GoTo/OtherFolder",                "G"},
+               {"<Actions>/Menu/View/Goto/Prev",                       "P"},
+               {"<Actions>/Menu/View/Goto/Next",                       "N"},
+               {"<Actions>/Menu/View/Goto/PrevUnread",                 "<shift>P"},
+               {"<Actions>/Menu/View/Goto/NextUnread",                 "<shift>N"},
+               {"<Actions>/Menu/View/Goto/OtherFolder",                "G"},
                {"<Actions>/Menu/View/OpenNewWindow",                   "<control><alt>N"},
                {"<Actions>/Menu/View/MessageSource",                   "<control>U"},
                {"<Actions>/Menu/View/AllHeaders",                      "<shift>H"},
@@ -345,59 +360,60 @@ static void prefs_keybind_apply_clicked(GtkWidget *widget)
 
        static struct KeyBind mutt_menurc[] = {
                /* main */
-               {"<Actions>/Menu/File/EmptyTrashes",                    ""},
-               {"<Actions>/Menu/File/SaveAs",                          "S"},
-               {"<Actions>/Menu/File/Print",                           "P"},
-               {"<Actions>/Menu/File/Exit",                            "Q"},
-
-               {"<Actions>/Menu/Edit/Copy",                            "<control>C"},
-               {"<Actions>/Menu/Edit/SelectAll",                       "<control>A"},
-               {"<Actions>/Menu/Edit/Find",                            "<control>F"},
-               {"<Actions>/Menu/Edit/SearchFolder",                    "/"},
-
-               {"<Actions>/Menu/View/ShowHide/MessageView",            "V"},
-               {"<Actions>/Menu/View/ThreadView",                      "<control>T"},
-               {"<Actions>/Menu/View/GoTo/Prev",                       ""},
-               {"<Actions>/Menu/View/GoTo/Next",                       ""},
-               {"<Actions>/Menu/View/GoTo/PrevUnread",                 ""},
-               {"<Actions>/Menu/View/GoTo/NextUnread",                 ""},
-               {"<Actions>/Menu/View/GoTo/OtherFolder",                "C"},
-               {"<Actions>/Menu/View/OpenNewWindow",                   "<control><alt>N"},
-               {"<Actions>/Menu/View/MessageSource",                   "<control>U"},
-               {"<Actions>/Menu/View/AllHeaders",                      "<control>H"},
-               {"<Actions>/Menu/View/UpdateSummary",                   "<control><alt>U"},
-
-               {"<Actions>/Menu/Message/Receive/CurrentAccount",
-                                                                       "<control>I"},
-               {"<Actions>/Menu/Message/Receive/AllAccounts",          "<shift><control>I"},
-               {"<Actions>/Menu/Message/ComposeEmail",                 "M"},
-               {"<Actions>/Menu/Message/Reply",                        "R"},
-               {"<Actions>/Menu/Message/ReplyTo/All",                  "G"},
-               {"<Actions>/Menu/Message/ReplyTo/Sender",               ""},
-               {"<Actions>/Menu/Message/ReplyTo/List",                 "<control>L"},
-               {"<Actions>/Menu/Message/Forward",                      "F"},
-               {"<Actions>/Menu/Message/Move",                         "<control>O"},
-               {"<Actions>/Menu/Message/Copy",                         "<shift>C"},
-               {"<Actions>/Menu/Message/Trash",                        "D"},
-               {"<Actions>/Menu/Message/Mark/Mark",                    "<shift>F"},
-               {"<Actions>/Menu/Message/Mark/Unmark",                  "U"},
-               {"<Actions>/Menu/Message/Mark/MarkUnread",              "<shift>N"},
-               {"<Actions>/Menu/Message/Mark/MarkRead",                ""},
-
-               {"<Actions>/Menu/Tools/AddressBook",                    "<shift><control>A"},
-               {"<Actions>/Menu/Tools/Execute",                        "X"},
-               {"<Actions>/Menu/Tools/NetworkLog",                     "<shift><control>L"},
+               {"<Actions>/Menu/File/SaveAs",                          "S"}, /* save-message */
+               {"<Actions>/Menu/File/Print",                           "P"}, /* print-message */
+               {"<Actions>/Menu/File/Exit",                            "Q"}, /* quit */
+
+               {"<Actions>/Menu/Edit/Copy",                            "<control>C"}, /* - */
+               {"<Actions>/Menu/Edit/SelectAll",                       "<control>A"}, /* - */
+               {"<Actions>/Menu/Edit/Find",                            "<alt>B"}, /* <esc>B: search in message bodies */
+               {"<Actions>/Menu/Edit/SearchFolder",                    "slash"}, /* search */
+               {"<Actions>/Menu/Edit/QuickSearch",                     "L"}, /* limit */
+
+               {"<Actions>/Menu/View/ShowHide/MessageView",            "V"}, /* - */
+               {"<Actions>/Menu/View/ThreadView",                      "<control>T"}, /* - */
+               {"<Actions>/Menu/View/Goto/Prev",                       "K"}, /* previous-entry */
+               {"<Actions>/Menu/View/Goto/Next",                       "J"}, /* next-entry */
+               {"<Actions>/Menu/View/Goto/PrevUnread",                 "<alt>U"}, /* <esc>Tab: previous-new-then-unread */
+               {"<Actions>/Menu/View/Goto/NextUnread",                 "U"}, /* Tab: next-new-then-unread */
+               {"<Actions>/Menu/View/Goto/OtherFolder",                "C"}, /* change-folder */
+               {"<Actions>/Menu/View/OpenNewWindow",                   "<control><alt>N"}, /* - */
+               {"<Actions>/Menu/View/MessageSource",                   "E"}, /* edit the raw message */
+               {"<Actions>/Menu/View/AllHeaders",                      "H"}, /* display-toggle-weed */
+               {"<Actions>/Menu/View/UpdateSummary",                   "<control><alt>U"}, /* - */
+
+               {"<Actions>/Menu/Message/Receive/CurrentAccount",       "<control>I"}, /* - */
+               {"<Actions>/Menu/Message/Receive/AllAccounts",          "<shift>G"}, /* fetch-mail */
+               {"<Actions>/Menu/Message/ComposeEmail",                 "M"}, /* mail */
+               {"<Actions>/Menu/Message/Reply",                        "R"}, /* reply */
+               {"<Actions>/Menu/Message/ReplyTo/All",                  "G"}, /* group-reply */
+               {"<Actions>/Menu/Message/ReplyTo/List",                 "<shift>L"}, /* list-reply */
+               {"<Actions>/Menu/Message/Forward",                      "F"}, /* forward-message */
+               {"<Actions>/Menu/Message/Move",                         "<control>O"}, /* - */
+               {"<Actions>/Menu/Message/Copy",                         "<shift>C"}, /* copy-message */
+               {"<Actions>/Menu/Message/Trash",                        "D"}, /* delete-message */
+               {"<Actions>/Menu/Message/Mark/Mark",                    "<shift>F"}, /* flag-message */
+               {"<Actions>/Menu/Message/Mark/Unmark",                  "<control><shift>F"}, /* - */
+               {"<Actions>/Menu/Message/Mark/MarkUnread",              "<shift>N"}, /* toggle-new */
+               {"<Actions>/Menu/Message/Mark/MarkRead",                "<control>R"}, /* read-thread */
+
+               {"<Actions>/Menu/Tools/AddressBook",                    "<shift><control>A"}, /* - */
+               {"<Actions>/Menu/Tools/Execute",                        "dollar"}, /* sync-mailbox */
+               {"<Actions>/Menu/Tools/NetworkLog",                     "<shift><control>L"}, /* - */
                /* compose */
-               {"<Actions>/Menu/Message/Close",                        "<alt>W"},
-               {"<Actions>/Menu/Edit/SelectAll",                       ""},
-               {"<Actions>/Menu/Edit/Advanced/BackWord",               "<alt>B"},
-               {"<Actions>/Menu/Edit/Advanced/ForwWord",               "<alt>F"},
-               {"<Actions>/Menu/Edit/Advanced/BegLine",                "<control>A"},
-               {"<Actions>/Menu/Edit/Advanced/DelBackWord",            "<control>W"},
-               {"<Actions>/Menu/Edit/Advanced/DelForwWord",            "<alt>D"},
+               {"<Actions>/Menu/Message/Close",                        "<alt>W"}, /* - */
+               {"<Actions>/Menu/Edit/Advanced/BackWord",               "<alt>B"}, /* - */
+               {"<Actions>/Menu/Edit/Advanced/ForwWord",               "<alt>F"}, /* - */
+               {"<Actions>/Menu/Edit/Advanced/BegLine",                "<control>A"}, /* - */
+               {"<Actions>/Menu/Edit/Advanced/DelBackWord",            "<control>W"}, /* - */
+               {"<Actions>/Menu/Edit/Advanced/DelForwWord",            "<alt>D"}, /* - */
        };
 
-       text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX(keybind.combo));
+#if !GTK_CHECK_VERSION(2, 24, 0)
+       text = gtk_combo_box_get_active_text(GTK_COMBO_BOX(keybind.combo));
+#else
+       text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(keybind.combo));
+#endif
 
        if (!strcmp(text, _("Default"))) {
                menurc = default_menurc;
@@ -446,7 +462,7 @@ static void prefs_other_create_widget(PrefsPage *_page, GtkWindow *window,
 
        GtkWidget *label_iotimeout;
        GtkWidget *spinbtn_iotimeout;
-       GtkObject *spinbtn_iotimeout_adj;
+       GtkAdjustment *spinbtn_iotimeout_adj;
 
        GtkWidget *vbox2;
        GtkWidget *checkbtn_askonclean;
@@ -460,8 +476,14 @@ 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;
-       CLAWS_TIP_DECL();
 
        vbox1 = gtk_vbox_new (FALSE, VSPACING);
        gtk_widget_show (vbox1);
@@ -541,7 +563,7 @@ static void prefs_other_create_widget(PrefsPage *_page, GtkWindow *window,
        gtk_widget_show (label_iotimeout);
        gtk_box_pack_start (GTK_BOX (hbox1), label_iotimeout, FALSE, FALSE, 0);
 
-       spinbtn_iotimeout_adj = gtk_adjustment_new (60, 0, 1000, 1, 10, 0);
+       spinbtn_iotimeout_adj = GTK_ADJUSTMENT(gtk_adjustment_new (60, 0, 1000, 1, 10, 0));
        spinbtn_iotimeout = gtk_spin_button_new
                (GTK_ADJUSTMENT (spinbtn_iotimeout_adj), 1, 0);
        gtk_widget_show (spinbtn_iotimeout);
@@ -550,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);
 
@@ -581,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), 
@@ -604,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;
@@ -616,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;
 }
 
@@ -650,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));
@@ -705,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