]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-local-xmpp-assistant-widget.c
Updated POTFILES.in
[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   w = gtk_image_new_from_pixbuf (pix);
92   gtk_grid_attach (GTK_GRID (self), w, 1, 0, 1, 1);
93   gtk_widget_show (w);
94
95   g_object_unref (pix);
96
97   self->priv->settings = empathy_account_settings_new ("salut", "local-xmpp",
98       NULL, _("People nearby"));
99
100   account_widget = empathy_account_widget_new_for_protocol (
101       self->priv->settings, TRUE);
102   empathy_account_widget_hide_buttons (account_widget);
103
104   g_signal_connect (account_widget, "handle-apply",
105       G_CALLBACK (handle_apply_cb), self);
106
107   gtk_grid_attach (GTK_GRID (self), GTK_WIDGET (account_widget), 0, 1, 2, 1);
108   gtk_widget_show (GTK_WIDGET (account_widget));
109
110   w = gtk_label_new (NULL);
111   markup = g_strdup_printf (
112       "<span size=\"small\">%s</span>",
113       _("You can change these details later or disable this feature "
114         "by choosing <span style=\"italic\">Edit → Accounts</span> "
115         "in the Contact List."));
116   gtk_label_set_markup (GTK_LABEL (w), markup);
117   g_free (markup);
118   gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
119   gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
120   gtk_grid_attach (GTK_GRID (self), w, 0, 2, 2, 1);
121   gtk_widget_show (w);
122 }
123
124 static void
125 empathy_local_xmpp_assistant_widget_dispose (GObject *object)
126 {
127   EmpathyLocalXmppAssistantWidget *self = (EmpathyLocalXmppAssistantWidget *)
128     object;
129
130   g_clear_object (&self->priv->settings);
131
132   G_OBJECT_CLASS (empathy_local_xmpp_assistant_widget_parent_class)->
133     dispose (object);
134 }
135
136 static void
137 empathy_local_xmpp_assistant_widget_class_init (
138     EmpathyLocalXmppAssistantWidgetClass *klass)
139 {
140   GObjectClass *object_class = G_OBJECT_CLASS (klass);
141
142   object_class->constructed = empathy_local_xmpp_assistant_widget_constructed;
143   object_class->dispose = empathy_local_xmpp_assistant_widget_dispose;
144
145   signals[SIG_VALID] =
146       g_signal_new ("valid",
147           G_TYPE_FROM_CLASS (klass),
148           G_SIGNAL_RUN_LAST, 0, NULL, NULL,
149           g_cclosure_marshal_generic,
150           G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
151
152   g_type_class_add_private (object_class,
153       sizeof (EmpathyLocalXmppAssistantWidgetPrivate));
154 }
155
156 GtkWidget *
157 empathy_local_xmpp_assistant_widget_new ()
158 {
159   return g_object_new (EMPATHY_TYPE_LOCAL_XMPP_ASSISTANT_WIDGET,
160       "row-spacing", 12,
161       NULL);
162 }
163
164 static void
165 account_enabled_cb (GObject *source,
166     GAsyncResult *result,
167     gpointer user_data)
168 {
169   TpAccount *account = TP_ACCOUNT (source);
170   GError *error = NULL;
171   TpAccountManager *account_mgr;
172
173   if (!tp_account_set_enabled_finish (account, result, &error))
174     {
175       DEBUG ("Failed to enable account: %s", error->message);
176       g_error_free (error);
177       return;
178     }
179
180   account_mgr = tp_account_manager_dup ();
181
182   empathy_connect_new_account (account, account_mgr);
183
184   g_object_unref (account_mgr);
185 }
186
187 static void
188 apply_account_cb (GObject *source,
189     GAsyncResult *result,
190     gpointer user_data)
191 {
192   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (source);
193   TpAccount *account;
194   GError *error = NULL;
195
196   if (!empathy_account_settings_apply_finish (settings, result, NULL, &error))
197     {
198       DEBUG ("Failed to create account: %s", error->message);
199       g_error_free (error);
200       return;
201     }
202
203   /* enable the newly created account */
204   account = empathy_account_settings_get_account (settings);
205   tp_account_set_enabled_async (account, TRUE, account_enabled_cb, NULL);
206 }
207
208 void
209 empathy_local_xmpp_assistant_widget_create_account (
210     EmpathyLocalXmppAssistantWidget *self)
211 {
212   empathy_account_settings_apply_async (self->priv->settings,
213       apply_account_cb, NULL);
214 }
215
216 gboolean
217 empathy_local_xmpp_assistant_widget_should_create_account (
218     TpAccountManager *manager)
219 {
220   gboolean salut_created = FALSE;
221   GList *accounts, *l;
222
223   accounts = tp_account_manager_get_valid_accounts (manager);
224
225   for (l = accounts; l != NULL;  l = g_list_next (l))
226     {
227       TpAccount *account = TP_ACCOUNT (l->data);
228
229       if (!tp_strdiff (tp_account_get_protocol_name (account), "local-xmpp"))
230         {
231           salut_created = TRUE;
232           break;
233         }
234     }
235
236   g_list_free (accounts);
237
238   return !salut_created;
239 }
240
241 gboolean
242 empathy_local_xmpp_assistant_widget_is_valid (
243         EmpathyLocalXmppAssistantWidget *self)
244 {
245   return empathy_account_settings_is_valid (self->priv->settings);
246 }