1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
/*
* empathy-accounts-plugin-widget.c
*
* Copyright (C) 2012 Collabora Ltd. <http://www.collabora.co.uk/>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include "empathy-accounts-plugin-widget.h"
#include <glib/gi18n-lib.h>
#include <telepathy-glib/telepathy-glib.h>
#include <libaccounts-glib/ag-service.h>
#include <libaccounts-glib/ag-account-service.h>
#include "libempathy-gtk/empathy-account-widget.h"
G_DEFINE_TYPE (EmpathyAccountsPluginWidget, empathy_accounts_plugin_widget, GTK_TYPE_BOX)
enum
{
PROP_ACCOUNT = 1,
N_PROPS
};
enum
{
SIG_DONE,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL];
struct _EmpathyAccountsPluginWidgetPriv
{
AgAccount *account;
EmpathyAccountSettings *settings;
EmpathyAccountWidget *account_widget;
GtkWidget *done_button;
};
static void
empathy_accounts_plugin_widget_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
EmpathyAccountsPluginWidget *self = EMPATHY_ACCOUNTS_PLUGIN_WIDGET (object);
switch (property_id)
{
case PROP_ACCOUNT:
g_value_set_object (value, self->priv->account);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
empathy_accounts_plugin_widget_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
EmpathyAccountsPluginWidget *self = EMPATHY_ACCOUNTS_PLUGIN_WIDGET (object);
switch (property_id)
{
case PROP_ACCOUNT:
self->priv->account = g_value_dup_object (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static EmpathyAccountSettings *
create_account_settings (AgAccount *account)
{
AgService *service;
GList *services;
AgAccountService *account_service;
GVariant *v;
gchar *manager = NULL, *protocol = NULL;
EmpathyAccountSettings *settings;
g_assert (account->id == 0);
services = ag_account_list_services_by_type (account, "IM");
g_return_val_if_fail (services != NULL, NULL);
service = (AgService *) services->data;
account_service = ag_account_service_new (account, service);
v = ag_account_service_get_variant (account_service,
"telepathy/manager", NULL);
if (v != NULL)
manager = g_variant_dup_string (v, NULL);
v = ag_account_service_get_variant (account_service,
"telepathy/protocol", NULL);
if (v != NULL)
protocol = g_variant_dup_string (v, NULL);
g_return_val_if_fail (manager != NULL, NULL);
g_return_val_if_fail (protocol != NULL, NULL);
settings = empathy_account_settings_new (manager, protocol, NULL,
ag_service_get_display_name (service));
empathy_account_settings_set_storage_provider (settings,
EMPATHY_UOA_PROVIDER);
empathy_account_settings_set_icon_name_async (settings,
ag_service_get_icon_name (service), NULL, NULL);
g_free (manager);
g_free (protocol);
return settings;
}
static void
response_cb (GtkWidget *widget,
gint response,
EmpathyAccountsPluginWidget *self)
{
if (response == GTK_RESPONSE_OK)
{
empathy_account_widget_apply_and_log_in (self->priv->account_widget);
/* Rely on account_widget_close_cb to fire the 'done' signal */
}
else
{
empathy_account_widget_discard_pending_changes (
self->priv->account_widget);
g_signal_emit (self, signals[SIG_DONE], 0);
}
}
static GtkWidget *
create_top_bar (EmpathyAccountsPluginWidget *self)
{
GtkWidget *bar, *content, *action, *label;
GtkCssProvider *css;
GError *error = NULL;
bar = gtk_info_bar_new_with_buttons (
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
NULL);
self->priv->done_button = gtk_info_bar_add_button (GTK_INFO_BAR (bar),
_("Done"), GTK_RESPONSE_OK);
gtk_widget_set_hexpand (bar, TRUE);
gtk_info_bar_set_message_type (GTK_INFO_BAR (bar), GTK_MESSAGE_QUESTION);
action = gtk_info_bar_get_action_area (GTK_INFO_BAR (bar));
gtk_orientable_set_orientation (GTK_ORIENTABLE (action),
GTK_ORIENTATION_HORIZONTAL);
gtk_widget_set_name (bar, "authorization-infobar");
css = gtk_css_provider_new ();
if (gtk_css_provider_load_from_data (css,
"@define-color question_bg_color rgb (222, 222, 222);"
"GtkInfoBar#authorization-infobar"
"{"
" color: @fg_color;"
"}",
-1, &error))
{
GtkStyleContext *context = gtk_widget_get_style_context (bar);
gtk_style_context_add_provider (context, (GtkStyleProvider *) css,
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
}
else
{
g_warning ("Error processing CSS theme override: %s", error->message);
g_clear_error (&error);
}
g_object_unref (css);
content = gtk_info_bar_get_content_area (GTK_INFO_BAR (bar));
label = gtk_label_new (_("Please enter your account details"));
gtk_container_add (GTK_CONTAINER (content), label);
gtk_widget_show (label);
g_signal_connect (bar, "response",
G_CALLBACK (response_cb), self);
return bar;
}
static void
account_widget_handle_apply_cb (EmpathyAccountWidget *widget,
gboolean valid,
EmpathyAccountsPluginWidget *self)
{
gtk_widget_set_sensitive (self->priv->done_button, valid);
}
static void
account_widget_close_cb (EmpathyAccountWidget *widget,
GtkResponseType response,
EmpathyAccountsPluginWidget *self)
{
g_signal_emit (self, signals[SIG_DONE], 0);
}
static void
add_account_widget (EmpathyAccountsPluginWidget *self)
{
GtkWidget *alig;
gboolean simple;
alig = gtk_alignment_new (0.5, 0, 0, 0);
gtk_box_pack_start (GTK_BOX (self), alig, TRUE, TRUE, 0);
gtk_widget_show (GTK_WIDGET (alig));
/* Use the simple widget only when creating the account */
simple = (self->priv->account->id == 0);
self->priv->account_widget = empathy_account_widget_new_for_protocol (
self->priv->settings, simple);
empathy_account_widget_hide_buttons (self->priv->account_widget);
gtk_widget_set_valign (GTK_WIDGET (self->priv->account_widget),
GTK_ALIGN_CENTER);
gtk_container_add (GTK_CONTAINER (alig),
GTK_WIDGET (self->priv->account_widget));
gtk_widget_show (GTK_WIDGET (self->priv->account_widget));
if (!empathy_account_settings_is_valid (self->priv->settings))
{
gtk_widget_set_sensitive (self->priv->done_button, FALSE);
}
g_signal_connect (self->priv->account_widget, "handle-apply",
G_CALLBACK (account_widget_handle_apply_cb), self);
g_signal_connect (self->priv->account_widget, "close",
G_CALLBACK (account_widget_close_cb), self);
}
static void
maybe_add_account_widget (EmpathyAccountsPluginWidget *self)
{
g_return_if_fail (self->priv->settings != NULL);
if (empathy_account_settings_is_ready (self->priv->settings))
{
add_account_widget (self);
}
else
{
g_signal_connect_swapped (self->priv->settings, "notify::ready",
G_CALLBACK (add_account_widget), self);
}
}
static void
manager_prepared_cb (GObject *source,
GAsyncResult *result,
gpointer user_data)
{
TpWeakRef *wr = user_data;
EmpathyAccountsPluginWidget *self = tp_weak_ref_dup_object (wr);
TpAccountManager *manager = (TpAccountManager *) source;
GList *accounts;
GError *error = NULL;
if (self == NULL)
{
tp_weak_ref_destroy (wr);
return;
}
if (!tp_proxy_prepare_finish (manager, result, &error))
{
g_debug ("Error preparing Account Manager: %s", error->message);
g_clear_error (&error);
goto out;
}
accounts = tp_account_manager_dup_valid_accounts (manager);
while (accounts != NULL)
{
TpAccount *account = accounts->data;
const GValue *value;
value = tp_account_get_storage_identifier (account);
if (G_VALUE_HOLDS_UINT (value) &&
g_value_get_uint (value) == self->priv->account->id)
{
self->priv->settings = empathy_account_settings_new_for_account (
account);
maybe_add_account_widget (self);
break;
}
accounts = g_list_delete_link (accounts, accounts);
}
g_list_free_full (accounts, g_object_unref);
out:
tp_weak_ref_destroy (wr);
g_object_unref (self);
}
static void
empathy_accounts_plugin_widget_constructed (GObject *object)
{
EmpathyAccountsPluginWidget *self = EMPATHY_ACCOUNTS_PLUGIN_WIDGET (object);
void (*chain_up) (GObject *) =
((GObjectClass *) empathy_accounts_plugin_widget_parent_class)->constructed;
GtkWidget *top;
if (chain_up != NULL)
chain_up (object);
g_return_if_fail (AG_IS_ACCOUNT (self->priv->account));
/* Top bar */
top = create_top_bar (self);
gtk_widget_show (top);
gtk_box_pack_start (GTK_BOX (self), top, FALSE, FALSE, 0);
if (self->priv->account->id != 0)
{
TpAccountManager *manager;
/* Prepare tp's account manager to find the TpAccount corresponding to our
* AgAccount */
manager = tp_account_manager_dup ();
tp_proxy_prepare_async (manager, NULL,
manager_prepared_cb, tp_weak_ref_new (self, NULL, NULL));
g_object_unref (manager);
return;
}
self->priv->settings = create_account_settings (self->priv->account);
maybe_add_account_widget (self);
}
static void
empathy_accounts_plugin_widget_dispose (GObject *object)
{
EmpathyAccountsPluginWidget *self = EMPATHY_ACCOUNTS_PLUGIN_WIDGET (object);
void (*chain_up) (GObject *) =
((GObjectClass *) empathy_accounts_plugin_widget_parent_class)->dispose;
g_clear_object (&self->priv->account);
g_clear_object (&self->priv->settings);
if (chain_up != NULL)
chain_up (object);
}
static void
empathy_accounts_plugin_widget_class_init (
EmpathyAccountsPluginWidgetClass *klass)
{
GObjectClass *oclass = G_OBJECT_CLASS (klass);
GParamSpec *spec;
oclass->get_property = empathy_accounts_plugin_widget_get_property;
oclass->set_property = empathy_accounts_plugin_widget_set_property;
oclass->constructed = empathy_accounts_plugin_widget_constructed;
oclass->dispose = empathy_accounts_plugin_widget_dispose;
spec = g_param_spec_object ("account", "account",
"AgAccount",
AG_TYPE_ACCOUNT,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (oclass, PROP_ACCOUNT, spec);
signals[SIG_DONE] = g_signal_new ("done",
G_OBJECT_CLASS_TYPE (klass),
G_SIGNAL_RUN_LAST,
0, NULL, NULL, NULL,
G_TYPE_NONE,
0);
g_type_class_add_private (klass, sizeof (EmpathyAccountsPluginWidgetPriv));
}
static void
empathy_accounts_plugin_widget_init (EmpathyAccountsPluginWidget *self)
{
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
EMPATHY_TYPE_ACCOUNTS_PLUGIN_WIDGET, EmpathyAccountsPluginWidgetPriv);
}
GtkWidget *
empathy_accounts_plugin_widget_new (AgAccount *account)
{
return g_object_new (EMPATHY_TYPE_ACCOUNTS_PLUGIN_WIDGET,
"account", account,
"orientation", GTK_ORIENTATION_VERTICAL,
"spacing", 10,
NULL);
}
|