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
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
23 #include "glibcompat.h"
32 * A message data container.
46 PurpleMessageFlags flags
;
47 } PurpleMessagePrivate
;
62 static GParamSpec
*properties
[PROP_LAST
];
64 static GHashTable
*messages
= NULL
;
66 G_DEFINE_TYPE_WITH_PRIVATE(PurpleMessage
, purple_message
, G_TYPE_OBJECT
)
68 /******************************************************************************
70 ******************************************************************************/
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"),
86 "time", (guint64
)time(NULL
),
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
;
101 timestamp
= time(NULL
);
103 return g_object_new(PURPLE_TYPE_MESSAGE
,
106 "contents", contents
,
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
),
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);
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
));
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
);
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
;
166 purple_message_set_author_alias(PurpleMessage
*msg
, const gchar
*alias
)
168 g_object_set(msg
, "author-alias", alias
, NULL
);
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
;
185 purple_message_set_contents(PurpleMessage
*msg
, const gchar
*cont
)
187 g_object_set(msg
, "contents", cont
, NULL
);
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
;
201 purple_message_is_empty(PurpleMessage
*msg
)
203 const gchar
*cont
= purple_message_get_contents(msg
);
205 return (cont
== NULL
|| cont
[0] == '\0');
209 purple_message_set_time(PurpleMessage
*msg
, guint64 msgtime
)
211 g_object_set(msg
, "time", msgtime
, NULL
);
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
;
225 purple_message_set_flags(PurpleMessage
*msg
, PurpleMessageFlags flags
)
227 g_object_set(msg
, "flags", flags
, NULL
);
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);
240 /******************************************************************************
242 ******************************************************************************/
245 purple_message_init(PurpleMessage
*msg
)
247 static guint max_id
= 0;
249 PurpleMessagePrivate
*priv
= purple_message_get_instance_private(msg
);
252 g_hash_table_insert(messages
, GINT_TO_POINTER(max_id
), msg
);
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
);
270 purple_message_get_property(GObject
*object
, guint par_id
, GValue
*value
,
273 PurpleMessage
*message
= PURPLE_MESSAGE(object
);
274 PurpleMessagePrivate
*priv
= purple_message_get_instance_private(message
);
278 g_value_set_uint(value
, priv
->id
);
281 g_value_set_string(value
, priv
->author
);
283 case PROP_AUTHOR_ALIAS
:
284 g_value_set_string(value
, priv
->author_alias
);
287 g_value_set_string(value
, priv
->recipient
);
290 g_value_set_string(value
, priv
->contents
);
293 g_value_set_uint64(value
, priv
->msgtime
);
296 g_value_set_flags(value
, priv
->flags
);
299 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, par_id
, pspec
);
305 purple_message_set_property(GObject
*object
, guint par_id
, const GValue
*value
,
308 PurpleMessage
*message
= PURPLE_MESSAGE(object
);
309 PurpleMessagePrivate
*priv
= purple_message_get_instance_private(message
);
313 g_free(priv
->author
);
314 priv
->author
= g_value_dup_string(value
);
316 case PROP_AUTHOR_ALIAS
:
317 g_free(priv
->author_alias
);
318 priv
->author_alias
= g_value_dup_string(value
);
321 g_free(priv
->recipient
);
322 priv
->recipient
= g_value_dup_string(value
);
325 g_free(priv
->contents
);
326 priv
->contents
= g_value_dup_string(value
);
329 priv
->msgtime
= g_value_get_uint64(value
);
332 priv
->flags
= g_value_get_flags(value
);
335 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, par_id
, pspec
);
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
);
377 _purple_message_init(void)
379 messages
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
380 NULL
, g_object_unref
);
384 _purple_message_uninit(void)
386 g_hash_table_destroy(messages
);