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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, 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,
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 (MC_IS_ACCOUNT (account), 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 (MC_IS_ACCOUNT (account), NULL);
194 g_return_val_if_fail (chat_id != NULL, NULL);
196 priv = GET_PRIV (manager);
198 for (l = priv->stores; l; l = g_list_next (l))
200 EmpathyLogStore *store = EMPATHY_LOG_STORE (l->data);
203 /* Insert dates of each store in the out list. Keep the out list sorted
204 * and avoid to insert dups. */
205 new = empathy_log_store_get_dates (store, account, chat_id, chatroom);
208 if (g_list_find_custom (out, new->data, (GCompareFunc) strcmp))
211 out = g_list_insert_sorted (out, new->data, (GCompareFunc) strcmp);
213 new = g_list_delete_link (new, new);
221 empathy_log_manager_get_messages_for_date (EmpathyLogManager *manager,
223 const gchar *chat_id,
227 GList *l, *out = NULL;
228 EmpathyLogManagerPriv *priv;
230 g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), NULL);
231 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
232 g_return_val_if_fail (chat_id != NULL, NULL);
234 priv = GET_PRIV (manager);
236 for (l = priv->stores; l; l = g_list_next (l))
238 EmpathyLogStore *store = EMPATHY_LOG_STORE (l->data);
240 out = g_list_concat (out, empathy_log_store_get_messages_for_date (
241 store, account, chat_id, chatroom, date));
248 log_manager_message_date_cmp (gconstpointer a,
251 EmpathyMessage *one = (EmpathyMessage *) a;
252 EmpathyMessage *two = (EmpathyMessage *) b;
253 time_t one_time, two_time;
255 one_time = empathy_message_get_timestamp (one);
256 two_time = empathy_message_get_timestamp (two);
258 /* Return -1 of message1 is older than message2 */
259 return one_time < two_time ? -1 : one_time - two_time;
263 empathy_log_manager_get_filtered_messages (EmpathyLogManager *manager,
265 const gchar *chat_id,
268 EmpathyLogMessageFilter filter,
271 EmpathyLogManagerPriv *priv;
276 g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), NULL);
277 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
278 g_return_val_if_fail (chat_id != NULL, NULL);
280 priv = GET_PRIV (manager);
282 /* Get num_messages from each log store and keep only the
283 * newest ones in the out list. Keep that list sorted: Older first. */
284 for (l = priv->stores; l; l = g_list_next (l))
286 EmpathyLogStore *store = EMPATHY_LOG_STORE (l->data);
289 new = empathy_log_store_get_filtered_messages (store, account, chat_id,
290 chatroom, num_messages, filter, user_data);
293 if (i < num_messages)
295 /* We have less message than needed so far. Keep this message */
296 out = g_list_insert_sorted (out, new->data,
297 (GCompareFunc) log_manager_message_date_cmp);
300 else if (log_manager_message_date_cmp (new->data, out->data) > 0)
302 /* This message is newer than the oldest message we have in out
303 * list. Remove the head of out list and insert this message */
304 g_object_unref (out->data);
305 out = g_list_delete_link (out, out);
306 out = g_list_insert_sorted (out, new->data,
307 (GCompareFunc) log_manager_message_date_cmp);
311 /* This message is older than the oldest message we have in out
313 g_object_unref (new->data);
316 new = g_list_delete_link (new, new);
324 empathy_log_manager_get_chats (EmpathyLogManager *manager,
327 GList *l, *out = NULL;
328 EmpathyLogManagerPriv *priv;
330 g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), NULL);
331 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
333 priv = GET_PRIV (manager);
335 for (l = priv->stores; l; l = g_list_next (l))
337 EmpathyLogStore *store = EMPATHY_LOG_STORE (l->data);
339 out = g_list_concat (out,
340 empathy_log_store_get_chats (store, account));
347 empathy_log_manager_search_new (EmpathyLogManager *manager,
350 GList *l, *out = NULL;
351 EmpathyLogManagerPriv *priv;
353 g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), NULL);
354 g_return_val_if_fail (!EMP_STR_EMPTY (text), NULL);
356 priv = GET_PRIV (manager);
358 for (l = priv->stores; l; l = g_list_next (l))
360 EmpathyLogStore *store = EMPATHY_LOG_STORE (l->data);
362 out = g_list_concat (out,
363 empathy_log_store_search_new (store, text));
370 empathy_log_manager_search_hit_free (EmpathyLogSearchHit *hit)
372 if (hit->account != NULL)
373 g_object_unref (hit->account);
376 g_free (hit->filename);
377 g_free (hit->chat_id);
379 g_slice_free (EmpathyLogSearchHit, hit);
383 empathy_log_manager_search_free (GList *hits)
387 for (l = hits; l; l = g_list_next (l))
389 empathy_log_manager_search_hit_free (l->data);
395 /* Format is just date, 20061201. */
397 empathy_log_manager_get_date_readable (const gchar *date)
401 t = empathy_time_parse (date);
403 return empathy_time_to_string_local (t, "%a %d %b %Y");
407 log_manager_chat_received_message_cb (EmpathyTpChat *tp_chat,
408 EmpathyMessage *message,
409 EmpathyLogManager *log_manager)
411 GError *error = NULL;
412 TpHandleType handle_type;
415 channel = empathy_tp_chat_get_channel (tp_chat);
416 tp_channel_get_handle (channel, &handle_type);
418 if (!empathy_log_manager_add_message (log_manager,
419 tp_channel_get_identifier (channel),
420 handle_type == TP_HANDLE_TYPE_ROOM,
423 DEBUG ("Failed to write message: %s",
424 error ? error->message : "No error message");
427 g_error_free (error);
432 log_manager_dispatcher_observe_cb (EmpathyDispatcher *dispatcher,
433 EmpathyDispatchOperation *operation,
434 EmpathyLogManager *log_manager)
438 channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
440 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
442 EmpathyTpChat *tp_chat;
444 tp_chat = EMPATHY_TP_CHAT (
445 empathy_dispatch_operation_get_channel_wrapper (operation));
447 g_signal_connect (tp_chat, "message-received",
448 G_CALLBACK (log_manager_chat_received_message_cb), log_manager);
454 empathy_log_manager_observe (EmpathyLogManager *log_manager,
455 EmpathyDispatcher *dispatcher)
457 g_signal_connect (dispatcher, "observe",
458 G_CALLBACK (log_manager_dispatcher_observe_cb), log_manager);