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 - Internet Messaging Library
* Copyright (C) Pidgin Developers <devel@pidgin.im>
*
* 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 "purplenotificationmanager.h"
#include "purpleprivate.h"
enum {
PROP_ZERO,
PROP_UNREAD_COUNT,
N_PROPERTIES,
};
static GParamSpec *properties[N_PROPERTIES] = { NULL, };
enum {
SIG_ADDED,
SIG_REMOVED,
SIG_READ,
SIG_UNREAD,
N_SIGNALS,
};
static guint signals[N_SIGNALS] = { 0, };
struct _PurpleNotificationManager {
GObject parent;
GPtrArray *notifications;
guint unread_count;
};
static PurpleNotificationManager *default_manager = NULL;
/******************************************************************************
* Helpers
*****************************************************************************/
static void
purple_notification_manager_set_unread_count(PurpleNotificationManager *manager,
guint unread_count)
{
if(manager->unread_count != unread_count) {
manager->unread_count = unread_count;
g_object_notify_by_pspec(G_OBJECT(manager),
properties[PROP_UNREAD_COUNT]);
}
}
static inline void
purple_notification_manager_increment_unread_count(PurpleNotificationManager *manager)
{
if(manager->unread_count < G_MAXUINT) {
purple_notification_manager_set_unread_count(manager,
manager->unread_count + 1);
}
}
static inline void
purple_notification_manager_decrement_unread_count(PurpleNotificationManager *manager)
{
if(manager->unread_count > 0) {
purple_notification_manager_set_unread_count(manager,
manager->unread_count - 1);
}
}
/******************************************************************************
* Callbacks
*****************************************************************************/
static void
purple_notification_manager_notify_cb(GObject *obj,
G_GNUC_UNUSED GParamSpec *pspec,
gpointer data)
{
PurpleNotification *notification = PURPLE_NOTIFICATION(obj);
PurpleNotificationManager *manager = data;
guint signal_id = 0;
/* This function is called after the property is changed. So we need to
* get the new value to determine how the state changed.
*/
if(purple_notification_get_read(notification)) {
purple_notification_manager_decrement_unread_count(manager);
signal_id = signals[SIG_READ];
} else {
purple_notification_manager_increment_unread_count(manager);
signal_id = signals[SIG_UNREAD];
}
g_signal_emit(manager, signal_id, 0, notification);
}
/******************************************************************************
* GListModel Implementation
*****************************************************************************/
static GType
purple_notification_manager_get_item_type(G_GNUC_UNUSED GListModel *list) {
return PURPLE_TYPE_NOTIFICATION;
}
static guint
purple_notification_manager_get_n_items(GListModel *list) {
PurpleNotificationManager *manager = PURPLE_NOTIFICATION_MANAGER(list);
return manager->notifications->len;
}
static gpointer
purple_notification_manager_get_item(GListModel *list, guint position) {
PurpleNotificationManager *manager = PURPLE_NOTIFICATION_MANAGER(list);
PurpleNotification *notification = NULL;
if(position < manager->notifications->len) {
notification = g_ptr_array_index(manager->notifications, position);
g_object_ref(notification);
}
return notification;
}
static void
purple_notification_manager_list_model_init(GListModelInterface *iface) {
iface->get_item_type = purple_notification_manager_get_item_type;
iface->get_n_items = purple_notification_manager_get_n_items;
iface->get_item = purple_notification_manager_get_item;
}
/******************************************************************************
* GObject Implementation
*****************************************************************************/
G_DEFINE_TYPE_EXTENDED(PurpleNotificationManager, purple_notification_manager,
G_TYPE_OBJECT, G_TYPE_FLAG_FINAL,
G_IMPLEMENT_INTERFACE(G_TYPE_LIST_MODEL, purple_notification_manager_list_model_init));
static void
purple_notification_manager_get_property(GObject *obj, guint param_id,
GValue *value, GParamSpec *pspec)
{
PurpleNotificationManager *manager = PURPLE_NOTIFICATION_MANAGER(obj);
switch(param_id) {
case PROP_UNREAD_COUNT:
g_value_set_uint(value,
purple_notification_manager_get_unread_count(manager));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
break;
}
}
static void
purple_notification_manager_finalize(GObject *obj) {
PurpleNotificationManager *manager = NULL;
manager = PURPLE_NOTIFICATION_MANAGER(obj);
g_ptr_array_free(manager->notifications, TRUE);
manager->notifications = NULL;
G_OBJECT_CLASS(purple_notification_manager_parent_class)->finalize(obj);
}
static void
purple_notification_manager_init(PurpleNotificationManager *manager) {
manager->notifications = g_ptr_array_new_full(0,
(GDestroyNotify)g_object_unref);
}
static void
purple_notification_manager_class_init(PurpleNotificationManagerClass *klass) {
GObjectClass *obj_class = G_OBJECT_CLASS(klass);
obj_class->get_property = purple_notification_manager_get_property;
obj_class->finalize = purple_notification_manager_finalize;
/* Properties */
/**
* PurpleNotificationManager:unread-count:
*
* The number of unread notifications in the manager.
*
* Since: 3.0.0
*/
properties[PROP_UNREAD_COUNT] = g_param_spec_uint(
"unread-count", "unread-count",
"The number of unread messages in the manager.",
0, G_MAXUINT, 0,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
/* Signals */
/**
* PurpleNotificationManager::added:
* @manager: The instance.
* @notification: The [class@Notification] that was added.
*
* Emitted after @notification has been added to @manager.
*
* Since: 3.0.0
*/
signals[SIG_ADDED] = g_signal_new_class_handler(
"added",
G_OBJECT_CLASS_TYPE(klass),
G_SIGNAL_RUN_LAST,
NULL,
NULL,
NULL,
NULL,
G_TYPE_NONE,
1,
PURPLE_TYPE_NOTIFICATION);
/**
* PurpleNotificationManager::removed:
* @manager: The instance.
* @notification: The [class@Notification] that was removed.
*
* Emitted after @notification has been removed from @manager.
*
* Since: 3.0.0
*/
signals[SIG_REMOVED] = g_signal_new_class_handler(
"removed",
G_OBJECT_CLASS_TYPE(klass),
G_SIGNAL_RUN_LAST,
NULL,
NULL,
NULL,
NULL,
G_TYPE_NONE,
1,
PURPLE_TYPE_NOTIFICATION);
/**
* PurpleNotificationManager::read:
* @manager: The instance.
* @notification: The [class@Notification].
*
* Emitted after @notification has been marked as read.
*
* Since: 3.0.0
*/
signals[SIG_READ] = g_signal_new_class_handler(
"read",
G_OBJECT_CLASS_TYPE(klass),
G_SIGNAL_RUN_LAST,
NULL,
NULL,
NULL,
NULL,
G_TYPE_NONE,
1,
PURPLE_TYPE_NOTIFICATION);
/**
* PurpleNotificationManager::unread:
* @manager: The instance.
* @notification: The [class@Notification].
*
* Emitted after @notification has been marked as unread.
*
* Since: 3.0.0
*/
signals[SIG_UNREAD] = g_signal_new_class_handler(
"unread",
G_OBJECT_CLASS_TYPE(klass),
G_SIGNAL_RUN_LAST,
NULL,
NULL,
NULL,
NULL,
G_TYPE_NONE,
1,
PURPLE_TYPE_NOTIFICATION);
}
/******************************************************************************
* Private API
*****************************************************************************/
void
purple_notification_manager_startup(void) {
if(default_manager == NULL) {
default_manager = g_object_new(PURPLE_TYPE_NOTIFICATION_MANAGER, NULL);
g_object_add_weak_pointer(G_OBJECT(default_manager),
(gpointer *)&default_manager);
}
}
void
purple_notification_manager_shutdown(void) {
g_clear_object(&default_manager);
}
/******************************************************************************
* Public API
*****************************************************************************/
PurpleNotificationManager *
purple_notification_manager_get_default(void) {
return default_manager;
}
GListModel *
purple_notification_manager_get_default_as_model(void) {
if(PURPLE_IS_NOTIFICATION_MANAGER(default_manager)) {
return G_LIST_MODEL(default_manager);
}
return NULL;
}
void
purple_notification_manager_add(PurpleNotificationManager *manager,
PurpleNotification *notification)
{
g_return_if_fail(PURPLE_IS_NOTIFICATION_MANAGER(manager));
g_return_if_fail(PURPLE_IS_NOTIFICATION(notification));
if(g_ptr_array_find(manager->notifications, notification, NULL)) {
const gchar *id = purple_notification_get_id(notification);
g_warning("double add detected for notification %s", id);
return;
}
g_ptr_array_insert(manager->notifications, 0, g_object_ref(notification));
/* Connect to the notify signal for the read property only so we can
* propagate out changes for any notification.
*/
g_signal_connect_object(notification, "notify::read",
G_CALLBACK(purple_notification_manager_notify_cb),
manager, 0);
/* If the notification is not read, we need to increment the unread count.
*/
if(!purple_notification_get_read(notification)) {
purple_notification_manager_increment_unread_count(manager);
}
g_signal_emit(G_OBJECT(manager), signals[SIG_ADDED], 0, notification);
g_list_model_items_changed(G_LIST_MODEL(manager), 0, 0, 1);
}
void
purple_notification_manager_remove(PurpleNotificationManager *manager,
PurpleNotification *notification)
{
guint index = 0;
g_return_if_fail(PURPLE_IS_NOTIFICATION_MANAGER(manager));
g_return_if_fail(PURPLE_IS_NOTIFICATION(notification));
if(g_ptr_array_find(manager->notifications, notification, &index)) {
/* Reference the notification so we can emit the signal after it's been
* removed from the hash table.
*/
g_object_ref(notification);
g_ptr_array_remove_index(manager->notifications, index);
/* Remove the notify signal handler for the read state incase someone
* else added a reference to the notification which would then mess
* with our unread count accounting.
*/
g_signal_handlers_disconnect_by_func(notification,
G_CALLBACK(purple_notification_manager_notify_cb),
manager);
/* If the notification is not read, we need to decrement the unread
* count.
*/
if(!purple_notification_get_read(notification)) {
purple_notification_manager_decrement_unread_count(manager);
}
g_signal_emit(G_OBJECT(manager), signals[SIG_REMOVED], 0, notification);
g_list_model_items_changed(G_LIST_MODEL(manager), index, 1, 0);
g_object_unref(notification);
}
}
/*
This function uses the following algorithm to optimally remove items from the
g_ptr_array to minimize the number of calls to g_list_model_items_changed. See
the pseudo code below for an easier to follow version.
A
A B C
B C
A A B C
B A C
B A A C
B C A
B C A A
set len = number_of_items
set pos = 0
set have_same = false
while pos < len
check item at pos
if same
if not have_same
reset count = 0
set start = pos
set have_same = TRUE
set count = count + 1
else
if have_same
remove count items from start
set pos = pos - count
set len = len - count
set have_same = FALSE
set pos = pos + 1
if have_same
remove count items from start
*/
void
purple_notification_manager_remove_with_account(PurpleNotificationManager *manager,
PurpleAccount *account,
gboolean all)
{
GListModel *model = NULL;
guint pos = 0, len = 0;
guint start = 0, count = 0;
gboolean have_same = FALSE;
g_return_if_fail(PURPLE_IS_NOTIFICATION_MANAGER(manager));
g_return_if_fail(PURPLE_IS_ACCOUNT(account));
model = G_LIST_MODEL(manager);
for(pos = 0; pos < manager->notifications->len; pos++) {
PurpleAccount *account2 = NULL;
PurpleNotification *notification = NULL;
PurpleNotificationType type;
gboolean can_remove = TRUE;
notification = g_ptr_array_index(manager->notifications, pos);
/* If the notification's type is connection error, set can_remove to
* the value of the all parameter.
*/
type = purple_notification_get_notification_type(notification);
if(type == PURPLE_NOTIFICATION_TYPE_CONNECTION_ERROR) {
can_remove = all;
}
account2 = purple_notification_get_account(notification);
if(account == account2 && can_remove) {
/* If this is the first item with the right account store its
* position.
*/
if(!have_same) {
count = 0;
start = pos;
have_same = TRUE;
}
/* Increment the count of items starting at the start position. */
count++;
} else {
if(have_same) {
/* Remove the run of items from the list. */
g_ptr_array_remove_range(manager->notifications, start, count);
g_list_model_items_changed(model, start, count, 0);
/* Adjust pos and len for the items that we removed. */
pos = pos - count;
len = len - count;
have_same = FALSE;
}
}
}
/* Clean up the last bit if the last item needs to be removed. */
if(have_same) {
g_ptr_array_remove_range(manager->notifications, start, count);
g_list_model_items_changed(model, start, count, 0);
}
}
guint
purple_notification_manager_get_unread_count(PurpleNotificationManager *manager) {
g_return_val_if_fail(PURPLE_IS_NOTIFICATION_MANAGER(manager), 0);
return manager->unread_count;
}
void
purple_notification_manager_clear(PurpleNotificationManager *manager) {
guint count = 0;
g_return_if_fail(PURPLE_IS_NOTIFICATION_MANAGER(manager));
count = manager->notifications->len;
for(guint pos = 0; pos < count; pos++) {
PurpleNotification *notification = NULL;
notification = g_ptr_array_index(manager->notifications, pos);
g_signal_emit(manager, signals[SIG_REMOVED], 0, notification);
}
g_ptr_array_remove_range(manager->notifications, 0, count);
g_list_model_items_changed(G_LIST_MODEL(manager), 0, count, 0);
}