]> git.0d.be Git - empathy.git/blob - libempathy/empathy-log-store-empathy.c
Added functions to determine if a contact has video capabilities
[empathy.git] / libempathy / empathy-log-store-empathy.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., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  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-store.h"
33 #include "empathy-log-store-empathy.h"
34 #include "empathy-log-manager.h"
35 #include "empathy-account-manager.h"
36 #include "empathy-contact.h"
37 #include "empathy-time.h"
38 #include "empathy-utils.h"
39
40 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
41 #include "empathy-debug.h"
42
43 #define LOG_DIR_CREATE_MODE       (S_IRUSR | S_IWUSR | S_IXUSR)
44 #define LOG_FILE_CREATE_MODE      (S_IRUSR | S_IWUSR)
45 #define LOG_DIR_CHATROOMS         "chatrooms"
46 #define LOG_FILENAME_SUFFIX       ".log"
47 #define LOG_TIME_FORMAT_FULL      "%Y%m%dT%H:%M:%S"
48 #define LOG_TIME_FORMAT           "%Y%m%d"
49 #define LOG_HEADER \
50     "<?xml version='1.0' encoding='utf-8'?>\n" \
51     "<?xml-stylesheet type=\"text/xsl\" href=\"empathy-log.xsl\"?>\n" \
52     "<log>\n"
53
54 #define LOG_FOOTER \
55     "</log>\n"
56
57
58 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLogStoreEmpathy)
59 typedef struct
60 {
61   gchar *basedir;
62   gchar *name;
63   EmpathyAccountManager *account_manager;
64 } EmpathyLogStoreEmpathyPriv;
65
66 static void log_store_iface_init (gpointer g_iface,gpointer iface_data);
67
68 G_DEFINE_TYPE_WITH_CODE (EmpathyLogStoreEmpathy, empathy_log_store_empathy,
69     G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_LOG_STORE,
70       log_store_iface_init));
71
72 static void
73 log_store_empathy_finalize (GObject *object)
74 {
75   EmpathyLogStoreEmpathy *self = EMPATHY_LOG_STORE_EMPATHY (object);
76   EmpathyLogStoreEmpathyPriv *priv = GET_PRIV (self);
77
78   g_object_unref (priv->account_manager);
79   g_free (priv->basedir);
80   g_free (priv->name);
81 }
82
83 static void
84 empathy_log_store_empathy_class_init (EmpathyLogStoreEmpathyClass *klass)
85 {
86   GObjectClass *object_class = G_OBJECT_CLASS (klass);
87
88   object_class->finalize = log_store_empathy_finalize;
89
90   g_type_class_add_private (object_class, sizeof (EmpathyLogStoreEmpathyPriv));
91 }
92
93 static void
94 empathy_log_store_empathy_init (EmpathyLogStoreEmpathy *self)
95 {
96   EmpathyLogStoreEmpathyPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
97       EMPATHY_TYPE_LOG_STORE_EMPATHY, EmpathyLogStoreEmpathyPriv);
98
99   self->priv = priv;
100
101   priv->basedir = g_build_path (G_DIR_SEPARATOR_S, g_get_home_dir (),
102       ".gnome2", PACKAGE_NAME, "logs", NULL);
103
104   priv->name = g_strdup ("Empathy");
105   priv->account_manager = empathy_account_manager_dup_singleton ();
106 }
107
108 static gchar *
109 log_store_empathy_get_dir (EmpathyLogStore *self,
110                            EmpathyAccount *account,
111                            const gchar *chat_id,
112                            gboolean chatroom)
113 {
114   const gchar *account_id;
115   gchar *basedir;
116   EmpathyLogStoreEmpathyPriv *priv;
117
118   priv = GET_PRIV (self);
119
120   account_id = empathy_account_get_unique_name (account);
121
122   if (chatroom)
123     basedir = g_build_path (G_DIR_SEPARATOR_S, priv->basedir, account_id,
124         LOG_DIR_CHATROOMS, chat_id, NULL);
125   else
126     basedir = g_build_path (G_DIR_SEPARATOR_S, priv->basedir,
127         account_id, chat_id, NULL);
128
129   return basedir;
130 }
131
132 static gchar *
133 log_store_empathy_get_timestamp_filename (void)
134 {
135   time_t t;
136   gchar *time_str;
137   gchar *filename;
138
139   t = empathy_time_get_current ();
140   time_str = empathy_time_to_string_local (t, LOG_TIME_FORMAT);
141   filename = g_strconcat (time_str, LOG_FILENAME_SUFFIX, NULL);
142
143   g_free (time_str);
144
145   return filename;
146 }
147
148 static gchar *
149 log_store_empathy_get_timestamp_from_message (EmpathyMessage *message)
150 {
151   time_t t;
152
153   t = empathy_message_get_timestamp (message);
154
155   /* We keep the timestamps in the messages as UTC. */
156   return empathy_time_to_string_utc (t, LOG_TIME_FORMAT_FULL);
157 }
158
159 static gchar *
160 log_store_empathy_get_filename (EmpathyLogStore *self,
161                                 EmpathyAccount *account,
162                                 const gchar *chat_id,
163                                 gboolean chatroom)
164 {
165   gchar *basedir;
166   gchar *timestamp;
167   gchar *filename;
168
169   basedir = log_store_empathy_get_dir (self, account, chat_id, chatroom);
170   timestamp = log_store_empathy_get_timestamp_filename ();
171   filename = g_build_filename (basedir, timestamp, NULL);
172
173   g_free (basedir);
174   g_free (timestamp);
175
176   return filename;
177 }
178
179 static gboolean
180 log_store_empathy_add_message (EmpathyLogStore *self,
181                                const gchar *chat_id,
182                                gboolean chatroom,
183                                EmpathyMessage *message,
184                                GError **error)
185 {
186   FILE *file;
187   EmpathyAccount *account;
188   EmpathyContact *sender;
189   const gchar *body_str;
190   const gchar *str;
191   EmpathyAvatar *avatar;
192   gchar *avatar_token = NULL;
193   gchar *filename;
194   gchar *basedir;
195   gchar *body;
196   gchar *timestamp;
197   gchar *contact_name;
198   gchar *contact_id;
199   TpChannelTextMessageType msg_type;
200
201   g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), FALSE);
202   g_return_val_if_fail (chat_id != NULL, FALSE);
203   g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
204
205   sender = empathy_message_get_sender (message);
206   account = empathy_contact_get_account (sender);
207   body_str = empathy_message_get_body (message);
208   msg_type = empathy_message_get_tptype (message);
209
210   if (EMP_STR_EMPTY (body_str))
211     return FALSE;
212
213   filename = log_store_empathy_get_filename (self, account, chat_id, chatroom);
214   basedir = g_path_get_dirname (filename);
215   if (!g_file_test (basedir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
216     {
217       DEBUG ("Creating directory:'%s'", basedir);
218       g_mkdir_with_parents (basedir, LOG_DIR_CREATE_MODE);
219     }
220   g_free (basedir);
221
222   DEBUG ("Adding message: '%s' to file: '%s'", body_str, filename);
223
224   if (!g_file_test (filename, G_FILE_TEST_EXISTS))
225     {
226       file = g_fopen (filename, "w+");
227       if (file != NULL)
228         g_fprintf (file, LOG_HEADER);
229
230       g_chmod (filename, LOG_FILE_CREATE_MODE);
231     }
232   else
233     {
234       file = g_fopen (filename, "r+");
235       if (file != NULL)
236         fseek (file, - strlen (LOG_FOOTER), SEEK_END);
237     }
238
239   body = g_markup_escape_text (body_str, -1);
240   timestamp = log_store_empathy_get_timestamp_from_message (message);
241
242   str = empathy_contact_get_name (sender);
243   contact_name = g_markup_escape_text (str, -1);
244
245   str = empathy_contact_get_id (sender);
246   contact_id = g_markup_escape_text (str, -1);
247
248   avatar = empathy_contact_get_avatar (sender);
249   if (avatar != NULL)
250     avatar_token = g_markup_escape_text (avatar->token, -1);
251
252   g_fprintf (file,
253        "<message time='%s' cm_id='%d' id='%s' name='%s' token='%s' isuser='%s' type='%s'>"
254        "%s</message>\n" LOG_FOOTER, timestamp,
255        empathy_message_get_id (message),
256        contact_id, contact_name,
257        avatar_token ? avatar_token : "",
258        empathy_contact_is_user (sender) ? "true" : "false",
259        empathy_message_type_to_str (msg_type), body);
260
261   fclose (file);
262   g_free (filename);
263   g_free (contact_id);
264   g_free (contact_name);
265   g_free (timestamp);
266   g_free (body);
267   g_free (avatar_token);
268
269   return TRUE;
270 }
271
272 static gboolean
273 log_store_empathy_exists (EmpathyLogStore *self,
274                           EmpathyAccount *account,
275                           const gchar *chat_id,
276                           gboolean chatroom)
277 {
278   gchar *dir;
279   gboolean exists;
280
281   dir = log_store_empathy_get_dir (self, account, chat_id, chatroom);
282   exists = g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
283   g_free (dir);
284
285   return exists;
286 }
287
288 static GList *
289 log_store_empathy_get_dates (EmpathyLogStore *self,
290                              EmpathyAccount *account,
291                              const gchar *chat_id,
292                              gboolean chatroom)
293 {
294   GList *dates = NULL;
295   gchar *date;
296   gchar *directory;
297   GDir *dir;
298   const gchar *filename;
299   const gchar *p;
300
301   g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
302   g_return_val_if_fail (chat_id != NULL, NULL);
303
304   directory = log_store_empathy_get_dir (self, account, chat_id, chatroom);
305   dir = g_dir_open (directory, 0, NULL);
306   if (!dir)
307     {
308       DEBUG ("Could not open directory:'%s'", directory);
309       g_free (directory);
310       return NULL;
311     }
312
313   DEBUG ("Collating a list of dates in:'%s'", directory);
314
315   while ((filename = g_dir_read_name (dir)) != NULL)
316     {
317       if (!g_str_has_suffix (filename, LOG_FILENAME_SUFFIX))
318         continue;
319
320       p = strstr (filename, LOG_FILENAME_SUFFIX);
321       date = g_strndup (filename, p - filename);
322
323       if (!date)
324         continue;
325
326       if (!g_regex_match_simple ("\\d{8}", date, 0, 0))
327         continue;
328
329       dates = g_list_insert_sorted (dates, date, (GCompareFunc) strcmp);
330     }
331
332   g_free (directory);
333   g_dir_close (dir);
334
335   DEBUG ("Parsed %d dates", g_list_length (dates));
336
337   return dates;
338 }
339
340 static gchar *
341 log_store_empathy_get_filename_for_date (EmpathyLogStore *self,
342                                          EmpathyAccount *account,
343                                          const gchar *chat_id,
344                                          gboolean chatroom,
345                                          const gchar *date)
346 {
347   gchar *basedir;
348   gchar *timestamp;
349   gchar *filename;
350
351   basedir = log_store_empathy_get_dir (self, account, chat_id, chatroom);
352   timestamp = g_strconcat (date, LOG_FILENAME_SUFFIX, NULL);
353   filename = g_build_filename (basedir, timestamp, NULL);
354
355   g_free (basedir);
356   g_free (timestamp);
357
358   return filename;
359 }
360
361 static EmpathyLogSearchHit *
362 log_store_empathy_search_hit_new (EmpathyLogStore *self,
363                                   const gchar *filename)
364 {
365   EmpathyLogStoreEmpathyPriv *priv = GET_PRIV (self);
366   EmpathyLogSearchHit *hit;
367   const gchar *account_name;
368   const gchar *end;
369   gchar **strv;
370   guint len;
371
372   if (!g_str_has_suffix (filename, LOG_FILENAME_SUFFIX))
373     return NULL;
374
375   strv = g_strsplit (filename, G_DIR_SEPARATOR_S, -1);
376   len = g_strv_length (strv);
377
378   hit = g_slice_new0 (EmpathyLogSearchHit);
379
380   end = strstr (strv[len-1], LOG_FILENAME_SUFFIX);
381   hit->date = g_strndup (strv[len-1], end - strv[len-1]);
382   hit->chat_id = g_strdup (strv[len-2]);
383   hit->is_chatroom = (strcmp (strv[len-3], LOG_DIR_CHATROOMS) == 0);
384
385   if (hit->is_chatroom)
386     account_name = strv[len-4];
387   else
388     account_name = strv[len-3];
389
390   hit->account = empathy_account_manager_lookup (priv->account_manager,
391     account_name);
392   hit->filename = g_strdup (filename);
393
394   g_strfreev (strv);
395
396   return hit;
397 }
398
399 static GList *
400 log_store_empathy_get_messages_for_file (EmpathyLogStore *self,
401                                          const gchar *filename)
402 {
403   GList *messages = NULL;
404   xmlParserCtxtPtr ctxt;
405   xmlDocPtr doc;
406   xmlNodePtr log_node;
407   xmlNodePtr node;
408   EmpathyLogSearchHit *hit;
409   EmpathyAccount *account;
410
411   g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
412   g_return_val_if_fail (filename != NULL, NULL);
413
414   DEBUG ("Attempting to parse filename:'%s'...", filename);
415
416   if (!g_file_test (filename, G_FILE_TEST_EXISTS))
417     {
418       DEBUG ("Filename:'%s' does not exist", filename);
419       return NULL;
420     }
421
422   /* Get the account from the filename */
423   hit = log_store_empathy_search_hit_new (self, filename);
424   account = g_object_ref (hit->account);
425   empathy_log_manager_search_hit_free (hit);
426
427   /* Create parser. */
428   ctxt = xmlNewParserCtxt ();
429
430   /* Parse and validate the file. */
431   doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
432   if (!doc)
433     {
434       g_warning ("Failed to parse file:'%s'", filename);
435       xmlFreeParserCtxt (ctxt);
436       return NULL;
437     }
438
439   /* The root node, presets. */
440   log_node = xmlDocGetRootElement (doc);
441   if (!log_node)
442     {
443       xmlFreeDoc (doc);
444       xmlFreeParserCtxt (ctxt);
445       return NULL;
446     }
447
448   /* Now get the messages. */
449   for (node = log_node->children; node; node = node->next)
450     {
451       EmpathyMessage *message;
452       EmpathyContact *sender;
453       gchar *time;
454       time_t t;
455       gchar *sender_id;
456       gchar *sender_name;
457       gchar *sender_avatar_token;
458       gchar *body;
459       gchar *is_user_str;
460       gboolean is_user = FALSE;
461       gchar *msg_type_str;
462       gchar *cm_id_str;
463       guint cm_id;
464       TpChannelTextMessageType msg_type = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
465
466       if (strcmp (node->name, "message") != 0)
467         continue;
468
469       body = xmlNodeGetContent (node);
470       time = xmlGetProp (node, "time");
471       sender_id = xmlGetProp (node, "id");
472       sender_name = xmlGetProp (node, "name");
473       sender_avatar_token = xmlGetProp (node, "token");
474       is_user_str = xmlGetProp (node, "isuser");
475       msg_type_str = xmlGetProp (node, "type");
476       cm_id_str = xmlGetProp (node, "cm_id");
477
478       if (is_user_str)
479         is_user = strcmp (is_user_str, "true") == 0;
480
481       if (msg_type_str)
482         msg_type = empathy_message_type_from_str (msg_type_str);
483
484       if (cm_id_str)
485         cm_id = atoi (cm_id_str);
486
487       t = empathy_time_parse (time);
488
489       sender = empathy_contact_new_for_log (account, sender_id, sender_name,
490                                             is_user);
491
492       if (!EMP_STR_EMPTY (sender_avatar_token))
493         empathy_contact_load_avatar_cache (sender,
494             sender_avatar_token);
495
496       message = empathy_message_new (body);
497       empathy_message_set_sender (message, sender);
498       empathy_message_set_timestamp (message, t);
499       empathy_message_set_tptype (message, msg_type);
500
501       if (cm_id_str)
502         empathy_message_set_id (message, cm_id);
503
504       messages = g_list_append (messages, message);
505
506       g_object_unref (sender);
507       xmlFree (time);
508       xmlFree (sender_id);
509       xmlFree (sender_name);
510       xmlFree (body);
511       xmlFree (is_user_str);
512       xmlFree (msg_type_str);
513       xmlFree (cm_id_str);
514       xmlFree (sender_avatar_token);
515     }
516
517   DEBUG ("Parsed %d messages", g_list_length (messages));
518
519   xmlFreeDoc (doc);
520   xmlFreeParserCtxt (ctxt);
521
522   return messages;
523 }
524
525 static GList *
526 log_store_empathy_get_all_files (EmpathyLogStore *self,
527                                  const gchar *dir)
528 {
529   GDir *gdir;
530   GList *files = NULL;
531   const gchar *name;
532   const gchar *basedir;
533   EmpathyLogStoreEmpathyPriv *priv;
534
535   priv = GET_PRIV (self);
536
537   basedir = dir ? dir : priv->basedir;
538
539   gdir = g_dir_open (basedir, 0, NULL);
540   if (!gdir)
541     return NULL;
542
543   while ((name = g_dir_read_name (gdir)) != NULL)
544     {
545       gchar *filename;
546
547       filename = g_build_filename (basedir, name, NULL);
548       if (g_str_has_suffix (filename, LOG_FILENAME_SUFFIX))
549         {
550           files = g_list_prepend (files, filename);
551           continue;
552         }
553
554       if (g_file_test (filename, G_FILE_TEST_IS_DIR))
555         {
556           /* Recursively get all log files */
557           files = g_list_concat (files,
558               log_store_empathy_get_all_files (self, filename));
559         }
560
561       g_free (filename);
562     }
563
564   g_dir_close (gdir);
565
566   return files;
567 }
568
569 static GList *
570 log_store_empathy_search_new (EmpathyLogStore *self,
571                               const gchar *text)
572 {
573   GList *files, *l;
574   GList *hits = NULL;
575   gchar *text_casefold;
576
577   g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
578   g_return_val_if_fail (!EMP_STR_EMPTY (text), NULL);
579
580   text_casefold = g_utf8_casefold (text, -1);
581
582   files = log_store_empathy_get_all_files (self, NULL);
583   DEBUG ("Found %d log files in total", g_list_length (files));
584
585   for (l = files; l; l = g_list_next (l))
586     {
587       gchar *filename;
588       GMappedFile *file;
589       gsize length;
590       gchar *contents;
591       gchar *contents_casefold;
592
593       filename = l->data;
594
595       file = g_mapped_file_new (filename, FALSE, NULL);
596       if (!file)
597         continue;
598
599       length = g_mapped_file_get_length (file);
600       contents = g_mapped_file_get_contents (file);
601       contents_casefold = g_utf8_casefold (contents, length);
602
603       g_mapped_file_free (file);
604
605       if (strstr (contents_casefold, text_casefold))
606         {
607           EmpathyLogSearchHit *hit;
608
609           hit = log_store_empathy_search_hit_new (self, filename);
610
611           if (hit)
612             {
613               hits = g_list_prepend (hits, hit);
614               DEBUG ("Found text:'%s' in file:'%s' on date:'%s'",
615                   text, hit->filename, hit->date);
616             }
617         }
618
619       g_free (contents_casefold);
620       g_free (filename);
621     }
622
623   g_list_free (files);
624   g_free (text_casefold);
625
626   return hits;
627 }
628
629 static GList *
630 log_store_empathy_get_chats_for_dir (EmpathyLogStore *self,
631                                      const gchar *dir,
632                                      gboolean is_chatroom)
633 {
634   GDir *gdir;
635   GList *hits = NULL;
636   const gchar *name;
637   GError *error = NULL;
638
639   gdir = g_dir_open (dir, 0, &error);
640   if (!gdir)
641     {
642       DEBUG ("Failed to open directory: %s, error: %s", dir, error->message);
643       g_error_free (error);
644       return NULL;
645     }
646
647   while ((name = g_dir_read_name (gdir)) != NULL)
648     {
649       EmpathyLogSearchHit *hit;
650
651       if (!is_chatroom && strcmp (name, LOG_DIR_CHATROOMS) == 0)
652         {
653           gchar *filename = g_build_filename (dir, name, NULL);
654           hits = g_list_concat (hits, log_store_empathy_get_chats_for_dir (
655                 self, filename, TRUE));
656           g_free (filename);
657           continue;
658         }
659       hit = g_slice_new0 (EmpathyLogSearchHit);
660       hit->chat_id = g_strdup (name);
661       hit->is_chatroom = is_chatroom;
662
663       hits = g_list_prepend (hits, hit);
664     }
665
666   g_dir_close (gdir);
667
668   return hits;
669 }
670
671
672 static GList *
673 log_store_empathy_get_messages_for_date (EmpathyLogStore *self,
674                                          EmpathyAccount *account,
675                                          const gchar *chat_id,
676                                          gboolean chatroom,
677                                          const gchar *date)
678 {
679   gchar *filename;
680   GList *messages;
681
682   g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
683   g_return_val_if_fail (chat_id != NULL, NULL);
684
685   filename = log_store_empathy_get_filename_for_date (self, account,
686       chat_id, chatroom, date);
687   messages = log_store_empathy_get_messages_for_file (self, filename);
688   g_free (filename);
689
690   return messages;
691 }
692
693 static GList *
694 log_store_empathy_get_chats (EmpathyLogStore *self,
695                               EmpathyAccount *account)
696 {
697   gchar *dir;
698   GList *hits;
699   EmpathyLogStoreEmpathyPriv *priv;
700
701   priv = GET_PRIV (self);
702
703   dir = g_build_filename (priv->basedir,
704       empathy_account_get_unique_name (account), NULL);
705
706   hits = log_store_empathy_get_chats_for_dir (self, dir, FALSE);
707
708   g_free (dir);
709
710   return hits;
711 }
712
713 static const gchar *
714 log_store_empathy_get_name (EmpathyLogStore *self)
715 {
716   EmpathyLogStoreEmpathyPriv *priv = GET_PRIV (self);
717
718   return priv->name;
719 }
720
721 static GList *
722 log_store_empathy_get_filtered_messages (EmpathyLogStore *self,
723                                          EmpathyAccount *account,
724                                          const gchar *chat_id,
725                                          gboolean chatroom,
726                                          guint num_messages,
727                                          EmpathyLogMessageFilter filter,
728                                          gpointer user_data)
729 {
730   GList *dates, *l, *messages = NULL;
731   guint i = 0;
732
733   dates = log_store_empathy_get_dates (self, account, chat_id, chatroom);
734
735   for (l = g_list_last (dates); l && i < num_messages; l = g_list_previous (l))
736     {
737       GList *new_messages, *n, *next;
738
739       /* FIXME: We should really restrict the message parsing to get only
740        * the newest num_messages. */
741       new_messages = log_store_empathy_get_messages_for_date (self, account,
742           chat_id, chatroom, l->data);
743
744       n = new_messages;
745       while (n != NULL)
746         {
747           next = g_list_next (n);
748           if (!filter (n->data, user_data))
749             {
750               g_object_unref (n->data);
751               new_messages = g_list_delete_link (new_messages, n);
752             }
753           else
754             {
755               i++;
756             }
757           n = next;
758         }
759       messages = g_list_concat (messages, new_messages);
760     }
761
762   g_list_foreach (dates, (GFunc) g_free, NULL);
763   g_list_free (dates);
764
765   return messages;
766 }
767
768 static void
769 log_store_iface_init (gpointer g_iface,
770                       gpointer iface_data)
771 {
772   EmpathyLogStoreInterface *iface = (EmpathyLogStoreInterface *) g_iface;
773
774   iface->get_name = log_store_empathy_get_name;
775   iface->exists = log_store_empathy_exists;
776   iface->add_message = log_store_empathy_add_message;
777   iface->get_dates = log_store_empathy_get_dates;
778   iface->get_messages_for_date = log_store_empathy_get_messages_for_date;
779   iface->get_chats = log_store_empathy_get_chats;
780   iface->search_new = log_store_empathy_search_new;
781   iface->ack_message = NULL;
782   iface->get_filtered_messages = log_store_empathy_get_filtered_messages;
783 }