]> git.0d.be Git - empathy.git/blob - libempathy/empathy-log-store-empathy.c
Support for history message in Adium themes. Use context HTMLs.
[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       empathy_message_set_is_backlog (message, TRUE);
501
502       if (cm_id_str)
503         empathy_message_set_id (message, cm_id);
504
505       messages = g_list_append (messages, message);
506
507       g_object_unref (sender);
508       xmlFree (time);
509       xmlFree (sender_id);
510       xmlFree (sender_name);
511       xmlFree (body);
512       xmlFree (is_user_str);
513       xmlFree (msg_type_str);
514       xmlFree (cm_id_str);
515       xmlFree (sender_avatar_token);
516     }
517
518   DEBUG ("Parsed %d messages", g_list_length (messages));
519
520   xmlFreeDoc (doc);
521   xmlFreeParserCtxt (ctxt);
522
523   return messages;
524 }
525
526 static GList *
527 log_store_empathy_get_all_files (EmpathyLogStore *self,
528                                  const gchar *dir)
529 {
530   GDir *gdir;
531   GList *files = NULL;
532   const gchar *name;
533   const gchar *basedir;
534   EmpathyLogStoreEmpathyPriv *priv;
535
536   priv = GET_PRIV (self);
537
538   basedir = dir ? dir : priv->basedir;
539
540   gdir = g_dir_open (basedir, 0, NULL);
541   if (!gdir)
542     return NULL;
543
544   while ((name = g_dir_read_name (gdir)) != NULL)
545     {
546       gchar *filename;
547
548       filename = g_build_filename (basedir, name, NULL);
549       if (g_str_has_suffix (filename, LOG_FILENAME_SUFFIX))
550         {
551           files = g_list_prepend (files, filename);
552           continue;
553         }
554
555       if (g_file_test (filename, G_FILE_TEST_IS_DIR))
556         {
557           /* Recursively get all log files */
558           files = g_list_concat (files,
559               log_store_empathy_get_all_files (self, filename));
560         }
561
562       g_free (filename);
563     }
564
565   g_dir_close (gdir);
566
567   return files;
568 }
569
570 static GList *
571 log_store_empathy_search_new (EmpathyLogStore *self,
572                               const gchar *text)
573 {
574   GList *files, *l;
575   GList *hits = NULL;
576   gchar *text_casefold;
577
578   g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
579   g_return_val_if_fail (!EMP_STR_EMPTY (text), NULL);
580
581   text_casefold = g_utf8_casefold (text, -1);
582
583   files = log_store_empathy_get_all_files (self, NULL);
584   DEBUG ("Found %d log files in total", g_list_length (files));
585
586   for (l = files; l; l = g_list_next (l))
587     {
588       gchar *filename;
589       GMappedFile *file;
590       gsize length;
591       gchar *contents;
592       gchar *contents_casefold;
593
594       filename = l->data;
595
596       file = g_mapped_file_new (filename, FALSE, NULL);
597       if (!file)
598         continue;
599
600       length = g_mapped_file_get_length (file);
601       contents = g_mapped_file_get_contents (file);
602       contents_casefold = g_utf8_casefold (contents, length);
603
604       g_mapped_file_free (file);
605
606       if (strstr (contents_casefold, text_casefold))
607         {
608           EmpathyLogSearchHit *hit;
609
610           hit = log_store_empathy_search_hit_new (self, filename);
611
612           if (hit)
613             {
614               hits = g_list_prepend (hits, hit);
615               DEBUG ("Found text:'%s' in file:'%s' on date:'%s'",
616                   text, hit->filename, hit->date);
617             }
618         }
619
620       g_free (contents_casefold);
621       g_free (filename);
622     }
623
624   g_list_free (files);
625   g_free (text_casefold);
626
627   return hits;
628 }
629
630 static GList *
631 log_store_empathy_get_chats_for_dir (EmpathyLogStore *self,
632                                      const gchar *dir,
633                                      gboolean is_chatroom)
634 {
635   GDir *gdir;
636   GList *hits = NULL;
637   const gchar *name;
638   GError *error = NULL;
639
640   gdir = g_dir_open (dir, 0, &error);
641   if (!gdir)
642     {
643       DEBUG ("Failed to open directory: %s, error: %s", dir, error->message);
644       g_error_free (error);
645       return NULL;
646     }
647
648   while ((name = g_dir_read_name (gdir)) != NULL)
649     {
650       EmpathyLogSearchHit *hit;
651
652       if (!is_chatroom && strcmp (name, LOG_DIR_CHATROOMS) == 0)
653         {
654           gchar *filename = g_build_filename (dir, name, NULL);
655           hits = g_list_concat (hits, log_store_empathy_get_chats_for_dir (
656                 self, filename, TRUE));
657           g_free (filename);
658           continue;
659         }
660       hit = g_slice_new0 (EmpathyLogSearchHit);
661       hit->chat_id = g_strdup (name);
662       hit->is_chatroom = is_chatroom;
663
664       hits = g_list_prepend (hits, hit);
665     }
666
667   g_dir_close (gdir);
668
669   return hits;
670 }
671
672
673 static GList *
674 log_store_empathy_get_messages_for_date (EmpathyLogStore *self,
675                                          EmpathyAccount *account,
676                                          const gchar *chat_id,
677                                          gboolean chatroom,
678                                          const gchar *date)
679 {
680   gchar *filename;
681   GList *messages;
682
683   g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
684   g_return_val_if_fail (chat_id != NULL, NULL);
685
686   filename = log_store_empathy_get_filename_for_date (self, account,
687       chat_id, chatroom, date);
688   messages = log_store_empathy_get_messages_for_file (self, filename);
689   g_free (filename);
690
691   return messages;
692 }
693
694 static GList *
695 log_store_empathy_get_chats (EmpathyLogStore *self,
696                               EmpathyAccount *account)
697 {
698   gchar *dir;
699   GList *hits;
700   EmpathyLogStoreEmpathyPriv *priv;
701
702   priv = GET_PRIV (self);
703
704   dir = g_build_filename (priv->basedir,
705       empathy_account_get_unique_name (account), NULL);
706
707   hits = log_store_empathy_get_chats_for_dir (self, dir, FALSE);
708
709   g_free (dir);
710
711   return hits;
712 }
713
714 static const gchar *
715 log_store_empathy_get_name (EmpathyLogStore *self)
716 {
717   EmpathyLogStoreEmpathyPriv *priv = GET_PRIV (self);
718
719   return priv->name;
720 }
721
722 static GList *
723 log_store_empathy_get_filtered_messages (EmpathyLogStore *self,
724                                          EmpathyAccount *account,
725                                          const gchar *chat_id,
726                                          gboolean chatroom,
727                                          guint num_messages,
728                                          EmpathyLogMessageFilter filter,
729                                          gpointer user_data)
730 {
731   GList *dates, *l, *messages = NULL;
732   guint i = 0;
733
734   dates = log_store_empathy_get_dates (self, account, chat_id, chatroom);
735
736   for (l = g_list_last (dates); l && i < num_messages; l = g_list_previous (l))
737     {
738       GList *new_messages, *n, *next;
739
740       /* FIXME: We should really restrict the message parsing to get only
741        * the newest num_messages. */
742       new_messages = log_store_empathy_get_messages_for_date (self, account,
743           chat_id, chatroom, l->data);
744
745       n = new_messages;
746       while (n != NULL)
747         {
748           next = g_list_next (n);
749           if (!filter (n->data, user_data))
750             {
751               g_object_unref (n->data);
752               new_messages = g_list_delete_link (new_messages, n);
753             }
754           else
755             {
756               i++;
757             }
758           n = next;
759         }
760       messages = g_list_concat (messages, new_messages);
761     }
762
763   g_list_foreach (dates, (GFunc) g_free, NULL);
764   g_list_free (dates);
765
766   return messages;
767 }
768
769 static void
770 log_store_iface_init (gpointer g_iface,
771                       gpointer iface_data)
772 {
773   EmpathyLogStoreInterface *iface = (EmpathyLogStoreInterface *) g_iface;
774
775   iface->get_name = log_store_empathy_get_name;
776   iface->exists = log_store_empathy_exists;
777   iface->add_message = log_store_empathy_add_message;
778   iface->get_dates = log_store_empathy_get_dates;
779   iface->get_messages_for_date = log_store_empathy_get_messages_for_date;
780   iface->get_chats = log_store_empathy_get_chats;
781   iface->search_new = log_store_empathy_search_new;
782   iface->ack_message = NULL;
783   iface->get_filtered_messages = log_store_empathy_get_filtered_messages;
784 }