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>
32 #include <telepathy-glib/interfaces.h>
34 #include "empathy-log-manager.h"
35 #include "empathy-log-store-empathy.h"
36 #include "empathy-log-store.h"
37 #include "empathy-tp-chat.h"
38 #include "empathy-utils.h"
40 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
41 #include "empathy-debug.h"
43 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLogManager)
47 } EmpathyLogManagerPriv;
49 G_DEFINE_TYPE (EmpathyLogManager, empathy_log_manager, G_TYPE_OBJECT);
51 static EmpathyLogManager * manager_singleton = NULL;
54 log_manager_finalize (GObject *object)
56 EmpathyLogManagerPriv *priv;
58 priv = GET_PRIV (object);
60 g_list_foreach (priv->stores, (GFunc) g_object_unref, NULL);
61 g_list_free (priv->stores);
65 log_manager_constructor (GType type,
67 GObjectConstructParam *props)
70 EmpathyLogManagerPriv *priv;
72 if (manager_singleton)
74 retval = g_object_ref (manager_singleton);
78 retval = G_OBJECT_CLASS (empathy_log_manager_parent_class)->constructor
79 (type, n_props, props);
81 manager_singleton = EMPATHY_LOG_MANAGER (retval);
82 g_object_add_weak_pointer (retval, (gpointer *) &manager_singleton);
84 priv = GET_PRIV (manager_singleton);
86 priv->stores = g_list_append (priv->stores,
87 g_object_new (EMPATHY_TYPE_LOG_STORE_EMPATHY, NULL));
94 empathy_log_manager_class_init (EmpathyLogManagerClass *klass)
96 GObjectClass *object_class = G_OBJECT_CLASS (klass);
98 object_class->constructor = log_manager_constructor;
99 object_class->finalize = log_manager_finalize;
101 g_type_class_add_private (object_class, sizeof (EmpathyLogManagerPriv));
105 empathy_log_manager_init (EmpathyLogManager *manager)
107 EmpathyLogManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
108 EMPATHY_TYPE_LOG_MANAGER, EmpathyLogManagerPriv);
110 manager->priv = priv;
114 empathy_log_manager_dup_singleton (void)
116 return g_object_new (EMPATHY_TYPE_LOG_MANAGER, NULL);
120 empathy_log_manager_add_message (EmpathyLogManager *manager,
121 const gchar *chat_id,
123 EmpathyMessage *message,
126 EmpathyLogManagerPriv *priv;
128 gboolean out = FALSE;
129 gboolean found = FALSE;
131 /* TODO: When multiple log stores appear with add_message implementations
132 * make this customisable. */
133 const gchar *add_store = "Empathy";
135 g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), FALSE);
136 g_return_val_if_fail (chat_id != NULL, FALSE);
137 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
139 priv = GET_PRIV (manager);
141 for (l = priv->stores; l; l = g_list_next (l))
143 if (!tp_strdiff (empathy_log_store_get_name (
144 EMPATHY_LOG_STORE (l->data)), add_store))
146 out = empathy_log_store_add_message (EMPATHY_LOG_STORE (l->data),
147 chat_id, chatroom, message, error);
154 DEBUG ("Failed to find chosen log store to write to.");
160 empathy_log_manager_exists (EmpathyLogManager *manager,
162 const gchar *chat_id,
166 EmpathyLogManagerPriv *priv;
168 g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), FALSE);
169 g_return_val_if_fail (chat_id != NULL, FALSE);
171 priv = GET_PRIV (manager);
173 for (l = priv->stores; l; l = g_list_next (l))
175 if (empathy_log_store_exists (EMPATHY_LOG_STORE (l->data),
176 account, chat_id, chatroom))
184 empathy_log_manager_get_dates (EmpathyLogManager *manager,
186 const gchar *chat_id,
189 GList *l, *out = NULL;
190 EmpathyLogManagerPriv *priv;
192 g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), NULL);
193 g_return_val_if_fail (chat_id != NULL, NULL);
195 priv = GET_PRIV (manager);
197 for (l = priv->stores; l; l = g_list_next (l))
199 EmpathyLogStore *store = EMPATHY_LOG_STORE (l->data);
202 /* Insert dates of each store in the out list. Keep the out list sorted
203 * and avoid to insert dups. */
204 new = empathy_log_store_get_dates (store, account, chat_id, chatroom);
207 if (g_list_find_custom (out, new->data, (GCompareFunc) strcmp))
210 out = g_list_insert_sorted (out, new->data, (GCompareFunc) strcmp);
212 new = g_list_delete_link (new, new);
220 empathy_log_manager_get_messages_for_date (EmpathyLogManager *manager,
222 const gchar *chat_id,
226 GList *l, *out = NULL;
227 EmpathyLogManagerPriv *priv;
229 g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), NULL);
230 g_return_val_if_fail (chat_id != NULL, NULL);
232 priv = GET_PRIV (manager);
234 for (l = priv->stores; l; l = g_list_next (l))
236 EmpathyLogStore *store = EMPATHY_LOG_STORE (l->data);
238 out = g_list_concat (out, empathy_log_store_get_messages_for_date (
239 store, account, chat_id, chatroom, date));
246 log_manager_message_date_cmp (gconstpointer a,
249 EmpathyMessage *one = (EmpathyMessage *) a;
250 EmpathyMessage *two = (EmpathyMessage *) b;
251 time_t one_time, two_time;
253 one_time = empathy_message_get_timestamp (one);
254 two_time = empathy_message_get_timestamp (two);
256 /* Return -1 of message1 is older than message2 */
257 return one_time < two_time ? -1 : one_time - two_time;
261 empathy_log_manager_get_filtered_messages (EmpathyLogManager *manager,
263 const gchar *chat_id,
266 EmpathyLogMessageFilter filter,
269 EmpathyLogManagerPriv *priv;
274 g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), NULL);
275 g_return_val_if_fail (chat_id != NULL, NULL);
277 priv = GET_PRIV (manager);
279 /* Get num_messages from each log store and keep only the
280 * newest ones in the out list. Keep that list sorted: Older first. */
281 for (l = priv->stores; l; l = g_list_next (l))
283 EmpathyLogStore *store = EMPATHY_LOG_STORE (l->data);
286 new = empathy_log_store_get_filtered_messages (store, account, chat_id,
287 chatroom, num_messages, filter, user_data);
290 if (i < num_messages)
292 /* We have less message than needed so far. Keep this message */
293 out = g_list_insert_sorted (out, new->data,
294 (GCompareFunc) log_manager_message_date_cmp);
297 else if (log_manager_message_date_cmp (new->data, out->data) > 0)
299 /* This message is newer than the oldest message we have in out
300 * list. Remove the head of out list and insert this message */
301 g_object_unref (out->data);
302 out = g_list_delete_link (out, out);
303 out = g_list_insert_sorted (out, new->data,
304 (GCompareFunc) log_manager_message_date_cmp);
308 /* This message is older than the oldest message we have in out
310 g_object_unref (new->data);
313 new = g_list_delete_link (new, new);
321 empathy_log_manager_get_chats (EmpathyLogManager *manager,
324 GList *l, *out = NULL;
325 EmpathyLogManagerPriv *priv;
327 g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), NULL);
329 priv = GET_PRIV (manager);
331 for (l = priv->stores; l; l = g_list_next (l))
333 EmpathyLogStore *store = EMPATHY_LOG_STORE (l->data);
335 out = g_list_concat (out,
336 empathy_log_store_get_chats (store, account));
343 empathy_log_manager_search_new (EmpathyLogManager *manager,
346 GList *l, *out = NULL;
347 EmpathyLogManagerPriv *priv;
349 g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), NULL);
350 g_return_val_if_fail (!EMP_STR_EMPTY (text), NULL);
352 priv = GET_PRIV (manager);
354 for (l = priv->stores; l; l = g_list_next (l))
356 EmpathyLogStore *store = EMPATHY_LOG_STORE (l->data);
358 out = g_list_concat (out,
359 empathy_log_store_search_new (store, text));
366 empathy_log_manager_search_hit_free (EmpathyLogSearchHit *hit)
368 if (hit->account != NULL)
369 g_object_unref (hit->account);
372 g_free (hit->filename);
373 g_free (hit->chat_id);
375 g_slice_free (EmpathyLogSearchHit, hit);
379 empathy_log_manager_search_free (GList *hits)
383 for (l = hits; l; l = g_list_next (l))
385 empathy_log_manager_search_hit_free (l->data);
391 /* Format is just date, 20061201. */
393 empathy_log_manager_get_date_readable (const gchar *date)
397 t = empathy_time_parse (date);
399 return empathy_time_to_string_local (t, "%a %d %b %Y");
403 log_manager_chat_received_message_cb (EmpathyTpChat *tp_chat,
404 EmpathyMessage *message,
405 EmpathyLogManager *log_manager)
407 GError *error = NULL;
408 TpHandleType handle_type;
411 channel = empathy_tp_chat_get_channel (tp_chat);
412 tp_channel_get_handle (channel, &handle_type);
414 if (!empathy_log_manager_add_message (log_manager,
415 tp_channel_get_identifier (channel),
416 handle_type == TP_HANDLE_TYPE_ROOM,
419 DEBUG ("Failed to write message: %s",
420 error ? error->message : "No error message");
423 g_error_free (error);
428 log_manager_dispatcher_observe_cb (EmpathyDispatcher *dispatcher,
429 EmpathyDispatchOperation *operation,
430 EmpathyLogManager *log_manager)
434 channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
436 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
438 EmpathyTpChat *tp_chat;
440 tp_chat = EMPATHY_TP_CHAT (
441 empathy_dispatch_operation_get_channel_wrapper (operation));
443 g_signal_connect (tp_chat, "message-received",
444 G_CALLBACK (log_manager_chat_received_message_cb), log_manager);
450 empathy_log_manager_observe (EmpathyLogManager *log_manager,
451 EmpathyDispatcher *dispatcher)
453 g_signal_connect (dispatcher, "observe",
454 G_CALLBACK (log_manager_dispatcher_observe_cb), log_manager);