Use G_DECLARE_FINAL_TYPE for PurpleMessage.
[pidgin-git.git] / libpurple / message.c
blob75b11b2f94c3466a4e4d3a3fd6f8fc2d4b6c1963
1 /* purple
3 * Purple is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
22 #include "internal.h"
23 #include "glibcompat.h"
25 #include "debug.h"
26 #include "enums.h"
27 #include "message.h"
29 /**
30 * PurpleMessage:
32 * A message data container.
34 struct _PurpleMessage
36 GObject parent;
39 typedef struct {
40 guint id;
41 gchar *author;
42 gchar *author_alias;
43 gchar *recipient;
44 gchar *contents;
45 guint64 msgtime;
46 PurpleMessageFlags flags;
47 } PurpleMessagePrivate;
49 enum
51 PROP_0,
52 PROP_ID,
53 PROP_AUTHOR,
54 PROP_AUTHOR_ALIAS,
55 PROP_RECIPIENT,
56 PROP_CONTENTS,
57 PROP_TIME,
58 PROP_FLAGS,
59 PROP_LAST
62 static GParamSpec *properties[PROP_LAST];
64 static GHashTable *messages = NULL;
66 G_DEFINE_TYPE_WITH_PRIVATE(PurpleMessage, purple_message, G_TYPE_OBJECT)
68 /******************************************************************************
69 * API implementation
70 ******************************************************************************/
72 PurpleMessage *
73 purple_message_new_outgoing(const gchar *who, const gchar *contents,
74 PurpleMessageFlags flags)
76 g_warn_if_fail(!(flags & PURPLE_MESSAGE_RECV));
77 g_warn_if_fail(!(flags & PURPLE_MESSAGE_SYSTEM));
79 flags |= PURPLE_MESSAGE_SEND;
81 /* who may be NULL for outgoing MUC messages */
82 return g_object_new(PURPLE_TYPE_MESSAGE,
83 "author-alias", _("Me"),
84 "recipient", who,
85 "contents", contents,
86 "time", (guint64)time(NULL),
87 "flags", flags,
88 NULL);
91 PurpleMessage *
92 purple_message_new_incoming(const gchar *who, const gchar *contents,
93 PurpleMessageFlags flags, guint64 timestamp)
95 g_warn_if_fail(!(flags & PURPLE_MESSAGE_SEND));
96 g_warn_if_fail(!(flags & PURPLE_MESSAGE_SYSTEM));
98 flags |= PURPLE_MESSAGE_RECV;
100 if (timestamp == 0)
101 timestamp = time(NULL);
103 return g_object_new(PURPLE_TYPE_MESSAGE,
104 "author", who,
105 "author-alias", who,
106 "contents", contents,
107 "time", timestamp,
108 "flags", flags,
109 NULL);
112 PurpleMessage *
113 purple_message_new_system(const gchar *contents, PurpleMessageFlags flags)
115 g_warn_if_fail(!(flags & PURPLE_MESSAGE_SEND));
116 g_warn_if_fail(!(flags & PURPLE_MESSAGE_RECV));
118 flags |= PURPLE_MESSAGE_SYSTEM;
120 return g_object_new(PURPLE_TYPE_MESSAGE,
121 "contents", contents,
122 "time", (guint64)time(NULL),
123 "flags", flags,
124 NULL);
127 guint
128 purple_message_get_id(PurpleMessage *msg)
130 PurpleMessagePrivate *priv = purple_message_get_instance_private(msg);
132 g_return_val_if_fail(priv != NULL, 0);
134 return priv->id;
137 PurpleMessage *
138 purple_message_find_by_id(guint id)
140 g_return_val_if_fail(id > 0, NULL);
142 return g_hash_table_lookup(messages, GINT_TO_POINTER(id));
145 const gchar *
146 purple_message_get_author(PurpleMessage *msg)
148 PurpleMessagePrivate *priv = purple_message_get_instance_private(msg);
150 g_return_val_if_fail(priv != NULL, NULL);
152 return priv->author;
155 const gchar *
156 purple_message_get_recipient(PurpleMessage *msg)
158 PurpleMessagePrivate *priv = purple_message_get_instance_private(msg);
160 g_return_val_if_fail(priv != NULL, NULL);
162 return priv->recipient;
165 void
166 purple_message_set_author_alias(PurpleMessage *msg, const gchar *alias)
168 g_object_set(msg, "author-alias", alias, NULL);
171 const gchar *
172 purple_message_get_author_alias(PurpleMessage *msg)
174 PurpleMessagePrivate *priv = purple_message_get_instance_private(msg);
176 g_return_val_if_fail(priv != NULL, NULL);
178 if (priv->author_alias == NULL)
179 return purple_message_get_author(msg);
181 return priv->author_alias;
184 void
185 purple_message_set_contents(PurpleMessage *msg, const gchar *cont)
187 g_object_set(msg, "contents", cont, NULL);
190 const gchar *
191 purple_message_get_contents(PurpleMessage *msg)
193 PurpleMessagePrivate *priv = purple_message_get_instance_private(msg);
195 g_return_val_if_fail(priv != NULL, NULL);
197 return priv->contents;
200 gboolean
201 purple_message_is_empty(PurpleMessage *msg)
203 const gchar *cont = purple_message_get_contents(msg);
205 return (cont == NULL || cont[0] == '\0');
208 void
209 purple_message_set_time(PurpleMessage *msg, guint64 msgtime)
211 g_object_set(msg, "time", msgtime, NULL);
214 guint64
215 purple_message_get_time(PurpleMessage *msg)
217 PurpleMessagePrivate *priv = purple_message_get_instance_private(msg);
219 g_return_val_if_fail(priv != NULL, 0);
221 return priv->msgtime;
224 void
225 purple_message_set_flags(PurpleMessage *msg, PurpleMessageFlags flags)
227 g_object_set(msg, "flags", flags, NULL);
230 PurpleMessageFlags
231 purple_message_get_flags(PurpleMessage *msg)
233 PurpleMessagePrivate *priv = purple_message_get_instance_private(msg);
235 g_return_val_if_fail(priv != NULL, 0);
237 return priv->flags;
240 /******************************************************************************
241 * Object stuff
242 ******************************************************************************/
244 static void
245 purple_message_init(PurpleMessage *msg)
247 static guint max_id = 0;
249 PurpleMessagePrivate *priv = purple_message_get_instance_private(msg);
251 priv->id = ++max_id;
252 g_hash_table_insert(messages, GINT_TO_POINTER(max_id), msg);
255 static void
256 purple_message_finalize(GObject *obj)
258 PurpleMessage *message = PURPLE_MESSAGE(obj);
259 PurpleMessagePrivate *priv = purple_message_get_instance_private(message);
261 g_free(priv->author);
262 g_free(priv->author_alias);
263 g_free(priv->recipient);
264 g_free(priv->contents);
266 G_OBJECT_CLASS(purple_message_parent_class)->finalize(obj);
269 static void
270 purple_message_get_property(GObject *object, guint par_id, GValue *value,
271 GParamSpec *pspec)
273 PurpleMessage *message = PURPLE_MESSAGE(object);
274 PurpleMessagePrivate *priv = purple_message_get_instance_private(message);
276 switch (par_id) {
277 case PROP_ID:
278 g_value_set_uint(value, priv->id);
279 break;
280 case PROP_AUTHOR:
281 g_value_set_string(value, priv->author);
282 break;
283 case PROP_AUTHOR_ALIAS:
284 g_value_set_string(value, priv->author_alias);
285 break;
286 case PROP_RECIPIENT:
287 g_value_set_string(value, priv->recipient);
288 break;
289 case PROP_CONTENTS:
290 g_value_set_string(value, priv->contents);
291 break;
292 case PROP_TIME:
293 g_value_set_uint64(value, priv->msgtime);
294 break;
295 case PROP_FLAGS:
296 g_value_set_flags(value, priv->flags);
297 break;
298 default:
299 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, par_id, pspec);
300 break;
304 static void
305 purple_message_set_property(GObject *object, guint par_id, const GValue *value,
306 GParamSpec *pspec)
308 PurpleMessage *message = PURPLE_MESSAGE(object);
309 PurpleMessagePrivate *priv = purple_message_get_instance_private(message);
311 switch (par_id) {
312 case PROP_AUTHOR:
313 g_free(priv->author);
314 priv->author = g_value_dup_string(value);
315 break;
316 case PROP_AUTHOR_ALIAS:
317 g_free(priv->author_alias);
318 priv->author_alias = g_value_dup_string(value);
319 break;
320 case PROP_RECIPIENT:
321 g_free(priv->recipient);
322 priv->recipient = g_value_dup_string(value);
323 break;
324 case PROP_CONTENTS:
325 g_free(priv->contents);
326 priv->contents = g_value_dup_string(value);
327 break;
328 case PROP_TIME:
329 priv->msgtime = g_value_get_uint64(value);
330 break;
331 case PROP_FLAGS:
332 priv->flags = g_value_get_flags(value);
333 break;
334 default:
335 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, par_id, pspec);
336 break;
340 static void
341 purple_message_class_init(PurpleMessageClass *klass)
343 GObjectClass *gobj_class = G_OBJECT_CLASS(klass);
345 gobj_class->finalize = purple_message_finalize;
346 gobj_class->get_property = purple_message_get_property;
347 gobj_class->set_property = purple_message_set_property;
349 properties[PROP_ID] = g_param_spec_uint("id",
350 "ID", "The session-unique message id",
351 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
352 properties[PROP_AUTHOR] = g_param_spec_string("author",
353 "Author", "The username of the person, who sent the message.",
354 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
355 properties[PROP_AUTHOR_ALIAS] = g_param_spec_string("author-alias",
356 "Author's alias", "The alias of the person, who sent the "
357 "message. For outgoing messages, it's your alias.",
358 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
359 properties[PROP_RECIPIENT] = g_param_spec_string("recipient",
360 "Recipient", "The username of the recipient.",
361 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
362 properties[PROP_CONTENTS] = g_param_spec_string("contents",
363 "Contents", "The message text",
364 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
365 properties[PROP_TIME] = g_param_spec_uint64("time",
366 "Time", "Message timestamp",
367 0, G_MAXUINT64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
368 properties[PROP_FLAGS] = g_param_spec_flags("flags",
369 "Flags", "Bitwise set of #PurpleMessageFlags flags",
370 PURPLE_TYPE_MESSAGE_FLAGS, 0,
371 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
373 g_object_class_install_properties(gobj_class, PROP_LAST, properties);
376 void
377 _purple_message_init(void)
379 messages = g_hash_table_new_full(g_direct_hash, g_direct_equal,
380 NULL, g_object_unref);
383 void
384 _purple_message_uninit(void)
386 g_hash_table_destroy(messages);
387 messages = NULL;