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