Updated Indonesian translation
[empathy-mirror.git] / libempathy-gtk / empathy-local-xmpp-assistant-widget.c
blob6ee8711a3278d306b27cb34dcdf4db51683bcab5
1 /*
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/>.
21 #include "config.h"
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)
38 enum {
39 SIG_VALID = 1,
40 LAST_SIGNAL
43 static gulong signals[LAST_SIGNAL] = { 0, };
45 struct _EmpathyLocalXmppAssistantWidgetPrivate
47 TpawAccountSettings *settings;
50 static void
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);
58 static void
59 handle_apply_cb (TpawAccountWidget *widget_object,
60 gboolean is_valid,
61 EmpathyLocalXmppAssistantWidget *self)
63 g_signal_emit (self, signals[SIG_VALID], 0, is_valid);
66 static void
67 empathy_local_xmpp_assistant_widget_constructed (GObject *object)
69 EmpathyLocalXmppAssistantWidget *self = (EmpathyLocalXmppAssistantWidget *)
70 object;
71 GtkWidget *w;
72 GdkPixbuf *pix;
73 TpawAccountWidget *account_widget;
74 gchar *markup;
76 G_OBJECT_CLASS (empathy_local_xmpp_assistant_widget_parent_class)->
77 constructed (object);
79 gtk_container_set_border_width (GTK_CONTAINER (self), 12);
81 w = gtk_label_new (
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);
89 gtk_widget_show (w);
91 pix = tpaw_pixbuf_from_icon_name_sized ("im-local-xmpp", 48);
92 if (pix != NULL)
94 w = gtk_image_new_from_pixbuf (pix);
95 gtk_grid_attach (GTK_GRID (self), w, 1, 0, 1, 1);
96 gtk_widget_show (w);
98 g_object_unref (pix);
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);
121 g_free (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);
125 gtk_widget_show (w);
128 static void
129 empathy_local_xmpp_assistant_widget_dispose (GObject *object)
131 EmpathyLocalXmppAssistantWidget *self = (EmpathyLocalXmppAssistantWidget *)
132 object;
134 g_clear_object (&self->priv->settings);
136 G_OBJECT_CLASS (empathy_local_xmpp_assistant_widget_parent_class)->
137 dispose (object);
140 static void
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;
149 signals[SIG_VALID] =
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));
160 GtkWidget *
161 empathy_local_xmpp_assistant_widget_new ()
163 return g_object_new (EMPATHY_TYPE_LOCAL_XMPP_ASSISTANT_WIDGET,
164 "row-spacing", 12,
165 NULL);
168 static void
169 account_enabled_cb (GObject *source,
170 GAsyncResult *result,
171 gpointer user_data)
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);
181 return;
184 account_mgr = tp_account_manager_dup ();
186 tpaw_connect_new_account (account, account_mgr);
188 g_object_unref (account_mgr);
191 static void
192 apply_account_cb (GObject *source,
193 GAsyncResult *result,
194 gpointer user_data)
196 TpawAccountSettings *settings = TPAW_ACCOUNT_SETTINGS (source);
197 TpAccount *account;
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);
204 return;
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);
212 void
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);
220 gboolean
221 empathy_local_xmpp_assistant_widget_should_create_account (
222 TpAccountManager *manager)
224 gboolean salut_created = FALSE;
225 GList *accounts, *l;
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;
236 break;
240 g_list_free_full (accounts, g_object_unref);
242 return !salut_created;
245 gboolean
246 empathy_local_xmpp_assistant_widget_is_valid (
247 EmpathyLocalXmppAssistantWidget *self)
249 return tpaw_account_settings_is_valid (self->priv->settings);