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