comparison src/roomlist.c @ 8113:d60272410bd5

[gaim-migrate @ 8817] Tim 'marv' Ringenbach wrote us a wonderful core/ui split chat room list thing-a-ma-jig. I've taken the liberty of adding jabber support to it as well. committer: Tailor Script <tailor@pidgin.im>
author Nathan Walp <nwalp@pidgin.im>
date Thu, 15 Jan 2004 22:20:29 +0000
parents
children 596c64a2a2d0
comparison
equal deleted inserted replaced
8112:67387ec77301 8113:d60272410bd5
1 /**
2 * @file roomlist.c Room List API
3 * @ingroup core
4 *
5 * gaim
6 *
7 * Copyright (C) 2003, Timothy Ringenbach <omarvo@hotmail.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 #include <glib.h>
25
26 #include "account.h"
27 #include "connection.h"
28 #include "debug.h"
29 #include "roomlist.h"
30
31
32 static GaimRoomlistUiOps *ops = NULL;
33
34 /**************************************************************************/
35 /** @name Room List API */
36 /**************************************************************************/
37 /*@{*/
38
39 GaimRoomlist *gaim_roomlist_new(GaimAccount *account)
40 {
41 GaimRoomlist *list;
42
43 g_return_val_if_fail(account != NULL, NULL);
44
45 list = g_new0(GaimRoomlist, 1);
46 list->account = account;
47 list->rooms = NULL;
48 list->fields = NULL;
49 list->ref = 1;
50
51 if (ops && ops->new)
52 ops->new(list);
53
54 return list;
55 }
56
57 void gaim_roomlist_ref(GaimRoomlist *list)
58 {
59 g_return_if_fail(list != NULL);
60
61 list->ref++;
62 gaim_debug_misc("roomlist", "reffing list, ref count now %d\n", list->ref);
63 }
64
65 static void gaim_roomlist_room_destroy(GaimRoomlist *list, GaimRoomlistRoom *r)
66 {
67 GList *l, *j;
68
69 for (l = list->fields, j = r->fields; l && j; l = l->next, j = j->next) {
70 GaimRoomlistField *f = l->data;
71 if (f->type == GAIM_ROOMLIST_FIELD_STRING)
72 g_free(j->data);
73 }
74
75 g_list_free(r->fields);
76 g_free(r->name);
77 g_free(r);
78 }
79
80 static void gaim_roomlist_field_destroy(GaimRoomlistField *f)
81 {
82 g_free(f->label);
83 g_free(f->name);
84 g_free(f);
85 }
86
87 static void gaim_roomlist_destroy(GaimRoomlist *list)
88 {
89 GList *l;
90
91 gaim_debug_misc("roomlist", "destroying list %p\n", list);
92
93 if (ops && ops->destroy)
94 ops->destroy(list);
95
96 if (list->rooms) {
97 for (l = list->rooms; l; l = l->next) {
98 GaimRoomlistRoom *r = l->data;
99 gaim_roomlist_room_destroy(list, r);
100 }
101 g_list_free(list->rooms);
102 }
103
104 if (list->fields) {
105 for (l = list->fields; l; l = l->next) {
106 GaimRoomlistField *f = l->data;
107 gaim_roomlist_field_destroy(f);
108 }
109 g_list_free(list->fields);
110 }
111
112 g_free(list);
113 }
114
115 void gaim_roomlist_unref(GaimRoomlist *list)
116 {
117 g_return_if_fail(list != NULL);
118
119 list->ref--;
120
121 gaim_debug_misc("roomlist", "unreffing list, ref count now %d\n", list->ref);
122 if (list->ref == 0)
123 gaim_roomlist_destroy(list);
124 }
125
126 void gaim_roomlist_set_fields(GaimRoomlist *list, GList *fields)
127 {
128 g_return_if_fail(list != NULL);
129
130 list->fields = fields;
131
132 if (ops && ops->set_fields)
133 ops->set_fields(list, fields);
134 }
135
136 void gaim_roomlist_set_in_progress(GaimRoomlist *list, gboolean in_progress)
137 {
138 g_return_if_fail(list != NULL);
139
140 if (ops && ops->in_progress)
141 ops->in_progress(list, in_progress);
142 }
143
144 void gaim_roomlist_room_add(GaimRoomlist *list, GaimRoomlistRoom *room)
145 {
146 g_return_if_fail(list != NULL);
147 g_return_if_fail(room != NULL);
148
149 list->rooms = g_list_append(list->rooms, room);
150
151 if (ops && ops->add_room)
152 ops->add_room(list, room);
153 }
154
155 gboolean gaim_roomlist_is_possible(GaimConnection *gc)
156 {
157 GaimPluginProtocolInfo *prpl_info = NULL;
158
159 g_return_val_if_fail(gc != NULL, FALSE);
160
161 if (gc->prpl != NULL)
162 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl);
163
164 if (prpl_info && prpl_info->roomlist_get_list)
165 return TRUE;
166 return FALSE;
167 }
168
169 GaimRoomlist *gaim_roomlist_get_list(GaimConnection *gc)
170 {
171 GaimPluginProtocolInfo *prpl_info = NULL;
172
173 g_return_val_if_fail(gc != NULL, NULL);
174
175 if (gc->prpl != NULL)
176 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl);
177
178 if (prpl_info && prpl_info->roomlist_get_list)
179 return prpl_info->roomlist_get_list(gc);
180 return NULL;
181 }
182
183 void gaim_roomlist_cancel_get_list(GaimRoomlist *list)
184 {
185 GaimPluginProtocolInfo *prpl_info = NULL;
186 GaimConnection *gc;
187
188 g_return_if_fail(list != NULL);
189
190 gc = gaim_account_get_connection(list->account);
191
192 g_return_if_fail(gc != NULL);
193
194 if (gc != NULL && gc->prpl != NULL)
195 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl);
196
197 if (prpl_info && prpl_info->roomlist_cancel)
198 prpl_info->roomlist_cancel(list);
199 }
200
201 void gaim_roomlist_expand_catagory(GaimRoomlist *list, GaimRoomlistRoom *catagory)
202 {
203 GaimPluginProtocolInfo *prpl_info = NULL;
204 GaimConnection *gc;
205
206 g_return_if_fail(list != NULL);
207 g_return_if_fail(catagory != NULL);
208 g_return_if_fail(catagory->type & GAIM_ROOMLIST_ROOMTYPE_CATAGORY);
209
210 gc = gaim_account_get_connection(list->account);
211 g_return_if_fail(gc != NULL);
212
213 if (gc->prpl != NULL)
214 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl);
215
216 if (prpl_info && prpl_info->roomlist_expand_catagory)
217 prpl_info->roomlist_expand_catagory(list, catagory);
218 }
219
220 /*@}*/
221
222 /**************************************************************************/
223 /** @name Room API */
224 /**************************************************************************/
225 /*@{*/
226
227 GaimRoomlistRoom *gaim_roomlist_room_new(GaimRoomlistRoomType type, const gchar *name,
228 GaimRoomlistRoom *parent)
229 {
230 GaimRoomlistRoom *room;
231
232 g_return_val_if_fail(name != NULL, NULL);
233
234 room = g_new0(GaimRoomlistRoom, 1);
235 room->type = type;
236 room->name = g_strdup(name);
237 room->parent = parent;
238
239 return room;
240 }
241
242 void gaim_roomlist_room_add_field(GaimRoomlist *list, GaimRoomlistRoom *room, gconstpointer field)
243 {
244 GaimRoomlistField *f;
245
246 g_return_if_fail(list != NULL);
247 g_return_if_fail(room != NULL);
248 g_return_if_fail(list->fields != NULL);
249
250 if (!room->fields)
251 f = list->fields->data;
252 else
253 f = g_list_nth_data(list->fields, g_list_length(room->fields));
254
255 g_return_if_fail(f != NULL);
256
257 switch(f->type) {
258 case GAIM_ROOMLIST_FIELD_STRING:
259 room->fields = g_list_append(room->fields, g_strdup(field));
260 break;
261 case GAIM_ROOMLIST_FIELD_BOOL:
262 case GAIM_ROOMLIST_FIELD_INT:
263 room->fields = g_list_append(room->fields, GINT_TO_POINTER(field));
264 break;
265 }
266 }
267
268 /*@}*/
269
270 /**************************************************************************/
271 /** @name Room Field API */
272 /**************************************************************************/
273 /*@{*/
274
275 GaimRoomlistField *gaim_roomlist_field_new(GaimRoomlistFieldType type,
276 const gchar *label, const gchar *name,
277 gboolean hidden)
278 {
279 GaimRoomlistField *f;
280
281 g_return_val_if_fail(label != NULL, NULL);
282 g_return_val_if_fail(name != NULL, NULL);
283
284 f = g_new0(GaimRoomlistField, 1);
285
286 f->type = type;
287 f->label = g_strdup(label);
288 f->name = g_strdup(name);
289 f->hidden = hidden;
290
291 return f;
292 }
293
294 /*@}*/
295
296 /**************************************************************************/
297 /** @name UI Registration Functions */
298 /**************************************************************************/
299 /*@{*/
300
301
302 void gaim_roomlist_set_ui_ops(GaimRoomlistUiOps *ui_ops)
303 {
304 ops = ui_ops;
305 }
306
307 GaimRoomlistUiOps *gaim_roomlist_get_ui_ops(void)
308 {
309 return ops;
310 }
311
312 /*@}*/