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
= NULL
;
132 g_return_val_if_fail(PURPLE_IS_MESSAGE(msg
), 0);
134 priv
= purple_message_get_instance_private(msg
);
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
));
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
);
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
;
169 purple_message_set_author_alias(PurpleMessage
*msg
, const gchar
*alias
)
171 g_object_set(msg
, "author-alias", alias
, NULL
);
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
;
189 purple_message_set_contents(PurpleMessage
*msg
, const gchar
*cont
)
191 g_object_set(msg
, "contents", cont
, NULL
);
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
;
206 purple_message_is_empty(PurpleMessage
*msg
)
208 const gchar
*cont
= purple_message_get_contents(msg
);
210 return (cont
== NULL
|| cont
[0] == '\0');
214 purple_message_set_time(PurpleMessage
*msg
, guint64 msgtime
)
216 g_object_set(msg
, "time", msgtime
, NULL
);
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
;
231 purple_message_set_flags(PurpleMessage
*msg
, PurpleMessageFlags flags
)
233 g_object_set(msg
, "flags", flags
, NULL
);
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
);
247 /******************************************************************************
249 ******************************************************************************/
252 purple_message_init(PurpleMessage
*msg
)
254 static guint max_id
= 0;
256 PurpleMessagePrivate
*priv
= purple_message_get_instance_private(msg
);
259 g_hash_table_insert(messages
, GINT_TO_POINTER(max_id
), msg
);
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
);
277 purple_message_get_property(GObject
*object
, guint par_id
, GValue
*value
,
280 PurpleMessage
*message
= PURPLE_MESSAGE(object
);
281 PurpleMessagePrivate
*priv
= purple_message_get_instance_private(message
);
285 g_value_set_uint(value
, priv
->id
);
288 g_value_set_string(value
, priv
->author
);
290 case PROP_AUTHOR_ALIAS
:
291 g_value_set_string(value
, priv
->author_alias
);
294 g_value_set_string(value
, priv
->recipient
);
297 g_value_set_string(value
, priv
->contents
);
300 g_value_set_uint64(value
, priv
->msgtime
);
303 g_value_set_flags(value
, priv
->flags
);
306 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, par_id
, pspec
);
312 purple_message_set_property(GObject
*object
, guint par_id
, const GValue
*value
,
315 PurpleMessage
*message
= PURPLE_MESSAGE(object
);
316 PurpleMessagePrivate
*priv
= purple_message_get_instance_private(message
);
320 g_free(priv
->author
);
321 priv
->author
= g_value_dup_string(value
);
323 case PROP_AUTHOR_ALIAS
:
324 g_free(priv
->author_alias
);
325 priv
->author_alias
= g_value_dup_string(value
);
328 g_free(priv
->recipient
);
329 priv
->recipient
= g_value_dup_string(value
);
332 g_free(priv
->contents
);
333 priv
->contents
= g_value_dup_string(value
);
336 priv
->msgtime
= g_value_get_uint64(value
);
339 priv
->flags
= g_value_get_flags(value
);
342 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, par_id
, pspec
);
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
);
384 _purple_message_init(void)
386 messages
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
387 NULL
, g_object_unref
);
391 _purple_message_uninit(void)
393 g_hash_table_destroy(messages
);