Back to index

unity  6.0.0
test_service_gdbus_wrapper.c
Go to the documentation of this file.
00001 #include "test_service_gdbus_wrapper.h"
00002 #include <unity.h>
00003 #include <gio/gio.h>
00004 
00005 const char * gdbus_wrapper_interface = 
00006 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
00007 "<node name=\"/\">\n"
00008 "        <interface name=\"com.canonical.gdbus_wrapper\">\n"
00009 "<!-- Properties -->\n"
00010 "                <!-- None -->\n"
00011 "\n"
00012 "<!-- Functions -->\n"
00013 "                <method name=\"TestMethod\">\n"
00014 "                        <!-- in -->\n"
00015 "                        <arg type=\"s\" name=\"query\" direction=\"in\" />\n"
00016 "                        <!-- out -->\n"
00017 "                        <arg type=\"s\" name=\"target\" direction=\"out\" />\n"
00018 "                </method>\n"
00019 "\n"
00020 "<!-- Signals -->\n"
00021 "                <signal name=\"TestSignal\">\n"
00022 "                        <arg type=\"s\" name=\"target\" direction=\"out\" />\n"
00023 "                </signal>\n"
00024 "\n"
00025 "<!-- End of interesting stuff -->\n"
00026 "\n"
00027 "        </interface>\n"
00028 "</node>\n"
00029 ;
00030 static void bus_got_cb          (GObject *object, GAsyncResult * res, gpointer user_data);
00031 static void bus_method          (GDBusConnection *connection,
00032                                 const gchar *sender,
00033                                 const gchar *object_path,
00034                                 const gchar *interface_name,
00035                                 const gchar *method_name,
00036                                 GVariant *parameters,
00037                                 GDBusMethodInvocation *invocation,
00038                                 gpointer user_data);
00039 
00040 G_DEFINE_TYPE(ServiceGDBusWrapper, service_gdbus_wrapper, G_TYPE_OBJECT);
00041 static GDBusNodeInfo * node_info = NULL;
00042 static GDBusInterfaceInfo * iface_info = NULL;
00043 static GDBusInterfaceVTable bus_vtable = {
00044   method_call: bus_method,
00045   get_property: NULL,
00046   set_property: NULL,
00047 };
00048 
00049 
00050 struct _ServiceGDBusWrapperPrivate
00051 {
00052   GDBusConnection * bus;
00053   GCancellable * bus_lookup;
00054   guint bus_registration;
00055 };
00056 
00057 static void
00058 service_gdbus_wrapper_dispose(GObject* object)
00059 {
00060   ServiceGDBusWrapper* self = SERVICE_GDBUS_WRAPPER(object);
00061   if (self->priv->bus_lookup != NULL) {
00062     g_cancellable_cancel(self->priv->bus_lookup);
00063     g_object_unref(self->priv->bus_lookup);
00064     self->priv->bus_lookup = NULL;
00065   }
00066 
00067   if (self->priv->bus_registration != 0) {
00068     g_dbus_connection_unregister_object(self->priv->bus, self->priv->bus_registration);
00069     self->priv->bus_registration = 0;
00070   }
00071 
00072   if (self->priv->bus != NULL) {
00073     g_object_unref(self->priv->bus);
00074     self->priv->bus = NULL;
00075   }
00076 
00077 }
00078 
00079 static void
00080 service_gdbus_wrapper_class_init(ServiceGDBusWrapperClass* klass)
00081 {
00082   G_OBJECT_CLASS(klass)->dispose = service_gdbus_wrapper_dispose;
00083   g_type_class_add_private (klass, sizeof (ServiceGDBusWrapperPrivate));
00084 
00085   if (node_info == NULL) 
00086   {
00087     GError * error = NULL;
00088 
00089     node_info = g_dbus_node_info_new_for_xml(gdbus_wrapper_interface, &error);
00090     if (error != NULL) 
00091     {
00092         g_error("Unable to parse GDBUS_WRAPPER interface: %s", error->message);
00093         g_error_free(error);
00094     }
00095   }
00096 
00097   if (node_info != NULL && iface_info == NULL) 
00098   {
00099     iface_info = g_dbus_node_info_lookup_interface(node_info,"com.canonical.gdbus_wrapper");
00100     if (iface_info == NULL) 
00101     {
00102       g_error("Unable to find interface 'com.canonical.gdbus_wrapper'");
00103     }
00104   }
00105 
00106 }
00107 
00108 static void
00109 service_gdbus_wrapper_init(ServiceGDBusWrapper* self)
00110 {
00111   self->priv = G_TYPE_INSTANCE_GET_PRIVATE(self, SERVICE_TYPE_GDBUS_WRAPPER, ServiceGDBusWrapperPrivate);
00112   self->priv->bus = NULL;
00113   self->priv->bus_lookup = NULL;
00114   self->priv->bus_registration = 0;
00115   
00116   self->priv->bus_lookup = g_cancellable_new();
00117   g_bus_get(G_BUS_TYPE_SESSION, self->priv->bus_lookup, bus_got_cb, self);
00118   
00119 }
00120 
00121 ServiceGDBusWrapper*
00122 service_gdbus_wrapper_new()
00123 {
00124   return g_object_new(SERVICE_TYPE_GDBUS_WRAPPER, NULL);
00125 }
00126 
00127 static void
00128 bus_got_cb (GObject *object, GAsyncResult * res, gpointer user_data)
00129 {
00130   GError * error = NULL;
00131   ServiceGDBusWrapper * self = SERVICE_GDBUS_WRAPPER(user_data);
00132   GDBusConnection * bus;
00133 
00134   bus = g_bus_get_finish(res, &error);
00135   if (error != NULL) {
00136     g_critical("Unable to get bus: %s", error->message);
00137     g_error_free(error);
00138     return;
00139   }
00140 
00141   self->priv->bus = bus;
00142 
00143   /* Register object */
00144   self->priv->bus_registration = g_dbus_connection_register_object(bus,
00145                                                   /* path */       "/com/canonical/gdbus_wrapper",
00146                                                   /* interface */  iface_info,
00147                                                   /* vtable */     &bus_vtable,
00148                                                   /* userdata */   self,
00149                                                   /* destroy */    NULL,
00150                                                   /* error */      &error);
00151 
00152   if (error != NULL) {
00153     g_critical ("Unable to create bus connection object, %s", error->message);
00154     g_error_free(error);
00155     return;
00156   }
00157   
00158   return;
00159 }
00160 
00161 static void
00162 bus_method (GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *method_name, GVariant *parameters, GDBusMethodInvocation *invocation, gpointer user_data)
00163 { 
00164   if (g_strcmp0(method_name, "TestMethod") == 0) 
00165   {
00166     GVariant * ret = NULL;
00167     GVariant * sig_data = NULL;
00168     GVariant * tmp = NULL;
00169     gchar * query = NULL;
00170     GError * error = NULL;
00171     g_variant_get(parameters, "(s)", &query);
00172     
00173     tmp = g_variant_new_string(query);
00174     ret = g_variant_new_tuple(&tmp, 1);
00175     g_dbus_method_invocation_return_value(invocation, ret);
00176     tmp = NULL; 
00177      
00178     // emit a signal with the same string as passed in every time this method is called
00179     
00180     tmp = g_variant_new_string(query);
00181     sig_data = g_variant_new_tuple(&tmp, 1);    
00182     g_dbus_connection_emit_signal(connection,
00183                                   NULL,                            /* destination bus, we don't care */
00184                                   "/com/canonical/gdbus_wrapper",  /* object path */
00185                                   "com.canonical.gdbus_wrapper",   /* interface name */
00186                                   "TestSignal",                    /* Signal name */
00187                                   sig_data,                        /* parameter */
00188                                   &error);                         /* error */
00189                                     
00190     if (error != NULL)
00191     {
00192       g_critical ("could not emit signal TestSignal with data %s", query);
00193       g_error_free(error);
00194     }
00195 
00196     g_free(query);
00197   }
00198 
00199   return;
00200 }
00201