]> git.0d.be Git - empathy.git/blob - src/empathy-auto-salut-account-helper.c
Don't pass managers to create_salut_account_if_needed
[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 static void
107 create_salut_account_am_ready_cb (GObject *source_object,
108     GAsyncResult *result,
109     gpointer user_data)
110 {
111   TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
112   EmpathyConnectionManagers *managers = user_data;
113   EmpathyAccountSettings  *settings;
114   TpConnectionManager *manager;
115   const TpConnectionManagerProtocol *protocol;
116   EBook      *book;
117   EContact   *contact;
118   gchar      *nickname = NULL;
119   gchar      *first_name = NULL;
120   gchar      *last_name = NULL;
121   gchar      *email = NULL;
122   gchar      *jid = NULL;
123   GError     *error = NULL;
124
125   if (!tp_account_manager_prepare_finish (account_manager, result, &error))
126     {
127       DEBUG ("Failed to prepare account manager: %s", error->message);
128       g_error_free (error);
129       goto out;
130     }
131
132   if (!should_create_salut_account (account_manager))
133     goto out;
134
135   manager = empathy_connection_managers_get_cm (managers, "salut");
136   if (manager == NULL)
137     {
138       DEBUG ("Salut not installed, not making a salut account");
139       goto out;
140     }
141
142   protocol = tp_connection_manager_get_protocol (manager, "local-xmpp");
143   if (protocol == NULL)
144     {
145       DEBUG ("Salut doesn't support local-xmpp!!");
146       goto out;
147     }
148
149   DEBUG ("Trying to add a salut account...");
150
151   /* Get self EContact from EDS */
152   if (!e_book_get_self (&contact, &book, &error))
153     {
154       DEBUG ("Failed to get self econtact: %s",
155           error ? error->message : "No error given");
156       g_clear_error (&error);
157       goto out;
158     }
159
160   settings = empathy_account_settings_new ("salut", "local-xmpp",
161       _("People nearby"));
162
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);
168
169   if (!tp_strdiff (nickname, "nickname"))
170     {
171       g_free (nickname);
172       nickname = NULL;
173     }
174
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);
178
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 : "");
187
188   empathy_account_settings_apply_async (settings,
189       salut_account_created, NULL);
190
191   g_free (nickname);
192   g_free (first_name);
193   g_free (last_name);
194   g_free (email);
195   g_free (jid);
196   g_object_unref (settings);
197   g_object_unref (contact);
198   g_object_unref (book);
199
200  out:
201   g_object_unref (managers);
202 }
203
204 static void
205 create_salut_account_cms_ready_cb (EmpathyConnectionManagers *managers)
206 {
207   TpAccountManager *manager;
208
209   manager = tp_account_manager_dup ();
210
211   tp_account_manager_prepare_async (manager, NULL,
212       create_salut_account_am_ready_cb, managers);
213
214   g_object_unref (manager);
215 }
216
217 void
218 create_salut_account_if_needed (void)
219 {
220   EmpathyConnectionManagers *managers;
221
222   managers = empathy_connection_managers_dup_singleton ();
223
224   if (empathy_connection_managers_is_ready (managers))
225     {
226       create_salut_account_cms_ready_cb (managers);
227     }
228   else
229     {
230       g_signal_connect (managers, "notify::ready",
231             G_CALLBACK (create_salut_account_cms_ready_cb), NULL);
232     }
233 }