Mercurial > pidgin.yaz
annotate src/status.c @ 10009:c124bef68132
[gaim-migrate @ 10926]
Thou shalt NULL terminate va_lists!
This fixes the crash-at-startup bug I mentioned in my commit
of Dave West's stuff last night.
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Fri, 10 Sep 2004 19:51:52 +0000 |
parents | 9baedaca25c7 |
children | 56e34a659db2 |
rev | line source |
---|---|
9944 | 1 /** |
2 * @file status.h Status API | |
3 * @ingroup core | |
4 * | |
6065 | 5 * gaim |
6 * | |
8046 | 7 * Gaim is the legal property of its developers, whose names are too numerous |
8 * to list here. Please refer to the COPYRIGHT file distributed with this | |
9 * source distribution. | |
9944 | 10 * |
6065 | 11 * This program is free software; you can redistribute it and/or modify |
12 * it under the terms of the GNU General Public License as published by | |
13 * the Free Software Foundation; either version 2 of the License, or | |
14 * (at your option) any later version. | |
15 * | |
16 * This program is distributed in the hope that it will be useful, | |
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 * GNU General Public License for more details. | |
20 * | |
21 * You should have received a copy of the GNU General Public License | |
22 * along with this program; if not, write to the Free Software | |
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
24 */ | |
9949 | 25 #include "internal.h" |
6065 | 26 |
9949 | 27 #include "blist.h" |
28 #include "debug.h" | |
29 #include "prefs.h" | |
6065 | 30 #include "status.h" |
31 #include "util.h" | |
32 | |
9949 | 33 /** |
34 * A type of status. | |
35 */ | |
36 struct _GaimStatusType | |
37 { | |
38 GaimStatusPrimitive primitive; | |
6371
8f94cce8faa5
[gaim-migrate @ 6876]
Christian Hammond <chipx86@chipx86.com>
parents:
6321
diff
changeset
|
39 |
9949 | 40 char *id; |
41 char *name; | |
42 char *primary_attr_id; | |
43 | |
44 gboolean saveable; | |
45 gboolean user_settable; | |
46 gboolean independent; | |
47 | |
48 GList *attrs; | |
49 }; | |
6371
8f94cce8faa5
[gaim-migrate @ 6876]
Christian Hammond <chipx86@chipx86.com>
parents:
6321
diff
changeset
|
50 |
9949 | 51 /** |
52 * A status attribute. | |
53 */ | |
54 struct _GaimStatusAttr | |
55 { | |
56 char *id; | |
57 char *name; | |
58 GaimValue *value_type; | |
59 }; | |
6065 | 60 |
9949 | 61 /** |
62 * A list of statuses. | |
63 */ | |
64 struct _GaimPresence | |
65 { | |
66 GaimPresenceContext context; | |
67 | |
68 gboolean idle; | |
69 time_t idle_time; | |
10006 | 70 time_t login_time; |
9949 | 71 |
72 unsigned int warning_level; | |
6065 | 73 |
9949 | 74 GList *statuses; |
75 GHashTable *status_table; | |
76 | |
77 GaimStatus *active_status; | |
6065 | 78 |
9949 | 79 union |
80 { | |
81 GaimAccount *account; | |
82 | |
83 struct | |
84 { | |
85 GaimConversation *conv; | |
86 char *user; | |
87 | |
88 } chat; | |
6065 | 89 |
9949 | 90 struct |
91 { | |
92 GaimAccount *account; | |
93 char *name; | |
94 size_t ref_count; | |
95 GList *buddies; | |
96 | |
97 } buddy; | |
98 | |
99 } u; | |
100 }; | |
101 | |
102 /** | |
103 * An active status. | |
104 */ | |
105 struct _GaimStatus | |
6065 | 106 { |
9949 | 107 GaimStatusType *type; |
108 GaimPresence *presence; | |
109 | |
110 const char *title; | |
6065 | 111 |
9949 | 112 gboolean active; |
6065 | 113 |
9949 | 114 GHashTable *attr_values; |
115 }; | |
6065 | 116 |
117 typedef struct | |
118 { | |
9949 | 119 GaimAccount *account; |
120 char *name; | |
10006 | 121 guint id; |
9949 | 122 |
123 } GaimStatusBuddyKey; | |
124 | |
125 | |
126 #if 0 | |
127 static GList *stored_statuses = NULL; | |
128 | |
129 /* | |
130 * XXX This stuff should be removed in a few versions. It stores the | |
131 * old v1 status stuff so we can write it later. We don't write out | |
132 * the new status stuff, though. These should all die soon, as the | |
133 * old status.xml was created before the new status system's design | |
134 * was created. | |
135 * | |
136 * -- ChipX86 | |
137 */ | |
138 typedef struct | |
139 { | |
140 char *name; | |
141 char *state; | |
142 char *message; | |
143 | |
144 } GaimStatusV1Info; | |
145 | |
146 static GList *v1_statuses = NULL; | |
147 #endif | |
148 | |
149 | |
150 static int primitive_scores[] = | |
151 { | |
152 0, /* unset */ | |
153 -500, /* offline */ | |
154 0, /* online */ | |
155 100, /* available */ | |
156 -75, /* unavailable */ | |
157 -50, /* hidden */ | |
158 -100, /* away */ | |
159 -200 /* extended away */ | |
160 -10, /* idle, special case. */ | |
161 -5 /* idle time, special case. */ | |
162 }; | |
163 | |
164 static GHashTable *buddy_presences = NULL; | |
165 | |
166 #define SCORE_IDLE 5 | |
167 #define SCORE_IDLE_TIME 6 | |
168 | |
169 /************************************************************************** | |
170 * GaimStatusType API | |
171 **************************************************************************/ | |
172 GaimStatusType * | |
173 gaim_status_type_new_full(GaimStatusPrimitive primitive, const char *id, | |
10009 | 174 const char *name, gboolean saveable, |
175 gboolean user_settable, gboolean independent) | |
9949 | 176 { |
177 GaimStatusType *status_type; | |
178 | |
179 g_return_val_if_fail(primitive != GAIM_STATUS_UNSET, NULL); | |
180 g_return_val_if_fail(id != NULL, NULL); | |
181 g_return_val_if_fail(name != NULL, NULL); | |
182 | |
183 status_type = g_new0(GaimStatusType, 1); | |
184 | |
185 status_type->primitive = primitive; | |
186 status_type->id = g_strdup(id); | |
187 status_type->name = g_strdup(name); | |
188 status_type->saveable = saveable; | |
189 status_type->user_settable = user_settable; | |
190 status_type->independent = independent; | |
191 | |
192 return status_type; | |
193 } | |
194 | |
195 GaimStatusType * | |
196 gaim_status_type_new(GaimStatusPrimitive primitive, const char *id, | |
10009 | 197 const char *name, gboolean user_settable) |
9949 | 198 { |
199 g_return_val_if_fail(primitive != GAIM_STATUS_UNSET, NULL); | |
200 g_return_val_if_fail(id != NULL, NULL); | |
201 g_return_val_if_fail(name != NULL, NULL); | |
202 | |
203 return gaim_status_type_new_full(primitive, id, name, FALSE, | |
204 user_settable, FALSE); | |
205 } | |
206 | |
207 GaimStatusType * | |
208 gaim_status_type_new_with_attrs(GaimStatusPrimitive primitive, | |
209 const char *id, const char *name, | |
210 gboolean saveable, gboolean user_settable, | |
211 gboolean independent, const char *attr_id, | |
212 const char *attr_name, GaimValue *attr_value, | |
213 ...) | |
214 { | |
215 GaimStatusType *status_type; | |
216 va_list args; | |
217 | |
218 g_return_val_if_fail(primitive != GAIM_STATUS_UNSET, NULL); | |
219 g_return_val_if_fail(id != NULL, NULL); | |
220 g_return_val_if_fail(name != NULL, NULL); | |
10006 | 221 g_return_val_if_fail(attr_id != NULL, NULL); |
9949 | 222 g_return_val_if_fail(attr_name != NULL, NULL); |
223 g_return_val_if_fail(attr_value != NULL, NULL); | |
224 | |
225 status_type = gaim_status_type_new_full(primitive, id, name, saveable, | |
226 user_settable, independent); | |
227 | |
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 = | |
10009 | 908 g_hash_table_new_full(g_str_hash, g_str_equal, |
909 g_free, (GFreeFunc)gaim_status_destroy); | |
9949 | 910 |
911 return presence; | |
912 } | |
913 | |
914 GaimPresence * | |
915 gaim_presence_new_for_account(GaimAccount *account) | |
916 { | |
917 GaimPresence *presence; | |
10009 | 918 |
9949 | 919 g_return_val_if_fail(account != NULL, NULL); |
920 | |
921 presence = gaim_presence_new(GAIM_PRESENCE_CONTEXT_ACCOUNT); | |
922 presence->u.account = account; | |
10006 | 923 presence->statuses = gaim_prpl_get_statuses(account, presence); |
9949 | 924 |
925 return presence; | |
926 } | |
927 | |
928 GaimPresence * | |
929 gaim_presence_new_for_conv(GaimConversation *conv) | |
930 { | |
931 GaimPresence *presence; | |
932 | |
933 g_return_val_if_fail(conv != NULL, NULL); | |
934 | |
935 presence = gaim_presence_new(GAIM_PRESENCE_CONTEXT_CONV); | |
936 presence->u.chat.conv = conv; | |
10006 | 937 /* presence->statuses = gaim_prpl_get_statuses(conv->account, presence); ? */ |
9949 | 938 |
939 return presence; | |
940 } | |
6216 | 941 |
9949 | 942 GaimPresence * |
943 gaim_presence_new_for_buddy(GaimBuddy *buddy) | |
944 { | |
945 GaimPresence *presence; | |
946 GaimStatusBuddyKey *key; | |
10006 | 947 GaimAccount *account; |
948 gchar *hash_seed; | |
9949 | 949 |
950 g_return_val_if_fail(buddy != NULL, NULL); | |
951 | |
10006 | 952 account = buddy->account; |
953 hash_seed = g_strdup_printf("%s:%s:%s", buddy->name, account->username, | |
954 account->protocol_id); | |
955 | |
9949 | 956 key = g_new0(GaimStatusBuddyKey, 1); |
957 key->account = buddy->account; | |
958 key->name = g_strdup(buddy->name); | |
10006 | 959 key->id = g_str_hash(hash_seed); |
9949 | 960 |
10006 | 961 g_free(hash_seed); |
962 hash_seed = NULL; | |
963 | |
964 presence = g_hash_table_lookup(buddy_presences, key); | |
965 if (presence == NULL) | |
9949 | 966 { |
967 presence = gaim_presence_new(GAIM_PRESENCE_CONTEXT_BUDDY); | |
968 | |
969 presence->u.buddy.name = g_strdup(buddy->name); | |
970 presence->u.buddy.account = buddy->account; | |
10006 | 971 presence->statuses = gaim_prpl_get_statuses(buddy->account, presence); |
9949 | 972 |
973 g_hash_table_insert(buddy_presences, key, presence); | |
974 } | |
975 else | |
976 { | |
977 g_free(key->name); | |
978 g_free(key); | |
979 } | |
980 | |
981 presence->u.buddy.ref_count++; | |
982 presence->u.buddy.buddies = g_list_append(presence->u.buddy.buddies, | |
983 buddy); | |
984 | |
985 return presence; | |
986 } | |
987 | |
988 void | |
989 gaim_presence_destroy(GaimPresence *presence) | |
990 { | |
991 g_return_if_fail(presence != NULL); | |
6216 | 992 |
9949 | 993 if (gaim_presence_get_context(presence) == GAIM_PRESENCE_CONTEXT_BUDDY) |
994 { | |
995 GaimStatusBuddyKey key; | |
996 | |
997 presence->u.buddy.ref_count--; | |
998 | |
999 g_return_if_fail(presence->u.buddy.ref_count == 0); | |
1000 | |
1001 key.account = presence->u.buddy.account; | |
1002 key.name = presence->u.buddy.name; | |
1003 | |
1004 g_hash_table_remove(buddy_presences, &key); | |
1005 | |
1006 if (presence->u.buddy.name != NULL) | |
1007 g_free(presence->u.buddy.name); | |
1008 } | |
1009 else if (gaim_presence_get_context(presence) == GAIM_PRESENCE_CONTEXT_CONV) | |
1010 { | |
1011 if (presence->u.chat.user != NULL) | |
1012 g_free(presence->u.chat.user); | |
1013 } | |
1014 | |
10006 | 1015 if (buddy_presences != NULL) |
1016 g_hash_table_destroy(buddy_presences); | |
1017 | |
9949 | 1018 if (presence->statuses != NULL) |
1019 g_list_free(presence->statuses); | |
1020 | |
1021 g_hash_table_destroy(presence->status_table); | |
1022 | |
1023 g_free(presence); | |
1024 } | |
1025 | |
1026 void | |
1027 gaim_presence_remove_buddy(GaimPresence *presence, GaimBuddy *buddy) | |
1028 { | |
1029 g_return_if_fail(presence != NULL); | |
1030 g_return_if_fail(buddy != NULL); | |
1031 g_return_if_fail(gaim_presence_get_context(presence) == | |
1032 GAIM_PRESENCE_CONTEXT_BUDDY); | |
1033 | |
1034 if (g_list_find(presence->u.buddy.buddies, buddy) != NULL) | |
1035 { | |
1036 presence->u.buddy.buddies = g_list_remove(presence->u.buddy.buddies, | |
1037 buddy); | |
1038 presence->u.buddy.ref_count--; | |
1039 } | |
6065 | 1040 } |
1041 | |
9949 | 1042 void |
1043 gaim_presence_add_status(GaimPresence *presence, GaimStatus *status) | |
1044 { | |
1045 g_return_if_fail(presence != NULL); | |
1046 g_return_if_fail(status != NULL); | |
1047 | |
1048 presence->statuses = g_list_append(presence->statuses, status); | |
1049 | |
1050 g_hash_table_insert(presence->status_table, | |
1051 g_strdup(gaim_status_get_id(status)), status); | |
1052 } | |
1053 | |
1054 void | |
1055 gaim_presence_add_presence(GaimPresence *presence, const GList *source_list) | |
1056 { | |
1057 const GList *l; | |
1058 | |
1059 g_return_if_fail(presence != NULL); | |
1060 g_return_if_fail(source_list != NULL); | |
1061 | |
1062 for (l = source_list; l != NULL; l = l->next) | |
1063 gaim_presence_add_status(presence, (GaimStatus *)l->data); | |
1064 } | |
1065 | |
1066 void | |
1067 gaim_presence_set_status_active(GaimPresence *presence, const char *status_id, | |
1068 gboolean active) | |
1069 { | |
1070 GaimStatus *status; | |
1071 | |
1072 g_return_if_fail(presence != NULL); | |
1073 g_return_if_fail(status_id != NULL); | |
1074 | |
1075 status = gaim_presence_get_status(presence, status_id); | |
1076 | |
1077 g_return_if_fail(status != NULL); | |
1078 | |
1079 if (!gaim_status_is_independent(status)) | |
1080 { | |
1081 if (!active) | |
1082 { | |
1083 gaim_debug_warning("status", | |
1084 "Attempted to set a non-independent status " | |
1085 "(%s) inactive. Only independent statuses " | |
1086 "can be specifically marked inactive.", | |
1087 status_id); | |
1088 | |
1089 return; | |
1090 } | |
1091 | |
1092 if (presence->active_status != NULL) | |
1093 gaim_status_set_active(presence->active_status, FALSE); | |
1094 | |
1095 presence->active_status = status; | |
1096 } | |
1097 | |
1098 gaim_status_set_active(status, active); | |
1099 } | |
1100 | |
1101 void | |
1102 gaim_presence_switch_status(GaimPresence *presence, const char *status_id) | |
1103 { | |
1104 GaimStatus *status; | |
1105 | |
1106 g_return_if_fail(presence != NULL); | |
1107 g_return_if_fail(status_id != NULL); | |
1108 | |
1109 status = gaim_presence_get_status(presence, status_id); | |
1110 | |
1111 g_return_if_fail(status != NULL); | |
1112 | |
1113 if (gaim_status_is_independent(status)) | |
1114 return; | |
1115 | |
1116 if (presence->active_status != NULL) | |
1117 gaim_status_set_active(presence->active_status, FALSE); | |
1118 | |
1119 gaim_status_set_active(status, TRUE); | |
1120 } | |
1121 | |
1122 static void | |
1123 update_buddy_idle(GaimBuddy *buddy, GaimPresence *presence, | |
1124 time_t current_time, gboolean old_idle, gboolean idle) | |
1125 { | |
1126 GaimBlistUiOps *ops = gaim_get_blist()->ui_ops; | |
1127 | |
1128 if (!old_idle && idle) | |
1129 { | |
1130 gaim_signal_emit(gaim_blist_get_handle(), "buddy-idle", buddy); | |
1131 | |
1132 if (gaim_prefs_get_bool("/core/logging/log_system") && | |
1133 gaim_prefs_get_bool("/core/logging/log_idle_state")) | |
1134 { | |
1135 GaimLog *log = gaim_account_get_log(buddy->account); | |
1136 char *tmp = g_strdup_printf(_("%s became idle"), | |
1137 gaim_buddy_get_alias(buddy)); | |
1138 | |
1139 gaim_log_write(log, GAIM_MESSAGE_SYSTEM, | |
1140 gaim_buddy_get_alias(buddy), current_time, tmp); | |
1141 g_free(tmp); | |
1142 } | |
1143 } | |
1144 else if (old_idle && !idle) | |
1145 { | |
1146 gaim_signal_emit(gaim_blist_get_handle(), "buddy-unidle", buddy); | |
1147 | |
1148 if (gaim_prefs_get_bool("/core/logging/log_system") && | |
1149 gaim_prefs_get_bool("/core/logging/log_idle_state")) | |
1150 { | |
1151 GaimLog *log = gaim_account_get_log(buddy->account); | |
1152 char *tmp = g_strdup_printf(_("%s became unidle"), | |
1153 gaim_buddy_get_alias(buddy)); | |
1154 | |
1155 gaim_log_write(log, GAIM_MESSAGE_SYSTEM, | |
1156 gaim_buddy_get_alias(buddy), current_time, tmp); | |
1157 g_free(tmp); | |
1158 } | |
1159 } | |
1160 | |
1161 gaim_contact_compute_priority_buddy(gaim_buddy_get_contact(buddy)); | |
1162 | |
1163 if (ops != NULL && ops->update != NULL) | |
1164 ops->update(gaim_get_blist(), (GaimBlistNode *)buddy); | |
1165 } | |
1166 | |
1167 void | |
1168 gaim_presence_set_idle(GaimPresence *presence, gboolean idle, time_t idle_time) | |
1169 { | |
1170 gboolean old_idle; | |
1171 | |
1172 g_return_if_fail(presence != NULL); | |
1173 | |
1174 if (presence->idle == idle && presence->idle_time == idle_time) | |
1175 return; | |
1176 | |
1177 old_idle = presence->idle; | |
1178 presence->idle = idle; | |
1179 presence->idle_time = (idle ? idle_time : 0); | |
1180 | |
1181 if (gaim_presence_get_context(presence) == GAIM_PRESENCE_CONTEXT_BUDDY) | |
1182 { | |
1183 const GList *l; | |
1184 time_t current_time = time(NULL); | |
1185 | |
1186 for (l = gaim_presence_get_buddies(presence); l != NULL; l = l->next) | |
1187 { | |
1188 update_buddy_idle((GaimBuddy *)l->data, presence, current_time, | |
1189 old_idle, idle); | |
1190 } | |
1191 } | |
1192 } | |
1193 | |
1194 void | |
10006 | 1195 gaim_presence_set_login_time(GaimPresence *presence, time_t login_time) |
1196 { | |
1197 g_return_if_fail(presence != NULL); | |
1198 | |
1199 if (presence->login_time == login_time) | |
1200 return; | |
1201 | |
1202 presence->login_time = login_time; | |
1203 } | |
1204 | |
1205 void | |
9949 | 1206 gaim_presence_set_warning_level(GaimPresence *presence, unsigned int level) |
6065 | 1207 { |
9949 | 1208 g_return_if_fail(presence != NULL); |
1209 g_return_if_fail(level <= 100); | |
1210 | |
1211 if (presence->warning_level == level) | |
1212 return; | |
1213 | |
1214 presence->warning_level = level; | |
1215 | |
1216 if (gaim_presence_get_context(presence) == GAIM_PRESENCE_CONTEXT_BUDDY) | |
1217 { | |
1218 GaimBlistUiOps *ops = gaim_get_blist()->ui_ops; | |
1219 | |
1220 if (ops != NULL && ops->update != NULL) | |
1221 { | |
1222 const GList *l; | |
1223 | |
1224 for (l = gaim_presence_get_buddies(presence); | |
1225 l != NULL; | |
1226 l = l->next) | |
1227 { | |
1228 ops->update(gaim_get_blist(), (GaimBlistNode *)l->data); | |
1229 } | |
1230 } | |
1231 } | |
1232 } | |
1233 | |
1234 GaimPresenceContext | |
1235 gaim_presence_get_context(const GaimPresence *presence) | |
1236 { | |
1237 g_return_val_if_fail(presence != NULL, GAIM_PRESENCE_CONTEXT_UNSET); | |
1238 | |
1239 return presence->context; | |
1240 } | |
1241 | |
1242 GaimAccount * | |
1243 gaim_presence_get_account(const GaimPresence *presence) | |
1244 { | |
1245 GaimPresenceContext context; | |
1246 | |
1247 g_return_val_if_fail(presence != NULL, NULL); | |
1248 | |
1249 context = gaim_presence_get_context(presence); | |
1250 | |
1251 g_return_val_if_fail(context == GAIM_PRESENCE_CONTEXT_ACCOUNT || | |
1252 context == GAIM_PRESENCE_CONTEXT_BUDDY, NULL); | |
1253 | |
1254 return presence->u.account; | |
1255 } | |
1256 | |
1257 GaimConversation * | |
1258 gaim_presence_get_conversation(const GaimPresence *presence) | |
1259 { | |
1260 g_return_val_if_fail(presence != NULL, NULL); | |
1261 g_return_val_if_fail(gaim_presence_get_context(presence) == | |
1262 GAIM_PRESENCE_CONTEXT_CONV, NULL); | |
1263 | |
1264 return presence->u.chat.conv; | |
1265 } | |
1266 | |
1267 const char * | |
1268 gaim_presence_get_chat_user(const GaimPresence *presence) | |
1269 { | |
1270 g_return_val_if_fail(presence != NULL, NULL); | |
1271 g_return_val_if_fail(gaim_presence_get_context(presence) == | |
1272 GAIM_PRESENCE_CONTEXT_CONV, NULL); | |
1273 | |
1274 return presence->u.chat.user; | |
1275 } | |
1276 | |
1277 const GList * | |
1278 gaim_presence_get_buddies(const GaimPresence *presence) | |
1279 { | |
1280 g_return_val_if_fail(presence != NULL, NULL); | |
1281 g_return_val_if_fail(gaim_presence_get_context(presence) == | |
1282 GAIM_PRESENCE_CONTEXT_BUDDY, NULL); | |
1283 | |
1284 return presence->u.buddy.buddies; | |
1285 } | |
1286 | |
1287 const GList * | |
1288 gaim_presence_get_statuses(const GaimPresence *presence) | |
1289 { | |
1290 g_return_val_if_fail(presence != NULL, NULL); | |
1291 | |
1292 return presence->statuses; | |
1293 } | |
1294 | |
1295 GaimStatus * | |
1296 gaim_presence_get_status(const GaimPresence *presence, const char *status_id) | |
1297 { | |
1298 GaimStatus *status; | |
10006 | 1299 const GList *l = NULL; |
9949 | 1300 |
1301 g_return_val_if_fail(presence != NULL, NULL); | |
1302 g_return_val_if_fail(status_id != NULL, NULL); | |
1303 | |
10006 | 1304 /* What's the purpose of this hash table? */ |
1305 status = (GaimStatus *)g_hash_table_lookup(presence->status_table, | |
1306 status_id); | |
1307 | |
1308 if (status == NULL) { | |
1309 for (l = gaim_presence_get_statuses(presence); | |
1310 l != NULL && status == NULL; l = l->next) | |
1311 { | |
1312 GaimStatus *temp_status = l->data; | |
1313 | |
1314 if (!strcmp(status_id, gaim_status_get_id(temp_status))) | |
1315 status = temp_status; | |
1316 } | |
1317 | |
1318 if (status != NULL) | |
1319 g_hash_table_insert(presence->status_table, | |
1320 g_strdup(gaim_status_get_id(status)), status); | |
1321 } | |
9949 | 1322 |
1323 return status; | |
1324 } | |
1325 | |
1326 GaimStatus * | |
1327 gaim_presence_get_active_status(const GaimPresence *presence) | |
1328 { | |
1329 g_return_val_if_fail(presence != NULL, NULL); | |
1330 | |
1331 return presence->active_status; | |
1332 } | |
1333 | |
1334 gboolean | |
1335 gaim_presence_is_available(const GaimPresence *presence) | |
1336 { | |
1337 GaimStatus *status; | |
1338 | |
1339 g_return_val_if_fail(presence != NULL, FALSE); | |
1340 | |
1341 status = gaim_presence_get_active_status(presence); | |
1342 | |
1343 return ((status != NULL && gaim_status_is_available(status)) && | |
1344 !gaim_presence_is_idle(presence)); | |
1345 } | |
1346 | |
1347 gboolean | |
1348 gaim_presence_is_online(const GaimPresence *presence) | |
1349 { | |
1350 GaimStatus *status; | |
1351 GaimStatusPrimitive primitive; | |
1352 | |
1353 g_return_val_if_fail(presence != NULL, FALSE); | |
1354 | |
1355 if ((status = gaim_presence_get_active_status(presence)) == NULL) | |
1356 return FALSE; | |
1357 | |
1358 primitive = gaim_status_type_get_primitive(gaim_status_get_type(status)); | |
1359 | |
1360 return (primitive != GAIM_STATUS_UNSET && | |
1361 primitive != GAIM_STATUS_OFFLINE); | |
1362 } | |
1363 | |
1364 gboolean | |
1365 gaim_presence_is_status_active(const GaimPresence *presence, | |
1366 const char *status_id) | |
1367 { | |
1368 GaimStatus *status; | |
1369 | |
1370 g_return_val_if_fail(presence != NULL, FALSE); | |
1371 g_return_val_if_fail(status_id != NULL, FALSE); | |
1372 | |
1373 status = gaim_presence_get_status(presence, status_id); | |
1374 | |
1375 return (status != NULL && gaim_status_is_active(status)); | |
1376 } | |
1377 | |
1378 gboolean | |
1379 gaim_presence_is_status_primitive_active(const GaimPresence *presence, | |
1380 GaimStatusPrimitive primitive) | |
1381 { | |
1382 GaimStatus *status; | |
1383 GaimStatusType *status_type; | |
1384 | |
1385 g_return_val_if_fail(presence != NULL, FALSE); | |
1386 g_return_val_if_fail(primitive != GAIM_STATUS_UNSET, FALSE); | |
1387 | |
1388 status = gaim_presence_get_active_status(presence); | |
1389 status_type = gaim_status_get_type(status); | |
1390 | |
1391 if (gaim_status_type_get_primitive(status_type) == primitive) | |
1392 return TRUE; | |
6065 | 1393 |
1394 return FALSE; | |
1395 } | |
1396 | |
9949 | 1397 gboolean |
1398 gaim_presence_is_idle(const GaimPresence *presence) | |
6065 | 1399 { |
9949 | 1400 g_return_val_if_fail(presence != NULL, FALSE); |
1401 | |
1402 return presence->idle; | |
6065 | 1403 } |
1404 | |
9949 | 1405 time_t |
1406 gaim_presence_get_idle_time(const GaimPresence *presence) | |
6065 | 1407 { |
9949 | 1408 g_return_val_if_fail(presence != NULL, 0); |
6065 | 1409 |
9949 | 1410 return presence->idle_time; |
1411 } | |
6065 | 1412 |
9949 | 1413 unsigned int |
1414 gaim_presence_get_warning_level(const GaimPresence *presence) | |
1415 { | |
1416 g_return_val_if_fail(presence != NULL, 0); | |
6216 | 1417 |
9949 | 1418 return presence->warning_level; |
6065 | 1419 } |
1420 | |
9949 | 1421 gint |
1422 gaim_presence_compare(const GaimPresence *presence1, | |
1423 const GaimPresence *presence2) | |
6065 | 1424 { |
9949 | 1425 gboolean idle1, idle2; |
1426 size_t idle_time_1, idle_time_2; | |
1427 int score1 = 0, score2 = 0; | |
1428 const GList *l; | |
6065 | 1429 |
9949 | 1430 if ((presence1 == NULL && presence2 == NULL) || (presence1 == presence2)) |
1431 return 0; | |
1432 else if (presence1 == NULL) | |
1433 return -1; | |
1434 else if (presence2 == NULL) | |
1435 return 1; | |
6065 | 1436 |
9949 | 1437 /* Compute the score of the first set of statuses. */ |
1438 for (l = gaim_presence_get_statuses(presence1); l != NULL; l = l->next) | |
1439 { | |
1440 GaimStatus *status = (GaimStatus *)l->data; | |
1441 GaimStatusType *type = gaim_status_get_type(status); | |
6065 | 1442 |
9949 | 1443 if (gaim_status_is_active(status)) |
1444 score1 += primitive_scores[gaim_status_type_get_primitive(type)]; | |
6065 | 1445 } |
1446 | |
9949 | 1447 /* Compute the score of the second set of statuses. */ |
1448 for (l = gaim_presence_get_statuses(presence1); l != NULL; l = l->next) | |
1449 { | |
1450 GaimStatus *status = (GaimStatus *)l->data; | |
1451 GaimStatusType *type = gaim_status_get_type(status); | |
6065 | 1452 |
9949 | 1453 if (gaim_status_is_active(status)) |
1454 score2 += primitive_scores[gaim_status_type_get_primitive(type)]; | |
6065 | 1455 } |
1456 | |
9949 | 1457 idle1 = gaim_presence_is_idle(presence1); |
1458 idle2 = gaim_presence_is_idle(presence2); | |
6065 | 1459 |
9949 | 1460 if (idle1) |
1461 score1 += primitive_scores[SCORE_IDLE]; | |
6065 | 1462 |
9949 | 1463 if (idle2) |
1464 score2 += primitive_scores[SCORE_IDLE]; | |
6065 | 1465 |
9949 | 1466 idle_time_1 = gaim_presence_get_idle_time(presence1); |
1467 idle_time_2 = gaim_presence_get_idle_time(presence2); | |
6065 | 1468 |
9949 | 1469 if (idle_time_1 > idle_time_2) |
1470 score1 += primitive_scores[SCORE_IDLE_TIME]; | |
1471 else if (idle_time_1 < idle_time_2) | |
1472 score2 += primitive_scores[SCORE_IDLE_TIME]; | |
6065 | 1473 |
9949 | 1474 if (score1 < score2) |
1475 return 1; | |
1476 else if (score1 > score2) | |
1477 return -1; | |
1478 | |
1479 return 0; | |
1480 } | |
1481 | |
6065 | 1482 |
9949 | 1483 /************************************************************************** |
1484 * Status subsystem | |
1485 **************************************************************************/ | |
1486 static void | |
1487 score_pref_changed_cb(const char *name, GaimPrefType type, gpointer value, | |
1488 gpointer data) | |
1489 { | |
1490 int index = GPOINTER_TO_INT(data); | |
6065 | 1491 |
9949 | 1492 primitive_scores[index] = GPOINTER_TO_INT(value); |
6065 | 1493 } |
1494 | |
10006 | 1495 guint |
1496 gaim_buddy_presences_hash(gconstpointer key) | |
1497 { | |
1498 return ((GaimStatusBuddyKey *)key)->id; | |
1499 } | |
1500 | |
1501 gboolean | |
1502 gaim_buddy_presences_equal(gconstpointer a, gconstpointer b) | |
1503 { | |
1504 GaimStatusBuddyKey *key_a = (GaimStatusBuddyKey *)a; | |
1505 GaimStatusBuddyKey *key_b = (GaimStatusBuddyKey *)b; | |
1506 | |
1507 if (key_a->id == key_b->id) | |
1508 return TRUE; | |
1509 else | |
1510 return FALSE; | |
1511 } | |
1512 | |
9949 | 1513 void |
1514 gaim_statuses_init(void) | |
6065 | 1515 { |
9949 | 1516 gaim_prefs_add_none("/core/status"); |
1517 gaim_prefs_add_none("/core/status/scores"); | |
6065 | 1518 |
9949 | 1519 gaim_prefs_add_int("/core/status/scores/offline", |
1520 primitive_scores[GAIM_STATUS_OFFLINE]); | |
1521 gaim_prefs_add_int("/core/status/scores/available", | |
1522 primitive_scores[GAIM_STATUS_AVAILABLE]); | |
1523 gaim_prefs_add_int("/core/status/scores/hidden", | |
1524 primitive_scores[GAIM_STATUS_HIDDEN]); | |
1525 gaim_prefs_add_int("/core/status/scores/away", | |
1526 primitive_scores[GAIM_STATUS_AWAY]); | |
1527 gaim_prefs_add_int("/core/status/scores/extended_away", | |
1528 primitive_scores[GAIM_STATUS_EXTENDED_AWAY]); | |
1529 gaim_prefs_add_int("/core/status/scores/idle", | |
1530 primitive_scores[SCORE_IDLE]); | |
6065 | 1531 |
9949 | 1532 gaim_prefs_connect_callback("/core/status/scores/offline", |
1533 score_pref_changed_cb, | |
1534 GINT_TO_POINTER(GAIM_STATUS_OFFLINE)); | |
1535 gaim_prefs_connect_callback("/core/status/scores/available", | |
1536 score_pref_changed_cb, | |
1537 GINT_TO_POINTER(GAIM_STATUS_AVAILABLE)); | |
1538 gaim_prefs_connect_callback("/core/status/scores/hidden", | |
1539 score_pref_changed_cb, | |
1540 GINT_TO_POINTER(GAIM_STATUS_HIDDEN)); | |
1541 gaim_prefs_connect_callback("/core/status/scores/away", | |
1542 score_pref_changed_cb, | |
1543 GINT_TO_POINTER(GAIM_STATUS_AWAY)); | |
1544 gaim_prefs_connect_callback("/core/status/scores/extended_away", | |
1545 score_pref_changed_cb, | |
1546 GINT_TO_POINTER(GAIM_STATUS_EXTENDED_AWAY)); | |
1547 gaim_prefs_connect_callback("/core/status/scores/idle", | |
1548 score_pref_changed_cb, | |
1549 GINT_TO_POINTER(SCORE_IDLE)); | |
10006 | 1550 |
1551 /* XXX - I don't think this is destroyed correctly. --Mark */ | |
1552 buddy_presences = g_hash_table_new(gaim_buddy_presences_hash, | |
1553 gaim_buddy_presences_equal); | |
9949 | 1554 } |
6065 | 1555 |
9949 | 1556 void |
1557 gaim_statuses_uninit(void) | |
1558 { | |
1559 } | |
6065 | 1560 |
9949 | 1561 void |
1562 gaim_statuses_sync(void) | |
1563 { | |
6065 | 1564 } |
9949 | 1565 |
1566 void | |
1567 gaim_statuses_load(void) | |
1568 { | |
1569 } |