Update Greek translation
[empathy-mirror.git] / libempathy-gtk / empathy-local-xmpp-assistant-widget.c
blobff34b168c9250951a257c7e66e6f5f942e72447c
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_label_set_max_width_chars (GTK_LABEL (w), 60);
89 gtk_grid_attach (GTK_GRID (self), w, 0, 0, 1, 1);
90 gtk_widget_show (w);
92 pix = tpaw_pixbuf_from_icon_name_sized ("im-local-xmpp", 48);
93 if (pix != NULL)
95 w = gtk_image_new_from_pixbuf (pix);
96 gtk_grid_attach (GTK_GRID (self), w, 1, 0, 1, 1);
97 gtk_widget_show (w);
99 g_object_unref (pix);
102 self->priv->settings = tpaw_account_settings_new ("salut", "local-xmpp",
103 NULL, _("People nearby"));
105 account_widget = tpaw_account_widget_new_for_protocol (
106 self->priv->settings, NULL, TRUE);
107 tpaw_account_widget_hide_buttons (account_widget);
109 g_signal_connect (account_widget, "handle-apply",
110 G_CALLBACK (handle_apply_cb), self);
112 gtk_grid_attach (GTK_GRID (self), GTK_WIDGET (account_widget), 0, 1, 2, 1);
113 gtk_widget_show (GTK_WIDGET (account_widget));
115 w = gtk_label_new (NULL);
116 markup = g_strdup_printf (
117 "<span size=\"small\">%s</span>",
118 _("You can change these details later or disable this feature "
119 "by choosing <span style=\"italic\">Edit → Accounts</span> "
120 "in the Contact List."));
121 gtk_label_set_markup (GTK_LABEL (w), markup);
122 g_free (markup);
123 gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
124 gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
125 gtk_grid_attach (GTK_GRID (self), w, 0, 2, 2, 1);
126 gtk_widget_show (w);
129 static void
130 empathy_local_xmpp_assistant_widget_dispose (GObject *object)
132 EmpathyLocalXmppAssistantWidget *self = (EmpathyLocalXmppAssistantWidget *)
133 object;
135 g_clear_object (&self->priv->settings);
137 G_OBJECT_CLASS (empathy_local_xmpp_assistant_widget_parent_class)->
138 dispose (object);
141 static void
142 empathy_local_xmpp_assistant_widget_class_init (
143 EmpathyLocalXmppAssistantWidgetClass *klass)
145 GObjectClass *object_class = G_OBJECT_CLASS (klass);
147 object_class->constructed = empathy_local_xmpp_assistant_widget_constructed;
148 object_class->dispose = empathy_local_xmpp_assistant_widget_dispose;
150 signals[SIG_VALID] =
151 g_signal_new ("valid",
152 G_TYPE_FROM_CLASS (klass),
153 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
154 g_cclosure_marshal_generic,
155 G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
157 g_type_class_add_private (object_class,
158 sizeof (EmpathyLocalXmppAssistantWidgetPrivate));
161 GtkWidget *
162 empathy_local_xmpp_assistant_widget_new ()
164 return g_object_new (EMPATHY_TYPE_LOCAL_XMPP_ASSISTANT_WIDGET,
165 "row-spacing", 12,
166 NULL);
169 static void
170 account_enabled_cb (GObject *source,
171 GAsyncResult *result,
172 gpointer user_data)
174 TpAccount *account = TP_ACCOUNT (source);
175 GError *error = NULL;
176 TpAccountManager *account_mgr;
178 if (!tp_account_set_enabled_finish (account, result, &error))
180 DEBUG ("Failed to enable account: %s", error->message);
181 g_error_free (error);
182 return;
185 account_mgr = tp_account_manager_dup ();
187 tpaw_connect_new_account (account, account_mgr);
189 g_object_unref (account_mgr);
192 static void
193 apply_account_cb (GObject *source,
194 GAsyncResult *result,
195 gpointer user_data)
197 TpawAccountSettings *settings = TPAW_ACCOUNT_SETTINGS (source);
198 TpAccount *account;
199 GError *error = NULL;
201 if (!tpaw_account_settings_apply_finish (settings, result, NULL, &error))
203 DEBUG ("Failed to create account: %s", error->message);
204 g_error_free (error);
205 return;
208 /* enable the newly created account */
209 account = tpaw_account_settings_get_account (settings);
210 tp_account_set_enabled_async (account, TRUE, account_enabled_cb, NULL);
213 void
214 empathy_local_xmpp_assistant_widget_create_account (
215 EmpathyLocalXmppAssistantWidget *self)
217 tpaw_account_settings_apply_async (self->priv->settings,
218 apply_account_cb, NULL);
221 gboolean
222 empathy_local_xmpp_assistant_widget_should_create_account (
223 TpAccountManager *manager)
225 gboolean salut_created = FALSE;
226 GList *accounts, *l;
228 accounts = tp_account_manager_dup_valid_accounts (manager);
230 for (l = accounts; l != NULL; l = g_list_next (l))
232 TpAccount *account = TP_ACCOUNT (l->data);
234 if (!tp_strdiff (tp_account_get_protocol_name (account), "local-xmpp"))
236 salut_created = TRUE;
237 break;
241 g_list_free_full (accounts, g_object_unref);
243 return !salut_created;
246 gboolean
247 empathy_local_xmpp_assistant_widget_is_valid (
248 EmpathyLocalXmppAssistantWidget *self)
250 return tpaw_account_settings_is_valid (self->priv->settings);