]> git.0d.be Git - empathy.git/blob - libempathy/empathy-log-store-empathy.c
Fallback to setting the global presence to offline if no account has a better presence.
[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 *account_name;
393   const gchar *end;
394   gchar **strv;
395   guint len;
396   GList *accounts, *l;
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   accounts = empathy_account_manager_dup_accounts (priv->account_manager);
417
418   for (l = accounts; l != NULL; l = g_list_next (l))
419     {
420       EmpathyAccount *account = EMPATHY_ACCOUNT (l->data);
421       gchar *name;
422
423       name = log_store_account_to_dirname (account);
424       if (!tp_strdiff (name, account_name))
425         {
426           g_assert (hit->account == NULL);
427           hit->account = account;
428           g_object_ref (account);
429         }
430       g_object_unref (account);
431       g_free (name);
432     }
433   g_list_free (accounts);
434
435   hit->filename = g_strdup (filename);
436
437   g_strfreev (strv);
438
439   return hit;
440 }
441
442 static GList *
443 log_store_empathy_get_messages_for_file (EmpathyLogStore *self,
444                                          const gchar *filename)
445 {
446   GList *messages = NULL;
447   xmlParserCtxtPtr ctxt;
448   xmlDocPtr doc;
449   xmlNodePtr log_node;
450   xmlNodePtr node;
451   EmpathyLogSearchHit *hit;
452   EmpathyAccount *account;
453
454   g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
455   g_return_val_if_fail (filename != NULL, NULL);
456
457   DEBUG ("Attempting to parse filename:'%s'...", filename);
458
459   if (!g_file_test (filename, G_FILE_TEST_EXISTS))
460     {
461       DEBUG ("Filename:'%s' does not exist", filename);
462       return NULL;
463     }
464
465   /* Get the account from the filename */
466   hit = log_store_empathy_search_hit_new (self, filename);
467
468   if (hit->account != NULL)
469     account = g_object_ref (hit->account);
470
471   empathy_log_manager_search_hit_free (hit);
472
473   if (hit->account == NULL)
474     return NULL;
475
476   /* Create parser. */
477   ctxt = xmlNewParserCtxt ();
478
479   /* Parse and validate the file. */
480   doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
481   if (!doc)
482     {
483       g_warning ("Failed to parse file:'%s'", filename);
484       xmlFreeParserCtxt (ctxt);
485       return NULL;
486     }
487
488   /* The root node, presets. */
489   log_node = xmlDocGetRootElement (doc);
490   if (!log_node)
491     {
492       xmlFreeDoc (doc);
493       xmlFreeParserCtxt (ctxt);
494       return NULL;
495     }
496
497   /* Now get the messages. */
498   for (node = log_node->children; node; node = node->next)
499     {
500       EmpathyMessage *message;
501       EmpathyContact *sender;
502       gchar *time;
503       time_t t;
504       gchar *sender_id;
505       gchar *sender_name;
506       gchar *sender_avatar_token;
507       gchar *body;
508       gchar *is_user_str;
509       gboolean is_user = FALSE;
510       gchar *msg_type_str;
511       gchar *cm_id_str;
512       guint cm_id;
513       TpChannelTextMessageType msg_type = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
514
515       if (strcmp (node->name, "message") != 0)
516         continue;
517
518       body = xmlNodeGetContent (node);
519       time = xmlGetProp (node, "time");
520       sender_id = xmlGetProp (node, "id");
521       sender_name = xmlGetProp (node, "name");
522       sender_avatar_token = xmlGetProp (node, "token");
523       is_user_str = xmlGetProp (node, "isuser");
524       msg_type_str = xmlGetProp (node, "type");
525       cm_id_str = xmlGetProp (node, "cm_id");
526
527       if (is_user_str)
528         is_user = strcmp (is_user_str, "true") == 0;
529
530       if (msg_type_str)
531         msg_type = empathy_message_type_from_str (msg_type_str);
532
533       if (cm_id_str)
534         cm_id = atoi (cm_id_str);
535
536       t = empathy_time_parse (time);
537
538       sender = empathy_contact_new_for_log (account, sender_id, sender_name,
539                                             is_user);
540
541       if (!EMP_STR_EMPTY (sender_avatar_token))
542         empathy_contact_load_avatar_cache (sender,
543             sender_avatar_token);
544
545       message = empathy_message_new (body);
546       empathy_message_set_sender (message, sender);
547       empathy_message_set_timestamp (message, t);
548       empathy_message_set_tptype (message, msg_type);
549       empathy_message_set_is_backlog (message, TRUE);
550
551       if (cm_id_str)
552         empathy_message_set_id (message, cm_id);
553
554       messages = g_list_append (messages, message);
555
556       g_object_unref (sender);
557       xmlFree (time);
558       xmlFree (sender_id);
559       xmlFree (sender_name);
560       xmlFree (body);
561       xmlFree (is_user_str);
562       xmlFree (msg_type_str);
563       xmlFree (cm_id_str);
564       xmlFree (sender_avatar_token);
565     }
566
567   DEBUG ("Parsed %d messages", g_list_length (messages));
568
569   xmlFreeDoc (doc);
570   xmlFreeParserCtxt (ctxt);
571
572   return messages;
573 }
574
575 static GList *
576 log_store_empathy_get_all_files (EmpathyLogStore *self,
577                                  const gchar *dir)
578 {
579   GDir *gdir;
580   GList *files = NULL;
581   const gchar *name;
582   const gchar *basedir;
583   EmpathyLogStoreEmpathyPriv *priv;
584
585   priv = GET_PRIV (self);
586
587   basedir = dir ? dir : priv->basedir;
588
589   gdir = g_dir_open (basedir, 0, NULL);
590   if (!gdir)
591     return NULL;
592
593   while ((name = g_dir_read_name (gdir)) != NULL)
594     {
595       gchar *filename;
596
597       filename = g_build_filename (basedir, name, NULL);
598       if (g_str_has_suffix (filename, LOG_FILENAME_SUFFIX))
599         {
600           files = g_list_prepend (files, filename);
601           continue;
602         }
603
604       if (g_file_test (filename, G_FILE_TEST_IS_DIR))
605         {
606           /* Recursively get all log files */
607           files = g_list_concat (files,
608               log_store_empathy_get_all_files (self, filename));
609         }
610
611       g_free (filename);
612     }
613
614   g_dir_close (gdir);
615
616   return files;
617 }
618
619 static GList *
620 log_store_empathy_search_new (EmpathyLogStore *self,
621                               const gchar *text)
622 {
623   GList *files, *l;
624   GList *hits = NULL;
625   gchar *text_casefold;
626
627   g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
628   g_return_val_if_fail (!EMP_STR_EMPTY (text), NULL);
629
630   text_casefold = g_utf8_casefold (text, -1);
631
632   files = log_store_empathy_get_all_files (self, NULL);
633   DEBUG ("Found %d log files in total", g_list_length (files));
634
635   for (l = files; l; l = g_list_next (l))
636     {
637       gchar *filename;
638       GMappedFile *file;
639       gsize length;
640       gchar *contents;
641       gchar *contents_casefold;
642
643       filename = l->data;
644
645       file = g_mapped_file_new (filename, FALSE, NULL);
646       if (!file)
647         continue;
648
649       length = g_mapped_file_get_length (file);
650       contents = g_mapped_file_get_contents (file);
651       contents_casefold = g_utf8_casefold (contents, length);
652
653       g_mapped_file_free (file);
654
655       if (strstr (contents_casefold, text_casefold))
656         {
657           EmpathyLogSearchHit *hit;
658
659           hit = log_store_empathy_search_hit_new (self, filename);
660
661           if (hit)
662             {
663               hits = g_list_prepend (hits, hit);
664               DEBUG ("Found text:'%s' in file:'%s' on date:'%s'",
665                   text, hit->filename, hit->date);
666             }
667         }
668
669       g_free (contents_casefold);
670       g_free (filename);
671     }
672
673   g_list_free (files);
674   g_free (text_casefold);
675
676   return hits;
677 }
678
679 static GList *
680 log_store_empathy_get_chats_for_dir (EmpathyLogStore *self,
681                                      const gchar *dir,
682                                      gboolean is_chatroom)
683 {
684   GDir *gdir;
685   GList *hits = NULL;
686   const gchar *name;
687   GError *error = NULL;
688
689   gdir = g_dir_open (dir, 0, &error);
690   if (!gdir)
691     {
692       DEBUG ("Failed to open directory: %s, error: %s", dir, error->message);
693       g_error_free (error);
694       return NULL;
695     }
696
697   while ((name = g_dir_read_name (gdir)) != NULL)
698     {
699       EmpathyLogSearchHit *hit;
700
701       if (!is_chatroom && strcmp (name, LOG_DIR_CHATROOMS) == 0)
702         {
703           gchar *filename = g_build_filename (dir, name, NULL);
704           hits = g_list_concat (hits, log_store_empathy_get_chats_for_dir (
705                 self, filename, TRUE));
706           g_free (filename);
707           continue;
708         }
709       hit = g_slice_new0 (EmpathyLogSearchHit);
710       hit->chat_id = g_strdup (name);
711       hit->is_chatroom = is_chatroom;
712
713       hits = g_list_prepend (hits, hit);
714     }
715
716   g_dir_close (gdir);
717
718   return hits;
719 }
720
721
722 static GList *
723 log_store_empathy_get_messages_for_date (EmpathyLogStore *self,
724                                          EmpathyAccount *account,
725                                          const gchar *chat_id,
726                                          gboolean chatroom,
727                                          const gchar *date)
728 {
729   gchar *filename;
730   GList *messages;
731
732   g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
733   g_return_val_if_fail (chat_id != NULL, NULL);
734
735   filename = log_store_empathy_get_filename_for_date (self, account,
736       chat_id, chatroom, date);
737   messages = log_store_empathy_get_messages_for_file (self, filename);
738   g_free (filename);
739
740   return messages;
741 }
742
743 static GList *
744 log_store_empathy_get_chats (EmpathyLogStore *self,
745                               EmpathyAccount *account)
746 {
747   gchar *dir;
748   GList *hits;
749   EmpathyLogStoreEmpathyPriv *priv;
750
751   priv = GET_PRIV (self);
752
753   dir = log_store_empathy_get_dir (self, account, NULL, FALSE);
754
755   hits = log_store_empathy_get_chats_for_dir (self, dir, FALSE);
756
757   g_free (dir);
758
759   return hits;
760 }
761
762 static const gchar *
763 log_store_empathy_get_name (EmpathyLogStore *self)
764 {
765   EmpathyLogStoreEmpathyPriv *priv = GET_PRIV (self);
766
767   return priv->name;
768 }
769
770 static GList *
771 log_store_empathy_get_filtered_messages (EmpathyLogStore *self,
772                                          EmpathyAccount *account,
773                                          const gchar *chat_id,
774                                          gboolean chatroom,
775                                          guint num_messages,
776                                          EmpathyLogMessageFilter filter,
777                                          gpointer user_data)
778 {
779   GList *dates, *l, *messages = NULL;
780   guint i = 0;
781
782   dates = log_store_empathy_get_dates (self, account, chat_id, chatroom);
783
784   for (l = g_list_last (dates); l && i < num_messages; l = g_list_previous (l))
785     {
786       GList *new_messages, *n, *next;
787
788       /* FIXME: We should really restrict the message parsing to get only
789        * the newest num_messages. */
790       new_messages = log_store_empathy_get_messages_for_date (self, account,
791           chat_id, chatroom, l->data);
792
793       n = new_messages;
794       while (n != NULL)
795         {
796           next = g_list_next (n);
797           if (!filter (n->data, user_data))
798             {
799               g_object_unref (n->data);
800               new_messages = g_list_delete_link (new_messages, n);
801             }
802           else
803             {
804               i++;
805             }
806           n = next;
807         }
808       messages = g_list_concat (messages, new_messages);
809     }
810
811   g_list_foreach (dates, (GFunc) g_free, NULL);
812   g_list_free (dates);
813
814   return messages;
815 }
816
817 static void
818 log_store_iface_init (gpointer g_iface,
819                       gpointer iface_data)
820 {
821   EmpathyLogStoreInterface *iface = (EmpathyLogStoreInterface *) g_iface;
822
823   iface->get_name = log_store_empathy_get_name;
824   iface->exists = log_store_empathy_exists;
825   iface->add_message = log_store_empathy_add_message;
826   iface->get_dates = log_store_empathy_get_dates;
827   iface->get_messages_for_date = log_store_empathy_get_messages_for_date;
828   iface->get_chats = log_store_empathy_get_chats;
829   iface->search_new = log_store_empathy_search_new;
830   iface->ack_message = NULL;
831   iface->get_filtered_messages = log_store_empathy_get_filtered_messages;
832 }