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
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
26 #include "savedstatuses.h"
33 * The maximum number of transient statuses to save. This
34 * is used during the shutdown process to clean out old
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
58 PurpleStatusPrimitive type
;
61 /* The timestamp when this saved status was created. This must be unique. */
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
;
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 *********************************************************************/
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
);
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
);
132 * Set the timestamp for when this saved status was created, and
133 * make sure it is unique.
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
,
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
158 * The goal is to have recently used statuses at the top of
159 * the list, but to also keep frequently used statuses near
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);
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.
185 remove_old_transient_statuses(void)
188 PurpleSavedStatus
*saved_status
, *startup_status
, *current_status
;
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.
202 for (l
= saved_statuses
; l
!= NULL
; 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
);
223 if (count
== MAX_TRANSIENTS
)
227 /*********************************************************************
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);
256 static PurpleXmlNode
*
257 status_to_xmlnode(PurpleSavedStatus
*status
)
259 PurpleXmlNode
*node
, *child
;
263 node
= purple_xmlnode_new("status");
264 if (status
->title
!= NULL
)
266 purple_xmlnode_set_attrib(node
, "name", status
->title
);
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
);
309 static PurpleXmlNode
*
310 statuses_to_xmlnode(void)
312 PurpleXmlNode
*node
, *child
;
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
);
333 if (!statuses_loaded
)
335 purple_debug_error("status", "Attempted to save statuses before they "
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);
344 purple_xmlnode_free(node
);
348 save_cb(gpointer data
)
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
;
374 ret
= g_new0(PurpleSavedStatusSub
, 1);
376 /* Read the account */
377 node
= purple_xmlnode_get_child(substatus
, "account");
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
);
389 if (ret
->account
== NULL
)
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
);
404 if (ret
->type
== NULL
)
410 /* Read the message */
411 node
= purple_xmlnode_get_child(substatus
, "message");
412 if ((node
!= NULL
) && ((data
= purple_xmlnode_get_data(node
)) != NULL
))
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>
431 * <account protocol='aim'>markdoliner</account>
432 * <state>available</state>
433 * <message>The ladies man is here to answer your queries.</message>
436 * <account protocol='aim'>giantgraypanda</account>
437 * <state>away</state>
438 * <message>A.C. ain't in charge no more.</message>
442 * I know. Moving, huh?
444 static PurpleSavedStatus
*
445 parse_status(PurpleXmlNode
*status
)
447 PurpleSavedStatus
*ret
;
453 ret
= g_new0(PurpleSavedStatus
, 1);
455 attrib
= purple_xmlnode_get_attrib(status
, "transient");
456 if (!purple_strequal(attrib
, "true"))
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 */
467 while (purple_savedstatus_find(ret
->title
) != NULL
)
470 ret
->title
= g_strdup_printf("%s %d", attrib
, 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
);
495 /* Read the message */
496 node
= purple_xmlnode_get_child(status
, "message");
497 if ((node
!= NULL
) && ((data
= purple_xmlnode_get_data(node
)) != NULL
))
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
);
509 ret
->substatuses
= g_list_prepend(ret
->substatuses
, new);
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).
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
)
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 /**************************************************************************
550 **************************************************************************/
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. */
558 g_return_val_if_fail(purple_savedstatus_find(title
) == NULL
, NULL
);
560 status
= g_new0(PurpleSavedStatus
, 1);
561 status
->title
= g_strdup(title
);
563 set_creation_time(status
, time(NULL
));
565 saved_statuses
= g_list_insert_sorted(saved_statuses
, status
, saved_statuses_sort_func
);
569 purple_signal_emit(purple_savedstatuses_get_handle(), "savedstatus-added",
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
);
588 purple_signal_emit(purple_savedstatuses_get_handle(),
589 "savedstatus-modified", status
);
593 purple_savedstatus_set_primitive_type(PurpleSavedStatus
*status
, PurpleStatusPrimitive type
)
595 g_return_if_fail(status
!= NULL
);
600 purple_signal_emit(purple_savedstatuses_get_handle(),
601 "savedstatus-modified", status
);
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
;
613 status
->message
= g_strdup(message
);
617 purple_signal_emit(purple_savedstatuses_get_handle(),
618 "savedstatus-modified", status
);
622 purple_savedstatus_set_substatus(PurpleSavedStatus
*saved_status
,
623 const PurpleAccount
*account
,
624 const PurpleStatusType
*type
,
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
);
647 purple_signal_emit(purple_savedstatuses_get_handle(),
648 "savedstatus-modified", saved_status
);
652 purple_savedstatus_unset_substatus(PurpleSavedStatus
*saved_status
,
653 const PurpleAccount
*account
)
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
);
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.
683 purple_savedstatus_unset_all_substatuses(const PurpleAccount
*account
,
686 g_return_if_fail(account
!= NULL
);
688 g_list_foreach(saved_statuses
, (GFunc
)purple_savedstatus_unset_substatus
, account
);
692 purple_savedstatus_delete_by_status(PurpleSavedStatus
*status
)
694 time_t creation_time
, current
, idleaway
;
696 g_return_if_fail(status
!= NULL
);
698 saved_statuses
= g_list_remove(saved_statuses
, status
);
699 creation_time
= purple_savedstatus_get_creation_time(status
);
700 g_hash_table_remove(creation_times
, (gconstpointer
)creation_time
);
701 free_saved_status(status
);
706 * If we just deleted our current status or our idleaway status,
707 * then set the appropriate pref back to 0.
709 current
= purple_prefs_get_int("/purple/savedstatus/default");
710 if (current
== creation_time
)
711 purple_prefs_set_int("/purple/savedstatus/default", 0);
713 idleaway
= purple_prefs_get_int("/purple/savedstatus/idleaway");
714 if (idleaway
== creation_time
)
715 purple_prefs_set_int("/purple/savedstatus/idleaway", 0);
717 purple_signal_emit(purple_savedstatuses_get_handle(),
718 "savedstatus-deleted", status
);
722 purple_savedstatus_delete(const char *title
)
724 PurpleSavedStatus
*status
;
726 status
= purple_savedstatus_find(title
);
731 if (purple_savedstatus_get_current() == status
)
734 purple_savedstatus_delete_by_status(status
);
740 purple_savedstatuses_get_all(void)
742 return saved_statuses
;
746 purple_savedstatuses_get_popular(unsigned int how_many
)
748 GList
*popular
= NULL
;
751 PurpleSavedStatus
*next
;
753 /* Copy 'how_many' elements to a new list. If 'how_many' is 0, then copy all of 'em. */
755 how_many
= (unsigned int) -1;
758 cur
= saved_statuses
;
759 while ((i
< how_many
) && (cur
!= NULL
))
762 if ((!purple_savedstatus_is_transient(next
)
763 || purple_savedstatus_get_message(next
) != NULL
))
765 popular
= g_list_prepend(popular
, next
);
771 popular
= g_list_reverse(popular
);
777 purple_savedstatus_get_current(void)
779 if (purple_savedstatus_is_idleaway())
780 return purple_savedstatus_get_idleaway();
782 return purple_savedstatus_get_default();
786 purple_savedstatus_get_default()
788 time_t creation_time
;
789 PurpleSavedStatus
*saved_status
= NULL
;
791 creation_time
= purple_prefs_get_int("/purple/savedstatus/default");
793 if (creation_time
!= 0)
794 saved_status
= g_hash_table_lookup(creation_times
, (gconstpointer
)creation_time
);
796 if (saved_status
== NULL
)
799 * We don't have a current saved status! This is either a new
800 * Purple user or someone upgrading from Purple 1.5.0 or older, or
801 * possibly someone who deleted the status they were currently
802 * using? In any case, add a default status.
804 saved_status
= purple_savedstatus_new(NULL
, PURPLE_STATUS_AVAILABLE
);
805 purple_prefs_set_int("/purple/savedstatus/default",
806 purple_savedstatus_get_creation_time(saved_status
));
813 purple_savedstatus_get_idleaway()
815 time_t creation_time
;
816 PurpleSavedStatus
*saved_status
= NULL
;
818 creation_time
= purple_prefs_get_int("/purple/savedstatus/idleaway");
820 if (creation_time
!= 0)
821 saved_status
= g_hash_table_lookup(creation_times
, (gconstpointer
)creation_time
);
823 if (saved_status
== NULL
)
825 /* We don't have a specified "idle" status! Weird. */
826 saved_status
= purple_savedstatus_find_transient_by_type_and_message(
827 PURPLE_STATUS_AWAY
, DEFAULT_AUTOAWAY_MESSAGE
);
829 if (saved_status
== NULL
)
831 saved_status
= purple_savedstatus_new(NULL
, PURPLE_STATUS_AWAY
);
832 purple_savedstatus_set_message(saved_status
, DEFAULT_AUTOAWAY_MESSAGE
);
833 purple_prefs_set_int("/purple/savedstatus/idleaway",
834 purple_savedstatus_get_creation_time(saved_status
));
842 purple_savedstatus_is_idleaway()
844 return purple_prefs_get_bool("/purple/savedstatus/isidleaway");
848 purple_savedstatus_set_idleaway(gboolean idleaway
)
850 GList
*accounts
, *node
;
851 PurpleSavedStatus
*old
, *saved_status
;
853 if (purple_savedstatus_is_idleaway() == idleaway
)
854 /* Don't need to do anything */
857 old
= purple_savedstatus_get_current();
858 saved_status
= idleaway
? purple_savedstatus_get_idleaway()
859 : purple_savedstatus_get_default();
860 purple_prefs_set_bool("/purple/savedstatus/isidleaway", idleaway
);
862 /* Changing our status makes us un-idle */
866 if (idleaway
&& (purple_savedstatus_get_primitive_type(old
) != PURPLE_STATUS_AVAILABLE
))
867 /* Our global status is already "away," so don't change anything */
870 accounts
= purple_accounts_get_all_active();
871 for (node
= accounts
; node
!= NULL
; node
= node
->next
)
873 PurpleAccount
*account
;
874 PurplePresence
*presence
;
875 PurpleStatus
*status
;
877 account
= node
->data
;
878 presence
= purple_account_get_presence(account
);
879 status
= purple_presence_get_active_status(presence
);
881 if (!idleaway
|| purple_status_is_available(status
))
882 purple_savedstatus_activate_for_account(saved_status
, account
);
885 g_list_free(accounts
);
887 purple_signal_emit(purple_savedstatuses_get_handle(), "savedstatus-changed",
892 purple_savedstatus_get_startup()
894 time_t creation_time
;
895 PurpleSavedStatus
*saved_status
= NULL
;
897 creation_time
= purple_prefs_get_int("/purple/savedstatus/startup");
899 if (creation_time
!= 0)
900 saved_status
= g_hash_table_lookup(creation_times
, (gconstpointer
)creation_time
);
902 if (saved_status
== NULL
)
905 * We don't have a status to apply.
906 * This may be the first login, or the user wants to
907 * restore the "current" status.
909 saved_status
= purple_savedstatus_get_current();
917 purple_savedstatus_find(const char *title
)
920 PurpleSavedStatus
*status
;
922 g_return_val_if_fail(title
!= NULL
, NULL
);
924 for (iter
= saved_statuses
; iter
!= NULL
; iter
= iter
->next
)
926 status
= (PurpleSavedStatus
*)iter
->data
;
927 if (purple_strequal(status
->title
, title
))
935 purple_savedstatus_find_by_creation_time(time_t creation_time
)
938 PurpleSavedStatus
*status
;
940 for (iter
= saved_statuses
; iter
!= NULL
; iter
= iter
->next
)
942 status
= (PurpleSavedStatus
*)iter
->data
;
943 if (status
->creation_time
== creation_time
)
951 purple_savedstatus_find_transient_by_type_and_message(PurpleStatusPrimitive type
,
955 PurpleSavedStatus
*status
;
957 for (iter
= saved_statuses
; iter
!= NULL
; iter
= iter
->next
)
959 status
= (PurpleSavedStatus
*)iter
->data
;
960 if ((status
->type
== type
) && purple_savedstatus_is_transient(status
) &&
961 !purple_savedstatus_has_substatuses(status
) &&
962 purple_strequal(status
->message
, message
))
972 purple_savedstatus_is_transient(const PurpleSavedStatus
*saved_status
)
974 g_return_val_if_fail(saved_status
!= NULL
, TRUE
);
976 return (saved_status
->title
== NULL
);
980 purple_savedstatus_get_title(const PurpleSavedStatus
*saved_status
)
984 g_return_val_if_fail(saved_status
!= NULL
, NULL
);
986 /* If we have a title then return it */
987 if (saved_status
->title
!= NULL
)
988 return saved_status
->title
;
990 /* Otherwise, this is a transient status and we make up a title on the fly */
991 message
= purple_savedstatus_get_message(saved_status
);
993 if ((message
== NULL
) || (*message
== '\0'))
995 PurpleStatusPrimitive primitive
;
996 primitive
= purple_savedstatus_get_primitive_type(saved_status
);
997 return purple_primitive_get_name_from_type(primitive
);
1002 static char buf
[64];
1003 stripped
= purple_markup_strip_html(message
);
1004 purple_util_chrreplace(stripped
, '\n', ' ');
1005 strncpy(buf
, stripped
, sizeof(buf
));
1006 buf
[sizeof(buf
) - 1] = '\0';
1007 if ((strlen(stripped
) + 1) > sizeof(buf
))
1009 /* Truncate and ellipsize */
1010 char *tmp
= g_utf8_find_prev_char(buf
, &buf
[sizeof(buf
) - 4]);
1018 PurpleStatusPrimitive
1019 purple_savedstatus_get_primitive_type(const PurpleSavedStatus
*saved_status
)
1021 g_return_val_if_fail(saved_status
!= NULL
, PURPLE_STATUS_OFFLINE
);
1023 return saved_status
->type
;
1027 purple_savedstatus_get_message(const PurpleSavedStatus
*saved_status
)
1029 g_return_val_if_fail(saved_status
!= NULL
, NULL
);
1031 return saved_status
->message
;
1035 purple_savedstatus_get_creation_time(const PurpleSavedStatus
*saved_status
)
1037 g_return_val_if_fail(saved_status
!= NULL
, 0);
1039 return saved_status
->creation_time
;
1043 purple_savedstatus_has_substatuses(const PurpleSavedStatus
*saved_status
)
1045 g_return_val_if_fail(saved_status
!= NULL
, FALSE
);
1047 return (saved_status
->substatuses
!= NULL
);
1050 PurpleSavedStatusSub
*
1051 purple_savedstatus_get_substatus(const PurpleSavedStatus
*saved_status
,
1052 const PurpleAccount
*account
)
1055 PurpleSavedStatusSub
*substatus
;
1057 g_return_val_if_fail(saved_status
!= NULL
, NULL
);
1058 g_return_val_if_fail(account
!= NULL
, NULL
);
1060 for (iter
= saved_status
->substatuses
; iter
!= NULL
; iter
= iter
->next
)
1062 substatus
= iter
->data
;
1063 if (substatus
->account
== account
)
1070 const PurpleStatusType
*
1071 purple_savedstatus_substatus_get_status_type(const PurpleSavedStatusSub
*substatus
)
1073 g_return_val_if_fail(substatus
!= NULL
, NULL
);
1075 return substatus
->type
;
1079 purple_savedstatus_substatus_get_message(const PurpleSavedStatusSub
*substatus
)
1081 g_return_val_if_fail(substatus
!= NULL
, NULL
);
1083 return substatus
->message
;
1087 purple_savedstatus_activate(PurpleSavedStatus
*saved_status
)
1089 GList
*accounts
, *node
;
1090 PurpleSavedStatus
*old
= purple_savedstatus_get_current();
1092 g_return_if_fail(saved_status
!= NULL
);
1094 /* Make sure our list of saved statuses remains sorted */
1095 saved_status
->lastused
= time(NULL
);
1096 saved_status
->usage_count
++;
1097 saved_statuses
= g_list_remove(saved_statuses
, saved_status
);
1098 saved_statuses
= g_list_insert_sorted(saved_statuses
, saved_status
, saved_statuses_sort_func
);
1099 purple_prefs_set_int("/purple/savedstatus/default",
1100 purple_savedstatus_get_creation_time(saved_status
));
1102 accounts
= purple_accounts_get_all_active();
1103 for (node
= accounts
; node
!= NULL
; node
= node
->next
)
1105 PurpleAccount
*account
;
1107 account
= node
->data
;
1109 purple_savedstatus_activate_for_account(saved_status
, account
);
1112 g_list_free(accounts
);
1114 if (purple_savedstatus_is_idleaway()) {
1115 purple_savedstatus_set_idleaway(FALSE
);
1117 purple_signal_emit(purple_savedstatuses_get_handle(), "savedstatus-changed",
1123 purple_savedstatus_activate_for_account(const PurpleSavedStatus
*saved_status
,
1124 PurpleAccount
*account
)
1126 const PurpleStatusType
*status_type
;
1127 const PurpleSavedStatusSub
*substatus
;
1128 const char *message
= NULL
;
1130 g_return_if_fail(saved_status
!= NULL
);
1131 g_return_if_fail(account
!= NULL
);
1133 substatus
= purple_savedstatus_get_substatus(saved_status
, account
);
1134 if (substatus
!= NULL
)
1136 status_type
= substatus
->type
;
1137 message
= substatus
->message
;
1141 status_type
= purple_account_get_status_type_with_primitive(account
, saved_status
->type
);
1142 if (status_type
== NULL
)
1144 message
= saved_status
->message
;
1147 if ((message
!= NULL
) &&
1148 (purple_status_type_get_attr(status_type
, "message")))
1150 purple_account_set_status(account
, purple_status_type_get_id(status_type
),
1151 TRUE
, "message", message
, NULL
);
1155 purple_account_set_status(account
, purple_status_type_get_id(status_type
),
1160 static PurpleSavedStatus
*
1161 purple_savedstatus_copy(PurpleSavedStatus
*savedstatus
)
1163 PurpleSavedStatus
*savedstatus_copy
;
1165 g_return_val_if_fail(savedstatus
!= NULL
, NULL
);
1167 savedstatus_copy
= g_new(PurpleSavedStatus
, 1);
1168 *savedstatus_copy
= *savedstatus
;
1170 return savedstatus_copy
;
1174 purple_savedstatus_get_type(void)
1176 static GType type
= 0;
1179 type
= g_boxed_type_register_static("PurpleSavedStatus",
1180 (GBoxedCopyFunc
)purple_savedstatus_copy
,
1181 (GBoxedFreeFunc
)g_free
);
1188 purple_savedstatuses_get_handle(void)
1196 purple_savedstatuses_init(void)
1198 void *handle
= purple_savedstatuses_get_handle();
1200 creation_times
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
1203 * Using 0 as the creation_time is a special case.
1204 * If someone calls purple_savedstatus_get_current() or
1205 * purple_savedstatus_get_idleaway() and either of those functions
1206 * sees a creation_time of 0, then it will create a default
1207 * saved status and return that to the user.
1209 purple_prefs_add_none("/purple/savedstatus");
1210 purple_prefs_add_int("/purple/savedstatus/default", 0);
1211 purple_prefs_add_int("/purple/savedstatus/startup", 0);
1212 purple_prefs_add_bool("/purple/savedstatus/startup_current_status", TRUE
);
1213 purple_prefs_add_int("/purple/savedstatus/idleaway", 0);
1214 purple_prefs_add_bool("/purple/savedstatus/isidleaway", FALSE
);
1218 purple_signal_register(handle
, "savedstatus-changed",
1219 purple_marshal_VOID__POINTER_POINTER
, G_TYPE_NONE
, 2,
1220 PURPLE_TYPE_SAVEDSTATUS
, PURPLE_TYPE_SAVEDSTATUS
);
1222 purple_signal_register(handle
, "savedstatus-added",
1223 purple_marshal_VOID__POINTER
, G_TYPE_NONE
, 1,
1224 PURPLE_TYPE_SAVEDSTATUS
);
1226 purple_signal_register(handle
, "savedstatus-deleted",
1227 purple_marshal_VOID__POINTER
, G_TYPE_NONE
, 1,
1228 PURPLE_TYPE_SAVEDSTATUS
);
1230 purple_signal_register(handle
, "savedstatus-modified",
1231 purple_marshal_VOID__POINTER
, G_TYPE_NONE
, 1,
1232 PURPLE_TYPE_SAVEDSTATUS
);
1234 purple_signal_connect(purple_accounts_get_handle(), "account-removed",
1236 PURPLE_CALLBACK(purple_savedstatus_unset_all_substatuses
),
1241 purple_savedstatuses_uninit(void)
1243 gpointer handle
= purple_savedstatuses_get_handle();
1245 remove_old_transient_statuses();
1247 if (save_timer
!= 0)
1249 g_source_remove(save_timer
);
1254 while (saved_statuses
!= NULL
) {
1255 PurpleSavedStatus
*saved_status
= saved_statuses
->data
;
1256 saved_statuses
= g_list_remove(saved_statuses
, saved_status
);
1257 free_saved_status(saved_status
);
1260 g_hash_table_destroy(creation_times
);
1261 creation_times
= NULL
;
1263 purple_signals_unregister_by_instance(handle
);
1264 purple_signals_disconnect_by_handle(handle
);