summaryrefslogtreecommitdiff
path: root/telepathy-glib/signalled-message.c
blob: fc34ee7bc8506a164b5318ca5fa35e2b70383423 (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * signalled-message.c - Source for TpSignalledMessage
 * Copyright (C) 2006-2010 Collabora Ltd.
 * Copyright (C) 2006-2008 Nokia Corporation
 *
 * 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
 */

/**
 * SECTION:signalled-message
 * @title: TpSignalledMessage
 * @short_description: a message received using the Telepathy message interface
 *
 * #TpSignalledMessage is used within Telepathy clients to represent a message
 * signalled by a connection manager. This can either be a message received from
 * someone else, confirmation that a message has been sent by the local user,
 * or a delivery report indicating that delivery of a message has
 * succeeded or failed.
 *
 * Since: 0.13.9
 */

#include "config.h"

#include "signalled-message.h"
#include "signalled-message-internal.h"
#include "message-internal.h"

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

/**
 * TpSignalledMessage:
 *
 * Opaque structure representing a received message using the Telepathy
 * messages interface
 *
 * Since: 0.13.9
 */

enum
{
  PROP_SENDER = 1
};

G_DEFINE_TYPE (TpSignalledMessage, tp_signalled_message, TP_TYPE_MESSAGE)

struct _TpSignalledMessagePrivate
{
  TpContact *sender;
};

static void
tp_signalled_message_get_property (GObject *object,
    guint property_id,
    GValue *value,
    GParamSpec *pspec)
{
  TpSignalledMessage *self = (TpSignalledMessage *) object;

  switch (property_id)
    {
      case PROP_SENDER:
        g_value_set_object (value, self->priv->sender);
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
tp_signalled_message_set_property (GObject *object,
    guint property_id,
    const GValue *value,
    GParamSpec *pspec)
{
  TpSignalledMessage *self = (TpSignalledMessage *) object;

  switch (property_id)
    {
      case PROP_SENDER:
        g_assert (self->priv->sender == NULL); /* construct only */
        self->priv->sender = g_value_dup_object (value);
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
tp_signalled_message_dispose (GObject *object)
{
  TpSignalledMessage *self = TP_SIGNALLED_MESSAGE (object);
  void (*dispose) (GObject *) =
    G_OBJECT_CLASS (tp_signalled_message_parent_class)->dispose;

  tp_clear_object (&self->priv->sender);

  if (dispose != NULL)
    dispose (object);
}

static void
tp_signalled_message_class_init (TpSignalledMessageClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GParamSpec *param_spec;

  gobject_class->get_property = tp_signalled_message_get_property;
  gobject_class->set_property = tp_signalled_message_set_property;
  gobject_class->dispose = tp_signalled_message_dispose;

  /**
   * TpSignalledMessage:sender:
   *
   * A #TpContact representing the sender of the message, if known, or %NULL
   * otherwise.
   *
   * Since: 0.13.9
   */
  param_spec = g_param_spec_object ("sender", "TpContact",
      "The sender of the message",
      TP_TYPE_CONTACT,
      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (gobject_class, PROP_SENDER,
      param_spec);

  g_type_class_add_private (gobject_class, sizeof (TpSignalledMessagePrivate));
}

static void
tp_signalled_message_init (TpSignalledMessage *self)
{
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self), TP_TYPE_SIGNALLED_MESSAGE,
      TpSignalledMessagePrivate);

  self->priv->sender = NULL;
}

/*
 * Create a new TpSignalledMessage.
 *
 * Any message-sender and message-sender-id in parts[0] will be ignored
 * completely: the caller is responsible for interpreting those fields
 * and providing a suitable @sender.
 *
 * The message-sender will be removed from the header, and the
 * message-sender-id will be set to match the #TpContact:identifier of @sender.
 *
 * @sender may be %NULL, which means the message wasn't sent by a contact
 * (this could be used for administrative messages from a chatroom or the
 * server) or we have no idea who sent it.
 */
TpMessage *
_tp_signalled_message_new (const GPtrArray *parts,
    TpContact *sender)
{
  TpMessage *self;
  guint i;

  g_return_val_if_fail (parts != NULL, NULL);
  g_return_val_if_fail (parts->len > 0, NULL);
  g_return_val_if_fail (sender == NULL || TP_IS_CONTACT (sender), NULL);

  self = g_object_new (TP_TYPE_SIGNALLED_MESSAGE,
      "sender", sender,
      NULL);

  for (i = 0; i < parts->len; i++)
    {
      /* First part is automatically created */
      if (i != 0)
        tp_message_append_part (self);

      tp_g_hash_table_update (g_ptr_array_index (self->parts, i),
          g_ptr_array_index (parts, i),
          (GBoxedCopyFunc) g_strdup,
          (GBoxedCopyFunc) tp_g_value_slice_dup);
    }

  /* This handle may not be persistent, user should use the TpContact
   * directly */
  tp_message_delete_key (self, 0, "message-sender");

  /* override any message-sender-id that the message might have had */
  if (sender == NULL)
    {
      tp_message_delete_key (self, 0, "message-sender-id");
    }
  else
    {
      tp_message_set_string (self, 0, "message-sender-id",
          tp_contact_get_identifier (sender));
    }

  _tp_message_set_immutable (self);

  return self;
}

/**
 * tp_signalled_message_get_sender:
 * @message: a #TpSignalledMessage
 *
 * Returns a #TpContact representing the sender of @message if known, %NULL
 * otherwise.
 *
 * Returns: (transfer none): the sender of the message
 *
 * Since: 0.13.9
 */
TpContact *
tp_signalled_message_get_sender (TpMessage *message)
{
  TpSignalledMessage *self;

  g_return_val_if_fail (TP_IS_SIGNALLED_MESSAGE (message), NULL);

  self = (TpSignalledMessage *) message;

  return self->priv->sender;
}

guint
_tp_signalled_message_get_pending_message_id (TpMessage *message,
    gboolean *valid)
{
  const GHashTable *part0;

  g_return_val_if_fail (TP_IS_SIGNALLED_MESSAGE (message), 0);
  g_return_val_if_fail (valid != NULL, 0);

  part0 = tp_message_peek (message, 0);
  if (part0 == NULL)
    {
      *valid = FALSE;
      return 0;
    }

  return tp_asv_get_uint32 (part0, "pending-message-id", valid);
}