From 9fee4385e567e0f4265ffdddb6898aa0d364214e Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Tue, 10 Apr 2012 16:52:49 +0200 Subject: [PATCH] Add tp_capabilities_supports_sms() https://bugs.freedesktop.org/show_bug.cgi?id=48506 --- docs/reference/telepathy-glib-sections.txt | 1 + telepathy-glib/capabilities.c | 78 ++++++++++++++++++++ telepathy-glib/capabilities.h | 2 + tests/capabilities.c | 105 ++++++++++++++++++++++++++++ 4 files changed, 186 insertions(+), 0 deletions(-) diff --git a/docs/reference/telepathy-glib-sections.txt b/docs/reference/telepathy-glib-sections.txt index 1862663..1f42fca 100644 --- a/docs/reference/telepathy-glib-sections.txt +++ b/docs/reference/telepathy-glib-sections.txt @@ -4987,6 +4987,7 @@ tp_capabilities_get_channel_classes tp_capabilities_is_specific_to_contact tp_capabilities_supports_text_chatrooms tp_capabilities_supports_text_chats +tp_capabilities_supports_sms tp_capabilities_supports_audio_call tp_capabilities_supports_audio_video_call tp_capabilities_supports_file_transfer diff --git a/telepathy-glib/capabilities.c b/telepathy-glib/capabilities.c index 66140b2..2aafdd1 100644 --- a/telepathy-glib/capabilities.c +++ b/telepathy-glib/capabilities.c @@ -360,6 +360,84 @@ tp_capabilities_supports_text_chatrooms (TpCapabilities *self) TP_HANDLE_TYPE_ROOM); } +/** + * tp_capabilities_supports_sms: + * @self: a #TpCapabilities object + * + * If the #TpCapabilities:contact-specific property is %FALSE, this function + * checks if SMS text channels can be requested with the connection associated + * with this #TpCapabilities. + * + * If the #TpCapabilities:contact-specific property is %TRUE, this function + * checks if the contact associated with this #TpCapabilities supports + * SMS text channels. + * + * Returns: %TRUE if a channel request containing Text as ChannelType, + * HandleTypeContact as TargetHandleType, a channel identifier and + * #TP_PROP_CHANNEL_INTERFACE_SMS_SMS_CHANNEL set to %TRUE can be + * expected to work, %FALSE otherwise. + * + * Since: 0.UNRELEASED + */ +gboolean +tp_capabilities_supports_sms (TpCapabilities *self) +{ + guint i; + + g_return_val_if_fail (TP_IS_CAPABILITIES (self), FALSE); + + for (i = 0; i < self->priv->classes->len; i++) + { + GValueArray *arr = g_ptr_array_index (self->priv->classes, i); + GHashTable *fixed; + const gchar * const *allowed; + const gchar *chan_type; + TpHandleType handle_type; + gboolean valid; + guint nb_fixed_props; + + fixed = g_value_get_boxed (g_value_array_get_nth (arr, 0)); + allowed = g_value_get_boxed (g_value_array_get_nth (arr, 1)); + + handle_type = tp_asv_get_uint32 (fixed, + TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, &valid); + + if (!valid) + continue; + + if (handle_type != TP_HANDLE_TYPE_CONTACT) + continue; + + chan_type = tp_asv_get_string (fixed, TP_PROP_CHANNEL_CHANNEL_TYPE); + + if (tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_TEXT)) + continue; + + /* SMSChannel be either in fixed or allowed properties */ + if (tp_asv_get_boolean (fixed, TP_PROP_CHANNEL_INTERFACE_SMS_SMS_CHANNEL, + NULL)) + { + /* In fixed, succeed if there is no more fixed properties required */ + nb_fixed_props = 3; + } + else + { + /* Not in fixed; check allowed */ + if (!tp_strv_contains (allowed, + TP_PROP_CHANNEL_INTERFACE_SMS_SMS_CHANNEL)) + continue; + + nb_fixed_props = 2; + } + + if (g_hash_table_size (fixed) == nb_fixed_props) + return TRUE; + } + + return FALSE; + +} + static gboolean supports_call_full (TpCapabilities *self, TpHandleType expected_handle_type, diff --git a/telepathy-glib/capabilities.h b/telepathy-glib/capabilities.h index e283331..1d55c1a 100644 --- a/telepathy-glib/capabilities.h +++ b/telepathy-glib/capabilities.h @@ -54,6 +54,8 @@ gboolean tp_capabilities_is_specific_to_contact (TpCapabilities *self); gboolean tp_capabilities_supports_text_chats (TpCapabilities *self); gboolean tp_capabilities_supports_text_chatrooms (TpCapabilities *self); +gboolean tp_capabilities_supports_sms (TpCapabilities *self); + gboolean tp_capabilities_supports_audio_call (TpCapabilities *self, TpHandleType handle_type); gboolean tp_capabilities_supports_audio_video_call (TpCapabilities *self, diff --git a/tests/capabilities.c b/tests/capabilities.c index 01eac78..f05964c 100644 --- a/tests/capabilities.c +++ b/tests/capabilities.c @@ -177,6 +177,7 @@ test_supports (Test *test, g_assert (tp_capabilities_is_specific_to_contact (caps)); g_assert (tp_capabilities_supports_text_chats (caps)); g_assert (!tp_capabilities_supports_text_chatrooms (caps)); + g_assert (!tp_capabilities_supports_sms (caps)); g_object_unref (caps); @@ -195,6 +196,7 @@ test_supports (Test *test, g_assert (tp_capabilities_is_specific_to_contact (caps)); g_assert (!tp_capabilities_supports_text_chats (caps)); g_assert (tp_capabilities_supports_text_chatrooms (caps)); + g_assert (!tp_capabilities_supports_sms (caps)); g_object_unref (caps); @@ -214,6 +216,7 @@ test_supports (Test *test, g_assert (tp_capabilities_is_specific_to_contact (caps)); g_assert (tp_capabilities_supports_text_chats (caps)); g_assert (tp_capabilities_supports_text_chatrooms (caps)); + g_assert (!tp_capabilities_supports_sms (caps)); g_object_unref (caps); @@ -223,6 +226,7 @@ test_supports (Test *test, g_assert (tp_capabilities_is_specific_to_contact (caps)); g_assert (!tp_capabilities_supports_text_chats (caps)); g_assert (!tp_capabilities_supports_text_chatrooms (caps)); + g_assert (!tp_capabilities_supports_sms (caps)); classes = tp_capabilities_get_channel_classes (caps); g_assert_cmpuint (classes->len, ==, 0); @@ -651,6 +655,105 @@ test_supports_room_list (Test *test, g_object_unref (caps); } +static void +add_sms_class (GPtrArray *classes, + gboolean add_extra_fixed, + gboolean use_allowed) +{ + GHashTable *fixed; + GPtrArray *allowed; + GValueArray *arr; + + fixed = tp_asv_new ( + TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, + TP_IFACE_CHANNEL_TYPE_TEXT, + TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, + TP_HANDLE_TYPE_CONTACT, + NULL); + + allowed = g_ptr_array_new (); + + if (use_allowed) + { + g_ptr_array_add (allowed, TP_PROP_CHANNEL_INTERFACE_SMS_SMS_CHANNEL); + } + else + { + tp_asv_set_boolean (fixed, TP_PROP_CHANNEL_INTERFACE_SMS_SMS_CHANNEL, + TRUE); + } + + g_ptr_array_add (allowed, NULL); + + if (add_extra_fixed) + tp_asv_set_boolean (fixed, "ExtraBadgersRequired", TRUE); + + arr = tp_value_array_build (2, + TP_HASH_TYPE_STRING_VARIANT_MAP, fixed, + G_TYPE_STRV, allowed->pdata, + G_TYPE_INVALID); + + g_hash_table_unref (fixed); + g_ptr_array_unref (allowed); + + g_ptr_array_add (classes, arr); +} + +static void +test_supports_sms (Test *test, + gconstpointer data G_GNUC_UNUSED) +{ + TpCapabilities *caps; + GPtrArray *classes; + + classes = g_ptr_array_sized_new (1); + add_sms_class (classes, FALSE, FALSE); + + caps = tp_tests_object_new_static_class (TP_TYPE_CAPABILITIES, + "channel-classes", classes, + "contact-specific", FALSE, + NULL); + + g_boxed_free (TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST, + classes); + + g_assert (tp_capabilities_supports_sms (caps)); + + g_object_unref (caps); + + /* Reject if more fixed properties are required */ + classes = g_ptr_array_sized_new (1); + add_sms_class (classes, TRUE, FALSE); + + caps = tp_tests_object_new_static_class (TP_TYPE_CAPABILITIES, + "channel-classes", classes, + "contact-specific", FALSE, + NULL); + + g_boxed_free (TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST, + classes); + + g_assert (!tp_capabilities_supports_sms (caps)); + + g_object_unref (caps); + + /* Test with SMS as an allowed property */ + classes = g_ptr_array_sized_new (1); + add_sms_class (classes, FALSE, TRUE); + + caps = tp_tests_object_new_static_class (TP_TYPE_CAPABILITIES, + "channel-classes", classes, + "contact-specific", FALSE, + NULL); + + g_boxed_free (TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST, + classes); + + g_assert (tp_capabilities_supports_sms (caps)); + + g_object_unref (caps); +} + int main (int argc, char **argv) @@ -668,6 +771,8 @@ main (int argc, test_supports_tube, NULL); g_test_add (TEST_PREFIX "supports/room-list", Test, NULL, setup, test_supports_room_list, NULL); + g_test_add (TEST_PREFIX "supports/sms", Test, NULL, setup, + test_supports_sms, NULL); return g_test_run (); } -- 1.7.7.6