pidgin/pidgin

Fix several leaks in tests

14 months ago, Elliott Sales de Andrade
4e1bf25f5575
Fix several leaks in tests

All of these are specific to tests, not the library code.

For the moment, `protocol_xfer` still leaks connections (and anything they hold on to) because it is very difficult to disentangle them from the connection manager in the partially implemented state they are in.

This fixes leaks of options in the account option test (these two leaks occur for every test since they all leak the option):
```
61 (48 direct, 13 indirect) bytes in 1 blocks are definitely lost in loss record 133 of 276
at 0x4848464: calloc (vg_replace_malloc.c:1340)
by 0x49F75F0: g_malloc0 (gmem.c:163)
by 0x48C3B2E: purple_account_option_new (purpleaccountoption.c:78)
by 0x4014AF: test_purple_account_option_copy_int (test_account_option.c:67)
by 0x4A1CC7D: UnknownInlinedFun (gtestutils.c:2933)
by 0x4A1CC7D: g_test_run_suite_internal (gtestutils.c:3021)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1D181: g_test_run_suite (gtestutils.c:3115)
by 0x4A156EC: UnknownInlinedFun (gtestutils.c:2234)
by 0x4A156EC: g_test_run (gtestutils.c:2221)
by 0x401721: main (test_account_option.c:157)

61 (48 direct, 13 indirect) bytes in 1 blocks are definitely lost in loss record 134 of 276
at 0x4848464: calloc (vg_replace_malloc.c:1340)
by 0x49F75F0: g_malloc0 (gmem.c:163)
by 0x48C3BC7: purple_account_option_copy (purpleaccountoption.c:93)
by 0x4014BF: test_purple_account_option_copy_int (test_account_option.c:68)
by 0x4A1CC7D: UnknownInlinedFun (gtestutils.c:2933)
by 0x4A1CC7D: g_test_run_suite_internal (gtestutils.c:3021)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1D181: g_test_run_suite (gtestutils.c:3115)
by 0x4A156EC: UnknownInlinedFun (gtestutils.c:2234)
by 0x4A156EC: g_test_run (gtestutils.c:2221)
by 0x401721: main (test_account_option.c:157)
```
leaks in the credential manager test (times 3 for read/write/cancel tests):
```
69 (16 direct, 53 indirect) bytes in 1 blocks are definitely lost in loss record 2,427 of 3,503
at 0x484386F: malloc (vg_replace_malloc.c:393)
by 0x4A58168: g_malloc (gmem.c:130)
by 0x4A6FAB5: g_slice_alloc (gslice.c:1074)
by 0x4A700EC: g_slice_alloc0 (gslice.c:1100)
by 0x4A3BECB: g_error_allocate (gerror.c:710)
by 0x4A3C93F: UnknownInlinedFun (gerror.c:724)
by 0x4A3C93F: g_error_new_valist (gerror.c:766)
by 0x4BEE558: g_task_return_new_error (gtask.c:1941)
by 0x48D82C0: purple_credential_manager_read_password_async (purplecredentialmanager.c:492)
by 0x403634: test_purple_credential_manager_no_provider_read_password_idle (test_credential_manager.c:329)
by 0x4A4ECB1: g_idle_dispatch (gmain.c:6124)
by 0x4A4FCBE: UnknownInlinedFun (gmain.c:3444)
by 0x4A4FCBE: g_main_context_dispatch (gmain.c:4162)
by 0x4AA5597: g_main_context_iterate.constprop.0 (gmain.c:4238)
by 0x4A4F28E: g_main_loop_run (gmain.c:4438)
by 0x40369F: test_purple_credential_manager_no_provider_read_password_async (test_credential_manager.c:345)
by 0x4A7DC7D: UnknownInlinedFun (gtestutils.c:2933)
by 0x4A7DC7D: g_test_run_suite_internal (gtestutils.c:3021)
by 0x4A7D9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A7D9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A7E181: g_test_run_suite (gtestutils.c:3115)
by 0x4A766EC: UnknownInlinedFun (gtestutils.c:2234)
by 0x4A766EC: g_test_run (gtestutils.c:2221)
by 0x4048F6: main (test_credential_manager.c:695)
```
a leak in the image test:
```
161 bytes in 1 blocks are definitely lost in loss record 260 of 274
at 0x484386F: malloc (vg_replace_malloc.c:393)
by 0x4A55363: g_try_malloc (gmem.c:286)
by 0x4A3D630: UnknownInlinedFun (gfileutils.c:819)
by 0x4A3D630: UnknownInlinedFun (gfileutils.c:924)
by 0x4A3D630: g_file_get_contents (gfileutils.c:1027)
by 0x401890: test_image_new_from_file (test_image.c:144)
by 0x4A7DC7D: UnknownInlinedFun (gtestutils.c:2933)
by 0x4A7DC7D: g_test_run_suite_internal (gtestutils.c:3021)
by 0x4A7D9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A7E181: g_test_run_suite (gtestutils.c:3115)
by 0x4A766EC: UnknownInlinedFun (gtestutils.c:2234)
by 0x4A766EC: g_test_run (gtestutils.c:2221)
by 0x40195D: main (test_image.c:172)
```
a leak in queued output stream test:
```
72 (40 direct, 32 indirect) bytes in 1 blocks are definitely lost in loss record 219 of 396
at 0x49D51EF: g_type_create_instance (gtype.c:1909)
by 0x49BAC1F: g_object_new_internal (gobject.c:2228)
by 0x49BC247: g_object_new_with_properties (gobject.c:2391)
by 0x49BCFF0: g_object_new (gobject.c:2037)
by 0x402003: test_queued_output_stream_push_bytes_async_error (test_queued_output_stream.c:219)
by 0x4A7DC7D: UnknownInlinedFun (gtestutils.c:2933)
by 0x4A7DC7D: g_test_run_suite_internal (gtestutils.c:3021)
by 0x4A7D9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A7E181: g_test_run_suite (gtestutils.c:3115)
by 0x4A766EC: UnknownInlinedFun (gtestutils.c:2234)
by 0x4A766EC: g_test_run (gtestutils.c:2221)
by 0x402429: main (test_queued_output_stream.c:280)
```
and protocol xfer tests (times 3 for each test that creates a test protocol object):
```
112 (48 direct, 64 indirect) bytes in 1 blocks are definitely lost in loss record 3,430 of 3,698
at 0x49D51EF: g_type_create_instance (gtype.c:1909)
by 0x49BAC1F: g_object_new_internal (gobject.c:2228)
by 0x49BC247: g_object_new_with_properties (gobject.c:2391)
by 0x49BCFF0: g_object_new (gobject.c:2037)
by 0x40291C: test_purple_protocol_xfer_send_file_func (test_protocol_xfer.c:146)
by 0x4A7DC7D: UnknownInlinedFun (gtestutils.c:2933)
by 0x4A7DC7D: g_test_run_suite_internal (gtestutils.c:3021)
by 0x4A7D9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A7E181: g_test_run_suite (gtestutils.c:3115)
by 0x4A766EC: UnknownInlinedFun (gtestutils.c:2234)
by 0x4A766EC: g_test_run (gtestutils.c:2221)
by 0x402B64: main (test_protocol_xfer.c:195)
```
and util tests (times 3 for each call to `purple_text_strip_mnemonic` in the test):
```
5 bytes in 1 blocks are definitely lost in loss record 5 of 247
at 0x484386F: malloc (vg_replace_malloc.c:393)
by 0x49F7168: g_malloc (gmem.c:130)
by 0x491975B: purple_text_strip_mnemonic (util.c:895)
by 0x4015B0: test_util_text_strip_mnemonic (test_util.c:49)
by 0x4A1CC7D: UnknownInlinedFun (gtestutils.c:2933)
by 0x4A1CC7D: g_test_run_suite_internal (gtestutils.c:3021)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1D181: g_test_run_suite (gtestutils.c:3115)
by 0x4A156EC: UnknownInlinedFun (gtestutils.c:2234)
by 0x4A156EC: g_test_run (gtestutils.c:2221)
by 0x401901: main (test_util.c:224)
```
and these leaks in any test that initializes the test UI:
```
4,104 bytes in 1 blocks are possibly lost in loss record 3,451 of 3,457
at 0x484386F: malloc (vg_replace_malloc.c:393)
by 0x5235B67: sqlite3MemMalloc.lto_priv.0 (sqlite3.c:25493)
by 0x5232797: UnknownInlinedFun (sqlite3.c:29181)
by 0x5232797: UnknownInlinedFun (sqlite3.c:29227)
by 0x5232797: sqlite3Malloc.lto_priv.0 (sqlite3.c:29221)
by 0x523BD8B: pcache1Alloc.lto_priv.0 (sqlite3.c:53546)
by 0x5249A8B: UnknownInlinedFun (sqlite3.c:53675)
by 0x5249A8B: allocateTempSpace (sqlite3.c:70848)
by 0x52625A6: sqlite3VdbeExec.lto_priv.0 (sqlite3.c:93857)
by 0x525CBEE: UnknownInlinedFun (sqlite3.c:87995)
by 0x525CBEE: UnknownInlinedFun (sqlite3.c:88056)
by 0x525CBEE: sqlite3_step (sqlite3.c:88045)
by 0x529B324: sqlite3_exec (sqlite3.c:131002)
by 0x48FD558: purple_sqlite3_run_migration (purplesqlite3.c:37)
by 0x48FDBB4: purple_sqlite3_run_migrations_from_resources (purplesqlite3.c:195)
by 0x48FDED9: purple_sqlite_history_adapter_run_migrations (purplesqlitehistoryadapter.c:69)
by 0x48FE7F0: purple_sqlite_history_adapter_activate (purplesqlitehistoryadapter.c:287)
by 0x48DB656: purple_history_adapter_activate (purplehistoryadapter.c:181)
by 0x48DC9BC: purple_history_manager_set_active (purplehistorymanager.c:308)
by 0x402BA8: test_ui_init_history (test_ui.c:132)
by 0x402C80: test_ui_purple_init (test_ui.c:167)
by 0x4027BB: main (test_contact.c:88)

4,368 bytes in 1 blocks are possibly lost in loss record 3,453 of 3,457
at 0x484386F: malloc (vg_replace_malloc.c:393)
by 0x5235B67: sqlite3MemMalloc.lto_priv.0 (sqlite3.c:25493)
by 0x5232797: UnknownInlinedFun (sqlite3.c:29181)
by 0x5232797: UnknownInlinedFun (sqlite3.c:29227)
by 0x5232797: sqlite3Malloc.lto_priv.0 (sqlite3.c:29221)
by 0x523BD8B: pcache1Alloc.lto_priv.0 (sqlite3.c:53546)
by 0x5240077: UnknownInlinedFun (sqlite3.c:53634)
by 0x5240077: pcache1FetchStage2 (sqlite3.c:54104)
by 0x5243E9C: UnknownInlinedFun (sqlite3.c:52671)
by 0x5243E9C: getPageNormal.lto_priv.0 (sqlite3.c:60628)
by 0x524A510: UnknownInlinedFun (sqlite3.c:60805)
by 0x524A510: btreeGetPage.lto_priv.0 (sqlite3.c:70289)
by 0x524C2F6: UnknownInlinedFun (sqlite3.c:71257)
by 0x524C2F6: sqlite3BtreeBeginTrans.lto_priv.0 (sqlite3.c:71647)
by 0x5266B3A: sqlite3VdbeExec.lto_priv.0 (sqlite3.c:93532)
by 0x525CBEE: UnknownInlinedFun (sqlite3.c:87995)
by 0x525CBEE: UnknownInlinedFun (sqlite3.c:88056)
by 0x525CBEE: sqlite3_step (sqlite3.c:88045)
by 0x48FD715: purple_sqlite3_get_schema_version (purplesqlite3.c:79)
by 0x48FD9DD: purple_sqlite3_run_migrations_from_resources (purplesqlite3.c:146)
by 0x48FDED9: purple_sqlite_history_adapter_run_migrations (purplesqlitehistoryadapter.c:69)
by 0x48FE7F0: purple_sqlite_history_adapter_activate (purplesqlitehistoryadapter.c:287)
by 0x48DB656: purple_history_adapter_activate (purplehistoryadapter.c:181)
by 0x48DC9BC: purple_history_manager_set_active (purplehistorymanager.c:308)
by 0x402BA8: test_ui_init_history (test_ui.c:132)
by 0x402C80: test_ui_purple_init (test_ui.c:167)
by 0x4027BB: main (test_contact.c:88)

4,368 bytes in 1 blocks are possibly lost in loss record 3,454 of 3,457
at 0x484386F: malloc (vg_replace_malloc.c:393)
by 0x5235B67: sqlite3MemMalloc.lto_priv.0 (sqlite3.c:25493)
by 0x5232797: UnknownInlinedFun (sqlite3.c:29181)
by 0x5232797: UnknownInlinedFun (sqlite3.c:29227)
by 0x5232797: sqlite3Malloc.lto_priv.0 (sqlite3.c:29221)
by 0x523BD8B: pcache1Alloc.lto_priv.0 (sqlite3.c:53546)
by 0x5240077: UnknownInlinedFun (sqlite3.c:53634)
by 0x5240077: pcache1FetchStage2 (sqlite3.c:54104)
by 0x5243E9C: UnknownInlinedFun (sqlite3.c:52671)
by 0x5243E9C: getPageNormal.lto_priv.0 (sqlite3.c:60628)
by 0x52499BC: UnknownInlinedFun (sqlite3.c:60805)
by 0x52499BC: UnknownInlinedFun (sqlite3.c:70289)
by 0x52499BC: btreeGetUnusedPage (sqlite3.c:70432)
by 0x524F504: allocateBtreePage.lto_priv.0 (sqlite3.c:74604)
by 0x5256209: btreeCreateTable.lto_priv.0 (sqlite3.c:77830)
by 0x5265EB2: UnknownInlinedFun (sqlite3.c:77849)
by 0x5265EB2: sqlite3VdbeExec.lto_priv.0 (sqlite3.c:96382)
by 0x525CBEE: UnknownInlinedFun (sqlite3.c:87995)
by 0x525CBEE: UnknownInlinedFun (sqlite3.c:88056)
by 0x525CBEE: sqlite3_step (sqlite3.c:88045)
by 0x529B324: sqlite3_exec (sqlite3.c:131002)
by 0x48FD558: purple_sqlite3_run_migration (purplesqlite3.c:37)
by 0x48FDBB4: purple_sqlite3_run_migrations_from_resources (purplesqlite3.c:195)
by 0x48FDED9: purple_sqlite_history_adapter_run_migrations (purplesqlitehistoryadapter.c:69)
by 0x48FE7F0: purple_sqlite_history_adapter_activate (purplesqlitehistoryadapter.c:287)
by 0x48DB656: purple_history_adapter_activate (purplehistoryadapter.c:181)
by 0x48DC9BC: purple_history_manager_set_active (purplehistorymanager.c:308)
by 0x402BA8: test_ui_init_history (test_ui.c:132)
by 0x402C80: test_ui_purple_init (test_ui.c:167)
by 0x4027BB: main (test_contact.c:88)
```

