summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWill Thompson <will@willthompson.co.uk>2012-01-30 16:05:34 +0000
committerWill Thompson <will@willthompson.co.uk>2012-01-30 16:05:34 +0000
commit5cd7e850bbd71605cf9e954c6a13afe8f5560dea (patch)
tree2983d750b2a8205c0fa8c07175d7e1ee696d06c6
parent10e528fb92452b21ac67a0685ed16c24ea66cba7 (diff)
PepService::changed: pass <item> element to callback
-rw-r--r--tests/wocky-pep-service-test.c60
-rw-r--r--wocky/wocky-pep-service.c16
2 files changed, 60 insertions, 16 deletions
diff --git a/tests/wocky-pep-service-test.c b/tests/wocky-pep-service-test.c
index d303bde..bb8bcae 100644
--- a/tests/wocky-pep-service-test.c
+++ b/tests/wocky-pep-service-test.c
@@ -33,10 +33,27 @@ static void
test_changed_signal_cb (WockyPepService *pep,
WockyBareContact *contact,
WockyStanza *stanza,
+ WockyNode *item,
test_data_t *test)
{
- g_assert (!wocky_strdiff (wocky_bare_contact_get_jid (contact),
- "alice@example.org"));
+ const gchar *id = wocky_node_get_attribute (
+ wocky_stanza_get_top_node (stanza), "id");
+
+ g_assert_cmpstr (wocky_bare_contact_get_jid (contact), ==,
+ "alice@example.org");
+
+ /* the id happens to hold the number of <item> children; we expect to get the
+ * first one if there is more than one. */
+ if (wocky_strdiff (id, "0"))
+ {
+ g_assert (item != NULL);
+ g_assert_cmpstr (item->name, ==, "item");
+ g_assert_cmpstr (wocky_node_get_attribute (item, "id"), ==, "1");
+ }
+ else
+ {
+ g_assert (item == NULL);
+ }
test->outstanding--;
g_main_loop_quit (test->loop);
@@ -45,27 +62,42 @@ test_changed_signal_cb (WockyPepService *pep,
static void
send_pep_event (WockyPorter *porter,
- const gchar *node)
+ const gchar *node,
+ guint n_items)
{
WockyStanza *stanza;
+ WockyNode *items;
+ gchar *n_items_str = g_strdup_printf ("%d", n_items);
+ guint i;
stanza = wocky_stanza_build (WOCKY_STANZA_TYPE_MESSAGE,
WOCKY_STANZA_SUB_TYPE_NONE,
"alice@example.org", NULL,
+ /* This is a hint for test_changed_signal_cb. */
+ '@', "id", n_items_str,
'(', "event",
':', WOCKY_XMPP_NS_PUBSUB_EVENT,
'(', "items",
- '@', "node", node,
- '(', "item",
- '@', "id", "1",
- '(', "payload", ')',
- ')',
+ '@', "node", node,
+ '*', &items,
')',
')',
NULL);
+ for (i = 1; i <= n_items; i++)
+ {
+ gchar *i_str = g_strdup_printf ("%d", i);
+ wocky_node_add_build (items,
+ '(', "item",
+ '@', "id", i_str,
+ '(', "payload", ')',
+ ')', NULL);
+ g_free (i_str);
+ }
+
wocky_porter_send (porter, stanza);
g_object_unref (stanza);
+ g_free (n_items_str);
}
static void
@@ -83,22 +115,24 @@ test_changed_signal (void)
wocky_porter_start (test->sched_out);
wocky_porter_start (test->sched_in);
- /* send event on the right node */
+ /* send events on the right node */
event_received = FALSE;
- send_pep_event (test->sched_in, TEST_NODE1);
+ send_pep_event (test->sched_in, TEST_NODE1, 0);
+ send_pep_event (test->sched_in, TEST_NODE1, 1);
+ send_pep_event (test->sched_in, TEST_NODE1, 2);
- test->outstanding += 1;
+ test->outstanding += 3;
test_wait_pending (test);
g_assert (event_received);
event_received = FALSE;
/* send event on the wrong node */
- send_pep_event (test->sched_in, TEST_NODE2);
+ send_pep_event (test->sched_in, TEST_NODE2, 1);
g_object_unref (pep);
/* send event to the right node after the PEP service has been destroyed */
- send_pep_event (test->sched_in, TEST_NODE1);
+ send_pep_event (test->sched_in, TEST_NODE1, 1);
test_close_both_porters (test);
teardown_test (test);
diff --git a/wocky/wocky-pep-service.c b/wocky/wocky-pep-service.c
index 854a088..1637753 100644
--- a/wocky/wocky-pep-service.c
+++ b/wocky/wocky-pep-service.c
@@ -209,6 +209,8 @@ wocky_pep_service_class_init (WockyPepServiceClass *wocky_pep_service_class)
* @self: a #WockyPepService object
* @contact: the #WockyBareContact who changed the node
* @stanza: the #WockyStanza
+ * @item: the first—and typically only—&lt;item&gt; element in @stanza, or
+ * %NULL if there is none.
*
* Emitted when the node value changes.
*/
@@ -217,8 +219,8 @@ wocky_pep_service_class_init (WockyPepServiceClass *wocky_pep_service_class)
G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
0,
NULL, NULL,
- _wocky_signals_marshal_VOID__OBJECT_OBJECT,
- G_TYPE_NONE, 2, WOCKY_TYPE_BARE_CONTACT, WOCKY_TYPE_STANZA);
+ _wocky_signals_marshal_VOID__OBJECT_OBJECT_POINTER,
+ G_TYPE_NONE, 3, WOCKY_TYPE_BARE_CONTACT, WOCKY_TYPE_STANZA, G_TYPE_POINTER);
}
/**
@@ -251,6 +253,7 @@ msg_event_cb (WockyPorter *porter,
const gchar *from;
WockyBareContact *contact;
WockyStanzaSubType sub_type;
+ WockyNode *event, *items, *item;
from = wocky_stanza_get_from (stanza);
if (from == NULL)
@@ -269,10 +272,17 @@ msg_event_cb (WockyPorter *porter,
return FALSE;
}
+ event = wocky_node_get_child_ns (wocky_stanza_get_top_node (stanza),
+ "event", WOCKY_XMPP_NS_PUBSUB_EVENT);
+ g_return_val_if_fail (event != NULL, FALSE);
+ items = wocky_node_get_child (event, "items");
+ g_return_val_if_fail (items != NULL, FALSE);
+ item = wocky_node_get_child (items, "item");
+
contact = wocky_contact_factory_ensure_bare_contact (
priv->contact_factory, from);
- g_signal_emit (G_OBJECT (self), signals[CHANGED], 0, contact, stanza);
+ g_signal_emit (G_OBJECT (self), signals[CHANGED], 0, contact, stanza, item);
g_object_unref (contact);
return TRUE;