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
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
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
36 gboolean is_constructed
; /* Indicates if the chat has finished being
40 /* Chat property enums */
50 /******************************************************************************
52 *****************************************************************************/
53 static GParamSpec
*properties
[PROP_LAST
];
55 G_DEFINE_TYPE_WITH_PRIVATE(PurpleChat
, purple_chat
, PURPLE_TYPE_BLIST_NODE
);
57 /******************************************************************************
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'))
72 return purple_chat_get_name_only(chat
);
75 const char *purple_chat_get_name_only(PurpleChat
*chat
)
77 PurpleChatPrivate
*priv
= 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
));
91 ret
= g_hash_table_lookup(priv
->components
, pce
->identifier
);
92 g_list_foreach(parts
, (GFunc
)g_free
, NULL
);
100 purple_chat_set_alias(PurpleChat
*chat
, const char *alias
)
102 PurpleChatPrivate
*priv
= NULL
;
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
)) {
118 old_alias
= priv
->alias
;
120 if ((new_alias
!= NULL
) && (*new_alias
!= '\0'))
121 priv
->alias
= new_alias
;
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",
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
);
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
;
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 /******************************************************************************
171 *****************************************************************************/
173 purple_chat_set_property(GObject
*obj
, guint param_id
, const GValue
*value
,
176 PurpleChat
*chat
= PURPLE_CHAT(obj
);
177 PurpleChatPrivate
*priv
= purple_chat_get_instance_private(chat
);
181 if (priv
->is_constructed
)
182 purple_chat_set_alias(chat
, g_value_get_string(value
));
185 purple_utf8_strip_unprintables(g_value_get_string(value
));
188 priv
->account
= g_value_get_object(value
);
190 case PROP_COMPONENTS
:
191 priv
->components
= g_value_get_pointer(value
);
194 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj
, param_id
, pspec
);
200 purple_chat_get_property(GObject
*obj
, guint param_id
, GValue
*value
,
203 PurpleChat
*chat
= PURPLE_CHAT(obj
);
204 PurpleChatPrivate
*priv
= purple_chat_get_instance_private(chat
);
208 g_value_set_string(value
, priv
->alias
);
211 g_value_set_object(value
, purple_chat_get_account(chat
));
213 case PROP_COMPONENTS
:
214 g_value_set_pointer(value
, purple_chat_get_components(chat
));
217 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj
, param_id
, pspec
);
222 /* GObject initialization function */
224 purple_chat_init(PurpleChat
*chat
)
228 /* Called when done constructing */
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 */
245 purple_chat_finalize(GObject
*object
)
247 PurpleChatPrivate
*priv
=
248 purple_chat_get_instance_private(PURPLE_CHAT(object
));
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(
271 "The alias for the chat.",
273 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT
| G_PARAM_STATIC_STRINGS
276 properties
[PROP_ACCOUNT
] = g_param_spec_object(
279 "The account that the chat belongs to.",
281 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
| G_PARAM_STATIC_STRINGS
284 properties
[PROP_COMPONENTS
] = g_param_spec_pointer(
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
);
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
,
303 "components", components
,