2 * Copyright (C) 2005-2007 Imendio AB
3 * Copyright (C) 2007-2011 Collabora Ltd.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program 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 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301 USA
20 * Authors: Mikael Hallendal <micke@imendio.com>
21 * Martyn Russell <martyn@imendio.com>
22 * Xavier Claessens <xclaesse@gmail.com>
23 * Travis Reitter <travis.reitter@collabora.co.uk>
27 #include "empathy-individual-store-manager.h"
29 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
30 #include "empathy-debug.h"
32 struct _EmpathyIndividualStoreManagerPriv
34 EmpathyIndividualManager
*manager
;
35 gboolean setup_idle_id
;
41 PROP_INDIVIDUAL_MANAGER
,
45 G_DEFINE_TYPE (EmpathyIndividualStoreManager
, empathy_individual_store_manager
,
46 EMPATHY_TYPE_INDIVIDUAL_STORE
);
49 individual_store_manager_members_changed_cb (EmpathyIndividualManager
*manager
,
54 EmpathyIndividualStoreManager
*self
)
57 EmpathyIndividualStore
*store
= EMPATHY_INDIVIDUAL_STORE (self
);
59 for (l
= removed
; l
; l
= l
->next
)
61 DEBUG ("Individual %s (%s) %s",
62 folks_individual_get_id (l
->data
),
63 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (l
->data
)),
66 individual_store_remove_individual_and_disconnect (store
, l
->data
);
69 for (l
= added
; l
; l
= l
->next
)
71 DEBUG ("Individual %s (%s) %s", folks_individual_get_id (l
->data
),
72 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (l
->data
)),
75 individual_store_add_individual_and_connect (store
, l
->data
);
80 individual_store_manager_groups_changed_cb (EmpathyIndividualManager
*manager
,
81 FolksIndividual
*individual
,
84 EmpathyIndividualStoreManager
*self
)
86 EmpathyIndividualStore
*store
= EMPATHY_INDIVIDUAL_STORE (self
);
88 DEBUG ("Updating groups for individual %s (%s)",
89 folks_individual_get_id (individual
),
90 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual
)));
92 /* We do this to make sure the groups are correct, if not, we
93 * would have to check the groups already set up for each
94 * contact and then see what has been updated.
96 empathy_individual_store_refresh_individual (store
, individual
);
100 individual_store_manager_manager_setup (gpointer user_data
)
102 EmpathyIndividualStoreManager
*self
= user_data
;
105 /* Signal connection. */
107 DEBUG ("handling individual renames unimplemented");
109 g_signal_connect (self
->priv
->manager
,
111 G_CALLBACK (individual_store_manager_members_changed_cb
), self
);
113 g_signal_connect (self
->priv
->manager
,
115 G_CALLBACK (individual_store_manager_groups_changed_cb
), self
);
117 /* Add contacts already created. */
118 individuals
= empathy_individual_manager_get_members (self
->priv
->manager
);
119 if (individuals
!= NULL
)
121 individual_store_manager_members_changed_cb (self
->priv
->manager
, "initial add",
122 individuals
, NULL
, 0, self
);
123 g_list_free (individuals
);
126 self
->priv
->setup_idle_id
= 0;
131 individual_store_manager_set_individual_manager (
132 EmpathyIndividualStoreManager
*self
,
133 EmpathyIndividualManager
*manager
)
135 g_assert (self
->priv
->manager
== NULL
); /* construct only */
136 self
->priv
->manager
= g_object_ref (manager
);
138 /* Let a chance to have all properties set before populating */
139 self
->priv
->setup_idle_id
= g_idle_add (
140 individual_store_manager_manager_setup
, self
);
144 individual_store_manager_member_renamed_cb (EmpathyIndividualManager
*manager
,
145 FolksIndividual
*old_individual
,
146 FolksIndividual
*new_individual
,
148 const gchar
*message
,
149 EmpathyIndividualStoreManager
*self
)
151 EmpathyIndividualStore
*store
= EMPATHY_INDIVIDUAL_STORE (self
);
153 DEBUG ("Individual %s (%s) renamed to %s",
154 folks_individual_get_id (old_individual
),
155 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (old_individual
)),
156 folks_individual_get_id (new_individual
));
158 /* remove old contact */
159 individual_store_remove_individual_and_disconnect (store
, old_individual
);
161 /* add the new contact */
162 individual_store_add_individual_and_connect (store
, new_individual
);
166 individual_store_manager_dispose (GObject
*object
)
168 EmpathyIndividualStoreManager
*self
= EMPATHY_INDIVIDUAL_STORE_MANAGER (
170 EmpathyIndividualStore
*store
= EMPATHY_INDIVIDUAL_STORE (object
);
171 GList
*individuals
, *l
;
173 individuals
= empathy_individual_manager_get_members (self
->priv
->manager
);
174 for (l
= individuals
; l
; l
= l
->next
)
176 empathy_individual_store_disconnect_individual (store
,
177 FOLKS_INDIVIDUAL (l
->data
));
179 tp_clear_pointer (&individuals
, g_list_free
);
181 if (self
->priv
->manager
!= NULL
)
183 g_signal_handlers_disconnect_by_func (self
->priv
->manager
,
184 G_CALLBACK (individual_store_manager_member_renamed_cb
), object
);
185 g_signal_handlers_disconnect_by_func (self
->priv
->manager
,
186 G_CALLBACK (individual_store_manager_members_changed_cb
), object
);
187 g_signal_handlers_disconnect_by_func (self
->priv
->manager
,
188 G_CALLBACK (individual_store_manager_groups_changed_cb
), object
);
189 g_clear_object (&self
->priv
->manager
);
192 if (self
->priv
->setup_idle_id
!= 0)
194 g_source_remove (self
->priv
->setup_idle_id
);
195 self
->priv
->setup_idle_id
= 0;
198 G_OBJECT_CLASS (empathy_individual_store_manager_parent_class
)->dispose (
203 individual_store_manager_get_property (GObject
*object
,
208 EmpathyIndividualStoreManager
*self
= EMPATHY_INDIVIDUAL_STORE_MANAGER (
213 case PROP_INDIVIDUAL_MANAGER
:
214 g_value_set_object (value
, self
->priv
->manager
);
217 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);
223 individual_store_manager_set_property (GObject
*object
,
230 case PROP_INDIVIDUAL_MANAGER
:
231 individual_store_manager_set_individual_manager (
232 EMPATHY_INDIVIDUAL_STORE_MANAGER (object
),
233 g_value_get_object (value
));
236 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);
242 individual_store_manager_reload_individuals (EmpathyIndividualStore
*store
)
244 EmpathyIndividualStoreManager
*self
= EMPATHY_INDIVIDUAL_STORE_MANAGER (
248 contacts
= empathy_individual_manager_get_members (self
->priv
->manager
);
250 individual_store_manager_members_changed_cb (self
->priv
->manager
,
251 "re-adding members: toggled group visibility",
252 contacts
, NULL
, 0, self
);
254 g_list_free (contacts
);
258 individual_store_manager_initial_loading (EmpathyIndividualStore
*store
)
260 EmpathyIndividualStoreManager
*self
= EMPATHY_INDIVIDUAL_STORE_MANAGER (
263 return self
->priv
->setup_idle_id
!= 0;
267 empathy_individual_store_manager_class_init (
268 EmpathyIndividualStoreManagerClass
*klass
)
270 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
271 EmpathyIndividualStoreClass
*store_class
= EMPATHY_INDIVIDUAL_STORE_CLASS (
274 object_class
->dispose
= individual_store_manager_dispose
;
275 object_class
->get_property
= individual_store_manager_get_property
;
276 object_class
->set_property
= individual_store_manager_set_property
;
278 store_class
->reload_individuals
= individual_store_manager_reload_individuals
;
279 store_class
->initial_loading
= individual_store_manager_initial_loading
;
281 g_object_class_install_property (object_class
,
282 PROP_INDIVIDUAL_MANAGER
,
283 g_param_spec_object ("individual-manager",
284 "Individual manager",
285 "Individual manager",
286 EMPATHY_TYPE_INDIVIDUAL_MANAGER
,
287 G_PARAM_CONSTRUCT_ONLY
| G_PARAM_READWRITE
));
289 g_type_class_add_private (object_class
,
290 sizeof (EmpathyIndividualStoreManagerPriv
));
294 empathy_individual_store_manager_init (EmpathyIndividualStoreManager
*self
)
296 self
->priv
= G_TYPE_INSTANCE_GET_PRIVATE (self
,
297 EMPATHY_TYPE_INDIVIDUAL_STORE_MANAGER
, EmpathyIndividualStoreManagerPriv
);
300 EmpathyIndividualStoreManager
*
301 empathy_individual_store_manager_new (EmpathyIndividualManager
*manager
)
303 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager
), NULL
);
305 return g_object_new (EMPATHY_TYPE_INDIVIDUAL_STORE_MANAGER
,
306 "individual-manager", manager
, NULL
);
309 EmpathyIndividualManager
*
310 empathy_individual_store_manager_get_manager (
311 EmpathyIndividualStoreManager
*self
)
313 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE_MANAGER (self
), FALSE
);
315 return self
->priv
->manager
;