summaryrefslogtreecommitdiff
path: root/telepathy-logger/dbus-service.c
blob: 6f00c44e9b6e1f9a7a96487c844b0615b57a17ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2009 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Authors: Cosimo Alfarano <cosimo.alfarano@collabora.co.uk>
 */

#include "config.h"
#include "dbus-service.h"

#include <glib.h>
#include <telepathy-glib/dbus.h>
#include <telepathy-glib/account.h>
#include <telepathy-glib/util.h>

#include <telepathy-logger/log-entry-text.h>
#include <telepathy-logger/log-manager.h>
#include <telepathy-logger/util.h>

#include <extensions/extensions.h>

#define DEBUG_FLAG TPL_DEBUG_DBUS_SERVICE
#include <telepathy-logger/debug.h>

static void tpl_logger_iface_init (gpointer iface, gpointer iface_data);

#define GET_PRIV(obj) TPL_GET_PRIV (obj, TplDBusService)
struct _TplDBusServicePriv
{
  TplLogManager *manager;
};

G_DEFINE_TYPE_WITH_CODE (TplDBusService, tpl_dbus_service, G_TYPE_OBJECT,
    G_IMPLEMENT_INTERFACE (TPL_TYPE_SVC_LOGGER, tpl_logger_iface_init));

static void
tpl_dbus_service_class_init (TplDBusServiceClass *klass)
{
  GObjectClass* object_class = G_OBJECT_CLASS (klass);

  g_type_class_add_private (object_class, sizeof (TplDBusServicePriv));
}


static void
tpl_dbus_service_init (TplDBusService *self)
{
  TplDBusServicePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
      TPL_TYPE_DBUS_SERVICE, TplDBusServicePriv);

  g_return_if_fail (TPL_IS_DBUS_SERVICE (self));

  self->priv = priv;
  priv->manager = tpl_log_manager_dup_singleton ();
}


TplDBusService *
tpl_dbus_service_new (void)
{
  return g_object_new (TPL_TYPE_DBUS_SERVICE, NULL);
}


static GPtrArray *
tpl_chat_message_marshal (GList *data)
{
  guint idx;
  GList *data_ptr;
  GPtrArray *retval;

  retval = g_ptr_array_new_with_free_func ((GDestroyNotify) g_value_array_free);

  DEBUG ("Marshalled a(ssx) data:");

  for (idx = 0, data_ptr = data;
      data_ptr != NULL;
      data_ptr = g_list_next (data_ptr), ++idx)
    {
      TplLogEntry *log = data_ptr->data;
      const gchar *message = tpl_log_entry_text_get_message (
          TPL_LOG_ENTRY_TEXT (log));
      const gchar *sender = tpl_contact_get_identifier (
          tpl_log_entry_text_get_sender (TPL_LOG_ENTRY_TEXT (log)));
      gint64 timestamp = tpl_log_entry_get_timestamp (log);

      g_ptr_array_add (retval, tp_value_array_build (3,
          G_TYPE_STRING, sender,
          G_TYPE_STRING, message,
          G_TYPE_INT64, timestamp,
          G_TYPE_INVALID));

      DEBUG ("%d = %s / %s / %" G_GINT64_FORMAT, idx, sender, message,
          timestamp);
    }

  return retval;
}


static void
tpl_dbus_service_get_recent_messages (TplSvcLogger *self,
    const gchar *account_path,
    const gchar *identifier,
    gboolean is_chatroom,
    guint lines,
    DBusGMethodInvocation *context)
{
  TplDBusServicePriv *priv = GET_PRIV (self);
  TpAccount *account = NULL;
  TpDBusDaemon *tp_dbus = NULL;
  GList *ret = NULL;
  GPtrArray *packed = NULL;
  GList *dates = NULL;
  GList *dates_ptr = NULL;
  GError *error = NULL;
  guint left_lines = lines;

  g_return_if_fail (TPL_IS_DBUS_SERVICE (self));
  g_return_if_fail (context != NULL);

  tp_dbus = tp_dbus_daemon_dup (&error);
  if (tp_dbus == NULL)
    {
      DEBUG ("Unable to acquire the bus daemon: %s", error->message);
      dbus_g_method_return_error (context, error);
      goto out;
    }

  account = tp_account_new (tp_dbus, account_path, &error);
  if (account == NULL)
    {
      DEBUG ("Unable to acquire the account for %s: %s", account_path,
          error->message);
      dbus_g_method_return_error (context, error);
      goto out;
    }

  dates = tpl_log_manager_get_dates (priv->manager, account, identifier,
      is_chatroom);
  if (dates == NULL)
    {
      error = g_error_new_literal (TPL_DBUS_SERVICE_ERROR,
          TPL_DBUS_SERVICE_ERROR_FAILED, "Error during date list retrieving, "
          "probably the account path or the identifier does not exist");
      dbus_g_method_return_error (context, error);
      goto out;
    }

  /* for each date returned, get at most <lines> lines, then if needed
   * check the previous date for the missing ones, and so on until
   * <lines> is reached, most recent date first */
  for (dates_ptr = g_list_last (dates);
      dates_ptr != NULL && left_lines > 0;
      dates_ptr = g_list_previous (dates_ptr))
    {
      gchar *date = dates_ptr->data;
      GList *messages = tpl_log_manager_get_messages_for_date (priv->manager,
          account, identifier, is_chatroom, date);
      GList *messages_ptr;

      /* from the most recent message, backward */
      for (messages_ptr = g_list_last (messages);
          messages_ptr != NULL && left_lines > 0;
          messages_ptr = g_list_previous (messages_ptr))
        {
          TplLogEntry *log = messages_ptr->data;
              /* keeps the reference and add to the result */
              ret = g_list_prepend (ret, g_object_ref (log));
              left_lines -= 1;
        }
      g_list_foreach (messages, (GFunc) g_object_unref, NULL);
      g_list_free (messages);
    }
  g_list_foreach (dates, (GFunc) g_free, NULL);
  g_list_free (dates);

  packed = tpl_chat_message_marshal (ret);

  tpl_svc_logger_return_from_get_recent_messages (context, packed);

  g_list_foreach (ret, (GFunc) g_object_unref, NULL);
  g_list_free (ret);
  g_ptr_array_free (packed, TRUE);

out:
  if (account != NULL)
    g_object_unref (account);

  if (tp_dbus != NULL)
    g_object_unref (tp_dbus);

  g_clear_error (&error);
}


static void
tpl_logger_iface_init (gpointer iface,
    gpointer iface_data)
{
  TplSvcLoggerClass *klass = (TplSvcLoggerClass *) iface;

#define IMPLEMENT(x) tpl_svc_logger_implement_##x (klass, tpl_dbus_service_##x)
  IMPLEMENT (get_recent_messages);
#undef IMPLEMENT
}