1 /* GData plugin for Claws-Mail
2 * Copyright (C) 2011 Holger Berndt
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.
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.
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/>.
21 # include "claws-features.h"
25 #include <glib/gi18n.h>
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"
38 #include "mainwindow.h"
39 #include "addr_compl.h"
40 #include "passwordstore.h"
42 #include "cm_gdata_contacts.h"
43 #include "cm_gdata_prefs.h"
45 static guint hook_address_completion;
46 static guint hook_offline_switch;
47 static guint timer_query_contacts = 0;
49 static gboolean my_address_completion_build_list_hook(gpointer source, gpointer data)
51 cm_gdata_add_contacts(source);
55 static gboolean my_offline_switch_hook(gpointer source, gpointer data)
57 cm_gdata_update_contacts_cache();
61 static void cm_gdata_save_config(void)
66 debug_print("Saving GData plugin configuration...\n");
68 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
69 pfile = prefs_write_open(rcpath);
71 if (!pfile || (prefs_set_block_label(pfile, "GDataPlugin") < 0))
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);
80 if (fprintf(pfile->fp, "\n") < 0) {
81 FILE_OP_ERROR(rcpath, "fprintf");
82 prefs_file_close_revert(pfile);
85 prefs_file_close(pfile);
86 debug_print("done.\n");
89 void cm_gdata_update_contacts_update_timer(void)
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);
96 gint plugin_init(gchar **error)
101 if(!check_plugin_version(MAKE_NUMERIC_VERSION(3,13,2,39),
102 VERSION_NUMERIC, _("GData"), error))
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"));
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"));
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);
125 /* If the refresh token is still stored in config, save it to
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 passwd_store_write_config();
133 cm_gdata_prefs_init();
135 debug_print("GData plugin loaded\n");
138 cm_gdata_load_contacts_cache_from_file();
139 cm_gdata_update_contacts_update_timer();
140 cm_gdata_update_contacts_cache();
145 gboolean plugin_done(void)
147 if(!claws_is_exiting()) {
148 hooks_unregister_hook(ADDDRESS_COMPLETION_BUILD_ADDRESS_LIST_HOOKLIST, hook_address_completion);
149 hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, hook_offline_switch);
150 g_source_remove(timer_query_contacts);
152 cm_gdata_prefs_done();
153 cm_gdata_contacts_done();
155 cm_gdata_save_config();
157 debug_print("GData plugin unloaded\n");
159 /* returning FALSE because dependant libraries may not be unload-safe. */
163 const gchar *plugin_name(void)
168 const gchar *plugin_desc(void)
170 return _("This plugin provides access to the GData protocol "
171 "for Claws Mail.\n\n"
172 "The GData protocol is an interface to Google services.\n"
173 "Currently, the only implemented functionality is to include "
174 "Google Contacts into the Tab-address completion.\n"
175 "\nFeedback to <berndth@gmx.de> is welcome.");
178 const gchar *plugin_type(void)
183 const gchar *plugin_licence(void)
188 const gchar *plugin_version(void)
193 struct PluginFeature *plugin_provides(void)
195 static struct PluginFeature features[] =
196 { {PLUGIN_UTILITY, N_("GData integration")},
197 {PLUGIN_NOTHING, NULL}};