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