Standardize all protocol header guard macros.
[pidgin-git.git] / libpurple / chat.c
blob773313b7e20162038b544bd36e4ab3508b59ed78
1 /*
2 * purple
4 * Purple is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 #include "internal.h"
24 #include "chat.h"
25 #include "util.h"
27 typedef struct _PurpleChatPrivate PurpleChatPrivate;
29 /* Private data for a chat node */
30 struct _PurpleChatPrivate {
31 char *alias; /* The display name of this chat. */
32 PurpleAccount *account; /* The account this chat is attached to */
33 GHashTable *components; /* the stuff the protocol needs to know to
34 join the chat */
36 gboolean is_constructed; /* Indicates if the chat has finished being
37 constructed. */
40 /* Chat property enums */
41 enum
43 PROP_0,
44 PROP_ALIAS,
45 PROP_ACCOUNT,
46 PROP_COMPONENTS,
47 PROP_LAST
50 /******************************************************************************
51 * Globals
52 *****************************************************************************/
53 static GParamSpec *properties[PROP_LAST];
55 G_DEFINE_TYPE_WITH_PRIVATE(PurpleChat, purple_chat, PURPLE_TYPE_BLIST_NODE);
57 /******************************************************************************
58 * API
59 *****************************************************************************/
61 const char *purple_chat_get_name(PurpleChat *chat)
63 PurpleChatPrivate *priv = NULL;
65 g_return_val_if_fail(PURPLE_IS_CHAT(chat), NULL);
67 priv = purple_chat_get_instance_private(chat);
69 if ((priv->alias != NULL) && (*priv->alias != '\0'))
70 return priv->alias;
72 return purple_chat_get_name_only(chat);
75 const char *purple_chat_get_name_only(PurpleChat *chat)
77 PurpleChatPrivate *priv = NULL;
78 char *ret = NULL;
79 PurpleProtocol *protocol = NULL;
81 g_return_val_if_fail(PURPLE_IS_CHAT(chat), NULL);
83 priv = purple_chat_get_instance_private(chat);
85 protocol = purple_protocols_find(purple_account_get_protocol_id(priv->account));
87 if (PURPLE_PROTOCOL_IMPLEMENTS(protocol, CHAT, info)) {
88 PurpleProtocolChatEntry *pce;
89 GList *parts = purple_protocol_chat_iface_info(protocol, purple_account_get_connection(priv->account));
90 pce = parts->data;
91 ret = g_hash_table_lookup(priv->components, pce->identifier);
92 g_list_foreach(parts, (GFunc)g_free, NULL);
93 g_list_free(parts);
96 return ret;
99 void
100 purple_chat_set_alias(PurpleChat *chat, const char *alias)
102 PurpleChatPrivate *priv = NULL;
103 char *old_alias;
104 char *new_alias = NULL;
106 g_return_if_fail(PURPLE_IS_CHAT(chat));
108 priv = purple_chat_get_instance_private(chat);
110 if ((alias != NULL) && (*alias != '\0'))
111 new_alias = purple_utf8_strip_unprintables(alias);
113 if (!purple_strequal(priv->alias, new_alias)) {
114 g_free(new_alias);
115 return;
118 old_alias = priv->alias;
120 if ((new_alias != NULL) && (*new_alias != '\0'))
121 priv->alias = new_alias;
122 else {
123 priv->alias = NULL;
124 g_free(new_alias); /* could be "\0" */
127 g_object_notify_by_pspec(G_OBJECT(chat), properties[PROP_ALIAS]);
129 purple_blist_save_node(purple_blist_get_default(),
130 PURPLE_BLIST_NODE(chat));
131 purple_blist_update_node(purple_blist_get_default(),
132 PURPLE_BLIST_NODE(chat));
134 purple_signal_emit(purple_blist_get_handle(), "blist-node-aliased",
135 chat, old_alias);
136 g_free(old_alias);
139 PurpleGroup *
140 purple_chat_get_group(PurpleChat *chat)
142 g_return_val_if_fail(PURPLE_IS_CHAT(chat), NULL);
144 return PURPLE_GROUP(PURPLE_BLIST_NODE(chat)->parent);
147 PurpleAccount *
148 purple_chat_get_account(PurpleChat *chat)
150 PurpleChatPrivate *priv = NULL;
152 g_return_val_if_fail(PURPLE_IS_CHAT(chat), NULL);
154 priv = purple_chat_get_instance_private(chat);
155 return priv->account;
158 GHashTable *
159 purple_chat_get_components(PurpleChat *chat)
161 PurpleChatPrivate *priv = NULL;
163 g_return_val_if_fail(PURPLE_IS_CHAT(chat), NULL);
165 priv = purple_chat_get_instance_private(chat);
166 return priv->components;
169 /******************************************************************************
170 * GObject Stuff
171 *****************************************************************************/
172 static void
173 purple_chat_set_property(GObject *obj, guint param_id, const GValue *value,
174 GParamSpec *pspec)
176 PurpleChat *chat = PURPLE_CHAT(obj);
177 PurpleChatPrivate *priv = purple_chat_get_instance_private(chat);
179 switch (param_id) {
180 case PROP_ALIAS:
181 if (priv->is_constructed)
182 purple_chat_set_alias(chat, g_value_get_string(value));
183 else
184 priv->alias =
185 purple_utf8_strip_unprintables(g_value_get_string(value));
186 break;
187 case PROP_ACCOUNT:
188 priv->account = g_value_get_object(value);
189 break;
190 case PROP_COMPONENTS:
191 priv->components = g_value_get_pointer(value);
192 break;
193 default:
194 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
195 break;
199 static void
200 purple_chat_get_property(GObject *obj, guint param_id, GValue *value,
201 GParamSpec *pspec)
203 PurpleChat *chat = PURPLE_CHAT(obj);
204 PurpleChatPrivate *priv = purple_chat_get_instance_private(chat);
206 switch (param_id) {
207 case PROP_ALIAS:
208 g_value_set_string(value, priv->alias);
209 break;
210 case PROP_ACCOUNT:
211 g_value_set_object(value, purple_chat_get_account(chat));
212 break;
213 case PROP_COMPONENTS:
214 g_value_set_pointer(value, purple_chat_get_components(chat));
215 break;
216 default:
217 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
218 break;
222 /* GObject initialization function */
223 static void
224 purple_chat_init(PurpleChat *chat)
228 /* Called when done constructing */
229 static void
230 purple_chat_constructed(GObject *object)
232 PurpleChat *chat = PURPLE_CHAT(object);
233 PurpleChatPrivate *priv = purple_chat_get_instance_private(chat);
235 G_OBJECT_CLASS(purple_chat_parent_class)->constructed(object);
237 purple_blist_new_node(purple_blist_get_default(),
238 PURPLE_BLIST_NODE(chat));
240 priv->is_constructed = TRUE;
243 /* GObject finalize function */
244 static void
245 purple_chat_finalize(GObject *object)
247 PurpleChatPrivate *priv =
248 purple_chat_get_instance_private(PURPLE_CHAT(object));
250 g_free(priv->alias);
251 g_hash_table_destroy(priv->components);
253 G_OBJECT_CLASS(purple_chat_parent_class)->finalize(object);
256 /* Class initializer function */
257 static void purple_chat_class_init(PurpleChatClass *klass)
259 GObjectClass *obj_class = G_OBJECT_CLASS(klass);
261 obj_class->finalize = purple_chat_finalize;
263 /* Setup properties */
264 obj_class->get_property = purple_chat_get_property;
265 obj_class->set_property = purple_chat_set_property;
266 obj_class->constructed = purple_chat_constructed;
268 properties[PROP_ALIAS] = g_param_spec_string(
269 "alias",
270 "Alias",
271 "The alias for the chat.",
272 NULL,
273 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS
276 properties[PROP_ACCOUNT] = g_param_spec_object(
277 "account",
278 "Account",
279 "The account that the chat belongs to.",
280 PURPLE_TYPE_ACCOUNT,
281 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS
284 properties[PROP_COMPONENTS] = g_param_spec_pointer(
285 "components",
286 "Components",
287 "The protocol components of the chat.",
288 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS
291 g_object_class_install_properties(obj_class, PROP_LAST, properties);
294 PurpleChat *
295 purple_chat_new(PurpleAccount *account, const char *alias, GHashTable *components)
297 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);
298 g_return_val_if_fail(components != NULL, NULL);
300 return g_object_new(PURPLE_TYPE_CHAT,
301 "account", account,
302 "alias", alias,
303 "components", components,
304 NULL);