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>
26 #include "empathy-utils.h"
28 #include "empathy-account-widget.h"
29 #include "empathy-ui-utils.h"
31 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
32 #include "empathy-debug.h"
34 G_DEFINE_TYPE (EmpathyLocalXmppAssistantWidget,
35 empathy_local_xmpp_assistant_widget, GTK_TYPE_GRID)
42 static gulong signals[LAST_SIGNAL] = { 0, };
44 struct _EmpathyLocalXmppAssistantWidgetPrivate
46 EmpathyAccountSettings *settings;
50 empathy_local_xmpp_assistant_widget_init (EmpathyLocalXmppAssistantWidget *self)
52 self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self),
53 EMPATHY_TYPE_LOCAL_XMPP_ASSISTANT_WIDGET,
54 EmpathyLocalXmppAssistantWidgetPrivate);
58 handle_apply_cb (EmpathyAccountWidget *widget_object,
60 EmpathyLocalXmppAssistantWidget *self)
62 g_signal_emit (self, signals[SIG_VALID], 0, is_valid);
66 empathy_local_xmpp_assistant_widget_constructed (GObject *object)
68 EmpathyLocalXmppAssistantWidget *self = (EmpathyLocalXmppAssistantWidget *)
72 EmpathyAccountWidget *account_widget;
75 G_OBJECT_CLASS (empathy_local_xmpp_assistant_widget_parent_class)->
78 gtk_container_set_border_width (GTK_CONTAINER (self), 12);
81 _("Empathy can automatically discover and chat with the people "
82 "connected on the same network as you. "
83 "If you want to use this feature, please check that the "
84 "details below are correct."));
85 gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
86 gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
87 gtk_grid_attach (GTK_GRID (self), w, 0, 0, 1, 1);
90 pix = empathy_pixbuf_from_icon_name_sized ("im-local-xmpp", 48);
93 w = gtk_image_new_from_pixbuf (pix);
94 gtk_grid_attach (GTK_GRID (self), w, 1, 0, 1, 1);
100 self->priv->settings = empathy_account_settings_new ("salut", "local-xmpp",
101 NULL, _("People nearby"));
103 account_widget = empathy_account_widget_new_for_protocol (
104 self->priv->settings, TRUE);
105 empathy_account_widget_hide_buttons (account_widget);
107 g_signal_connect (account_widget, "handle-apply",
108 G_CALLBACK (handle_apply_cb), self);
110 gtk_grid_attach (GTK_GRID (self), GTK_WIDGET (account_widget), 0, 1, 2, 1);
111 gtk_widget_show (GTK_WIDGET (account_widget));
113 w = gtk_label_new (NULL);
114 markup = g_strdup_printf (
115 "<span size=\"small\">%s</span>",
116 _("You can change these details later or disable this feature "
117 "by choosing <span style=\"italic\">Edit → Accounts</span> "
118 "in the Contact List."));
119 gtk_label_set_markup (GTK_LABEL (w), markup);
121 gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
122 gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
123 gtk_grid_attach (GTK_GRID (self), w, 0, 2, 2, 1);
128 empathy_local_xmpp_assistant_widget_dispose (GObject *object)
130 EmpathyLocalXmppAssistantWidget *self = (EmpathyLocalXmppAssistantWidget *)
133 g_clear_object (&self->priv->settings);
135 G_OBJECT_CLASS (empathy_local_xmpp_assistant_widget_parent_class)->
140 empathy_local_xmpp_assistant_widget_class_init (
141 EmpathyLocalXmppAssistantWidgetClass *klass)
143 GObjectClass *object_class = G_OBJECT_CLASS (klass);
145 object_class->constructed = empathy_local_xmpp_assistant_widget_constructed;
146 object_class->dispose = empathy_local_xmpp_assistant_widget_dispose;
149 g_signal_new ("valid",
150 G_TYPE_FROM_CLASS (klass),
151 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
152 g_cclosure_marshal_generic,
153 G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
155 g_type_class_add_private (object_class,
156 sizeof (EmpathyLocalXmppAssistantWidgetPrivate));
160 empathy_local_xmpp_assistant_widget_new ()
162 return g_object_new (EMPATHY_TYPE_LOCAL_XMPP_ASSISTANT_WIDGET,
168 account_enabled_cb (GObject *source,
169 GAsyncResult *result,
172 TpAccount *account = TP_ACCOUNT (source);
173 GError *error = NULL;
174 TpAccountManager *account_mgr;
176 if (!tp_account_set_enabled_finish (account, result, &error))
178 DEBUG ("Failed to enable account: %s", error->message);
179 g_error_free (error);
183 account_mgr = tp_account_manager_dup ();
185 empathy_connect_new_account (account, account_mgr);
187 g_object_unref (account_mgr);
191 apply_account_cb (GObject *source,
192 GAsyncResult *result,
195 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (source);
197 GError *error = NULL;
199 if (!empathy_account_settings_apply_finish (settings, result, NULL, &error))
201 DEBUG ("Failed to create account: %s", error->message);
202 g_error_free (error);
206 /* enable the newly created account */
207 account = empathy_account_settings_get_account (settings);
208 tp_account_set_enabled_async (account, TRUE, account_enabled_cb, NULL);
212 empathy_local_xmpp_assistant_widget_create_account (
213 EmpathyLocalXmppAssistantWidget *self)
215 empathy_account_settings_apply_async (self->priv->settings,
216 apply_account_cb, NULL);
220 empathy_local_xmpp_assistant_widget_should_create_account (
221 TpAccountManager *manager)
223 gboolean salut_created = FALSE;
226 accounts = tp_account_manager_dup_valid_accounts (manager);
228 for (l = accounts; l != NULL; l = g_list_next (l))
230 TpAccount *account = TP_ACCOUNT (l->data);
232 if (!tp_strdiff (tp_account_get_protocol_name (account), "local-xmpp"))
234 salut_created = TRUE;
239 g_list_free_full (accounts, g_object_unref);
241 return !salut_created;
245 empathy_local_xmpp_assistant_widget_is_valid (
246 EmpathyLocalXmppAssistantWidget *self)
248 return empathy_account_settings_is_valid (self->priv->settings);