]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-local-xmpp-assistant-widget.c
Fix broken nl translation
[empathy.git] / libempathy-gtk / empathy-local-xmpp-assistant-widget.c
1 /*
2  * empathy-local-xmpp-assistant-widget.h - Source for
3  * EmpathyLocalXmppAssistantWidget
4  *
5  * Copyright (C) 2012 - Collabora Ltd.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #include "config.h"
22 #include "empathy-local-xmpp-assistant-widget.h"
23
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>
28
29 #include "empathy-ui-utils.h"
30 #include "empathy-utils.h"
31
32 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
33 #include "empathy-debug.h"
34
35 G_DEFINE_TYPE (EmpathyLocalXmppAssistantWidget,
36     empathy_local_xmpp_assistant_widget, GTK_TYPE_GRID)
37
38 enum {
39   SIG_VALID = 1,
40   LAST_SIGNAL
41 };
42
43 static gulong signals[LAST_SIGNAL] = { 0, };
44
45 struct _EmpathyLocalXmppAssistantWidgetPrivate
46 {
47   TpawAccountSettings  *settings;
48 };
49
50 static void
51 empathy_local_xmpp_assistant_widget_init (EmpathyLocalXmppAssistantWidget *self)
52 {
53   self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self),
54       EMPATHY_TYPE_LOCAL_XMPP_ASSISTANT_WIDGET,
55       EmpathyLocalXmppAssistantWidgetPrivate);
56 }
57
58 static void
59 handle_apply_cb (TpawAccountWidget *widget_object,
60     gboolean is_valid,
61     EmpathyLocalXmppAssistantWidget *self)
62 {
63   g_signal_emit (self, signals[SIG_VALID], 0, is_valid);
64 }
65
66 static void
67 empathy_local_xmpp_assistant_widget_constructed (GObject *object)
68 {
69   EmpathyLocalXmppAssistantWidget *self = (EmpathyLocalXmppAssistantWidget *)
70     object;
71   GtkWidget *w;
72   GdkPixbuf *pix;
73   TpawAccountWidget *account_widget;
74   gchar *markup;
75
76   G_OBJECT_CLASS (empathy_local_xmpp_assistant_widget_parent_class)->
77     constructed (object);
78
79   gtk_container_set_border_width (GTK_CONTAINER (self), 12);
80
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);
90
91   pix = tpaw_pixbuf_from_icon_name_sized ("im-local-xmpp", 48);
92   if (pix != NULL)
93     {
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);
97
98       g_object_unref (pix);
99     }
100
101   self->priv->settings = tpaw_account_settings_new ("salut", "local-xmpp",
102       NULL, _("People nearby"));
103
104   account_widget = tpaw_account_widget_new_for_protocol (
105       self->priv->settings, NULL, TRUE);
106   tpaw_account_widget_hide_buttons (account_widget);
107
108   g_signal_connect (account_widget, "handle-apply",
109       G_CALLBACK (handle_apply_cb), self);
110
111   gtk_grid_attach (GTK_GRID (self), GTK_WIDGET (account_widget), 0, 1, 2, 1);
112   gtk_widget_show (GTK_WIDGET (account_widget));
113
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);
126 }
127
128 static void
129 empathy_local_xmpp_assistant_widget_dispose (GObject *object)
130 {
131   EmpathyLocalXmppAssistantWidget *self = (EmpathyLocalXmppAssistantWidget *)
132     object;
133
134   g_clear_object (&self->priv->settings);
135
136   G_OBJECT_CLASS (empathy_local_xmpp_assistant_widget_parent_class)->
137     dispose (object);
138 }
139
140 static void
141 empathy_local_xmpp_assistant_widget_class_init (
142     EmpathyLocalXmppAssistantWidgetClass *klass)
143 {
144   GObjectClass *object_class = G_OBJECT_CLASS (klass);
145
146   object_class->constructed = empathy_local_xmpp_assistant_widget_constructed;
147   object_class->dispose = empathy_local_xmpp_assistant_widget_dispose;
148
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);
155
156   g_type_class_add_private (object_class,
157       sizeof (EmpathyLocalXmppAssistantWidgetPrivate));
158 }
159
160 GtkWidget *
161 empathy_local_xmpp_assistant_widget_new ()
162 {
163   return g_object_new (EMPATHY_TYPE_LOCAL_XMPP_ASSISTANT_WIDGET,
164       "row-spacing", 12,
165       NULL);
166 }
167
168 static void
169 account_enabled_cb (GObject *source,
170     GAsyncResult *result,
171     gpointer user_data)
172 {
173   TpAccount *account = TP_ACCOUNT (source);
174   GError *error = NULL;
175   TpAccountManager *account_mgr;
176
177   if (!tp_account_set_enabled_finish (account, result, &error))
178     {
179       DEBUG ("Failed to enable account: %s", error->message);
180       g_error_free (error);
181       return;
182     }
183
184   account_mgr = tp_account_manager_dup ();
185
186   tpaw_connect_new_account (account, account_mgr);
187
188   g_object_unref (account_mgr);
189 }
190
191 static void
192 apply_account_cb (GObject *source,
193     GAsyncResult *result,
194     gpointer user_data)
195 {
196   TpawAccountSettings *settings = TPAW_ACCOUNT_SETTINGS (source);
197   TpAccount *account;
198   GError *error = NULL;
199
200   if (!tpaw_account_settings_apply_finish (settings, result, NULL, &error))
201     {
202       DEBUG ("Failed to create account: %s", error->message);
203       g_error_free (error);
204       return;
205     }
206
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);
210 }
211
212 void
213 empathy_local_xmpp_assistant_widget_create_account (
214     EmpathyLocalXmppAssistantWidget *self)
215 {
216   tpaw_account_settings_apply_async (self->priv->settings,
217       apply_account_cb, NULL);
218 }
219
220 gboolean
221 empathy_local_xmpp_assistant_widget_should_create_account (
222     TpAccountManager *manager)
223 {
224   gboolean salut_created = FALSE;
225   GList *accounts, *l;
226
227   accounts = tp_account_manager_dup_valid_accounts (manager);
228
229   for (l = accounts; l != NULL;  l = g_list_next (l))
230     {
231       TpAccount *account = TP_ACCOUNT (l->data);
232
233       if (!tp_strdiff (tp_account_get_protocol_name (account), "local-xmpp"))
234         {
235           salut_created = TRUE;
236           break;
237         }
238     }
239
240   g_list_free_full (accounts, g_object_unref);
241
242   return !salut_created;
243 }
244
245 gboolean
246 empathy_local_xmpp_assistant_widget_is_valid (
247         EmpathyLocalXmppAssistantWidget *self)
248 {
249   return tpaw_account_settings_is_valid (self->priv->settings);
250 }