Testing Done:
Ran tests in valgrind, and all above leaks were gone except for noted `protocol_xfer` issues, and a bunch of leaks of the `PurpleBuddy`-`PurpleContact` compatibility bindings, which will go away in their entirety eventually.

Reviewed at https://reviews.imfreedom.org/r/2385/
/*
* purple
* Copyright (C) Pidgin Developers <devel@pidgin.im>
*
* Purple is the legal property of its developers, whose names are too numerous
* to list here. Please refer to the COPYRIGHT file distributed with this
* source distribution.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
*/
#include <glib/gi18n-lib.h>
#include "purplepresence.h"
#include "debug.h"
#include "purpleenums.h"
#include "purpleprivate.h"
typedef struct {
gboolean idle;
GDateTime *idle_time;
GDateTime *login_time;
GHashTable *status_table;
PurpleStatus *active_status;
PurplePresencePrimitive primitive;
char *message;
char *emoji;
gboolean mobile;
} PurplePresencePrivate;
enum {
PROP_0,
PROP_IDLE,
PROP_IDLE_TIME,
PROP_LOGIN_TIME,
PROP_ACTIVE_STATUS,
PROP_PRIMITIVE,
PROP_MESSAGE,
PROP_EMOJI,
PROP_MOBILE,
N_PROPERTIES
};
static GParamSpec *properties[N_PROPERTIES];
G_DEFINE_TYPE_WITH_PRIVATE(PurplePresence, purple_presence, G_TYPE_OBJECT)
/******************************************************************************
* Helpers
*****************************************************************************/
static void
purple_presence_set_active_status(PurplePresence *presence,
PurpleStatus *status)
{
PurplePresencePrivate *priv = NULL;
priv = purple_presence_get_instance_private(presence);
if(g_set_object(&priv->active_status, status)) {
GObject *obj = G_OBJECT(presence);
g_object_freeze_notify(obj);
g_object_notify_by_pspec(obj, properties[PROP_ACTIVE_STATUS]);
g_object_notify_by_pspec(obj, properties[PROP_PRIMITIVE]);
g_object_notify_by_pspec(obj, properties[PROP_MESSAGE]);
g_object_thaw_notify(obj);
}
}
/******************************************************************************
* GObject Implementation
*****************************************************************************/
static void
purple_presence_set_property(GObject *obj, guint param_id, const GValue *value,
GParamSpec *pspec)
{
PurplePresence *presence = PURPLE_PRESENCE(obj);
switch (param_id) {
case PROP_IDLE:
purple_presence_set_idle(presence, g_value_get_boolean(value),
NULL);
break;
case PROP_IDLE_TIME:
purple_presence_set_idle(presence, TRUE, g_value_get_boxed(value));
break;
case PROP_LOGIN_TIME:
purple_presence_set_login_time(presence, g_value_get_boxed(value));
break;
case PROP_ACTIVE_STATUS:
purple_presence_set_active_status(presence,
g_value_get_object(value));
break;
case PROP_PRIMITIVE:
purple_presence_set_primitive(presence, g_value_get_enum(value));
break;
case PROP_MESSAGE:
purple_presence_set_message(presence, g_value_get_string(value));
break;
case PROP_EMOJI:
purple_presence_set_emoji(presence, g_value_get_string(value));
break;
case PROP_MOBILE:
purple_presence_set_mobile(presence, g_value_get_boolean(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
break;
}
}
static void
purple_presence_get_property(GObject *obj, guint param_id, GValue *value,
GParamSpec *pspec)
{
PurplePresence *presence = PURPLE_PRESENCE(obj);
switch (param_id) {
case PROP_IDLE:
g_value_set_boolean(value, purple_presence_is_idle(presence));
break;
case PROP_IDLE_TIME:
g_value_set_boxed(value, purple_presence_get_idle_time(presence));
break;
case PROP_LOGIN_TIME:
g_value_set_boxed(value, purple_presence_get_login_time(presence));
break;
case PROP_ACTIVE_STATUS:
g_value_set_object(value, purple_presence_get_active_status(presence));
break;
case PROP_PRIMITIVE:
g_value_set_enum(value, purple_presence_get_primitive(presence));
break;
case PROP_MESSAGE:
g_value_set_string(value, purple_presence_get_message(presence));
break;
case PROP_EMOJI:
g_value_set_string(value, purple_presence_get_emoji(presence));
break;
case PROP_MOBILE:
g_value_set_boolean(value, purple_presence_get_mobile(presence));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
break;
}
}
static void
purple_presence_init(PurplePresence *presence) {
PurplePresencePrivate *priv = NULL;
priv = purple_presence_get_instance_private(presence);
priv->status_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
NULL);
}
static void
purple_presence_finalize(GObject *obj) {
PurplePresencePrivate *priv = NULL;
priv = purple_presence_get_instance_private(PURPLE_PRESENCE(obj));
g_hash_table_destroy(priv->status_table);
g_clear_object(&priv->active_status);
g_clear_pointer(&priv->idle_time, g_date_time_unref);
g_clear_pointer(&priv->login_time, g_date_time_unref);
g_clear_pointer(&priv->message, g_free);
g_clear_pointer(&priv->emoji, g_free);
G_OBJECT_CLASS(purple_presence_parent_class)->finalize(obj);
}
static void
purple_presence_class_init(PurplePresenceClass *klass) {
GObjectClass *obj_class = G_OBJECT_CLASS(klass);
obj_class->get_property = purple_presence_get_property;
obj_class->set_property = purple_presence_set_property;
obj_class->finalize = purple_presence_finalize;
/**
* PurplePresence:idle:
*
* Whether or not the presence is in an idle state.
*/
properties[PROP_IDLE] = g_param_spec_boolean("idle", "Idle",
"Whether the presence is in idle state.", FALSE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
/**
* PurplePresence:idle-time:
*
* The time when the presence went idle.
*/
properties[PROP_IDLE_TIME] = g_param_spec_boxed(
"idle-time", "Idle time",
"The idle time of the presence",
G_TYPE_DATE_TIME,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
/**
* PurplePresence:login-time:
*
* The login-time of the presence.
*/
properties[PROP_LOGIN_TIME] = g_param_spec_boxed(
"login-time", "Login time",
"The login time of the presence.",
G_TYPE_DATE_TIME,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
/**
* PurplePresence:active-status:
*
* The currently active status of the presence.
*/
properties[PROP_ACTIVE_STATUS] = g_param_spec_object("active-status",
"Active status",
"The active status for the presence.", PURPLE_TYPE_STATUS,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
/**
* PurplePresence:primitive:
*
* The [enum@Purple.StatusPrimitive] for this presence.
*
* Since: 3.0.0
*/
properties[PROP_PRIMITIVE] = g_param_spec_enum(
"primitive", "primitive",
"The primitive for the presence",
PURPLE_TYPE_PRESENCE_PRIMITIVE,
PURPLE_PRESENCE_PRIMITIVE_OFFLINE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
/**
* PurplePresence:message:
*
* The status message of the presence.
*
* Since: 3.0.0
*/
properties[PROP_MESSAGE] = g_param_spec_string(
"message", "message",
"The status message",
NULL,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
/**
* PurplePresence:emoji:
*
* The emoji or mood of the presence.
*
* Since: 3.0.0
*/
properties[PROP_EMOJI] = g_param_spec_string(
"emoji", "emoji",
"The emoji for the presence.",
NULL,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
/**
* PurplePresence:mobile:
*
* Whether or not the presence is on a mobile device.
*
* Since: 3.0.0
*/
properties[PROP_MOBILE] = g_param_spec_boolean(
"mobile", "mobile",
"Whether or not the presence is on a mobile device.",
FALSE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
}
/******************************************************************************
* Public API
*****************************************************************************/
PurplePresence *
purple_presence_new(void) {
return g_object_new(PURPLE_TYPE_PRESENCE, NULL);
}
void
purple_presence_set_status_active(PurplePresence *presence,
const gchar *status_id, gboolean active)
{
PurpleStatus *status = NULL;
g_return_if_fail(PURPLE_IS_PRESENCE(presence));
g_return_if_fail(status_id != NULL);
status = purple_presence_get_status(presence, status_id);
g_return_if_fail(PURPLE_IS_STATUS(status));
/* TODO: Should we do the following? */
/* g_return_if_fail(active == status->active); */
if(purple_status_is_exclusive(status)) {
if(!active) {
purple_debug_warning("presence",
"Attempted to set a non-independent status "
"(%s) inactive. Only independent statuses "
"can be specifically marked inactive.",
status_id);
return;
}
}
purple_status_set_active(status, active);
}
void
purple_presence_switch_status(PurplePresence *presence, const gchar *status_id)
{
purple_presence_set_status_active(presence, status_id, TRUE);
}
void
purple_presence_set_idle(PurplePresence *presence, gboolean idle,
GDateTime *idle_time)
{
PurplePresencePrivate *priv = NULL;
PurplePresenceClass *klass = NULL;
gboolean old_idle;
GObject *obj = NULL;
g_return_if_fail(PURPLE_IS_PRESENCE(presence));
priv = purple_presence_get_instance_private(presence);
klass = PURPLE_PRESENCE_GET_CLASS(presence);
if (priv->idle == idle && priv->idle_time == idle_time) {
return;
}
old_idle = priv->idle;
priv->idle = idle;
g_clear_pointer(&priv->idle_time, g_date_time_unref);
if(idle && idle_time != NULL) {
priv->idle_time = g_date_time_ref(idle_time);
}
obj = G_OBJECT(presence);
g_object_freeze_notify(obj);
g_object_notify_by_pspec(obj, properties[PROP_IDLE]);
g_object_notify_by_pspec(obj, properties[PROP_IDLE_TIME]);
g_object_thaw_notify(obj);
if(klass->update_idle) {
klass->update_idle(presence, old_idle);
}
}
void
purple_presence_set_login_time(PurplePresence *presence, GDateTime *login_time)
{
PurplePresencePrivate *priv = NULL;
g_return_if_fail(PURPLE_IS_PRESENCE(presence));
priv = purple_presence_get_instance_private(presence);
if(priv->login_time != NULL && login_time != NULL) {
if(g_date_time_equal(priv->login_time, login_time)) {
return;
}
}
if(priv->login_time != NULL) {
g_date_time_unref(priv->login_time);
}
if(login_time != NULL) {
priv->login_time = g_date_time_ref(login_time);
} else {
priv->login_time = NULL;
}
g_object_notify_by_pspec(G_OBJECT(presence), properties[PROP_LOGIN_TIME]);
}
GList *
purple_presence_get_statuses(PurplePresence *presence) {
PurplePresenceClass *klass = NULL;
g_return_val_if_fail(PURPLE_IS_PRESENCE(presence), NULL);
klass = PURPLE_PRESENCE_GET_CLASS(presence);
if(klass && klass->get_statuses) {
return klass->get_statuses(presence);
}
return NULL;
}
PurpleStatus *
purple_presence_get_status(PurplePresence *presence, const gchar *status_id) {
PurplePresencePrivate *priv = NULL;
PurpleStatus *status = NULL;
GList *l = NULL;
g_return_val_if_fail(PURPLE_IS_PRESENCE(presence), NULL);
g_return_val_if_fail(status_id != NULL, NULL);
priv = purple_presence_get_instance_private(presence);
/* What's the purpose of this hash table? */
status = (PurpleStatus *)g_hash_table_lookup(priv->status_table,
status_id);
if(status == NULL) {
for(l = purple_presence_get_statuses(presence);
l != NULL && status == NULL; l = l->next)
{
PurpleStatus *temp_status = l->data;
if (purple_strequal(status_id, purple_status_get_id(temp_status))) {
status = temp_status;
}
}
if(status != NULL) {
g_hash_table_insert(priv->status_table,
g_strdup(purple_status_get_id(status)), status);
}
}
return status;
}
PurpleStatus *
purple_presence_get_active_status(PurplePresence *presence) {
PurplePresencePrivate *priv = NULL;
g_return_val_if_fail(PURPLE_IS_PRESENCE(presence), NULL);
priv = purple_presence_get_instance_private(presence);
return priv->active_status;
}
gboolean
purple_presence_is_available(PurplePresence *presence) {
PurpleStatus *status = NULL;
g_return_val_if_fail(PURPLE_IS_PRESENCE(presence), FALSE);
status = purple_presence_get_active_status(presence);
return ((status != NULL && purple_status_is_available(status)) &&
!purple_presence_is_idle(presence));
}
gboolean
purple_presence_is_online(PurplePresence *presence) {
PurpleStatus *status = NULL;
g_return_val_if_fail(PURPLE_IS_PRESENCE(presence), FALSE);
if((status = purple_presence_get_active_status(presence)) == NULL) {
return FALSE;
}
return purple_status_is_online(status);
}
gboolean
purple_presence_is_status_active(PurplePresence *presence,
const gchar *status_id)
{
PurpleStatus *status = NULL;
g_return_val_if_fail(PURPLE_IS_PRESENCE(presence), FALSE);
g_return_val_if_fail(status_id != NULL, FALSE);
status = purple_presence_get_status(presence, status_id);
return (status != NULL && purple_status_is_active(status));
}
gboolean
purple_presence_is_status_primitive_active(PurplePresence *presence,
PurpleStatusPrimitive primitive)
{
GList *l = NULL;
g_return_val_if_fail(PURPLE_IS_PRESENCE(presence), FALSE);
g_return_val_if_fail(primitive != PURPLE_STATUS_UNSET, FALSE);
for(l = purple_presence_get_statuses(presence); l != NULL; l = l->next) {
PurpleStatus *temp_status = l->data;
PurpleStatusType *type = purple_status_get_status_type(temp_status);
if(purple_status_type_get_primitive(type) == primitive &&
purple_status_is_active(temp_status))
{
return TRUE;
}
}
return FALSE;
}
gboolean
purple_presence_is_idle(PurplePresence *presence) {
PurplePresencePrivate *priv = NULL;
g_return_val_if_fail(PURPLE_IS_PRESENCE(presence), FALSE);
if(!purple_presence_is_online(presence)) {
return FALSE;
}
priv = purple_presence_get_instance_private(presence);
return priv->idle;
}
GDateTime *
purple_presence_get_idle_time(PurplePresence *presence) {
PurplePresencePrivate *priv = NULL;
g_return_val_if_fail(PURPLE_IS_PRESENCE(presence), NULL);
priv = purple_presence_get_instance_private(presence);
return priv->idle_time;
}
GDateTime *
purple_presence_get_login_time(PurplePresence *presence) {
PurplePresencePrivate *priv = NULL;
g_return_val_if_fail(PURPLE_IS_PRESENCE(presence), 0);
priv = purple_presence_get_instance_private(presence);
return priv->login_time;
}
gint
purple_presence_compare(PurplePresence *presence1, PurplePresence *presence2) {
GDateTime *idle1 = NULL;
GDateTime *idle2 = NULL;
GDateTime *now = NULL;
GTimeSpan diff1 = 0;
GTimeSpan diff2 = 0;
if(presence1 == presence2) {
return 0;
} else if (presence1 == NULL) {
return 1;
} else if (presence2 == NULL) {
return -1;
}
if(purple_presence_is_online(presence1) &&
!purple_presence_is_online(presence2))
{
return -1;
} else if(purple_presence_is_online(presence2) &&
!purple_presence_is_online(presence1))
{
return 1;
}
idle1 = purple_presence_get_idle_time(presence1);
idle2 = purple_presence_get_idle_time(presence2);
if(idle1 == NULL && idle2 == NULL) {
return 0;
} else if(idle1 == NULL && idle2 != NULL) {
return -1;
} else if(idle1 != NULL && idle2 == NULL) {
return 1;
}
now = g_date_time_new_now_local();
diff1 = g_date_time_difference(now, idle1);
diff2 = g_date_time_difference(now, idle2);
g_date_time_unref(now);
if(diff1 > diff2) {
return 1;
} else if (diff1 < diff2) {
return -1;
}
return 0;
}
PurplePresencePrimitive
purple_presence_get_primitive(PurplePresence *presence) {
PurplePresencePrivate *priv = NULL;
g_return_val_if_fail(PURPLE_IS_PRESENCE(presence),
PURPLE_PRESENCE_PRIMITIVE_OFFLINE);
priv = purple_presence_get_instance_private(presence);
return priv->primitive;
}
void
purple_presence_set_primitive(PurplePresence *presence,
PurplePresencePrimitive primitive)
{
PurplePresencePrivate *priv = NULL;
g_return_if_fail(PURPLE_IS_PRESENCE(presence));
priv = purple_presence_get_instance_private(presence);
if(priv->primitive != primitive) {
priv->primitive = primitive;
g_object_notify_by_pspec(G_OBJECT(presence),
properties[PROP_PRIMITIVE]);
}
}
const char *
purple_presence_get_message(PurplePresence *presence) {
PurplePresencePrivate *priv = NULL;
g_return_val_if_fail(PURPLE_IS_PRESENCE(presence), NULL);
priv = purple_presence_get_instance_private(presence);
if(priv->message != NULL) {
return priv->message;
}
return purple_status_get_attr_string(priv->active_status, "message");
}
void
purple_presence_set_message(PurplePresence *presence, const char *message) {
PurplePresencePrivate *priv = NULL;
g_return_if_fail(PURPLE_IS_PRESENCE(presence));
priv = purple_presence_get_instance_private(presence);
if(!purple_strequal(priv->message, message)) {
g_free(priv->message);
priv->message = g_strdup(message);
g_object_notify_by_pspec(G_OBJECT(presence), properties[PROP_MESSAGE]);
}
}
const char *
purple_presence_get_emoji(PurplePresence *presence) {
PurplePresencePrivate *priv = NULL;
g_return_val_if_fail(PURPLE_IS_PRESENCE(presence), NULL);
priv = purple_presence_get_instance_private(presence);
return priv->emoji;
}
void
purple_presence_set_emoji(PurplePresence *presence, const char *emoji) {
PurplePresencePrivate *priv = NULL;
g_return_if_fail(PURPLE_IS_PRESENCE(presence));
priv = purple_presence_get_instance_private(presence);
if(!purple_strequal(priv->emoji, emoji)) {
g_free(priv->emoji);
priv->emoji = g_strdup(emoji);
g_object_notify_by_pspec(G_OBJECT(presence), properties[PROP_EMOJI]);
}
}
gboolean
purple_presence_get_mobile(PurplePresence *presence) {
PurplePresencePrivate *priv = NULL;
g_return_val_if_fail(PURPLE_IS_PRESENCE(presence), FALSE);
priv = purple_presence_get_instance_private(presence);
return priv->mobile;
}
void
purple_presence_set_mobile(PurplePresence *presence, gboolean mobile) {
PurplePresencePrivate *priv = NULL;
g_return_if_fail(PURPLE_IS_PRESENCE(presence));
priv = purple_presence_get_instance_private(presence);
if(priv->mobile != mobile) {
priv->mobile = mobile;
g_object_notify_by_pspec(G_OBJECT(presence), properties[PROP_MOBILE]);
}
}
const char *
purple_presence_primitive_to_string(PurplePresencePrimitive primitive) {
switch(primitive) {
case PURPLE_PRESENCE_PRIMITIVE_OFFLINE:
return _("Offline");
break;
case PURPLE_PRESENCE_PRIMITIVE_AVAILABLE:
return _("Available");
break;
case PURPLE_PRESENCE_PRIMITIVE_IDLE:
return _("Idle");
break;
case PURPLE_PRESENCE_PRIMITIVE_INVISIBLE:
return _("Invisible");
break;
case PURPLE_PRESENCE_PRIMITIVE_AWAY:
return _("Away");
break;
case PURPLE_PRESENCE_PRIMITIVE_EXTENDED_AWAY:
return _("Do not disturb");
break;
case PURPLE_PRESENCE_PRIMITIVE_STREAMING:
return _("Streaming");
break;
default:
return _("Unknown");
break;
}
}