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