Convert XMPP console dialogs to popovers.
[pidgin-git.git] / libpurple / message.c
blob3136be9b59dfd3879059fc487e367958b6046ffe
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 = NULL;
132 g_return_val_if_fail(PURPLE_IS_MESSAGE(msg), 0);
134 priv = purple_message_get_instance_private(msg);
135 return priv->id;
138 PurpleMessage *
139 purple_message_find_by_id(guint id)
141 g_return_val_if_fail(id > 0, NULL);
143 return g_hash_table_lookup(messages, GINT_TO_POINTER(id));
146 const gchar *
147 purple_message_get_author(PurpleMessage *msg)
149 PurpleMessagePrivate *priv = NULL;
151 g_return_val_if_fail(PURPLE_IS_MESSAGE(msg), NULL);
153 priv = purple_message_get_instance_private(msg);
154 return priv->author;
157 const gchar *
158 purple_message_get_recipient(PurpleMessage *msg)
160 PurpleMessagePrivate *priv = NULL;
162 g_return_val_if_fail(PURPLE_IS_MESSAGE(msg), NULL);
164 priv = purple_message_get_instance_private(msg);
165 return priv->recipient;
168 void
169 purple_message_set_author_alias(PurpleMessage *msg, const gchar *alias)
171 g_object_set(msg, "author-alias", alias, NULL);
174 const gchar *
175 purple_message_get_author_alias(PurpleMessage *msg)
177 PurpleMessagePrivate *priv = NULL;
179 g_return_val_if_fail(PURPLE_IS_MESSAGE(msg), NULL);
180 priv = purple_message_get_instance_private(msg);
182 if (priv->author_alias == NULL)
183 return purple_message_get_author(msg);
185 return priv->author_alias;
188 void
189 purple_message_set_contents(PurpleMessage *msg, const gchar *cont)
191 g_object_set(msg, "contents", cont, NULL);
194 const gchar *
195 purple_message_get_contents(PurpleMessage *msg)
197 PurpleMessagePrivate *priv = NULL;
199 g_return_val_if_fail(PURPLE_IS_MESSAGE(msg), NULL);
201 priv = purple_message_get_instance_private(msg);
202 return priv->contents;
205 gboolean
206 purple_message_is_empty(PurpleMessage *msg)
208 const gchar *cont = purple_message_get_contents(msg);
210 return (cont == NULL || cont[0] == '\0');
213 void
214 purple_message_set_time(PurpleMessage *msg, guint64 msgtime)
216 g_object_set(msg, "time", msgtime, NULL);
219 guint64
220 purple_message_get_time(PurpleMessage *msg)
222 PurpleMessagePrivate *priv = NULL;
224 g_return_val_if_fail(PURPLE_IS_MESSAGE(msg), 0);
226 priv = purple_message_get_instance_private(msg);
227 return priv->msgtime;
230 void
231 purple_message_set_flags(PurpleMessage *msg, PurpleMessageFlags flags)
233 g_object_set(msg, "flags", flags, NULL);
236 PurpleMessageFlags
237 purple_message_get_flags(PurpleMessage *msg)
239 PurpleMessagePrivate *priv = NULL;
241 g_return_val_if_fail(PURPLE_IS_MESSAGE(msg), 0);
243 priv = purple_message_get_instance_private(msg);
244 return priv->flags;
247 /******************************************************************************
248 * Object stuff
249 ******************************************************************************/
251 static void
252 purple_message_init(PurpleMessage *msg)
254 static guint max_id = 0;
256 PurpleMessagePrivate *priv = purple_message_get_instance_private(msg);
258 priv->id = ++max_id;
259 g_hash_table_insert(messages, GINT_TO_POINTER(max_id), msg);
262 static void
263 purple_message_finalize(GObject *obj)
265 PurpleMessage *message = PURPLE_MESSAGE(obj);
266 PurpleMessagePrivate *priv = purple_message_get_instance_private(message);
268 g_free(priv->author);
269 g_free(priv->author_alias);
270 g_free(priv->recipient);
271 g_free(priv->contents);
273 G_OBJECT_CLASS(purple_message_parent_class)->finalize(obj);
276 static void
277 purple_message_get_property(GObject *object, guint par_id, GValue *value,
278 GParamSpec *pspec)
280 PurpleMessage *message = PURPLE_MESSAGE(object);
281 PurpleMessagePrivate *priv = purple_message_get_instance_private(message);
283 switch (par_id) {
284 case PROP_ID:
285 g_value_set_uint(value, priv->id);
286 break;
287 case PROP_AUTHOR:
288 g_value_set_string(value, priv->author);
289 break;
290 case PROP_AUTHOR_ALIAS:
291 g_value_set_string(value, priv->author_alias);
292 break;
293 case PROP_RECIPIENT:
294 g_value_set_string(value, priv->recipient);
295 break;
296 case PROP_CONTENTS:
297 g_value_set_string(value, priv->contents);
298 break;
299 case PROP_TIME:
300 g_value_set_uint64(value, priv->msgtime);
301 break;
302 case PROP_FLAGS:
303 g_value_set_flags(value, priv->flags);
304 break;
305 default:
306 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, par_id, pspec);
307 break;
311 static void
312 purple_message_set_property(GObject *object, guint par_id, const GValue *value,
313 GParamSpec *pspec)
315 PurpleMessage *message = PURPLE_MESSAGE(object);
316 PurpleMessagePrivate *priv = purple_message_get_instance_private(message);
318 switch (par_id) {
319 case PROP_AUTHOR:
320 g_free(priv->author);
321 priv->author = g_value_dup_string(value);
322 break;
323 case PROP_AUTHOR_ALIAS:
324 g_free(priv->author_alias);
325 priv->author_alias = g_value_dup_string(value);
326 break;
327 case PROP_RECIPIENT:
328 g_free(priv->recipient);
329 priv->recipient = g_value_dup_string(value);
330 break;
331 case PROP_CONTENTS:
332 g_free(priv->contents);
333 priv->contents = g_value_dup_string(value);
334 break;
335 case PROP_TIME:
336 priv->msgtime = g_value_get_uint64(value);
337 break;
338 case PROP_FLAGS:
339 priv->flags = g_value_get_flags(value);
340 break;
341 default:
342 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, par_id, pspec);
343 break;
347 static void
348 purple_message_class_init(PurpleMessageClass *klass)
350 GObjectClass *gobj_class = G_OBJECT_CLASS(klass);
352 gobj_class->finalize = purple_message_finalize;
353 gobj_class->get_property = purple_message_get_property;
354 gobj_class->set_property = purple_message_set_property;
356 properties[PROP_ID] = g_param_spec_uint("id",
357 "ID", "The session-unique message id",
358 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
359 properties[PROP_AUTHOR] = g_param_spec_string("author",
360 "Author", "The username of the person, who sent the message.",
361 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
362 properties[PROP_AUTHOR_ALIAS] = g_param_spec_string("author-alias",
363 "Author's alias", "The alias of the person, who sent the "
364 "message. For outgoing messages, it's your alias.",
365 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
366 properties[PROP_RECIPIENT] = g_param_spec_string("recipient",
367 "Recipient", "The username of the recipient.",
368 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
369 properties[PROP_CONTENTS] = g_param_spec_string("contents",
370 "Contents", "The message text",
371 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
372 properties[PROP_TIME] = g_param_spec_uint64("time",
373 "Time", "Message timestamp",
374 0, G_MAXUINT64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
375 properties[PROP_FLAGS] = g_param_spec_flags("flags",
376 "Flags", "Bitwise set of #PurpleMessageFlags flags",
377 PURPLE_TYPE_MESSAGE_FLAGS, 0,
378 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
380 g_object_class_install_properties(gobj_class, PROP_LAST, properties);
383 void
384 _purple_message_init(void)
386 messages = g_hash_table_new_full(g_direct_hash, g_direct_equal,
387 NULL, g_object_unref);
390 void
391 _purple_message_uninit(void)
393 g_hash_table_destroy(messages);
394 messages = NULL;