1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2010 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
20 * Travis Reitter <travis.reitter@collabora.co.uk>
27 #include <telepathy-glib/account-manager.h>
28 #include <telepathy-glib/enums.h>
29 #include <telepathy-glib/proxy-subclass.h>
30 #include <telepathy-glib/util.h>
32 #include <folks/folks.h>
34 #include <extensions/extensions.h>
36 #include "empathy-individual-manager.h"
37 #include "empathy-contact-manager.h"
38 #include "empathy-contact-list.h"
39 #include "empathy-marshal.h"
40 #include "empathy-utils.h"
42 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
43 #include "empathy-debug.h"
45 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIndividualManager)
48 FolksIndividualAggregator *aggregator;
49 EmpathyContactManager *contact_manager;
51 /* account object path (gchar *) => GHashTable containing favorite contacts
52 * (contact ID (gchar *) => TRUE) */
53 GHashTable *favourites;
54 TpProxySignalConnection *favourite_contacts_changed_signal;
55 } EmpathyIndividualManagerPriv;
57 G_DEFINE_TYPE (EmpathyIndividualManager, empathy_individual_manager,
60 static EmpathyIndividualManager *manager_singleton = NULL;
63 individual_group_changed_cb (FolksIndividual *individual,
66 EmpathyIndividualManager *self)
68 g_signal_emit_by_name (self, "groups-changed", individual, group,
73 aggregator_individuals_added_cb (FolksIndividualAggregator *aggregator,
75 EmpathyIndividualManager *self)
79 for (l = individuals; l; l = l->next)
81 g_signal_connect (l->data, "group-changed",
82 G_CALLBACK (individual_group_changed_cb), self);
85 /* TODO: don't hard-code the reason or message */
86 g_signal_emit_by_name (self, "members-changed",
87 "individual(s) added", individuals, NULL,
88 TP_CHANNEL_GROUP_CHANGE_REASON_NONE, TRUE);
92 aggregator_individuals_removed_cb (FolksIndividualAggregator *aggregator,
94 EmpathyIndividualManager *self)
98 for (l = individuals; l; l = l->next)
100 g_signal_handlers_disconnect_by_func (l->data,
101 individual_group_changed_cb, self);
104 /* TODO: don't hard-code the reason or message */
105 g_signal_emit_by_name (self, "members-changed",
106 "individual(s) removed", NULL, individuals,
107 TP_CHANNEL_GROUP_CHANGE_REASON_NONE, TRUE);
111 individual_manager_finalize (GObject *object)
113 EmpathyIndividualManagerPriv *priv = GET_PRIV (object);
115 tp_proxy_signal_connection_disconnect (
116 priv->favourite_contacts_changed_signal);
118 if (priv->logger != NULL)
119 g_object_unref (priv->logger);
121 if (priv->contact_manager != NULL)
122 g_object_unref (priv->contact_manager);
124 if (priv->aggregator != NULL)
125 g_object_unref (priv->aggregator);
127 g_hash_table_destroy (priv->favourites);
131 individual_manager_constructor (GType type,
133 GObjectConstructParam *props)
137 if (manager_singleton)
139 retval = g_object_ref (manager_singleton);
144 G_OBJECT_CLASS (empathy_individual_manager_parent_class)->
145 constructor (type, n_props, props);
147 manager_singleton = EMPATHY_INDIVIDUAL_MANAGER (retval);
148 g_object_add_weak_pointer (retval, (gpointer) & manager_singleton);
155 * empathy_individual_manager_initialized:
157 * Reports whether or not the singleton has already been created.
159 * There can be instances where you want to access the #EmpathyIndividualManager
160 * only if it has been set up for this process.
162 * Returns: %TRUE if the #EmpathyIndividualManager singleton has previously
166 empathy_individual_manager_initialized (void)
168 return (manager_singleton != NULL);
172 empathy_individual_manager_class_init (EmpathyIndividualManagerClass *klass)
174 GObjectClass *object_class = G_OBJECT_CLASS (klass);
176 object_class->finalize = individual_manager_finalize;
177 object_class->constructor = individual_manager_constructor;
179 g_signal_new ("groups-changed",
180 G_TYPE_FROM_CLASS (klass),
184 _empathy_marshal_VOID__OBJECT_STRING_BOOLEAN,
185 G_TYPE_NONE, 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_STRING, G_TYPE_BOOLEAN);
187 g_signal_new ("members-changed",
188 G_TYPE_FROM_CLASS (klass),
192 _empathy_marshal_VOID__STRING_OBJECT_OBJECT_UINT,
194 4, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_UINT);
196 g_type_class_add_private (object_class,
197 sizeof (EmpathyIndividualManagerPriv));
201 empathy_individual_manager_init (EmpathyIndividualManager *self)
203 EmpathyIndividualManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
204 EMPATHY_TYPE_INDIVIDUAL_MANAGER, EmpathyIndividualManagerPriv);
206 GError *error = NULL;
209 priv->contact_manager = empathy_contact_manager_dup_singleton ();
211 priv->favourites = g_hash_table_new_full (g_str_hash, g_str_equal,
212 (GDestroyNotify) g_free, (GDestroyNotify) g_hash_table_unref);
214 priv->aggregator = folks_individual_aggregator_new ();
217 g_signal_connect (priv->aggregator, "individuals-added",
218 G_CALLBACK (aggregator_individuals_added_cb), self);
219 g_signal_connect (priv->aggregator, "individuals-removed",
220 G_CALLBACK (aggregator_individuals_removed_cb), self);
224 DEBUG ("Failed to get individual aggregator: %s", error->message);
225 g_clear_error (&error);
228 bus = tp_dbus_daemon_dup (&error);
232 priv->logger = g_object_new (TP_TYPE_PROXY,
233 "bus-name", "org.freedesktop.Telepathy.Logger",
235 "/org/freedesktop/Telepathy/Logger", "dbus-daemon", bus, NULL);
236 g_object_unref (bus);
238 tp_proxy_add_interface_by_id (priv->logger, EMP_IFACE_QUARK_LOGGER);
242 DEBUG ("Failed to get telepathy-logger proxy: %s", error->message);
243 g_clear_error (&error);
247 EmpathyIndividualManager *
248 empathy_individual_manager_dup_singleton (void)
250 return g_object_new (EMPATHY_TYPE_INDIVIDUAL_MANAGER, NULL);
253 /* TODO: support adding and removing Individuals */
256 empathy_individual_manager_get_members (EmpathyIndividualManager *self)
258 EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
259 GHashTable *individuals;
261 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self), NULL);
263 individuals = folks_individual_aggregator_get_individuals (priv->aggregator);
264 return individuals ? g_hash_table_get_values (individuals) : NULL;
268 empathy_individual_manager_lookup_member (EmpathyIndividualManager *self,
271 EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
272 GHashTable *individuals;
274 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self), NULL);
276 individuals = folks_individual_aggregator_get_individuals (priv->aggregator);
277 if (individuals != NULL)
278 return g_hash_table_lookup (individuals, id);
284 empathy_individual_manager_remove (EmpathyIndividualManager *self,
285 FolksIndividual *individual,
286 const gchar *message)
288 /* TODO: implement */
289 DEBUG (G_STRLOC ": individual removal not implemented");