Mercurial > pidgin
annotate src/status.c @ 10010:56e34a659db2
[gaim-migrate @ 10927]
Further deprecation of serv_got_update
committer: Tailor Script <tailor@pidgin.im>
| author | Mark Doliner <mark@kingant.net> |
|---|---|
| date | Fri, 10 Sep 2004 22:39:35 +0000 |
| parents | c124bef68132 |
| children | a38881ec9c0f |
| rev | line source |
|---|---|
| 9944 | 1 /** |
| 2 * @file status.h Status API | |
| 3 * @ingroup core | |
| 4 * | |
| 6065 | 5 * gaim |
| 6 * | |
| 8046 | 7 * Gaim is the legal property of its developers, whose names are too numerous |
| 8 * to list here. Please refer to the COPYRIGHT file distributed with this | |
| 9 * source distribution. | |
| 9944 | 10 * |
| 6065 | 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 */ | |
| 9949 | 25 #include "internal.h" |
| 6065 | 26 |
| 9949 | 27 #include "blist.h" |
| 28 #include "debug.h" | |
| 29 #include "prefs.h" | |
| 6065 | 30 #include "status.h" |
| 31 #include "util.h" | |
| 32 | |
| 9949 | 33 /** |
| 34 * A type of status. | |
| 35 */ | |
| 36 struct _GaimStatusType | |
| 37 { | |
| 38 GaimStatusPrimitive primitive; | |
|
6371
8f94cce8faa5
[gaim-migrate @ 6876]
Christian Hammond <chipx86@chipx86.com>
parents:
6321
diff
changeset
|
39 |
| 9949 | 40 char *id; |
| 41 char *name; | |
| 42 char *primary_attr_id; | |
| 43 | |
| 44 gboolean saveable; | |
| 45 gboolean user_settable; | |
| 46 gboolean independent; | |
| 47 | |
| 48 GList *attrs; | |
| 49 }; | |
|
6371
8f94cce8faa5
[gaim-migrate @ 6876]
Christian Hammond <chipx86@chipx86.com>
parents:
6321
diff
changeset
|
50 |
| 9949 | 51 /** |
| 52 * A status attribute. | |
| 53 */ | |
| 54 struct _GaimStatusAttr | |
| 55 { | |
| 56 char *id; | |
| 57 char *name; | |
| 58 GaimValue *value_type; | |
| 59 }; | |
| 6065 | 60 |
| 9949 | 61 /** |
| 62 * A list of statuses. | |
| 63 */ | |
| 64 struct _GaimPresence | |
| 65 { | |
| 66 GaimPresenceContext context; | |
| 67 | |
| 68 gboolean idle; | |
| 69 time_t idle_time; | |
| 10006 | 70 time_t login_time; |
| 9949 | 71 |
| 72 unsigned int warning_level; | |
| 6065 | 73 |
| 9949 | 74 GList *statuses; |
| 75 GHashTable *status_table; | |
| 76 | |
| 77 GaimStatus *active_status; | |
| 6065 | 78 |
| 9949 | 79 union |
| 80 { | |
| 81 GaimAccount *account; | |
| 82 | |
| 83 struct | |
| 84 { | |
| 85 GaimConversation *conv; | |
| 86 char *user; | |
| 87 | |
| 88 } chat; | |
| 6065 | 89 |
| 9949 | 90 struct |
| 91 { | |
| 92 GaimAccount *account; | |
| 93 char *name; | |
| 94 size_t ref_count; | |
| 95 GList *buddies; | |
| 96 | |
| 97 } buddy; | |
| 98 | |
| 99 } u; | |
| 100 }; | |
| 101 | |
| 102 /** | |
| 103 * An active status. | |
| 104 */ | |
| 105 struct _GaimStatus | |
| 6065 | 106 { |
| 9949 | 107 GaimStatusType *type; |
| 108 GaimPresence *presence; | |
| 109 | |
| 110 const char *title; | |
| 6065 | 111 |
| 9949 | 112 gboolean active; |
| 6065 | 113 |
| 9949 | 114 GHashTable *attr_values; |
| 115 }; | |
| 6065 | 116 |
| 117 typedef struct | |
| 118 { | |
| 9949 | 119 GaimAccount *account; |
| 120 char *name; | |
| 10006 | 121 guint id; |
| 9949 | 122 |
| 123 } GaimStatusBuddyKey; | |
| 124 | |
| 125 | |
| 126 #if 0 | |
| 127 static GList *stored_statuses = NULL; | |
| 128 | |
| 129 /* | |
| 130 * XXX This stuff should be removed in a few versions. It stores the | |
| 131 * old v1 status stuff so we can write it later. We don't write out | |
| 132 * the new status stuff, though. These should all die soon, as the | |
| 133 * old status.xml was created before the new status system's design | |
| 134 * was created. | |
| 135 * | |
| 136 * -- ChipX86 | |
| 137 */ | |
| 138 typedef struct | |
| 139 { | |
| 140 char *name; | |
| 141 char *state; | |
| 142 char *message; | |
| 143 | |
| 144 } GaimStatusV1Info; | |
| 145 | |
| 146 static GList *v1_statuses = NULL; | |
| 147 #endif | |
| 148 | |
| 149 | |
| 150 static int primitive_scores[] = | |
| 151 { | |
| 152 0, /* unset */ | |
| 153 -500, /* offline */ | |
| 154 0, /* online */ | |
| 155 100, /* available */ | |
| 156 -75, /* unavailable */ | |
| 157 -50, /* hidden */ | |
| 158 -100, /* away */ | |
| 159 -200 /* extended away */ | |
| 160 -10, /* idle, special case. */ | |
| 161 -5 /* idle time, special case. */ | |
| 162 }; | |
| 163 | |
| 164 static GHashTable *buddy_presences = NULL; | |
| 165 | |
| 166 #define SCORE_IDLE 5 | |
| 167 #define SCORE_IDLE_TIME 6 | |
| 168 | |
| 169 /************************************************************************** | |
| 170 * GaimStatusType API | |
| 171 **************************************************************************/ | |
| 172 GaimStatusType * | |
| 173 gaim_status_type_new_full(GaimStatusPrimitive primitive, const char *id, | |
| 10009 | 174 const char *name, gboolean saveable, |
| 175 gboolean user_settable, gboolean independent) | |
| 9949 | 176 { |
| 177 GaimStatusType *status_type; | |
| 178 | |
| 179 g_return_val_if_fail(primitive != GAIM_STATUS_UNSET, NULL); | |
| 180 g_return_val_if_fail(id != NULL, NULL); | |
| 181 g_return_val_if_fail(name != NULL, NULL); | |
| 182 | |
| 183 status_type = g_new0(GaimStatusType, 1); | |
| 184 | |
| 185 status_type->primitive = primitive; | |
| 186 status_type->id = g_strdup(id); | |
| 187 status_type->name = g_strdup(name); | |
| 188 status_type->saveable = saveable; | |
| 189 status_type->user_settable = user_settable; | |
| 190 status_type->independent = independent; | |
| 191 | |
| 192 return status_type; | |
| 193 } | |
| 194 | |
| 195 GaimStatusType * | |
| 196 gaim_status_type_new(GaimStatusPrimitive primitive, const char *id, | |
| 10009 | 197 const char *name, gboolean user_settable) |
| 9949 | 198 { |
| 199 g_return_val_if_fail(primitive != GAIM_STATUS_UNSET, NULL); | |
| 200 g_return_val_if_fail(id != NULL, NULL); | |
| 201 g_return_val_if_fail(name != NULL, NULL); | |
| 202 | |
| 203 return gaim_status_type_new_full(primitive, id, name, FALSE, | |
| 204 user_settable, FALSE); | |
| 205 } | |
| 206 | |
| 207 GaimStatusType * | |
| 208 gaim_status_type_new_with_attrs(GaimStatusPrimitive primitive, | |
| 209 const char *id, const char *name, | |
| 210 gboolean saveable, gboolean user_settable, | |
| 211 gboolean independent, const char *attr_id, | |
| 212 const char *attr_name, GaimValue *attr_value, | |
| 213 ...) | |
| 214 { | |
| 215 GaimStatusType *status_type; | |
| 216 va_list args; | |
| 217 | |
| 218 g_return_val_if_fail(primitive != GAIM_STATUS_UNSET, NULL); | |
| 219 g_return_val_if_fail(id != NULL, NULL); | |
| 220 g_return_val_if_fail(name != NULL, NULL); | |
| 10006 | 221 g_return_val_if_fail(attr_id != NULL, NULL); |
| 9949 | 222 g_return_val_if_fail(attr_name != NULL, NULL); |
| 223 g_return_val_if_fail(attr_value != NULL, NULL); | |
| 224 | |
| 225 status_type = gaim_status_type_new_full(primitive, id, name, saveable, | |
| 226 user_settable, independent); | |
| 227 | |
| 10010 | 228 /* Add the first attribute */ |
| 9949 | 229 gaim_status_type_add_attr(status_type, attr_id, attr_name, attr_value); |
| 230 | |
| 231 va_start(args, attr_value); | |
| 232 gaim_status_type_add_attrs_vargs(status_type, args); | |
| 233 va_end(args); | |
| 234 | |
| 235 return status_type; | |
| 236 } | |
| 237 | |
| 238 void | |
| 239 gaim_status_type_destroy(GaimStatusType *status_type) | |
| 240 { | |
| 241 GList *l; | |
| 242 | |
| 243 g_return_if_fail(status_type != NULL); | |
| 244 | |
| 245 g_free(status_type->id); | |
| 246 g_free(status_type->name); | |
| 247 | |
| 248 if (status_type->primary_attr_id != NULL) | |
| 249 g_free(status_type->primary_attr_id); | |
| 250 | |
| 251 if (status_type->attrs != NULL) | |
| 252 { | |
| 253 for (l = status_type->attrs; l != NULL; l = l->next) | |
| 254 gaim_status_attr_destroy((GaimStatusAttr *)l->data); | |
| 255 | |
| 256 g_list_free(status_type->attrs); | |
| 257 } | |
| 258 | |
| 259 g_free(status_type); | |
| 260 } | |
| 261 | |
| 262 void | |
| 263 gaim_status_type_set_primary_attr(GaimStatusType *status_type, const char *id) | |
| 264 { | |
| 265 g_return_if_fail(status_type != NULL); | |
| 266 | |
| 267 if (status_type->primary_attr_id != NULL) | |
| 268 g_free(status_type->primary_attr_id); | |
| 269 | |
| 270 status_type->primary_attr_id = (id == NULL ? NULL : g_strdup(id)); | |
| 271 } | |
| 272 | |
| 273 void | |
| 274 gaim_status_type_add_attr(GaimStatusType *status_type, const char *id,\ | |
| 275 const char *name, GaimValue *value) | |
| 276 { | |
| 277 GaimStatusAttr *attr; | |
| 278 | |
| 279 g_return_if_fail(status_type != NULL); | |
| 280 g_return_if_fail(id != NULL); | |
| 281 g_return_if_fail(name != NULL); | |
| 282 g_return_if_fail(value != NULL); | |
| 283 | |
| 284 attr = gaim_status_attr_new(id, name, value); | |
| 285 | |
| 286 status_type->attrs = g_list_append(status_type->attrs, attr); | |
| 287 } | |
| 288 | |
| 289 void | |
| 290 gaim_status_type_add_attrs_vargs(GaimStatusType *status_type, va_list args) | |
| 291 { | |
| 292 const char *id, *name; | |
| 293 GaimValue *value; | |
| 294 | |
| 295 g_return_if_fail(status_type != NULL); | |
| 296 | |
| 297 while ((id = va_arg(args, const char *)) != NULL) | |
| 298 { | |
| 299 name = va_arg(args, const char *); | |
| 300 g_return_if_fail(name != NULL); | |
| 301 | |
| 302 value = va_arg(args, GaimValue *); | |
| 303 g_return_if_fail(value != NULL); | |
| 6065 | 304 |
| 9949 | 305 gaim_status_type_add_attr(status_type, id, name, value); |
| 306 } | |
| 307 } | |
| 308 | |
| 10010 | 309 void |
| 310 gaim_status_type_add_attrs(GaimStatusType *status_type, const char *id, | |
| 311 const char *name, GaimValue *value, ...) | |
| 312 { | |
| 313 va_list args; | |
| 314 | |
| 315 g_return_if_fail(status_type != NULL); | |
| 316 g_return_if_fail(id != NULL); | |
| 317 g_return_if_fail(name != NULL); | |
| 318 g_return_if_fail(value != NULL); | |
| 319 | |
| 320 /* Add the first attribute */ | |
| 321 gaim_status_type_add_attr(status_type, id, name, value); | |
| 322 | |
| 323 va_start(args, value); | |
| 324 gaim_status_type_add_attrs_vargs(status_type, args); | |
| 325 va_end(args); | |
| 326 } | |
| 327 | |
| 9949 | 328 GaimStatusPrimitive |
| 329 gaim_status_type_get_primitive(const GaimStatusType *status_type) | |
| 330 { | |
| 331 g_return_val_if_fail(status_type != NULL, GAIM_STATUS_UNSET); | |
| 332 | |
| 333 return status_type->primitive; | |
| 334 } | |
| 335 | |
| 336 const char * | |
| 337 gaim_status_type_get_id(const GaimStatusType *status_type) | |
| 338 { | |
| 339 g_return_val_if_fail(status_type != NULL, NULL); | |
| 340 | |
| 341 return status_type->id; | |
| 342 } | |
| 343 | |
| 344 const char * | |
| 345 gaim_status_type_get_name(const GaimStatusType *status_type) | |
| 346 { | |
| 347 g_return_val_if_fail(status_type != NULL, NULL); | |
| 348 | |
| 349 return status_type->name; | |
| 350 } | |
| 351 | |
| 352 gboolean | |
| 353 gaim_status_type_is_saveable(const GaimStatusType *status_type) | |
| 354 { | |
| 355 g_return_val_if_fail(status_type != NULL, FALSE); | |
| 356 | |
| 357 return status_type->saveable; | |
| 358 } | |
| 359 | |
| 360 gboolean | |
| 361 gaim_status_type_is_user_settable(const GaimStatusType *status_type) | |
| 362 { | |
| 363 g_return_val_if_fail(status_type != NULL, FALSE); | |
| 364 | |
| 365 return status_type->user_settable; | |
| 366 } | |
| 367 | |
| 368 gboolean | |
| 369 gaim_status_type_is_independent(const GaimStatusType *status_type) | |
| 370 { | |
| 371 g_return_val_if_fail(status_type != NULL, FALSE); | |
| 372 | |
| 373 return status_type->independent; | |
| 374 } | |
| 375 | |
| 376 gboolean | |
| 377 gaim_status_type_is_available(const GaimStatusType *status_type) | |
| 378 { | |
| 379 GaimStatusPrimitive primitive; | |
| 380 | |
| 381 g_return_val_if_fail(status_type != NULL, FALSE); | |
| 382 | |
| 383 primitive = gaim_status_type_get_primitive(status_type); | |
| 384 | |
| 385 return (primitive == GAIM_STATUS_AVAILABLE || | |
| 386 primitive == GAIM_STATUS_HIDDEN); | |
| 387 } | |
| 388 | |
| 389 const char * | |
| 390 gaim_status_type_get_primary_attr(const GaimStatusType *status_type) | |
| 391 { | |
| 392 g_return_val_if_fail(status_type != NULL, NULL); | |
| 393 | |
| 394 return status_type->primary_attr_id; | |
| 395 } | |
| 396 | |
| 397 GaimStatusAttr * | |
| 398 gaim_status_type_get_attr(const GaimStatusType *status_type, const char *id) | |
| 399 { | |
| 400 GList *l; | |
| 401 | |
| 402 g_return_val_if_fail(status_type != NULL, NULL); | |
| 403 g_return_val_if_fail(id != NULL, NULL); | |
| 404 | |
| 405 for (l = status_type->attrs; l != NULL; l = l->next) | |
| 406 { | |
| 407 GaimStatusAttr *attr = (GaimStatusAttr *)l->data; | |
| 408 | |
| 409 if (!strcmp(gaim_status_attr_get_id(attr), id)) | |
| 410 return attr; | |
| 411 } | |
| 412 | |
| 413 return NULL; | |
| 414 } | |
| 415 | |
| 416 const GList * | |
| 417 gaim_status_type_get_attrs(const GaimStatusType *status_type) | |
| 418 { | |
| 419 g_return_val_if_fail(status_type != NULL, NULL); | |
| 420 | |
| 421 return status_type->attrs; | |
| 422 } | |
| 423 | |
| 424 | |
| 425 /************************************************************************** | |
| 426 * GaimStatusAttr API | |
| 427 **************************************************************************/ | |
| 428 GaimStatusAttr * | |
| 429 gaim_status_attr_new(const char *id, const char *name, GaimValue *value_type) | |
| 430 { | |
| 431 GaimStatusAttr *attr; | |
| 432 | |
| 433 g_return_val_if_fail(id != NULL, NULL); | |
| 434 g_return_val_if_fail(name != NULL, NULL); | |
| 435 g_return_val_if_fail(value_type != NULL, NULL); | |
| 436 | |
| 437 attr = g_new0(GaimStatusAttr, 1); | |
| 438 | |
| 439 attr->id = g_strdup(id); | |
| 440 attr->name = g_strdup(name); | |
| 441 attr->value_type = value_type; | |
| 442 | |
| 443 return attr; | |
| 444 } | |
| 445 | |
| 446 void | |
| 447 gaim_status_attr_destroy(GaimStatusAttr *attr) | |
| 448 { | |
| 449 g_return_if_fail(attr != NULL); | |
| 450 | |
| 451 g_free(attr->id); | |
| 452 g_free(attr->name); | |
| 453 | |
| 454 gaim_value_destroy(attr->value_type); | |
| 455 | |
| 456 g_free(attr); | |
| 457 } | |
| 458 | |
| 459 const char * | |
| 460 gaim_status_attr_get_id(const GaimStatusAttr *attr) | |
| 461 { | |
| 462 g_return_val_if_fail(attr != NULL, NULL); | |
| 463 | |
| 464 return attr->id; | |
| 465 } | |
| 466 | |
| 467 const char * | |
| 468 gaim_status_attr_get_name(const GaimStatusAttr *attr) | |
| 469 { | |
| 470 g_return_val_if_fail(attr != NULL, NULL); | |
| 471 | |
| 472 return attr->name; | |
| 473 } | |
| 474 | |
| 475 GaimValue * | |
| 476 gaim_status_attr_get_value_type(const GaimStatusAttr *attr) | |
| 477 { | |
| 478 g_return_val_if_fail(attr != NULL, NULL); | |
| 479 | |
| 480 return attr->value_type; | |
| 481 } | |
| 482 | |
| 483 | |
| 484 /************************************************************************** | |
| 485 * GaimStatus API | |
| 486 **************************************************************************/ | |
| 487 GaimStatus * | |
| 488 gaim_status_new(GaimStatusType *status_type, GaimPresence *presence) | |
| 489 { | |
| 490 GaimStatus *status; | |
| 491 const GList *l; | |
| 492 | |
| 493 g_return_val_if_fail(status_type != NULL, NULL); | |
| 494 g_return_val_if_fail(presence != NULL, NULL); | |
| 495 | |
| 496 status = g_new0(GaimStatus, 1); | |
| 497 | |
| 498 status->type = status_type; | |
| 499 status->presence = presence; | |
| 500 | |
| 501 status->attr_values = | |
| 502 g_hash_table_new_full(g_str_hash, g_str_equal, g_free, | |
| 503 (GDestroyNotify)gaim_value_destroy); | |
| 504 | |
| 505 for (l = gaim_status_type_get_attrs(status_type); l != NULL; l = l->next) | |
| 506 { | |
| 507 GaimStatusAttr *attr = (GaimStatusAttr *)l->data; | |
| 508 GaimValue *value = gaim_status_attr_get_value_type(attr); | |
| 509 GaimValue *new_value = gaim_value_dup(value); | |
| 510 | |
| 511 g_hash_table_insert(status->attr_values, | |
| 512 g_strdup(gaim_status_attr_get_id(attr)), new_value); | |
| 513 } | |
| 514 | |
| 515 return status; | |
| 516 } | |
| 517 | |
| 518 void | |
| 519 gaim_status_destroy(GaimStatus *status) | |
| 520 { | |
| 521 g_return_if_fail(status != NULL); | |
| 522 | |
| 523 gaim_status_set_active(status, FALSE); | |
| 524 | |
| 525 g_hash_table_destroy(status->attr_values); | |
| 526 | |
| 527 g_free(status); | |
| 528 } | |
| 6065 | 529 |
| 530 static void | |
| 9949 | 531 notify_buddy_status_update(GaimBuddy *buddy, GaimPresence *presence, |
| 532 GaimStatus *old_status, GaimStatus *new_status) | |
| 533 { | |
| 534 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
| 535 | |
| 536 if (gaim_prefs_get_bool("/core/logging/log_system") && | |
| 537 gaim_prefs_get_bool("/core/logging/log_away_state")) | |
| 538 { | |
| 539 time_t current_time = time(NULL); | |
| 540 const char *buddy_alias = gaim_buddy_get_alias(buddy); | |
| 541 char *tmp = NULL; | |
| 542 | |
| 543 if (!gaim_status_is_available(old_status) && | |
| 544 gaim_status_is_available(new_status)) | |
| 545 { | |
| 546 tmp = g_strdup_printf(_("%s came back"), buddy_alias); | |
| 547 } | |
| 548 else if (gaim_status_is_available(old_status) && | |
| 549 !gaim_status_is_available(new_status)) | |
| 550 { | |
| 551 tmp = g_strdup_printf(_("%s went away"), buddy_alias); | |
| 552 } | |
| 553 | |
| 554 if (tmp != NULL) | |
| 555 { | |
| 556 GaimLog *log = gaim_account_get_log(buddy->account); | |
| 557 | |
| 558 gaim_log_write(log, GAIM_MESSAGE_SYSTEM, buddy_alias, | |
| 559 current_time, tmp); | |
| 560 g_free(tmp); | |
| 561 } | |
| 562 } | |
| 563 | |
| 564 if (ops != NULL && ops->status_changed != NULL) | |
| 565 ops->status_changed(buddy, new_status); | |
| 566 } | |
| 567 | |
| 568 static void | |
| 569 notify_status_update(GaimPresence *presence, GaimStatus *old_status, | |
| 570 GaimStatus *new_status) | |
| 6065 | 571 { |
| 9949 | 572 GaimPresenceContext context = gaim_presence_get_context(presence); |
| 573 | |
| 10006 | 574 gaim_debug_info("notify_status_update", "Context is %d\n", context); |
| 575 | |
| 9949 | 576 if (context == GAIM_PRESENCE_CONTEXT_ACCOUNT) |
| 577 { | |
| 578 GaimAccountUiOps *ops = gaim_accounts_get_ui_ops(); | |
| 579 | |
| 580 if (ops != NULL && ops->status_changed != NULL) | |
| 581 { | |
| 582 ops->status_changed(gaim_presence_get_account(presence), | |
| 583 new_status); | |
| 584 } | |
| 585 } | |
| 586 else if (context == GAIM_PRESENCE_CONTEXT_CONV) | |
| 587 { | |
| 588 /* TODO */ | |
| 589 #if 0 | |
| 590 GaimConversationUiOps *ops; | |
| 591 GaimConversation *conv; | |
| 592 | |
| 593 conv = gaim_status_get_conversation(new_status); | |
| 594 #endif | |
| 595 } | |
| 596 else if (context == GAIM_PRESENCE_CONTEXT_BUDDY) | |
| 597 { | |
| 598 const GList *l; | |
| 599 | |
| 600 for (l = gaim_presence_get_buddies(presence); l != NULL; l = l->next) | |
| 601 { | |
| 602 notify_buddy_status_update((GaimBuddy *)l->data, presence, | |
| 603 old_status, new_status); | |
| 604 } | |
| 605 } | |
| 606 } | |
| 607 | |
| 608 void | |
| 609 gaim_status_set_active(GaimStatus *status, gboolean active) | |
| 610 { | |
| 611 GaimStatusType *status_type; | |
| 612 GaimPresence *presence; | |
| 613 GaimStatus *old_status; | |
| 614 | |
| 615 g_return_if_fail(status != NULL); | |
| 616 | |
| 617 if (status->active == active) | |
| 618 return; | |
| 619 | |
| 620 status_type = gaim_status_get_type(status); | |
| 6065 | 621 |
| 10006 | 622 if (!active && !gaim_status_type_is_independent(status_type)) |
| 9949 | 623 { |
| 624 gaim_debug(GAIM_DEBUG_ERROR, "status", | |
| 10006 | 625 "Cannot deactivate an exclusive status (%s).\n", |
| 626 gaim_status_type_get_id(status_type)); | |
| 9949 | 627 return; |
| 628 } | |
| 629 | |
| 630 presence = gaim_status_get_presence(status); | |
| 631 old_status = gaim_presence_get_active_status(presence); | |
| 632 | |
| 633 if (!gaim_status_type_is_independent(status_type)) | |
| 634 { | |
| 635 const GList *l; | |
| 636 | |
| 10006 | 637 for (l = gaim_presence_get_statuses(presence); l != NULL; l = l->next) |
| 9949 | 638 { |
| 639 GaimStatus *temp_status = (GaimStatus *)l->data; | |
| 640 GaimStatusType *temp_type; | |
| 641 | |
| 10006 | 642 if (!gaim_status_compare(temp_status, status)) |
| 9949 | 643 continue; |
| 644 | |
| 645 temp_type = gaim_status_get_type(temp_status); | |
| 646 | |
| 647 if (gaim_status_type_is_independent(temp_type)) | |
| 648 continue; | |
| 649 | |
| 650 if (gaim_status_is_active(temp_status)) | |
| 651 { | |
| 652 /* | |
| 653 * Since we don't want an infinite loop, we have to set | |
| 654 * the active variable ourself. | |
| 655 */ | |
| 656 temp_status->active = FALSE; | |
| 657 | |
| 658 notify_status_update(presence, old_status, temp_status); | |
| 659 | |
| 660 break; | |
| 661 } | |
| 662 } | |
| 663 } | |
| 664 | |
| 665 status->active = active; | |
| 6065 | 666 |
| 9949 | 667 notify_status_update(presence, old_status, status); |
| 668 } | |
| 669 | |
| 670 void | |
| 671 gaim_status_set_attr_boolean(GaimStatus *status, const char *id, | |
| 672 gboolean value) | |
| 673 { | |
| 674 GaimStatusType *status_type; | |
| 675 GaimStatusAttr *attr; | |
| 676 GaimValue *attr_value; | |
| 677 | |
| 678 g_return_if_fail(status != NULL); | |
| 679 g_return_if_fail(id != NULL); | |
| 680 | |
| 681 status_type = gaim_status_get_type(status); | |
| 682 | |
| 683 /* Make sure this attribute exists. */ | |
| 684 attr = gaim_status_type_get_attr(status_type, id); | |
| 685 g_return_if_fail(attr != NULL); | |
| 686 | |
| 687 attr_value = gaim_status_attr_get_value_type(attr); | |
| 688 g_return_if_fail(gaim_value_get_type(attr_value) == GAIM_TYPE_BOOLEAN); | |
| 689 | |
| 690 gaim_value_set_boolean(attr_value, value); | |
| 691 } | |
| 692 | |
| 693 void | |
| 694 gaim_status_set_attr_int(GaimStatus *status, const char *id, int value) | |
| 695 { | |
| 696 GaimStatusType *status_type; | |
| 697 GaimStatusAttr *attr; | |
| 698 GaimValue *attr_value; | |
| 699 | |
| 700 g_return_if_fail(status != NULL); | |
| 701 g_return_if_fail(id != NULL); | |
| 702 | |
| 703 status_type = gaim_status_get_type(status); | |
| 704 | |
| 705 /* Make sure this attribute exists. */ | |
| 706 attr = gaim_status_type_get_attr(status_type, id); | |
| 707 g_return_if_fail(attr != NULL); | |
| 708 | |
| 709 attr_value = gaim_status_attr_get_value_type(attr); | |
| 710 g_return_if_fail(gaim_value_get_type(attr_value) == GAIM_TYPE_INT); | |
| 711 | |
| 712 gaim_value_set_int(attr_value, value); | |
| 6065 | 713 } |
| 714 | |
| 9949 | 715 void |
| 716 gaim_status_set_attr_string(GaimStatus *status, const char *id, | |
| 717 const char *value) | |
| 718 { | |
| 719 GaimStatusType *status_type; | |
| 720 GaimStatusAttr *attr; | |
| 721 GaimValue *attr_value; | |
| 722 | |
| 723 g_return_if_fail(status != NULL); | |
| 724 g_return_if_fail(id != NULL); | |
| 725 | |
| 726 status_type = gaim_status_get_type(status); | |
| 727 | |
| 728 /* Make sure this attribute exists. */ | |
| 729 attr = gaim_status_type_get_attr(status_type, id); | |
| 730 g_return_if_fail(attr != NULL); | |
| 731 | |
| 732 attr_value = gaim_status_attr_get_value_type(attr); | |
| 733 g_return_if_fail(gaim_value_get_type(attr_value) == GAIM_TYPE_STRING); | |
| 734 | |
| 735 gaim_value_set_string(attr_value, value); | |
| 736 } | |
| 737 | |
| 738 GaimStatusType * | |
| 739 gaim_status_get_type(const GaimStatus *status) | |
| 740 { | |
| 741 g_return_val_if_fail(status != NULL, NULL); | |
| 742 | |
| 743 return status->type; | |
| 744 } | |
| 745 | |
| 746 GaimPresence * | |
| 747 gaim_status_get_presence(const GaimStatus *status) | |
| 6065 | 748 { |
| 9949 | 749 g_return_val_if_fail(status != NULL, NULL); |
| 750 | |
| 751 return status->presence; | |
| 752 } | |
| 753 | |
| 754 const char * | |
| 755 gaim_status_get_id(const GaimStatus *status) | |
| 756 { | |
| 757 g_return_val_if_fail(status != NULL, NULL); | |
| 758 | |
| 759 return gaim_status_type_get_id(gaim_status_get_type(status)); | |
| 760 } | |
| 761 | |
| 762 const char * | |
| 763 gaim_status_get_name(const GaimStatus *status) | |
| 764 { | |
| 765 g_return_val_if_fail(status != NULL, NULL); | |
| 766 | |
| 767 return gaim_status_type_get_name(gaim_status_get_type(status)); | |
| 768 } | |
| 769 | |
| 770 gboolean | |
| 771 gaim_status_is_independent(const GaimStatus *status) | |
| 772 { | |
| 773 g_return_val_if_fail(status != NULL, FALSE); | |
| 774 | |
| 775 return gaim_status_type_is_independent(gaim_status_get_type(status)); | |
| 776 } | |
| 777 | |
| 778 gboolean | |
| 779 gaim_status_is_available(const GaimStatus *status) | |
| 780 { | |
| 781 g_return_val_if_fail(status != NULL, FALSE); | |
| 782 | |
| 783 return gaim_status_type_is_available(gaim_status_get_type(status)); | |
| 784 } | |
| 6216 | 785 |
| 9949 | 786 gboolean |
| 787 gaim_status_is_active(const GaimStatus *status) | |
| 788 { | |
| 789 g_return_val_if_fail(status != NULL, FALSE); | |
| 790 | |
| 791 return status->active; | |
| 792 } | |
| 793 | |
| 794 GaimValue * | |
| 795 gaim_status_get_attr_value(const GaimStatus *status, const char *id) | |
| 796 { | |
| 797 GaimStatusType *status_type; | |
| 798 GaimStatusAttr *attr; | |
| 799 | |
| 800 g_return_val_if_fail(status != NULL, NULL); | |
| 801 g_return_val_if_fail(id != NULL, NULL); | |
| 802 | |
| 803 status_type = gaim_status_get_type(status); | |
| 804 | |
| 805 /* Make sure this attribute exists. */ | |
| 806 attr = gaim_status_type_get_attr(status_type, id); | |
| 807 g_return_val_if_fail(attr != NULL, NULL); | |
| 808 | |
| 809 return (GaimValue *)g_hash_table_lookup(status->attr_values, id); | |
| 810 } | |
| 811 | |
| 812 gboolean | |
| 813 gaim_status_get_attr_boolean(const GaimStatus *status, const char *id) | |
| 814 { | |
| 815 const GaimValue *value; | |
| 816 | |
| 817 g_return_val_if_fail(status != NULL, FALSE); | |
| 818 g_return_val_if_fail(id != NULL, FALSE); | |
| 819 | |
| 820 if ((value = gaim_status_get_attr_value(status, id)) == NULL) | |
| 821 return FALSE; | |
| 822 | |
| 823 g_return_val_if_fail(gaim_value_get_type(value) == GAIM_TYPE_STRING, FALSE); | |
| 824 | |
| 825 return gaim_value_get_boolean(value); | |
| 826 } | |
| 827 | |
| 828 int | |
| 829 gaim_status_get_attr_int(const GaimStatus *status, const char *id) | |
| 830 { | |
| 831 const GaimValue *value; | |
| 832 | |
| 833 g_return_val_if_fail(status != NULL, FALSE); | |
| 834 g_return_val_if_fail(id != NULL, FALSE); | |
| 835 | |
| 836 if ((value = gaim_status_get_attr_value(status, id)) == NULL) | |
| 837 return FALSE; | |
| 838 | |
| 839 g_return_val_if_fail(gaim_value_get_type(value) == GAIM_TYPE_INT, 0); | |
| 840 | |
| 841 return gaim_value_get_int(value); | |
| 842 } | |
| 843 | |
| 844 const char * | |
| 845 gaim_status_get_attr_string(const GaimStatus *status, const char *id) | |
| 846 { | |
| 847 const GaimValue *value; | |
| 848 | |
| 849 g_return_val_if_fail(status != NULL, FALSE); | |
| 850 g_return_val_if_fail(id != NULL, FALSE); | |
| 851 | |
| 852 if ((value = gaim_status_get_attr_value(status, id)) == NULL) | |
| 853 return FALSE; | |
| 854 | |
| 855 g_return_val_if_fail(gaim_value_get_type(value) == GAIM_TYPE_STRING, NULL); | |
| 856 | |
| 857 return gaim_value_get_string(value); | |
| 858 } | |
| 859 | |
| 860 gint | |
| 861 gaim_status_compare(const GaimStatus *status1, const GaimStatus *status2) | |
| 862 { | |
| 863 GaimStatusType *type1, *type2; | |
| 864 int score1 = 0, score2 = 0; | |
| 6065 | 865 |
| 9949 | 866 if ((status1 == NULL && status2 == NULL) || |
| 867 (status1 == status2)) | |
| 868 { | |
| 869 return 0; | |
| 870 } | |
| 871 else if (status1 == NULL) | |
| 872 return 1; | |
| 873 else if (status2 == NULL) | |
| 874 return -1; | |
| 875 | |
| 876 type1 = gaim_status_get_type(status1); | |
| 877 type2 = gaim_status_get_type(status2); | |
| 878 | |
| 879 if (gaim_status_is_active(status1)) | |
| 880 score1 = primitive_scores[gaim_status_type_get_primitive(type1)]; | |
| 881 | |
| 882 if (gaim_status_is_active(status2)) | |
| 883 score2 = primitive_scores[gaim_status_type_get_primitive(type2)]; | |
| 884 | |
| 885 if (score1 > score2) | |
| 886 return -1; | |
| 887 else if (score1 < score2) | |
| 888 return 1; | |
| 889 | |
| 890 return 0; | |
| 891 } | |
| 892 | |
| 893 | |
| 894 /************************************************************************** | |
| 895 * GaimPresence API | |
| 896 **************************************************************************/ | |
| 897 GaimPresence * | |
| 898 gaim_presence_new(GaimPresenceContext context) | |
| 899 { | |
| 900 GaimPresence *presence; | |
| 901 | |
| 902 g_return_val_if_fail(context != GAIM_PRESENCE_CONTEXT_UNSET, NULL); | |
| 903 | |
| 904 presence = g_new0(GaimPresence, 1); | |
| 905 | |
| 906 presence->context = context; | |
| 907 | |
| 908 presence->status_table = | |
| 10009 | 909 g_hash_table_new_full(g_str_hash, g_str_equal, |
| 910 g_free, (GFreeFunc)gaim_status_destroy); | |
| 9949 | 911 |
| 912 return presence; | |
| 913 } | |
| 914 | |
| 915 GaimPresence * | |
| 916 gaim_presence_new_for_account(GaimAccount *account) | |
| 917 { | |
| 918 GaimPresence *presence; | |
| 10009 | 919 |
| 9949 | 920 g_return_val_if_fail(account != NULL, NULL); |
| 921 | |
| 922 presence = gaim_presence_new(GAIM_PRESENCE_CONTEXT_ACCOUNT); | |
| 923 presence->u.account = account; | |
| 10006 | 924 presence->statuses = gaim_prpl_get_statuses(account, presence); |
| 9949 | 925 |
| 926 return presence; | |
| 927 } | |
| 928 | |
| 929 GaimPresence * | |
| 930 gaim_presence_new_for_conv(GaimConversation *conv) | |
| 931 { | |
| 932 GaimPresence *presence; | |
| 933 | |
| 934 g_return_val_if_fail(conv != NULL, NULL); | |
| 935 | |
| 936 presence = gaim_presence_new(GAIM_PRESENCE_CONTEXT_CONV); | |
| 937 presence->u.chat.conv = conv; | |
| 10006 | 938 /* presence->statuses = gaim_prpl_get_statuses(conv->account, presence); ? */ |
| 9949 | 939 |
| 940 return presence; | |
| 941 } | |
| 6216 | 942 |
| 9949 | 943 GaimPresence * |
| 944 gaim_presence_new_for_buddy(GaimBuddy *buddy) | |
| 945 { | |
| 946 GaimPresence *presence; | |
| 947 GaimStatusBuddyKey *key; | |
| 10006 | 948 GaimAccount *account; |
| 949 gchar *hash_seed; | |
| 9949 | 950 |
| 951 g_return_val_if_fail(buddy != NULL, NULL); | |
| 952 | |
| 10006 | 953 account = buddy->account; |
| 954 hash_seed = g_strdup_printf("%s:%s:%s", buddy->name, account->username, | |
| 955 account->protocol_id); | |
| 956 | |
| 9949 | 957 key = g_new0(GaimStatusBuddyKey, 1); |
| 958 key->account = buddy->account; | |
| 959 key->name = g_strdup(buddy->name); | |
| 10006 | 960 key->id = g_str_hash(hash_seed); |
| 9949 | 961 |
| 10006 | 962 g_free(hash_seed); |
| 963 hash_seed = NULL; | |
| 964 | |
| 965 presence = g_hash_table_lookup(buddy_presences, key); | |
| 966 if (presence == NULL) | |
| 9949 | 967 { |
| 968 presence = gaim_presence_new(GAIM_PRESENCE_CONTEXT_BUDDY); | |
| 969 | |
| 970 presence->u.buddy.name = g_strdup(buddy->name); | |
| 971 presence->u.buddy.account = buddy->account; | |
| 10006 | 972 presence->statuses = gaim_prpl_get_statuses(buddy->account, presence); |
| 9949 | 973 |
| 974 g_hash_table_insert(buddy_presences, key, presence); | |
| 975 } | |
| 976 else | |
| 977 { | |
| 978 g_free(key->name); | |
| 979 g_free(key); | |
| 980 } | |
| 981 | |
| 982 presence->u.buddy.ref_count++; | |
| 983 presence->u.buddy.buddies = g_list_append(presence->u.buddy.buddies, | |
| 984 buddy); | |
| 985 | |
| 986 return presence; | |
| 987 } | |
| 988 | |
| 989 void | |
| 990 gaim_presence_destroy(GaimPresence *presence) | |
| 991 { | |
| 992 g_return_if_fail(presence != NULL); | |
| 6216 | 993 |
| 9949 | 994 if (gaim_presence_get_context(presence) == GAIM_PRESENCE_CONTEXT_BUDDY) |
| 995 { | |
| 996 GaimStatusBuddyKey key; | |
| 997 | |
| 998 presence->u.buddy.ref_count--; | |
| 999 | |
| 1000 g_return_if_fail(presence->u.buddy.ref_count == 0); | |
| 1001 | |
| 1002 key.account = presence->u.buddy.account; | |
| 1003 key.name = presence->u.buddy.name; | |
| 1004 | |
| 1005 g_hash_table_remove(buddy_presences, &key); | |
| 1006 | |
| 1007 if (presence->u.buddy.name != NULL) | |
| 1008 g_free(presence->u.buddy.name); | |
| 1009 } | |
| 1010 else if (gaim_presence_get_context(presence) == GAIM_PRESENCE_CONTEXT_CONV) | |
| 1011 { | |
| 1012 if (presence->u.chat.user != NULL) | |
| 1013 g_free(presence->u.chat.user); | |
| 1014 } | |
| 1015 | |
| 10006 | 1016 if (buddy_presences != NULL) |
| 1017 g_hash_table_destroy(buddy_presences); | |
| 1018 | |
| 9949 | 1019 if (presence->statuses != NULL) |
| 1020 g_list_free(presence->statuses); | |
| 1021 | |
| 1022 g_hash_table_destroy(presence->status_table); | |
| 1023 | |
| 1024 g_free(presence); | |
| 1025 } | |
| 1026 | |
| 1027 void | |
| 1028 gaim_presence_remove_buddy(GaimPresence *presence, GaimBuddy *buddy) | |
| 1029 { | |
| 1030 g_return_if_fail(presence != NULL); | |
| 1031 g_return_if_fail(buddy != NULL); | |
| 1032 g_return_if_fail(gaim_presence_get_context(presence) == | |
| 1033 GAIM_PRESENCE_CONTEXT_BUDDY); | |
| 1034 | |
| 1035 if (g_list_find(presence->u.buddy.buddies, buddy) != NULL) | |
| 1036 { | |
| 1037 presence->u.buddy.buddies = g_list_remove(presence->u.buddy.buddies, | |
| 1038 buddy); | |
| 1039 presence->u.buddy.ref_count--; | |
| 1040 } | |
| 6065 | 1041 } |
| 1042 | |
| 9949 | 1043 void |
| 1044 gaim_presence_add_status(GaimPresence *presence, GaimStatus *status) | |
| 1045 { | |
| 1046 g_return_if_fail(presence != NULL); | |
| 1047 g_return_if_fail(status != NULL); | |
| 1048 | |
| 1049 presence->statuses = g_list_append(presence->statuses, status); | |
| 1050 | |
| 1051 g_hash_table_insert(presence->status_table, | |
| 1052 g_strdup(gaim_status_get_id(status)), status); | |
| 1053 } | |
| 1054 | |
| 1055 void | |
| 1056 gaim_presence_add_presence(GaimPresence *presence, const GList *source_list) | |
| 1057 { | |
| 1058 const GList *l; | |
| 1059 | |
| 1060 g_return_if_fail(presence != NULL); | |
| 1061 g_return_if_fail(source_list != NULL); | |
| 1062 | |
| 1063 for (l = source_list; l != NULL; l = l->next) | |
| 1064 gaim_presence_add_status(presence, (GaimStatus *)l->data); | |
| 1065 } | |
| 1066 | |
| 1067 void | |
| 1068 gaim_presence_set_status_active(GaimPresence *presence, const char *status_id, | |
| 1069 gboolean active) | |
| 1070 { | |
| 1071 GaimStatus *status; | |
| 1072 | |
| 1073 g_return_if_fail(presence != NULL); | |
| 1074 g_return_if_fail(status_id != NULL); | |
| 1075 | |
| 1076 status = gaim_presence_get_status(presence, status_id); | |
| 1077 | |
| 1078 g_return_if_fail(status != NULL); | |
| 1079 | |
| 1080 if (!gaim_status_is_independent(status)) | |
| 1081 { | |
| 1082 if (!active) | |
| 1083 { | |
| 1084 gaim_debug_warning("status", | |
| 1085 "Attempted to set a non-independent status " | |
| 1086 "(%s) inactive. Only independent statuses " | |
| 1087 "can be specifically marked inactive.", | |
| 1088 status_id); | |
| 1089 | |
| 1090 return; | |
| 1091 } | |
| 1092 | |
| 1093 if (presence->active_status != NULL) | |
| 1094 gaim_status_set_active(presence->active_status, FALSE); | |
| 1095 | |
| 1096 presence->active_status = status; | |
| 1097 } | |
| 1098 | |
| 1099 gaim_status_set_active(status, active); | |
| 1100 } | |
| 1101 | |
| 1102 void | |
| 1103 gaim_presence_switch_status(GaimPresence *presence, const char *status_id) | |
| 1104 { | |
| 1105 GaimStatus *status; | |
| 1106 | |
| 1107 g_return_if_fail(presence != NULL); | |
| 1108 g_return_if_fail(status_id != NULL); | |
| 1109 | |
| 1110 status = gaim_presence_get_status(presence, status_id); | |
| 1111 | |
| 1112 g_return_if_fail(status != NULL); | |
| 1113 | |
| 1114 if (gaim_status_is_independent(status)) | |
| 1115 return; | |
| 1116 | |
| 1117 if (presence->active_status != NULL) | |
| 1118 gaim_status_set_active(presence->active_status, FALSE); | |
| 1119 | |
| 1120 gaim_status_set_active(status, TRUE); | |
| 1121 } | |
| 1122 | |
| 1123 static void | |
| 1124 update_buddy_idle(GaimBuddy *buddy, GaimPresence *presence, | |
| 1125 time_t current_time, gboolean old_idle, gboolean idle) | |
| 1126 { | |
| 1127 GaimBlistUiOps *ops = gaim_get_blist()->ui_ops; | |
| 1128 | |
| 1129 if (!old_idle && idle) | |
| 1130 { | |
| 1131 gaim_signal_emit(gaim_blist_get_handle(), "buddy-idle", buddy); | |
| 1132 | |
| 1133 if (gaim_prefs_get_bool("/core/logging/log_system") && | |
| 1134 gaim_prefs_get_bool("/core/logging/log_idle_state")) | |
| 1135 { | |
| 1136 GaimLog *log = gaim_account_get_log(buddy->account); | |
| 1137 char *tmp = g_strdup_printf(_("%s became idle"), | |
| 1138 gaim_buddy_get_alias(buddy)); | |
| 1139 | |
| 1140 gaim_log_write(log, GAIM_MESSAGE_SYSTEM, | |
| 1141 gaim_buddy_get_alias(buddy), current_time, tmp); | |
| 1142 g_free(tmp); | |
| 1143 } | |
| 1144 } | |
| 1145 else if (old_idle && !idle) | |
| 1146 { | |
| 1147 gaim_signal_emit(gaim_blist_get_handle(), "buddy-unidle", buddy); | |
| 1148 | |
| 1149 if (gaim_prefs_get_bool("/core/logging/log_system") && | |
| 1150 gaim_prefs_get_bool("/core/logging/log_idle_state")) | |
| 1151 { | |
| 1152 GaimLog *log = gaim_account_get_log(buddy->account); | |
| 1153 char *tmp = g_strdup_printf(_("%s became unidle"), | |
| 1154 gaim_buddy_get_alias(buddy)); | |
| 1155 | |
| 1156 gaim_log_write(log, GAIM_MESSAGE_SYSTEM, | |
| 1157 gaim_buddy_get_alias(buddy), current_time, tmp); | |
| 1158 g_free(tmp); | |
| 1159 } | |
| 1160 } | |
| 1161 | |
| 1162 gaim_contact_compute_priority_buddy(gaim_buddy_get_contact(buddy)); | |
| 1163 | |
| 1164 if (ops != NULL && ops->update != NULL) | |
| 1165 ops->update(gaim_get_blist(), (GaimBlistNode *)buddy); | |
| 1166 } | |
| 1167 | |
| 1168 void | |
| 1169 gaim_presence_set_idle(GaimPresence *presence, gboolean idle, time_t idle_time) | |
| 1170 { | |
| 1171 gboolean old_idle; | |
| 1172 | |
| 1173 g_return_if_fail(presence != NULL); | |
| 1174 | |
| 1175 if (presence->idle == idle && presence->idle_time == idle_time) | |
| 1176 return; | |
| 1177 | |
| 1178 old_idle = presence->idle; | |
| 1179 presence->idle = idle; | |
| 1180 presence->idle_time = (idle ? idle_time : 0); | |
| 1181 | |
| 1182 if (gaim_presence_get_context(presence) == GAIM_PRESENCE_CONTEXT_BUDDY) | |
| 1183 { | |
| 1184 const GList *l; | |
| 1185 time_t current_time = time(NULL); | |
| 1186 | |
| 1187 for (l = gaim_presence_get_buddies(presence); l != NULL; l = l->next) | |
| 1188 { | |
| 1189 update_buddy_idle((GaimBuddy *)l->data, presence, current_time, | |
| 1190 old_idle, idle); | |
| 1191 } | |
| 1192 } | |
| 1193 } | |
| 1194 | |
| 1195 void | |
| 10006 | 1196 gaim_presence_set_login_time(GaimPresence *presence, time_t login_time) |
| 1197 { | |
| 1198 g_return_if_fail(presence != NULL); | |
| 1199 | |
| 1200 if (presence->login_time == login_time) | |
| 1201 return; | |
| 1202 | |
| 1203 presence->login_time = login_time; | |
| 1204 } | |
| 1205 | |
| 1206 void | |
| 9949 | 1207 gaim_presence_set_warning_level(GaimPresence *presence, unsigned int level) |
| 6065 | 1208 { |
| 9949 | 1209 g_return_if_fail(presence != NULL); |
| 1210 g_return_if_fail(level <= 100); | |
| 1211 | |
| 1212 if (presence->warning_level == level) | |
| 1213 return; | |
| 1214 | |
| 1215 presence->warning_level = level; | |
| 1216 | |
| 1217 if (gaim_presence_get_context(presence) == GAIM_PRESENCE_CONTEXT_BUDDY) | |
| 1218 { | |
| 1219 GaimBlistUiOps *ops = gaim_get_blist()->ui_ops; | |
| 1220 | |
| 1221 if (ops != NULL && ops->update != NULL) | |
| 1222 { | |
| 1223 const GList *l; | |
| 1224 | |
| 1225 for (l = gaim_presence_get_buddies(presence); | |
| 1226 l != NULL; | |
| 1227 l = l->next) | |
| 1228 { | |
| 1229 ops->update(gaim_get_blist(), (GaimBlistNode *)l->data); | |
| 1230 } | |
| 1231 } | |
| 1232 } | |
| 1233 } | |
| 1234 | |
| 1235 GaimPresenceContext | |
| 1236 gaim_presence_get_context(const GaimPresence *presence) | |
| 1237 { | |
| 1238 g_return_val_if_fail(presence != NULL, GAIM_PRESENCE_CONTEXT_UNSET); | |
| 1239 | |
| 1240 return presence->context; | |
| 1241 } | |
| 1242 | |
| 1243 GaimAccount * | |
| 1244 gaim_presence_get_account(const GaimPresence *presence) | |
| 1245 { | |
| 1246 GaimPresenceContext context; | |
| 1247 | |
| 1248 g_return_val_if_fail(presence != NULL, NULL); | |
| 1249 | |
| 1250 context = gaim_presence_get_context(presence); | |
| 1251 | |
| 1252 g_return_val_if_fail(context == GAIM_PRESENCE_CONTEXT_ACCOUNT || | |
| 1253 context == GAIM_PRESENCE_CONTEXT_BUDDY, NULL); | |
| 1254 | |
| 1255 return presence->u.account; | |
| 1256 } | |
| 1257 | |
| 1258 GaimConversation * | |
| 1259 gaim_presence_get_conversation(const GaimPresence *presence) | |
| 1260 { | |
| 1261 g_return_val_if_fail(presence != NULL, NULL); | |
| 1262 g_return_val_if_fail(gaim_presence_get_context(presence) == | |
| 1263 GAIM_PRESENCE_CONTEXT_CONV, NULL); | |
| 1264 | |
| 1265 return presence->u.chat.conv; | |
| 1266 } | |
| 1267 | |
| 1268 const char * | |
| 1269 gaim_presence_get_chat_user(const GaimPresence *presence) | |
| 1270 { | |
| 1271 g_return_val_if_fail(presence != NULL, NULL); | |
| 1272 g_return_val_if_fail(gaim_presence_get_context(presence) == | |
| 1273 GAIM_PRESENCE_CONTEXT_CONV, NULL); | |
| 1274 | |
| 1275 return presence->u.chat.user; | |
| 1276 } | |
| 1277 | |
| 1278 const GList * | |
| 1279 gaim_presence_get_buddies(const GaimPresence *presence) | |
| 1280 { | |
| 1281 g_return_val_if_fail(presence != NULL, NULL); | |
| 1282 g_return_val_if_fail(gaim_presence_get_context(presence) == | |
| 1283 GAIM_PRESENCE_CONTEXT_BUDDY, NULL); | |
| 1284 | |
| 1285 return presence->u.buddy.buddies; | |
| 1286 } | |
| 1287 | |
| 1288 const GList * | |
| 1289 gaim_presence_get_statuses(const GaimPresence *presence) | |
| 1290 { | |
| 1291 g_return_val_if_fail(presence != NULL, NULL); | |
| 1292 | |
| 1293 return presence->statuses; | |
| 1294 } | |
| 1295 | |
| 1296 GaimStatus * | |
| 1297 gaim_presence_get_status(const GaimPresence *presence, const char *status_id) | |
| 1298 { | |
| 1299 GaimStatus *status; | |
| 10006 | 1300 const GList *l = NULL; |
| 9949 | 1301 |
| 1302 g_return_val_if_fail(presence != NULL, NULL); | |
| 1303 g_return_val_if_fail(status_id != NULL, NULL); | |
| 1304 | |
| 10006 | 1305 /* What's the purpose of this hash table? */ |
| 1306 status = (GaimStatus *)g_hash_table_lookup(presence->status_table, | |
| 1307 status_id); | |
| 1308 | |
| 1309 if (status == NULL) { | |
| 1310 for (l = gaim_presence_get_statuses(presence); | |
| 1311 l != NULL && status == NULL; l = l->next) | |
| 1312 { | |
| 1313 GaimStatus *temp_status = l->data; | |
| 1314 | |
| 1315 if (!strcmp(status_id, gaim_status_get_id(temp_status))) | |
| 1316 status = temp_status; | |
| 1317 } | |
| 1318 | |
| 1319 if (status != NULL) | |
| 1320 g_hash_table_insert(presence->status_table, | |
| 1321 g_strdup(gaim_status_get_id(status)), status); | |
| 1322 } | |
| 9949 | 1323 |
| 1324 return status; | |
| 1325 } | |
| 1326 | |
| 1327 GaimStatus * | |
| 1328 gaim_presence_get_active_status(const GaimPresence *presence) | |
| 1329 { | |
| 1330 g_return_val_if_fail(presence != NULL, NULL); | |
| 1331 | |
| 1332 return presence->active_status; | |
| 1333 } | |
| 1334 | |
| 1335 gboolean | |
| 1336 gaim_presence_is_available(const GaimPresence *presence) | |
| 1337 { | |
| 1338 GaimStatus *status; | |
| 1339 | |
| 1340 g_return_val_if_fail(presence != NULL, FALSE); | |
| 1341 | |
| 1342 status = gaim_presence_get_active_status(presence); | |
| 1343 | |
| 1344 return ((status != NULL && gaim_status_is_available(status)) && | |
| 1345 !gaim_presence_is_idle(presence)); | |
| 1346 } | |
| 1347 | |
| 1348 gboolean | |
| 1349 gaim_presence_is_online(const GaimPresence *presence) | |
| 1350 { | |
| 1351 GaimStatus *status; | |
| 1352 GaimStatusPrimitive primitive; | |
| 1353 | |
| 1354 g_return_val_if_fail(presence != NULL, FALSE); | |
| 1355 | |
| 1356 if ((status = gaim_presence_get_active_status(presence)) == NULL) | |
| 1357 return FALSE; | |
| 1358 | |
| 1359 primitive = gaim_status_type_get_primitive(gaim_status_get_type(status)); | |
| 1360 | |
| 1361 return (primitive != GAIM_STATUS_UNSET && | |
| 1362 primitive != GAIM_STATUS_OFFLINE); | |
| 1363 } | |
| 1364 | |
| 1365 gboolean | |
| 1366 gaim_presence_is_status_active(const GaimPresence *presence, | |
| 1367 const char *status_id) | |
| 1368 { | |
| 1369 GaimStatus *status; | |
| 1370 | |
| 1371 g_return_val_if_fail(presence != NULL, FALSE); | |
| 1372 g_return_val_if_fail(status_id != NULL, FALSE); | |
| 1373 | |
| 1374 status = gaim_presence_get_status(presence, status_id); | |
| 1375 | |
| 1376 return (status != NULL && gaim_status_is_active(status)); | |
| 1377 } | |
| 1378 | |
| 1379 gboolean | |
| 1380 gaim_presence_is_status_primitive_active(const GaimPresence *presence, | |
| 1381 GaimStatusPrimitive primitive) | |
| 1382 { | |
| 1383 GaimStatus *status; | |
| 1384 GaimStatusType *status_type; | |
| 1385 | |
| 1386 g_return_val_if_fail(presence != NULL, FALSE); | |
| 1387 g_return_val_if_fail(primitive != GAIM_STATUS_UNSET, FALSE); | |
| 1388 | |
| 1389 status = gaim_presence_get_active_status(presence); | |
| 1390 status_type = gaim_status_get_type(status); | |
| 1391 | |
| 1392 if (gaim_status_type_get_primitive(status_type) == primitive) | |
| 1393 return TRUE; | |
| 6065 | 1394 |
| 1395 return FALSE; | |
| 1396 } | |
| 1397 | |
| 9949 | 1398 gboolean |
| 1399 gaim_presence_is_idle(const GaimPresence *presence) | |
| 6065 | 1400 { |
| 9949 | 1401 g_return_val_if_fail(presence != NULL, FALSE); |
| 1402 | |
| 1403 return presence->idle; | |
| 6065 | 1404 } |
| 1405 | |
| 9949 | 1406 time_t |
| 1407 gaim_presence_get_idle_time(const GaimPresence *presence) | |
| 6065 | 1408 { |
| 9949 | 1409 g_return_val_if_fail(presence != NULL, 0); |
| 6065 | 1410 |
| 9949 | 1411 return presence->idle_time; |
| 1412 } | |
| 6065 | 1413 |
| 9949 | 1414 unsigned int |
| 1415 gaim_presence_get_warning_level(const GaimPresence *presence) | |
| 1416 { | |
| 1417 g_return_val_if_fail(presence != NULL, 0); | |
| 6216 | 1418 |
| 9949 | 1419 return presence->warning_level; |
| 6065 | 1420 } |
| 1421 | |
| 9949 | 1422 gint |
| 1423 gaim_presence_compare(const GaimPresence *presence1, | |
| 1424 const GaimPresence *presence2) | |
| 6065 | 1425 { |
| 9949 | 1426 gboolean idle1, idle2; |
| 1427 size_t idle_time_1, idle_time_2; | |
| 1428 int score1 = 0, score2 = 0; | |
| 1429 const GList *l; | |
| 6065 | 1430 |
| 9949 | 1431 if ((presence1 == NULL && presence2 == NULL) || (presence1 == presence2)) |
| 1432 return 0; | |
| 1433 else if (presence1 == NULL) | |
| 1434 return -1; | |
| 1435 else if (presence2 == NULL) | |
| 1436 return 1; | |
| 6065 | 1437 |
| 9949 | 1438 /* Compute the score of the first set of statuses. */ |
| 1439 for (l = gaim_presence_get_statuses(presence1); l != NULL; l = l->next) | |
| 1440 { | |
| 1441 GaimStatus *status = (GaimStatus *)l->data; | |
| 1442 GaimStatusType *type = gaim_status_get_type(status); | |
| 6065 | 1443 |
| 9949 | 1444 if (gaim_status_is_active(status)) |
| 1445 score1 += primitive_scores[gaim_status_type_get_primitive(type)]; | |
| 6065 | 1446 } |
| 1447 | |
| 9949 | 1448 /* Compute the score of the second set of statuses. */ |
| 1449 for (l = gaim_presence_get_statuses(presence1); l != NULL; l = l->next) | |
| 1450 { | |
| 1451 GaimStatus *status = (GaimStatus *)l->data; | |
| 1452 GaimStatusType *type = gaim_status_get_type(status); | |
| 6065 | 1453 |
| 9949 | 1454 if (gaim_status_is_active(status)) |
| 1455 score2 += primitive_scores[gaim_status_type_get_primitive(type)]; | |
| 6065 | 1456 } |
| 1457 | |
| 9949 | 1458 idle1 = gaim_presence_is_idle(presence1); |
| 1459 idle2 = gaim_presence_is_idle(presence2); | |
| 6065 | 1460 |
| 9949 | 1461 if (idle1) |
| 1462 score1 += primitive_scores[SCORE_IDLE]; | |
| 6065 | 1463 |
| 9949 | 1464 if (idle2) |
| 1465 score2 += primitive_scores[SCORE_IDLE]; | |
| 6065 | 1466 |
| 9949 | 1467 idle_time_1 = gaim_presence_get_idle_time(presence1); |
| 1468 idle_time_2 = gaim_presence_get_idle_time(presence2); | |
| 6065 | 1469 |
| 9949 | 1470 if (idle_time_1 > idle_time_2) |
| 1471 score1 += primitive_scores[SCORE_IDLE_TIME]; | |
| 1472 else if (idle_time_1 < idle_time_2) | |
| 1473 score2 += primitive_scores[SCORE_IDLE_TIME]; | |
| 6065 | 1474 |
| 9949 | 1475 if (score1 < score2) |
| 1476 return 1; | |
| 1477 else if (score1 > score2) | |
| 1478 return -1; | |
| 1479 | |
| 1480 return 0; | |
| 1481 } | |
| 1482 | |
| 6065 | 1483 |
| 9949 | 1484 /************************************************************************** |
| 1485 * Status subsystem | |
| 1486 **************************************************************************/ | |
| 1487 static void | |
| 1488 score_pref_changed_cb(const char *name, GaimPrefType type, gpointer value, | |
| 1489 gpointer data) | |
| 1490 { | |
| 1491 int index = GPOINTER_TO_INT(data); | |
| 6065 | 1492 |
| 9949 | 1493 primitive_scores[index] = GPOINTER_TO_INT(value); |
| 6065 | 1494 } |
| 1495 | |
| 10006 | 1496 guint |
| 1497 gaim_buddy_presences_hash(gconstpointer key) | |
| 1498 { | |
| 1499 return ((GaimStatusBuddyKey *)key)->id; | |
| 1500 } | |
| 1501 | |
| 1502 gboolean | |
| 1503 gaim_buddy_presences_equal(gconstpointer a, gconstpointer b) | |
| 1504 { | |
| 1505 GaimStatusBuddyKey *key_a = (GaimStatusBuddyKey *)a; | |
| 1506 GaimStatusBuddyKey *key_b = (GaimStatusBuddyKey *)b; | |
| 1507 | |
| 1508 if (key_a->id == key_b->id) | |
| 1509 return TRUE; | |
| 1510 else | |
| 1511 return FALSE; | |
| 1512 } | |
| 1513 | |
| 9949 | 1514 void |
| 1515 gaim_statuses_init(void) | |
| 6065 | 1516 { |
| 9949 | 1517 gaim_prefs_add_none("/core/status"); |
| 1518 gaim_prefs_add_none("/core/status/scores"); | |
| 6065 | 1519 |
| 9949 | 1520 gaim_prefs_add_int("/core/status/scores/offline", |
| 1521 primitive_scores[GAIM_STATUS_OFFLINE]); | |
| 1522 gaim_prefs_add_int("/core/status/scores/available", | |
| 1523 primitive_scores[GAIM_STATUS_AVAILABLE]); | |
| 1524 gaim_prefs_add_int("/core/status/scores/hidden", | |
| 1525 primitive_scores[GAIM_STATUS_HIDDEN]); | |
| 1526 gaim_prefs_add_int("/core/status/scores/away", | |
| 1527 primitive_scores[GAIM_STATUS_AWAY]); | |
| 1528 gaim_prefs_add_int("/core/status/scores/extended_away", | |
| 1529 primitive_scores[GAIM_STATUS_EXTENDED_AWAY]); | |
| 1530 gaim_prefs_add_int("/core/status/scores/idle", | |
| 1531 primitive_scores[SCORE_IDLE]); | |
| 6065 | 1532 |
| 9949 | 1533 gaim_prefs_connect_callback("/core/status/scores/offline", |
| 1534 score_pref_changed_cb, | |
| 1535 GINT_TO_POINTER(GAIM_STATUS_OFFLINE)); | |
| 1536 gaim_prefs_connect_callback("/core/status/scores/available", | |
| 1537 score_pref_changed_cb, | |
| 1538 GINT_TO_POINTER(GAIM_STATUS_AVAILABLE)); | |
| 1539 gaim_prefs_connect_callback("/core/status/scores/hidden", | |
| 1540 score_pref_changed_cb, | |
| 1541 GINT_TO_POINTER(GAIM_STATUS_HIDDEN)); | |
| 1542 gaim_prefs_connect_callback("/core/status/scores/away", | |
| 1543 score_pref_changed_cb, | |
| 1544 GINT_TO_POINTER(GAIM_STATUS_AWAY)); | |
| 1545 gaim_prefs_connect_callback("/core/status/scores/extended_away", | |
| 1546 score_pref_changed_cb, | |
| 1547 GINT_TO_POINTER(GAIM_STATUS_EXTENDED_AWAY)); | |
| 1548 gaim_prefs_connect_callback("/core/status/scores/idle", | |
| 1549 score_pref_changed_cb, | |
| 1550 GINT_TO_POINTER(SCORE_IDLE)); | |
| 10006 | 1551 |
| 1552 /* XXX - I don't think this is destroyed correctly. --Mark */ | |
| 1553 buddy_presences = g_hash_table_new(gaim_buddy_presences_hash, | |
| 1554 gaim_buddy_presences_equal); | |
| 9949 | 1555 } |
| 6065 | 1556 |
| 9949 | 1557 void |
| 1558 gaim_statuses_uninit(void) | |
| 1559 { | |
| 1560 } | |
| 6065 | 1561 |
| 9949 | 1562 void |
| 1563 gaim_statuses_sync(void) | |
| 1564 { | |
| 6065 | 1565 } |
| 9949 | 1566 |
| 1567 void | |
| 1568 gaim_statuses_load(void) | |
| 1569 { | |
| 1570 } |
