[balsa/gtk3] Port from GnomeKeyring to libsecret



commit 552774a80568f38f0d688184ee4e4a4e6fa1b99b
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Sun Feb 24 19:05:20 2013 -0500

    Port from GnomeKeyring to libsecret
    
        * configure.in: implement --with-libsecret as an alternative to
        GnomeSecret.
        * libbalsa/imap-server.c (libbalsa_imap_server_new_from_config):
        use libsecret if configured.
        * libbalsa/server.c (libbalsa_server_load_config),
        (libbalsa_server_save_config): ditto.
        * libbalsa/server.h: ditto.
        * src/balsa-app.c (ask_password_real): new string to request
        saving in secret service.
        * src/folder-conf.c (folder_conf_imap_node): ditto.
        * src/mailbox-conf.c (create_imap_mailbox_dialog): ditto.

 ChangeLog              |   16 ++++++++++
 configure.in           |   51 +++++++++++++++++++++++---------
 libbalsa/imap-server.c |   50 +++++++++++++++++++++++++++++--
 libbalsa/server.c      |   76 +++++++++++++++++++++++++++++++++++++++++++----
 libbalsa/server.h      |    8 ++++-
 src/balsa-app.c        |    7 +++-
 src/folder-conf.c      |    7 +++-
 src/mailbox-conf.c     |    7 +++-
 8 files changed, 189 insertions(+), 33 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index c81763e..f89e903 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2013-02-24  Peter Bloomfield
+
+       Port from GnomeKeyring to libsecret.
+
+       * configure.in: implement --with-libsecret as an alternative to
+       GnomeSecret.
+       * libbalsa/imap-server.c (libbalsa_imap_server_new_from_config):
+       use libsecret if configured.
+       * libbalsa/server.c (libbalsa_server_load_config),
+       (libbalsa_server_save_config): ditto.
+       * libbalsa/server.h: ditto.
+       * src/balsa-app.c (ask_password_real): new string to request
+       saving in secret service.
+       * src/folder-conf.c (folder_conf_imap_node): ditto.
+       * src/mailbox-conf.c (create_imap_mailbox_dialog): ditto.
+
 2013-02-23  Peter Bloomfield
 
        * update all copyrights to 2013.
diff --git a/configure.in b/configure.in
index 839f991..010f3dd 100644
--- a/configure.in
+++ b/configure.in
@@ -192,6 +192,11 @@ AC_ARG_WITH(gmime,
                   [Version of GMime 2.4, or 2.6 (default=detect)]),
                   [with_gmime=$withval],[with_gmime=detect])
 
