]> git.0d.be Git - empathy.git/blob - libempathy/empathy-log-manager.c
Started splitting empathy log code from the log manager to make logging more pluggabl...
[empathy.git] / libempathy / empathy-log-manager.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2003-2007 Imendio AB
4  * Copyright (C) 2007-2008 Collabora Ltd.
5  *
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.
10  *
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.
15  *
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.
20  *
21  * Authors: Xavier Claessens <xclaesse@gmail.com>
22  *          Jonny Lamb <jonny.lamb@collabora.co.uk>
23  */
24
25 #include <config.h>
26
27 #include <string.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <glib/gstdio.h>
31
32 #include "empathy-log-manager.h"
33 #include "empathy-log-source-empathy.h"
34 #include "empathy-utils.h"
35
36 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
37 #include "empathy-debug.h"
38
39 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLogManager)
40 typedef struct {
41         GList *sources;
42 } EmpathyLogManagerPriv;
43
44 G_DEFINE_TYPE (EmpathyLogManager, empathy_log_manager, G_TYPE_OBJECT);
45
46 static EmpathyLogManager * manager_singleton = NULL;
47
48 static void
49 empathy_log_manager_init (EmpathyLogManager *manager)
50 {
51         EmpathyLogManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
52                 EMPATHY_TYPE_LOG_MANAGER, EmpathyLogManagerPriv);
53         manager->priv = priv;
54 }
55
56 static void
57 log_manager_finalize (GObject *object)
58 {
59         EmpathyLogManagerPriv *priv;
60         GList *l;
61
62         priv = GET_PRIV (object);
63
64         for (l = priv->sources; l; l = l->next) {
65                 g_slice_free (EmpathyLogSource, l->data);
66         }
67
68         g_list_free (priv->sources);
69 }
70
71 static GObject *
72 log_manager_constructor (GType type,
73                          guint n_props,
74                          GObjectConstructParam *props)
75 {
76         GObject *retval;
77
78         if (manager_singleton) {
79                 retval = g_object_ref (manager_singleton);
80         } else {
81                 retval = G_OBJECT_CLASS (empathy_log_manager_parent_class)->constructor
82                         (type, n_props, props);
83
84                 manager_singleton = EMPATHY_LOG_MANAGER (retval);
85                 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
86         }
87
88         return retval;
89 }
90
91 static void
92 empathy_log_manager_class_init (EmpathyLogManagerClass *klass)
93 {
94         GObjectClass *object_class = G_OBJECT_CLASS (klass);
95
96         object_class->finalize = log_manager_finalize;
97         object_class->constructor = log_manager_constructor;
98
99         g_type_class_add_private (object_class, sizeof (EmpathyLogManagerPriv));
100 }
101
102 EmpathyLogManager *
103 empathy_log_manager_dup_singleton (void)
104 {
105         return g_object_new (EMPATHY_TYPE_LOG_MANAGER, NULL);
106 }
107
108 void
109 empathy_log_manager_add_message (EmpathyLogManager *manager,
110                                  const gchar       *chat_id,
111                                  gboolean           chatroom,
112                                  EmpathyMessage     *message)
113 {
114         EmpathyLogManagerPriv *priv;
115         GList *l;
116
117         g_return_if_fail (EMPATHY_IS_LOG_MANAGER (manager));
118         g_return_if_fail (chat_id != NULL);
119         g_return_if_fail (EMPATHY_IS_MESSAGE (message));
120
121         priv = GET_PRIV (manager);
122
123         for (l = priv->sources; l; l = l->next) {
124                 EmpathyLogSource *source = (EmpathyLogSource *) l->data;
125
126                 if (!source->add_message)
127                         continue;
128
129                 source->add_message (manager, chat_id, chatroom, message);
130         }
131 }
132
133 gboolean
134 empathy_log_manager_exists (EmpathyLogManager *manager,
135                             McAccount         *account,
136                             const gchar       *chat_id,
137                             gboolean           chatroom)
138 {
139         GList *l;
140         EmpathyLogManagerPriv *priv;
141
142         g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), FALSE);
143         g_return_val_if_fail (MC_IS_ACCOUNT (account), FALSE);
144         g_return_val_if_fail (chat_id != NULL, FALSE);
145
146         priv = GET_PRIV (manager);
147
148         for (l = priv->sources; l; l = l->next) {
149                 EmpathyLogSource *source = (EmpathyLogSource *) l->data;
150
151                 if (!source->exists)
152                         continue;
153
154                 if (source->exists (manager, account, chat_id, chatroom)) {
155                         return TRUE;
156                 }
157         }
158
159         return FALSE;
160 }
161
162 GList *
163 empathy_log_manager_get_dates (EmpathyLogManager *manager,
164                                McAccount         *account,
165                                const gchar       *chat_id,
166                                gboolean           chatroom)
167 {
168         GList *l, *out = NULL;
169         EmpathyLogManagerPriv *priv;
170
171         g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), NULL);
172         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
173         g_return_val_if_fail (chat_id != NULL, NULL);
174
175         priv = GET_PRIV (manager);
176
177         for (l = priv->sources; l; l = l->next) {
178                 EmpathyLogSource *source = (EmpathyLogSource *) l->data;
179
180                 if (!source->get_dates)
181                         continue;
182
183                 if (!out) {
184                         out = source->get_dates (manager, account, chat_id, chatroom);
185                 } else {
186                         /* TODO fix this */
187                         out = g_list_concat (out, source->get_dates (manager, account, chat_id, chatroom));
188                 }
189         }
190
191         return out;
192 }
193
194 GList *
195 empathy_log_manager_get_messages_for_date (EmpathyLogManager *manager,
196                                            McAccount         *account,
197                                            const gchar       *chat_id,
198                                            gboolean           chatroom,
199                                            const gchar       *date)
200 {
201         GList *l, *out = NULL;
202         EmpathyLogManagerPriv *priv;
203
204         g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), NULL);
205         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
206         g_return_val_if_fail (chat_id != NULL, NULL);
207
208         priv = GET_PRIV (manager);
209
210         for (l = priv->sources; l; l = l->next) {
211                 EmpathyLogSource *source = (EmpathyLogSource *) l->data;
212
213                 if (!source->get_messages_for_date)
214                         continue;
215
216                 if (!out) {
217                         out = source->get_messages_for_date (manager, account, chat_id, chatroom, date);
218                 } else {
219                         out = g_list_concat (out, source->get_messages_for_date (manager, account, chat_id, chatroom, date));
220                 }
221         }
222
223         return out;
224 }
225
226 GList *
227 empathy_log_manager_get_last_messages (EmpathyLogManager *manager,
228                                        McAccount         *account,
229                                        const gchar       *chat_id,
230                                        gboolean           chatroom)
231 {
232         GList *l, *out = NULL;
233         EmpathyLogManagerPriv *priv;
234
235         g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), NULL);
236         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
237         g_return_val_if_fail (chat_id != NULL, NULL);
238
239         priv = GET_PRIV (manager);
240
241         for (l = priv->sources; l; l = l->next) {
242                 EmpathyLogSource *source = (EmpathyLogSource *) l->data;
243
244                 if (!source->get_last_messages)
245                         continue;
246
247                 if (!out) {
248                         out = source->get_last_messages (manager, account, chat_id, chatroom);
249                 } else {
250                         out = g_list_concat (out, source->get_last_messages (manager, account, chat_id, chatroom));
251                 }
252         }
253
254         return out;
255 }
256
257 GList *
258 empathy_log_manager_get_chats (EmpathyLogManager *manager,
259                                McAccount         *account)
260 {
261         GList *l, *out = NULL;
262         EmpathyLogManagerPriv *priv;
263
264         g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), NULL);
265         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
266
267         priv = GET_PRIV (manager);
268
269         DEBUG ("size of sources is %i", g_list_length (priv->sources));
270
271         for (l = priv->sources; l; l = l->next) {
272                 EmpathyLogSource *source = (EmpathyLogSource *) l->data;
273
274                 if (!source->get_chats)
275                         continue;
276
277                 if (!out) {
278                         out = source->get_chats (manager, account);
279                 } else {
280                         out = g_list_concat (out, source->get_chats (manager, account));
281                 }
282         }
283
284         return out;
285 }
286
287 GList *
288 empathy_log_manager_search_new (EmpathyLogManager *manager,
289                                 const gchar       *text)
290 {
291         GList *l, *out = NULL;
292         EmpathyLogManagerPriv *priv;
293
294         g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), NULL);
295         g_return_val_if_fail (!EMP_STR_EMPTY (text), NULL);
296
297         priv = GET_PRIV (manager);
298
299         for (l = priv->sources; l; l = l->next) {
300                 EmpathyLogSource *source = (EmpathyLogSource *) l->data;
301
302                 if (!source->search_new)
303                         continue;
304
305                 if (!out) {
306                         out = source->search_new (manager, text);
307                 } else {
308                         out = g_list_concat (out, source->search_new (manager, text));
309                 }
310         }
311
312         return out;
313 }
314
315 void
316 empathy_log_manager_search_hit_free (EmpathyLogSearchHit *hit)
317 {
318         if (hit->account) {
319                 g_object_unref (hit->account);
320         }
321
322         g_free (hit->date);
323         g_free (hit->filename);
324         g_free (hit->chat_id);
325
326         g_slice_free (EmpathyLogSearchHit, hit);
327 }
328
329 void
330 empathy_log_manager_search_free (GList *hits)
331 {
332         GList *l;
333
334         for (l = hits; l; l = l->next) {
335                 empathy_log_manager_search_hit_free (l->data);
336         }
337         
338         g_list_free (hits);
339 }
340
341 /* Format is just date, 20061201. */
342 gchar *
343 empathy_log_manager_get_date_readable (const gchar *date)
344 {
345         time_t t;
346
347         t = empathy_time_parse (date);
348
349         return empathy_time_to_string_local (t, "%a %d %b %Y");
350 }