Mon, 01 Oct 2007 21:25:18 +0000
merge of '2a1e61ae4a0491e4906ffc58a5d4c01f406b5a61'
and '933b5898de30964f6fd69cbb3c5bbf029db95405'
| 8113 | 1 | /** |
| 2 | * @file roomlist.c Room List API | |
| 3 | * @ingroup core | |
|
20147
66f05a854eee
applied changes from 8a731bbd0197fbcc91a705c2d8f528154216defa
Richard Laager <rlaager@pidgin.im>
parents:
19859
diff
changeset
|
4 | */ |
|
66f05a854eee
applied changes from 8a731bbd0197fbcc91a705c2d8f528154216defa
Richard Laager <rlaager@pidgin.im>
parents:
19859
diff
changeset
|
5 | |
|
66f05a854eee
applied changes from 8a731bbd0197fbcc91a705c2d8f528154216defa
Richard Laager <rlaager@pidgin.im>
parents:
19859
diff
changeset
|
6 | /* purple |
| 8113 | 7 | * |
| 15884 | 8 | * 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
|
9 | * 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
|
10 | * source distribution. |
| 8113 | 11 | * |
| 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. | |
| 16 | * | |
| 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. | |
| 21 | * | |
| 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 | |
|
19859
71d37b57eff2
The FSF changed its address a while ago; our files were out of date.
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
18265
diff
changeset
|
24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
| 8113 | 25 | */ |
| 26 | ||
| 27 | #include <glib.h> | |
| 28 | ||
|
18265
9f26190d7f46
Move the define in internal.h instead.
Sadrul Habib Chowdhury <sadrul@pidgin.im>
parents:
15884
diff
changeset
|
29 | #include "internal.h" |
|
9f26190d7f46
Move the define in internal.h instead.
Sadrul Habib Chowdhury <sadrul@pidgin.im>
parents:
15884
diff
changeset
|
30 | |
| 8113 | 31 | #include "account.h" |
| 32 | #include "connection.h" | |
| 33 | #include "debug.h" | |
| 34 | #include "roomlist.h" | |
| 8199 | 35 | #include "server.h" |
| 8113 | 36 | |
| 37 | ||
| 15884 | 38 | static PurpleRoomlistUiOps *ops = NULL; |
| 8113 | 39 | |
| 40 | /**************************************************************************/ | |
| 41 | /** @name Room List API */ | |
| 42 | /**************************************************************************/ | |
| 43 | /*@{*/ | |
| 44 | ||
| 15884 | 45 | void purple_roomlist_show_with_account(PurpleAccount *account) |
| 8352 | 46 | { |
| 47 | if (ops && ops->show_with_account) | |
| 48 | ops->show_with_account(account); | |
| 49 | } | |
| 50 | ||
| 15884 | 51 | PurpleRoomlist *purple_roomlist_new(PurpleAccount *account) |
| 8113 | 52 | { |
| 15884 | 53 | PurpleRoomlist *list; |
| 8113 | 54 | |
| 55 | g_return_val_if_fail(account != NULL, NULL); | |
| 56 | ||
| 15884 | 57 | list = g_new0(PurpleRoomlist, 1); |
| 8113 | 58 | list->account = account; |
| 59 | list->rooms = NULL; | |
| 60 | list->fields = NULL; | |
| 61 | list->ref = 1; | |
| 62 | ||
| 10027 | 63 | if (ops && ops->create) |
| 64 | ops->create(list); | |
| 8113 | 65 | |
| 66 | return list; | |
| 67 | } | |
| 68 | ||
| 15884 | 69 | void purple_roomlist_ref(PurpleRoomlist *list) |
| 8113 | 70 | { |
| 71 | g_return_if_fail(list != NULL); | |
| 72 | ||
| 73 | list->ref++; | |
| 15884 | 74 | purple_debug_misc("roomlist", "reffing list, ref count now %d\n", list->ref); |
| 8113 | 75 | } |
| 76 | ||
| 15884 | 77 | static void purple_roomlist_room_destroy(PurpleRoomlist *list, PurpleRoomlistRoom *r) |
| 8113 | 78 | { |
| 79 | GList *l, *j; | |
| 80 | ||
| 81 | for (l = list->fields, j = r->fields; l && j; l = l->next, j = j->next) { | |
| 15884 | 82 | PurpleRoomlistField *f = l->data; |
| 83 | if (f->type == PURPLE_ROOMLIST_FIELD_STRING) | |
| 8113 | 84 | g_free(j->data); |
| 85 | } | |
| 86 | ||
| 87 | g_list_free(r->fields); | |
| 88 | g_free(r->name); | |
| 89 | g_free(r); | |
| 90 | } | |
| 91 | ||
| 15884 | 92 | static void purple_roomlist_field_destroy(PurpleRoomlistField *f) |
| 8113 | 93 | { |
| 94 | g_free(f->label); | |
| 95 | g_free(f->name); | |
| 96 | g_free(f); | |
| 97 | } | |
| 98 | ||
| 15884 | 99 | static void purple_roomlist_destroy(PurpleRoomlist *list) |
| 8113 | 100 | { |
| 101 | GList *l; | |
| 102 | ||
| 15884 | 103 | purple_debug_misc("roomlist", "destroying list %p\n", list); |
| 8113 | 104 | |
| 105 | if (ops && ops->destroy) | |
| 106 | ops->destroy(list); | |
| 107 | ||
|
14097
0c340861ab79
[gaim-migrate @ 16638]
Mark Doliner <markdoliner@pidgin.im>
parents:
12250
diff
changeset
|
108 | for (l = list->rooms; l; l = l->next) { |
| 15884 | 109 | PurpleRoomlistRoom *r = l->data; |
| 110 | purple_roomlist_room_destroy(list, r); | |
| 8113 | 111 | } |
|
14097
0c340861ab79
[gaim-migrate @ 16638]
Mark Doliner <markdoliner@pidgin.im>
parents:
12250
diff
changeset
|
112 | g_list_free(list->rooms); |
| 8113 | 113 | |
| 15884 | 114 | 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
|
115 | g_list_free(list->fields); |
| 8113 | 116 | |
| 117 | g_free(list); | |
| 118 | } | |
| 119 | ||
| 15884 | 120 | void purple_roomlist_unref(PurpleRoomlist *list) |
| 8113 | 121 | { |
| 122 | g_return_if_fail(list != NULL); | |
|
12250
5b14301dd1ec
[gaim-migrate @ 14552]
Richard Laager <rlaager@pidgin.im>
parents:
10027
diff
changeset
|
123 | g_return_if_fail(list->ref > 0); |
| 8113 | 124 | |
| 125 | list->ref--; | |
| 126 | ||
| 15884 | 127 | purple_debug_misc("roomlist", "unreffing list, ref count now %d\n", list->ref); |
| 8113 | 128 | if (list->ref == 0) |
| 15884 | 129 | purple_roomlist_destroy(list); |
| 8113 | 130 | } |
| 131 | ||
| 15884 | 132 | void purple_roomlist_set_fields(PurpleRoomlist *list, GList *fields) |
| 8113 | 133 | { |
| 134 | g_return_if_fail(list != NULL); | |
| 135 | ||
| 136 | list->fields = fields; | |
| 137 | ||
| 138 | if (ops && ops->set_fields) | |
| 139 | ops->set_fields(list, fields); | |
| 140 | } | |
| 141 | ||
| 15884 | 142 | void purple_roomlist_set_in_progress(PurpleRoomlist *list, gboolean in_progress) |
| 8113 | 143 | { |
| 144 | g_return_if_fail(list != NULL); | |
| 145 | ||
| 8199 | 146 | list->in_progress = in_progress; |
| 147 | ||
| 8113 | 148 | if (ops && ops->in_progress) |
| 149 | ops->in_progress(list, in_progress); | |
| 150 | } | |
| 151 | ||
| 15884 | 152 | gboolean purple_roomlist_get_in_progress(PurpleRoomlist *list) |
| 8199 | 153 | { |
| 154 | g_return_val_if_fail(list != NULL, FALSE); | |
| 155 | ||
| 156 | return list->in_progress; | |
| 157 | } | |
| 158 | ||
| 15884 | 159 | void purple_roomlist_room_add(PurpleRoomlist *list, PurpleRoomlistRoom *room) |
| 8113 | 160 | { |
| 161 | g_return_if_fail(list != NULL); | |
| 162 | g_return_if_fail(room != NULL); | |
| 163 | ||
| 164 | list->rooms = g_list_append(list->rooms, room); | |
| 165 | ||
| 166 | if (ops && ops->add_room) | |
| 167 | ops->add_room(list, room); | |
| 168 | } | |
| 169 | ||
| 15884 | 170 | PurpleRoomlist *purple_roomlist_get_list(PurpleConnection *gc) |
| 8113 | 171 | { |
| 15884 | 172 | PurplePluginProtocolInfo *prpl_info = NULL; |
| 8113 | 173 | |
| 174 | g_return_val_if_fail(gc != NULL, NULL); | |
| 175 | ||
| 176 | if (gc->prpl != NULL) | |
| 15884 | 177 | prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(gc->prpl); |
| 8113 | 178 | |
| 179 | if (prpl_info && prpl_info->roomlist_get_list) | |
| 180 | return prpl_info->roomlist_get_list(gc); | |
| 181 | return NULL; | |
| 182 | } | |
| 183 | ||
| 15884 | 184 | void purple_roomlist_cancel_get_list(PurpleRoomlist *list) |
| 8113 | 185 | { |
| 15884 | 186 | PurplePluginProtocolInfo *prpl_info = NULL; |
| 187 | PurpleConnection *gc; | |
| 8113 | 188 | |
| 189 | g_return_if_fail(list != NULL); | |
| 190 | ||
| 15884 | 191 | gc = purple_account_get_connection(list->account); |
| 8113 | 192 | |
| 193 | g_return_if_fail(gc != NULL); | |
| 194 | ||
| 195 | if (gc != NULL && gc->prpl != NULL) | |
| 15884 | 196 | prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(gc->prpl); |
| 8113 | 197 | |
| 198 | if (prpl_info && prpl_info->roomlist_cancel) | |
| 199 | prpl_info->roomlist_cancel(list); | |
| 200 | } | |
| 201 | ||
| 15884 | 202 | void purple_roomlist_expand_category(PurpleRoomlist *list, PurpleRoomlistRoom *category) |
| 8113 | 203 | { |
| 15884 | 204 | PurplePluginProtocolInfo *prpl_info = NULL; |
| 205 | PurpleConnection *gc; | |
| 8113 | 206 | |
| 207 | g_return_if_fail(list != NULL); | |
| 8584 | 208 | g_return_if_fail(category != NULL); |
| 15884 | 209 | g_return_if_fail(category->type & PURPLE_ROOMLIST_ROOMTYPE_CATEGORY); |
| 8113 | 210 | |
| 15884 | 211 | gc = purple_account_get_connection(list->account); |
| 8113 | 212 | g_return_if_fail(gc != NULL); |
| 213 | ||
| 214 | if (gc->prpl != NULL) | |
| 15884 | 215 | prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(gc->prpl); |
| 8113 | 216 | |
| 8584 | 217 | if (prpl_info && prpl_info->roomlist_expand_category) |
| 218 | prpl_info->roomlist_expand_category(list, category); | |
| 8113 | 219 | } |
| 220 | ||
| 221 | /*@}*/ | |
| 222 | ||
| 223 | /**************************************************************************/ | |
| 224 | /** @name Room API */ | |
| 225 | /**************************************************************************/ | |
| 226 | /*@{*/ | |
| 227 | ||
| 15884 | 228 | PurpleRoomlistRoom *purple_roomlist_room_new(PurpleRoomlistRoomType type, const gchar *name, |
| 229 | PurpleRoomlistRoom *parent) | |
| 8113 | 230 | { |
| 15884 | 231 | PurpleRoomlistRoom *room; |
| 8113 | 232 | |
| 233 | g_return_val_if_fail(name != NULL, NULL); | |
| 234 | ||
| 15884 | 235 | room = g_new0(PurpleRoomlistRoom, 1); |
| 8113 | 236 | room->type = type; |
| 237 | room->name = g_strdup(name); | |
| 238 | room->parent = parent; | |
| 239 | ||
| 240 | return room; | |
| 241 | } | |
| 242 | ||
| 15884 | 243 | void purple_roomlist_room_add_field(PurpleRoomlist *list, PurpleRoomlistRoom *room, gconstpointer field) |
| 8113 | 244 | { |
| 15884 | 245 | PurpleRoomlistField *f; |
| 8113 | 246 | |
| 247 | g_return_if_fail(list != NULL); | |
| 248 | g_return_if_fail(room != NULL); | |
| 249 | g_return_if_fail(list->fields != NULL); | |
| 250 | ||
| 251 | if (!room->fields) | |
| 252 | f = list->fields->data; | |
| 253 | else | |
| 254 | f = g_list_nth_data(list->fields, g_list_length(room->fields)); | |
| 255 | ||
| 256 | g_return_if_fail(f != NULL); | |
| 257 | ||
| 258 | switch(f->type) { | |
| 15884 | 259 | case PURPLE_ROOMLIST_FIELD_STRING: |
| 8113 | 260 | room->fields = g_list_append(room->fields, g_strdup(field)); |
| 261 | break; | |
| 15884 | 262 | case PURPLE_ROOMLIST_FIELD_BOOL: |
| 263 | case PURPLE_ROOMLIST_FIELD_INT: | |
| 8113 | 264 | room->fields = g_list_append(room->fields, GINT_TO_POINTER(field)); |
| 265 | break; | |
| 266 | } | |
| 267 | } | |
| 268 | ||
| 15884 | 269 | void purple_roomlist_room_join(PurpleRoomlist *list, PurpleRoomlistRoom *room) |
| 8199 | 270 | { |
| 271 | GHashTable *components; | |
| 272 | GList *l, *j; | |
| 15884 | 273 | PurpleConnection *gc; |
| 8199 | 274 | |
| 275 | g_return_if_fail(list != NULL); | |
| 276 | g_return_if_fail(room != NULL); | |
| 277 | ||
| 15884 | 278 | gc = purple_account_get_connection(list->account); |
| 8199 | 279 | if (!gc) |
| 280 | return; | |
| 281 | ||
| 282 | components = g_hash_table_new(g_str_hash, g_str_equal); | |
| 283 | ||
| 284 | g_hash_table_replace(components, "name", room->name); | |
| 285 | for (l = list->fields, j = room->fields; l && j; l = l->next, j = j->next) { | |
| 15884 | 286 | PurpleRoomlistField *f = l->data; |
| 8199 | 287 | |
| 288 | g_hash_table_replace(components, f->name, j->data); | |
| 289 | } | |
| 290 | ||
| 291 | serv_join_chat(gc, components); | |
| 292 | ||
| 293 | g_hash_table_destroy(components); | |
| 294 | } | |
| 295 | ||
| 8113 | 296 | /*@}*/ |
| 297 | ||
| 298 | /**************************************************************************/ | |
| 299 | /** @name Room Field API */ | |
| 300 | /**************************************************************************/ | |
| 301 | /*@{*/ | |
| 302 | ||
| 15884 | 303 | PurpleRoomlistField *purple_roomlist_field_new(PurpleRoomlistFieldType type, |
| 8113 | 304 | const gchar *label, const gchar *name, |
| 305 | gboolean hidden) | |
| 306 | { | |
| 15884 | 307 | PurpleRoomlistField *f; |
| 8113 | 308 | |
| 309 | g_return_val_if_fail(label != NULL, NULL); | |
| 310 | g_return_val_if_fail(name != NULL, NULL); | |
| 311 | ||
| 15884 | 312 | f = g_new0(PurpleRoomlistField, 1); |
| 8113 | 313 | |
| 314 | f->type = type; | |
| 315 | f->label = g_strdup(label); | |
| 316 | f->name = g_strdup(name); | |
| 317 | f->hidden = hidden; | |
| 318 | ||
| 319 | return f; | |
| 320 | } | |
| 321 | ||
| 322 | /*@}*/ | |
| 323 | ||
| 324 | /**************************************************************************/ | |
| 325 | /** @name UI Registration Functions */ | |
| 326 | /**************************************************************************/ | |
| 327 | /*@{*/ | |
| 328 | ||
| 329 | ||
| 15884 | 330 | void purple_roomlist_set_ui_ops(PurpleRoomlistUiOps *ui_ops) |
| 8113 | 331 | { |
| 332 | ops = ui_ops; | |
| 333 | } | |
| 334 | ||
| 15884 | 335 | PurpleRoomlistUiOps *purple_roomlist_get_ui_ops(void) |
| 8113 | 336 | { |
| 337 | return ops; | |
| 338 | } | |
| 339 | ||
| 340 | /*@}*/ |