1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2003-2007 Imendio AB
4 * Copyright (C) 2007-2008 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Xavier Claessens <xclaesse@gmail.com>
29 #include <glib/gstdio.h>
31 #include <telepathy-glib/util.h>
33 #include "empathy-log-manager.h"
34 #include "empathy-log-store-empathy.h"
35 #include "empathy-log-store.h"
36 #include "empathy-tp-chat.h"
37 #include "empathy-utils.h"
39 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
40 #include "empathy-debug.h"
42 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLogManager)
46 } EmpathyLogManagerPriv;
48 G_DEFINE_TYPE (EmpathyLogManager, empathy_log_manager, G_TYPE_OBJECT);
50 static EmpathyLogManager * manager_singleton = NULL;
53 log_manager_finalize (GObject *object)
55 EmpathyLogManagerPriv *priv;
57 priv = GET_PRIV (object);
59 g_list_foreach (priv->stores, (GFunc) g_object_unref, NULL);
60 g_list_free (priv->stores);
64 log_manager_constructor (GType type,
66 GObjectConstructParam *props)
69 EmpathyLogManagerPriv *priv;
71 if (manager_singleton)
73 retval = g_object_ref (manager_singleton);
77 retval = G_OBJECT_CLASS (empathy_log_manager_parent_class)->constructor
78 (type, n_props, props);
80 manager_singleton = EMPATHY_LOG_MANAGER (retval);
81 g_object_add_weak_pointer (retval, (gpointer *) &manager_singleton);
83 priv = GET_PRIV (manager_singleton);
85 priv->stores = g_list_append (priv->stores,
86 g_object_new (EMPATHY_TYPE_LOG_STORE_EMPATHY, NULL));
93 empathy_log_manager_class_init (EmpathyLogManagerClass *klass)
95 GObjectClass *object_class = G_OBJECT_CLASS (klass);
97 object_class->constructor = log_manager_constructor;
98 object_class->finalize = log_manager_finalize;
100 g_type_class_add_private (object_class, sizeof (EmpathyLogManagerPriv));
104 empathy_log_manager_init (EmpathyLogManager *manager)
106 EmpathyLogManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
107 EMPATHY_TYPE_LOG_MANAGER, EmpathyLogManagerPriv);
109 manager->priv = priv;
113 empathy_log_manager_dup_singleton (void)
115 return g_object_new (EMPATHY_TYPE_LOG_MANAGER, NULL);
119 empathy_log_manager_add_message (EmpathyLogManager *manager,
120 const gchar *chat_id,
122 EmpathyMessage *message,
125 EmpathyLogManagerPriv *priv;
127 gboolean out = FALSE;
128 gboolean found = FALSE;
130 /* TODO: When multiple log stores appear with add_message implementations
131 * make this customisable. */
132 const gchar *add_store = "Empathy";
134 g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), FALSE);
135 g_return_val_if_fail (chat_id != NULL, FALSE);
136 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
138 priv = GET_PRIV (manager);
140 for (l = priv->stores; l; l = g_list_next (l))
142 if (!tp_strdiff (empathy_log_store_get_name (
143 EMPATHY_LOG_STORE (l->data)), add_store))
145 out = empathy_log_store_add_message (EMPATHY_LOG_STORE (l->data),
146 chat_id, chatroom, message, error);
153 DEBUG ("Failed to find chosen log store to write to.");
159 empathy_log_manager_exists (EmpathyLogManager *manager,
160 EmpathyAccount *account,
161 const gchar *chat_id,
165 EmpathyLogManagerPriv *priv;
167 g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), FALSE);
168 g_return_val_if_fail (chat_id != NULL, FALSE);
170 priv = GET_PRIV (manager);
172 for (l = priv->stores; l; l = g_list_next (l))
174 if (empathy_log_store_exists (EMPATHY_LOG_STORE (l->data),
175 account, chat_id, chatroom))
183 empathy_log_manager_get_dates (EmpathyLogManager *manager,
184 EmpathyAccount *account,
185 const gchar *chat_id,
188 GList *l, *out = NULL;
189 EmpathyLogManagerPriv *priv;
191 g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), NULL);
192 g_return_val_if_fail (chat_id != NULL, NULL);
194 priv = GET_PRIV (manager);
196 for (l = priv->stores; l; l = g_list_next (l))
198 EmpathyLogStore *store = EMPATHY_LOG_STORE (l->data);
201 /* Insert dates of each store in the out list. Keep the out list sorted
202 * and avoid to insert dups. */
203 new = empathy_log_store_get_dates (store, account, chat_id, chatroom);
206 if (g_list_find_custom (out, new->data, (GCompareFunc) strcmp))
209 out = g_list_insert_sorted (out, new->data, (GCompareFunc) strcmp);
211 new = g_list_delete_link (new, new);
219 empathy_log_manager_get_messages_for_date (EmpathyLogManager *manager,
220 EmpathyAccount *account,
221 const gchar *chat_id,
225 GList *l, *out = NULL;
226 EmpathyLogManagerPriv *priv;
228 g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), NULL);
229 g_return_val_if_fail (chat_id != NULL, NULL);
231 priv = GET_PRIV (manager);
233 for (l = priv->stores; l; l = g_list_next (l))
235 EmpathyLogStore *store = EMPATHY_LOG_STORE (l->data);
237 out = g_list_concat (out, empathy_log_store_get_messages_for_date (
238 store, account, chat_id, chatroom, date));
245 log_manager_message_date_cmp (gconstpointer a,
248 EmpathyMessage *one = (EmpathyMessage *) a;
249 EmpathyMessage *two = (EmpathyMessage *) b;
250 time_t one_time, two_time;
252 one_time = empathy_message_get_timestamp (one);
253 two_time = empathy_message_get_timestamp (two);
255 /* Return -1 of message1 is older than message2 */
256 return one_time < two_time ? -1 : one_time - two_time;
260 empathy_log_manager_get_filtered_messages (EmpathyLogManager *manager,
261 EmpathyAccount *account,
262 const gchar *chat_id,
265 EmpathyLogMessageFilter filter,
268 EmpathyLogManagerPriv *priv;
273 g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), NULL);
274 g_return_val_if_fail (chat_id != NULL, NULL);
276 priv = GET_PRIV (manager);
278 /* Get num_messages from each log store and keep only the
279 * newest ones in the out list. Keep that list sorted: Older first. */
280 for (l = priv->stores; l; l = g_list_next (l))
282 EmpathyLogStore *store = EMPATHY_LOG_STORE (l->data);
285 new = empathy_log_store_get_filtered_messages (store, account, chat_id,
286 chatroom, num_messages, filter, user_data);
289 if (i < num_messages)
291 /* We have less message than needed so far. Keep this message */
292 out = g_list_insert_sorted (out, new->data,
293 (GCompareFunc) log_manager_message_date_cmp);
296 else if (log_manager_message_date_cmp (new->data, out->data) > 0)
298 /* This message is newer than the oldest message we have in out
299 * list. Remove the head of out list and insert this message */
300 g_object_unref (out->data);
301 out = g_list_delete_link (out, out);
302 out = g_list_insert_sorted (out, new->data,
303 (GCompareFunc) log_manager_message_date_cmp);
307 /* This message is older than the oldest message we have in out
309 g_object_unref (new->data);
312 new = g_list_delete_link (new, new);
320 empathy_log_manager_get_chats (EmpathyLogManager *manager,
321 EmpathyAccount *account)
323 GList *l, *out = NULL;
324 EmpathyLogManagerPriv *priv;
326 g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), NULL);
328 priv = GET_PRIV (manager);
330 for (l = priv->stores; l; l = g_list_next (l))
332 EmpathyLogStore *store = EMPATHY_LOG_STORE (l->data);
334 out = g_list_concat (out,
335 empathy_log_store_get_chats (store, account));
342 empathy_log_manager_search_new (EmpathyLogManager *manager,
345 GList *l, *out = NULL;
346 EmpathyLogManagerPriv *priv;
348 g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), NULL);
349 g_return_val_if_fail (!EMP_STR_EMPTY (text), NULL);
351 priv = GET_PRIV (manager);
353 for (l = priv->stores; l; l = g_list_next (l))
355 EmpathyLogStore *store = EMPATHY_LOG_STORE (l->data);
357 out = g_list_concat (out,
358 empathy_log_store_search_new (store, text));
365 empathy_log_manager_search_hit_free (EmpathyLogSearchHit *hit)
367 if (hit->account != NULL)
368 g_object_unref (hit->account);
371 g_free (hit->filename);
372 g_free (hit->chat_id);
374 g_slice_free (EmpathyLogSearchHit, hit);
378 empathy_log_manager_search_free (GList *hits)
382 for (l = hits; l; l = g_list_next (l))
384 empathy_log_manager_search_hit_free (l->data);
390 /* Format is just date, 20061201. */
392 empathy_log_manager_get_date_readable (const gchar *date)
396 t = empathy_time_parse (date);
398 return empathy_time_to_string_local (t, "%a %d %b %Y");
402 log_manager_chat_received_message_cb (EmpathyTpChat *tp_chat,
403 EmpathyMessage *message,
404 EmpathyLogManager *log_manager)
406 GError *error = NULL;
407 TpHandleType handle_type;
410 channel = empathy_tp_chat_get_channel (tp_chat);
411 tp_channel_get_handle (channel, &handle_type);
413 if (!empathy_log_manager_add_message (log_manager,
414 tp_channel_get_identifier (channel),
415 handle_type == TP_HANDLE_TYPE_ROOM,
418 DEBUG ("Failed to write message: %s",
419 error ? error->message : "No error message");
422 g_error_free (error);
427 log_manager_dispatcher_observe_cb (EmpathyDispatcher *dispatcher,
428 EmpathyDispatchOperation *operation,
429 EmpathyLogManager *log_manager)
433 channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
435 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
437 EmpathyTpChat *tp_chat;
439 tp_chat = EMPATHY_TP_CHAT (
440 empathy_dispatch_operation_get_channel_wrapper (operation));
442 g_signal_connect (tp_chat, "message-received",
443 G_CALLBACK (log_manager_chat_received_message_cb), log_manager);
449 empathy_log_manager_observe (EmpathyLogManager *log_manager,
450 EmpathyDispatcher *dispatcher)
452 g_signal_connect (dispatcher, "observe",
453 G_CALLBACK (log_manager_dispatcher_observe_cb), log_manager);