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