2 * @file roomlist.c Room List API
8 * Purple is the legal property of its developers, whose names are too numerous
9 * to list here. Please refer to the COPYRIGHT file distributed with this
10 * source distribution.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
30 #include "connection.h"
36 static PurpleRoomlistUiOps
*ops
= NULL
;
38 /**************************************************************************/
39 /** @name Room List API */
40 /**************************************************************************/
43 void purple_roomlist_show_with_account(PurpleAccount
*account
)
45 if (ops
&& ops
->show_with_account
)
46 ops
->show_with_account(account
);
49 PurpleRoomlist
*purple_roomlist_new(PurpleAccount
*account
)
53 g_return_val_if_fail(account
!= NULL
, NULL
);
55 list
= g_new0(PurpleRoomlist
, 1);
56 list
->account
= account
;
61 if (ops
&& ops
->create
)
67 void purple_roomlist_ref(PurpleRoomlist
*list
)
69 g_return_if_fail(list
!= NULL
);
72 purple_debug_misc("roomlist", "reffing list, ref count now %d\n", list
->ref
);
75 static void purple_roomlist_room_destroy(PurpleRoomlist
*list
, PurpleRoomlistRoom
*r
)
79 for (l
= list
->fields
, j
= r
->fields
; l
&& j
; l
= l
->next
, j
= j
->next
) {
80 PurpleRoomlistField
*f
= l
->data
;
81 if (f
->type
== PURPLE_ROOMLIST_FIELD_STRING
)
85 g_list_free(r
->fields
);
90 static void purple_roomlist_field_destroy(PurpleRoomlistField
*f
)
97 static void purple_roomlist_destroy(PurpleRoomlist
*list
)
101 purple_debug_misc("roomlist", "destroying list %p\n", list
);
103 if (ops
&& ops
->destroy
)
106 for (l
= list
->rooms
; l
; l
= l
->next
) {
107 PurpleRoomlistRoom
*r
= l
->data
;
108 purple_roomlist_room_destroy(list
, r
);
110 g_list_free(list
->rooms
);
112 g_list_foreach(list
->fields
, (GFunc
)purple_roomlist_field_destroy
, NULL
);
113 g_list_free(list
->fields
);
118 void purple_roomlist_unref(PurpleRoomlist
*list
)
120 g_return_if_fail(list
!= NULL
);
121 g_return_if_fail(list
->ref
> 0);
125 purple_debug_misc("roomlist", "unreffing list, ref count now %d\n", list
->ref
);
127 purple_roomlist_destroy(list
);
130 void purple_roomlist_set_fields(PurpleRoomlist
*list
, GList
*fields
)
132 g_return_if_fail(list
!= NULL
);
134 list
->fields
= fields
;
136 if (ops
&& ops
->set_fields
)
137 ops
->set_fields(list
, fields
);
140 void purple_roomlist_set_in_progress(PurpleRoomlist
*list
, gboolean in_progress
)
142 g_return_if_fail(list
!= NULL
);
144 list
->in_progress
= in_progress
;
146 if (ops
&& ops
->in_progress
)
147 ops
->in_progress(list
, in_progress
);
150 gboolean
purple_roomlist_get_in_progress(PurpleRoomlist
*list
)
152 g_return_val_if_fail(list
!= NULL
, FALSE
);
154 return list
->in_progress
;
157 void purple_roomlist_room_add(PurpleRoomlist
*list
, PurpleRoomlistRoom
*room
)
159 g_return_if_fail(list
!= NULL
);
160 g_return_if_fail(room
!= NULL
);
162 list
->rooms
= g_list_append(list
->rooms
, room
);
164 if (ops
&& ops
->add_room
)
165 ops
->add_room(list
, room
);
168 PurpleRoomlist
*purple_roomlist_get_list(PurpleConnection
*gc
)
170 PurplePlugin
*prpl
= NULL
;
171 PurplePluginProtocolInfo
*prpl_info
= NULL
;
173 g_return_val_if_fail(gc
!= NULL
, NULL
);
174 g_return_val_if_fail(PURPLE_CONNECTION_IS_CONNECTED(gc
), NULL
);
176 prpl
= purple_connection_get_prpl(gc
);
179 prpl_info
= PURPLE_PLUGIN_PROTOCOL_INFO(prpl
);
181 if(prpl_info
&& prpl_info
->roomlist_get_list
)
182 return prpl_info
->roomlist_get_list(gc
);
187 void purple_roomlist_cancel_get_list(PurpleRoomlist
*list
)
189 PurplePlugin
*prpl
= NULL
;
190 PurplePluginProtocolInfo
*prpl_info
= NULL
;
191 PurpleConnection
*gc
;
193 g_return_if_fail(list
!= NULL
);
195 gc
= purple_account_get_connection(list
->account
);
197 g_return_if_fail(gc
!= NULL
);
200 prpl
= purple_connection_get_prpl(gc
);
203 prpl_info
= PURPLE_PLUGIN_PROTOCOL_INFO(prpl
);
205 if(prpl_info
&& prpl_info
->roomlist_cancel
)
206 prpl_info
->roomlist_cancel(list
);
209 void purple_roomlist_expand_category(PurpleRoomlist
*list
, PurpleRoomlistRoom
*category
)
211 PurplePlugin
*prpl
= NULL
;
212 PurplePluginProtocolInfo
*prpl_info
= NULL
;
213 PurpleConnection
*gc
;
215 g_return_if_fail(list
!= NULL
);
216 g_return_if_fail(category
!= NULL
);
217 g_return_if_fail(category
->type
& PURPLE_ROOMLIST_ROOMTYPE_CATEGORY
);
219 gc
= purple_account_get_connection(list
->account
);
220 g_return_if_fail(gc
!= NULL
);
223 prpl
= purple_connection_get_prpl(gc
);
226 prpl_info
= PURPLE_PLUGIN_PROTOCOL_INFO(prpl
);
228 if(prpl_info
&& prpl_info
->roomlist_expand_category
)
229 prpl_info
->roomlist_expand_category(list
, category
);
232 GList
* purple_roomlist_get_fields(PurpleRoomlist
*list
)
239 /**************************************************************************/
240 /** @name Room API */
241 /**************************************************************************/
244 PurpleRoomlistRoom
*purple_roomlist_room_new(PurpleRoomlistRoomType type
, const gchar
*name
,
245 PurpleRoomlistRoom
*parent
)
247 PurpleRoomlistRoom
*room
;
249 g_return_val_if_fail(name
!= NULL
, NULL
);
251 room
= g_new0(PurpleRoomlistRoom
, 1);
253 room
->name
= g_strdup(name
);
254 room
->parent
= parent
;
259 void purple_roomlist_room_add_field(PurpleRoomlist
*list
, PurpleRoomlistRoom
*room
, gconstpointer field
)
261 PurpleRoomlistField
*f
;
263 g_return_if_fail(list
!= NULL
);
264 g_return_if_fail(room
!= NULL
);
265 g_return_if_fail(list
->fields
!= NULL
);
267 /* If this is the first call for this room, grab the first field in
268 * the Roomlist's fields. Otherwise, grab the field that is one
269 * more than the number of fields already present for the room.
270 * (This works because g_list_nth_data() is zero-indexed and
271 * g_list_length() is one-indexed.) */
273 f
= list
->fields
->data
;
275 f
= g_list_nth_data(list
->fields
, g_list_length(room
->fields
));
277 g_return_if_fail(f
!= NULL
);
280 case PURPLE_ROOMLIST_FIELD_STRING
:
281 room
->fields
= g_list_append(room
->fields
, g_strdup(field
));
283 case PURPLE_ROOMLIST_FIELD_BOOL
:
284 case PURPLE_ROOMLIST_FIELD_INT
:
285 room
->fields
= g_list_append(room
->fields
, GINT_TO_POINTER(field
));
290 void purple_roomlist_room_join(PurpleRoomlist
*list
, PurpleRoomlistRoom
*room
)
292 GHashTable
*components
;
294 PurpleConnection
*gc
;
296 g_return_if_fail(list
!= NULL
);
297 g_return_if_fail(room
!= NULL
);
299 gc
= purple_account_get_connection(list
->account
);
303 components
= g_hash_table_new(g_str_hash
, g_str_equal
);
305 g_hash_table_replace(components
, "name", room
->name
);
306 for (l
= list
->fields
, j
= room
->fields
; l
&& j
; l
= l
->next
, j
= j
->next
) {
307 PurpleRoomlistField
*f
= l
->data
;
309 g_hash_table_replace(components
, f
->name
, j
->data
);
312 serv_join_chat(gc
, components
);
314 g_hash_table_destroy(components
);
317 PurpleRoomlistRoomType
purple_roomlist_room_get_type(PurpleRoomlistRoom
*room
)
322 const char * purple_roomlist_room_get_name(PurpleRoomlistRoom
*room
)
327 PurpleRoomlistRoom
* purple_roomlist_room_get_parent(PurpleRoomlistRoom
*room
)
332 GList
* purple_roomlist_room_get_fields(PurpleRoomlistRoom
*room
)
339 /**************************************************************************/
340 /** @name Room Field API */
341 /**************************************************************************/
344 PurpleRoomlistField
*purple_roomlist_field_new(PurpleRoomlistFieldType type
,
345 const gchar
*label
, const gchar
*name
,
348 PurpleRoomlistField
*f
;
350 g_return_val_if_fail(label
!= NULL
, NULL
);
351 g_return_val_if_fail(name
!= NULL
, NULL
);
353 f
= g_new0(PurpleRoomlistField
, 1);
356 f
->label
= g_strdup(label
);
357 f
->name
= g_strdup(name
);
363 PurpleRoomlistFieldType
purple_roomlist_field_get_type(PurpleRoomlistField
*field
)
368 const char * purple_roomlist_field_get_label(PurpleRoomlistField
*field
)
373 gboolean
purple_roomlist_field_get_hidden(PurpleRoomlistField
*field
)
375 return field
->hidden
;
380 /**************************************************************************/
381 /** @name UI Registration Functions */
382 /**************************************************************************/
386 void purple_roomlist_set_ui_ops(PurpleRoomlistUiOps
*ui_ops
)
391 PurpleRoomlistUiOps
*purple_roomlist_get_ui_ops(void)