+AC_ARG_WITH(libsecret,
+   AC_HELP_STRING([--with-libsecret],
+                  [Link to libsecret instead of gnome-keyring (default=no)]),
+                  [with_libsecret=$withval],[with_libsecret=no])
+
 AC_ARG_ENABLE(more-warnings,
    AC_HELP_STRING([--enable-more-warnings],
                   [Enable maximum compiler warnings (default=yes)]),
@@ -289,21 +294,23 @@ dnl #####################################################################
 
 if test x$with_gnome != xno; then
    gnome_extras="$gnome_extras gio-unix-2.0"
-   AC_MSG_CHECKING([whether we have gnome-keyring])
-   if $PKG_CONFIG --atleast-version=1.0 gnome-keyring-1; then
-      gnome_extras="$gnome_extras gnome-keyring-1"
-      AC_DEFINE(HAVE_GNOME_KEYRING,1,[Defined when gnome-keyring is there.])
-      # Work around http://bugzilla.gnome.org/show_bug.cgi?id=556530
-      if $PKG_CONFIG --atleast-version=2.24.1 gnome-keyring-1; then
-         AC_DEFINE(HAVE_GNOME_KEYRING_24,1,
-                   [Defined with gnome-keyring-2.24 or newer.])
-      fi
-      with_gnome_keyring=yes
-   else
-      with_gnome_keyring=no
+   if test x$with_libsecret == xno; then
+     AC_MSG_CHECKING([whether we have gnome-keyring])
+     if $PKG_CONFIG --atleast-version=1.0 gnome-keyring-1; then
+        gnome_extras="$gnome_extras gnome-keyring-1"
+        AC_DEFINE(HAVE_GNOME_KEYRING,1,[Defined when gnome-keyring is there.])
+        # Work around http://bugzilla.gnome.org/show_bug.cgi?id=556530
+        if $PKG_CONFIG --atleast-version=2.24.1 gnome-keyring-1; then
+           AC_DEFINE(HAVE_GNOME_KEYRING_24,1,
+                     [Defined with gnome-keyring-2.24 or newer.])
+        fi
+        with_gnome_keyring=yes
+     else
+        with_gnome_keyring=no
+     fi
+     AC_MSG_RESULT($with_gnome_keyring)
+     AC_DEFINE(HAVE_GNOME,1,[Defined when gnome libs are available.])
    fi
-   AC_MSG_RESULT($with_gnome_keyring)
-   AC_DEFINE(HAVE_GNOME,1,[Defined when gnome libs are available.])
 else
    gnome_extras=""
 fi
@@ -827,6 +834,21 @@ else
     AC_MSG_RESULT([no])
 fi
 
+# Libsecret configuration
+#
+AC_MSG_CHECKING([whether to configure for libsecret])
+if test x$with_libsecret != xno; then
+    AC_MSG_RESULT([yes])
+    PKG_CHECK_MODULES(LIBSECRET, [ libsecret-1 ], [],
+                      [You asked to link to libsecret, but the
+                       development tool is not installed])
+    AC_DEFINE(HAVE_LIBSECRET,1,[Defined when libsecret can be used.])
+    BALSA_CFLAGS="$BALSA_CFLAGS $LIBSECRET_CFLAGS"
+    BALSA_LIBS="$BALSA_LIBS $LIBSECRET_LIBS"
+else
+    AC_MSG_RESULT([no])
+fi
+
 dnl #####################################################################
 dnl 5. Headers.
 dnl #####################################################################
@@ -1027,6 +1049,7 @@ echo "              Use Compface: $with_compface"
 echo "  Install extra MIME icons: $install_mimeicons"
 echo "    Configure for Touch UI: $enable_touch_ui"
 echo "             GMime version: $with_gmime"
+echo "             Use libsecret: $with_libsecret"
 dnl echo "                  Use SASL: $need_sasl"
 echo ""
 
diff --git a/libbalsa/imap-server.c b/libbalsa/imap-server.c
index 220e261..d611a4f 100644
--- a/libbalsa/imap-server.c
+++ b/libbalsa/imap-server.c
@@ -33,9 +33,11 @@
 #include <stdlib.h>
 #include <pthread.h>
 
-#if defined(HAVE_GNOME_KEYRING)
+#if defined(HAVE_LIBSECRET)
+#include <libsecret/secret.h>
+#elif defined(HAVE_GNOME_KEYRING)
 #include <gnome-keyring.h>
-#endif
+#endif                          /* defined(HAVE_LIBSECRET) */
 
 #include "libbalsa.h"
 #include "libbalsa-conf.h"
@@ -569,7 +571,47 @@ libbalsa_imap_server_new_from_config(void)
     if (!server->passwd) {
         server->remember_passwd = libbalsa_conf_get_bool("RememberPasswd=false");
         if(server->remember_passwd) {
-#if defined (HAVE_GNOME_KEYRING)
+#if defined(HAVE_LIBSECRET)
+            GError *err = NULL;
+
+            server->passwd =
+                secret_password_lookup_sync(LIBBALSA_SERVER_SECRET_SCHEMA,
+                                            NULL, &err,
+                                            "protocol", server->protocol,
+                                            "server",   server->host,
+                                            "user",     server->user,
+                                            NULL);
+            if (err) {
+                libbalsa_free_password(server->passwd);
+                server->passwd = NULL;
+                printf(_("Error looking up password for %s %s: %s\n"),
+                       server->user, server->host, err->message);
+                printf(_("Falling back\n"));
+                g_clear_error(&err);
+                server->passwd =
+                    libbalsa_conf_private_get_string("Password");
+                if (server->passwd != NULL) {
+                    gchar *buff = libbalsa_rot(server->passwd);
+                    libbalsa_free_password(server->passwd);
+                    server->passwd = buff;
+                    secret_password_store_sync
+                        (LIBBALSA_SERVER_SECRET_SCHEMA, NULL,
+                         _("Balsa passwords"), server->passwd, NULL, &err,
+                         "protocol", server->protocol,
+                         "server",   server->host,
+                         "user",     server->user,
+                         NULL);
+                    /* We could in principle clear the password in the
+                     * config file here but we do not for the backward
+                     * compatibility. */
+                    if (err) {
+                        printf(_("Error storing password for %s %s: %s\n"),
+                               server->user, server->host, err->message);
+                        g_error_free(err);
+                    }
+                }
+            }
+#elif defined (HAVE_GNOME_KEYRING)
            GnomeKeyringResult r;
            server->passwd = NULL;
            r = gnome_keyring_find_password_sync(LIBBALSA_SERVER_KEYRING_SCHEMA,
@@ -607,7 +649,7 @@ libbalsa_imap_server_new_from_config(void)
                libbalsa_free_password(server->passwd);
                server->passwd = buff;
            }
-#endif
+#endif                          /* defined(HAVE_LIBSECRET) */
        }
         if(server->passwd && server->passwd[0] == '\0') {
             libbalsa_free_password(server->passwd);
diff --git a/libbalsa/server.c b/libbalsa/server.c
index 1ecd92f..86dff2b 100644
--- a/libbalsa/server.c
+++ b/libbalsa/server.c
@@ -28,9 +28,11 @@
 #include <string.h>
 #include <stdlib.h>
 
-#if defined(HAVE_GNOME_KEYRING)
+#if defined(HAVE_LIBSECRET)
+#include <libsecret/secret.h>
+#elif defined(HAVE_GNOME_KEYRING)
 #include <gnome-keyring.h>
-#endif
+#endif                          /* defined(HAVE_LIBSECRET) */
 
 #ifdef USE_TLS
 #include <openssl/err.h>
@@ -42,7 +44,18 @@
 #include "libbalsa-conf.h"
 #include <glib/gi18n.h>
 
-#if defined(HAVE_GNOME_KEYRING) && !defined(HAVE_GNOME_KEYRING_24)
+#if defined(HAVE_LIBSECRET)
+static const SecretSchema server_schema = {
+    "org.gnome.Balsa.NetworkPassword", SECRET_SCHEMA_NONE,
+    {
+       { "protocol", SECRET_SCHEMA_ATTRIBUTE_STRING },
+       { "server",   SECRET_SCHEMA_ATTRIBUTE_STRING },
+       { "user",     SECRET_SCHEMA_ATTRIBUTE_STRING },
+       { NULL, 0 }
+    }
+};
+const SecretSchema *LIBBALSA_SERVER_SECRET_SCHEMA = &server_schema;
+#elif defined(HAVE_GNOME_KEYRING) && !defined(HAVE_GNOME_KEYRING_24)
 static const GnomeKeyringPasswordSchema server_schema = {
     GNOME_KEYRING_ITEM_GENERIC_SECRET,
     {
@@ -54,7 +67,7 @@ static const GnomeKeyringPasswordSchema server_schema = {
 };
 const GnomeKeyringPasswordSchema* LIBBALSA_SERVER_KEYRING_SCHEMA =
     &server_schema;
-#endif /* HAVE_GNOME_KEYRING */
+#endif                          /* defined(HAVE_LIBSECRET) */
 
 static GObjectClass *parent_class = NULL;
 static void libbalsa_server_class_init(LibBalsaServerClass * klass);
@@ -326,7 +339,46 @@ libbalsa_server_load_config(LibBalsaServer * server)
     server->remember_passwd = libbalsa_conf_get_bool("RememberPasswd=false");
 
     if(server->remember_passwd) {
-#if defined (HAVE_GNOME_KEYRING)
+#if defined(HAVE_LIBSECRET)
+        GError *err = NULL;
+
+        server->passwd =
+            secret_password_lookup_sync(LIBBALSA_SERVER_SECRET_SCHEMA,
+                                        NULL, &err,
+                                        "protocol", server->protocol,
+                                        "server",   server->host,
+                                        "user",     server->user,
+                                        NULL);
+        if (err) {
+            libbalsa_free_password(server->passwd);
+            server->passwd = NULL;
+            printf(_("Error looking up password for %s %s: %s\n"),
+                   server->user, server->host, err->message);
+            printf(_("Falling back\n"));
+            g_clear_error(&err);
+            server->passwd = libbalsa_conf_private_get_string("Password");
+            if (server->passwd != NULL) {
+                gchar *buff = libbalsa_rot(server->passwd);
+                libbalsa_free_password(server->passwd);
+                server->passwd = buff;
+                secret_password_store_sync
+                    (LIBBALSA_SERVER_SECRET_SCHEMA, NULL,
+                     _("Balsa passwords"), server->passwd, NULL, &err,
+                     "protocol", server->protocol,
+                     "server",   server->host,
+                     "user",     server->user,
+                     NULL);
+                /* We could in principle clear the password in the
+                 * config file here but we do not for the backward
+                 * compatibility. */
+                if (err) {
+                    printf(_("Error storing password for %s %s: %s\n"),
+                           server->user, server->host, err->message);
+                    g_error_free(err);
+                }
+            }
+        }
+#elif defined (HAVE_GNOME_KEYRING)
        GnomeKeyringResult res =
            gnome_keyring_find_password_sync(LIBBALSA_SERVER_KEYRING_SCHEMA,
                                             &server->passwd,
@@ -386,7 +438,17 @@ libbalsa_server_save_config(LibBalsaServer * server)
                           server->remember_passwd && server->passwd != NULL);
 
     if (server->remember_passwd && server->passwd != NULL) {
-#if defined(HAVE_GNOME_KEYRING)
+#if defined(HAVE_LIBSECRET)
+        GError *err = NULL;
+
+        secret_password_store_sync(LIBBALSA_SERVER_SECRET_SCHEMA, NULL,
+                                   _("Balsa passwords"), server->passwd,
+                                   NULL, &err,
+                                   "protocol", server->protocol,
+                                   "server",   server->host,
+                                   "user",     server->user,
+                                   NULL);
+#elif defined (HAVE_GNOME_KEYRING)
        gnome_keyring_store_password_sync(LIBBALSA_SERVER_KEYRING_SCHEMA,
                                     NULL,
                                     _("Balsa passwords"),
@@ -399,7 +461,7 @@ libbalsa_server_save_config(LibBalsaServer * server)
        gchar *buff = libbalsa_rot(server->passwd);
        libbalsa_conf_private_set_string("Password", buff);
        g_free(buff);
-#endif
+#endif                          /* defined(HAVE_LIBSECRET) */
     }
     libbalsa_conf_set_bool("SSL", server->use_ssl);
     libbalsa_conf_set_int("TLSMode", server->tls_mode);
diff --git a/libbalsa/server.h b/libbalsa/server.h
index bd16cf0..2c3af1e 100644
--- a/libbalsa/server.h
+++ b/libbalsa/server.h
@@ -30,7 +30,11 @@
 #include "imap/libimap.h"
 #include "libbalsa.h"
 
-#if defined (HAVE_GNOME_KEYRING)
+#if defined(HAVE_LIBSECRET)
+#include <libsecret/secret.h>
+extern const SecretSchema *LIBBALSA_SERVER_SECRET_SCHEMA;
+#define libbalsa_free_password secret_password_free
+#elif defined (HAVE_GNOME_KEYRING)
 #include <gnome-keyring.h>
 
 #if defined(HAVE_GNOME_KEYRING_24)
@@ -42,7 +46,7 @@ extern const GnomeKeyringPasswordSchema* LIBBALSA_SERVER_KEYRING_SCHEMA;
 #define libbalsa_free_password gnome_keyring_free_password
 #else
 #define libbalsa_free_password g_free
-#endif /* HAVE_GNOME_KEYRING */
+#endif                          /* defined(HAVE_LIBSECRET) */
 
 #define LIBBALSA_TYPE_SERVER \
     (libbalsa_server_get_type())
diff --git a/src/balsa-app.c b/src/balsa-app.c
index 4c141d0..eb79ab6 100644
--- a/src/balsa-app.c
+++ b/src/balsa-app.c
@@ -61,13 +61,16 @@ ask_password_real(LibBalsaServer * server, LibBalsaMailbox * mbox)
     GtkWidget *dialog, *entry, *rememb;
     GtkWidget *content_area;
     gchar *prompt, *passwd = NULL;
-#if defined(HAVE_GNOME_KEYRING)
+#if defined(HAVE_LIBSECRET)
+    static const gchar *remember_password_message =
+        N_("_Remember password in Secret Service");
+#elif defined (HAVE_GNOME_KEYRING)
     static const gchar *remember_password_message =
         N_("_Remember password in keyring");
 #else
     static const gchar *remember_password_message =
         N_("_Remember password");
-#endif
+#endif                          /* defined(HAVE_LIBSECRET) */
 
     g_return_val_if_fail(server != NULL, NULL);
     if (mbox)
diff --git a/src/folder-conf.c b/src/folder-conf.c
index 826a8e2..b37f853 100644
--- a/src/folder-conf.c
+++ b/src/folder-conf.c
@@ -273,13 +273,16 @@ folder_conf_imap_node(BalsaMailboxNode *mn)
     gchar *default_server;
     int r = 0;
 
-#if defined(HAVE_GNOME_KEYRING)
+#if defined(HAVE_LIBSECRET)
+    static const gchar *remember_password_message =
+        N_("_Remember password in Secret Service");
+#elif defined (HAVE_GNOME_KEYRING)
     static const gchar *remember_password_message =
         N_("_Remember password in keyring");
 #else
     static const gchar *remember_password_message =
         N_("_Remember password");
-#endif
+#endif                          /* defined(HAVE_LIBSECRET) */
 
     /* Allow only one dialog per mailbox node, and one with mn == NULL
      * for creating a new folder. */
diff --git a/src/mailbox-conf.c b/src/mailbox-conf.c
index 8d1d827..dcefc46 100644
--- a/src/mailbox-conf.c
+++ b/src/mailbox-conf.c
@@ -1314,13 +1314,16 @@ create_imap_mailbox_dialog(MailboxConfWindow *mcw)
     GtkWidget *entry;
     gint row = -1;
 
-#if defined(HAVE_GNOME_KEYRING)
+#if defined(HAVE_LIBSECRET)
+    static const gchar *remember_password_message =
+        N_("_Remember password in Secret Service");
+#elif defined (HAVE_GNOME_KEYRING)
     static const gchar *remember_password_message =
         N_("_Remember password in keyring");
 #else
     static const gchar *remember_password_message =
         N_("_Remember password");
-#endif
+#endif                          /* defined(HAVE_LIBSECRET) */
 
     notebook = gtk_notebook_new();
     grid = libbalsa_create_grid();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]