Mercurial > pidgin
annotate libgaim/savedstatuses.c @ 14688:37bb5e487347
[gaim-migrate @ 17440]
Fix Coverity CID 291 - don't dereference conv before the NULL check
committer: Tailor Script <tailor@pidgin.im>
author | Stu Tomlinson <stu@nosnilmot.com> |
---|---|
date | Sat, 07 Oct 2006 17:09:14 +0000 |
parents | 587268c9c6d7 |
children | 091b6ce6372e |
rev | line source |
---|---|
14192 | 1 /** |
2 * @file savedstatuses.c Saved Status API | |
3 * @ingroup core | |
4 * | |
5 * gaim | |
6 * | |
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. | |
10 * | |
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 */ | |
25 #include "internal.h" | |
26 | |
27 #include "debug.h" | |
28 #include "idle.h" | |
29 #include "notify.h" | |
30 #include "savedstatuses.h" | |
31 #include "dbus-maybe.h" | |
32 #include "status.h" | |
33 #include "util.h" | |
34 #include "xmlnode.h" | |
35 | |
36 /** | |
37 * The maximum number of transient statuses to save. This | |
38 * is used during the shutdown process to clean out old | |
39 * transient statuses. | |
40 */ | |
41 #define MAX_TRANSIENTS 5 | |
42 | |
43 /** | |
44 * The default message to use when the user becomes auto-away. | |
45 */ | |
46 #define DEFAULT_AUTOAWAY_MESSAGE _("I'm not here right now") | |
47 | |
48 /** | |
49 * The information stores a snap-shot of the statuses of all | |
50 * your accounts. Basically these are your saved away messages. | |
51 * There is an overall status and message that applies to | |
52 * all your accounts, and then each individual account can | |
53 * optionally have a different custom status and message. | |
54 * | |
55 * The changes to status.xml caused by the new status API | |
56 * are fully backward compatible. The new status API just | |
57 * adds the optional sub-statuses to the XML file. | |
58 */ | |
59 struct _GaimSavedStatus | |
60 { | |
61 char *title; | |
62 GaimStatusPrimitive type; | |
63 char *message; | |
64 | |
65 /** The timestamp when this saved status was created. This must be unique. */ | |
66 time_t creation_time; | |
67 | |
68 time_t lastused; | |
69 | |
70 unsigned int usage_count; | |
71 | |
72 GList *substatuses; /**< A list of GaimSavedStatusSub's. */ | |
73 }; | |
74 | |
75 /* | |
76 * TODO: If a GaimStatusType is deleted, need to also delete any | |
77 * associated GaimSavedStatusSub's? | |
78 */ | |
79 struct _GaimSavedStatusSub | |
80 { | |
81 GaimAccount *account; | |
82 const GaimStatusType *type; | |
83 char *message; | |
84 }; | |
85 | |
86 static GList *saved_statuses = NULL; | |
87 static guint save_timer = 0; | |
88 static gboolean statuses_loaded = FALSE; | |
89 | |
90 /* | |
91 * This hash table keeps track of which timestamps we've | |
92 * used so that we don't have two saved statuses with the | |
93 * same 'creation_time' timestamp. The 'created' timestamp | |
94 * is used as a unique identifier. | |
95 * | |
96 * So the key in this hash table is the creation_time and | |
97 * the value is a pointer to the GaimSavedStatus. | |
98 */ | |
99 static GHashTable *creation_times; | |
100 | |
101 static void schedule_save(void); | |
102 | |
103 /********************************************************************* | |
104 * Private utility functions * | |
105 *********************************************************************/ | |
106 | |
107 static void | |
108 free_saved_status_sub(GaimSavedStatusSub *substatus) | |
109 { | |
110 g_return_if_fail(substatus != NULL); | |
111 | |
112 g_free(substatus->message); | |
113 GAIM_DBUS_UNREGISTER_POINTER(substatus); | |
114 g_free(substatus); | |
115 } | |
116 | |
117 static void | |
118 free_saved_status(GaimSavedStatus *status) | |
119 { | |
120 g_return_if_fail(status != NULL); | |
121 | |
122 g_free(status->title); | |
123 g_free(status->message); | |
124 | |
125 while (status->substatuses != NULL) | |
126 { | |
127 GaimSavedStatusSub *substatus = status->substatuses->data; | |
128 status->substatuses = g_list_remove(status->substatuses, substatus); | |
129 free_saved_status_sub(substatus); | |
130 } | |
131 | |
132 GAIM_DBUS_UNREGISTER_POINTER(status); | |
133 g_free(status); | |
134 } | |
135 | |
136 /* | |
137 * Set the timestamp for when this saved status was created, and | |
138 * make sure it is unique. | |
139 */ | |
140 static void | |
141 set_creation_time(GaimSavedStatus *status, time_t creation_time) | |
142 { | |
143 g_return_if_fail(status != NULL); | |
144 | |
145 /* Avoid using 0 because it's an invalid hash key */ | |
146 status->creation_time = creation_time != 0 ? creation_time : 1; | |
147 | |
148 while (g_hash_table_lookup(creation_times, &status->creation_time) != NULL) | |
149 status->creation_time++; | |
150 | |
151 g_hash_table_insert(creation_times, | |
152 &status->creation_time, | |
153 status); | |
154 } | |
155 | |
156 /** | |
157 * A magic number is calcuated for each status, and then the | |
158 * statuses are ordered by the magic number. The magic number | |
159 * is the date the status was last used offset by one day for | |
160 * each time the status has been used (but only by 10 days at | |
161 * the most). | |
162 * | |
163 * The goal is to have recently used statuses at the top of | |
164 * the list, but to also keep frequently used statuses near | |
165 * the top. | |
166 */ | |
167 static gint | |
168 saved_statuses_sort_func(gconstpointer a, gconstpointer b) | |
169 { | |
170 const GaimSavedStatus *saved_status_a = a; | |
171 const GaimSavedStatus *saved_status_b = b; | |
172 time_t time_a = saved_status_a->lastused + | |
173 (MIN(saved_status_a->usage_count, 10) * 86400); | |
174 time_t time_b = saved_status_b->lastused + | |
175 (MIN(saved_status_b->usage_count, 10) * 86400); | |
176 if (time_a > time_b) | |
177 return -1; | |
178 if (time_a < time_b) | |
179 return 1; | |
180 return 0; | |
181 } | |
182 | |
183 /** | |
184 * Transient statuses are added and removed automatically by | |
185 * Gaim. If they're not used for a certain length of time then | |
186 * they'll expire and be automatically removed. This function | |
187 * does the expiration. | |
188 */ | |
189 static void | |
190 remove_old_transient_statuses() | |
191 { | |
192 GList *l, *next; | |
193 GaimSavedStatus *saved_status, *current_status; | |
194 int count; | |
195 time_t creation_time; | |
196 | |
197 current_status = gaim_savedstatus_get_current(); | |
198 | |
199 /* | |
200 * Iterate through the list of saved statuses. Delete all | |
201 * transient statuses except for the first MAX_TRANSIENTS | |
202 * (remember, the saved statuses are already sorted by popularity). | |
203 */ | |
204 count = 0; | |
205 for (l = saved_statuses; l != NULL; l = next) | |
206 { | |
207 next = l->next; | |
208 saved_status = l->data; | |
209 if (gaim_savedstatus_is_transient(saved_status)) | |
210 { | |
211 if (count == MAX_TRANSIENTS) | |
212 { | |
213 if (saved_status != current_status) | |
214 { | |
215 saved_statuses = g_list_remove(saved_statuses, saved_status); | |
216 creation_time = gaim_savedstatus_get_creation_time(saved_status); | |
217 g_hash_table_remove(creation_times, &creation_time); | |
218 free_saved_status(saved_status); | |
219 } | |
220 } | |
221 else | |
222 count++; | |
223 } | |
224 } | |
225 | |
226 if (count == MAX_TRANSIENTS) | |
227 schedule_save(); | |
228 } | |
229 | |
230 /********************************************************************* | |
231 * Writing to disk * | |
232 *********************************************************************/ | |
233 | |
234 static xmlnode * | |
235 substatus_to_xmlnode(GaimSavedStatusSub *substatus) | |
236 { | |
237 xmlnode *node, *child; | |
238 | |
239 node = xmlnode_new("substatus"); | |
240 | |
241 child = xmlnode_new_child(node, "account"); | |
242 xmlnode_set_attrib(child, "protocol", gaim_account_get_protocol_id(substatus->account)); | |
243 xmlnode_insert_data(child, gaim_account_get_username(substatus->account), -1); | |
244 | |
245 child = xmlnode_new_child(node, "state"); | |
246 xmlnode_insert_data(child, gaim_status_type_get_id(substatus->type), -1); | |
247 | |
248 if (substatus->message != NULL) | |
249 { | |
250 child = xmlnode_new_child(node, "message"); | |
251 xmlnode_insert_data(child, substatus->message, -1); | |
252 } | |
253 | |
254 return node; | |
255 } | |
256 | |
257 static xmlnode * | |
258 status_to_xmlnode(GaimSavedStatus *status) | |
259 { | |
260 xmlnode *node, *child; | |
261 char buf[21]; | |
262 GList *cur; | |
263 | |
264 node = xmlnode_new("status"); | |
265 if (status->title != NULL) | |
266 { | |
267 xmlnode_set_attrib(node, "name", status->title); | |
268 } | |
269 else | |
270 { | |
271 /* | |
272 * Gaim 1.5.0 and earlier require a name to be set, so we | |
273 * do this little hack to maintain backward compatability | |
274 * in the status.xml file. Eventually this should be removed | |
275 * and we should determine if a status is transient by | |
276 * whether the "name" attribute is set to something or if | |
277 * it does not exist at all. | |
278 */ | |
279 xmlnode_set_attrib(node, "name", "Auto-Cached"); | |
280 xmlnode_set_attrib(node, "transient", "true"); | |
281 } | |
282 | |
283 snprintf(buf, sizeof(buf), "%lu", status->creation_time); | |
284 xmlnode_set_attrib(node, "created", buf); | |
285 | |
286 snprintf(buf, sizeof(buf), "%lu", status->lastused); | |
287 xmlnode_set_attrib(node, "lastused", buf); | |
288 | |
289 snprintf(buf, sizeof(buf), "%u", status->usage_count); | |
290 xmlnode_set_attrib(node, "usage_count", buf); | |
291 | |
292 child = xmlnode_new_child(node, "state"); | |
293 xmlnode_insert_data(child, gaim_primitive_get_id_from_type(status->type), -1); | |
294 | |
295 if (status->message != NULL) | |
296 { | |
297 child = xmlnode_new_child(node, "message"); | |
298 xmlnode_insert_data(child, status->message, -1); | |
299 } | |
300 | |
301 for (cur = status->substatuses; cur != NULL; cur = cur->next) | |
302 { | |
303 child = substatus_to_xmlnode(cur->data); | |
304 xmlnode_insert_child(node, child); | |
305 } | |
306 | |
307 return node; | |
308 } | |
309 | |
310 static xmlnode * | |
311 statuses_to_xmlnode(void) | |
312 { | |
313 xmlnode *node, *child; | |
314 GList *cur; | |
315 | |
316 node = xmlnode_new("statuses"); | |
317 xmlnode_set_attrib(node, "version", "1.0"); | |
318 | |
319 for (cur = saved_statuses; cur != NULL; cur = cur->next) | |
320 { | |
321 child = status_to_xmlnode(cur->data); | |
322 xmlnode_insert_child(node, child); | |
323 } | |
324 | |
325 return node; | |
326 } | |
327 | |
328 static void | |
329 sync_statuses(void) | |
330 { | |
331 xmlnode *node; | |
332 char *data; | |
333 | |
334 if (!statuses_loaded) | |
335 { | |
336 gaim_debug_error("status", "Attempted to save statuses before they " | |
337 "were read!\n"); | |
338 return; | |
339 } | |
340 | |
341 node = statuses_to_xmlnode(); | |
342 data = xmlnode_to_formatted_str(node, NULL); | |
343 gaim_util_write_data_to_file("status.xml", data, -1); | |
344 g_free(data); | |
345 xmlnode_free(node); | |
346 } | |
347 | |
348 static gboolean | |
349 save_cb(gpointer data) | |
350 { | |
351 sync_statuses(); | |
352 save_timer = 0; | |
353 return FALSE; | |
354 } | |
355 | |
356 static void | |
357 schedule_save(void) | |
358 { | |
359 if (save_timer == 0) | |
360 save_timer = gaim_timeout_add(5000, save_cb, NULL); | |
361 } | |
362 | |
363 | |
364 /********************************************************************* | |
365 * Reading from disk * | |
366 *********************************************************************/ | |
367 | |
368 static GaimSavedStatusSub * | |
369 parse_substatus(xmlnode *substatus) | |
370 { | |
371 GaimSavedStatusSub *ret; | |
372 xmlnode *node; | |
373 char *data; | |
374 | |
375 ret = g_new0(GaimSavedStatusSub, 1); | |
376 GAIM_DBUS_REGISTER_POINTER(ret, GaimSavedStatusSub); | |
377 | |
378 /* Read the account */ | |
379 node = xmlnode_get_child(substatus, "account"); | |
380 if (node != NULL) | |
381 { | |
382 char *acct_name; | |
383 const char *protocol; | |
384 acct_name = xmlnode_get_data(node); | |
385 protocol = xmlnode_get_attrib(node, "protocol"); | |
386 if ((acct_name != NULL) && (protocol != NULL)) | |
387 ret->account = gaim_accounts_find(acct_name, protocol); | |
388 g_free(acct_name); | |
389 } | |
390 | |
391 if (ret->account == NULL) | |
392 { | |
393 g_free(ret); | |
394 return NULL; | |
395 } | |
396 | |
397 /* Read the state */ | |
398 node = xmlnode_get_child(substatus, "state"); | |
399 if ((node != NULL) && ((data = xmlnode_get_data(node)) != NULL)) | |
400 { | |
401 ret->type = gaim_status_type_find_with_id( | |
402 ret->account->status_types, data); | |
403 g_free(data); | |
404 } | |
405 | |
406 /* Read the message */ | |
407 node = xmlnode_get_child(substatus, "message"); | |
408 if ((node != NULL) && ((data = xmlnode_get_data(node)) != NULL)) | |
409 { | |
410 ret->message = data; | |
411 } | |
412 | |
413 return ret; | |
414 } | |
415 | |
416 /** | |
417 * Parse a saved status and add it to the saved_statuses linked list. | |
418 * | |
419 * Here's an example of the XML for a saved status: | |
420 * <status name="Girls"> | |
421 * <state>away</state> | |
422 * <message>I like the way that they walk | |
423 * And it's chill to hear them talk | |
424 * And I can always make them smile | |
425 * From White Castle to the Nile</message> | |
426 * <substatus> | |
427 * <account protocol='prpl-oscar'>markdoliner</account> | |
428 * <state>available</state> | |
429 * <message>The ladies man is here to answer your queries.</message> | |
430 * </substatus> | |
431 * <substatus> | |
432 * <account protocol='prpl-oscar'>giantgraypanda</account> | |
433 * <state>away</state> | |
434 * <message>A.C. ain't in charge no more.</message> | |
435 * </substatus> | |
436 * </status> | |
437 * | |
438 * I know. Moving, huh? | |
439 */ | |
440 static GaimSavedStatus * | |
441 parse_status(xmlnode *status) | |
442 { | |
443 GaimSavedStatus *ret; | |
444 xmlnode *node; | |
445 const char *attrib; | |
446 char *data; | |
447 int i; | |
448 | |
449 ret = g_new0(GaimSavedStatus, 1); | |
450 GAIM_DBUS_REGISTER_POINTER(ret, GaimSavedStatus); | |
451 | |
452 attrib = xmlnode_get_attrib(status, "transient"); | |
453 if ((attrib == NULL) || (strcmp(attrib, "true"))) | |
454 { | |
455 /* Read the title */ | |
456 attrib = xmlnode_get_attrib(status, "name"); | |
457 ret->title = g_strdup(attrib); | |
458 } | |
459 | |
460 if (ret->title != NULL) | |
461 { | |
462 /* Ensure the title is unique */ | |
463 i = 2; | |
464 while (gaim_savedstatus_find(ret->title) != NULL) | |
465 { | |
466 g_free(ret->title); | |
467 ret->title = g_strdup_printf("%s %d", attrib, i); | |
468 i++; | |
469 } | |
470 } | |
471 | |
472 /* Read the creation time */ | |
473 attrib = xmlnode_get_attrib(status, "created"); | |
474 set_creation_time(ret, (attrib != NULL ? atol(attrib) : 0)); | |
475 | |
476 /* Read the last used time */ | |
477 attrib = xmlnode_get_attrib(status, "lastused"); | |
478 ret->lastused = (attrib != NULL ? atol(attrib) : 0); | |
479 | |
480 /* Read the usage count */ | |
481 attrib = xmlnode_get_attrib(status, "usage_count"); | |
482 ret->usage_count = (attrib != NULL ? atol(attrib) : 0); | |
483 | |
484 /* Read the primitive status type */ | |
485 node = xmlnode_get_child(status, "state"); | |
486 if ((node != NULL) && ((data = xmlnode_get_data(node)) != NULL)) | |
487 { | |
488 ret->type = gaim_primitive_get_type_from_id(data); | |
489 g_free(data); | |
490 } | |
491 | |
492 /* Read the message */ | |
493 node = xmlnode_get_child(status, "message"); | |
494 if ((node != NULL) && ((data = xmlnode_get_data(node)) != NULL)) | |
495 { | |
496 ret->message = data; | |
497 } | |
498 | |
499 /* Read substatuses */ | |
500 for (node = xmlnode_get_child(status, "substatus"); node != NULL; | |
501 node = xmlnode_get_next_twin(node)) | |
502 { | |
503 GaimSavedStatusSub *new; | |
504 new = parse_substatus(node); | |
505 if (new != NULL) | |
506 ret->substatuses = g_list_prepend(ret->substatuses, new); | |
507 } | |
508 | |
509 return ret; | |
510 } | |
511 | |
512 /** | |
513 * Read the saved statuses from a file in the Gaim user dir. | |
514 * | |
515 * @return TRUE on success, FALSE on failure (if the file can not | |
516 * be opened, or if it contains invalid XML). | |
517 */ | |
518 static void | |
519 load_statuses(void) | |
520 { | |
521 xmlnode *statuses, *status; | |
522 | |
523 statuses_loaded = TRUE; | |
524 | |
525 statuses = gaim_util_read_xml_from_file("status.xml", _("saved statuses")); | |
526 | |
527 if (statuses == NULL) | |
528 return; | |
529 | |
530 for (status = xmlnode_get_child(statuses, "status"); status != NULL; | |
531 status = xmlnode_get_next_twin(status)) | |
532 { | |
533 GaimSavedStatus *new; | |
534 new = parse_status(status); | |
535 saved_statuses = g_list_prepend(saved_statuses, new); | |
536 } | |
537 saved_statuses = g_list_sort(saved_statuses, saved_statuses_sort_func); | |
538 | |
539 xmlnode_free(statuses); | |
540 } | |
541 | |
542 | |
543 /************************************************************************** | |
544 * Saved status API | |
545 **************************************************************************/ | |
546 GaimSavedStatus * | |
547 gaim_savedstatus_new(const char *title, GaimStatusPrimitive type) | |
548 { | |
549 GaimSavedStatus *status; | |
550 | |
551 /* Make sure we don't already have a saved status with this title. */ | |
552 if (title != NULL) | |
553 g_return_val_if_fail(gaim_savedstatus_find(title) == NULL, NULL); | |
554 | |
555 status = g_new0(GaimSavedStatus, 1); | |
556 GAIM_DBUS_REGISTER_POINTER(status, GaimSavedStatus); | |
557 status->title = g_strdup(title); | |
558 status->type = type; | |
559 set_creation_time(status, time(NULL)); | |
560 | |
561 saved_statuses = g_list_insert_sorted(saved_statuses, status, saved_statuses_sort_func); | |
562 | |
563 schedule_save(); | |
564 | |
565 return status; | |
566 } | |
567 | |
568 void | |
569 gaim_savedstatus_set_title(GaimSavedStatus *status, const char *title) | |
570 { | |
571 g_return_if_fail(status != NULL); | |
572 | |
573 /* Make sure we don't already have a saved status with this title. */ | |
574 g_return_if_fail(gaim_savedstatus_find(title) == NULL); | |
575 | |
576 g_free(status->title); | |
577 status->title = g_strdup(title); | |
578 | |
579 schedule_save(); | |
580 } | |
581 | |
582 void | |
583 gaim_savedstatus_set_type(GaimSavedStatus *status, GaimStatusPrimitive type) | |
584 { | |
585 g_return_if_fail(status != NULL); | |
586 | |
587 status->type = type; | |
588 | |
589 schedule_save(); | |
590 } | |
591 | |
592 void | |
593 gaim_savedstatus_set_message(GaimSavedStatus *status, const char *message) | |
594 { | |
595 g_return_if_fail(status != NULL); | |
596 | |
597 g_free(status->message); | |
598 if ((message != NULL) && (*message == '\0')) | |
599 status->message = NULL; | |
600 else | |
601 status->message = g_strdup(message); | |
602 | |
603 schedule_save(); | |
604 } | |
605 | |
606 void | |
607 gaim_savedstatus_set_substatus(GaimSavedStatus *saved_status, | |
608 const GaimAccount *account, | |
609 const GaimStatusType *type, | |
610 const char *message) | |
611 { | |
612 GaimSavedStatusSub *substatus; | |
613 | |
614 g_return_if_fail(saved_status != NULL); | |
615 g_return_if_fail(account != NULL); | |
616 g_return_if_fail(type != NULL); | |
617 | |
618 /* Find an existing substatus or create a new one */ | |
619 substatus = gaim_savedstatus_get_substatus(saved_status, account); | |
620 if (substatus == NULL) | |
621 { | |
622 substatus = g_new0(GaimSavedStatusSub, 1); | |
623 GAIM_DBUS_REGISTER_POINTER(substatus, GaimSavedStatusSub); | |
624 substatus->account = (GaimAccount *)account; | |
625 saved_status->substatuses = g_list_prepend(saved_status->substatuses, substatus); | |
626 } | |
627 | |
628 substatus->type = type; | |
629 g_free(substatus->message); | |
630 substatus->message = g_strdup(message); | |
631 | |
632 schedule_save(); | |
633 } | |
634 | |
635 void | |
636 gaim_savedstatus_unset_substatus(GaimSavedStatus *saved_status, | |
637 const GaimAccount *account) | |
638 { | |
639 GList *iter; | |
640 GaimSavedStatusSub *substatus; | |
641 | |
642 g_return_if_fail(saved_status != NULL); | |
643 g_return_if_fail(account != NULL); | |
644 | |
645 for (iter = saved_status->substatuses; iter != NULL; iter = iter->next) | |
646 { | |
647 substatus = iter->data; | |
648 if (substatus->account == account) | |
649 { | |
650 saved_status->substatuses = g_list_delete_link(saved_status->substatuses, iter); | |
651 g_free(substatus->message); | |
652 g_free(substatus); | |
653 return; | |
654 } | |
655 } | |
656 } | |
657 | |
658 gboolean | |
659 gaim_savedstatus_delete(const char *title) | |
660 { | |
661 GaimSavedStatus *status; | |
662 time_t creation_time, current, idleaway; | |
663 | |
664 status = gaim_savedstatus_find(title); | |
665 | |
666 if (status == NULL) | |
667 return FALSE; | |
668 | |
669 saved_statuses = g_list_remove(saved_statuses, status); | |
670 creation_time = gaim_savedstatus_get_creation_time(status); | |
671 g_hash_table_remove(creation_times, &creation_time); | |
672 free_saved_status(status); | |
673 | |
674 schedule_save(); | |
675 | |
676 /* | |
677 * If we just deleted our current status or our idleaway status, | |
678 * then set the appropriate pref back to 0. | |
679 */ | |
680 current = gaim_prefs_get_int("/core/savedstatus/default"); | |
681 if (current == creation_time) | |
682 gaim_prefs_set_int("/core/savedstatus/default", 0); | |
683 | |
684 idleaway = gaim_prefs_get_int("/core/savedstatus/idleaway"); | |
685 if (idleaway == creation_time) | |
686 gaim_prefs_set_int("/core/savedstatus/idleaway", 0); | |
687 | |
688 return TRUE; | |
689 } | |
690 | |
691 const GList * | |
692 gaim_savedstatuses_get_all(void) | |
693 { | |
694 return saved_statuses; | |
695 } | |
696 | |
697 GList * | |
698 gaim_savedstatuses_get_popular(unsigned int how_many) | |
699 { | |
700 GList *popular = NULL; | |
701 GList *cur; | |
702 int i; | |
703 GaimSavedStatus *current, *next; | |
704 | |
705 /* We don't want the current status to be in the GList */ | |
706 current = gaim_savedstatus_get_current(); | |
707 | |
708 /* Copy 'how_many' elements to a new list */ | |
709 i = 0; | |
710 cur = saved_statuses; | |
711 while ((i < how_many) && (cur != NULL)) | |
712 { | |
713 next = cur->data; | |
714 if ((next != current) && (!gaim_savedstatus_is_transient(next) | |
715 || gaim_savedstatus_get_message(next) != NULL)) | |
716 { | |
717 popular = g_list_prepend(popular, cur->data); | |
718 i++; | |
719 } | |
720 cur = cur->next; | |
721 } | |
722 | |
723 popular = g_list_reverse(popular); | |
724 | |
725 return popular; | |
726 } | |
727 | |
728 GaimSavedStatus * | |
729 gaim_savedstatus_get_current(void) | |
730 { | |
731 if (gaim_savedstatus_is_idleaway()) | |
732 return gaim_savedstatus_get_idleaway(); | |
733 else | |
734 return gaim_savedstatus_get_default(); | |
735 } | |
736 | |
737 GaimSavedStatus * | |
738 gaim_savedstatus_get_default() | |
739 { | |
740 int creation_time; | |
741 GaimSavedStatus *saved_status = NULL; | |
742 | |
743 creation_time = gaim_prefs_get_int("/core/savedstatus/default"); | |
744 | |
745 if (creation_time != 0) | |
746 saved_status = g_hash_table_lookup(creation_times, &creation_time); | |
747 | |
748 if (saved_status == NULL) | |
749 { | |
750 /* | |
751 * We don't have a current saved status! This is either a new | |
752 * Gaim user or someone upgrading from Gaim 1.5.0 or older, or | |
753 * possibly someone who deleted the status they were currently | |
754 * using? In any case, add a default status. | |
755 */ | |
756 saved_status = gaim_savedstatus_new(NULL, GAIM_STATUS_AVAILABLE); | |
757 gaim_prefs_set_int("/core/savedstatus/default", | |
758 gaim_savedstatus_get_creation_time(saved_status)); | |
759 } | |
760 | |
761 return saved_status; | |
762 } | |
763 | |
764 GaimSavedStatus * | |
765 gaim_savedstatus_get_idleaway() | |
766 { | |
767 int creation_time; | |
768 GaimSavedStatus *saved_status = NULL; | |
769 | |
770 creation_time = gaim_prefs_get_int("/core/savedstatus/idleaway"); | |
771 | |
772 if (creation_time != 0) | |
773 saved_status = g_hash_table_lookup(creation_times, &creation_time); | |
774 | |
775 if (saved_status == NULL) | |
776 { | |
777 /* We don't have a specified "idle" status! Weird. */ | |
778 saved_status = gaim_savedstatus_find_transient_by_type_and_message( | |
779 GAIM_STATUS_AWAY, DEFAULT_AUTOAWAY_MESSAGE); | |
780 | |
781 if (saved_status == NULL) | |
782 { | |
783 saved_status = gaim_savedstatus_new(NULL, GAIM_STATUS_AWAY); | |
784 gaim_savedstatus_set_message(saved_status, DEFAULT_AUTOAWAY_MESSAGE); | |
785 gaim_prefs_set_int("/core/savedstatus/idleaway", | |
786 gaim_savedstatus_get_creation_time(saved_status)); | |
787 } | |
788 } | |
789 | |
790 return saved_status; | |
791 } | |
792 | |
793 gboolean | |
794 gaim_savedstatus_is_idleaway() | |
795 { | |
796 return gaim_prefs_get_bool("/core/savedstatus/isidleaway"); | |
797 } | |
798 | |
799 void | |
800 gaim_savedstatus_set_idleaway(gboolean idleaway) | |
801 { | |
802 GList *accounts, *node; | |
803 GaimSavedStatus *old, *saved_status; | |
804 | |
805 if (gaim_savedstatus_is_idleaway() == idleaway) | |
806 /* Don't need to do anything */ | |
807 return; | |
808 | |
809 /* Changing our status makes us un-idle */ | |
810 if (!idleaway) | |
811 gaim_idle_touch(); | |
812 | |
813 old = gaim_savedstatus_get_current(); | |
814 gaim_prefs_set_bool("/core/savedstatus/isidleaway", idleaway); | |
14361 | 815 saved_status = idleaway ? gaim_savedstatus_get_idleaway() |
816 : gaim_savedstatus_get_default(); | |
817 | |
818 if (idleaway && (gaim_savedstatus_get_type(old) != GAIM_STATUS_AVAILABLE)) | |
819 /* Our global status is already "away," so don't change anything */ | |
820 return; | |
14192 | 821 |
822 accounts = gaim_accounts_get_all_active(); | |
823 for (node = accounts; node != NULL; node = node->next) | |
824 { | |
825 GaimAccount *account; | |
826 GaimPresence *presence; | |
827 GaimStatus *status; | |
828 | |
829 account = node->data; | |
830 presence = gaim_account_get_presence(account); | |
831 status = gaim_presence_get_active_status(presence); | |
832 | |
833 if (!idleaway || gaim_status_is_available(status)) | |
834 gaim_savedstatus_activate_for_account(saved_status, account); | |
835 } | |
836 | |
837 g_list_free(accounts); | |
838 | |
839 gaim_signal_emit(gaim_savedstatuses_get_handle(), "savedstatus-changed", | |
840 saved_status, old); | |
841 } | |
842 | |
843 GaimSavedStatus * | |
844 gaim_savedstatus_get_startup() | |
845 { | |
846 int creation_time; | |
847 GaimSavedStatus *saved_status = NULL; | |
848 | |
849 creation_time = gaim_prefs_get_int("/core/savedstatus/startup"); | |
850 | |
851 if (creation_time != 0) | |
852 saved_status = g_hash_table_lookup(creation_times, &creation_time); | |
853 | |
854 if (saved_status == NULL) | |
855 { | |
856 /* | |
857 * We don't have a status to apply. | |
858 * This may be the first login, or the user wants to | |
859 * restore the "current" status. | |
860 */ | |
861 saved_status = gaim_savedstatus_get_current(); | |
862 } | |
863 | |
864 return saved_status; | |
865 } | |
866 | |
867 | |
868 GaimSavedStatus * | |
869 gaim_savedstatus_find(const char *title) | |
870 { | |
871 GList *iter; | |
872 GaimSavedStatus *status; | |
873 | |
874 g_return_val_if_fail(title != NULL, NULL); | |
875 | |
876 for (iter = saved_statuses; iter != NULL; iter = iter->next) | |
877 { | |
878 status = (GaimSavedStatus *)iter->data; | |
879 if ((status->title != NULL) && !strcmp(status->title, title)) | |
880 return status; | |
881 } | |
882 | |
883 return NULL; | |
884 } | |
885 | |
886 GaimSavedStatus * | |
887 gaim_savedstatus_find_by_creation_time(time_t creation_time) | |
888 { | |
889 GList *iter; | |
890 GaimSavedStatus *status; | |
891 | |
892 for (iter = saved_statuses; iter != NULL; iter = iter->next) | |
893 { | |
894 status = (GaimSavedStatus *)iter->data; | |
895 if (status->creation_time == creation_time) | |
896 return status; | |
897 } | |
898 | |
899 return NULL; | |
900 } | |
901 | |
902 GaimSavedStatus * | |
903 gaim_savedstatus_find_transient_by_type_and_message(GaimStatusPrimitive type, | |
904 const char *message) | |
905 { | |
906 GList *iter; | |
907 GaimSavedStatus *status; | |
908 | |
909 for (iter = saved_statuses; iter != NULL; iter = iter->next) | |
910 { | |
911 status = (GaimSavedStatus *)iter->data; | |
912 if ((status->type == type) && gaim_savedstatus_is_transient(status) && | |
14663
587268c9c6d7
[gaim-migrate @ 17409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14361
diff
changeset
|
913 !gaim_savedstatus_has_substatuses(status) && |
14192 | 914 (((status->message == NULL) && (message == NULL)) || |
915 ((status->message != NULL) && (message != NULL) && !strcmp(status->message, message)))) | |
916 { | |
917 return status; | |
918 } | |
919 } | |
920 | |
921 return NULL; | |
922 } | |
923 | |
924 gboolean | |
925 gaim_savedstatus_is_transient(const GaimSavedStatus *saved_status) | |
926 { | |
927 g_return_val_if_fail(saved_status != NULL, TRUE); | |
928 | |
929 return (saved_status->title == NULL); | |
930 } | |
931 | |
932 const char * | |
933 gaim_savedstatus_get_title(const GaimSavedStatus *saved_status) | |
934 { | |
935 const char *message; | |
936 | |
937 g_return_val_if_fail(saved_status != NULL, NULL); | |
938 | |
939 /* If we have a title then return it */ | |
940 if (saved_status->title != NULL) | |
941 return saved_status->title; | |
942 | |
943 /* Otherwise, this is a transient status and we make up a title on the fly */ | |
944 message = gaim_savedstatus_get_message(saved_status); | |
945 | |
946 if ((message == NULL) || (*message == '\0')) | |
947 { | |
948 GaimStatusPrimitive primitive; | |
949 primitive = gaim_savedstatus_get_type(saved_status); | |
950 return gaim_primitive_get_name_from_type(primitive); | |
951 } | |
952 else | |
953 { | |
954 char *stripped; | |
955 static char buf[64]; | |
956 stripped = gaim_markup_strip_html(message); | |
957 gaim_util_chrreplace(stripped, '\n', ' '); | |
958 strncpy(buf, stripped, sizeof(buf)); | |
959 buf[sizeof(buf) - 1] = '\0'; | |
960 if ((strlen(stripped) + 1) > sizeof(buf)) | |
961 { | |
962 /* Truncate and ellipsize */ | |
963 char *tmp = g_utf8_find_prev_char(buf, &buf[sizeof(buf) - 4]); | |
964 strcpy(tmp, "..."); | |
965 } | |
966 g_free(stripped); | |
967 return buf; | |
968 } | |
969 } | |
970 | |
971 GaimStatusPrimitive | |
972 gaim_savedstatus_get_type(const GaimSavedStatus *saved_status) | |
973 { | |
974 g_return_val_if_fail(saved_status != NULL, GAIM_STATUS_OFFLINE); | |
975 | |
976 return saved_status->type; | |
977 } | |
978 | |
979 const char * | |
980 gaim_savedstatus_get_message(const GaimSavedStatus *saved_status) | |
981 { | |
982 g_return_val_if_fail(saved_status != NULL, NULL); | |
983 | |
984 return saved_status->message; | |
985 } | |
986 | |
987 time_t | |
988 gaim_savedstatus_get_creation_time(const GaimSavedStatus *saved_status) | |
989 { | |
990 g_return_val_if_fail(saved_status != NULL, 0); | |
991 | |
992 return saved_status->creation_time; | |
993 } | |
994 | |
995 gboolean | |
996 gaim_savedstatus_has_substatuses(const GaimSavedStatus *saved_status) | |
997 { | |
998 g_return_val_if_fail(saved_status != NULL, FALSE); | |
999 | |
1000 return (saved_status->substatuses != NULL); | |
1001 } | |
1002 | |
1003 GaimSavedStatusSub * | |
1004 gaim_savedstatus_get_substatus(const GaimSavedStatus *saved_status, | |
1005 const GaimAccount *account) | |
1006 { | |
1007 GList *iter; | |
1008 GaimSavedStatusSub *substatus; | |
1009 | |
1010 g_return_val_if_fail(saved_status != NULL, NULL); | |
1011 g_return_val_if_fail(account != NULL, NULL); | |
1012 | |
1013 for (iter = saved_status->substatuses; iter != NULL; iter = iter->next) | |
1014 { | |
1015 substatus = iter->data; | |
1016 if (substatus->account == account) | |
1017 return substatus; | |
1018 } | |
1019 | |
1020 return NULL; | |
1021 } | |
1022 | |
1023 const GaimStatusType * | |
1024 gaim_savedstatus_substatus_get_type(const GaimSavedStatusSub *substatus) | |
1025 { | |
1026 g_return_val_if_fail(substatus != NULL, NULL); | |
1027 | |
1028 return substatus->type; | |
1029 } | |
1030 | |
1031 const char * | |
1032 gaim_savedstatus_substatus_get_message(const GaimSavedStatusSub *substatus) | |
1033 { | |
1034 g_return_val_if_fail(substatus != NULL, NULL); | |
1035 | |
1036 return substatus->message; | |
1037 } | |
1038 | |
1039 void | |
1040 gaim_savedstatus_activate(GaimSavedStatus *saved_status) | |
1041 { | |
1042 GList *accounts, *node; | |
1043 GaimSavedStatus *old = gaim_savedstatus_get_current(); | |
1044 | |
1045 g_return_if_fail(saved_status != NULL); | |
1046 | |
1047 /* Make sure our list of saved statuses remains sorted */ | |
1048 saved_status->lastused = time(NULL); | |
1049 saved_status->usage_count++; | |
1050 saved_statuses = g_list_remove(saved_statuses, saved_status); | |
1051 saved_statuses = g_list_insert_sorted(saved_statuses, saved_status, saved_statuses_sort_func); | |
1052 | |
1053 accounts = gaim_accounts_get_all_active(); | |
1054 for (node = accounts; node != NULL; node = node->next) | |
1055 { | |
1056 GaimAccount *account; | |
1057 | |
1058 account = node->data; | |
1059 | |
1060 gaim_savedstatus_activate_for_account(saved_status, account); | |
1061 } | |
1062 | |
1063 g_list_free(accounts); | |
1064 | |
1065 gaim_prefs_set_int("/core/savedstatus/default", | |
1066 gaim_savedstatus_get_creation_time(saved_status)); | |
1067 gaim_savedstatus_set_idleaway(FALSE); | |
1068 | |
1069 gaim_signal_emit(gaim_savedstatuses_get_handle(), "savedstatus-changed", | |
1070 saved_status, old); | |
1071 } | |
1072 | |
1073 void | |
1074 gaim_savedstatus_activate_for_account(const GaimSavedStatus *saved_status, | |
1075 GaimAccount *account) | |
1076 { | |
1077 const GaimStatusType *status_type; | |
1078 const GaimSavedStatusSub *substatus; | |
1079 const char *message = NULL; | |
1080 | |
1081 g_return_if_fail(saved_status != NULL); | |
1082 g_return_if_fail(account != NULL); | |
1083 | |
1084 substatus = gaim_savedstatus_get_substatus(saved_status, account); | |
1085 if (substatus != NULL) | |
1086 { | |
1087 status_type = substatus->type; | |
1088 message = substatus->message; | |
1089 } | |
1090 else | |
1091 { | |
1092 status_type = gaim_account_get_status_type_with_primitive(account, saved_status->type); | |
1093 if (status_type == NULL) | |
1094 return; | |
1095 message = saved_status->message; | |
1096 } | |
1097 | |
1098 if ((message != NULL) && | |
1099 (gaim_status_type_get_attr(status_type, "message"))) | |
1100 { | |
1101 gaim_account_set_status(account, gaim_status_type_get_id(status_type), | |
1102 TRUE, "message", message, NULL); | |
1103 } | |
1104 else | |
1105 { | |
1106 gaim_account_set_status(account, gaim_status_type_get_id(status_type), | |
1107 TRUE, NULL); | |
1108 } | |
1109 } | |
1110 | |
1111 void * | |
1112 gaim_savedstatuses_get_handle(void) | |
1113 { | |
1114 static int handle; | |
1115 | |
1116 return &handle; | |
1117 } | |
1118 | |
1119 void | |
1120 gaim_savedstatuses_init(void) | |
1121 { | |
1122 void *handle = gaim_savedstatuses_get_handle(); | |
1123 | |
1124 creation_times = g_hash_table_new(g_int_hash, g_int_equal); | |
1125 | |
1126 /* | |
1127 * Using 0 as the creation_time is a special case. | |
1128 * If someone calls gaim_savedstatus_get_current() or | |
1129 * gaim_savedstatus_get_idleaway() and either of those functions | |
1130 * sees a creation_time of 0, then it will create a default | |
1131 * saved status and return that to the user. | |
1132 */ | |
1133 gaim_prefs_add_none("/core/savedstatus"); | |
1134 gaim_prefs_add_int("/core/savedstatus/default", 0); | |
1135 gaim_prefs_add_int("/core/savedstatus/startup", 0); | |
1136 gaim_prefs_add_bool("/core/savedstatus/startup_current_status", TRUE); | |
1137 gaim_prefs_add_int("/core/savedstatus/idleaway", 0); | |
1138 gaim_prefs_add_bool("/core/savedstatus/isidleaway", FALSE); | |
1139 | |
1140 load_statuses(); | |
1141 | |
1142 gaim_signal_register(handle, "savedstatus-changed", | |
1143 gaim_marshal_VOID__POINTER_POINTER, NULL, 2, | |
1144 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
1145 GAIM_SUBTYPE_SAVEDSTATUS), | |
1146 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
1147 GAIM_SUBTYPE_SAVEDSTATUS)); | |
1148 } | |
1149 | |
1150 void | |
1151 gaim_savedstatuses_uninit(void) | |
1152 { | |
1153 remove_old_transient_statuses(); | |
1154 | |
1155 if (save_timer != 0) | |
1156 { | |
1157 gaim_timeout_remove(save_timer); | |
1158 save_timer = 0; | |
1159 sync_statuses(); | |
1160 } | |
1161 | |
1162 while (saved_statuses != NULL) { | |
1163 GaimSavedStatus *saved_status = saved_statuses->data; | |
1164 saved_statuses = g_list_remove(saved_statuses, saved_status); | |
1165 free_saved_status(saved_status); | |
1166 } | |
1167 | |
1168 g_hash_table_destroy(creation_times); | |
1169 | |
1170 gaim_signals_unregister_by_instance(gaim_savedstatuses_get_handle()); | |
1171 } | |
1172 |