Add a plugin method to allow updating stored passwords on master password change.
[claws.git] / src / plugins / vcalendar / vcal_prefs.c
index fc07408..680e807 100644 (file)
@@ -30,6 +30,7 @@
 #include "defs.h"
 
 #include "mainwindow.h"
+#include "password.h"
 #include "prefs.h"
 #include "prefs_gtk.h"
 #include "prefswindow.h"
@@ -38,6 +39,7 @@
 #include "vcalendar.h"
 #include "vcal_prefs.h"
 #include "vcal_folder.h"
+#include "vcal_dbus.h"
 
 #define PREFS_BLOCK_NAME "VCalendar"
 
@@ -68,6 +70,9 @@ struct VcalendarPage
        GtkWidget *export_freebusy_pass_entry;
 
        GtkWidget *freebusy_get_url_entry;
+       
+       GtkWidget *ssl_verify_peer_checkbtn;
+       GtkWidget *calendar_server_checkbtn;
 };
 
 VcalendarPrefs vcalprefs;
@@ -110,6 +115,12 @@ static PrefParam param[] = {
        {"export_freebusy_pass", "", &vcalprefs.export_freebusy_pass, P_PASSWORD,
         NULL, NULL, NULL},
 
+       {"ssl_verify_peer", "TRUE", &vcalprefs.ssl_verify_peer, P_BOOL,
+        NULL, NULL, NULL},
+
+       {"calendar_server", "FALSE", &vcalprefs.calendar_server, P_BOOL,
+        NULL, NULL, NULL},
+
        {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
 };
 
@@ -130,7 +141,8 @@ static void set_auth_sensitivity(struct VcalendarPage *page)
             !strncmp(export_path, "ftp://", 6) ||
             !strncmp(export_path, "https://", 8) ||
             !strncmp(export_path, "sftp://", 5) ||
-            !strncmp(export_path, "webcal://", 9))) {
+            !strncmp(export_path, "webcal://", 9) ||
+            !strncmp(export_path, "webcals://", 10))) {
                gtk_widget_set_sensitive(page->export_user_label, TRUE);        
                gtk_widget_set_sensitive(page->export_user_entry, TRUE);        
                gtk_widget_set_sensitive(page->export_pass_label, TRUE);        
@@ -146,7 +158,8 @@ static void set_auth_sensitivity(struct VcalendarPage *page)
             !strncmp(export_freebusy_path, "ftp://", 6) ||
             !strncmp(export_freebusy_path, "https://", 8) ||
             !strncmp(export_freebusy_path, "sftp://", 5) ||
-            !strncmp(export_freebusy_path, "webcal://", 9))) {
+            !strncmp(export_freebusy_path, "webcal://", 9) ||
+            !strncmp(export_freebusy_path, "webcals://", 10))) {
                gtk_widget_set_sensitive(page->export_freebusy_user_label, TRUE);       
                gtk_widget_set_sensitive(page->export_freebusy_user_entry, TRUE);       
                gtk_widget_set_sensitive(page->export_freebusy_pass_label, TRUE);       
@@ -201,6 +214,16 @@ void register_orage_checkbtn_toggled(GtkToggleButton       *toggle_btn,
        vcalprefs.orage_registered = gtk_toggle_button_get_active(toggle_btn);
 }
 
+void calendar_server_checkbtn_toggled(GtkToggleButton *toggle, GtkWidget *widget)
+{
+       gboolean active = gtk_toggle_button_get_active(toggle);
+       if (active)
+               connect_dbus();
+       else
+               disconnect_dbus();
+       vcalprefs.calendar_server = active;
+}
+
 static void vcal_prefs_create_widget_func(PrefsPage * _page,
                                           GtkWindow * window,
                                           gpointer data)
@@ -223,6 +246,7 @@ static void vcal_prefs_create_widget_func(PrefsPage * _page,
        GtkWidget *export_command_label;
        GtkWidget *export_command_entry;
        GtkWidget *register_orage_checkbtn;
+       GtkWidget *calendar_server_checkbtn;
 
        GtkWidget *export_user_label;
        GtkWidget *export_user_entry;
@@ -243,6 +267,11 @@ static void vcal_prefs_create_widget_func(PrefsPage * _page,
        GtkWidget *freebusy_get_url_label;
        GtkWidget *freebusy_get_url_entry;
 
+       GtkWidget *frame_ssl_options;
+       GtkWidget *ssl_verify_peer_checkbtn;
+       gchar *export_pass = NULL;
+       gchar *export_freebusy_pass = NULL;
+
        vbox1 = gtk_vbox_new (FALSE, VSPACING);
        gtk_widget_show (vbox1);
        gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
@@ -389,6 +418,19 @@ static void vcal_prefs_create_widget_func(PrefsPage * _page,
        gtk_widget_show (register_orage_checkbtn);
        gtk_box_pack_start(GTK_BOX (hbox3), register_orage_checkbtn, TRUE, TRUE, 0);
 
+       hbox3 = gtk_hbox_new (FALSE, 8);
+       gtk_widget_show (hbox3);
+       gtk_box_pack_start(GTK_BOX (vbox3), hbox3, TRUE, TRUE, 0);
+       calendar_server_checkbtn = gtk_check_button_new_with_label(_("Export as GNOME shell calendar server"));
+       CLAWS_SET_TIP(calendar_server_checkbtn,
+                     _("Register D-Bus calendar server interface to export Claws Mail's calendar"));
+       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(calendar_server_checkbtn),
+                                    vcalprefs.calendar_server);
+       g_signal_connect(G_OBJECT(calendar_server_checkbtn), "toggled",
+                        G_CALLBACK(calendar_server_checkbtn_toggled), NULL);
+       gtk_widget_show(calendar_server_checkbtn);
+       gtk_box_pack_start(GTK_BOX(hbox3), calendar_server_checkbtn, TRUE, TRUE, 0);
+
 /* freebusy export */
 /* export enable + path stuff */
        PACK_FRAME(vbox2, frame_freebusy_export, _("Free/Busy information"));
@@ -492,6 +534,25 @@ static void vcal_prefs_create_widget_func(PrefsPage * _page,
        gtk_entry_set_text(GTK_ENTRY(freebusy_get_url_entry), 
                        vcalprefs.freebusy_get_url);
 
+/* SSL frame */
+       PACK_FRAME(vbox2, frame_ssl_options, _("SSL options"));
+       vbox3 = gtk_vbox_new (FALSE, 8);
+       gtk_widget_show (vbox3);
+       gtk_container_add (GTK_CONTAINER (frame_ssl_options), vbox3);
+       gtk_container_set_border_width (GTK_CONTAINER (vbox3), VBOX_BORDER);
+
+/* SSL peer verification */
+       hbox2 = gtk_hbox_new (FALSE, 8);
+       gtk_widget_show (hbox2);
+       gtk_box_pack_start(GTK_BOX (vbox3), hbox2, TRUE, TRUE, 0);
+
+       ssl_verify_peer_checkbtn = gtk_check_button_new_with_label(
+               _("Verify SSL certificate validity"));
+       gtk_widget_show(ssl_verify_peer_checkbtn);
+       gtk_box_pack_start(GTK_BOX (hbox2), ssl_verify_peer_checkbtn, FALSE, FALSE, 0);
+       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ssl_verify_peer_checkbtn), 
+                       vcalprefs.ssl_verify_peer);
+
        if (!vcalprefs.export_user)
                vcalprefs.export_user = g_strdup("");
        if (!vcalprefs.export_pass)
@@ -501,10 +562,23 @@ static void vcal_prefs_create_widget_func(PrefsPage * _page,
        if (!vcalprefs.export_freebusy_pass)
                vcalprefs.export_freebusy_pass = g_strdup("");
 
+       export_pass = password_decrypt(vcalprefs.export_pass, NULL);
+       export_freebusy_pass = password_decrypt(vcalprefs.export_freebusy_pass, NULL);
+       
        gtk_entry_set_text(GTK_ENTRY(export_user_entry), vcalprefs.export_user);
-       gtk_entry_set_text(GTK_ENTRY(export_pass_entry), vcalprefs.export_pass);
+       gtk_entry_set_text(GTK_ENTRY(export_pass_entry), (export_pass != NULL ? export_pass : ""));
        gtk_entry_set_text(GTK_ENTRY(export_freebusy_user_entry), vcalprefs.export_freebusy_user);
-       gtk_entry_set_text(GTK_ENTRY(export_freebusy_pass_entry), vcalprefs.export_freebusy_pass);
+       gtk_entry_set_text(GTK_ENTRY(export_freebusy_pass_entry), (export_freebusy_pass != NULL ? export_freebusy_pass : ""));
+
+       if (export_pass != NULL) {
+               memset(export_pass, 0, strlen(export_pass));
+       }
+       g_free(export_pass);
+
+       if (export_freebusy_pass != NULL) {
+               memset(export_freebusy_pass, 0, strlen(export_freebusy_pass));
+       }
+       g_free(export_freebusy_pass);
 
        g_signal_connect(G_OBJECT(export_enable_checkbtn),
                         "toggled", G_CALLBACK(path_changed), page);
@@ -537,6 +611,8 @@ static void vcal_prefs_create_widget_func(PrefsPage * _page,
        page->export_freebusy_pass_label = export_freebusy_pass_label;
        page->export_freebusy_pass_entry = export_freebusy_pass_entry;
 
+       page->ssl_verify_peer_checkbtn = ssl_verify_peer_checkbtn;
+
        set_auth_sensitivity(page);
 
        page->freebusy_get_url_entry = freebusy_get_url_entry;
@@ -552,6 +628,7 @@ void vcal_prefs_save(void)
 {
        PrefFile *pfile;
        gchar *rcpath;
+
        rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
        pfile = prefs_write_open(rcpath);
        g_free(rcpath);
@@ -559,7 +636,7 @@ void vcal_prefs_save(void)
                return;
 
        if (prefs_write_param(param, pfile->fp) < 0) {
-               g_warning("failed to write Vcalendar configuration to file\n");
+               g_warning("failed to write vCalendar configuration to file");
                prefs_file_close_revert(pfile);
                return;
        }
@@ -573,6 +650,7 @@ void vcal_prefs_save(void)
 static void vcal_prefs_save_func(PrefsPage * _page)
 {
        struct VcalendarPage *page = (struct VcalendarPage *) _page;
+       gchar *pass;
 
 /* alert */
        vcalprefs.alert_enable =
@@ -603,8 +681,11 @@ static void vcal_prefs_save_func(PrefsPage * _page)
        vcalprefs.export_user =
            gtk_editable_get_chars(GTK_EDITABLE(page->export_user_entry), 0, -1);
        g_free(vcalprefs.export_pass);
-       vcalprefs.export_pass =
-           gtk_editable_get_chars(GTK_EDITABLE(page->export_pass_entry), 0, -1);
+       pass = gtk_editable_get_chars(GTK_EDITABLE(page->export_pass_entry), 0, -1);
+       
+       vcalprefs.export_pass = password_encrypt(pass, NULL);
+       memset(pass, 0, strlen(pass));
+       g_free(pass);
        
 /* free/busy export */
        vcalprefs.export_freebusy_enable = 
@@ -623,20 +704,43 @@ static void vcal_prefs_save_func(PrefsPage * _page)
        vcalprefs.export_freebusy_user =
            gtk_editable_get_chars(GTK_EDITABLE(page->export_freebusy_user_entry), 0, -1);
        g_free(vcalprefs.export_freebusy_pass);
-       vcalprefs.export_freebusy_pass =
-           gtk_editable_get_chars(GTK_EDITABLE(page->export_freebusy_pass_entry), 0, -1);
-       
+       pass = gtk_editable_get_chars(GTK_EDITABLE(page->export_freebusy_pass_entry), 0, -1);
+       vcalprefs.export_freebusy_pass = password_encrypt(pass, NULL);
+       memset(pass, 0, strlen(pass));
+       g_free(pass);
 
 /* free/busy import */
        g_free(vcalprefs.freebusy_get_url);
        vcalprefs.freebusy_get_url =
            gtk_editable_get_chars(GTK_EDITABLE(page->freebusy_get_url_entry), 0, -1);
 
+/* SSL */
+       vcalprefs.ssl_verify_peer = 
+           gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON
+                                        (page->ssl_verify_peer_checkbtn));
 
        vcal_prefs_save();
        vcal_folder_export(NULL);
 }
 
+void vcal_prefs_master_password_change(const gchar *oldp, const gchar *newp) {
+       gchar *pass;
+       pass = password_decrypt(vcalprefs.export_pass, oldp);
+       if (pass != NULL) {
+               g_free(vcalprefs.export_pass);
+               vcalprefs.export_pass = password_encrypt(pass, newp);
+               memset(pass, 0, strlen(pass));
+       }
+       g_free(pass);
+       pass = password_decrypt(vcalprefs.export_freebusy_pass, oldp);
+       if (pass != NULL) {
+               g_free(vcalprefs.export_freebusy_pass);
+               vcalprefs.export_freebusy_pass = password_encrypt(pass, newp);
+               memset(pass, 0, strlen(pass));
+       }
+       g_free(pass);
+}
+
 void vcal_prefs_init(void)
 {
        static gchar *path[3];