2 * empathy-local-xmpp-assistant-widget.h - Source for
3 * EmpathyLocalXmppAssistantWidget
5 * Copyright (C) 2012 - Collabora Ltd.
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.
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.
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/>.
22 #include "empathy-local-xmpp-assistant-widget.h"
24 #include <glib/gi18n-lib.h>
27 #include <libebook/e-book.h>
30 #include <libempathy/empathy-utils.h>
32 #include <libempathy-gtk/empathy-account-widget.h>
33 #include <libempathy-gtk/empathy-ui-utils.h>
35 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
36 #include <libempathy/empathy-debug.h>
38 G_DEFINE_TYPE (EmpathyLocalXmppAssistantWidget,
39 empathy_local_xmpp_assistant_widget, GTK_TYPE_GRID)
46 static gulong signals[LAST_SIGNAL] = { 0, };
48 struct _EmpathyLocalXmppAssistantWidgetPrivate
50 EmpathyAccountSettings *settings;
54 empathy_local_xmpp_assistant_widget_init (EmpathyLocalXmppAssistantWidget *self)
56 self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self),
57 EMPATHY_TYPE_LOCAL_XMPP_ASSISTANT_WIDGET,
58 EmpathyLocalXmppAssistantWidgetPrivate);
61 static EmpathyAccountSettings *
62 create_salut_account_settings (void)
64 EmpathyAccountSettings *settings;
68 gchar *nickname = NULL;
69 gchar *first_name = NULL;
70 gchar *last_name = NULL;
76 settings = empathy_account_settings_new ("salut", "local-xmpp", NULL,
80 /* Get self EContact from EDS */
81 if (!e_book_get_self (&contact, &book, &error))
83 DEBUG ("Failed to get self econtact: %s", error->message);
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);
94 if (!tp_strdiff (nickname, "nickname"))
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);
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 : "");
118 g_object_unref (contact);
119 g_object_unref (book);
126 handle_apply_cb (EmpathyAccountWidget *widget_object,
128 EmpathyLocalXmppAssistantWidget *self)
130 g_signal_emit (self, signals[SIG_VALID], 0, is_valid);
134 empathy_local_xmpp_assistant_widget_constructed (GObject *object)
136 EmpathyLocalXmppAssistantWidget *self = (EmpathyLocalXmppAssistantWidget *)
140 EmpathyAccountWidget *account_widget;
143 G_OBJECT_CLASS (empathy_local_xmpp_assistant_widget_parent_class)->
144 constructed (object);
146 gtk_container_set_border_width (GTK_CONTAINER (self), 12);
149 _("Empathy can automatically discover and chat with the people "
150 "connected on the same network as you. "
151 "If you want to use this feature, please check that the "
152 "details below are correct."));
153 gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
154 gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
155 gtk_grid_attach (GTK_GRID (self), w, 0, 0, 1, 1);
158 pix = empathy_pixbuf_from_icon_name_sized ("im-local-xmpp", 48);
159 w = gtk_image_new_from_pixbuf (pix);
160 gtk_grid_attach (GTK_GRID (self), w, 1, 0, 1, 1);
163 g_object_unref (pix);
165 self->priv->settings = create_salut_account_settings ();
167 account_widget = empathy_account_widget_new_for_protocol (
168 self->priv->settings, TRUE);
169 empathy_account_widget_hide_buttons (account_widget);
171 g_signal_connect (account_widget, "handle-apply",
172 G_CALLBACK (handle_apply_cb), self);
174 gtk_grid_attach (GTK_GRID (self), GTK_WIDGET (account_widget), 0, 1, 2, 1);
175 gtk_widget_show (GTK_WIDGET (account_widget));
177 w = gtk_label_new (NULL);
178 markup = g_strdup_printf (
179 "<span size=\"small\">%s</span>",
180 _("You can change these details later or disable this feature "
181 "by choosing <span style=\"italic\">Edit → Accounts</span> "
182 "in the Contact List."));
183 gtk_label_set_markup (GTK_LABEL (w), markup);
185 gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
186 gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
187 gtk_grid_attach (GTK_GRID (self), w, 0, 2, 2, 1);
192 empathy_local_xmpp_assistant_widget_dispose (GObject *object)
194 EmpathyLocalXmppAssistantWidget *self = (EmpathyLocalXmppAssistantWidget *)
197 g_clear_object (&self->priv->settings);
199 G_OBJECT_CLASS (empathy_local_xmpp_assistant_widget_parent_class)->
204 empathy_local_xmpp_assistant_widget_class_init (
205 EmpathyLocalXmppAssistantWidgetClass *klass)
207 GObjectClass *object_class = G_OBJECT_CLASS (klass);
209 object_class->constructed = empathy_local_xmpp_assistant_widget_constructed;
210 object_class->dispose = empathy_local_xmpp_assistant_widget_dispose;
213 g_signal_new ("valid",
214 G_TYPE_FROM_CLASS (klass),
215 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
216 g_cclosure_marshal_generic,
217 G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
219 g_type_class_add_private (object_class,
220 sizeof (EmpathyLocalXmppAssistantWidgetPrivate));
224 empathy_local_xmpp_assistant_widget_new ()
226 return g_object_new (EMPATHY_TYPE_LOCAL_XMPP_ASSISTANT_WIDGET,
232 account_enabled_cb (GObject *source,
233 GAsyncResult *result,
236 TpAccount *account = TP_ACCOUNT (source);
237 GError *error = NULL;
238 TpAccountManager *account_mgr;
240 if (!tp_account_set_enabled_finish (account, result, &error))
242 DEBUG ("Failed to enable account: %s", error->message);
243 g_error_free (error);
247 account_mgr = tp_account_manager_dup ();
249 empathy_connect_new_account (account, account_mgr);
251 g_object_unref (account_mgr);
255 apply_account_cb (GObject *source,
256 GAsyncResult *result,
259 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (source);
261 GError *error = NULL;
263 if (!empathy_account_settings_apply_finish (settings, result, NULL, &error))
265 DEBUG ("Failed to create account: %s", error->message);
266 g_error_free (error);
270 /* enable the newly created account */
271 account = empathy_account_settings_get_account (settings);
272 tp_account_set_enabled_async (account, TRUE, account_enabled_cb, NULL);
276 empathy_local_xmpp_assistant_widget_create_account (
277 EmpathyLocalXmppAssistantWidget *self)
279 empathy_account_settings_apply_async (self->priv->settings,
280 apply_account_cb, NULL);
284 empathy_local_xmpp_assistant_widget_should_create_account (
285 TpAccountManager *manager)
287 gboolean salut_created = FALSE;
290 accounts = tp_account_manager_get_valid_accounts (manager);
292 for (l = accounts; l != NULL; l = g_list_next (l))
294 TpAccount *account = TP_ACCOUNT (l->data);
296 if (!tp_strdiff (tp_account_get_protocol (account), "local-xmpp"))
298 salut_created = TRUE;
303 g_list_free (accounts);
305 return !salut_created;
309 empathy_local_xmpp_assistant_widget_is_valid (
310 EmpathyLocalXmppAssistantWidget *self)
312 return empathy_account_settings_is_valid (self->priv->settings);