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-account-widget.h"
27 #include "empathy-ui-utils.h"
28 #include "empathy-utils.h"
30 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
31 #include "empathy-debug.h"
33 G_DEFINE_TYPE (EmpathyLocalXmppAssistantWidget,
34 empathy_local_xmpp_assistant_widget, GTK_TYPE_GRID)
41 static gulong signals[LAST_SIGNAL] = { 0, };
43 struct _EmpathyLocalXmppAssistantWidgetPrivate
45 EmpathyAccountSettings *settings;
49 empathy_local_xmpp_assistant_widget_init (EmpathyLocalXmppAssistantWidget *self)
51 self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self),
52 EMPATHY_TYPE_LOCAL_XMPP_ASSISTANT_WIDGET,
53 EmpathyLocalXmppAssistantWidgetPrivate);
57 handle_apply_cb (EmpathyAccountWidget *widget_object,
59 EmpathyLocalXmppAssistantWidget *self)
61 g_signal_emit (self, signals[SIG_VALID], 0, is_valid);
65 empathy_local_xmpp_assistant_widget_constructed (GObject *object)
67 EmpathyLocalXmppAssistantWidget *self = (EmpathyLocalXmppAssistantWidget *)
71 EmpathyAccountWidget *account_widget;
74 G_OBJECT_CLASS (empathy_local_xmpp_assistant_widget_parent_class)->
77 gtk_container_set_border_width (GTK_CONTAINER (self), 12);
80 _("Empathy can automatically discover and chat with the people "
81 "connected on the same network as you. "
82 "If you want to use this feature, please check that the "
83 "details below are correct."));
84 gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
85 gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
86 gtk_grid_attach (GTK_GRID (self), w, 0, 0, 1, 1);
89 pix = empathy_pixbuf_from_icon_name_sized ("im-local-xmpp", 48);
92 w = gtk_image_new_from_pixbuf (pix);
93 gtk_grid_attach (GTK_GRID (self), w, 1, 0, 1, 1);
99 self->priv->settings = empathy_account_settings_new ("salut", "local-xmpp",
100 NULL, _("People nearby"));
102 account_widget = empathy_account_widget_new_for_protocol (
103 self->priv->settings, TRUE);
104 empathy_account_widget_hide_buttons (account_widget);
106 g_signal_connect (account_widget, "handle-apply",
107 G_CALLBACK (handle_apply_cb), self);
109 gtk_grid_attach (GTK_GRID (self), GTK_WIDGET (account_widget), 0, 1, 2, 1);
110 gtk_widget_show (GTK_WIDGET (account_widget));
112 w = gtk_label_new (NULL);
113 markup = g_strdup_printf (
114 "<span size=\"small\">%s</span>",
115 _("You can change these details later or disable this feature "
116 "by choosing <span style=\"italic\">Edit → Accounts</span> "
117 "in the Contact List."));
118 gtk_label_set_markup (GTK_LABEL (w), markup);
120 gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
121 gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
122 gtk_grid_attach (GTK_GRID (self), w, 0, 2, 2, 1);
127 empathy_local_xmpp_assistant_widget_dispose (GObject *object)
129 EmpathyLocalXmppAssistantWidget *self = (EmpathyLocalXmppAssistantWidget *)
132 g_clear_object (&self->priv->settings);
134 G_OBJECT_CLASS (empathy_local_xmpp_assistant_widget_parent_class)->
139 empathy_local_xmpp_assistant_widget_class_init (
140 EmpathyLocalXmppAssistantWidgetClass *klass)
142 GObjectClass *object_class = G_OBJECT_CLASS (klass);
144 object_class->constructed = empathy_local_xmpp_assistant_widget_constructed;
145 object_class->dispose = empathy_local_xmpp_assistant_widget_dispose;
148 g_signal_new ("valid",
149 G_TYPE_FROM_CLASS (klass),
150 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
151 g_cclosure_marshal_generic,
152 G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
154 g_type_class_add_private (object_class,
155 sizeof (EmpathyLocalXmppAssistantWidgetPrivate));
159 empathy_local_xmpp_assistant_widget_new ()
161 return g_object_new (EMPATHY_TYPE_LOCAL_XMPP_ASSISTANT_WIDGET,
167 account_enabled_cb (GObject *source,
168 GAsyncResult *result,
171 TpAccount *account = TP_ACCOUNT (source);
172 GError *error = NULL;
173 TpAccountManager *account_mgr;
175 if (!tp_account_set_enabled_finish (account, result, &error))
177 DEBUG ("Failed to enable account: %s", error->message);
178 g_error_free (error);
182 account_mgr = tp_account_manager_dup ();
184 empathy_connect_new_account (account, account_mgr);
186 g_object_unref (account_mgr);
190 apply_account_cb (GObject *source,
191 GAsyncResult *result,
194 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (source);
196 GError *error = NULL;
198 if (!empathy_account_settings_apply_finish (settings, result, NULL, &error))
200 DEBUG ("Failed to create account: %s", error->message);
201 g_error_free (error);
205 /* enable the newly created account */
206 account = empathy_account_settings_get_account (settings);
207 tp_account_set_enabled_async (account, TRUE, account_enabled_cb, NULL);
211 empathy_local_xmpp_assistant_widget_create_account (
212 EmpathyLocalXmppAssistantWidget *self)
214 empathy_account_settings_apply_async (self->priv->settings,
215 apply_account_cb, NULL);
219 empathy_local_xmpp_assistant_widget_should_create_account (
220 TpAccountManager *manager)
222 gboolean salut_created = FALSE;
225 accounts = tp_account_manager_dup_valid_accounts (manager);
227 for (l = accounts; l != NULL; l = g_list_next (l))
229 TpAccount *account = TP_ACCOUNT (l->data);
231 if (!tp_strdiff (tp_account_get_protocol_name (account), "local-xmpp"))
233 salut_created = TRUE;
238 g_list_free_full (accounts, g_object_unref);
240 return !salut_created;
244 empathy_local_xmpp_assistant_widget_is_valid (
245 EmpathyLocalXmppAssistantWidget *self)
247 return empathy_account_settings_is_valid (self->priv->settings);