patch-libupower-glib_up-client_c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. $OpenBSD: patch-libupower-glib_up-client_c,v 1.5 2016/09/01 10:11:47 landry Exp $
  2. Backport https://cgit.freedesktop.org/upower/patch/?id=932a6a39e35754be571e1274aec4730fd42dba13
  3. https://bugs.freedesktop.org/show_bug.cgi?id=95350
  4. --- libupower-glib/up-client.c.orig Wed Jul 29 14:47:49 2015
  5. +++ libupower-glib/up-client.c Wed Aug 31 12:31:10 2016
  6. @@ -39,9 +39,10 @@
  7. #include "up-daemon-generated.h"
  8. #include "up-device.h"
  9. -static void up_client_class_init (UpClientClass *klass);
  10. -static void up_client_init (UpClient *client);
  11. -static void up_client_finalize (GObject *object);
  12. +static void up_client_class_init (UpClientClass *klass);
  13. +static void up_client_initable_iface_init (GInitableIface *iface);
  14. +static void up_client_init (UpClient *client);
  15. +static void up_client_finalize (GObject *object);
  16. #define UP_CLIENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), UP_TYPE_CLIENT, UpClientPrivate))
  17. @@ -73,7 +74,8 @@ enum {
  18. static guint signals [UP_CLIENT_LAST_SIGNAL] = { 0 };
  19. static gpointer up_client_object = NULL;
  20. -G_DEFINE_TYPE (UpClient, up_client, G_TYPE_OBJECT)
  21. +G_DEFINE_TYPE_WITH_CODE (UpClient, up_client, G_TYPE_OBJECT,
  22. + G_IMPLEMENT_INTERFACE(G_TYPE_INITABLE, up_client_initable_iface_init))
  23. /**
  24. * up_client_get_devices:
  25. @@ -434,11 +436,10 @@ up_client_class_init (UpClientClass *klass)
  26. * up_client_init:
  27. * @client: This class instance
  28. */
  29. -static void
  30. -up_client_init (UpClient *client)
  31. +static gboolean
  32. +up_client_initable_init (GInitable *initable, GCancellable *cancellable, GError **error)
  33. {
  34. - GError *error = NULL;
  35. -
  36. + UpClient *client = UP_CLIENT (initable);
  37. client->priv = UP_CLIENT_GET_PRIVATE (client);
  38. /* connect to main interface */
  39. @@ -446,13 +447,10 @@ up_client_init (UpClient *client)
  40. G_DBUS_PROXY_FLAGS_NONE,
  41. "org.freedesktop.UPower",
  42. "/org/freedesktop/UPower",
  43. - NULL,
  44. - &error);
  45. - if (client->priv->proxy == NULL) {
  46. - g_warning ("Couldn't connect to proxy: %s", error->message);
  47. - g_error_free (error);
  48. - return;
  49. - }
  50. + cancellable,
  51. + error);
  52. + if (client->priv->proxy == NULL)
  53. + return FALSE;
  54. /* all callbacks */
  55. g_signal_connect (client->priv->proxy, "device-added",
  56. @@ -461,9 +459,26 @@ up_client_init (UpClient *client)
  57. G_CALLBACK (up_device_removed_cb), client);
  58. g_signal_connect (client->priv->proxy, "notify",
  59. G_CALLBACK (up_client_notify_cb), client);
  60. +
  61. + return TRUE;
  62. }
  63. +static void
  64. +up_client_initable_iface_init (GInitableIface *iface)
  65. +{
  66. + iface->init = up_client_initable_init;
  67. +}
  68. +
  69. /*
  70. + * up_client_init:
  71. + * @client: This class instance
  72. + */
  73. +static void
  74. +up_client_init (UpClient *client)
  75. +{
  76. +}
  77. +
  78. +/*
  79. * up_client_finalize:
  80. */
  81. static void
  82. @@ -482,23 +497,52 @@ up_client_finalize (GObject *object)
  83. }
  84. /**
  85. - * up_client_new:
  86. + * up_client_new_full:
  87. + * @cancellable: (allow-none): A #GCancellable or %NULL.
  88. + * @error: Return location for error or %NULL.
  89. *
  90. - * Creates a new #UpClient object.
  91. + * Creates a new #UpClient object. If connecting to upowerd on D-Bus fails,
  92. + % this returns %NULL and sets @error.
  93. *
  94. - * Return value: a new UpClient object.
  95. + * Return value: a new UpClient object, or %NULL on failure.
  96. *
  97. - * Since: 0.9.0
  98. + * Since: 0.99.5
  99. **/
  100. UpClient *
  101. -up_client_new (void)
  102. +up_client_new_full (GCancellable *cancellable, GError **error)
  103. {
  104. if (up_client_object != NULL) {
  105. g_object_ref (up_client_object);
  106. } else {
  107. - up_client_object = g_object_new (UP_TYPE_CLIENT, NULL);
  108. - g_object_add_weak_pointer (up_client_object, &up_client_object);
  109. + up_client_object = g_initable_new (UP_TYPE_CLIENT, cancellable, error, NULL);
  110. + if (up_client_object)
  111. + g_object_add_weak_pointer (up_client_object, &up_client_object);
  112. }
  113. return UP_CLIENT (up_client_object);
  114. +}
  115. +
  116. +/**
  117. + * up_client_new:
  118. + *
  119. + * Creates a new #UpClient object. If connecting to upowerd on D-Bus fails,
  120. + * this returns %NULL and prints out a warning with the error message.
  121. + * Consider using up_client_new_full() instead which allows you to handle errors
  122. + * and cancelling long operations yourself.
  123. + *
  124. + * Return value: a new UpClient object, or %NULL on failure.
  125. + *
  126. + * Since: 0.9.0
  127. + **/
  128. +UpClient *
  129. +up_client_new (void)
  130. +{
  131. + GError *error = NULL;
  132. + UpClient *client;
  133. + client = up_client_new_full (NULL, &error);
  134. + if (client == NULL) {
  135. + g_warning ("Couldn't connect to proxy: %s", error->message);
  136. + g_error_free (error);
  137. + }
  138. + return client;
  139. }