GDBusProxy: Remove error in get_cached_property() and add set_cached_property()

This makes it possible to use the cached properties mechanism even if
constructing the proxy with the DO_NOT_LOAD_PROPERTIES flag.

This is useful for cases where you obtain the and track object
properties out-of-band. For example, in udisks, the plan is to have
something like this

 Manager.GetObjects    (out ao paths, out aa{sa{sv}} all_properties);
 Manager.ObjectAdded   (o path, a{sa{sv}} all_properties);
 Manager.ObjectChanged (o path, a{sa{sv}} all_properties);
 Manager.ObjectRemoved (o path, a{sa{sv}} all_properties);

E.g. the first GetObjects() call will return *all* data about *all*
exported objects. Further, this way a client will only need to listen
these three signals (three AddMatch) on the Manager object and it will
never need to do GetAll() etc (e.g. can use DO_NOT_LOAD_PROPERTIES).

(Of course this only works if the client is interested in all
objects... while this is true for udisks it is generally not true for
other D-Bus services).

Also use expected_interface to check for programming errors.
This commit is contained in:
David Zeuthen
2010-05-12 20:43:40 -04:00
parent b690e637d4
commit f909cb5b27
8 changed files with 174 additions and 51 deletions

View File

@@ -160,7 +160,7 @@ accounts_user_get_user_name (AccountsUser *user)
GVariant *value;
const gchar *ret;
g_return_val_if_fail (ACCOUNTS_IS_USER (user), NULL);
value = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (user), "UserName", NULL);
value = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (user), "UserName");
ret = g_variant_get_string (value, NULL);
g_variant_unref (value);
return ret;
@@ -172,7 +172,7 @@ accounts_user_get_real_name (AccountsUser *user)
GVariant *value;
const gchar *ret;
g_return_val_if_fail (ACCOUNTS_IS_USER (user), NULL);
value = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (user), "RealName", NULL);
value = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (user), "RealName");
ret = g_variant_get_string (value, NULL);
g_variant_unref (value);
return ret;
@@ -184,7 +184,7 @@ accounts_user_get_automatic_login (AccountsUser *user)
GVariant *value;
gboolean ret;
g_return_val_if_fail (ACCOUNTS_IS_USER (user), FALSE);
value = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (user), "AutomaticLogin", NULL);
value = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (user), "AutomaticLogin");
ret = g_variant_get_boolean (value);
g_variant_unref (value);
return ret;

View File

@@ -32,7 +32,7 @@ print_properties (GDBusProxy *proxy)
const gchar *key = property_names[n];
GVariant *value;
gchar *value_str;
value = g_dbus_proxy_get_cached_property (proxy, key, NULL);
value = g_dbus_proxy_get_cached_property (proxy, key);
value_str = g_variant_print (value, TRUE);
g_print (" %s -> %s\n", key, value_str);
g_variant_unref (value);

View File

@@ -531,8 +531,7 @@ test_peer (void)
g_assert_no_error (error);
g_assert (proxy != NULL);
error = NULL;
value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty", &error);
g_assert_no_error (error);
value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
/* try invoking a method */

View File

@@ -150,13 +150,11 @@ test_properties (GDBusConnection *connection,
*
* No need to test all properties - GVariant has already been tested
*/
variant = g_dbus_proxy_get_cached_property (proxy, "y", &error);
g_assert_no_error (error);
variant = g_dbus_proxy_get_cached_property (proxy, "y");
g_assert (variant != NULL);
g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
g_variant_unref (variant);
variant = g_dbus_proxy_get_cached_property (proxy, "o", &error);
g_assert_no_error (error);
variant = g_dbus_proxy_get_cached_property (proxy, "o");
g_assert (variant != NULL);
g_assert_cmpstr (g_variant_get_string (variant, NULL), ==, "/some/path");
g_variant_unref (variant);
@@ -180,11 +178,20 @@ test_properties (GDBusConnection *connection,
g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
g_variant_unref (result);
_g_assert_signal_received (proxy, "g-properties-changed");
variant = g_dbus_proxy_get_cached_property (proxy, "y", &error);
g_assert_no_error (error);
variant = g_dbus_proxy_get_cached_property (proxy, "y");
g_assert (variant != NULL);
g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
g_variant_unref (variant);
g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_byte (142));
variant = g_dbus_proxy_get_cached_property (proxy, "y");
g_assert (variant != NULL);
g_assert_cmpint (g_variant_get_byte (variant), ==, 142);
g_variant_unref (variant);
g_dbus_proxy_set_cached_property (proxy, "y", NULL);
variant = g_dbus_proxy_get_cached_property (proxy, "y");
g_assert (variant == NULL);
}
/* ---------------------------------------------------------------------------------------------------- */
@@ -352,6 +359,7 @@ static const gchar *frob_dbus_interface_xml =
" <method name='Sleep'>"
" <arg type='i' name='timeout' direction='in'/>"
" </method>"
" <property name='y' type='y' access='readwrite'/>"
" </interface>"
"</node>";
static GDBusInterfaceInfo *frob_dbus_interface_info;
@@ -367,6 +375,12 @@ on_proxy_appeared (GDBusConnection *connection,
test_properties (connection, name, name_owner, proxy);
test_signals (connection, name, name_owner, proxy);
/* This is obviously wrong but expected interface is not set so we don't fail... */
g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_string ("error_me_out!"));
g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_byte (42));
g_dbus_proxy_set_cached_property (proxy, "does-not-exist", g_variant_new_string ("something"));
g_dbus_proxy_set_cached_property (proxy, "does-not-exist", NULL);
/* Now repeat the method tests, with an expected interface set */
g_dbus_proxy_set_interface_info (proxy, frob_dbus_interface_info);
test_methods (connection, name, name_owner, proxy);
@@ -376,6 +390,24 @@ on_proxy_appeared (GDBusConnection *connection,
*/
test_bogus_method_return (connection, name, name_owner, proxy);
/* Also check that we complain if setting a cached property of the wrong type */
if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
{
g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_string ("error_me_out!"));
}
g_test_trap_assert_stderr ("*Trying to set property y of type s but according to the expected interface the type is y*");
g_test_trap_assert_failed();
if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
{
g_dbus_proxy_set_cached_property (proxy, "does-not-exist", g_variant_new_string ("something"));
}
g_test_trap_assert_stderr ("*Trying to lookup property does-not-exist which isn't in expected interface com.example.Frob*");
g_test_trap_assert_failed();
/* this should work, however (since the type is correct) */
g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_byte (42));
g_main_loop_quit (loop);
}