grim/pidgin

Fix several leaks in tests

15 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/
/*
* finch
*
* Finch 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include <config.h>
#include <glib/gi18n-lib.h>
#include <purple.h>
#include <gnt.h>
#include "gntconv.h"
#include "gntmedia.h"
#include "libfinch.h"
/* An incredibly large part of the following is from gtkmedia.c */
#undef hangup
#define FINCH_TYPE_MEDIA (finch_media_get_type())
#define FINCH_MEDIA(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), FINCH_TYPE_MEDIA, FinchMedia))
#define FINCH_MEDIA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), FINCH_TYPE_MEDIA, FinchMediaClass))
#define FINCH_IS_MEDIA(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), FINCH_TYPE_MEDIA))
#define FINCH_IS_MEDIA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), FINCH_TYPE_MEDIA))
#define FINCH_MEDIA_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), FINCH_TYPE_MEDIA, FinchMediaClass))
typedef struct _FinchMedia FinchMedia;
typedef struct _FinchMediaClass FinchMediaClass;
typedef enum _FinchMediaState FinchMediaState;
struct _FinchMediaClass
{
GntBoxClass parent_class;
};
struct _FinchMedia
{
GntBox parent;
PurpleMedia *media;
GntWidget *accept;
GntWidget *reject;
GntWidget *hangup;
GntWidget *calling;
PurpleConversation *conv;
};
static GType finch_media_get_type (void);
static void finch_media_class_init (FinchMediaClass *klass);
static void finch_media_init (FinchMedia *media);
static void finch_media_finalize (GObject *object);
static void finch_media_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
static void finch_media_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
enum {
MESSAGE,
LAST_SIGNAL
};
static guint finch_media_signals[LAST_SIGNAL] = {0};
enum {
PROP_0,
PROP_MEDIA,
};
G_DEFINE_TYPE(FinchMedia, finch_media, GNT_TYPE_BOX);
static void
finch_media_class_init (FinchMediaClass *klass)
{
GObjectClass *gobject_class = (GObjectClass*)klass;
gobject_class->finalize = finch_media_finalize;
gobject_class->set_property = finch_media_set_property;
gobject_class->get_property = finch_media_get_property;
g_object_class_install_property(gobject_class, PROP_MEDIA,
g_param_spec_object("media",
"PurpleMedia",
"The PurpleMedia associated with this media.",
PURPLE_TYPE_MEDIA,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
finch_media_signals[MESSAGE] = g_signal_new("message", G_TYPE_FROM_CLASS(klass),
G_SIGNAL_RUN_LAST, 0, NULL, NULL,
g_cclosure_marshal_VOID__STRING,
G_TYPE_NONE, 1, G_TYPE_STRING);
}
static void
finch_media_init (FinchMedia *media)
{
media->calling = gnt_label_new(_("Calling..."));
media->hangup = gnt_button_new(_("Hangup"));
media->accept = gnt_button_new(_("Accept"));
media->reject = gnt_button_new(_("Reject"));
gnt_box_set_alignment(GNT_BOX(media), GNT_ALIGN_MID);
gnt_box_add_widget(GNT_BOX(media), media->accept);
gnt_box_add_widget(GNT_BOX(media), media->reject);
}
static void
finch_media_finalize (GObject *media)
{
FinchMedia *gntmedia = FINCH_MEDIA(media);
purple_debug_info("gntmedia", "finch_media_finalize");
g_clear_object(&gntmedia->media);
}
static void
finch_media_emit_message(FinchMedia *gntmedia, const char *msg)
{
g_signal_emit(gntmedia, finch_media_signals[MESSAGE], 0, msg);
}
static void
finch_media_connected_cb(G_GNUC_UNUSED PurpleMedia *media,
FinchMedia *gntmedia)
{
GntWidget *parent;
finch_media_emit_message(gntmedia, _("Call in progress."));
gnt_box_remove(GNT_BOX(gntmedia), gntmedia->accept);
gnt_box_remove(GNT_BOX(gntmedia), gntmedia->reject);
gnt_box_remove(GNT_BOX(gntmedia), gntmedia->hangup);
gnt_box_remove(GNT_BOX(gntmedia), gntmedia->calling);
gnt_box_add_widget(GNT_BOX(gntmedia), gntmedia->hangup);
g_clear_pointer(&gntmedia->accept, gnt_widget_destroy);
g_clear_pointer(&gntmedia->reject, gnt_widget_destroy);
g_clear_pointer(&gntmedia->calling, gnt_widget_destroy);
parent = gnt_widget_get_toplevel(GNT_WIDGET(gntmedia));
gnt_box_readjust(GNT_BOX(parent));
gnt_widget_draw(parent);
}
static void
finch_media_wait_cb(G_GNUC_UNUSED PurpleMedia *media, FinchMedia *gntmedia)
{
GntWidget *parent;
gnt_box_remove(GNT_BOX(gntmedia), gntmedia->accept);
gnt_box_remove(GNT_BOX(gntmedia), gntmedia->reject);
gnt_box_remove(GNT_BOX(gntmedia), gntmedia->hangup);
gnt_box_remove(GNT_BOX(gntmedia), gntmedia->calling);
gnt_box_add_widget(GNT_BOX(gntmedia), gntmedia->calling);
gnt_box_add_widget(GNT_BOX(gntmedia), gntmedia->hangup);
parent = gnt_widget_get_toplevel(GNT_WIDGET(gntmedia));
gnt_box_readjust(GNT_BOX(parent));
gnt_widget_draw(parent);
}
static void
finch_media_state_changed_cb(PurpleMedia *media, PurpleMediaState state,
gchar *sid, gchar *name, FinchMedia *gntmedia)
{
purple_debug_info("gntmedia", "state: %d sid: %s name: %s\n",
state, sid, name);
if (sid == NULL && name == NULL) {
if (state == PURPLE_MEDIA_STATE_END) {
finch_media_emit_message(gntmedia,
_("The call has been terminated."));
finch_conversation_set_info_widget(gntmedia->conv, NULL);
gnt_widget_destroy(GNT_WIDGET(gntmedia));
/*
* XXX: This shouldn't have to be here
* to free the FinchMedia widget.
*/
g_object_unref(gntmedia);
}
} else if (state == PURPLE_MEDIA_STATE_CONNECTED) {
finch_media_connected_cb(media, gntmedia);
} else if (state == PURPLE_MEDIA_STATE_NEW &&
sid != NULL && name != NULL &&
purple_media_is_initiator(media, sid, name) == FALSE) {
PurpleAccount *account;
PurpleBuddy *buddy;
const gchar *alias;
PurpleMediaSessionType type =
purple_media_get_session_type(media, sid);
gchar *message = NULL;
account = purple_media_get_account(gntmedia->media);
buddy = purple_blist_find_buddy(account, name);
alias = buddy ? purple_buddy_get_contact_alias(buddy) : name;
if (type & PURPLE_MEDIA_AUDIO) {
message = g_strdup_printf(
_("%s wishes to start an audio session with you."),
alias);
} else {
message = g_strdup_printf(
_("%s is trying to start an unsupported media session type with you."),
alias);
}
finch_media_emit_message(gntmedia, message);
g_free(message);
}
}
static void
finch_media_stream_info_cb(G_GNUC_UNUSED PurpleMedia *media,
PurpleMediaInfoType type,
G_GNUC_UNUSED gchar *sid,
G_GNUC_UNUSED gchar *name,
G_GNUC_UNUSED gboolean local,
FinchMedia *gntmedia)
{
if (type == PURPLE_MEDIA_INFO_REJECT) {
finch_media_emit_message(gntmedia,
_("You have rejected the call."));
}
}
static void
finch_media_accept_cb(PurpleMedia *media, G_GNUC_UNUSED GntWidget *widget)
{
purple_media_stream_info(media, PURPLE_MEDIA_INFO_ACCEPT,
NULL, NULL, TRUE);
}
static void
finch_media_hangup_cb(PurpleMedia *media, G_GNUC_UNUSED GntWidget *widget)
{
purple_media_stream_info(media, PURPLE_MEDIA_INFO_HANGUP,
NULL, NULL, TRUE);
}
static void
finch_media_reject_cb(PurpleMedia *media, G_GNUC_UNUSED GntWidget *widget)
{
purple_media_stream_info(media, PURPLE_MEDIA_INFO_REJECT,
NULL, NULL, TRUE);
}
static void
finch_media_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
FinchMedia *media;
g_return_if_fail(FINCH_IS_MEDIA(object));
media = FINCH_MEDIA(object);
switch (prop_id) {
case PROP_MEDIA:
g_set_object(&media->media, g_value_get_object(value));
g_signal_connect_swapped(G_OBJECT(media->accept), "activate",
G_CALLBACK(finch_media_accept_cb), media->media);
g_signal_connect_swapped(G_OBJECT(media->reject), "activate",
G_CALLBACK(finch_media_reject_cb), media->media);
g_signal_connect_swapped(G_OBJECT(media->hangup), "activate",
G_CALLBACK(finch_media_hangup_cb), media->media);
if (purple_media_is_initiator(media->media, NULL, NULL)) {
finch_media_wait_cb(media->media, media);
}
g_signal_connect(G_OBJECT(media->media), "state-changed",
G_CALLBACK(finch_media_state_changed_cb), media);
g_signal_connect(G_OBJECT(media->media), "stream-info",
G_CALLBACK(finch_media_stream_info_cb), media);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
finch_media_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
FinchMedia *media;
g_return_if_fail(FINCH_IS_MEDIA(object));
media = FINCH_MEDIA(object);
switch (prop_id) {
case PROP_MEDIA:
g_value_set_object(value, media->media);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static GntWidget *
finch_media_new(PurpleMedia *media)
{
return GNT_WIDGET(g_object_new(finch_media_get_type(),
"media", media,
"vertical", FALSE,
"homogeneous", FALSE,
NULL));
}
static void
gntmedia_message_cb(G_GNUC_UNUSED FinchMedia *gntmedia, const char *msg,
PurpleConversation *conv)
{
if (PURPLE_IS_IM_CONVERSATION(conv)) {
purple_conversation_write_system_message(conv, msg, 0);
}
}
static gboolean
finch_new_media(G_GNUC_UNUSED PurpleMediaManager *manager, PurpleMedia *media,
PurpleAccount *account, gchar *name,
G_GNUC_UNUSED gpointer data)
{
GntWidget *gntmedia;
PurpleConversation *conv;
conv = PURPLE_CONVERSATION(purple_im_conversation_new(account, name));
gntmedia = finch_media_new(media);
g_signal_connect(G_OBJECT(gntmedia), "message", G_CALLBACK(gntmedia_message_cb), conv);
FINCH_MEDIA(gntmedia)->conv = conv;
finch_conversation_set_info_widget(conv, gntmedia);
return TRUE;
}
static PurpleCmdRet
call_cmd_cb(PurpleConversation *conv, G_GNUC_UNUSED const char *cmd,
G_GNUC_UNUSED char **args, G_GNUC_UNUSED char **error,
G_GNUC_UNUSED gpointer data)
{
PurpleAccount *account = purple_conversation_get_account(conv);
if (!purple_protocol_initiate_media(account,
purple_conversation_get_name(conv),
PURPLE_MEDIA_AUDIO))
return PURPLE_CMD_RET_FAILED;
return PURPLE_CMD_RET_OK;
}
void finch_media_manager_init(void)
{
PurpleMediaManager *manager = purple_media_manager_get();
PurpleMediaElementInfo *audio_src = NULL;
PurpleMediaElementInfo *audio_sink = NULL;
const char *pref;
purple_prefs_add_none(FINCH_PREFS_ROOT "/vvconfig");
purple_prefs_add_none(FINCH_PREFS_ROOT "/vvconfig/audio");
purple_prefs_add_none(FINCH_PREFS_ROOT "/vvconfig/audio/src");
purple_prefs_add_none(FINCH_PREFS_ROOT "/vvconfig/audio/sink");
purple_prefs_add_string(FINCH_PREFS_ROOT "/vvconfig/audio/src/device", "");
purple_prefs_add_string(FINCH_PREFS_ROOT "/vvconfig/audio/sink/device", "");
pref = purple_prefs_get_string(
FINCH_PREFS_ROOT "/vvconfig/audio/src/device");
if (pref)
audio_src = purple_media_manager_get_element_info(manager, pref);
if (!audio_src) {
pref = "autoaudiosrc";
purple_prefs_set_string(
FINCH_PREFS_ROOT "/vvconfig/audio/src/device", pref);
audio_src = purple_media_manager_get_element_info(manager,
pref);
}
pref = purple_prefs_get_string(
FINCH_PREFS_ROOT "/vvconfig/audio/sink/device");
if (pref)
audio_sink = purple_media_manager_get_element_info(manager, pref);
if (!audio_sink) {
pref = "autoaudiosink";
purple_prefs_set_string(
FINCH_PREFS_ROOT "/vvconfig/audio/sink/device", pref);
audio_sink = purple_media_manager_get_element_info(manager,
pref);
}
g_signal_connect(G_OBJECT(manager), "init-media", G_CALLBACK(finch_new_media), NULL);
purple_cmd_register("call", "", PURPLE_CMD_P_DEFAULT,
PURPLE_CMD_FLAG_IM, NULL,
call_cmd_cb, _("call: Make an audio call."), NULL);
purple_media_manager_set_ui_caps(manager,
PURPLE_MEDIA_CAPS_AUDIO |
PURPLE_MEDIA_CAPS_AUDIO_SINGLE_DIRECTION);
purple_debug_info("gntmedia", "Registering media element types\n");
purple_media_manager_set_active_element(manager, audio_src);
purple_media_manager_set_active_element(manager, audio_sink);
}
void finch_media_manager_uninit(void)
{
PurpleMediaManager *manager = purple_media_manager_get();
g_signal_handlers_disconnect_by_func(G_OBJECT(manager),
G_CALLBACK(finch_new_media), NULL);
}