Mercurial > pidgin
annotate src/status.c @ 12587:8afd25a37b35
[gaim-migrate @ 14916]
This should fix a bug where auto-away would switch your status to
away when you become idle and your status was invisible. That's bad.
committer: Tailor Script <tailor@pidgin.im>
| author | Mark Doliner <mark@kingant.net> |
|---|---|
| date | Wed, 21 Dec 2005 03:24:16 +0000 |
| parents | 410462ce25ba |
| children | 3169cd6727ad |
| rev | line source |
|---|---|
| 9944 | 1 /** |
| 10067 | 2 * @file status.c Status API |
| 9944 | 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" |
| 10400 | 28 #include "core.h" |
| 11187 | 29 #include "dbus-maybe.h" |
| 9949 | 30 #include "debug.h" |
| 10337 | 31 #include "notify.h" |
| 9949 | 32 #include "prefs.h" |
| 6065 | 33 #include "status.h" |
| 34 | |
| 9949 | 35 /** |
| 36 * A type of status. | |
| 37 */ | |
| 38 struct _GaimStatusType | |
| 39 { | |
| 40 GaimStatusPrimitive primitive; | |
|
6371
8f94cce8faa5
[gaim-migrate @ 6876]
Christian Hammond <chipx86@chipx86.com>
parents:
6321
diff
changeset
|
41 |
| 9949 | 42 char *id; |
| 43 char *name; | |
| 44 char *primary_attr_id; | |
| 45 | |
| 46 gboolean saveable; | |
| 47 gboolean user_settable; | |
| 48 gboolean independent; | |
| 49 | |
| 50 GList *attrs; | |
| 51 }; | |
|
6371
8f94cce8faa5
[gaim-migrate @ 6876]
Christian Hammond <chipx86@chipx86.com>
parents:
6321
diff
changeset
|
52 |
| 9949 | 53 /** |
| 54 * A status attribute. | |
| 55 */ | |
| 56 struct _GaimStatusAttr | |
| 57 { | |
| 58 char *id; | |
| 59 char *name; | |
| 60 GaimValue *value_type; | |
| 61 }; | |
| 6065 | 62 |
| 9949 | 63 /** |
| 64 * A list of statuses. | |
| 65 */ | |
| 66 struct _GaimPresence | |
| 67 { | |
| 68 GaimPresenceContext context; | |
| 69 | |
| 70 gboolean idle; | |
| 71 time_t idle_time; | |
| 11249 | 72 time_t login_time; |
| 9949 | 73 |
| 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; | |
| 121 } GaimStatusBuddyKey; | |
| 122 | |
| 123 static int primitive_scores[] = | |
| 124 { | |
| 125 0, /* unset */ | |
| 126 -500, /* offline */ | |
| 127 100, /* available */ | |
| 128 -75, /* unavailable */ | |
| 129 -50, /* hidden */ | |
| 130 -100, /* away */ | |
| 10860 | 131 -200, /* extended away */ |
| 9949 | 132 -10, /* idle, special case. */ |
| 133 -5 /* idle time, special case. */ | |
| 134 }; | |
| 135 | |
| 136 static GHashTable *buddy_presences = NULL; | |
| 137 | |
|
12164
281ab2ecc08c
[gaim-migrate @ 14465]
Richard Laager <rlaager@wiktel.com>
parents:
12145
diff
changeset
|
138 #define SCORE_IDLE 7 |
|
281ab2ecc08c
[gaim-migrate @ 14465]
Richard Laager <rlaager@wiktel.com>
parents:
12145
diff
changeset
|
139 #define SCORE_IDLE_TIME 8 |
| 9949 | 140 |
| 141 /************************************************************************** | |
| 10419 | 142 * GaimStatusPrimitive API |
| 143 **************************************************************************/ | |
| 144 static struct GaimStatusPrimitiveMap | |
| 145 { | |
| 146 GaimStatusPrimitive type; | |
| 147 const char *id; | |
| 148 const char *name; | |
| 149 | |
| 150 } const status_primitive_map[] = | |
| 151 { | |
| 152 { GAIM_STATUS_UNSET, "unset", N_("Unset") }, | |
| 153 { GAIM_STATUS_OFFLINE, "offline", N_("Offline") }, | |
| 154 { GAIM_STATUS_AVAILABLE, "available", N_("Available") }, | |
| 155 { GAIM_STATUS_UNAVAILABLE, "unavailable", N_("Unavailable") }, | |
| 156 { GAIM_STATUS_HIDDEN, "hidden", N_("Hidden") }, | |
| 157 { GAIM_STATUS_AWAY, "away", N_("Away") }, | |
| 158 { GAIM_STATUS_EXTENDED_AWAY, "extended_away", N_("Extended Away") } | |
| 159 }; | |
| 160 | |
| 161 const char * | |
| 162 gaim_primitive_get_id_from_type(GaimStatusPrimitive type) | |
| 163 { | |
| 164 int i; | |
| 165 | |
| 166 for (i = 0; i < GAIM_STATUS_NUM_PRIMITIVES; i++) | |
| 167 { | |
| 168 if (type == status_primitive_map[i].type) | |
| 169 return status_primitive_map[i].id; | |
| 170 } | |
| 171 | |
| 172 return status_primitive_map[0].id; | |
| 173 } | |
| 174 | |
| 175 const char * | |
| 176 gaim_primitive_get_name_from_type(GaimStatusPrimitive type) | |
| 177 { | |
| 178 int i; | |
| 179 | |
| 180 for (i = 0; i < GAIM_STATUS_NUM_PRIMITIVES; i++) | |
| 181 { | |
| 182 if (type == status_primitive_map[i].type) | |
| 183 return status_primitive_map[i].name; | |
| 184 } | |
| 185 | |
| 186 return status_primitive_map[0].name; | |
| 187 } | |
| 188 | |
| 189 GaimStatusPrimitive | |
| 190 gaim_primitive_get_type_from_id(const char *id) | |
| 191 { | |
| 192 int i; | |
| 193 | |
| 194 g_return_val_if_fail(id != NULL, GAIM_STATUS_UNSET); | |
| 195 | |
| 196 for (i = 0; i < GAIM_STATUS_NUM_PRIMITIVES; i++) | |
| 197 { | |
| 198 if (!strcmp(id, status_primitive_map[i].id)) | |
| 199 return status_primitive_map[i].type; | |
| 200 } | |
| 201 | |
| 202 return status_primitive_map[0].type; | |
| 203 } | |
| 204 | |
| 205 | |
| 206 /************************************************************************** | |
| 9949 | 207 * GaimStatusType API |
| 208 **************************************************************************/ | |
| 209 GaimStatusType * | |
| 210 gaim_status_type_new_full(GaimStatusPrimitive primitive, const char *id, | |
| 10009 | 211 const char *name, gboolean saveable, |
| 212 gboolean user_settable, gboolean independent) | |
| 9949 | 213 { |
| 214 GaimStatusType *status_type; | |
| 215 | |
| 216 g_return_val_if_fail(primitive != GAIM_STATUS_UNSET, NULL); | |
| 217 g_return_val_if_fail(id != NULL, NULL); | |
| 218 g_return_val_if_fail(name != NULL, NULL); | |
| 219 | |
| 220 status_type = g_new0(GaimStatusType, 1); | |
| 11187 | 221 GAIM_DBUS_REGISTER_POINTER(status_type, GaimStatusType); |
| 9949 | 222 |
| 223 status_type->primitive = primitive; | |
| 224 status_type->id = g_strdup(id); | |
| 225 status_type->name = g_strdup(name); | |
| 226 status_type->saveable = saveable; | |
| 227 status_type->user_settable = user_settable; | |
| 228 status_type->independent = independent; | |
| 229 | |
| 230 return status_type; | |
| 231 } | |
| 232 | |
| 233 GaimStatusType * | |
| 234 gaim_status_type_new(GaimStatusPrimitive primitive, const char *id, | |
| 10009 | 235 const char *name, gboolean user_settable) |
| 9949 | 236 { |
| 237 g_return_val_if_fail(primitive != GAIM_STATUS_UNSET, NULL); | |
| 238 g_return_val_if_fail(id != NULL, NULL); | |
| 239 g_return_val_if_fail(name != NULL, NULL); | |
| 240 | |
| 241 return gaim_status_type_new_full(primitive, id, name, FALSE, | |
| 242 user_settable, FALSE); | |
| 243 } | |
| 244 | |
| 245 GaimStatusType * | |
| 246 gaim_status_type_new_with_attrs(GaimStatusPrimitive primitive, | |
| 247 const char *id, const char *name, | |
| 248 gboolean saveable, gboolean user_settable, | |
| 249 gboolean independent, const char *attr_id, | |
| 250 const char *attr_name, GaimValue *attr_value, | |
| 251 ...) | |
| 252 { | |
| 253 GaimStatusType *status_type; | |
| 254 va_list args; | |
| 255 | |
| 256 g_return_val_if_fail(primitive != GAIM_STATUS_UNSET, NULL); | |
| 257 g_return_val_if_fail(id != NULL, NULL); | |
| 258 g_return_val_if_fail(name != NULL, NULL); | |
| 10012 | 259 g_return_val_if_fail(attr_id != NULL, NULL); |
| 9949 | 260 g_return_val_if_fail(attr_name != NULL, NULL); |
| 261 g_return_val_if_fail(attr_value != NULL, NULL); | |
| 262 | |
| 263 status_type = gaim_status_type_new_full(primitive, id, name, saveable, | |
| 264 user_settable, independent); | |
| 265 | |
| 10010 | 266 /* Add the first attribute */ |
| 9949 | 267 gaim_status_type_add_attr(status_type, attr_id, attr_name, attr_value); |
| 268 | |
| 269 va_start(args, attr_value); | |
| 270 gaim_status_type_add_attrs_vargs(status_type, args); | |
| 271 va_end(args); | |
| 272 | |
| 273 return status_type; | |
| 274 } | |
| 275 | |
| 276 void | |
| 277 gaim_status_type_destroy(GaimStatusType *status_type) | |
| 278 { | |
| 279 GList *l; | |
| 280 | |
| 281 g_return_if_fail(status_type != NULL); | |
| 282 | |
| 283 g_free(status_type->id); | |
| 284 g_free(status_type->name); | |
| 285 | |
| 286 if (status_type->primary_attr_id != NULL) | |
| 287 g_free(status_type->primary_attr_id); | |
| 288 | |
| 289 if (status_type->attrs != NULL) | |
| 290 { | |
| 291 for (l = status_type->attrs; l != NULL; l = l->next) | |
| 292 gaim_status_attr_destroy((GaimStatusAttr *)l->data); | |
| 293 | |
| 294 g_list_free(status_type->attrs); | |
| 295 } | |
| 296 | |
| 11187 | 297 GAIM_DBUS_UNREGISTER_POINTER(status_type); |
| 9949 | 298 g_free(status_type); |
| 299 } | |
| 300 | |
| 301 void | |
| 302 gaim_status_type_set_primary_attr(GaimStatusType *status_type, const char *id) | |
| 303 { | |
| 304 g_return_if_fail(status_type != NULL); | |
| 305 | |
| 306 if (status_type->primary_attr_id != NULL) | |
| 307 g_free(status_type->primary_attr_id); | |
| 308 | |
| 309 status_type->primary_attr_id = (id == NULL ? NULL : g_strdup(id)); | |
| 310 } | |
| 311 | |
| 312 void | |
| 10197 | 313 gaim_status_type_add_attr(GaimStatusType *status_type, const char *id, |
| 9949 | 314 const char *name, GaimValue *value) |
| 315 { | |
| 316 GaimStatusAttr *attr; | |
| 317 | |
| 318 g_return_if_fail(status_type != NULL); | |
| 319 g_return_if_fail(id != NULL); | |
| 320 g_return_if_fail(name != NULL); | |
| 321 g_return_if_fail(value != NULL); | |
| 322 | |
| 323 attr = gaim_status_attr_new(id, name, value); | |
| 324 | |
| 325 status_type->attrs = g_list_append(status_type->attrs, attr); | |
| 326 } | |
| 327 | |
| 328 void | |
| 329 gaim_status_type_add_attrs_vargs(GaimStatusType *status_type, va_list args) | |
| 330 { | |
| 331 const char *id, *name; | |
| 332 GaimValue *value; | |
| 333 | |
| 334 g_return_if_fail(status_type != NULL); | |
| 335 | |
| 336 while ((id = va_arg(args, const char *)) != NULL) | |
| 337 { | |
| 338 name = va_arg(args, const char *); | |
| 339 g_return_if_fail(name != NULL); | |
| 340 | |
| 341 value = va_arg(args, GaimValue *); | |
| 342 g_return_if_fail(value != NULL); | |
| 6065 | 343 |
| 9949 | 344 gaim_status_type_add_attr(status_type, id, name, value); |
| 345 } | |
| 346 } | |
| 347 | |
| 10010 | 348 void |
| 349 gaim_status_type_add_attrs(GaimStatusType *status_type, const char *id, | |
| 350 const char *name, GaimValue *value, ...) | |
| 351 { | |
| 352 va_list args; | |
| 353 | |
| 354 g_return_if_fail(status_type != NULL); | |
| 355 g_return_if_fail(id != NULL); | |
| 356 g_return_if_fail(name != NULL); | |
| 357 g_return_if_fail(value != NULL); | |
| 358 | |
| 359 /* Add the first attribute */ | |
| 360 gaim_status_type_add_attr(status_type, id, name, value); | |
| 361 | |
| 362 va_start(args, value); | |
| 363 gaim_status_type_add_attrs_vargs(status_type, args); | |
| 364 va_end(args); | |
| 365 } | |
| 366 | |
| 9949 | 367 GaimStatusPrimitive |
| 368 gaim_status_type_get_primitive(const GaimStatusType *status_type) | |
| 369 { | |
| 370 g_return_val_if_fail(status_type != NULL, GAIM_STATUS_UNSET); | |
| 371 | |
| 372 return status_type->primitive; | |
| 373 } | |
| 374 | |
| 375 const char * | |
| 376 gaim_status_type_get_id(const GaimStatusType *status_type) | |
| 377 { | |
| 378 g_return_val_if_fail(status_type != NULL, NULL); | |
| 379 | |
| 380 return status_type->id; | |
| 381 } | |
| 382 | |
| 383 const char * | |
| 384 gaim_status_type_get_name(const GaimStatusType *status_type) | |
| 385 { | |
| 386 g_return_val_if_fail(status_type != NULL, NULL); | |
| 387 | |
| 388 return status_type->name; | |
| 389 } | |
| 390 | |
| 391 gboolean | |
| 392 gaim_status_type_is_saveable(const GaimStatusType *status_type) | |
| 393 { | |
| 394 g_return_val_if_fail(status_type != NULL, FALSE); | |
| 395 | |
| 396 return status_type->saveable; | |
| 397 } | |
| 398 | |
| 399 gboolean | |
| 400 gaim_status_type_is_user_settable(const GaimStatusType *status_type) | |
| 401 { | |
| 402 g_return_val_if_fail(status_type != NULL, FALSE); | |
| 403 | |
| 404 return status_type->user_settable; | |
| 405 } | |
| 406 | |
| 407 gboolean | |
| 408 gaim_status_type_is_independent(const GaimStatusType *status_type) | |
| 409 { | |
| 410 g_return_val_if_fail(status_type != NULL, FALSE); | |
| 411 | |
| 412 return status_type->independent; | |
| 413 } | |
| 414 | |
| 415 gboolean | |
| 10067 | 416 gaim_status_type_is_exclusive(const GaimStatusType *status_type) |
| 417 { | |
| 418 g_return_val_if_fail(status_type != NULL, FALSE); | |
| 419 | |
| 420 return !status_type->independent; | |
| 421 } | |
| 422 | |
| 423 gboolean | |
| 9949 | 424 gaim_status_type_is_available(const GaimStatusType *status_type) |
| 425 { | |
| 426 GaimStatusPrimitive primitive; | |
| 427 | |
| 428 g_return_val_if_fail(status_type != NULL, FALSE); | |
| 429 | |
| 430 primitive = gaim_status_type_get_primitive(status_type); | |
| 431 | |
| 12587 | 432 return (primitive == GAIM_STATUS_AVAILABLE); |
| 9949 | 433 } |
| 434 | |
| 435 const char * | |
| 436 gaim_status_type_get_primary_attr(const GaimStatusType *status_type) | |
| 437 { | |
| 438 g_return_val_if_fail(status_type != NULL, NULL); | |
| 439 | |
| 440 return status_type->primary_attr_id; | |
| 441 } | |
| 442 | |
| 443 GaimStatusAttr * | |
| 444 gaim_status_type_get_attr(const GaimStatusType *status_type, const char *id) | |
| 445 { | |
| 446 GList *l; | |
| 447 | |
| 448 g_return_val_if_fail(status_type != NULL, NULL); | |
| 449 g_return_val_if_fail(id != NULL, NULL); | |
| 450 | |
| 451 for (l = status_type->attrs; l != NULL; l = l->next) | |
| 452 { | |
| 453 GaimStatusAttr *attr = (GaimStatusAttr *)l->data; | |
| 454 | |
| 455 if (!strcmp(gaim_status_attr_get_id(attr), id)) | |
| 456 return attr; | |
| 457 } | |
| 458 | |
| 459 return NULL; | |
| 460 } | |
| 461 | |
| 462 const GList * | |
| 463 gaim_status_type_get_attrs(const GaimStatusType *status_type) | |
| 464 { | |
| 465 g_return_val_if_fail(status_type != NULL, NULL); | |
| 466 | |
| 467 return status_type->attrs; | |
| 468 } | |
| 469 | |
| 10348 | 470 const GaimStatusType * |
| 471 gaim_status_type_find_with_id(GList *status_types, const char *id) | |
| 472 { | |
| 473 GaimStatusType *status_type; | |
| 474 | |
| 475 g_return_val_if_fail(id != NULL, NULL); | |
| 476 | |
| 477 while (status_types != NULL) | |
| 478 { | |
| 479 status_type = status_types->data; | |
| 480 | |
| 481 if (!strcmp(id, status_type->id)) | |
| 482 return status_type; | |
| 10895 | 483 |
| 484 status_types = status_types->next; | |
| 10348 | 485 } |
| 486 | |
| 487 return NULL; | |
| 488 } | |
| 489 | |
| 9949 | 490 |
| 491 /************************************************************************** | |
| 492 * GaimStatusAttr API | |
| 493 **************************************************************************/ | |
| 494 GaimStatusAttr * | |
| 495 gaim_status_attr_new(const char *id, const char *name, GaimValue *value_type) | |
| 496 { | |
| 497 GaimStatusAttr *attr; | |
| 498 | |
| 499 g_return_val_if_fail(id != NULL, NULL); | |
| 500 g_return_val_if_fail(name != NULL, NULL); | |
| 501 g_return_val_if_fail(value_type != NULL, NULL); | |
| 502 | |
| 503 attr = g_new0(GaimStatusAttr, 1); | |
| 11187 | 504 GAIM_DBUS_REGISTER_POINTER(attr, GaimStatusAttr); |
| 9949 | 505 |
| 506 attr->id = g_strdup(id); | |
| 507 attr->name = g_strdup(name); | |
| 508 attr->value_type = value_type; | |
| 509 | |
| 510 return attr; | |
| 511 } | |
| 512 | |
| 513 void | |
| 514 gaim_status_attr_destroy(GaimStatusAttr *attr) | |
| 515 { | |
| 516 g_return_if_fail(attr != NULL); | |
| 517 | |
| 518 g_free(attr->id); | |
| 519 g_free(attr->name); | |
| 520 | |
| 521 gaim_value_destroy(attr->value_type); | |
| 522 | |
| 11187 | 523 GAIM_DBUS_UNREGISTER_POINTER(attr); |
| 9949 | 524 g_free(attr); |
| 525 } | |
| 526 | |
| 527 const char * | |
| 528 gaim_status_attr_get_id(const GaimStatusAttr *attr) | |
| 529 { | |
| 530 g_return_val_if_fail(attr != NULL, NULL); | |
| 531 | |
| 532 return attr->id; | |
| 533 } | |
| 534 | |
| 535 const char * | |
| 536 gaim_status_attr_get_name(const GaimStatusAttr *attr) | |
| 537 { | |
| 538 g_return_val_if_fail(attr != NULL, NULL); | |
| 539 | |
| 540 return attr->name; | |
| 541 } | |
| 542 | |
| 543 GaimValue * | |
| 11249 | 544 gaim_status_attr_get_value(const GaimStatusAttr *attr) |
| 9949 | 545 { |
| 546 g_return_val_if_fail(attr != NULL, NULL); | |
| 547 | |
| 548 return attr->value_type; | |
| 549 } | |
| 550 | |
| 551 | |
| 552 /************************************************************************** | |
| 553 * GaimStatus API | |
| 554 **************************************************************************/ | |
| 555 GaimStatus * | |
| 556 gaim_status_new(GaimStatusType *status_type, GaimPresence *presence) | |
| 557 { | |
| 558 GaimStatus *status; | |
| 559 const GList *l; | |
| 560 | |
| 561 g_return_val_if_fail(status_type != NULL, NULL); | |
| 562 g_return_val_if_fail(presence != NULL, NULL); | |
| 563 | |
| 564 status = g_new0(GaimStatus, 1); | |
| 11187 | 565 GAIM_DBUS_REGISTER_POINTER(status, GaimStatus); |
| 9949 | 566 |
| 567 status->type = status_type; | |
| 568 status->presence = presence; | |
| 569 | |
| 570 status->attr_values = | |
| 571 g_hash_table_new_full(g_str_hash, g_str_equal, g_free, | |
| 572 (GDestroyNotify)gaim_value_destroy); | |
| 573 | |
| 574 for (l = gaim_status_type_get_attrs(status_type); l != NULL; l = l->next) | |
| 575 { | |
| 576 GaimStatusAttr *attr = (GaimStatusAttr *)l->data; | |
| 11249 | 577 GaimValue *value = gaim_status_attr_get_value(attr); |
| 9949 | 578 GaimValue *new_value = gaim_value_dup(value); |
| 579 | |
| 580 g_hash_table_insert(status->attr_values, | |
| 10197 | 581 g_strdup(gaim_status_attr_get_id(attr)), |
| 582 new_value); | |
| 9949 | 583 } |
| 584 | |
| 585 return status; | |
| 586 } | |
| 587 | |
| 10754 | 588 /* |
| 589 * TODO: If the GaimStatus is in a GaimPresence, then | |
| 590 * remove it from the GaimPresence? | |
| 591 */ | |
| 9949 | 592 void |
| 593 gaim_status_destroy(GaimStatus *status) | |
| 594 { | |
| 595 g_return_if_fail(status != NULL); | |
| 596 | |
| 597 g_hash_table_destroy(status->attr_values); | |
| 598 | |
| 11187 | 599 GAIM_DBUS_UNREGISTER_POINTER(status); |
| 9949 | 600 g_free(status); |
| 601 } | |
| 6065 | 602 |
| 603 static void | |
| 9949 | 604 notify_buddy_status_update(GaimBuddy *buddy, GaimPresence *presence, |
| 605 GaimStatus *old_status, GaimStatus *new_status) | |
| 606 { | |
| 607 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
| 608 | |
| 11698 | 609 if (gaim_prefs_get_bool("/core/logging/log_system")) |
| 9949 | 610 { |
| 611 time_t current_time = time(NULL); | |
| 612 const char *buddy_alias = gaim_buddy_get_alias(buddy); | |
| 613 char *tmp = NULL; | |
| 614 | |
| 11624 | 615 if (((old_status == NULL) || !gaim_status_is_available(old_status)) && |
| 9949 | 616 gaim_status_is_available(new_status)) |
| 617 { | |
| 618 tmp = g_strdup_printf(_("%s came back"), buddy_alias); | |
| 619 } | |
| 11624 | 620 else if ((old_status != NULL) && gaim_status_is_available(old_status) && |
| 9949 | 621 !gaim_status_is_available(new_status)) |
| 622 { | |
| 623 tmp = g_strdup_printf(_("%s went away"), buddy_alias); | |
| 624 } | |
| 625 | |
| 626 if (tmp != NULL) | |
| 627 { | |
| 628 GaimLog *log = gaim_account_get_log(buddy->account); | |
| 629 | |
| 630 gaim_log_write(log, GAIM_MESSAGE_SYSTEM, buddy_alias, | |
| 631 current_time, tmp); | |
| 632 g_free(tmp); | |
| 633 } | |
| 634 } | |
| 635 | |
| 10012 | 636 if (ops != NULL && ops->update != NULL) |
| 637 ops->update(gaim_get_blist(), (GaimBlistNode*)buddy); | |
| 9949 | 638 } |
| 639 | |
| 640 static void | |
| 641 notify_status_update(GaimPresence *presence, GaimStatus *old_status, | |
|
10176
0109f3a518d2
[gaim-migrate @ 11291]
Christian Hammond <chipx86@chipx86.com>
parents:
10153
diff
changeset
|
642 GaimStatus *new_status) |
| 6065 | 643 { |
| 9949 | 644 GaimPresenceContext context = gaim_presence_get_context(presence); |
| 645 | |
| 646 if (context == GAIM_PRESENCE_CONTEXT_ACCOUNT) | |
| 647 { | |
|
10176
0109f3a518d2
[gaim-migrate @ 11291]
Christian Hammond <chipx86@chipx86.com>
parents:
10153
diff
changeset
|
648 GaimAccount *account = gaim_presence_get_account(presence); |
| 9949 | 649 GaimAccountUiOps *ops = gaim_accounts_get_ui_ops(); |
| 650 | |
| 10400 | 651 if (gaim_account_get_enabled(account, gaim_core_get_ui())) |
| 10447 | 652 gaim_prpl_change_account_status(account, old_status, new_status); |
|
10176
0109f3a518d2
[gaim-migrate @ 11291]
Christian Hammond <chipx86@chipx86.com>
parents:
10153
diff
changeset
|
653 |
| 9949 | 654 if (ops != NULL && ops->status_changed != NULL) |
| 655 { | |
|
10176
0109f3a518d2
[gaim-migrate @ 11291]
Christian Hammond <chipx86@chipx86.com>
parents:
10153
diff
changeset
|
656 ops->status_changed(account, new_status); |
| 9949 | 657 } |
| 658 } | |
| 659 else if (context == GAIM_PRESENCE_CONTEXT_BUDDY) | |
| 660 { | |
| 661 const GList *l; | |
| 662 | |
| 663 for (l = gaim_presence_get_buddies(presence); l != NULL; l = l->next) | |
| 664 { | |
| 665 notify_buddy_status_update((GaimBuddy *)l->data, presence, | |
| 666 old_status, new_status); | |
| 667 } | |
| 668 } | |
| 669 } | |
| 670 | |
| 10204 | 671 static void |
| 672 status_has_changed(GaimStatus *status) | |
| 9949 | 673 { |
| 674 GaimPresence *presence; | |
| 675 GaimStatus *old_status; | |
| 676 | |
| 677 presence = gaim_status_get_presence(status); | |
| 678 | |
| 10204 | 679 /* |
| 680 * If this status is exclusive, then we must be setting it to "active." | |
| 681 * Since we are setting it to active, we want to set the currently | |
| 682 * active status to "inactive." | |
| 683 */ | |
| 684 if (gaim_status_is_exclusive(status)) | |
| 9949 | 685 { |
| 10754 | 686 old_status = gaim_presence_get_active_status(presence); |
| 10760 | 687 if (old_status != NULL && (old_status != status)) |
| 10754 | 688 old_status->active = FALSE; |
| 689 presence->active_status = status; | |
| 9949 | 690 } |
| 10754 | 691 else |
| 692 old_status = NULL; | |
| 9949 | 693 |
| 10204 | 694 notify_status_update(presence, old_status, status); |
| 695 } | |
| 696 | |
| 697 void | |
| 698 gaim_status_set_active(GaimStatus *status, gboolean active) | |
| 699 { | |
| 10754 | 700 gaim_status_set_active_with_attrs(status, active, NULL); |
| 10204 | 701 } |
| 702 | |
| 11249 | 703 /* |
| 704 * This used to parse the va_list directly, but now it creates a GList | |
| 705 * and passes it to gaim_status_set_active_with_attrs_list(). That | |
| 706 * function was created because accounts.c needs to pass a GList of | |
| 707 * attributes to the status API. | |
| 708 */ | |
| 10204 | 709 void |
| 710 gaim_status_set_active_with_attrs(GaimStatus *status, gboolean active, va_list args) | |
| 711 { | |
| 11249 | 712 GList *attrs = NULL; |
| 713 const gchar *id; | |
| 714 gpointer data; | |
| 715 | |
| 716 if (args != NULL) | |
| 717 { | |
| 718 while ((id = va_arg(args, const char *)) != NULL) | |
| 719 { | |
| 720 attrs = g_list_append(attrs, (char *)id); | |
| 721 data = va_arg(args, void *); | |
| 722 attrs = g_list_append(attrs, data); | |
| 723 } | |
| 724 } | |
| 725 gaim_status_set_active_with_attrs_list(status, active, attrs); | |
| 726 g_list_free(attrs); | |
| 727 } | |
| 728 | |
| 729 void | |
| 730 gaim_status_set_active_with_attrs_list(GaimStatus *status, gboolean active, | |
| 731 const GList *attrs) | |
| 732 { | |
| 10204 | 733 gboolean changed = FALSE; |
| 734 const gchar *id; | |
| 735 | |
| 10714 | 736 g_return_if_fail(status != NULL); |
| 737 | |
| 10204 | 738 if (!active && gaim_status_is_exclusive(status)) |
| 739 { | |
| 740 gaim_debug_error("status", | |
| 741 "Cannot deactivate an exclusive status (%s).\n", | |
| 742 gaim_status_get_id(status)); | |
| 743 return; | |
| 744 } | |
| 745 | |
| 746 if (status->active != active) | |
| 10738 | 747 { |
| 10204 | 748 changed = TRUE; |
| 10738 | 749 } |
| 10204 | 750 |
| 9949 | 751 status->active = active; |
| 6065 | 752 |
| 10204 | 753 /* Set any attributes */ |
| 11249 | 754 while (attrs) |
| 10204 | 755 { |
| 756 GaimValue *value; | |
| 11249 | 757 |
| 758 id = attrs->data; | |
| 759 attrs = attrs->next; | |
| 10204 | 760 value = gaim_status_get_attr_value(status, id); |
| 10713 | 761 if (value == NULL) |
| 762 { | |
| 763 gaim_debug_warning("status", "The attribute \"%s\" on the status \"%s\" is " | |
| 10714 | 764 "not supported.\n", id, status->type->name); |
| 10713 | 765 /* Skip over the data and move on to the next attribute */ |
| 11249 | 766 attrs = attrs->next; |
| 10713 | 767 continue; |
| 768 } | |
| 769 | |
| 10204 | 770 if (value->type == GAIM_TYPE_STRING) |
| 771 { | |
| 11249 | 772 const gchar *string_data = attrs->data; |
| 773 attrs = attrs->next; | |
| 10204 | 774 if (((string_data == NULL) && (value->data.string_data == NULL)) || |
| 775 ((string_data != NULL) && (value->data.string_data != NULL) && | |
| 776 !strcmp(string_data, value->data.string_data))) | |
| 777 { | |
| 778 continue; | |
| 779 } | |
| 780 gaim_status_set_attr_string(status, id, string_data); | |
| 781 changed = TRUE; | |
| 782 } | |
| 783 else if (value->type == GAIM_TYPE_INT) | |
| 784 { | |
| 11586 | 785 int int_data = GPOINTER_TO_INT(attrs->data); |
| 11249 | 786 attrs = attrs->next; |
| 10204 | 787 if (int_data == value->data.int_data) |
| 788 continue; | |
| 789 gaim_status_set_attr_int(status, id, int_data); | |
| 790 changed = TRUE; | |
| 791 } | |
| 792 else if (value->type == GAIM_TYPE_BOOLEAN) | |
| 793 { | |
| 11586 | 794 gboolean boolean_data = GPOINTER_TO_INT(attrs->data); |
| 11249 | 795 attrs = attrs->next; |
| 10204 | 796 if (boolean_data == value->data.boolean_data) |
| 797 continue; | |
| 798 gaim_status_set_attr_int(status, id, boolean_data); | |
| 799 changed = TRUE; | |
| 800 } | |
| 801 else | |
| 802 { | |
| 803 /* We don't know what the data is--skip over it */ | |
| 11249 | 804 attrs = attrs->next; |
| 10204 | 805 } |
| 806 } | |
| 807 | |
| 808 if (!changed) | |
| 809 return; | |
| 810 status_has_changed(status); | |
| 9949 | 811 } |
| 812 | |
| 813 void | |
| 814 gaim_status_set_attr_boolean(GaimStatus *status, const char *id, | |
| 815 gboolean value) | |
| 816 { | |
| 817 GaimStatusType *status_type; | |
| 818 GaimValue *attr_value; | |
| 819 | |
| 820 g_return_if_fail(status != NULL); | |
| 821 g_return_if_fail(id != NULL); | |
| 822 | |
| 823 status_type = gaim_status_get_type(status); | |
| 824 | |
| 10197 | 825 /* Make sure this attribute exists and is the correct type. */ |
| 826 attr_value = gaim_status_get_attr_value(status, id); | |
| 827 g_return_if_fail(attr_value != NULL); | |
| 9949 | 828 g_return_if_fail(gaim_value_get_type(attr_value) == GAIM_TYPE_BOOLEAN); |
| 829 | |
| 830 gaim_value_set_boolean(attr_value, value); | |
| 831 } | |
| 832 | |
| 833 void | |
| 834 gaim_status_set_attr_int(GaimStatus *status, const char *id, int value) | |
| 835 { | |
| 836 GaimStatusType *status_type; | |
| 837 GaimValue *attr_value; | |
| 838 | |
| 839 g_return_if_fail(status != NULL); | |
| 840 g_return_if_fail(id != NULL); | |
| 841 | |
| 842 status_type = gaim_status_get_type(status); | |
| 843 | |
| 10197 | 844 /* Make sure this attribute exists and is the correct type. */ |
| 845 attr_value = gaim_status_get_attr_value(status, id); | |
| 846 g_return_if_fail(attr_value != NULL); | |
| 9949 | 847 g_return_if_fail(gaim_value_get_type(attr_value) == GAIM_TYPE_INT); |
| 848 | |
| 849 gaim_value_set_int(attr_value, value); | |
| 6065 | 850 } |
| 851 | |
| 9949 | 852 void |
| 853 gaim_status_set_attr_string(GaimStatus *status, const char *id, | |
| 854 const char *value) | |
| 855 { | |
| 856 GaimStatusType *status_type; | |
| 857 GaimValue *attr_value; | |
| 858 | |
| 859 g_return_if_fail(status != NULL); | |
| 860 g_return_if_fail(id != NULL); | |
| 861 | |
| 862 status_type = gaim_status_get_type(status); | |
| 863 | |
| 10197 | 864 /* Make sure this attribute exists and is the correct type. */ |
| 10196 | 865 attr_value = gaim_status_get_attr_value(status, id); |
| 12434 | 866 /* This used to be g_return_if_fail, but it's failing a LOT, so |
| 867 * let's generate a log error for now. */ | |
| 868 /* g_return_if_fail(attr_value != NULL); */ | |
| 869 if (attr_value == NULL) { | |
| 870 gaim_debug_error("status", | |
| 871 "Attempted to set status attribute '%s' for " | |
| 872 "status '%s', which is not legal. Fix " | |
| 873 "this!\n", id, | |
| 874 gaim_status_type_get_name(gaim_status_get_type(status))); | |
| 875 return; | |
| 876 | |
| 877 } | |
| 9949 | 878 g_return_if_fail(gaim_value_get_type(attr_value) == GAIM_TYPE_STRING); |
| 879 | |
| 880 gaim_value_set_string(attr_value, value); | |
| 881 } | |
| 882 | |
| 883 GaimStatusType * | |
| 884 gaim_status_get_type(const GaimStatus *status) | |
| 885 { | |
| 886 g_return_val_if_fail(status != NULL, NULL); | |
| 887 | |
| 888 return status->type; | |
| 889 } | |
| 890 | |
| 891 GaimPresence * | |
| 892 gaim_status_get_presence(const GaimStatus *status) | |
| 6065 | 893 { |
| 9949 | 894 g_return_val_if_fail(status != NULL, NULL); |
| 895 | |
| 896 return status->presence; | |
| 897 } | |
| 898 | |
| 899 const char * | |
| 900 gaim_status_get_id(const GaimStatus *status) | |
| 901 { | |
| 902 g_return_val_if_fail(status != NULL, NULL); | |
| 903 | |
| 904 return gaim_status_type_get_id(gaim_status_get_type(status)); | |
| 905 } | |
| 906 | |
| 907 const char * | |
| 908 gaim_status_get_name(const GaimStatus *status) | |
| 909 { | |
| 910 g_return_val_if_fail(status != NULL, NULL); | |
| 911 | |
| 912 return gaim_status_type_get_name(gaim_status_get_type(status)); | |
| 913 } | |
| 914 | |
| 915 gboolean | |
| 916 gaim_status_is_independent(const GaimStatus *status) | |
| 917 { | |
| 918 g_return_val_if_fail(status != NULL, FALSE); | |
| 919 | |
| 920 return gaim_status_type_is_independent(gaim_status_get_type(status)); | |
| 921 } | |
| 922 | |
| 923 gboolean | |
| 10067 | 924 gaim_status_is_exclusive(const GaimStatus *status) |
| 925 { | |
| 926 g_return_val_if_fail(status != NULL, FALSE); | |
| 927 | |
| 928 return gaim_status_type_is_exclusive(gaim_status_get_type(status)); | |
| 929 } | |
| 930 | |
| 931 gboolean | |
| 9949 | 932 gaim_status_is_available(const GaimStatus *status) |
| 933 { | |
| 934 g_return_val_if_fail(status != NULL, FALSE); | |
| 935 | |
| 936 return gaim_status_type_is_available(gaim_status_get_type(status)); | |
| 937 } | |
| 6216 | 938 |
| 9949 | 939 gboolean |
| 940 gaim_status_is_active(const GaimStatus *status) | |
| 941 { | |
| 942 g_return_val_if_fail(status != NULL, FALSE); | |
| 943 | |
| 944 return status->active; | |
| 945 } | |
| 946 | |
|
10040
81059dce3aed
[gaim-migrate @ 10999]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
947 gboolean |
|
81059dce3aed
[gaim-migrate @ 10999]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
948 gaim_status_is_online(const GaimStatus *status) |
|
81059dce3aed
[gaim-migrate @ 10999]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
949 { |
|
81059dce3aed
[gaim-migrate @ 10999]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
950 GaimStatusPrimitive primitive; |
|
81059dce3aed
[gaim-migrate @ 10999]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
951 |
|
81059dce3aed
[gaim-migrate @ 10999]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
952 g_return_val_if_fail( status != NULL, FALSE); |
|
81059dce3aed
[gaim-migrate @ 10999]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
953 |
|
81059dce3aed
[gaim-migrate @ 10999]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
954 primitive = gaim_status_type_get_primitive(gaim_status_get_type(status)); |
|
81059dce3aed
[gaim-migrate @ 10999]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
955 |
|
81059dce3aed
[gaim-migrate @ 10999]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
956 return (primitive != GAIM_STATUS_UNSET && |
|
81059dce3aed
[gaim-migrate @ 10999]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
957 primitive != GAIM_STATUS_OFFLINE); |
|
81059dce3aed
[gaim-migrate @ 10999]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
958 } |
|
81059dce3aed
[gaim-migrate @ 10999]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
959 |
| 9949 | 960 GaimValue * |
| 961 gaim_status_get_attr_value(const GaimStatus *status, const char *id) | |
| 962 { | |
| 963 g_return_val_if_fail(status != NULL, NULL); | |
| 964 g_return_val_if_fail(id != NULL, NULL); | |
| 965 | |
| 966 return (GaimValue *)g_hash_table_lookup(status->attr_values, id); | |
| 967 } | |
| 968 | |
| 969 gboolean | |
| 970 gaim_status_get_attr_boolean(const GaimStatus *status, const char *id) | |
| 971 { | |
| 972 const GaimValue *value; | |
| 973 | |
| 974 g_return_val_if_fail(status != NULL, FALSE); | |
| 975 g_return_val_if_fail(id != NULL, FALSE); | |
| 976 | |
| 977 if ((value = gaim_status_get_attr_value(status, id)) == NULL) | |
| 978 return FALSE; | |
| 979 | |
| 10197 | 980 g_return_val_if_fail(gaim_value_get_type(value) == GAIM_TYPE_BOOLEAN, FALSE); |
| 9949 | 981 |
| 982 return gaim_value_get_boolean(value); | |
| 983 } | |
| 984 | |
| 985 int | |
| 986 gaim_status_get_attr_int(const GaimStatus *status, const char *id) | |
| 987 { | |
| 988 const GaimValue *value; | |
| 989 | |
| 10507 | 990 g_return_val_if_fail(status != NULL, 0); |
| 991 g_return_val_if_fail(id != NULL, 0); | |
| 9949 | 992 |
| 993 if ((value = gaim_status_get_attr_value(status, id)) == NULL) | |
| 10507 | 994 return 0; |
| 9949 | 995 |
| 996 g_return_val_if_fail(gaim_value_get_type(value) == GAIM_TYPE_INT, 0); | |
| 997 | |
| 998 return gaim_value_get_int(value); | |
| 999 } | |
| 1000 | |
| 1001 const char * | |
| 1002 gaim_status_get_attr_string(const GaimStatus *status, const char *id) | |
| 1003 { | |
| 1004 const GaimValue *value; | |
| 1005 | |
| 10507 | 1006 g_return_val_if_fail(status != NULL, NULL); |
| 1007 g_return_val_if_fail(id != NULL, NULL); | |
| 9949 | 1008 |
| 1009 if ((value = gaim_status_get_attr_value(status, id)) == NULL) | |
| 10504 | 1010 return NULL; |
| 9949 | 1011 |
| 1012 g_return_val_if_fail(gaim_value_get_type(value) == GAIM_TYPE_STRING, NULL); | |
| 1013 | |
| 1014 return gaim_value_get_string(value); | |
| 1015 } | |
| 1016 | |
| 1017 gint | |
| 1018 gaim_status_compare(const GaimStatus *status1, const GaimStatus *status2) | |
| 1019 { | |
| 1020 GaimStatusType *type1, *type2; | |
| 1021 int score1 = 0, score2 = 0; | |
| 6065 | 1022 |
| 9949 | 1023 if ((status1 == NULL && status2 == NULL) || |
| 1024 (status1 == status2)) | |
| 1025 { | |
| 1026 return 0; | |
| 1027 } | |
| 1028 else if (status1 == NULL) | |
| 1029 return 1; | |
| 1030 else if (status2 == NULL) | |
| 1031 return -1; | |
| 1032 | |
| 1033 type1 = gaim_status_get_type(status1); | |
| 1034 type2 = gaim_status_get_type(status2); | |
| 1035 | |
| 1036 if (gaim_status_is_active(status1)) | |
| 1037 score1 = primitive_scores[gaim_status_type_get_primitive(type1)]; | |
| 1038 | |
| 1039 if (gaim_status_is_active(status2)) | |
| 1040 score2 = primitive_scores[gaim_status_type_get_primitive(type2)]; | |
| 1041 | |
| 1042 if (score1 > score2) | |
| 1043 return -1; | |
| 1044 else if (score1 < score2) | |
| 1045 return 1; | |
| 1046 | |
| 1047 return 0; | |
| 1048 } | |
| 1049 | |
| 1050 | |
| 1051 /************************************************************************** | |
| 1052 * GaimPresence API | |
| 1053 **************************************************************************/ | |
| 1054 GaimPresence * | |
| 1055 gaim_presence_new(GaimPresenceContext context) | |
| 1056 { | |
| 1057 GaimPresence *presence; | |
| 1058 | |
| 1059 g_return_val_if_fail(context != GAIM_PRESENCE_CONTEXT_UNSET, NULL); | |
| 1060 | |
| 1061 presence = g_new0(GaimPresence, 1); | |
| 11187 | 1062 GAIM_DBUS_REGISTER_POINTER(presence, GaimPresence); |
| 9949 | 1063 |
| 1064 presence->context = context; | |
| 1065 | |
| 1066 presence->status_table = | |
| 10009 | 1067 g_hash_table_new_full(g_str_hash, g_str_equal, |
| 11638 | 1068 g_free, NULL); |
| 9949 | 1069 |
| 1070 return presence; | |
| 1071 } | |
| 1072 | |
| 1073 GaimPresence * | |
| 1074 gaim_presence_new_for_account(GaimAccount *account) | |
| 1075 { | |
| 10012 | 1076 GaimPresence *presence = NULL; |
| 9949 | 1077 g_return_val_if_fail(account != NULL, NULL); |
| 1078 | |
| 1079 presence = gaim_presence_new(GAIM_PRESENCE_CONTEXT_ACCOUNT); | |
| 1080 presence->u.account = account; | |
| 10006 | 1081 presence->statuses = gaim_prpl_get_statuses(account, presence); |
| 9949 | 1082 |
| 1083 return presence; | |
| 1084 } | |
| 1085 | |
| 1086 GaimPresence * | |
| 1087 gaim_presence_new_for_conv(GaimConversation *conv) | |
| 1088 { | |
| 1089 GaimPresence *presence; | |
| 1090 | |
| 1091 g_return_val_if_fail(conv != NULL, NULL); | |
| 1092 | |
| 1093 presence = gaim_presence_new(GAIM_PRESENCE_CONTEXT_CONV); | |
| 1094 presence->u.chat.conv = conv; | |
| 10006 | 1095 /* presence->statuses = gaim_prpl_get_statuses(conv->account, presence); ? */ |
| 9949 | 1096 |
| 1097 return presence; | |
| 1098 } | |
| 6216 | 1099 |
| 9949 | 1100 GaimPresence * |
| 1101 gaim_presence_new_for_buddy(GaimBuddy *buddy) | |
| 1102 { | |
| 1103 GaimPresence *presence; | |
| 1104 GaimStatusBuddyKey *key; | |
| 10006 | 1105 GaimAccount *account; |
| 9949 | 1106 |
| 1107 g_return_val_if_fail(buddy != NULL, NULL); | |
| 10012 | 1108 account = buddy->account; |
| 9949 | 1109 |
| 1110 key = g_new0(GaimStatusBuddyKey, 1); | |
| 1111 key->account = buddy->account; | |
| 1112 key->name = g_strdup(buddy->name); | |
| 10006 | 1113 |
| 1114 presence = g_hash_table_lookup(buddy_presences, key); | |
| 1115 if (presence == NULL) | |
| 9949 | 1116 { |
| 1117 presence = gaim_presence_new(GAIM_PRESENCE_CONTEXT_BUDDY); | |
| 1118 | |
| 1119 presence->u.buddy.name = g_strdup(buddy->name); | |
| 1120 presence->u.buddy.account = buddy->account; | |
| 10006 | 1121 presence->statuses = gaim_prpl_get_statuses(buddy->account, presence); |
| 9949 | 1122 |
| 1123 g_hash_table_insert(buddy_presences, key, presence); | |
| 1124 } | |
| 1125 else | |
| 1126 { | |
| 1127 g_free(key->name); | |
| 1128 g_free(key); | |
| 1129 } | |
| 1130 | |
| 1131 presence->u.buddy.ref_count++; | |
| 1132 presence->u.buddy.buddies = g_list_append(presence->u.buddy.buddies, | |
| 1133 buddy); | |
| 1134 | |
| 1135 return presence; | |
| 1136 } | |
| 1137 | |
| 1138 void | |
| 1139 gaim_presence_destroy(GaimPresence *presence) | |
| 1140 { | |
| 1141 g_return_if_fail(presence != NULL); | |
| 6216 | 1142 |
| 9949 | 1143 if (gaim_presence_get_context(presence) == GAIM_PRESENCE_CONTEXT_BUDDY) |
| 1144 { | |
| 1145 GaimStatusBuddyKey key; | |
| 1146 | |
| 10077 | 1147 if(presence->u.buddy.ref_count != 0) |
| 1148 return; | |
| 9949 | 1149 |
| 1150 key.account = presence->u.buddy.account; | |
| 1151 key.name = presence->u.buddy.name; | |
| 1152 | |
| 1153 g_hash_table_remove(buddy_presences, &key); | |
| 1154 | |
| 1155 if (presence->u.buddy.name != NULL) | |
| 1156 g_free(presence->u.buddy.name); | |
| 1157 } | |
| 1158 else if (gaim_presence_get_context(presence) == GAIM_PRESENCE_CONTEXT_CONV) | |
| 1159 { | |
| 1160 if (presence->u.chat.user != NULL) | |
| 1161 g_free(presence->u.chat.user); | |
| 1162 } | |
| 1163 | |
| 11638 | 1164 if (presence->statuses != NULL) { |
| 1165 g_list_foreach(presence->statuses, (GFunc)gaim_status_destroy, NULL); | |
| 9949 | 1166 g_list_free(presence->statuses); |
| 11638 | 1167 } |
| 9949 | 1168 |
| 1169 g_hash_table_destroy(presence->status_table); | |
| 1170 | |
| 11187 | 1171 GAIM_DBUS_UNREGISTER_POINTER(presence); |
| 9949 | 1172 g_free(presence); |
| 1173 } | |
| 1174 | |
| 10580 | 1175 /* |
| 1176 * TODO: Maybe we should cal gaim_presence_destroy() after we | |
| 1177 * decrement the ref count? I don't see why we should | |
| 1178 * make other places do it manually when we can do it here. | |
| 1179 */ | |
| 9949 | 1180 void |
| 1181 gaim_presence_remove_buddy(GaimPresence *presence, GaimBuddy *buddy) | |
| 1182 { | |
| 1183 g_return_if_fail(presence != NULL); | |
| 1184 g_return_if_fail(buddy != NULL); | |
| 1185 g_return_if_fail(gaim_presence_get_context(presence) == | |
| 1186 GAIM_PRESENCE_CONTEXT_BUDDY); | |
| 1187 | |
| 1188 if (g_list_find(presence->u.buddy.buddies, buddy) != NULL) | |
| 1189 { | |
| 1190 presence->u.buddy.buddies = g_list_remove(presence->u.buddy.buddies, | |
| 1191 buddy); | |
| 1192 presence->u.buddy.ref_count--; | |
| 1193 } | |
| 6065 | 1194 } |
| 1195 | |
| 9949 | 1196 void |
| 1197 gaim_presence_add_status(GaimPresence *presence, GaimStatus *status) | |
| 1198 { | |
| 1199 g_return_if_fail(presence != NULL); | |
| 1200 g_return_if_fail(status != NULL); | |
| 1201 | |
| 1202 presence->statuses = g_list_append(presence->statuses, status); | |
| 1203 | |
| 1204 g_hash_table_insert(presence->status_table, | |
| 1205 g_strdup(gaim_status_get_id(status)), status); | |
| 1206 } | |
| 1207 | |
| 1208 void | |
| 11318 | 1209 gaim_presence_add_list(GaimPresence *presence, const GList *source_list) |
| 9949 | 1210 { |
| 1211 const GList *l; | |
| 1212 | |
| 1213 g_return_if_fail(presence != NULL); | |
| 1214 g_return_if_fail(source_list != NULL); | |
| 1215 | |
| 1216 for (l = source_list; l != NULL; l = l->next) | |
| 1217 gaim_presence_add_status(presence, (GaimStatus *)l->data); | |
| 1218 } | |
| 1219 | |
| 1220 void | |
| 1221 gaim_presence_set_status_active(GaimPresence *presence, const char *status_id, | |
| 1222 gboolean active) | |
| 1223 { | |
| 1224 GaimStatus *status; | |
| 1225 | |
| 1226 g_return_if_fail(presence != NULL); | |
| 1227 g_return_if_fail(status_id != NULL); | |
| 1228 | |
| 1229 status = gaim_presence_get_status(presence, status_id); | |
| 1230 | |
| 1231 g_return_if_fail(status != NULL); | |
| 10348 | 1232 /* TODO: Should we do the following? */ |
| 1233 /* g_return_if_fail(active == status->active); */ | |
| 9949 | 1234 |
| 10067 | 1235 if (gaim_status_is_exclusive(status)) |
| 9949 | 1236 { |
| 1237 if (!active) | |
| 1238 { | |
| 1239 gaim_debug_warning("status", | |
| 1240 "Attempted to set a non-independent status " | |
| 1241 "(%s) inactive. Only independent statuses " | |
| 1242 "can be specifically marked inactive.", | |
| 1243 status_id); | |
| 1244 return; | |
| 1245 } | |
| 1246 } | |
| 1247 | |
| 1248 gaim_status_set_active(status, active); | |
| 1249 } | |
| 1250 | |
| 1251 void | |
| 1252 gaim_presence_switch_status(GaimPresence *presence, const char *status_id) | |
| 1253 { | |
| 10754 | 1254 gaim_presence_set_status_active(presence, status_id, TRUE); |
| 9949 | 1255 } |
| 1256 | |
| 1257 static void | |
| 1258 update_buddy_idle(GaimBuddy *buddy, GaimPresence *presence, | |
| 1259 time_t current_time, gboolean old_idle, gboolean idle) | |
| 1260 { | |
| 1261 GaimBlistUiOps *ops = gaim_get_blist()->ui_ops; | |
|
12015
5a63ea24ac83
[gaim-migrate @ 14308]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11975
diff
changeset
|
1262 GaimConversation *conv; |
| 9949 | 1263 |
| 1264 if (!old_idle && idle) | |
| 1265 { | |
| 11698 | 1266 if (gaim_prefs_get_bool("/core/logging/log_system")) |
| 9949 | 1267 { |
| 1268 GaimLog *log = gaim_account_get_log(buddy->account); | |
| 1269 char *tmp = g_strdup_printf(_("%s became idle"), | |
| 1270 gaim_buddy_get_alias(buddy)); | |
| 1271 | |
| 1272 gaim_log_write(log, GAIM_MESSAGE_SYSTEM, | |
| 1273 gaim_buddy_get_alias(buddy), current_time, tmp); | |
| 1274 g_free(tmp); | |
| 1275 } | |
| 1276 } | |
| 1277 else if (old_idle && !idle) | |
| 1278 { | |
| 11698 | 1279 if (gaim_prefs_get_bool("/core/logging/log_system")) |
| 9949 | 1280 { |
| 1281 GaimLog *log = gaim_account_get_log(buddy->account); | |
| 1282 char *tmp = g_strdup_printf(_("%s became unidle"), | |
| 1283 gaim_buddy_get_alias(buddy)); | |
| 1284 | |
| 1285 gaim_log_write(log, GAIM_MESSAGE_SYSTEM, | |
| 1286 gaim_buddy_get_alias(buddy), current_time, tmp); | |
| 1287 g_free(tmp); | |
| 1288 } | |
| 1289 } | |
| 1290 | |
|
11935
cb73483c9f63
[gaim-migrate @ 14226]
Etan Reisner <pidgin@unreliablesource.net>
parents:
11905
diff
changeset
|
1291 if (old_idle != idle) |
|
cb73483c9f63
[gaim-migrate @ 14226]
Etan Reisner <pidgin@unreliablesource.net>
parents:
11905
diff
changeset
|
1292 gaim_signal_emit(gaim_blist_get_handle(), "buddy-idle-changed", buddy, |
|
cb73483c9f63
[gaim-migrate @ 14226]
Etan Reisner <pidgin@unreliablesource.net>
parents:
11905
diff
changeset
|
1293 old_idle, idle); |
|
cb73483c9f63
[gaim-migrate @ 14226]
Etan Reisner <pidgin@unreliablesource.net>
parents:
11905
diff
changeset
|
1294 |
| 10378 | 1295 gaim_contact_invalidate_priority_buddy(gaim_buddy_get_contact(buddy)); |
| 9949 | 1296 |
|
12015
5a63ea24ac83
[gaim-migrate @ 14308]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11975
diff
changeset
|
1297 /* Should this be done here? It'd perhaps make more sense to |
|
5a63ea24ac83
[gaim-migrate @ 14308]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11975
diff
changeset
|
1298 * connect to buddy-[un]idle signals and update from there |
|
5a63ea24ac83
[gaim-migrate @ 14308]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11975
diff
changeset
|
1299 */ |
|
5a63ea24ac83
[gaim-migrate @ 14308]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11975
diff
changeset
|
1300 |
| 9949 | 1301 if (ops != NULL && ops->update != NULL) |
| 1302 ops->update(gaim_get_blist(), (GaimBlistNode *)buddy); | |
|
12015
5a63ea24ac83
[gaim-migrate @ 14308]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11975
diff
changeset
|
1303 |
|
5a63ea24ac83
[gaim-migrate @ 14308]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11975
diff
changeset
|
1304 conv = gaim_find_conversation_with_account(GAIM_CONV_TYPE_IM, |
|
5a63ea24ac83
[gaim-migrate @ 14308]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11975
diff
changeset
|
1305 gaim_buddy_get_name(buddy), |
|
5a63ea24ac83
[gaim-migrate @ 14308]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11975
diff
changeset
|
1306 gaim_buddy_get_account(buddy)); |
|
5a63ea24ac83
[gaim-migrate @ 14308]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11975
diff
changeset
|
1307 if (conv) |
|
5a63ea24ac83
[gaim-migrate @ 14308]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11975
diff
changeset
|
1308 { |
|
5a63ea24ac83
[gaim-migrate @ 14308]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11975
diff
changeset
|
1309 GaimConversationUiOps *conv_ops; |
|
5a63ea24ac83
[gaim-migrate @ 14308]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11975
diff
changeset
|
1310 conv_ops = gaim_conversation_get_ui_ops(conv); |
|
5a63ea24ac83
[gaim-migrate @ 14308]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11975
diff
changeset
|
1311 if (conv_ops && conv_ops->updated) |
|
5a63ea24ac83
[gaim-migrate @ 14308]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11975
diff
changeset
|
1312 conv_ops->updated(conv, GAIM_CONV_UPDATE_AWAY); |
|
5a63ea24ac83
[gaim-migrate @ 14308]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11975
diff
changeset
|
1313 } |
| 9949 | 1314 } |
| 1315 | |
| 1316 void | |
| 1317 gaim_presence_set_idle(GaimPresence *presence, gboolean idle, time_t idle_time) | |
| 1318 { | |
| 1319 gboolean old_idle; | |
| 1320 | |
| 1321 g_return_if_fail(presence != NULL); | |
| 1322 | |
| 1323 if (presence->idle == idle && presence->idle_time == idle_time) | |
| 1324 return; | |
| 1325 | |
| 1326 old_idle = presence->idle; | |
| 1327 presence->idle = idle; | |
| 1328 presence->idle_time = (idle ? idle_time : 0); | |
| 1329 | |
| 1330 if (gaim_presence_get_context(presence) == GAIM_PRESENCE_CONTEXT_BUDDY) | |
| 1331 { | |
| 1332 const GList *l; | |
| 1333 time_t current_time = time(NULL); | |
| 1334 | |
| 1335 for (l = gaim_presence_get_buddies(presence); l != NULL; l = l->next) | |
| 1336 { | |
| 1337 update_buddy_idle((GaimBuddy *)l->data, presence, current_time, | |
| 1338 old_idle, idle); | |
| 1339 } | |
| 1340 } | |
|
11551
78aad676fdb2
[gaim-migrate @ 13806]
Luke Schierer <lschiere@pidgin.im>
parents:
11522
diff
changeset
|
1341 else if(gaim_presence_get_context(presence) == GAIM_PRESENCE_CONTEXT_ACCOUNT) |
|
78aad676fdb2
[gaim-migrate @ 13806]
Luke Schierer <lschiere@pidgin.im>
parents:
11522
diff
changeset
|
1342 { |
| 11975 | 1343 GaimAccount *account; |
| 1344 GaimConnection *gc; | |
|
11551
78aad676fdb2
[gaim-migrate @ 13806]
Luke Schierer <lschiere@pidgin.im>
parents:
11522
diff
changeset
|
1345 GaimPluginProtocolInfo *prpl_info = NULL; |
|
78aad676fdb2
[gaim-migrate @ 13806]
Luke Schierer <lschiere@pidgin.im>
parents:
11522
diff
changeset
|
1346 |
| 11975 | 1347 account = gaim_presence_get_account(presence); |
|
12145
5bda6a03d43b
[gaim-migrate @ 14446]
Richard Laager <rlaager@wiktel.com>
parents:
12123
diff
changeset
|
1348 |
|
5bda6a03d43b
[gaim-migrate @ 14446]
Richard Laager <rlaager@wiktel.com>
parents:
12123
diff
changeset
|
1349 if (gaim_prefs_get_bool("/core/logging/log_system")) |
|
5bda6a03d43b
[gaim-migrate @ 14446]
Richard Laager <rlaager@wiktel.com>
parents:
12123
diff
changeset
|
1350 { |
|
5bda6a03d43b
[gaim-migrate @ 14446]
Richard Laager <rlaager@wiktel.com>
parents:
12123
diff
changeset
|
1351 GaimLog *log = gaim_account_get_log(account); |
|
5bda6a03d43b
[gaim-migrate @ 14446]
Richard Laager <rlaager@wiktel.com>
parents:
12123
diff
changeset
|
1352 char *msg; |
| 11975 | 1353 |
|
12145
5bda6a03d43b
[gaim-migrate @ 14446]
Richard Laager <rlaager@wiktel.com>
parents:
12123
diff
changeset
|
1354 if (idle) |
|
5bda6a03d43b
[gaim-migrate @ 14446]
Richard Laager <rlaager@wiktel.com>
parents:
12123
diff
changeset
|
1355 msg = g_strdup_printf(_("+++ %s became idle"), gaim_account_get_username(account)); |
|
5bda6a03d43b
[gaim-migrate @ 14446]
Richard Laager <rlaager@wiktel.com>
parents:
12123
diff
changeset
|
1356 else |
|
5bda6a03d43b
[gaim-migrate @ 14446]
Richard Laager <rlaager@wiktel.com>
parents:
12123
diff
changeset
|
1357 msg = g_strdup_printf(_("+++ %s became unidle"), gaim_account_get_username(account)); |
|
5bda6a03d43b
[gaim-migrate @ 14446]
Richard Laager <rlaager@wiktel.com>
parents:
12123
diff
changeset
|
1358 gaim_log_write(log, GAIM_MESSAGE_SYSTEM, |
|
5bda6a03d43b
[gaim-migrate @ 14446]
Richard Laager <rlaager@wiktel.com>
parents:
12123
diff
changeset
|
1359 gaim_account_get_username(account), |
|
5bda6a03d43b
[gaim-migrate @ 14446]
Richard Laager <rlaager@wiktel.com>
parents:
12123
diff
changeset
|
1360 idle_time, msg); |
|
5bda6a03d43b
[gaim-migrate @ 14446]
Richard Laager <rlaager@wiktel.com>
parents:
12123
diff
changeset
|
1361 g_free(msg); |
|
5bda6a03d43b
[gaim-migrate @ 14446]
Richard Laager <rlaager@wiktel.com>
parents:
12123
diff
changeset
|
1362 } |
| 11975 | 1363 |
| 1364 gc = gaim_account_get_connection(account); | |
| 1365 | |
|
11551
78aad676fdb2
[gaim-migrate @ 13806]
Luke Schierer <lschiere@pidgin.im>
parents:
11522
diff
changeset
|
1366 if (gc != NULL && gc->prpl != NULL) |
|
78aad676fdb2
[gaim-migrate @ 13806]
Luke Schierer <lschiere@pidgin.im>
parents:
11522
diff
changeset
|
1367 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); |
|
78aad676fdb2
[gaim-migrate @ 13806]
Luke Schierer <lschiere@pidgin.im>
parents:
11522
diff
changeset
|
1368 |
|
78aad676fdb2
[gaim-migrate @ 13806]
Luke Schierer <lschiere@pidgin.im>
parents:
11522
diff
changeset
|
1369 if (prpl_info && g_list_find(gaim_connections_get_all(), gc) && |
|
78aad676fdb2
[gaim-migrate @ 13806]
Luke Schierer <lschiere@pidgin.im>
parents:
11522
diff
changeset
|
1370 prpl_info->set_idle) |
|
78aad676fdb2
[gaim-migrate @ 13806]
Luke Schierer <lschiere@pidgin.im>
parents:
11522
diff
changeset
|
1371 prpl_info->set_idle(gc, time(NULL) - idle_time); |
|
78aad676fdb2
[gaim-migrate @ 13806]
Luke Schierer <lschiere@pidgin.im>
parents:
11522
diff
changeset
|
1372 } |
| 9949 | 1373 } |
| 1374 | |
| 1375 void | |
| 10006 | 1376 gaim_presence_set_login_time(GaimPresence *presence, time_t login_time) |
| 1377 { | |
| 1378 g_return_if_fail(presence != NULL); | |
| 1379 | |
| 1380 if (presence->login_time == login_time) | |
| 1381 return; | |
| 1382 | |
| 1383 presence->login_time = login_time; | |
| 1384 } | |
| 1385 | |
| 9949 | 1386 GaimPresenceContext |
| 1387 gaim_presence_get_context(const GaimPresence *presence) | |
| 1388 { | |
| 1389 g_return_val_if_fail(presence != NULL, GAIM_PRESENCE_CONTEXT_UNSET); | |
| 1390 | |
| 1391 return presence->context; | |
| 1392 } | |
| 1393 | |
| 1394 GaimAccount * | |
| 1395 gaim_presence_get_account(const GaimPresence *presence) | |
| 1396 { | |
| 1397 GaimPresenceContext context; | |
| 1398 | |
| 1399 g_return_val_if_fail(presence != NULL, NULL); | |
| 1400 | |
| 1401 context = gaim_presence_get_context(presence); | |
| 1402 | |
| 1403 g_return_val_if_fail(context == GAIM_PRESENCE_CONTEXT_ACCOUNT || | |
| 1404 context == GAIM_PRESENCE_CONTEXT_BUDDY, NULL); | |
| 1405 | |
| 1406 return presence->u.account; | |
| 1407 } | |
| 1408 | |
| 1409 GaimConversation * | |
| 1410 gaim_presence_get_conversation(const GaimPresence *presence) | |
| 1411 { | |
| 1412 g_return_val_if_fail(presence != NULL, NULL); | |
| 1413 g_return_val_if_fail(gaim_presence_get_context(presence) == | |
| 1414 GAIM_PRESENCE_CONTEXT_CONV, NULL); | |
| 1415 | |
| 1416 return presence->u.chat.conv; | |
| 1417 } | |
| 1418 | |
| 1419 const char * | |
| 1420 gaim_presence_get_chat_user(const GaimPresence *presence) | |
| 1421 { | |
| 1422 g_return_val_if_fail(presence != NULL, NULL); | |
| 1423 g_return_val_if_fail(gaim_presence_get_context(presence) == | |
| 1424 GAIM_PRESENCE_CONTEXT_CONV, NULL); | |
| 1425 | |
| 1426 return presence->u.chat.user; | |
| 1427 } | |
| 1428 | |
| 1429 const GList * | |
| 1430 gaim_presence_get_buddies(const GaimPresence *presence) | |
| 1431 { | |
| 1432 g_return_val_if_fail(presence != NULL, NULL); | |
| 1433 g_return_val_if_fail(gaim_presence_get_context(presence) == | |
| 1434 GAIM_PRESENCE_CONTEXT_BUDDY, NULL); | |
| 1435 | |
| 1436 return presence->u.buddy.buddies; | |
| 1437 } | |
| 1438 | |
| 1439 const GList * | |
| 1440 gaim_presence_get_statuses(const GaimPresence *presence) | |
| 1441 { | |
| 1442 g_return_val_if_fail(presence != NULL, NULL); | |
| 1443 | |
| 1444 return presence->statuses; | |
| 1445 } | |
| 1446 | |
| 1447 GaimStatus * | |
| 1448 gaim_presence_get_status(const GaimPresence *presence, const char *status_id) | |
| 1449 { | |
| 1450 GaimStatus *status; | |
| 10006 | 1451 const GList *l = NULL; |
| 9949 | 1452 |
| 1453 g_return_val_if_fail(presence != NULL, NULL); | |
| 1454 g_return_val_if_fail(status_id != NULL, NULL); | |
| 1455 | |
| 10006 | 1456 /* What's the purpose of this hash table? */ |
| 10012 | 1457 status = (GaimStatus *)g_hash_table_lookup(presence->status_table, |
| 10006 | 1458 status_id); |
| 10012 | 1459 |
| 10006 | 1460 if (status == NULL) { |
| 10012 | 1461 for (l = gaim_presence_get_statuses(presence); |
| 10006 | 1462 l != NULL && status == NULL; l = l->next) |
| 1463 { | |
| 1464 GaimStatus *temp_status = l->data; | |
| 10012 | 1465 |
| 10006 | 1466 if (!strcmp(status_id, gaim_status_get_id(temp_status))) |
| 1467 status = temp_status; | |
| 1468 } | |
| 1469 | |
| 1470 if (status != NULL) | |
| 1471 g_hash_table_insert(presence->status_table, | |
| 1472 g_strdup(gaim_status_get_id(status)), status); | |
| 10012 | 1473 } |
| 9949 | 1474 |
| 1475 return status; | |
| 1476 } | |
| 1477 | |
| 1478 GaimStatus * | |
| 1479 gaim_presence_get_active_status(const GaimPresence *presence) | |
| 1480 { | |
| 1481 g_return_val_if_fail(presence != NULL, NULL); | |
| 1482 | |
| 1483 return presence->active_status; | |
| 1484 } | |
| 1485 | |
| 1486 gboolean | |
| 1487 gaim_presence_is_available(const GaimPresence *presence) | |
| 1488 { | |
| 1489 GaimStatus *status; | |
| 1490 | |
| 1491 g_return_val_if_fail(presence != NULL, FALSE); | |
| 1492 | |
| 1493 status = gaim_presence_get_active_status(presence); | |
| 1494 | |
| 1495 return ((status != NULL && gaim_status_is_available(status)) && | |
| 1496 !gaim_presence_is_idle(presence)); | |
| 1497 } | |
| 1498 | |
| 1499 gboolean | |
| 1500 gaim_presence_is_online(const GaimPresence *presence) | |
| 1501 { | |
| 1502 GaimStatus *status; | |
| 1503 | |
| 1504 g_return_val_if_fail(presence != NULL, FALSE); | |
| 1505 | |
| 1506 if ((status = gaim_presence_get_active_status(presence)) == NULL) | |
| 1507 return FALSE; | |
| 1508 | |
|
10040
81059dce3aed
[gaim-migrate @ 10999]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
1509 return gaim_status_is_online(status); |
| 9949 | 1510 } |
| 1511 | |
| 1512 gboolean | |
| 1513 gaim_presence_is_status_active(const GaimPresence *presence, | |
| 1514 const char *status_id) | |
| 1515 { | |
| 1516 GaimStatus *status; | |
| 1517 | |
| 1518 g_return_val_if_fail(presence != NULL, FALSE); | |
| 1519 g_return_val_if_fail(status_id != NULL, FALSE); | |
| 1520 | |
| 1521 status = gaim_presence_get_status(presence, status_id); | |
| 1522 | |
| 1523 return (status != NULL && gaim_status_is_active(status)); | |
| 1524 } | |
| 1525 | |
| 1526 gboolean | |
| 1527 gaim_presence_is_status_primitive_active(const GaimPresence *presence, | |
| 1528 GaimStatusPrimitive primitive) | |
| 1529 { | |
| 1530 GaimStatus *status; | |
| 1531 GaimStatusType *status_type; | |
| 1532 | |
| 1533 g_return_val_if_fail(presence != NULL, FALSE); | |
| 1534 g_return_val_if_fail(primitive != GAIM_STATUS_UNSET, FALSE); | |
| 1535 | |
| 1536 status = gaim_presence_get_active_status(presence); | |
| 1537 status_type = gaim_status_get_type(status); | |
| 1538 | |
| 1539 if (gaim_status_type_get_primitive(status_type) == primitive) | |
| 1540 return TRUE; | |
| 6065 | 1541 |
| 1542 return FALSE; | |
| 1543 } | |
| 1544 | |
| 9949 | 1545 gboolean |
| 1546 gaim_presence_is_idle(const GaimPresence *presence) | |
| 6065 | 1547 { |
| 9949 | 1548 g_return_val_if_fail(presence != NULL, FALSE); |
| 1549 | |
| 11634 | 1550 return gaim_presence_is_online(presence) && presence->idle; |
| 6065 | 1551 } |
| 1552 | |
| 9949 | 1553 time_t |
| 1554 gaim_presence_get_idle_time(const GaimPresence *presence) | |
| 6065 | 1555 { |
| 9949 | 1556 g_return_val_if_fail(presence != NULL, 0); |
| 6065 | 1557 |
| 9949 | 1558 return presence->idle_time; |
| 1559 } | |
| 6065 | 1560 |
| 10567 | 1561 time_t |
| 1562 gaim_presence_get_login_time(const GaimPresence *presence) | |
| 1563 { | |
| 1564 g_return_val_if_fail(presence != NULL, 0); | |
| 1565 | |
| 11973 | 1566 return gaim_presence_is_online(presence) ? presence->login_time : 0; |
| 10567 | 1567 } |
| 1568 | |
| 9949 | 1569 gint |
| 1570 gaim_presence_compare(const GaimPresence *presence1, | |
| 1571 const GaimPresence *presence2) | |
| 6065 | 1572 { |
| 9949 | 1573 gboolean idle1, idle2; |
| 10860 | 1574 time_t idle_time_1, idle_time_2; |
| 9949 | 1575 int score1 = 0, score2 = 0; |
| 1576 const GList *l; | |
| 6065 | 1577 |
|
12164
281ab2ecc08c
[gaim-migrate @ 14465]
Richard Laager <rlaager@wiktel.com>
parents:
12145
diff
changeset
|
1578 if (presence1 == presence2) |
| 9949 | 1579 return 0; |
| 1580 else if (presence1 == NULL) | |
|
10151
d83e6f2125b1
[gaim-migrate @ 11228]
Christian Hammond <chipx86@chipx86.com>
parents:
10087
diff
changeset
|
1581 return 1; |
| 9949 | 1582 else if (presence2 == NULL) |
|
10151
d83e6f2125b1
[gaim-migrate @ 11228]
Christian Hammond <chipx86@chipx86.com>
parents:
10087
diff
changeset
|
1583 return -1; |
| 6065 | 1584 |
| 9949 | 1585 /* Compute the score of the first set of statuses. */ |
| 1586 for (l = gaim_presence_get_statuses(presence1); l != NULL; l = l->next) | |
| 1587 { | |
| 1588 GaimStatus *status = (GaimStatus *)l->data; | |
| 1589 GaimStatusType *type = gaim_status_get_type(status); | |
| 6065 | 1590 |
| 9949 | 1591 if (gaim_status_is_active(status)) |
| 1592 score1 += primitive_scores[gaim_status_type_get_primitive(type)]; | |
| 6065 | 1593 } |
|
12164
281ab2ecc08c
[gaim-migrate @ 14465]
Richard Laager <rlaager@wiktel.com>
parents:
12145
diff
changeset
|
1594 score1 += gaim_account_get_int(gaim_presence_get_account(presence1), "score", 0); |
| 6065 | 1595 |
| 9949 | 1596 /* Compute the score of the second set of statuses. */ |
|
10151
d83e6f2125b1
[gaim-migrate @ 11228]
Christian Hammond <chipx86@chipx86.com>
parents:
10087
diff
changeset
|
1597 for (l = gaim_presence_get_statuses(presence2); l != NULL; l = l->next) |
| 9949 | 1598 { |
| 1599 GaimStatus *status = (GaimStatus *)l->data; | |
| 1600 GaimStatusType *type = gaim_status_get_type(status); | |
| 6065 | 1601 |
| 9949 | 1602 if (gaim_status_is_active(status)) |
| 1603 score2 += primitive_scores[gaim_status_type_get_primitive(type)]; | |
| 6065 | 1604 } |
|
12164
281ab2ecc08c
[gaim-migrate @ 14465]
Richard Laager <rlaager@wiktel.com>
parents:
12145
diff
changeset
|
1605 score2 += gaim_account_get_int(gaim_presence_get_account(presence2), "score", 0); |
| 6065 | 1606 |
| 9949 | 1607 idle1 = gaim_presence_is_idle(presence1); |
| 1608 idle2 = gaim_presence_is_idle(presence2); | |
| 6065 | 1609 |
| 9949 | 1610 if (idle1) |
| 1611 score1 += primitive_scores[SCORE_IDLE]; | |
| 6065 | 1612 |
| 9949 | 1613 if (idle2) |
| 1614 score2 += primitive_scores[SCORE_IDLE]; | |
| 6065 | 1615 |
| 10860 | 1616 idle_time_1 = time(NULL) - gaim_presence_get_idle_time(presence1); |
| 1617 idle_time_2 = time(NULL) - gaim_presence_get_idle_time(presence2); | |
| 6065 | 1618 |
| 9949 | 1619 if (idle_time_1 > idle_time_2) |
| 1620 score1 += primitive_scores[SCORE_IDLE_TIME]; | |
| 1621 else if (idle_time_1 < idle_time_2) | |
| 1622 score2 += primitive_scores[SCORE_IDLE_TIME]; | |
| 6065 | 1623 |
| 9949 | 1624 if (score1 < score2) |
| 1625 return 1; | |
| 1626 else if (score1 > score2) | |
| 1627 return -1; | |
| 1628 | |
| 1629 return 0; | |
| 1630 } | |
| 1631 | |
| 6065 | 1632 |
| 9949 | 1633 /************************************************************************** |
| 1634 * Status subsystem | |
| 1635 **************************************************************************/ | |
| 1636 static void | |
| 1637 score_pref_changed_cb(const char *name, GaimPrefType type, gpointer value, | |
| 1638 gpointer data) | |
| 1639 { | |
| 1640 int index = GPOINTER_TO_INT(data); | |
| 6065 | 1641 |
| 9949 | 1642 primitive_scores[index] = GPOINTER_TO_INT(value); |
| 6065 | 1643 } |
| 1644 | |
| 10519 | 1645 static guint |
| 10006 | 1646 gaim_buddy_presences_hash(gconstpointer key) |
| 1647 { | |
| 10012 | 1648 const GaimStatusBuddyKey *me = key; |
| 1649 guint ret; | |
| 1650 char *str; | |
| 1651 | |
| 1652 str = g_strdup_printf("%p%s", me->account, me->name); | |
| 1653 ret = g_str_hash(str); | |
| 1654 g_free(str); | |
| 1655 | |
| 1656 return ret; | |
| 10006 | 1657 } |
| 1658 | |
| 10519 | 1659 static gboolean |
| 10006 | 1660 gaim_buddy_presences_equal(gconstpointer a, gconstpointer b) |
| 1661 { | |
| 1662 GaimStatusBuddyKey *key_a = (GaimStatusBuddyKey *)a; | |
| 1663 GaimStatusBuddyKey *key_b = (GaimStatusBuddyKey *)b; | |
| 1664 | |
| 10012 | 1665 if(key_a->account == key_b->account && |
| 1666 !strcmp(key_a->name, key_b->name)) | |
| 10006 | 1667 return TRUE; |
| 1668 else | |
| 1669 return FALSE; | |
| 1670 } | |
| 1671 | |
| 10519 | 1672 static void |
| 1673 gaim_buddy_presences_key_free(gpointer a) | |
| 1674 { | |
| 1675 GaimStatusBuddyKey *key = (GaimStatusBuddyKey *)a; | |
| 1676 g_free(key->name); | |
| 1677 g_free(key); | |
| 1678 } | |
| 1679 | |
| 10087 | 1680 void * |
| 10418 | 1681 gaim_status_get_handle(void) { |
| 10087 | 1682 static int handle; |
| 1683 | |
| 1684 return &handle; | |
| 1685 } | |
| 1686 | |
| 9949 | 1687 void |
| 10418 | 1688 gaim_status_init(void) |
| 6065 | 1689 { |
| 10418 | 1690 void *handle = gaim_status_get_handle; |
| 10087 | 1691 |
| 9949 | 1692 gaim_prefs_add_none("/core/status"); |
| 1693 gaim_prefs_add_none("/core/status/scores"); | |
| 6065 | 1694 |
| 9949 | 1695 gaim_prefs_add_int("/core/status/scores/offline", |
| 1696 primitive_scores[GAIM_STATUS_OFFLINE]); | |
| 1697 gaim_prefs_add_int("/core/status/scores/available", | |
| 1698 primitive_scores[GAIM_STATUS_AVAILABLE]); | |
| 1699 gaim_prefs_add_int("/core/status/scores/hidden", | |
| 1700 primitive_scores[GAIM_STATUS_HIDDEN]); | |
| 1701 gaim_prefs_add_int("/core/status/scores/away", | |
| 1702 primitive_scores[GAIM_STATUS_AWAY]); | |
| 1703 gaim_prefs_add_int("/core/status/scores/extended_away", | |
| 1704 primitive_scores[GAIM_STATUS_EXTENDED_AWAY]); | |
| 1705 gaim_prefs_add_int("/core/status/scores/idle", | |
| 1706 primitive_scores[SCORE_IDLE]); | |
| 6065 | 1707 |
| 10087 | 1708 gaim_prefs_connect_callback(handle, "/core/status/scores/offline", |
| 9949 | 1709 score_pref_changed_cb, |
| 1710 GINT_TO_POINTER(GAIM_STATUS_OFFLINE)); | |
| 10087 | 1711 gaim_prefs_connect_callback(handle, "/core/status/scores/available", |
| 9949 | 1712 score_pref_changed_cb, |
| 1713 GINT_TO_POINTER(GAIM_STATUS_AVAILABLE)); | |
| 10087 | 1714 gaim_prefs_connect_callback(handle, "/core/status/scores/hidden", |
| 9949 | 1715 score_pref_changed_cb, |
| 1716 GINT_TO_POINTER(GAIM_STATUS_HIDDEN)); | |
| 10087 | 1717 gaim_prefs_connect_callback(handle, "/core/status/scores/away", |
| 9949 | 1718 score_pref_changed_cb, |
| 1719 GINT_TO_POINTER(GAIM_STATUS_AWAY)); | |
| 10087 | 1720 gaim_prefs_connect_callback(handle, "/core/status/scores/extended_away", |
| 9949 | 1721 score_pref_changed_cb, |
| 1722 GINT_TO_POINTER(GAIM_STATUS_EXTENDED_AWAY)); | |
| 10087 | 1723 gaim_prefs_connect_callback(handle, "/core/status/scores/idle", |
| 9949 | 1724 score_pref_changed_cb, |
| 1725 GINT_TO_POINTER(SCORE_IDLE)); | |
| 10006 | 1726 |
| 10519 | 1727 buddy_presences = g_hash_table_new_full(gaim_buddy_presences_hash, |
| 1728 gaim_buddy_presences_equal, | |
| 1729 gaim_buddy_presences_key_free, NULL); | |
| 9949 | 1730 } |
| 6065 | 1731 |
| 9949 | 1732 void |
| 10418 | 1733 gaim_status_uninit(void) |
| 9949 | 1734 { |
|
10176
0109f3a518d2
[gaim-migrate @ 11291]
Christian Hammond <chipx86@chipx86.com>
parents:
10153
diff
changeset
|
1735 if (buddy_presences != NULL) |
|
0109f3a518d2
[gaim-migrate @ 11291]
Christian Hammond <chipx86@chipx86.com>
parents:
10153
diff
changeset
|
1736 { |
| 10077 | 1737 g_hash_table_destroy(buddy_presences); |
|
10176
0109f3a518d2
[gaim-migrate @ 11291]
Christian Hammond <chipx86@chipx86.com>
parents:
10153
diff
changeset
|
1738 |
|
0109f3a518d2
[gaim-migrate @ 11291]
Christian Hammond <chipx86@chipx86.com>
parents:
10153
diff
changeset
|
1739 buddy_presences = NULL; |
|
0109f3a518d2
[gaim-migrate @ 11291]
Christian Hammond <chipx86@chipx86.com>
parents:
10153
diff
changeset
|
1740 } |
| 9949 | 1741 } |
