Make GData plugin use the password store.
[claws.git] / src / plugins / gdata / gdata_plugin.c
1 /* GData plugin for Claws-Mail
2  * Copyright (C) 2011 Holger Berndt
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include "config.h"
21 #  include "claws-features.h"
22 #endif
23
24 #include <glib.h>
25 #include <glib/gi18n.h>
26
27 #ifdef G_OS_UNIX
28 # include <libintl.h>
29 #endif
30
31 #include "common/plugin.h"
32 #include "common/version.h"
33 #include "common/utils.h"
34 #include "common/hooks.h"
35 #include "common/defs.h"
36 #include "common/prefs.h"
37 #include "main.h"
38 #include "mainwindow.h"
39 #include "addr_compl.h"
40 #include "passwordstore.h"
41
42 #include "cm_gdata_contacts.h"
43 #include "cm_gdata_prefs.h"
44
45 static guint hook_address_completion;
46 static guint hook_offline_switch;
47 static guint timer_query_contacts = 0;
48
49 static gboolean my_address_completion_build_list_hook(gpointer source, gpointer data)
50 {
51   cm_gdata_add_contacts(source);
52   return FALSE;
53 }
54
55 static gboolean my_offline_switch_hook(gpointer source, gpointer data)
56 {
57   cm_gdata_update_contacts_cache();
58   return FALSE;
59 }
60
61 static void cm_gdata_save_config(void)
62 {
63   PrefFile *pfile;
64   gchar *rcpath;
65
66   debug_print("Saving GData plugin configuration...\n");
67
68   rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
69   pfile = prefs_write_open(rcpath);
70   g_free(rcpath);
71   if (!pfile || (prefs_set_block_label(pfile, "GDataPlugin") < 0))
72     return;
73
74   if (prefs_write_param(cm_gdata_param, pfile->fp) < 0) {
75     debug_print("failed!\n");
76     g_warning("GData Plugin: Failed to write plugin configuration to file");
77     prefs_file_close_revert(pfile);
78     return;
79   }
80   if (fprintf(pfile->fp, "\n") < 0) {
81     FILE_OP_ERROR(rcpath, "fprintf");
82     prefs_file_close_revert(pfile);
83   }
84   else
85     prefs_file_close(pfile);
86   debug_print("done.\n");
87 }
88
89 void cm_gdata_update_contacts_update_timer(void)
90 {
91   if(timer_query_contacts != 0)
92     g_source_remove(timer_query_contacts);
93   timer_query_contacts = g_timeout_add_seconds(cm_gdata_config.max_cache_age, (GSourceFunc)cm_gdata_update_contacts_cache, NULL);
94 }
95
96 gint plugin_init(gchar **error)
97 {
98   gchar *rcpath;
99
100   /* Version check */
101   if(!check_plugin_version(MAKE_NUMERIC_VERSION(3,13,2,39),
102                            VERSION_NUMERIC, _("GData"), error))
103     return -1;
104
105   hook_address_completion = hooks_register_hook(ADDDRESS_COMPLETION_BUILD_ADDRESS_LIST_HOOKLIST,
106       my_address_completion_build_list_hook, NULL);
107   if(hook_address_completion == (guint) -1) {
108     *error = g_strdup(_("Failed to register address completion hook in the GData plugin"));
109     return -1;
110   }
111
112   hook_offline_switch = hooks_register_hook(OFFLINE_SWITCH_HOOKLIST, my_offline_switch_hook, NULL);
113   if(hook_offline_switch == (guint) -1) {
114     hooks_unregister_hook(ADDDRESS_COMPLETION_BUILD_ADDRESS_LIST_HOOKLIST, hook_address_completion);
115     *error = g_strdup(_("Failed to register offline switch hook in the GData plugin"));
116     return -1;
117   }
118
119   /* Configuration */
120   prefs_set_default(cm_gdata_param);
121   rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
122   prefs_read_config(cm_gdata_param, "GDataPlugin", rcpath, NULL);
123   g_free(rcpath);
124
125         /* If the refresh token is still stored in config, save it to
126          * password store. */
127         if(cm_gdata_config.oauth2_refresh_token != NULL) {
128                 passwd_store_set(PWS_PLUGIN, "GData", GDATA_TOKEN_PWD_STRING,
129                                 cm_gdata_config.oauth2_refresh_token, FALSE);
130         }
131
132   cm_gdata_prefs_init();
133
134   debug_print("GData plugin loaded\n");
135
136   /* contacts cache */
137   cm_gdata_load_contacts_cache_from_file();
138   cm_gdata_update_contacts_update_timer();
139   cm_gdata_update_contacts_cache();
140
141   return 0;
142 }
143
144 gboolean plugin_done(void)
145 {
146   if(!claws_is_exiting()) {
147     hooks_unregister_hook(ADDDRESS_COMPLETION_BUILD_ADDRESS_LIST_HOOKLIST, hook_address_completion);
148     hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, hook_offline_switch);
149     g_source_remove(timer_query_contacts);
150   }
151   cm_gdata_prefs_done();
152   cm_gdata_contacts_done();
153
154   cm_gdata_save_config();
155
156   debug_print("GData plugin unloaded\n");
157
158   /* returning FALSE because dependant libraries may not be unload-safe. */
159   return FALSE;
160 }
161
162 const gchar *plugin_name(void)
163 {
164   return _("GData");
165 }
166
167 const gchar *plugin_desc(void)
168 {
169   return _("This plugin provides access to the GData protocol "
170            "for Claws Mail.\n\n"
171       "The GData protocol is an interface to Google services.\n"
172       "Currently, the only implemented functionality is to include "
173       "Google Contacts into the Tab-address completion.\n"
174      "\nFeedback to <berndth@gmx.de> is welcome.");
175 }
176
177 const gchar *plugin_type(void)
178 {
179   return "GTK2";
180 }
181
182 const gchar *plugin_licence(void)
183 {
184   return "GPL3+";
185 }
186
187 const gchar *plugin_version(void)
188 {
189   return VERSION;
190 }
191
192 struct PluginFeature *plugin_provides(void)
193 {
194   static struct PluginFeature features[] =
195     { {PLUGIN_UTILITY, N_("GData integration")},
196       {PLUGIN_NOTHING, NULL}};
197   return features;
198 }