gtkcolorpickershell.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* GTK - The GIMP Toolkit
  2. * Copyright (C) 2018, Red Hat, Inc
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "config.h"
  18. #include "gtkcolorpickershellprivate.h"
  19. #include <gio/gio.h>
  20. struct _GtkColorPickerShell
  21. {
  22. GObject parent_instance;
  23. GDBusProxy *shell_proxy;
  24. GTask *task;
  25. };
  26. struct _GtkColorPickerShellClass
  27. {
  28. GObjectClass parent_class;
  29. };
  30. static GInitableIface *initable_parent_iface;
  31. static void gtk_color_picker_shell_initable_iface_init (GInitableIface *iface);
  32. static void gtk_color_picker_shell_iface_init (GtkColorPickerInterface *iface);
  33. G_DEFINE_TYPE_WITH_CODE (GtkColorPickerShell, gtk_color_picker_shell, G_TYPE_OBJECT,
  34. G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, gtk_color_picker_shell_initable_iface_init)
  35. G_IMPLEMENT_INTERFACE (GTK_TYPE_COLOR_PICKER, gtk_color_picker_shell_iface_init))
  36. static gboolean
  37. gtk_color_picker_shell_initable_init (GInitable *initable,
  38. GCancellable *cancellable,
  39. GError **error)
  40. {
  41. GtkColorPickerShell *picker = GTK_COLOR_PICKER_SHELL (initable);
  42. char *owner;
  43. picker->shell_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
  44. G_DBUS_PROXY_FLAGS_NONE,
  45. NULL,
  46. "org.gnome.Shell.Screenshot",
  47. "/org/gnome/Shell/Screenshot",
  48. "org.gnome.Shell.Screenshot",
  49. NULL,
  50. error);
  51. if (picker->shell_proxy == NULL)
  52. {
  53. g_debug ("Failed to create shell screenshot proxy");
  54. return FALSE;
  55. }
  56. owner = g_dbus_proxy_get_name_owner (picker->shell_proxy);
  57. if (owner == NULL)
  58. {
  59. g_debug ("org.gnome.Shell.Screenshot not provided");
  60. g_clear_object (&picker->shell_proxy);
  61. return FALSE;
  62. }
  63. g_free (owner);
  64. return TRUE;
  65. }
  66. static void
  67. gtk_color_picker_shell_initable_iface_init (GInitableIface *iface)
  68. {
  69. initable_parent_iface = g_type_interface_peek_parent (iface);
  70. iface->init = gtk_color_picker_shell_initable_init;
  71. }
  72. static void
  73. gtk_color_picker_shell_init (GtkColorPickerShell *picker)
  74. {
  75. }
  76. static void
  77. gtk_color_picker_shell_finalize (GObject *object)
  78. {
  79. GtkColorPickerShell *picker = GTK_COLOR_PICKER_SHELL (object);
  80. g_clear_object (&picker->shell_proxy);
  81. G_OBJECT_CLASS (gtk_color_picker_shell_parent_class)->finalize (object);
  82. }
  83. static void
  84. gtk_color_picker_shell_class_init (GtkColorPickerShellClass *class)
  85. {
  86. GObjectClass *object_class = G_OBJECT_CLASS (class);
  87. object_class->finalize = gtk_color_picker_shell_finalize;
  88. }
  89. GtkColorPicker *
  90. gtk_color_picker_shell_new (void)
  91. {
  92. return GTK_COLOR_PICKER (g_initable_new (GTK_TYPE_COLOR_PICKER_SHELL, NULL, NULL, NULL));
  93. }
  94. static void
  95. color_picked (GObject *source,
  96. GAsyncResult *res,
  97. gpointer data)
  98. {
  99. GtkColorPickerShell *picker = GTK_COLOR_PICKER_SHELL (data);
  100. GError *error = NULL;
  101. GVariant *ret, *dict;
  102. ret = g_dbus_proxy_call_finish (picker->shell_proxy, res, &error);
  103. if (ret == NULL)
  104. {
  105. g_task_return_error (picker->task, error);
  106. }
  107. else
  108. {
  109. double d1, d2, d3;
  110. g_variant_get (ret, "(@a{sv})", &dict);
  111. if (!g_variant_lookup (dict, "color", "(ddd)", &d1, &d2, &d3))
  112. g_task_return_new_error (picker->task, G_IO_ERROR, G_IO_ERROR_FAILED, "No color received");
  113. else
  114. g_task_return_pointer (picker->task,
  115. gdk_rgba_copy (&(GdkRGBA){d1, d2, d3, 1.0f}), (GDestroyNotify)gdk_rgba_free);
  116. g_variant_unref (dict);
  117. g_variant_unref (ret);
  118. }
  119. g_clear_object (&picker->task);
  120. }
  121. static void
  122. gtk_color_picker_shell_pick (GtkColorPicker *cp,
  123. GAsyncReadyCallback callback,
  124. gpointer user_data)
  125. {
  126. GtkColorPickerShell *picker = GTK_COLOR_PICKER_SHELL (cp);
  127. if (picker->task)
  128. return;
  129. picker->task = g_task_new (picker, NULL, callback, user_data);
  130. g_dbus_proxy_call (picker->shell_proxy,
  131. "PickColor",
  132. NULL,
  133. 0,
  134. -1,
  135. NULL,
  136. color_picked,
  137. picker);
  138. }
  139. static GdkRGBA *
  140. gtk_color_picker_shell_pick_finish (GtkColorPicker *cp,
  141. GAsyncResult *res,
  142. GError **error)
  143. {
  144. g_return_val_if_fail (g_task_is_valid (res, cp), NULL);
  145. return g_task_propagate_pointer (G_TASK (res), error);
  146. }
  147. static void
  148. gtk_color_picker_shell_iface_init (GtkColorPickerInterface *iface)
  149. {
  150. iface->pick = gtk_color_picker_shell_pick;
  151. iface->pick_finish = gtk_color_picker_shell_pick_finish;
  152. }