Beispiel #1
0
gboolean packagekit_available (void)
{
  DBusGConnection *connection;
  DBusGProxy *proxy;
  gboolean available = FALSE;

  connection = dbus_g_bus_get (DBUS_BUS_SESSION, NULL);
  if (connection == NULL) {
    return FALSE;
  }

  proxy = dbus_g_proxy_new_for_name (connection,
                                     DBUS_SERVICE_DBUS,
                                     DBUS_PATH_DBUS,
                                     DBUS_INTERFACE_DBUS);

  org_freedesktop_DBus_name_has_owner (proxy,
                                       "org.freedesktop.PackageKit",
                                       &available,
                                       NULL);

  g_object_unref (proxy);
  dbus_g_connection_unref (connection);

  return available;
}
int
main (int argc, char *argv[])
{
	DBusGConnection *bus;
	DBusGProxy *bus_proxy;
	GMainLoop *loop = NULL;
	GError *err = NULL;
	gboolean nm_running;

	/* Initialize GType system */
	g_type_init ();

	g_print ("Monitor 'org.freedesktop.NetworkManager' D-Bus name\n");
	g_print ("===================================================\n");

	/* Get system bus */
	bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL);

	/* Create a D-Bus proxy to D-Bus daemon */
	bus_proxy = dbus_g_proxy_new_for_name (bus,
	                                       "org.freedesktop.DBus",
	                                       "/org/freedesktop/DBus",
	                                       "org.freedesktop.DBus");

	if (!bus_proxy) {
		g_message ("Error: Couldn't create D-Bus object proxy for org.freedesktop.DBus.");
		dbus_g_connection_unref (bus);
		return -1;
	}

	/* Call NameHasOwner method to find out if NM is running. When NM runs it claims
	 * 'org.freedesktop.NetworkManager' service name on D-Bus */
	if (!org_freedesktop_DBus_name_has_owner (bus_proxy, NM_DBUS_SERVICE, &nm_running, &err)) {
		g_message ("Error: NameHasOwner request failed: %s",
		                 (err && err->message) ? err->message : "(unknown)");
		g_clear_error (&err);
		g_object_unref (bus_proxy);
		dbus_g_connection_unref (bus);
		return -1;
	}
	g_print ("NM is %s\n", nm_running ? "running" : "not running");


	/* Connect to NameOwnerChanged signal to monitor NM running state */
	dbus_g_proxy_add_signal (bus_proxy, "NameOwnerChanged",
	                         G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
	                         G_TYPE_INVALID);
	dbus_g_proxy_connect_signal (bus_proxy,
	                             "NameOwnerChanged",
	                             G_CALLBACK (proxy_name_owner_changed),
	                             &nm_running, NULL);

	loop = g_main_loop_new (NULL, FALSE);  /* Create main loop */
	g_main_loop_run (loop);                /* Run main loop */

	g_object_unref (bus_proxy);
	dbus_g_connection_unref (bus);

	return 0;
}
int
main (int argv, char ** argc)
{
	g_type_init();

	g_debug("Waiting to init.");


	GError * error = NULL;
	DBusGConnection * session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
	if (error != NULL) {
		g_error("Unable to get session bus: %s", error->message);
		return 1;
	}

    DBusGProxy * bus_proxy = dbus_g_proxy_new_for_name(session_bus, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);

	gboolean has_owner = FALSE;
	gint owner_count = 0;
	while (!has_owner && owner_count < 10000) {
		org_freedesktop_DBus_name_has_owner(bus_proxy, "org.test", &has_owner, NULL);
		owner_count++;
	}

	if (owner_count == 10000) {
		g_error("Unable to get name owner after 10000 tries");
		return 1;
	}

	g_usleep(500000);

	g_debug("Initing");

	guint nameret = 0;

	if (!org_freedesktop_DBus_request_name(bus_proxy, NOTIFICATION_WATCHER_DBUS_ADDR, 0, &nameret, &error)) {
		g_error("Unable to call to request name");
		return 1;
	}   

	if (nameret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
		g_error("Unable to get name");
		return 1;
	}

	dbus_connection_add_filter(dbus_g_connection_get_connection(session_bus), dbus_filter, NULL, NULL);

	/* This is the final kill function.  It really shouldn't happen
	   unless we get an error. */
	g_timeout_add(2000, kill_func, NULL);

	g_debug("Entering Mainloop");

	mainloop = g_main_loop_new(NULL, FALSE);
	g_main_loop_run(mainloop);

	g_debug("Exiting");

	return 0;
}