Mon, 19 Mar 2007 07:01:17 +0000
sed -ie 's/gaim/purple/g'
| 8113 | 1 | /** |
| 2 | * @file roomlist.c Room List API | |
| 3 | * @ingroup core | |
| 4 | * | |
| 15884 | 5 | * purple |
| 8113 | 6 | * |
| 15884 | 7 | * Purple is the legal property of its developers, whose names are too numerous |
|
8146
4961c9c5fd61
[gaim-migrate @ 8854]
John Silvestri <john.silvestri@gmail.com>
parents:
8113
diff
changeset
|
8 | * to list here. Please refer to the COPYRIGHT file distributed with this |
|
4961c9c5fd61
[gaim-migrate @ 8854]
John Silvestri <john.silvestri@gmail.com>
parents:
8113
diff
changeset
|
9 | * source distribution. |
| 8113 | 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify | |
| 12 | * it under the terms of the GNU General Public License as published by | |
| 13 | * the Free Software Foundation; either version 2 of the License, or | |
| 14 | * (at your option) any later version. | |
| 15 | * | |
| 16 | * This program is distributed in the hope that it will be useful, | |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 19 | * GNU General Public License for more details. | |
| 20 | * | |
| 21 | * You should have received a copy of the GNU General Public License | |
| 22 | * along with this program; if not, write to the Free Software | |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 24 | */ | |
| 25 | ||
| 26 | #include <glib.h> | |
| 27 | ||
| 28 | #include "account.h" | |
| 29 | #include "connection.h" | |
| 30 | #include "debug.h" | |
| 31 | #include "roomlist.h" | |
| 8199 | 32 | #include "server.h" |
| 8113 | 33 | |
| 34 | ||
| 15884 | 35 | static PurpleRoomlistUiOps *ops = NULL; |
| 8113 | 36 | |
| 37 | /**************************************************************************/ | |
| 38 | /** @name Room List API */ | |
| 39 | /**************************************************************************/ | |
| 40 | /*@{*/ | |
| 41 | ||
| 15884 | 42 | void purple_roomlist_show_with_account(PurpleAccount *account) |
| 8352 | 43 | { |
| 44 | if (ops && ops->show_with_account) | |
| 45 | ops->show_with_account(account); | |
| 46 | } | |
| 47 | ||
| 15884 | 48 | PurpleRoomlist *purple_roomlist_new(PurpleAccount *account) |
| 8113 | 49 | { |
| 15884 | 50 | PurpleRoomlist *list; |
| 8113 | 51 | |
| 52 | g_return_val_if_fail(account != NULL, NULL); | |
| 53 | ||
| 15884 | 54 | list = g_new0(PurpleRoomlist, 1); |
| 8113 | 55 | list->account = account; |
| 56 | list->rooms = NULL; | |
| 57 | list->fields = NULL; | |
| 58 | list->ref = 1; | |
| 59 | ||
| 10027 | 60 | if (ops && ops->create) |
| 61 | ops->create(list); | |
| 8113 | 62 | |
| 63 | return list; | |
| 64 | } | |
| 65 | ||
| 15884 | 66 | void purple_roomlist_ref(PurpleRoomlist *list) |
| 8113 | 67 | { |
| 68 | g_return_if_fail(list != NULL); | |
| 69 | ||
| 70 | list->ref++; | |
| 15884 | 71 | purple_debug_misc("roomlist", "reffing list, ref count now %d\n", list->ref); |
| 8113 | 72 | } |
| 73 | ||
| 15884 | 74 | static void purple_roomlist_room_destroy(PurpleRoomlist *list, PurpleRoomlistRoom *r) |
| 8113 | 75 | { |
| 76 | GList *l, *j; | |
| 77 | ||
| 78 | for (l = list->fields, j = r->fields; l && j; l = l->next, j = j->next) { | |
| 15884 | 79 | PurpleRoomlistField *f = l->data; |
| 80 | if (f->type == PURPLE_ROOMLIST_FIELD_STRING) | |
| 8113 | 81 | g_free(j->data); |
| 82 | } | |
| 83 | ||
| 84 | g_list_free(r->fields); | |
| 85 | g_free(r->name); | |
| 86 | g_free(r); | |
| 87 | } | |
| 88 | ||
| 15884 | 89 | static void purple_roomlist_field_destroy(PurpleRoomlistField *f) |
| 8113 | 90 | { |
| 91 | g_free(f->label); | |
| 92 | g_free(f->name); | |
| 93 | g_free(f); | |
| 94 | } | |
| 95 | ||
| 15884 | 96 | static void purple_roomlist_destroy(PurpleRoomlist *list) |
| 8113 | 97 | { |
| 98 | GList *l; | |
| 99 | ||
| 15884 | 100 | purple_debug_misc("roomlist", "destroying list %p\n", list); |
| 8113 | 101 | |
| 102 | if (ops && ops->destroy) | |
| 103 | ops->destroy(list); | |
| 104 | ||
|
14097
0c340861ab79
[gaim-migrate @ 16638]
Mark Doliner <markdoliner@pidgin.im>
parents:
12250
diff
changeset
|
105 | for (l = list->rooms; l; l = l->next) { |
| 15884 | 106 | PurpleRoomlistRoom *r = l->data; |
| 107 | purple_roomlist_room_destroy(list, r); | |
| 8113 | 108 | } |
|
14097
0c340861ab79
[gaim-migrate @ 16638]
Mark Doliner <markdoliner@pidgin.im>
parents:
12250
diff
changeset
|
109 | g_list_free(list->rooms); |
| 8113 | 110 | |
| 15884 | 111 | g_list_foreach(list->fields, (GFunc)purple_roomlist_field_destroy, NULL); |
|
14097
0c340861ab79
[gaim-migrate @ 16638]
Mark Doliner <markdoliner@pidgin.im>
parents:
12250
diff
changeset
|
112 | g_list_free(list->fields); |
| 8113 | 113 | |
| 114 | g_free(list); | |
| 115 | } | |
| 116 | ||
| 15884 | 117 | void purple_roomlist_unref(PurpleRoomlist *list) |
| 8113 | 118 | { |
| 119 | g_return_if_fail(list != NULL); | |
|
12250
5b14301dd1ec
[gaim-migrate @ 14552]
Richard Laager <rlaager@pidgin.im>
parents:
10027
diff
changeset
|
120 | g_return_if_fail(list->ref > 0); |
| 8113 | 121 | |
| 122 | list->ref--; | |
| 123 | ||
| 15884 | 124 | purple_debug_misc("roomlist", "unreffing list, ref count now %d\n", list->ref); |
| 8113 | 125 | if (list->ref == 0) |
| 15884 | 126 | purple_roomlist_destroy(list); |
| 8113 | 127 | } |
| 128 | ||
| 15884 | 129 | void purple_roomlist_set_fields(PurpleRoomlist *list, GList *fields) |
| 8113 | 130 | { |
| 131 | g_return_if_fail(list != NULL); | |
| 132 | ||
| 133 | list->fields = fields; | |
| 134 | ||
| 135 | if (ops && ops->set_fields) | |
| 136 | ops->set_fields(list, fields); | |
| 137 | } | |
| 138 | ||
| 15884 | 139 | void purple_roomlist_set_in_progress(PurpleRoomlist *list, gboolean in_progress) |
| 8113 | 140 | { |
| 141 | g_return_if_fail(list != NULL); | |
| 142 | ||
| 8199 | 143 | list->in_progress = in_progress; |
| 144 | ||
| 8113 | 145 | if (ops && ops->in_progress) |
| 146 | ops->in_progress(list, in_progress); | |
| 147 | } | |
| 148 | ||
| 15884 | 149 | gboolean purple_roomlist_get_in_progress(PurpleRoomlist *list) |
| 8199 | 150 | { |
| 151 | g_return_val_if_fail(list != NULL, FALSE); | |
| 152 | ||
| 153 | return list->in_progress; | |
| 154 | } | |
| 155 | ||
| 15884 | 156 | void purple_roomlist_room_add(PurpleRoomlist *list, PurpleRoomlistRoom *room) |
| 8113 | 157 | { |
| 158 | g_return_if_fail(list != NULL); | |
| 159 | g_return_if_fail(room != NULL); | |
| 160 | ||
| 161 | list->rooms = g_list_append(list->rooms, room); | |
| 162 | ||
| 163 | if (ops && ops->add_room) | |
| 164 | ops->add_room(list, room); | |
| 165 | } | |
| 166 | ||
| 15884 | 167 | PurpleRoomlist *purple_roomlist_get_list(PurpleConnection *gc) |
| 8113 | 168 | { |
| 15884 | 169 | PurplePluginProtocolInfo *prpl_info = NULL; |
| 8113 | 170 | |
| 171 | g_return_val_if_fail(gc != NULL, NULL); | |
| 172 | ||
| 173 | if (gc->prpl != NULL) | |
| 15884 | 174 | prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(gc->prpl); |
| 8113 | 175 | |
| 176 | if (prpl_info && prpl_info->roomlist_get_list) | |
| 177 | return prpl_info->roomlist_get_list(gc); | |
| 178 | return NULL; | |
| 179 | } | |
| 180 | ||
| 15884 | 181 | void purple_roomlist_cancel_get_list(PurpleRoomlist *list) |
| 8113 | 182 | { |
| 15884 | 183 | PurplePluginProtocolInfo *prpl_info = NULL; |
| 184 | PurpleConnection *gc; | |
| 8113 | 185 | |
| 186 | g_return_if_fail(list != NULL); | |
| 187 | ||
| 15884 | 188 | gc = purple_account_get_connection(list->account); |
| 8113 | 189 | |
| 190 | g_return_if_fail(gc != NULL); | |
| 191 | ||
| 192 | if (gc != NULL && gc->prpl != NULL) | |
| 15884 | 193 | prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(gc->prpl); |
| 8113 | 194 | |
| 195 | if (prpl_info && prpl_info->roomlist_cancel) | |
| 196 | prpl_info->roomlist_cancel(list); | |
| 197 | } | |
| 198 | ||
| 15884 | 199 | void purple_roomlist_expand_category(PurpleRoomlist *list, PurpleRoomlistRoom *category) |
| 8113 | 200 | { |
| 15884 | 201 | PurplePluginProtocolInfo *prpl_info = NULL; |
| 202 | PurpleConnection *gc; | |
| 8113 | 203 | |
| 204 | g_return_if_fail(list != NULL); | |
| 8584 | 205 | g_return_if_fail(category != NULL); |
| 15884 | 206 | g_return_if_fail(category->type & PURPLE_ROOMLIST_ROOMTYPE_CATEGORY); |
| 8113 | 207 | |
| 15884 | 208 | gc = purple_account_get_connection(list->account); |
| 8113 | 209 | g_return_if_fail(gc != NULL); |
| 210 | ||
| 211 | if (gc->prpl != NULL) | |
| 15884 | 212 | prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(gc->prpl); |
| 8113 | 213 | |
| 8584 | 214 | if (prpl_info && prpl_info->roomlist_expand_category) |
| 215 | prpl_info->roomlist_expand_category(list, category); | |
| 8113 | 216 | } |
| 217 | ||
| 218 | /*@}*/ | |
| 219 | ||
| 220 | /**************************************************************************/ | |
| 221 | /** @name Room API */ | |
| 222 | /**************************************************************************/ | |
| 223 | /*@{*/ | |
| 224 | ||
| 15884 | 225 | PurpleRoomlistRoom *purple_roomlist_room_new(PurpleRoomlistRoomType type, const gchar *name, |
| 226 | PurpleRoomlistRoom *parent) | |
| 8113 | 227 | { |
| 15884 | 228 | PurpleRoomlistRoom *room; |
| 8113 | 229 | |
| 230 | g_return_val_if_fail(name != NULL, NULL); | |
| 231 | ||
| 15884 | 232 | room = g_new0(PurpleRoomlistRoom, 1); |
| 8113 | 233 | room->type = type; |
| 234 | room->name = g_strdup(name); | |
| 235 | room->parent = parent; | |
| 236 | ||
| 237 | return room; | |
| 238 | } | |
| 239 | ||
| 15884 | 240 | void purple_roomlist_room_add_field(PurpleRoomlist *list, PurpleRoomlistRoom *room, gconstpointer field) |
| 8113 | 241 | { |
| 15884 | 242 | PurpleRoomlistField *f; |
| 8113 | 243 | |
| 244 | g_return_if_fail(list != NULL); | |
| 245 | g_return_if_fail(room != NULL); | |
| 246 | g_return_if_fail(list->fields != NULL); | |
| 247 | ||
| 248 | if (!room->fields) | |
| 249 | f = list->fields->data; | |
| 250 | else | |
| 251 | f = g_list_nth_data(list->fields, g_list_length(room->fields)); | |
| 252 | ||
| 253 | g_return_if_fail(f != NULL); | |
| 254 | ||
| 255 | switch(f->type) { | |
| 15884 | 256 | case PURPLE_ROOMLIST_FIELD_STRING: |
| 8113 | 257 | room->fields = g_list_append(room->fields, g_strdup(field)); |
| 258 | break; | |
| 15884 | 259 | case PURPLE_ROOMLIST_FIELD_BOOL: |
| 260 | case PURPLE_ROOMLIST_FIELD_INT: | |
| 8113 | 261 | room->fields = g_list_append(room->fields, GINT_TO_POINTER(field)); |
| 262 | break; | |
| 263 | } | |
| 264 | } | |
| 265 | ||
| 15884 | 266 | void purple_roomlist_room_join(PurpleRoomlist *list, PurpleRoomlistRoom *room) |
| 8199 | 267 | { |
| 268 | GHashTable *components; | |
| 269 | GList *l, *j; | |
| 15884 | 270 | PurpleConnection *gc; |
| 8199 | 271 | |
| 272 | g_return_if_fail(list != NULL); | |
| 273 | g_return_if_fail(room != NULL); | |
| 274 | ||
| 15884 | 275 | gc = purple_account_get_connection(list->account); |
| 8199 | 276 | if (!gc) |
| 277 | return; | |
| 278 | ||
| 279 | components = g_hash_table_new(g_str_hash, g_str_equal); | |
| 280 | ||
| 281 | g_hash_table_replace(components, "name", room->name); | |
| 282 | for (l = list->fields, j = room->fields; l && j; l = l->next, j = j->next) { | |
| 15884 | 283 | PurpleRoomlistField *f = l->data; |
| 8199 | 284 | |
| 285 | g_hash_table_replace(components, f->name, j->data); | |
| 286 | } | |
| 287 | ||
| 288 | serv_join_chat(gc, components); | |
| 289 | ||
| 290 | g_hash_table_destroy(components); | |
| 291 | } | |
| 292 | ||
| 8113 | 293 | /*@}*/ |
| 294 | ||
| 295 | /**************************************************************************/ | |
| 296 | /** @name Room Field API */ | |
| 297 | /**************************************************************************/ | |
| 298 | /*@{*/ | |
| 299 | ||
| 15884 | 300 | PurpleRoomlistField *purple_roomlist_field_new(PurpleRoomlistFieldType type, |
| 8113 | 301 | const gchar *label, const gchar *name, |
| 302 | gboolean hidden) | |
| 303 | { | |
| 15884 | 304 | PurpleRoomlistField *f; |
| 8113 | 305 | |
| 306 | g_return_val_if_fail(label != NULL, NULL); | |
| 307 | g_return_val_if_fail(name != NULL, NULL); | |
| 308 | ||
| 15884 | 309 | f = g_new0(PurpleRoomlistField, 1); |
| 8113 | 310 | |
| 311 | f->type = type; | |
| 312 | f->label = g_strdup(label); | |
| 313 | f->name = g_strdup(name); | |
| 314 | f->hidden = hidden; | |
| 315 | ||
| 316 | return f; | |
| 317 | } | |
| 318 | ||
| 319 | /*@}*/ | |
| 320 | ||
| 321 | /**************************************************************************/ | |
| 322 | /** @name UI Registration Functions */ | |
| 323 | /**************************************************************************/ | |
| 324 | /*@{*/ | |
| 325 | ||
| 326 | ||
| 15884 | 327 | void purple_roomlist_set_ui_ops(PurpleRoomlistUiOps *ui_ops) |
| 8113 | 328 | { |
| 329 | ops = ui_ops; | |
| 330 | } | |
| 331 | ||
| 15884 | 332 | PurpleRoomlistUiOps *purple_roomlist_get_ui_ops(void) |
| 8113 | 333 | { |
| 334 | return ops; | |
| 335 | } | |
| 336 | ||
| 337 | /*@}*/ |