Make GData plugin use the password store.
[claws.git] / src / plugins / gdata / cm_gdata_contacts.c
index 20ae00d5381761370d409a2ec63ace9b165c4f4c..86fc265a5a665923a26f11aee4369c6d02f1d54f 100644 (file)
@@ -1,5 +1,6 @@
 /* GData plugin for Claws-Mail
  * Copyright (C) 2011 Holger Berndt
 /* GData plugin for Claws-Mail
  * Copyright (C) 2011 Holger Berndt
+ * Copyright (C) 2011-2015 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
  *
  * 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
@@ -30,6 +31,7 @@
 
 #include "addr_compl.h"
 #include "main.h"
 
 #include "addr_compl.h"
 #include "main.h"
+#include "passwordstore.h"
 #include "prefs_common.h"
 #include "mainwindow.h"
 #include "common/log.h"
 #include "prefs_common.h"
 #include "mainwindow.h"
 #include "common/log.h"
@@ -239,12 +241,12 @@ static void write_cache_to_file(void)
     g_node_append(contactsnode, contactnode);
   }
 
     g_node_append(contactsnode, contactnode);
   }
 
-  /* Actual writing and cleanup */
-  xml_write_tree(rootnode, pfile->fp);
-  if(prefs_file_close(pfile) < 0)
-    debug_print("GData plugin error: Failed to write file " GDATA_CONTACTS_FILENAME "\n");
-
-  debug_print("GData plugin error: Wrote cache to file " GDATA_CONTACTS_FILENAME "\n");
+       /* Actual writing and cleanup */
+       xml_write_tree(rootnode, pfile->fp);
+       if (prefs_file_close(pfile) < 0)
+               debug_print("GData plugin error: Failed to write file " GDATA_CONTACTS_FILENAME "\n");
+       else
+               debug_print("GData plugin: Wrote cache to file " GDATA_CONTACTS_FILENAME "\n");
 
   /* Free XML tree */
   xml_free_tree(rootnode);
 
   /* Free XML tree */
   xml_free_tree(rootnode);
@@ -336,9 +338,9 @@ static void cm_gdata_query_contacts_ready(GDataContactsService *service, GAsyncR
   }
   g_object_unref(feed);
   contacts_cache.contacts = g_slist_reverse(contacts_cache.contacts);
   }
   g_object_unref(feed);
   contacts_cache.contacts = g_slist_reverse(contacts_cache.contacts);
-       /* i18n: First part of "Added X of Y contacts to cache" */
+       /* TRANSLATORS: First part of "Added X of Y contacts to cache" */
   tmpstr1 = g_strdup_printf(ngettext("Added %d of", "Added %d of", num_contacts_added), num_contacts_added);
   tmpstr1 = g_strdup_printf(ngettext("Added %d of", "Added %d of", num_contacts_added), num_contacts_added);
-       /* i18n: Second part of "Added X of Y contacts to cache" */
+       /* TRANSLATORS: Second part of "Added X of Y contacts to cache" */
   tmpstr2 = g_strdup_printf(ngettext("1 contact to the cache", "%d contacts to the cache", num_contacts), num_contacts);
   log_message(LOG_PROTOCOL, "%s %s\n", tmpstr1, tmpstr2);
        g_free(tmpstr1);
   tmpstr2 = g_strdup_printf(ngettext("1 contact to the cache", "%d contacts to the cache", num_contacts), num_contacts);
   log_message(LOG_PROTOCOL, "%s %s\n", tmpstr1, tmpstr2);
        g_free(tmpstr1);
@@ -520,6 +522,8 @@ static guchar* decode(const gchar *in)
 
 static void query()
 {
 
 static void query()
 {
+       gchar *token;
+
   if(cm_gdata_contacts_query_running)
   {
     debug_print("GData plugin: Network query already in progress");
   if(cm_gdata_contacts_query_running)
   {
     debug_print("GData plugin: Network query already in progress");
@@ -550,10 +554,13 @@ static void query()
   {
 #if GDATA_CHECK_VERSION(0,17,2)
     /* Try to restore from saved refresh token.*/
   {
 #if GDATA_CHECK_VERSION(0,17,2)
     /* Try to restore from saved refresh token.*/
-    if(cm_gdata_config.oauth2_refresh_token)
+               if((token = passwd_store_get(PWS_PLUGIN,
+                                               "GData", GDATA_TOKEN_PWD_STRING)) != NULL)
     {
       log_message(LOG_PROTOCOL, _("GData plugin: Trying to refresh authorization\n"));
     {
       log_message(LOG_PROTOCOL, _("GData plugin: Trying to refresh authorization\n"));
-      gdata_oauth2_authorizer_set_refresh_token(authorizer, cm_gdata_config.oauth2_refresh_token);
+      gdata_oauth2_authorizer_set_refresh_token(authorizer, token);
+      memset(token, 0, strlen(token));
+      g_free(token);
       gdata_authorizer_refresh_authorization_async(GDATA_AUTHORIZER(authorizer), NULL, (GAsyncReadyCallback)cm_gdata_refresh_ready, NULL);
     }
     else
       gdata_authorizer_refresh_authorization_async(GDATA_AUTHORIZER(authorizer), NULL, (GAsyncReadyCallback)cm_gdata_refresh_ready, NULL);
     }
     else
@@ -617,6 +624,8 @@ gboolean cm_gdata_update_contacts_cache(void)
 
 void cm_gdata_contacts_done(void)
 {
 
 void cm_gdata_contacts_done(void)
 {
+  gchar *pass;
+
   g_free(contacts_group_id);
   contacts_group_id = NULL;
 
   g_free(contacts_group_id);
   contacts_group_id = NULL;
 
@@ -628,7 +637,11 @@ void cm_gdata_contacts_done(void)
   {
 #if GDATA_CHECK_VERSION(0,17,2)
     /* store refresh token */
   {
 #if GDATA_CHECK_VERSION(0,17,2)
     /* store refresh token */
-    cm_gdata_config.oauth2_refresh_token = gdata_oauth2_authorizer_dup_refresh_token(authorizer);
+    pass = gdata_oauth2_authorizer_dup_refresh_token(authorizer);
+               passwd_store_set(PWS_PLUGIN, "GData", GDATA_TOKEN_PWD_STRING, pass,
+                               FALSE);
+    memset(pass, 0, strlen(pass));
+    g_free(pass);
 #endif
 
     g_object_unref(G_OBJECT(authorizer));
 #endif
 
     g_object_unref(G_OBJECT(authorizer));
@@ -665,7 +678,7 @@ void cm_gdata_load_contacts_cache_from_file(void)
 
   /* Check that root entry is "gdata" */
   if(strcmp2(xmlnode->tag->tag, "gdata") != 0) {
 
   /* Check that root entry is "gdata" */
   if(strcmp2(xmlnode->tag->tag, "gdata") != 0) {
-    g_warning("wrong gdata cache file\n");
+    g_warning("wrong gdata cache file");
     xml_free_tree(rootnode);
     return;
   }
     xml_free_tree(rootnode);
     return;
   }