]> git.0d.be Git - empathy.git/blob - libempathy/empathy-account-manager.c
Yet more style fixes.
[empathy.git] / libempathy / empathy-account-manager.c
1 /*
2  * Copyright (C) 2008 Collabora Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  * 
18  * Authors: Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
19  */
20
21 #include "config.h"
22
23 #include <libmissioncontrol/mc-account-monitor.h>
24
25 #include "empathy-account-manager.h"
26 #include "empathy-marshal.h"
27 #include "empathy-utils.h"
28
29 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountManager)
30
31 typedef struct {
32   McAccountMonitor *monitor;
33   MissionControl   *mc;
34
35   GHashTable       *accounts;
36   int               connected;
37   int               connecting;
38   gboolean          dispose_run;
39 } EmpathyAccountManagerPriv;
40
41 typedef struct {
42   McPresence presence;
43   TpConnectionStatus connection;
44   gboolean is_enabled;
45
46   guint source_id;
47 } AccountData;
48
49 enum {
50   ACCOUNT_CREATED,
51   ACCOUNT_DELETED,
52   ACCOUNT_ENABLED,
53   ACCOUNT_DISABLED,
54   ACCOUNT_CHANGED,
55   ACCOUNT_CONNECTION_CHANGED,
56   ACCOUNT_PRESENCE_CHANGED,
57   LAST_SIGNAL
58 };
59
60 static guint signals[LAST_SIGNAL];
61 static EmpathyAccountManager *manager_singleton = NULL;
62
63 G_DEFINE_TYPE (EmpathyAccountManager, empathy_account_manager, G_TYPE_OBJECT);
64
65 static AccountData *
66 account_data_new (McPresence presence,
67                   TpConnectionStatus connection,
68                   gboolean is_enabled)
69 {
70   AccountData *retval;
71
72   retval = g_slice_new0 (AccountData);
73   retval->presence = presence;
74   retval->connection = connection;
75   retval->is_enabled = is_enabled;
76   retval->source_id = 0;
77
78   return retval;
79 }
80
81 static AccountData *
82 account_data_new_default (MissionControl *mc,
83                           McAccount *account)
84 {
85   McPresence actual_p;
86   TpConnectionStatus actual_c;
87   GError *err = NULL;
88
89   actual_p = mission_control_get_presence_actual (mc, &err);
90   if (err != NULL)
91     {
92       actual_p = MC_PRESENCE_UNSET;
93       g_clear_error (&err);
94     }
95
96   actual_c = mission_control_get_connection_status (mc, account, &err);
97
98   if (err != NULL)
99     actual_c = TP_CONNECTION_STATUS_DISCONNECTED;
100
101   return account_data_new (actual_p, actual_c, mc_account_is_enabled (account));
102 }
103
104 static void
105 account_data_free (AccountData *data)
106 {
107   if (data->source_id > 0)
108     {
109       g_source_remove (data->source_id);
110       data->source_id = 0;
111     }
112
113   g_slice_free (AccountData, data);
114 }
115
116 static void
117 account_created_cb (McAccountMonitor *mon,
118                     gchar *account_name,
119                     EmpathyAccountManager *manager)
120 {
121   McAccount *account;
122   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
123
124   account = mc_account_lookup (account_name);
125
126   if (account)
127     {
128       AccountData *data;
129
130       data = account_data_new_default (priv->mc, account);
131
132       g_hash_table_insert (priv->accounts, account, data);
133
134       g_signal_emit (manager, signals[ACCOUNT_CREATED], 0, account);
135     }
136 }
137
138 static void
139 account_deleted_cb (McAccountMonitor *mon,
140                     gchar *account_name,
141                     EmpathyAccountManager *manager)
142 {
143   McAccount *account;
144   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
145
146   account = mc_account_lookup (account_name);
147
148   if (account)
149     {
150       g_signal_emit (manager, signals[ACCOUNT_DELETED], 0, account);
151
152       g_hash_table_remove (priv->accounts, account);
153       g_object_unref (account);
154     }
155 }
156
157 static void
158 account_changed_cb (McAccountMonitor *mon,
159                     gchar *account_name,
160                     EmpathyAccountManager *manager)
161 {
162   McAccount *account;
163
164   account = mc_account_lookup (account_name);
165
166   if (account)
167     {
168       g_signal_emit (manager, signals[ACCOUNT_CHANGED], 0, account);
169       g_object_unref (account);
170     }
171 }
172
173 static void
174 account_disabled_cb (McAccountMonitor *mon,
175                      gchar *account_name,
176                      EmpathyAccountManager *manager)
177 {
178   McAccount *account;
179   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
180   AccountData *data;
181
182   account = mc_account_lookup (account_name);
183
184   if (account)
185     {
186       data = g_hash_table_lookup (priv->accounts, account);
187       g_assert (data);
188       data->is_enabled = FALSE;
189
190       g_signal_emit (manager, signals[ACCOUNT_DISABLED], 0, account);
191     }
192 }
193
194 static void
195 account_enabled_cb (McAccountMonitor *mon,
196                     gchar *account_name,
197                     EmpathyAccountManager *manager)
198 {
199   McAccount *account;
200   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
201   AccountData *data;
202
203   account = mc_account_lookup (account_name);
204
205   if (account)
206     {
207       data = g_hash_table_lookup (priv->accounts, account);
208       g_assert (data);
209       data->is_enabled = TRUE;
210
211       g_signal_emit (manager, signals[ACCOUNT_ENABLED], 0, account);
212       g_object_unref (account);
213     }
214 }
215
216 static void
217 update_connection_numbers (EmpathyAccountManager *manager,
218                            TpConnectionStatus conn,
219                            TpConnectionStatus old_c)
220 {
221   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
222
223   if (conn == TP_CONNECTION_STATUS_CONNECTED)
224     {
225       priv->connected++;
226       if (old_c == TP_CONNECTION_STATUS_CONNECTING)
227         priv->connecting--;
228     }
229
230   if (conn == TP_CONNECTION_STATUS_CONNECTING)
231     {
232       priv->connecting++;
233       if (old_c == TP_CONNECTION_STATUS_CONNECTED)
234         priv->connected--;
235     }
236
237   if (conn == TP_CONNECTION_STATUS_DISCONNECTED)
238     {
239       if (old_c == TP_CONNECTION_STATUS_CONNECTED)
240         priv->connected--;
241
242       if (old_c == TP_CONNECTION_STATUS_CONNECTING)
243         priv->connecting--;
244     }
245 }
246
247 static gboolean
248 remove_data_timeout (gpointer _data)
249 {
250   AccountData *data = _data;
251
252   data->source_id = 0;
253
254   return FALSE;
255 }
256
257 static void
258 account_status_changed_cb (MissionControl *mc,
259                            TpConnectionStatus connection,
260                            McPresence presence,
261                            TpConnectionStatusReason reason,
262                            const gchar *unique_name,
263                            EmpathyAccountManager *manager)
264 {
265   McAccount *account;
266   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
267   AccountData *data;
268   McPresence old_p;
269   TpConnectionStatus old_c;
270   gboolean emit_presence = FALSE, emit_connection = FALSE;
271
272   account = mc_account_lookup (unique_name);
273
274   if (account)
275     {
276       data = g_hash_table_lookup (priv->accounts, account);
277       g_assert (data);
278
279       old_p = data->presence;
280       old_c = data->connection;
281
282       if (old_p != presence)
283         {
284           data->presence = presence;
285           emit_presence = TRUE;
286         }
287
288       if (old_c != connection)
289         {
290           data->connection = connection;
291           update_connection_numbers (manager, connection, old_c);
292
293           if (old_c == TP_CONNECTION_STATUS_CONNECTING &&
294               connection == TP_CONNECTION_STATUS_CONNECTED)
295             {
296                 if (data->source_id > 0) {
297                   g_source_remove (data->source_id);
298                   data->source_id = 0;
299                 }
300
301                 data->source_id = g_timeout_add_seconds (10,
302                                                          remove_data_timeout,
303                                                          data);
304             }
305           emit_connection = TRUE;
306         }
307
308       if (emit_presence)
309         g_signal_emit (manager, signals[ACCOUNT_PRESENCE_CHANGED], 0,
310                        account, presence, old_p);
311
312       if (emit_connection)
313         g_signal_emit (manager, signals[ACCOUNT_CONNECTION_CHANGED], 0,
314                        account, reason, connection, old_c);
315
316       g_object_unref (account);
317     }
318 }
319
320 static void
321 empathy_account_manager_init (EmpathyAccountManager *manager)
322 {
323   EmpathyAccountManagerPriv *priv =
324       G_TYPE_INSTANCE_GET_PRIVATE (manager,
325                                    EMPATHY_TYPE_ACCOUNT_MANAGER, EmpathyAccountManagerPriv);
326   GList *mc_accounts, *l;
327   AccountData *data;
328
329   manager->priv = priv;
330   priv->monitor = mc_account_monitor_new ();
331   priv->mc = empathy_mission_control_new ();
332   priv->connected = priv->connecting = 0;
333   priv->dispose_run = FALSE;
334
335   priv->accounts = g_hash_table_new_full (empathy_account_hash,
336                                           empathy_account_equal,
337                                           g_object_unref, 
338                                           (GDestroyNotify) account_data_free);
339
340   mc_accounts = mc_accounts_list ();
341
342   for (l = mc_accounts; l; l = l->next)
343     {
344       data = account_data_new_default (priv->mc, l->data);
345
346       /* no need to g_object_ref () the account here, as mc_accounts_list ()
347        * already increases the refcount.
348        */
349       g_hash_table_insert (priv->accounts, l->data, data);
350     }
351
352   g_signal_connect (priv->monitor, "account-created",
353                     G_CALLBACK (account_created_cb), manager);
354   g_signal_connect (priv->monitor, "account-deleted",
355                     G_CALLBACK (account_deleted_cb), manager);
356   g_signal_connect (priv->monitor, "account-disabled",
357                     G_CALLBACK (account_disabled_cb), manager);
358   g_signal_connect (priv->monitor, "account-enabled",
359                     G_CALLBACK (account_enabled_cb), manager);
360   g_signal_connect (priv->monitor, "account-changed",
361                     G_CALLBACK (account_changed_cb), manager);
362
363   dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc), "AccountStatusChanged",
364                                G_CALLBACK (account_status_changed_cb),
365                                manager, NULL);
366
367   g_list_free (mc_accounts);
368 }
369
370 static void
371 disconnect_monitor_signals (McAccountMonitor *monitor,
372                             GObject *obj)
373 {
374   g_signal_handlers_disconnect_by_func (monitor,
375                                         account_created_cb, obj);
376   g_signal_handlers_disconnect_by_func (monitor,
377                                         account_deleted_cb, obj);
378   g_signal_handlers_disconnect_by_func (monitor,
379                                         account_disabled_cb, obj);
380   g_signal_handlers_disconnect_by_func (monitor,
381                                         account_enabled_cb, obj);
382   g_signal_handlers_disconnect_by_func (monitor,
383                                         account_changed_cb, obj);
384 }
385
386 static void
387 do_finalize (GObject *obj)
388 {
389   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (obj);
390   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
391
392   g_hash_table_unref (priv->accounts);
393
394   G_OBJECT_CLASS (empathy_account_manager_parent_class)->finalize (obj);
395 }
396
397 static void
398 do_dispose (GObject *obj)
399 {
400   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (obj);
401   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
402
403   if (priv->dispose_run)
404     return;
405
406   priv->dispose_run = TRUE;
407
408   dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
409                                   "AccountStatusChanged",
410                                   G_CALLBACK (account_status_changed_cb),
411                                   obj);
412
413   disconnect_monitor_signals (priv->monitor, obj);
414
415   if (priv->monitor)
416     {
417       g_object_unref (priv->monitor);
418       priv->monitor = NULL;
419     }
420
421   if (priv->mc)
422     g_object_unref (priv->mc);
423
424   g_hash_table_remove_all (priv->accounts);
425
426   G_OBJECT_CLASS (empathy_account_manager_parent_class)->dispose (obj);
427 }
428
429 static GObject*
430 do_constructor (GType type,
431                 guint n_construct_params,
432                 GObjectConstructParam *construct_params)
433 {
434   GObject *retval;
435
436   if (!manager_singleton)
437     {
438       retval = G_OBJECT_CLASS (empathy_account_manager_parent_class)->constructor (type,
439                                                                                    n_construct_params,
440                                                                                    construct_params);
441       manager_singleton = EMPATHY_ACCOUNT_MANAGER (retval);
442     } 
443   else
444     retval = g_object_ref (manager_singleton);
445
446   return retval;
447 }
448
449 static void
450 empathy_account_manager_class_init (EmpathyAccountManagerClass *klass)
451 {
452   GObjectClass *oclass = G_OBJECT_CLASS (klass);
453
454   oclass->finalize = do_finalize;
455   oclass->dispose = do_dispose;
456   oclass->constructor = do_constructor;
457
458   signals[ACCOUNT_CREATED] =
459     g_signal_new ("account-created",
460                   G_TYPE_FROM_CLASS (klass),
461                   G_SIGNAL_RUN_LAST,
462                   0,
463                   NULL, NULL,
464                   g_cclosure_marshal_VOID__OBJECT,
465                   G_TYPE_NONE,
466                   1, MC_TYPE_ACCOUNT);
467
468   signals[ACCOUNT_DELETED] =
469     g_signal_new ("account-deleted",
470                   G_TYPE_FROM_CLASS (klass),
471                   G_SIGNAL_RUN_LAST,
472                   0,
473                   NULL, NULL,
474                   g_cclosure_marshal_VOID__OBJECT,
475                   G_TYPE_NONE,
476                   1, MC_TYPE_ACCOUNT);
477
478   signals[ACCOUNT_ENABLED] =
479     g_signal_new ("account-enabled",
480                   G_TYPE_FROM_CLASS (klass),
481                   G_SIGNAL_RUN_LAST,
482                   0,
483                   NULL, NULL,
484                   g_cclosure_marshal_VOID__OBJECT,
485                   G_TYPE_NONE,
486                   1, MC_TYPE_ACCOUNT);
487
488   signals[ACCOUNT_DISABLED] =
489     g_signal_new ("account-disabled",
490                   G_TYPE_FROM_CLASS (klass),
491                   G_SIGNAL_RUN_LAST,
492                   0,
493                   NULL, NULL,
494                   g_cclosure_marshal_VOID__OBJECT,
495                   G_TYPE_NONE,
496                   1, MC_TYPE_ACCOUNT);
497
498   signals[ACCOUNT_CHANGED] =
499     g_signal_new ("account-changed",
500                   G_TYPE_FROM_CLASS (klass),
501                   G_SIGNAL_RUN_LAST,
502                   0,
503                   NULL, NULL,
504                   g_cclosure_marshal_VOID__OBJECT,
505                   G_TYPE_NONE,
506                   1, MC_TYPE_ACCOUNT);
507
508   signals[ACCOUNT_CONNECTION_CHANGED] =
509     g_signal_new ("account-connection-changed",
510                   G_TYPE_FROM_CLASS (klass),
511                   G_SIGNAL_RUN_LAST,
512                   0,
513                   NULL, NULL,
514                   _empathy_marshal_VOID__OBJECT_INT_UINT_UINT,
515                   G_TYPE_NONE,
516                   4, MC_TYPE_ACCOUNT,
517                   G_TYPE_INT,   /* reason */
518                   G_TYPE_UINT,  /* actual connection */
519                   G_TYPE_UINT); /* previous connection */
520
521   signals[ACCOUNT_PRESENCE_CHANGED] =
522     g_signal_new ("account-presence-changed",
523                   G_TYPE_FROM_CLASS (klass),
524                   G_SIGNAL_RUN_LAST,
525                   0,
526                   NULL, NULL,
527                   _empathy_marshal_VOID__OBJECT_INT_INT,
528                   G_TYPE_NONE,
529                   3, MC_TYPE_ACCOUNT,
530                   G_TYPE_INT,  /* actual presence */
531                   G_TYPE_INT); /* previous presence */
532   
533   g_type_class_add_private (oclass, sizeof (EmpathyAccountManagerPriv));
534 }
535
536 /* public methods */
537
538 EmpathyAccountManager *
539 empathy_account_manager_new (void)
540 {
541   return g_object_new (EMPATHY_TYPE_ACCOUNT_MANAGER, NULL);
542 }
543
544 int
545 empathy_account_manager_get_connected_accounts (EmpathyAccountManager *manager)
546 {
547   EmpathyAccountManagerPriv *priv;
548
549   g_assert (EMPATHY_IS_ACCOUNT_MANAGER (manager));
550
551   priv = GET_PRIV (manager);
552
553   return priv->connected;
554 }
555
556 int
557 empathy_account_manager_get_connecting_accounts (EmpathyAccountManager *manager)
558 {
559   EmpathyAccountManagerPriv *priv;
560
561   g_assert (EMPATHY_IS_ACCOUNT_MANAGER (manager));
562
563   priv = GET_PRIV (manager);
564
565   return priv->connecting;
566 }
567
568 gboolean
569 empathy_account_manager_is_account_just_connected (EmpathyAccountManager *manager,
570                                                    McAccount *account)
571 {
572   EmpathyAccountManagerPriv *priv;
573   AccountData *data;
574
575   g_assert (EMPATHY_IS_ACCOUNT_MANAGER (manager));
576
577   priv = GET_PRIV (manager);
578   data = g_hash_table_lookup (priv->accounts, account);
579
580   g_assert (data);
581
582   return (data->source_id > 0);
583 }
584