]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-local-xmpp-assistant-widget.c
Move should_create_salut_account to local-xmpp-assistant-widget
[empathy.git] / libempathy-gtk / empathy-local-xmpp-assistant-widget.c
1 /*
2  * empathy-local-xmpp-assistant-widget.h - Source for
3  * EmpathyLocalXmppAssistantWidget
4  *
5  * Copyright (C) 2012 - Collabora Ltd.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with This library. If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "config.h"
22 #include "empathy-local-xmpp-assistant-widget.h"
23
24 #include <glib/gi18n-lib.h>
25
26 #if HAVE_EDS
27 #include <libebook/e-book.h>
28 #endif
29
30 #include <libempathy/empathy-utils.h>
31
32 #include <libempathy-gtk/empathy-account-widget.h>
33 #include <libempathy-gtk/empathy-ui-utils.h>
34
35 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
36 #include <libempathy/empathy-debug.h>
37
38 G_DEFINE_TYPE (EmpathyLocalXmppAssistantWidget,
39     empathy_local_xmpp_assistant_widget, GTK_TYPE_BOX)
40
41 enum {
42   SIG_VALID = 1,
43   LAST_SIGNAL
44 };
45
46 static gulong signals[LAST_SIGNAL] = { 0, };
47
48 struct _EmpathyLocalXmppAssistantWidgetPrivate
49 {
50   EmpathyAccountSettings  *settings;
51 };
52
53 static void
54 empathy_local_xmpp_assistant_widget_init (EmpathyLocalXmppAssistantWidget *self)
55 {
56   self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self),
57       EMPATHY_TYPE_LOCAL_XMPP_ASSISTANT_WIDGET,
58       EmpathyLocalXmppAssistantWidgetPrivate);
59 }
60
61 static EmpathyAccountSettings *
62 create_salut_account_settings (void)
63 {
64   EmpathyAccountSettings  *settings;
65 #if HAVE_EDS
66   EBook *book;
67   EContact *contact;
68   gchar *nickname = NULL;
69   gchar *first_name = NULL;
70   gchar *last_name = NULL;
71   gchar *email = NULL;
72   gchar *jid = NULL;
73   GError *error = NULL;
74 #endif
75
76   settings = empathy_account_settings_new ("salut", "local-xmpp", NULL,
77       _("People nearby"));
78
79 #if HAVE_EDS
80   /* Get self EContact from EDS */
81   if (!e_book_get_self (&contact, &book, &error))
82     {
83       DEBUG ("Failed to get self econtact: %s", error->message);
84       g_error_free (error);
85       return settings;
86     }
87
88   nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
89   first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
90   last_name = e_contact_get (contact, E_CONTACT_FAMILY_NAME);
91   email = e_contact_get (contact, E_CONTACT_EMAIL_1);
92   jid = e_contact_get (contact, E_CONTACT_IM_JABBER_HOME_1);
93
94   if (!tp_strdiff (nickname, "nickname"))
95     {
96       g_free (nickname);
97       nickname = NULL;
98     }
99
100   DEBUG ("Salut account created:\nnickname=%s\nfirst-name=%s\n"
101      "last-name=%s\nemail=%s\njid=%s\n",
102      nickname, first_name, last_name, email, jid);
103
104   empathy_account_settings_set_string (settings,
105       "nickname", nickname ? nickname : "");
106   empathy_account_settings_set_string (settings,
107       "first-name", first_name ? first_name : "");
108   empathy_account_settings_set_string (settings,
109       "last-name", last_name ? last_name : "");
110   empathy_account_settings_set_string (settings, "email", email ? email : "");
111   empathy_account_settings_set_string (settings, "jid", jid ? jid : "");
112
113   g_free (nickname);
114   g_free (first_name);
115   g_free (last_name);
116   g_free (email);
117   g_free (jid);
118   g_object_unref (contact);
119   g_object_unref (book);
120 #endif
121
122   return settings;
123 }
124
125 static void
126 handle_apply_cb (EmpathyAccountWidget *widget_object,
127     gboolean is_valid,
128     EmpathyLocalXmppAssistantWidget *self)
129 {
130   g_signal_emit (self, signals[SIG_VALID], 0, is_valid);
131 }
132
133 static void
134 empathy_local_xmpp_assistant_widget_constructed (GObject *object)
135 {
136   EmpathyLocalXmppAssistantWidget *self = (EmpathyLocalXmppAssistantWidget *)
137     object;
138   GtkWidget *hbox, *w;
139   GdkPixbuf *pix;
140   GtkWidget *account_widget;
141   EmpathyAccountWidget *widget_object;
142   gchar *markup;
143
144   G_OBJECT_CLASS (empathy_local_xmpp_assistant_widget_parent_class)->
145     constructed (object);
146
147   gtk_container_set_border_width (GTK_CONTAINER (self), 12);
148
149   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
150   gtk_box_pack_start (GTK_BOX (self), hbox, TRUE, TRUE, 0);
151   gtk_widget_show (hbox);
152
153   w = gtk_label_new (NULL);
154   markup = g_strdup_printf ("%s (<span style=\"italic\">%s</span>).",
155       _("Empathy can automatically discover and chat with the people "
156         "connected on the same network as you. "
157         "If you want to use this feature, please check that the "
158         "details below are correct. "
159         "You can easily change these details later or disable this feature "
160         "by using the 'Accounts' dialog"),
161       _("Edit->Accounts"));
162   gtk_label_set_markup (GTK_LABEL (w), markup);
163   g_free (markup);
164   gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
165   gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
166   gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
167   gtk_widget_show (w);
168
169   pix = empathy_pixbuf_from_icon_name_sized ("im-local-xmpp", 80);
170   w = gtk_image_new_from_pixbuf (pix);
171   gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 6);
172   gtk_widget_show (w);
173
174   g_object_unref (pix);
175
176   self->priv->settings = create_salut_account_settings ();
177
178   widget_object = empathy_account_widget_new_for_protocol (self->priv->settings,
179       TRUE);
180   empathy_account_widget_hide_buttons (widget_object);
181
182   account_widget = empathy_account_widget_get_widget (widget_object);
183
184   g_signal_connect (widget_object, "handle-apply",
185       G_CALLBACK (handle_apply_cb), self);
186
187   gtk_box_pack_start (GTK_BOX (self), account_widget,
188       FALSE, FALSE, 0);
189   gtk_widget_show (account_widget);
190 }
191
192 static void
193 empathy_local_xmpp_assistant_widget_dispose (GObject *object)
194 {
195   EmpathyLocalXmppAssistantWidget *self = (EmpathyLocalXmppAssistantWidget *)
196     object;
197
198   g_clear_object (&self->priv->settings);
199
200   G_OBJECT_CLASS (empathy_local_xmpp_assistant_widget_parent_class)->
201     dispose (object);
202 }
203
204 static void
205 empathy_local_xmpp_assistant_widget_class_init (
206     EmpathyLocalXmppAssistantWidgetClass *klass)
207 {
208   GObjectClass *object_class = G_OBJECT_CLASS (klass);
209
210   object_class->constructed = empathy_local_xmpp_assistant_widget_constructed;
211   object_class->dispose = empathy_local_xmpp_assistant_widget_dispose;
212
213   signals[SIG_VALID] =
214       g_signal_new ("valid",
215           G_TYPE_FROM_CLASS (klass),
216           G_SIGNAL_RUN_LAST, 0, NULL, NULL,
217           g_cclosure_marshal_generic,
218           G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
219
220   g_type_class_add_private (object_class,
221       sizeof (EmpathyLocalXmppAssistantWidgetPrivate));
222 }
223
224 GtkWidget *
225 empathy_local_xmpp_assistant_widget_new ()
226 {
227   return g_object_new (EMPATHY_TYPE_LOCAL_XMPP_ASSISTANT_WIDGET,
228       "orientation", GTK_ORIENTATION_VERTICAL,
229       NULL);
230 }
231
232 static void
233 account_enabled_cb (GObject *source,
234     GAsyncResult *result,
235     gpointer user_data)
236 {
237   TpAccount *account = TP_ACCOUNT (source);
238   GError *error = NULL;
239   TpAccountManager *account_mgr;
240
241   if (!tp_account_set_enabled_finish (account, result, &error))
242     {
243       DEBUG ("Failed to enable account: %s", error->message);
244       g_error_free (error);
245       return;
246     }
247
248   account_mgr = tp_account_manager_dup ();
249
250   empathy_connect_new_account (account, account_mgr);
251
252   g_object_unref (account_mgr);
253 }
254
255 static void
256 apply_account_cb (GObject *source,
257     GAsyncResult *result,
258     gpointer user_data)
259 {
260   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (source);
261   TpAccount *account;
262   GError *error = NULL;
263
264   if (!empathy_account_settings_apply_finish (settings, result, NULL, &error))
265     {
266       DEBUG ("Failed to create account: %s", error->message);
267       g_error_free (error);
268       return;
269     }
270
271   /* enable the newly created account */
272   account = empathy_account_settings_get_account (settings);
273   tp_account_set_enabled_async (account, TRUE, account_enabled_cb, NULL);
274 }
275
276 void
277 empathy_local_xmpp_assistant_widget_create_account (
278     EmpathyLocalXmppAssistantWidget *self)
279 {
280   empathy_account_settings_apply_async (self->priv->settings,
281       apply_account_cb, NULL);
282 }
283
284 gboolean
285 empathy_local_xmpp_assistant_widget_should_create_account (
286     TpAccountManager *manager)
287 {
288   gboolean salut_created = FALSE;
289   GList *accounts, *l;
290
291   accounts = tp_account_manager_get_valid_accounts (manager);
292
293   for (l = accounts; l != NULL;  l = g_list_next (l))
294     {
295       TpAccount *account = TP_ACCOUNT (l->data);
296
297       if (!tp_strdiff (tp_account_get_protocol (account), "local-xmpp"))
298         {
299           salut_created = TRUE;
300           break;
301         }
302     }
303
304   g_list_free (accounts);
305
306   return !salut_created;
307 }