[glibmm] DBus Client Example: create a DBusProxy in preparation for method call.



commit 460d21d10350e2154f0f81f52a8df09bd9f581e0
Author: José Alburquerque <jaalburqu svn gnome org>
Date:   Tue Dec 14 23:13:33 2010 -0500

    	DBus Client Example: create a DBusProxy in preparation for method call.
    
    	* examples/dbus/well-known-address-client.cc: Asynchronously create a
    	DBusProxy which can then be used to call a method on the user's
    	session bus.  This tests asynchronous creation of objects that derive
    	from the AsyncInitable interface.  The reference counting of the newly
    	created object should be checked because in the SlotAsyncReady slot,
    	the proxy has a reference count of two when it should possibly be
    	one (the g_dbus_proxy_new_finish() docs say that in the callback,
    	unreferencing the proxy destroys it).

 ChangeLog                                  |   13 +++++++
 examples/dbus/well-known-address-client.cc |   48 ++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+), 0 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 0bdb096..6474195 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,18 @@
 2010-12-14  José Alburquerque  <jaalburqu svn gnome org>
 
+	DBus Client Example: create a DBusProxy in preparation for method call.
+
+	* examples/dbus/well-known-address-client.cc: Asynchronously create a
+	DBusProxy which can then be used to call a method on the user's
+	session bus.  This tests asynchronous creation of objects that derive
+	from the AsyncInitable interface.  The reference counting of the newly
+	created object should be checked because in the SlotAsyncReady slot,
+	the proxy has a reference count of two when it should possibly be
+	one (the g_dbus_proxy_new_finish() docs say that in the callback,
+	unreferencing the proxy destroys it).
+
+2010-12-14  José Alburquerque  <jaalburqu svn gnome org>
+
 	DBus[Connection|Proxy]: Don't take extra reference on async creation.
 
 	* gio/src/dbusconnection.ccg:
diff --git a/examples/dbus/well-known-address-client.cc b/examples/dbus/well-known-address-client.cc
index 3105815..43977b7 100644
--- a/examples/dbus/well-known-address-client.cc
+++ b/examples/dbus/well-known-address-client.cc
@@ -19,17 +19,65 @@
 #include <glibmm.h>
 #include <iostream>
 
+// The main loop.
+Glib::RefPtr<Glib::MainLoop> loop;
+
+// A main loop idle callback to quit when the main loop is idle.
+bool main_loop_idle()
+{
+  loop->quit();
+  return false;
+}
+
+// A callback to finish creating a DBusProxy that was asynchronously created
+// for the user session's bus and then try to call the well known 'ListNames'
+// method.
+void dbus_proxy_available(Glib::RefPtr<Gio::AsyncResult>& result)
+{
+  Glib::RefPtr<Gio::DBusProxy> proxy = Gio::DBusProxy::create_finish(result);
+
+  if(!proxy)
+  {
+    std::cerr << "The proxy to the user's session bus was not successfully "
+      "created." << std::endl;
+    loop->quit();
+    return;
+  }
+
+  // Call the 'ListNames' method and print the results.
+
+  // Connect an idle callback to the main loop to quit when the main loop is
+  // idle now that the method call is finished.
+  Glib::signal_idle().connect(sigc::ptr_fun(&main_loop_idle));
+}
+
 int main(int, char**)
 {
   std::locale::global(std::locale(""));
   Gio::init();
 
+  loop = Glib::MainLoop::create();
+
   // Get the user session bus connection.
   Glib::RefPtr<Gio::DBusConnection> connection =
     Gio::DBusConnection::get_sync(Gio::BUS_TYPE_SESSION);
 
+  // Check for an unavailable connection.
+  if (!connection)
+  {
+    std::cerr << "The user's session bus is not available." << std::endl;
+    return 1;
+  }
+
   // Print out the unique name of the connection to the user session bus.
   std::cout << connection->get_unique_name() << std::endl;
 
+  // Create the proxy to the bus asynchronously.
+  Gio::DBusProxy::create(connection, "org.freedesktop.DBus",
+    "/org/freedesktop/DBus", "org.freedesktop.DBus",
+    sigc::ptr_fun(&dbus_proxy_available));
+
+  loop->run();
+
   return 0;
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]