]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-local-xmpp-assistant-widget.c
b714fa499a421e0e99f660208eb222a403e36618
[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
26 #include <libempathy/empathy-utils.h>
27
28 #include <libempathy-gtk/empathy-account-widget.h>
29 #include <libempathy-gtk/empathy-ui-utils.h>
30
31 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
32 #include <libempathy/empathy-debug.h>
33
34 G_DEFINE_TYPE (EmpathyLocalXmppAssistantWidget,
35     empathy_local_xmpp_assistant_widget, GTK_TYPE_GRID)
36
37 enum {
38   SIG_VALID = 1,
39   LAST_SIGNAL
40 };
41
42 static gulong signals[LAST_SIGNAL] = { 0, };
43
44 struct _EmpathyLocalXmppAssistantWidgetPrivate
45 {
46   EmpathyAccountSettings  *settings;
47 };
48
49 static void
50 empathy_local_xmpp_assistant_widget_init (EmpathyLocalXmppAssistantWidget *self)
51 {
52   self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self),
53       EMPATHY_TYPE_LOCAL_XMPP_ASSISTANT_WIDGET,
54       EmpathyLocalXmppAssistantWidgetPrivate);
55 }
56
57 static void
58 handle_apply_cb (EmpathyAccountWidget *widget_object,
59     gboolean is_valid,
60     EmpathyLocalXmppAssistantWidget *self)
61 {
62   g_signal_emit (self, signals[SIG_VALID], 0, is_valid);
63 }
64
65 static void
66 empathy_local_xmpp_assistant_widget_constructed (GObject *object)
67 {
68   EmpathyLocalXmppAssistantWidget *self = (EmpathyLocalXmppAssistantWidget *)
69     object;
70   GtkWidget *w;
71   GdkPixbuf *pix;
72   EmpathyAccountWidget *account_widget;
73   gchar *markup;
74
75   G_OBJECT_CLASS (empathy_local_xmpp_assistant_widget_parent_class)->
76     constructed (object);
77
78   gtk_container_set_border_width (GTK_CONTAINER (self), 12);
79
80   w = gtk_label_new (
81       _("Empathy can automatically discover and chat with the people "
82         "connected on the same network as you. "
83         "If you want to use this feature, please check that the "
84         "details below are correct."));
85   gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
86   gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
87   gtk_grid_attach (GTK_GRID (self), w, 0, 0, 1, 1);
88   gtk_widget_show (w);
89
90   pix = empathy_pixbuf_from_icon_name_sized ("im-local-xmpp", 48);
91   if (pix != NULL)
92     {
93       w = gtk_image_new_from_pixbuf (pix);
94       gtk_grid_attach (GTK_GRID (self), w, 1, 0, 1, 1);
95       gtk_widget_show (w);
96
97       g_object_unref (pix);
98     }
99
100   self->priv->settings = empathy_account_settings_new ("salut", "local-xmpp",
101       NULL, _("People nearby"));
102
103   account_widget = empathy_account_widget_new_for_protocol (
104       self->priv->settings, TRUE);
105   empathy_account_widget_hide_buttons (account_widget);
106
107   g_signal_connect (account_widget, "handle-apply",
108       G_CALLBACK (handle_apply_cb), self);
109
110   gtk_grid_attach (GTK_GRID (self), GTK_WIDGET (account_widget), 0, 1, 2, 1);
111   gtk_widget_show (GTK_WIDGET (account_widget));
112
113   w = gtk_label_new (NULL);
114   markup = g_strdup_printf (
115       "<span size=\"small\">%s</span>",
116       _("You can change these details later or disable this feature "
117         "by choosing <span style=\"italic\">Edit → Accounts</span> "
118         "in the Contact List."));
119   gtk_label_set_markup (GTK_LABEL (w), markup);
120   g_free (markup);
121   gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
122   gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
123   gtk_grid_attach (GTK_GRID (self), w, 0, 2, 2, 1);
124   gtk_widget_show (w);
125 }
126
127 static void
128 empathy_local_xmpp_assistant_widget_dispose (GObject *object)
129 {
130   EmpathyLocalXmppAssistantWidget *self = (EmpathyLocalXmppAssistantWidget *)
131     object;
132
133   g_clear_object (&self->priv->settings);
134
135   G_OBJECT_CLASS (empathy_local_xmpp_assistant_widget_parent_class)->
136     dispose (object);
137 }
138
139 static void
140 empathy_local_xmpp_assistant_widget_class_init (
141     EmpathyLocalXmppAssistantWidgetClass *klass)
142 {
143   GObjectClass *object_class = G_OBJECT_CLASS (klass);
144
145   object_class->constructed = empathy_local_xmpp_assistant_widget_constructed;
146   object_class->dispose = empathy_local_xmpp_assistant_widget_dispose;
147
148   signals[SIG_VALID] =
149       g_signal_new ("valid",
150           G_TYPE_FROM_CLASS (klass),
151           G_SIGNAL_RUN_LAST, 0, NULL, NULL,
152           g_cclosure_marshal_generic,
153           G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
154
155   g_type_class_add_private (object_class,
156       sizeof (EmpathyLocalXmppAssistantWidgetPrivate));
157 }
158
159 GtkWidget *
160 empathy_local_xmpp_assistant_widget_new ()
161 {
162   return g_object_new (EMPATHY_TYPE_LOCAL_XMPP_ASSISTANT_WIDGET,
163       "row-spacing", 12,
164       NULL);
165 }
166
167 static void
168 account_enabled_cb (GObject *source,
169     GAsyncResult *result,
170     gpointer user_data)
171 {
172   TpAccount *account = TP_ACCOUNT (source);
173   GError *error = NULL;
174   TpAccountManager *account_mgr;
175
176   if (!tp_account_set_enabled_finish (account, result, &error))
177     {
178       DEBUG ("Failed to enable account: %s", error->message);
179       g_error_free (error);
180       return;
181     }
182
183   account_mgr = tp_account_manager_dup ();
184
185   empathy_connect_new_account (account, account_mgr);
186
187   g_object_unref (account_mgr);
188 }
189
190 static void
191 apply_account_cb (GObject *source,
192     GAsyncResult *result,
193     gpointer user_data)
194 {
195   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (source);
196   TpAccount *account;
197   GError *error = NULL;
198
199   if (!empathy_account_settings_apply_finish (settings, result, NULL, &error))
200     {
201       DEBUG ("Failed to create account: %s", error->message);
202       g_error_free (error);
203       return;
204     }
205
206   /* enable the newly created account */
207   account = empathy_account_settings_get_account (settings);
208   tp_account_set_enabled_async (account, TRUE, account_enabled_cb, NULL);
209 }
210
211 void
212 empathy_local_xmpp_assistant_widget_create_account (
213     EmpathyLocalXmppAssistantWidget *self)
214 {
215   empathy_account_settings_apply_async (self->priv->settings,
216       apply_account_cb, NULL);
217 }
218
219 gboolean
220 empathy_local_xmpp_assistant_widget_should_create_account (
221     TpAccountManager *manager)
222 {
223   gboolean salut_created = FALSE;
224   GList *accounts, *l;
225
226   accounts = tp_account_manager_dup_valid_accounts (manager);
227
228   for (l = accounts; l != NULL;  l = g_list_next (l))
229     {
230       TpAccount *account = TP_ACCOUNT (l->data);
231
232       if (!tp_strdiff (tp_account_get_protocol_name (account), "local-xmpp"))
233         {
234           salut_created = TRUE;
235           break;
236         }
237     }
238
239   g_list_free_full (accounts, g_object_unref);
240
241   return !salut_created;
242 }
243
244 gboolean
245 empathy_local_xmpp_assistant_widget_is_valid (
246         EmpathyLocalXmppAssistantWidget *self)
247 {
248   return empathy_account_settings_is_valid (self->priv->settings);
249 }