You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

170 lines
5.1 KiB

  1. /*
  2. * Copyright (C) 2009-2010 Nick Schermer <nick@xfce.org>
  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.1 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, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <dbus/dbus-glib.h>
  22. #include <common/panel-private.h>
  23. #include <common/panel-xfconf.h>
  24. #include <libxfce4panel/xfce-panel-macros.h>
  25. static void
  26. panel_properties_store_value (XfconfChannel *channel,
  27. const gchar *xfconf_property,
  28. GType xfconf_property_type,
  29. GObject *object,
  30. const gchar *object_property)
  31. {
  32. GValue value = { 0, };
  33. GdkColor *color;
  34. guint16 alpha = 0xffff;
  35. #ifndef NDEBUG
  36. GParamSpec *pspec;
  37. #endif
  38. panel_return_if_fail (G_IS_OBJECT (object));
  39. panel_return_if_fail (XFCONF_IS_CHANNEL (channel));
  40. #ifndef NDEBUG
  41. /* check if the types match */
  42. pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), object_property);
  43. panel_assert (pspec != NULL);
  44. if (G_PARAM_SPEC_VALUE_TYPE (pspec) != xfconf_property_type)
  45. {
  46. g_critical ("Object and Xfconf properties don't match! %s::%s. %s != %s",
  47. G_OBJECT_TYPE_NAME (object), xfconf_property,
  48. g_type_name (xfconf_property_type),
  49. g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
  50. }
  51. #endif
  52. /* write the property to the xfconf channel */
  53. g_value_init (&value, xfconf_property_type);
  54. g_object_get_property (G_OBJECT (object), object_property, &value);
  55. if (G_LIKELY (xfconf_property_type != GDK_TYPE_COLOR))
  56. {
  57. xfconf_channel_set_property (channel, xfconf_property, &value);
  58. }
  59. else
  60. {
  61. /* work around xfconf's lack of storing colors (bug #7117) and
  62. * do the same as xfconf_g_property_bind_gdkcolor() does */
  63. color = g_value_get_boxed (&value);
  64. xfconf_channel_set_array (channel, xfconf_property,
  65. XFCONF_TYPE_UINT16, &color->red,
  66. XFCONF_TYPE_UINT16, &color->green,
  67. XFCONF_TYPE_UINT16, &color->blue,
  68. XFCONF_TYPE_UINT16, &alpha,
  69. G_TYPE_INVALID);
  70. }
  71. g_value_unset (&value);
  72. }
  73. XfconfChannel *
  74. panel_properties_get_channel (GObject *object_for_weak_ref)
  75. {
  76. GError *error = NULL;
  77. XfconfChannel *channel;
  78. panel_return_val_if_fail (G_IS_OBJECT (object_for_weak_ref), NULL);
  79. if (!xfconf_init (&error))
  80. {
  81. g_critical ("Failed to initialize Xfconf: %s", error->message);
  82. g_error_free (error);
  83. return NULL;
  84. }
  85. channel = xfconf_channel_get (XFCE_PANEL_CHANNEL_NAME);
  86. g_object_weak_ref (object_for_weak_ref, (GWeakNotify) xfconf_shutdown, NULL);
  87. return channel;
  88. }
  89. void
  90. panel_properties_bind (XfconfChannel *channel,
  91. GObject *object,
  92. const gchar *property_base,
  93. const PanelProperty *properties,
  94. gboolean save_properties)
  95. {
  96. const PanelProperty *prop;
  97. gchar *property;
  98. panel_return_if_fail (channel == NULL || XFCONF_IS_CHANNEL (channel));
  99. panel_return_if_fail (G_IS_OBJECT (object));
  100. panel_return_if_fail (property_base != NULL && *property_base == '/');
  101. panel_return_if_fail (properties != NULL);
  102. if (G_LIKELY (channel == NULL))
  103. channel = panel_properties_get_channel (object);
  104. panel_return_if_fail (XFCONF_IS_CHANNEL (channel));
  105. /* walk the properties array */
  106. for (prop = properties; prop->property != NULL; prop++)
  107. {
  108. property = g_strconcat (property_base, "/", prop->property, NULL);
  109. if (save_properties)
  110. panel_properties_store_value (channel, property, prop->type, object, prop->property);
  111. if (G_LIKELY (prop->type != GDK_TYPE_COLOR))
  112. xfconf_g_property_bind (channel, property, prop->type, object, prop->property);
  113. else
  114. xfconf_g_property_bind_gdkcolor (channel, property, object, prop->property);
  115. g_free (property);
  116. }
  117. }
  118. void
  119. panel_properties_unbind (GObject *object)
  120. {
  121. xfconf_g_property_unbind_all (object);
  122. }
  123. GType
  124. panel_properties_value_array_get_type (void)
  125. {
  126. static volatile gsize type__volatile = 0;
  127. GType type;
  128. if (g_once_init_enter (&type__volatile))
  129. {
  130. type = dbus_g_type_get_collection ("GPtrArray", G_TYPE_VALUE);
  131. g_once_init_leave (&type__volatile, type);
  132. }
  133. return type__volatile;
  134. }