]> git.0d.be Git - empathy.git/blob - src/empathy-auto-salut-account-helper.c
factor out and export create_salut_account_settings
[empathy.git] / src / empathy-auto-salut-account-helper.c
1 /*
2  * Copyright (C) 2007-2010 Collabora Ltd.
3  *
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.
8  *
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.
13  *
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
17  *
18  * Authors: Xavier Claessens <xclaesse@gmail.com>
19  *          Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
20  */
21
22 #include <glib.h>
23 #include <glib/gi18n.h>
24
25 #include <telepathy-glib/account-manager.h>
26 #include <telepathy-glib/util.h>
27 #include <libebook/e-book.h>
28
29 #include <libempathy/empathy-account-settings.h>
30 #include <libempathy-gtk/empathy-conf.h>
31
32 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
33 #include <libempathy/empathy-debug.h>
34
35 #include "empathy-auto-salut-account-helper.h"
36
37 /* Salut account creation. The TpAccountManager first argument
38  * must already be prepared when calling this function. */
39 gboolean
40 should_create_salut_account (TpAccountManager *manager)
41 {
42   gboolean salut_created = FALSE;
43   GList *accounts, *l;
44
45   /* Check if we already created a salut account */
46   empathy_conf_get_bool (empathy_conf_get (),
47       EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
48       &salut_created);
49
50   if (salut_created)
51     {
52       DEBUG ("Gconf says we already created a salut account once");
53       return FALSE;
54     }
55
56   accounts = tp_account_manager_get_valid_accounts (manager);
57
58   for (l = accounts; l != NULL;  l = g_list_next (l))
59     {
60       TpAccount *account = TP_ACCOUNT (l->data);
61
62       if (!tp_strdiff (tp_account_get_protocol (account), "local-xmpp"))
63         {
64           salut_created = TRUE;
65           break;
66         }
67     }
68
69   g_list_free (accounts);
70
71   if (salut_created)
72     {
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,
76           TRUE);
77     }
78
79   return !salut_created;
80 }
81
82 static void
83 salut_account_created (GObject *source,
84     GAsyncResult *result,
85     gpointer user_data)
86 {
87   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (source);
88   TpAccount *account;
89   GError *error = NULL;
90
91   if (!empathy_account_settings_apply_finish (settings, result, &error))
92     {
93       DEBUG ("Failed to create salut account: %s", error->message);
94       g_error_free (error);
95       return;
96     }
97
98   account = empathy_account_settings_get_account (settings);
99
100   tp_account_set_enabled_async (account, TRUE, NULL, NULL);
101   empathy_conf_set_bool (empathy_conf_get (),
102       EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
103       TRUE);
104 }
105
106 EmpathyAccountSettings *
107 create_salut_account_settings (void)
108 {
109   EmpathyAccountSettings  *settings;
110   EBook *book;
111   EContact *contact;
112   gchar *nickname = NULL;
113   gchar *first_name = NULL;
114   gchar *last_name = NULL;
115   gchar *email = NULL;
116   gchar *jid = NULL;
117   GError *error = NULL;
118
119   settings = empathy_account_settings_new ("salut", "local-xmpp",
120       _("People nearby"));
121
122   /* Get self EContact from EDS */
123   if (!e_book_get_self (&contact, &book, &error))
124     {
125       DEBUG ("Failed to get self econtact: %s",
126           error ? error->message : "No error given");
127       g_error_free (error);
128       return settings;
129     }
130
131   nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
132   first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
133   last_name = e_contact_get (contact, E_CONTACT_FAMILY_NAME);
134   email = e_contact_get (contact, E_CONTACT_EMAIL_1);
135   jid = e_contact_get (contact, E_CONTACT_IM_JABBER_HOME_1);
136
137   if (!tp_strdiff (nickname, "nickname"))
138     {
139       g_free (nickname);
140       nickname = NULL;
141     }
142
143   DEBUG ("Salut account created:\nnickname=%s\nfirst-name=%s\n"
144      "last-name=%s\nemail=%s\njid=%s\n",
145      nickname, first_name, last_name, email, jid);
146
147   empathy_account_settings_set_string (settings,
148       "nickname", nickname ? nickname : "");
149   empathy_account_settings_set_string (settings,
150       "first-name", first_name ? first_name : "");
151   empathy_account_settings_set_string (settings,
152       "last-name", last_name ? last_name : "");
153   empathy_account_settings_set_string (settings, "email", email ? email : "");
154   empathy_account_settings_set_string (settings, "jid", jid ? jid : "");
155
156   g_free (nickname);
157   g_free (first_name);
158   g_free (last_name);
159   g_free (email);
160   g_free (jid);
161   g_object_unref (contact);
162   g_object_unref (book);
163
164   return settings;
165 }
166
167 static void
168 create_salut_account_am_ready_cb (GObject *source_object,
169     GAsyncResult *result,
170     gpointer user_data)
171 {
172   TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
173   EmpathyConnectionManagers *managers = user_data;
174   EmpathyAccountSettings  *settings;
175   TpConnectionManager *manager;
176   const TpConnectionManagerProtocol *protocol;
177   GError     *error = NULL;
178
179   if (!tp_account_manager_prepare_finish (account_manager, result, &error))
180     {
181       DEBUG ("Failed to prepare account manager: %s", error->message);
182       g_error_free (error);
183       goto out;
184     }
185
186   if (!should_create_salut_account (account_manager))
187     goto out;
188
189   manager = empathy_connection_managers_get_cm (managers, "salut");
190   if (manager == NULL)
191     {
192       DEBUG ("Salut not installed, not making a salut account");
193       goto out;
194     }
195
196   protocol = tp_connection_manager_get_protocol (manager, "local-xmpp");
197   if (protocol == NULL)
198     {
199       DEBUG ("Salut doesn't support local-xmpp!!");
200       goto out;
201     }
202
203   DEBUG ("Trying to add a salut account...");
204
205   settings = create_salut_account_settings ();
206   if (!empathy_account_settings_is_valid (settings))
207     {
208       g_object_unref (settings);
209       goto out;
210     }
211
212   empathy_account_settings_apply_async (settings,
213       salut_account_created, NULL);
214
215   g_object_unref (settings);
216
217 out:
218   g_object_unref (managers);
219 }
220
221 static void
222 create_salut_account_cms_ready_cb (EmpathyConnectionManagers *managers)
223 {
224   TpAccountManager *manager;
225
226   manager = tp_account_manager_dup ();
227
228   tp_account_manager_prepare_async (manager, NULL,
229       create_salut_account_am_ready_cb, managers);
230
231   g_object_unref (manager);
232 }
233
234 void
235 create_salut_account_if_needed (void)
236 {
237   EmpathyConnectionManagers *managers;
238
239   managers = empathy_connection_managers_dup_singleton ();
240
241   if (empathy_connection_managers_is_ready (managers))
242     {
243       create_salut_account_cms_ready_cb (managers);
244     }
245   else
246     {
247       g_signal_connect (managers, "notify::ready",
248             G_CALLBACK (create_salut_account_cms_ready_cb), NULL);
249     }
250 }