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