rename accountopt.[ch] to purpleaccountoption.[ch]
[pidgin-git.git] / libpurple / savedstatuses.c
bloba54206b31c538c37c3c0eb6181cb71e69ed90111
1 /* purple
3 * Purple is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 #include "internal.h"
23 #include "debug.h"
24 #include "idle.h"
25 #include "notify.h"
26 #include "savedstatuses.h"
27 #include "request.h"
28 #include "status.h"
29 #include "util.h"
30 #include "xmlnode.h"
33 * The maximum number of transient statuses to save. This
34 * is used during the shutdown process to clean out old
35 * transient statuses.
37 #define MAX_TRANSIENTS 5
40 * The default message to use when the user becomes auto-away.
42 #define DEFAULT_AUTOAWAY_MESSAGE _("I'm not here right now")
45 * The information stores a snap-shot of the statuses of all
46 * your accounts. Basically these are your saved away messages.
47 * There is an overall status and message that applies to
48 * all your accounts, and then each individual account can
49 * optionally have a different custom status and message.
51 * The changes to status.xml caused by the new status API
52 * are fully backward compatible. The new status API just
53 * adds the optional sub-statuses to the XML file.
55 struct _PurpleSavedStatus
57 char *title;
58 PurpleStatusPrimitive type;
59 char *message;
61 /* The timestamp when this saved status was created. This must be unique. */
62 time_t creation_time;
64 time_t lastused;
66 unsigned int usage_count;
68 GList *substatuses; /* A list of PurpleSavedStatusSub's. */
72 * TODO: If a PurpleStatusType is deleted, need to also delete any
73 * associated PurpleSavedStatusSub's?
75 struct _PurpleSavedStatusSub
77 PurpleAccount *account;
78 const PurpleStatusType *type;
79 char *message;
82 static GList *saved_statuses = NULL;
83 static guint save_timer = 0;
84 static gboolean statuses_loaded = FALSE;
87 * This hash table keeps track of which timestamps we've
88 * used so that we don't have two saved statuses with the
89 * same 'creation_time' timestamp. The 'created' timestamp
90 * is used as a unique identifier.
92 * So the key in this hash table is the creation_time and
93 * the value is a pointer to the PurpleSavedStatus.
95 static GHashTable *creation_times;
97 static void schedule_save(void);
99 /*********************************************************************
100 * Private utility functions *
101 *********************************************************************/
103 static void
104 free_saved_status_sub(PurpleSavedStatusSub *substatus)
106 g_return_if_fail(substatus != NULL);
108 g_free(substatus->message);
109 purple_request_close_with_handle(substatus);
110 g_free(substatus);
113 static void
114 free_saved_status(PurpleSavedStatus *status)
116 g_return_if_fail(status != NULL);
118 g_free(status->title);
119 g_free(status->message);
121 while (status->substatuses != NULL)
123 PurpleSavedStatusSub *substatus = status->substatuses->data;
124 status->substatuses = g_list_remove(status->substatuses, substatus);
125 free_saved_status_sub(substatus);
127 purple_request_close_with_handle(status);
128 g_free(status);
132 * Set the timestamp for when this saved status was created, and
133 * make sure it is unique.
135 static void
136 set_creation_time(PurpleSavedStatus *status, time_t creation_time)
138 g_return_if_fail(status != NULL);
140 /* Avoid using 0 because it's an invalid hash key */
141 status->creation_time = creation_time != 0 ? creation_time : 1;
143 while (g_hash_table_lookup(creation_times, (gconstpointer)status->creation_time) != NULL)
144 status->creation_time++;
146 g_hash_table_insert(creation_times,
147 (gpointer)status->creation_time,
148 status);
152 * A magic number is calculated for each status, and then the
153 * statuses are ordered by the magic number. The magic number
154 * is the date the status was last used offset by one day for
155 * each time the status has been used (but only by 10 days at
156 * the most).
158 * The goal is to have recently used statuses at the top of
159 * the list, but to also keep frequently used statuses near
160 * the top.
162 static gint
163 saved_statuses_sort_func(gconstpointer a, gconstpointer b)
165 const PurpleSavedStatus *saved_status_a = a;
166 const PurpleSavedStatus *saved_status_b = b;
167 time_t time_a = saved_status_a->lastused +
168 (MIN(saved_status_a->usage_count, 10) * 86400);
169 time_t time_b = saved_status_b->lastused +
170 (MIN(saved_status_b->usage_count, 10) * 86400);
171 if (time_a > time_b)
172 return -1;
173 if (time_a < time_b)
174 return 1;
175 return 0;
179 * Transient statuses are added and removed automatically by
180 * Purple. If they're not used for a certain length of time then
181 * they'll expire and be automatically removed. This function
182 * does the expiration.
184 static void
185 remove_old_transient_statuses(void)
187 GList *l, *next;
188 PurpleSavedStatus *saved_status, *startup_status, *current_status;
189 int count;
190 time_t creation_time;
192 startup_status = purple_savedstatus_get_startup();
193 current_status = purple_savedstatus_get_current();
196 * Iterate through the list of saved statuses. Delete all
197 * transient statuses except for the first MAX_TRANSIENTS
198 * (remember, the saved statuses are already sorted by popularity).
199 * We should also keep the startup status, if any is set.
201 count = 0;
202 for (l = saved_statuses; l != NULL; l = next)
204 next = l->next;
205 saved_status = l->data;
206 if (purple_savedstatus_is_transient(saved_status))
208 if (count == MAX_TRANSIENTS)
210 if (saved_status != current_status && saved_status != startup_status)
212 saved_statuses = g_list_remove(saved_statuses, saved_status);
213 creation_time = purple_savedstatus_get_creation_time(saved_status);
214 g_hash_table_remove(creation_times, (gconstpointer)creation_time);
215 free_saved_status(saved_status);
218 else
219 count++;
223 if (count == MAX_TRANSIENTS)
224 schedule_save();
227 /*********************************************************************
228 * Writing to disk *
229 *********************************************************************/
231 static PurpleXmlNode *
232 substatus_to_xmlnode(PurpleSavedStatusSub *substatus)
234 PurpleXmlNode *node, *child;
236 node = purple_xmlnode_new("substatus");
238 child = purple_xmlnode_new_child(node, "account");
239 purple_xmlnode_set_attrib(child, "protocol", purple_account_get_protocol_id(substatus->account));
240 purple_xmlnode_insert_data(child,
241 purple_normalize(substatus->account,
242 purple_account_get_username(substatus->account)), -1);
244 child = purple_xmlnode_new_child(node, "state");
245 purple_xmlnode_insert_data(child, purple_status_type_get_id(substatus->type), -1);
247 if (substatus->message != NULL)
249 child = purple_xmlnode_new_child(node, "message");
250 purple_xmlnode_insert_data(child, substatus->message, -1);
253 return node;
256 static PurpleXmlNode *
257 status_to_xmlnode(PurpleSavedStatus *status)
259 PurpleXmlNode *node, *child;
260 char buf[21];
261 GList *cur;
263 node = purple_xmlnode_new("status");
264 if (status->title != NULL)
266 purple_xmlnode_set_attrib(node, "name", status->title);
268 else
271 * Purple 1.5.0 and earlier require a name to be set, so we
272 * do this little hack to maintain backward compatability
273 * in the status.xml file. Eventually this should be removed
274 * and we should determine if a status is transient by
275 * whether the "name" attribute is set to something or if
276 * it does not exist at all.
278 purple_xmlnode_set_attrib(node, "name", "Auto-Cached");
279 purple_xmlnode_set_attrib(node, "transient", "true");
282 g_snprintf(buf, sizeof(buf), "%lu", status->creation_time);
283 purple_xmlnode_set_attrib(node, "created", buf);
285 g_snprintf(buf, sizeof(buf), "%lu", status->lastused);
286 purple_xmlnode_set_attrib(node, "lastused", buf);
288 g_snprintf(buf, sizeof(buf), "%u", status->usage_count);
289 purple_xmlnode_set_attrib(node, "usage_count", buf);
291 child = purple_xmlnode_new_child(node, "state");
292 purple_xmlnode_insert_data(child, purple_primitive_get_id_from_type(status->type), -1);
294 if (status->message != NULL)
296 child = purple_xmlnode_new_child(node, "message");
297 purple_xmlnode_insert_data(child, status->message, -1);
300 for (cur = status->substatuses; cur != NULL; cur = cur->next)
302 child = substatus_to_xmlnode(cur->data);
303 purple_xmlnode_insert_child(node, child);
306 return node;
309 static PurpleXmlNode *
310 statuses_to_xmlnode(void)
312 PurpleXmlNode *node, *child;
313 GList *cur;
315 node = purple_xmlnode_new("statuses");
316 purple_xmlnode_set_attrib(node, "version", "1.0");
318 for (cur = saved_statuses; cur != NULL; cur = cur->next)
320 child = status_to_xmlnode(cur->data);
321 purple_xmlnode_insert_child(node, child);
324 return node;
327 static void
328 sync_statuses(void)
330 PurpleXmlNode *node;
331 char *data;
333 if (!statuses_loaded)
335 purple_debug_error("status", "Attempted to save statuses before they "
336 "were read!\n");
337 return;
340 node = statuses_to_xmlnode();
341 data = purple_xmlnode_to_formatted_str(node, NULL);
342 purple_util_write_data_to_config_file("status.xml", data, -1);
343 g_free(data);
344 purple_xmlnode_free(node);
347 static gboolean
348 save_cb(gpointer data)
350 sync_statuses();
351 save_timer = 0;
352 return FALSE;
355 static void
356 schedule_save(void)
358 if (save_timer == 0)
359 save_timer = g_timeout_add_seconds(5, save_cb, NULL);
363 /*********************************************************************
364 * Reading from disk *
365 *********************************************************************/
367 static PurpleSavedStatusSub *
368 parse_substatus(PurpleXmlNode *substatus)
370 PurpleSavedStatusSub *ret;
371 PurpleXmlNode *node;
372 char *data;
374 ret = g_new0(PurpleSavedStatusSub, 1);
376 /* Read the account */
377 node = purple_xmlnode_get_child(substatus, "account");
378 if (node != NULL)
380 char *acct_name;
381 const char *protocol;
382 acct_name = purple_xmlnode_get_data(node);
383 protocol = purple_xmlnode_get_attrib(node, "protocol");
384 if ((acct_name != NULL) && (protocol != NULL))
385 ret->account = purple_accounts_find(acct_name, protocol);
386 g_free(acct_name);
389 if (ret->account == NULL)
391 g_free(ret);
392 return NULL;
395 /* Read the state */
396 node = purple_xmlnode_get_child(substatus, "state");
397 if ((node != NULL) && ((data = purple_xmlnode_get_data(node)) != NULL))
399 ret->type = purple_status_type_find_with_id(
400 purple_account_get_status_types(ret->account), data);
401 g_free(data);
404 if (ret->type == NULL)
406 g_free(ret);
407 return NULL;
410 /* Read the message */
411 node = purple_xmlnode_get_child(substatus, "message");
412 if ((node != NULL) && ((data = purple_xmlnode_get_data(node)) != NULL))
414 ret->message = data;
417 return ret;
421 * Parse a saved status and add it to the saved_statuses linked list.
423 * Here's an example of the XML for a saved status:
424 * <status name="Girls">
425 * <state>away</state>
426 * <message>I like the way that they walk
427 * And it's chill to hear them talk
428 * And I can always make them smile
429 * From White Castle to the Nile</message>
430 * <substatus>
431 * <account protocol='aim'>markdoliner</account>
432 * <state>available</state>
433 * <message>The ladies man is here to answer your queries.</message>
434 * </substatus>
435 * <substatus>
436 * <account protocol='aim'>giantgraypanda</account>
437 * <state>away</state>
438 * <message>A.C. ain't in charge no more.</message>
439 * </substatus>
440 * </status>
442 * I know. Moving, huh?
444 static PurpleSavedStatus *
445 parse_status(PurpleXmlNode *status)
447 PurpleSavedStatus *ret;
448 PurpleXmlNode *node;
449 const char *attrib;
450 char *data;
451 int i;
453 ret = g_new0(PurpleSavedStatus, 1);
455 attrib = purple_xmlnode_get_attrib(status, "transient");
456 if (!purple_strequal(attrib, "true"))
458 /* Read the title */
459 attrib = purple_xmlnode_get_attrib(status, "name");
460 ret->title = g_strdup(attrib);
463 if (ret->title != NULL)
465 /* Ensure the title is unique */
466 i = 2;
467 while (purple_savedstatus_find(ret->title) != NULL)
469 g_free(ret->title);
470 ret->title = g_strdup_printf("%s %d", attrib, i);
471 i++;
475 /* Read the creation time */
476 attrib = purple_xmlnode_get_attrib(status, "created");
477 set_creation_time(ret, (attrib != NULL ? atol(attrib) : 0));
479 /* Read the last used time */
480 attrib = purple_xmlnode_get_attrib(status, "lastused");
481 ret->lastused = (attrib != NULL ? atol(attrib) : 0);
483 /* Read the usage count */
484 attrib = purple_xmlnode_get_attrib(status, "usage_count");
485 ret->usage_count = (attrib != NULL ? atol(attrib) : 0);
487 /* Read the primitive status type */
488 node = purple_xmlnode_get_child(status, "state");
489 if ((node != NULL) && ((data = purple_xmlnode_get_data(node)) != NULL))
491 ret->type = purple_primitive_get_type_from_id(data);
492 g_free(data);
495 /* Read the message */
496 node = purple_xmlnode_get_child(status, "message");
497 if ((node != NULL) && ((data = purple_xmlnode_get_data(node)) != NULL))
499 ret->message = data;
502 /* Read substatuses */
503 for (node = purple_xmlnode_get_child(status, "substatus"); node != NULL;
504 node = purple_xmlnode_get_next_twin(node))
506 PurpleSavedStatusSub *new;
507 new = parse_substatus(node);
508 if (new != NULL)
509 ret->substatuses = g_list_prepend(ret->substatuses, new);
512 return ret;
516 * load_statuses:
518 * Read the saved statuses from a file in the Purple user dir.
520 * Returns: TRUE on success, FALSE on failure (if the file can not
521 * be opened, or if it contains invalid XML).
523 static void
524 load_statuses(void)
526 PurpleXmlNode *statuses, *status;
528 statuses_loaded = TRUE;
530 statuses = purple_util_read_xml_from_config_file("status.xml", _("saved statuses"));
532 if (statuses == NULL)
533 return;
535 for (status = purple_xmlnode_get_child(statuses, "status"); status != NULL;
536 status = purple_xmlnode_get_next_twin(status))
538 PurpleSavedStatus *new;
539 new = parse_status(status);
540 saved_statuses = g_list_prepend(saved_statuses, new);
542 saved_statuses = g_list_sort(saved_statuses, saved_statuses_sort_func);
544 purple_xmlnode_free(statuses);
548 /**************************************************************************
549 * Saved status API
550 **************************************************************************/
551 PurpleSavedStatus *
552 purple_savedstatus_new(const char *title, PurpleStatusPrimitive type)
554 PurpleSavedStatus *status;
556 /* Make sure we don't already have a saved status with this title. */
557 if (title != NULL)
558 g_return_val_if_fail(purple_savedstatus_find(title) == NULL, NULL);
560 status = g_new0(PurpleSavedStatus, 1);
561 status->title = g_strdup(title);
562 status->type = type;
563 set_creation_time(status, time(NULL));
565 saved_statuses = g_list_insert_sorted(saved_statuses, status, saved_statuses_sort_func);
567 schedule_save();
569 purple_signal_emit(purple_savedstatuses_get_handle(), "savedstatus-added",
570 status);
572 return status;
575 void
576 purple_savedstatus_set_title(PurpleSavedStatus *status, const char *title)
578 g_return_if_fail(status != NULL);
580 /* Make sure we don't already have a saved status with this title. */
581 g_return_if_fail(purple_savedstatus_find(title) == NULL);
583 g_free(status->title);
584 status->title = g_strdup(title);
586 schedule_save();
588 purple_signal_emit(purple_savedstatuses_get_handle(),
589 "savedstatus-modified", status);
592 void
593 purple_savedstatus_set_primitive_type(PurpleSavedStatus *status, PurpleStatusPrimitive type)
595 g_return_if_fail(status != NULL);
597 status->type = type;
599 schedule_save();
600 purple_signal_emit(purple_savedstatuses_get_handle(),
601 "savedstatus-modified", status);
604 void
605 purple_savedstatus_set_message(PurpleSavedStatus *status, const char *message)
607 g_return_if_fail(status != NULL);
609 g_free(status->message);
610 if ((message != NULL) && (*message == '\0'))
611 status->message = NULL;
612 else
613 status->message = g_strdup(message);
615 schedule_save();
617 purple_signal_emit(purple_savedstatuses_get_handle(),
618 "savedstatus-modified", status);
621 void
622 purple_savedstatus_set_substatus(PurpleSavedStatus *saved_status,
623 const PurpleAccount *account,
624 const PurpleStatusType *type,
625 const char *message)
627 PurpleSavedStatusSub *substatus;
629 g_return_if_fail(saved_status != NULL);
630 g_return_if_fail(account != NULL);
631 g_return_if_fail(type != NULL);
633 /* Find an existing substatus or create a new one */
634 substatus = purple_savedstatus_get_substatus(saved_status, account);
635 if (substatus == NULL)
637 substatus = g_new0(PurpleSavedStatusSub, 1);
638 substatus->account = (PurpleAccount *)account;
639 saved_status->substatuses = g_list_prepend(saved_status->substatuses, substatus);
642 substatus->type = type;
643 g_free(substatus->message);
644 substatus->message = g_strdup(message);
646 schedule_save();
647 purple_signal_emit(purple_savedstatuses_get_handle(),
648 "savedstatus-modified", saved_status);
651 void
652 purple_savedstatus_unset_substatus(PurpleSavedStatus *saved_status,
653 const PurpleAccount *account)
655 GList *iter;
656 PurpleSavedStatusSub *substatus;
658 g_return_if_fail(saved_status != NULL);
659 g_return_if_fail(account != NULL);
661 for (iter = saved_status->substatuses; iter != NULL; iter = iter->next)
663 substatus = iter->data;
664 if (substatus->account == account)
666 saved_status->substatuses = g_list_delete_link(saved_status->substatuses, iter);
667 g_free(substatus->message);
668 g_free(substatus);
669 return;
673 purple_signal_emit(purple_savedstatuses_get_handle(),
674 "savedstatus-modified", saved_status);
678 * This gets called when an account is deleted. We iterate through
679 * all of our saved statuses and delete any substatuses that may
680 * exist for this account.
682 static void
683 purple_savedstatus_unset_all_substatuses(const PurpleAccount *account,
684 gpointer user_data)
686 GList *iter;
687 PurpleSavedStatus *status;
689 g_return_if_fail(account != NULL);
691 for (iter = saved_statuses; iter != NULL; iter = iter->next)
693 status = (PurpleSavedStatus *)iter->data;
694 purple_savedstatus_unset_substatus(status, account);
698 void
699 purple_savedstatus_delete_by_status(PurpleSavedStatus *status)
701 time_t creation_time, current, idleaway;
703 g_return_if_fail(status != NULL);
705 saved_statuses = g_list_remove(saved_statuses, status);
706 creation_time = purple_savedstatus_get_creation_time(status);
707 g_hash_table_remove(creation_times, (gconstpointer)creation_time);
708 free_saved_status(status);
710 schedule_save();
713 * If we just deleted our current status or our idleaway status,
714 * then set the appropriate pref back to 0.
716 current = purple_prefs_get_int("/purple/savedstatus/default");
717 if (current == creation_time)
718 purple_prefs_set_int("/purple/savedstatus/default", 0);
720 idleaway = purple_prefs_get_int("/purple/savedstatus/idleaway");
721 if (idleaway == creation_time)
722 purple_prefs_set_int("/purple/savedstatus/idleaway", 0);
724 purple_signal_emit(purple_savedstatuses_get_handle(),
725 "savedstatus-deleted", status);
728 gboolean
729 purple_savedstatus_delete(const char *title)
731 PurpleSavedStatus *status;
733 status = purple_savedstatus_find(title);
735 if (status == NULL)
736 return FALSE;
738 if (purple_savedstatus_get_current() == status)
739 return FALSE;
741 purple_savedstatus_delete_by_status(status);
743 return TRUE;
746 GList *
747 purple_savedstatuses_get_all(void)
749 return saved_statuses;
752 GList *
753 purple_savedstatuses_get_popular(unsigned int how_many)
755 GList *popular = NULL;
756 GList *cur;
757 unsigned int i;
758 PurpleSavedStatus *next;
760 /* Copy 'how_many' elements to a new list. If 'how_many' is 0, then copy all of 'em. */
761 if (how_many == 0)
762 how_many = (unsigned int) -1;
764 i = 0;
765 cur = saved_statuses;
766 while ((i < how_many) && (cur != NULL))
768 next = cur->data;
769 if ((!purple_savedstatus_is_transient(next)
770 || purple_savedstatus_get_message(next) != NULL))
772 popular = g_list_prepend(popular, next);
773 i++;
775 cur = cur->next;
778 popular = g_list_reverse(popular);
780 return popular;
783 PurpleSavedStatus *
784 purple_savedstatus_get_current(void)
786 if (purple_savedstatus_is_idleaway())
787 return purple_savedstatus_get_idleaway();
788 else
789 return purple_savedstatus_get_default();
792 PurpleSavedStatus *
793 purple_savedstatus_get_default()
795 time_t creation_time;
796 PurpleSavedStatus *saved_status = NULL;
798 creation_time = purple_prefs_get_int("/purple/savedstatus/default");
800 if (creation_time != 0)
801 saved_status = g_hash_table_lookup(creation_times, (gconstpointer)creation_time);
803 if (saved_status == NULL)
806 * We don't have a current saved status! This is either a new
807 * Purple user or someone upgrading from Purple 1.5.0 or older, or
808 * possibly someone who deleted the status they were currently
809 * using? In any case, add a default status.
811 saved_status = purple_savedstatus_new(NULL, PURPLE_STATUS_AVAILABLE);
812 purple_prefs_set_int("/purple/savedstatus/default",
813 purple_savedstatus_get_creation_time(saved_status));
816 return saved_status;
819 PurpleSavedStatus *
820 purple_savedstatus_get_idleaway()
822 time_t creation_time;
823 PurpleSavedStatus *saved_status = NULL;
825 creation_time = purple_prefs_get_int("/purple/savedstatus/idleaway");
827 if (creation_time != 0)
828 saved_status = g_hash_table_lookup(creation_times, (gconstpointer)creation_time);
830 if (saved_status == NULL)
832 /* We don't have a specified "idle" status! Weird. */
833 saved_status = purple_savedstatus_find_transient_by_type_and_message(
834 PURPLE_STATUS_AWAY, DEFAULT_AUTOAWAY_MESSAGE);
836 if (saved_status == NULL)
838 saved_status = purple_savedstatus_new(NULL, PURPLE_STATUS_AWAY);
839 purple_savedstatus_set_message(saved_status, DEFAULT_AUTOAWAY_MESSAGE);
840 purple_prefs_set_int("/purple/savedstatus/idleaway",
841 purple_savedstatus_get_creation_time(saved_status));
845 return saved_status;
848 gboolean
849 purple_savedstatus_is_idleaway()
851 return purple_prefs_get_bool("/purple/savedstatus/isidleaway");
854 void
855 purple_savedstatus_set_idleaway(gboolean idleaway)
857 GList *accounts, *node;
858 PurpleSavedStatus *old, *saved_status;
860 if (purple_savedstatus_is_idleaway() == idleaway)
861 /* Don't need to do anything */
862 return;
864 old = purple_savedstatus_get_current();
865 saved_status = idleaway ? purple_savedstatus_get_idleaway()
866 : purple_savedstatus_get_default();
867 purple_prefs_set_bool("/purple/savedstatus/isidleaway", idleaway);
869 /* Changing our status makes us un-idle */
870 if (!idleaway)
871 purple_idle_touch();
873 if (idleaway && (purple_savedstatus_get_primitive_type(old) != PURPLE_STATUS_AVAILABLE))
874 /* Our global status is already "away," so don't change anything */
875 return;
877 accounts = purple_accounts_get_all_active();
878 for (node = accounts; node != NULL; node = node->next)
880 PurpleAccount *account;
881 PurplePresence *presence;
882 PurpleStatus *status;
884 account = node->data;
885 presence = purple_account_get_presence(account);
886 status = purple_presence_get_active_status(presence);
888 if (!idleaway || purple_status_is_available(status))
889 purple_savedstatus_activate_for_account(saved_status, account);
892 g_list_free(accounts);
894 purple_signal_emit(purple_savedstatuses_get_handle(), "savedstatus-changed",
895 saved_status, old);
898 PurpleSavedStatus *
899 purple_savedstatus_get_startup()
901 time_t creation_time;
902 PurpleSavedStatus *saved_status = NULL;
904 creation_time = purple_prefs_get_int("/purple/savedstatus/startup");
906 if (creation_time != 0)
907 saved_status = g_hash_table_lookup(creation_times, (gconstpointer)creation_time);
909 if (saved_status == NULL)
912 * We don't have a status to apply.
913 * This may be the first login, or the user wants to
914 * restore the "current" status.
916 saved_status = purple_savedstatus_get_current();
919 return saved_status;
923 PurpleSavedStatus *
924 purple_savedstatus_find(const char *title)
926 GList *iter;
927 PurpleSavedStatus *status;
929 g_return_val_if_fail(title != NULL, NULL);
931 for (iter = saved_statuses; iter != NULL; iter = iter->next)
933 status = (PurpleSavedStatus *)iter->data;
934 if (purple_strequal(status->title, title))
935 return status;
938 return NULL;
941 PurpleSavedStatus *
942 purple_savedstatus_find_by_creation_time(time_t creation_time)
944 GList *iter;
945 PurpleSavedStatus *status;
947 for (iter = saved_statuses; iter != NULL; iter = iter->next)
949 status = (PurpleSavedStatus *)iter->data;
950 if (status->creation_time == creation_time)
951 return status;
954 return NULL;
957 PurpleSavedStatus *
958 purple_savedstatus_find_transient_by_type_and_message(PurpleStatusPrimitive type,
959 const char *message)
961 GList *iter;
962 PurpleSavedStatus *status;
964 for (iter = saved_statuses; iter != NULL; iter = iter->next)
966 status = (PurpleSavedStatus *)iter->data;
967 if ((status->type == type) && purple_savedstatus_is_transient(status) &&
968 !purple_savedstatus_has_substatuses(status) &&
969 purple_strequal(status->message, message))
971 return status;
975 return NULL;
978 gboolean
979 purple_savedstatus_is_transient(const PurpleSavedStatus *saved_status)
981 g_return_val_if_fail(saved_status != NULL, TRUE);
983 return (saved_status->title == NULL);
986 const char *
987 purple_savedstatus_get_title(const PurpleSavedStatus *saved_status)
989 const char *message;
991 g_return_val_if_fail(saved_status != NULL, NULL);
993 /* If we have a title then return it */
994 if (saved_status->title != NULL)
995 return saved_status->title;
997 /* Otherwise, this is a transient status and we make up a title on the fly */
998 message = purple_savedstatus_get_message(saved_status);
1000 if ((message == NULL) || (*message == '\0'))
1002 PurpleStatusPrimitive primitive;
1003 primitive = purple_savedstatus_get_primitive_type(saved_status);
1004 return purple_primitive_get_name_from_type(primitive);
1006 else
1008 char *stripped;
1009 static char buf[64];
1010 stripped = purple_markup_strip_html(message);
1011 purple_util_chrreplace(stripped, '\n', ' ');
1012 strncpy(buf, stripped, sizeof(buf));
1013 buf[sizeof(buf) - 1] = '\0';
1014 if ((strlen(stripped) + 1) > sizeof(buf))
1016 /* Truncate and ellipsize */
1017 char *tmp = g_utf8_find_prev_char(buf, &buf[sizeof(buf) - 4]);
1018 strcpy(tmp, "...");
1020 g_free(stripped);
1021 return buf;
1025 PurpleStatusPrimitive
1026 purple_savedstatus_get_primitive_type(const PurpleSavedStatus *saved_status)
1028 g_return_val_if_fail(saved_status != NULL, PURPLE_STATUS_OFFLINE);
1030 return saved_status->type;
1033 const char *
1034 purple_savedstatus_get_message(const PurpleSavedStatus *saved_status)
1036 g_return_val_if_fail(saved_status != NULL, NULL);
1038 return saved_status->message;
1041 time_t
1042 purple_savedstatus_get_creation_time(const PurpleSavedStatus *saved_status)
1044 g_return_val_if_fail(saved_status != NULL, 0);
1046 return saved_status->creation_time;
1049 gboolean
1050 purple_savedstatus_has_substatuses(const PurpleSavedStatus *saved_status)
1052 g_return_val_if_fail(saved_status != NULL, FALSE);
1054 return (saved_status->substatuses != NULL);
1057 PurpleSavedStatusSub *
1058 purple_savedstatus_get_substatus(const PurpleSavedStatus *saved_status,
1059 const PurpleAccount *account)
1061 GList *iter;
1062 PurpleSavedStatusSub *substatus;
1064 g_return_val_if_fail(saved_status != NULL, NULL);
1065 g_return_val_if_fail(account != NULL, NULL);
1067 for (iter = saved_status->substatuses; iter != NULL; iter = iter->next)
1069 substatus = iter->data;
1070 if (substatus->account == account)
1071 return substatus;
1074 return NULL;
1077 const PurpleStatusType *
1078 purple_savedstatus_substatus_get_status_type(const PurpleSavedStatusSub *substatus)
1080 g_return_val_if_fail(substatus != NULL, NULL);
1082 return substatus->type;
1085 const char *
1086 purple_savedstatus_substatus_get_message(const PurpleSavedStatusSub *substatus)
1088 g_return_val_if_fail(substatus != NULL, NULL);
1090 return substatus->message;
1093 void
1094 purple_savedstatus_activate(PurpleSavedStatus *saved_status)
1096 GList *accounts, *node;
1097 PurpleSavedStatus *old = purple_savedstatus_get_current();
1099 g_return_if_fail(saved_status != NULL);
1101 /* Make sure our list of saved statuses remains sorted */
1102 saved_status->lastused = time(NULL);
1103 saved_status->usage_count++;
1104 saved_statuses = g_list_remove(saved_statuses, saved_status);
1105 saved_statuses = g_list_insert_sorted(saved_statuses, saved_status, saved_statuses_sort_func);
1106 purple_prefs_set_int("/purple/savedstatus/default",
1107 purple_savedstatus_get_creation_time(saved_status));
1109 accounts = purple_accounts_get_all_active();
1110 for (node = accounts; node != NULL; node = node->next)
1112 PurpleAccount *account;
1114 account = node->data;
1116 purple_savedstatus_activate_for_account(saved_status, account);
1119 g_list_free(accounts);
1121 if (purple_savedstatus_is_idleaway()) {
1122 purple_savedstatus_set_idleaway(FALSE);
1123 } else {
1124 purple_signal_emit(purple_savedstatuses_get_handle(), "savedstatus-changed",
1125 saved_status, old);
1129 void
1130 purple_savedstatus_activate_for_account(const PurpleSavedStatus *saved_status,
1131 PurpleAccount *account)
1133 const PurpleStatusType *status_type;
1134 const PurpleSavedStatusSub *substatus;
1135 const char *message = NULL;
1137 g_return_if_fail(saved_status != NULL);
1138 g_return_if_fail(account != NULL);
1140 substatus = purple_savedstatus_get_substatus(saved_status, account);
1141 if (substatus != NULL)
1143 status_type = substatus->type;
1144 message = substatus->message;
1146 else
1148 status_type = purple_account_get_status_type_with_primitive(account, saved_status->type);
1149 if (status_type == NULL)
1150 return;
1151 message = saved_status->message;
1154 if ((message != NULL) &&
1155 (purple_status_type_get_attr(status_type, "message")))
1157 purple_account_set_status(account, purple_status_type_get_id(status_type),
1158 TRUE, "message", message, NULL);
1160 else
1162 purple_account_set_status(account, purple_status_type_get_id(status_type),
1163 TRUE, NULL);
1167 static PurpleSavedStatus *
1168 purple_savedstatus_copy(PurpleSavedStatus *savedstatus)
1170 PurpleSavedStatus *savedstatus_copy;
1172 g_return_val_if_fail(savedstatus != NULL, NULL);
1174 savedstatus_copy = g_new(PurpleSavedStatus, 1);
1175 *savedstatus_copy = *savedstatus;
1177 return savedstatus_copy;
1180 GType
1181 purple_savedstatus_get_type(void)
1183 static GType type = 0;
1185 if (type == 0) {
1186 type = g_boxed_type_register_static("PurpleSavedStatus",
1187 (GBoxedCopyFunc)purple_savedstatus_copy,
1188 (GBoxedFreeFunc)g_free);
1191 return type;
1194 void *
1195 purple_savedstatuses_get_handle(void)
1197 static int handle;
1199 return &handle;
1202 void
1203 purple_savedstatuses_init(void)
1205 void *handle = purple_savedstatuses_get_handle();
1207 creation_times = g_hash_table_new(g_direct_hash, g_direct_equal);
1210 * Using 0 as the creation_time is a special case.
1211 * If someone calls purple_savedstatus_get_current() or
1212 * purple_savedstatus_get_idleaway() and either of those functions
1213 * sees a creation_time of 0, then it will create a default
1214 * saved status and return that to the user.
1216 purple_prefs_add_none("/purple/savedstatus");
1217 purple_prefs_add_int("/purple/savedstatus/default", 0);
1218 purple_prefs_add_int("/purple/savedstatus/startup", 0);
1219 purple_prefs_add_bool("/purple/savedstatus/startup_current_status", TRUE);
1220 purple_prefs_add_int("/purple/savedstatus/idleaway", 0);
1221 purple_prefs_add_bool("/purple/savedstatus/isidleaway", FALSE);
1223 load_statuses();
1225 purple_signal_register(handle, "savedstatus-changed",
1226 purple_marshal_VOID__POINTER_POINTER, G_TYPE_NONE, 2,
1227 PURPLE_TYPE_SAVEDSTATUS, PURPLE_TYPE_SAVEDSTATUS);
1229 purple_signal_register(handle, "savedstatus-added",
1230 purple_marshal_VOID__POINTER, G_TYPE_NONE, 1,
1231 PURPLE_TYPE_SAVEDSTATUS);
1233 purple_signal_register(handle, "savedstatus-deleted",
1234 purple_marshal_VOID__POINTER, G_TYPE_NONE, 1,
1235 PURPLE_TYPE_SAVEDSTATUS);
1237 purple_signal_register(handle, "savedstatus-modified",
1238 purple_marshal_VOID__POINTER, G_TYPE_NONE, 1,
1239 PURPLE_TYPE_SAVEDSTATUS);
1241 purple_signal_connect(purple_accounts_get_handle(), "account-removed",
1242 handle,
1243 PURPLE_CALLBACK(purple_savedstatus_unset_all_substatuses),
1244 NULL);
1247 void
1248 purple_savedstatuses_uninit(void)
1250 gpointer handle = purple_savedstatuses_get_handle();
1252 remove_old_transient_statuses();
1254 if (save_timer != 0)
1256 g_source_remove(save_timer);
1257 save_timer = 0;
1258 sync_statuses();
1261 while (saved_statuses != NULL) {
1262 PurpleSavedStatus *saved_status = saved_statuses->data;
1263 saved_statuses = g_list_remove(saved_statuses, saved_status);
1264 free_saved_status(saved_status);
1267 g_hash_table_destroy(creation_times);
1268 creation_times = NULL;
1270 purple_signals_unregister_by_instance(handle);
1271 purple_signals_disconnect_by_handle(handle);