2 * Copyright (C) 2007-2010 Collabora Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Xavier Claessens <xclaesse@gmail.com>
19 * Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
23 #include <glib/gi18n.h>
25 #include <telepathy-glib/account-manager.h>
26 #include <telepathy-glib/util.h>
27 #include <libebook/e-book.h>
29 #include <libempathy/empathy-account-settings.h>
30 #include <libempathy-gtk/empathy-conf.h>
32 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
33 #include <libempathy/empathy-debug.h>
35 #include "empathy-auto-salut-account-helper.h"
37 /* Salut account creation. The TpAccountManager first argument
38 * must already be prepared when calling this function. */
40 should_create_salut_account (TpAccountManager *manager)
42 gboolean salut_created = FALSE;
45 /* Check if we already created a salut account */
46 empathy_conf_get_bool (empathy_conf_get (),
47 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
52 DEBUG ("Gconf says we already created a salut account once");
56 accounts = tp_account_manager_get_valid_accounts (manager);
58 for (l = accounts; l != NULL; l = g_list_next (l))
60 TpAccount *account = TP_ACCOUNT (l->data);
62 if (!tp_strdiff (tp_account_get_protocol (account), "local-xmpp"))
69 g_list_free (accounts);
73 DEBUG ("Existing salut account already exists, flagging so in gconf");
74 empathy_conf_set_bool (empathy_conf_get (),
75 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
79 return !salut_created;
83 salut_account_created (GObject *source,
87 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (source);
91 if (!empathy_account_settings_apply_finish (settings, result, &error))
93 DEBUG ("Failed to create salut account: %s", error->message);
98 account = empathy_account_settings_get_account (settings);
100 tp_account_set_enabled_async (account, TRUE, NULL, NULL);
101 empathy_conf_set_bool (empathy_conf_get (),
102 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
107 create_salut_account_am_ready_cb (GObject *source_object,
108 GAsyncResult *result,
111 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
112 EmpathyConnectionManagers *managers = user_data;
113 EmpathyAccountSettings *settings;
114 TpConnectionManager *manager;
115 const TpConnectionManagerProtocol *protocol;
118 gchar *nickname = NULL;
119 gchar *first_name = NULL;
120 gchar *last_name = NULL;
123 GError *error = NULL;
125 if (!tp_account_manager_prepare_finish (account_manager, result, &error))
127 DEBUG ("Failed to prepare account manager: %s", error->message);
128 g_error_free (error);
132 if (!should_create_salut_account (account_manager))
135 manager = empathy_connection_managers_get_cm (managers, "salut");
138 DEBUG ("Salut not installed, not making a salut account");
142 protocol = tp_connection_manager_get_protocol (manager, "local-xmpp");
143 if (protocol == NULL)
145 DEBUG ("Salut doesn't support local-xmpp!!");
149 DEBUG ("Trying to add a salut account...");
151 /* Get self EContact from EDS */
152 if (!e_book_get_self (&contact, &book, &error))
154 DEBUG ("Failed to get self econtact: %s",
155 error ? error->message : "No error given");
156 g_clear_error (&error);
160 settings = empathy_account_settings_new ("salut", "local-xmpp",
163 nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
164 first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
165 last_name = e_contact_get (contact, E_CONTACT_FAMILY_NAME);
166 email = e_contact_get (contact, E_CONTACT_EMAIL_1);
167 jid = e_contact_get (contact, E_CONTACT_IM_JABBER_HOME_1);
169 if (!tp_strdiff (nickname, "nickname"))
175 DEBUG ("Salut account created:\nnickname=%s\nfirst-name=%s\n"
176 "last-name=%s\nemail=%s\njid=%s\n",
177 nickname, first_name, last_name, email, jid);
179 empathy_account_settings_set_string (settings,
180 "nickname", nickname ? nickname : "");
181 empathy_account_settings_set_string (settings,
182 "first-name", first_name ? first_name : "");
183 empathy_account_settings_set_string (settings,
184 "last-name", last_name ? last_name : "");
185 empathy_account_settings_set_string (settings, "email", email ? email : "");
186 empathy_account_settings_set_string (settings, "jid", jid ? jid : "");
188 empathy_account_settings_apply_async (settings,
189 salut_account_created, NULL);
196 g_object_unref (settings);
197 g_object_unref (contact);
198 g_object_unref (book);
201 g_object_unref (managers);
205 create_salut_account_if_needed (EmpathyConnectionManagers *managers)
207 TpAccountManager *manager;
209 manager = tp_account_manager_dup ();
211 tp_account_manager_prepare_async (manager, NULL,
212 create_salut_account_am_ready_cb, g_object_ref (managers));
214 g_object_unref (manager);