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>
25 #include <tp-account-widgets/tpaw-account-widget.h>
26 #include <tp-account-widgets/tpaw-pixbuf-utils.h>
27 #include <tp-account-widgets/tpaw-utils.h>
29 #include "empathy-ui-utils.h"
30 #include "empathy-utils.h"
32 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
33 #include "empathy-debug.h"
35 G_DEFINE_TYPE (EmpathyLocalXmppAssistantWidget
,
36 empathy_local_xmpp_assistant_widget
, GTK_TYPE_GRID
)
43 static gulong signals
[LAST_SIGNAL
] = { 0, };
45 struct _EmpathyLocalXmppAssistantWidgetPrivate
47 TpawAccountSettings
*settings
;
51 empathy_local_xmpp_assistant_widget_init (EmpathyLocalXmppAssistantWidget
*self
)
53 self
->priv
= G_TYPE_INSTANCE_GET_PRIVATE ((self
),
54 EMPATHY_TYPE_LOCAL_XMPP_ASSISTANT_WIDGET
,
55 EmpathyLocalXmppAssistantWidgetPrivate
);
59 handle_apply_cb (TpawAccountWidget
*widget_object
,
61 EmpathyLocalXmppAssistantWidget
*self
)
63 g_signal_emit (self
, signals
[SIG_VALID
], 0, is_valid
);
67 empathy_local_xmpp_assistant_widget_constructed (GObject
*object
)
69 EmpathyLocalXmppAssistantWidget
*self
= (EmpathyLocalXmppAssistantWidget
*)
73 TpawAccountWidget
*account_widget
;
76 G_OBJECT_CLASS (empathy_local_xmpp_assistant_widget_parent_class
)->
79 gtk_container_set_border_width (GTK_CONTAINER (self
), 12);
82 _("Empathy can automatically discover and chat with the people "
83 "connected on the same network as you. "
84 "If you want to use this feature, please check that the "
85 "details below are correct."));
86 gtk_misc_set_alignment (GTK_MISC (w
), 0, 0.5);
87 gtk_label_set_line_wrap (GTK_LABEL (w
), TRUE
);
88 gtk_grid_attach (GTK_GRID (self
), w
, 0, 0, 1, 1);
91 pix
= tpaw_pixbuf_from_icon_name_sized ("im-local-xmpp", 48);
94 w
= gtk_image_new_from_pixbuf (pix
);
95 gtk_grid_attach (GTK_GRID (self
), w
, 1, 0, 1, 1);
101 self
->priv
->settings
= tpaw_account_settings_new ("salut", "local-xmpp",
102 NULL
, _("People nearby"));
104 account_widget
= tpaw_account_widget_new_for_protocol (
105 self
->priv
->settings
, NULL
, TRUE
);
106 tpaw_account_widget_hide_buttons (account_widget
);
108 g_signal_connect (account_widget
, "handle-apply",
109 G_CALLBACK (handle_apply_cb
), self
);
111 gtk_grid_attach (GTK_GRID (self
), GTK_WIDGET (account_widget
), 0, 1, 2, 1);
112 gtk_widget_show (GTK_WIDGET (account_widget
));
114 w
= gtk_label_new (NULL
);
115 markup
= g_strdup_printf (
116 "<span size=\"small\">%s</span>",
117 _("You can change these details later or disable this feature "
118 "by choosing <span style=\"italic\">Edit → Accounts</span> "
119 "in the Contact List."));
120 gtk_label_set_markup (GTK_LABEL (w
), markup
);
122 gtk_misc_set_alignment (GTK_MISC (w
), 0, 0.5);
123 gtk_label_set_line_wrap (GTK_LABEL (w
), TRUE
);
124 gtk_grid_attach (GTK_GRID (self
), w
, 0, 2, 2, 1);
129 empathy_local_xmpp_assistant_widget_dispose (GObject
*object
)
131 EmpathyLocalXmppAssistantWidget
*self
= (EmpathyLocalXmppAssistantWidget
*)
134 g_clear_object (&self
->priv
->settings
);
136 G_OBJECT_CLASS (empathy_local_xmpp_assistant_widget_parent_class
)->
141 empathy_local_xmpp_assistant_widget_class_init (
142 EmpathyLocalXmppAssistantWidgetClass
*klass
)
144 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
146 object_class
->constructed
= empathy_local_xmpp_assistant_widget_constructed
;
147 object_class
->dispose
= empathy_local_xmpp_assistant_widget_dispose
;
150 g_signal_new ("valid",
151 G_TYPE_FROM_CLASS (klass
),
152 G_SIGNAL_RUN_LAST
, 0, NULL
, NULL
,
153 g_cclosure_marshal_generic
,
154 G_TYPE_NONE
, 1, G_TYPE_BOOLEAN
);
156 g_type_class_add_private (object_class
,
157 sizeof (EmpathyLocalXmppAssistantWidgetPrivate
));
161 empathy_local_xmpp_assistant_widget_new ()
163 return g_object_new (EMPATHY_TYPE_LOCAL_XMPP_ASSISTANT_WIDGET
,
169 account_enabled_cb (GObject
*source
,
170 GAsyncResult
*result
,
173 TpAccount
*account
= TP_ACCOUNT (source
);
174 GError
*error
= NULL
;
175 TpAccountManager
*account_mgr
;
177 if (!tp_account_set_enabled_finish (account
, result
, &error
))
179 DEBUG ("Failed to enable account: %s", error
->message
);
180 g_error_free (error
);
184 account_mgr
= tp_account_manager_dup ();
186 tpaw_connect_new_account (account
, account_mgr
);
188 g_object_unref (account_mgr
);
192 apply_account_cb (GObject
*source
,
193 GAsyncResult
*result
,
196 TpawAccountSettings
*settings
= TPAW_ACCOUNT_SETTINGS (source
);
198 GError
*error
= NULL
;
200 if (!tpaw_account_settings_apply_finish (settings
, result
, NULL
, &error
))
202 DEBUG ("Failed to create account: %s", error
->message
);
203 g_error_free (error
);
207 /* enable the newly created account */
208 account
= tpaw_account_settings_get_account (settings
);
209 tp_account_set_enabled_async (account
, TRUE
, account_enabled_cb
, NULL
);
213 empathy_local_xmpp_assistant_widget_create_account (
214 EmpathyLocalXmppAssistantWidget
*self
)
216 tpaw_account_settings_apply_async (self
->priv
->settings
,
217 apply_account_cb
, NULL
);
221 empathy_local_xmpp_assistant_widget_should_create_account (
222 TpAccountManager
*manager
)
224 gboolean salut_created
= FALSE
;
227 accounts
= tp_account_manager_dup_valid_accounts (manager
);
229 for (l
= accounts
; l
!= NULL
; l
= g_list_next (l
))
231 TpAccount
*account
= TP_ACCOUNT (l
->data
);
233 if (!tp_strdiff (tp_account_get_protocol_name (account
), "local-xmpp"))
235 salut_created
= TRUE
;
240 g_list_free_full (accounts
, g_object_unref
);
242 return !salut_created
;
246 empathy_local_xmpp_assistant_widget_is_valid (
247 EmpathyLocalXmppAssistantWidget
*self
)
249 return tpaw_account_settings_is_valid (self
->priv
->settings
);