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