static gboolean
theme_is_equal (const MateThemeMetaInfo *a, const MateThemeMetaInfo *b)
{
  gboolean a_set, b_set;

  if (!(a->gtk_theme_name && b->gtk_theme_name) ||
      strcmp (a->gtk_theme_name, b->gtk_theme_name))
    return FALSE;

  if (!(a->icon_theme_name && b->icon_theme_name) ||
      strcmp (a->icon_theme_name, b->icon_theme_name))
    return FALSE;

  if (!(a->marco_theme_name && b->marco_theme_name) ||
      strcmp (a->marco_theme_name, b->marco_theme_name))
    return FALSE;

  if (!(a->cursor_theme_name && b->cursor_theme_name) ||
      strcmp (a->cursor_theme_name, b->cursor_theme_name))
    return FALSE;

  if (a->cursor_size != b->cursor_size)
    return FALSE;

  a_set = a->gtk_color_scheme && strcmp (a->gtk_color_scheme, "");
  b_set = b->gtk_color_scheme && strcmp (b->gtk_color_scheme, "");
  if ((a_set != b_set) ||
      (a_set && !mate_theme_color_scheme_equal (a->gtk_color_scheme, b->gtk_color_scheme)))
    return FALSE;

  return TRUE;
}
static void
color_button_clicked_cb (GtkWidget *colorbutton, AppearanceData *data)
{
  GtkWidget *widget;
  GdkColor color;
  GString *scheme = g_string_new (NULL);
  gchar *colstr;
  gchar *old_scheme = NULL;
  gint i;

  for (i = 0; i < NUM_SYMBOLIC_COLORS; ++i) {
    widget = appearance_capplet_get_widget (data, symbolic_names[i]);
    gtk_color_button_get_color (GTK_COLOR_BUTTON (widget), &color);

    colstr = gdk_color_to_string (&color);
    g_string_append_printf (scheme, "%s:%s\n", symbolic_names[i], colstr);
    g_free (colstr);
  }
  /* remove the last newline */
  g_string_truncate (scheme, scheme->len - 1);

  /* verify that the scheme really has changed */
  g_object_get (gtk_settings_get_default (), "gtk-color-scheme", &old_scheme, NULL);

  if (!mate_theme_color_scheme_equal (old_scheme, scheme->str)) {
    g_settings_set_string (data->interface_settings, COLOR_SCHEME_KEY, scheme->str);

    gtk_widget_set_sensitive (appearance_capplet_get_widget (data, "color_scheme_defaults_button"), TRUE);
  }
  g_free (old_scheme);
  g_string_free (scheme, TRUE);
}
void
mate_meta_theme_set (MateThemeMetaInfo *meta_theme_info)
{
  GSettings *interface_settings;
  GSettings *marco_settings;
  GSettings *mouse_settings;
  GSettings *notification_settings = NULL;
  const char * const *schemas;
  gboolean schema_exists;
  gint i;
  gchar *old_key;
  gint old_key_int;

  interface_settings = g_settings_new (INTERFACE_SCHEMA);
  marco_settings = g_settings_new (MARCO_SCHEMA);
  mouse_settings = g_settings_new (MOUSE_SCHEMA);
  
  /* We  need this because mate-control-center does not depend on mate-notification-daemon,
   * and if we try to get notification theme without schema installed, gsettings crashes
   * see https://bugzilla.gnome.org/show_bug.cgi?id=651225 */
  schemas = g_settings_list_schemas ();
  schema_exists = FALSE;
  for (i = 0; schemas[i] != NULL; i++) {
    if (g_strcmp0 (schemas[i], NOTIFICATION_SCHEMA) == 0) {
      schema_exists = TRUE;
      break;
      }
  }
  if (schema_exists == TRUE) {
      notification_settings = g_settings_new (NOTIFICATION_SCHEMA);
  }

  /* Set the gtk+ key */
  old_key = g_settings_get_string (interface_settings, GTK_THEME_KEY);
  if (compare (old_key, meta_theme_info->gtk_theme_name))
    {
      g_settings_set_string (interface_settings, GTK_THEME_KEY, meta_theme_info->gtk_theme_name);
    }
  g_free (old_key);

  /* Set the color scheme key */
  old_key = g_settings_get_string (interface_settings, COLOR_SCHEME_KEY);
  if (compare (old_key, meta_theme_info->gtk_color_scheme))
    {
      /* only save the color scheme if it differs from the default
         scheme for the selected gtk theme */
      gchar *newval, *gtkcols;

      newval = meta_theme_info->gtk_color_scheme;
      gtkcols = gtkrc_get_color_scheme_for_theme (meta_theme_info->gtk_theme_name);

      if (newval == NULL || !strcmp (newval, "") ||
          mate_theme_color_scheme_equal (newval, gtkcols))
        {
          g_settings_reset (interface_settings, COLOR_SCHEME_KEY);
        }
      else
        {
          g_settings_set_string (interface_settings, COLOR_SCHEME_KEY, newval);
        }
      g_free (gtkcols);
    }
  g_free (old_key);

  /* Set the wm key */
  g_settings_set_string (marco_settings, MARCO_THEME_KEY, meta_theme_info->marco_theme_name);

  /* set the icon theme */
  old_key = g_settings_get_string (interface_settings, ICON_THEME_KEY);
  if (compare (old_key, meta_theme_info->icon_theme_name))
    {
      g_settings_set_string (interface_settings, ICON_THEME_KEY, meta_theme_info->icon_theme_name);
    }
  g_free (old_key);

  /* set the notification theme */
  if (notification_settings != NULL)
    {
    if (meta_theme_info->notification_theme_name != NULL)
      {
        old_key = g_settings_get_string (notification_settings, NOTIFICATION_THEME_KEY);
        if (compare (old_key, meta_theme_info->notification_theme_name))
          {
            g_settings_set_string (notification_settings, NOTIFICATION_THEME_KEY, meta_theme_info->notification_theme_name);
          }
        g_free (old_key);
      }
    }

  /* Set the cursor theme key */
#ifdef HAVE_XCURSOR
  old_key = g_settings_get_string (mouse_settings, CURSOR_THEME_KEY);
  if (compare (old_key, meta_theme_info->cursor_theme_name))
    {
      g_settings_set_string (mouse_settings, CURSOR_THEME_KEY, meta_theme_info->cursor_theme_name);
    }

  old_key_int = g_settings_get_int (mouse_settings, CURSOR_SIZE_KEY);
  if (old_key_int != meta_theme_info->cursor_size)
    {
      g_settings_set_int (mouse_settings, CURSOR_SIZE_KEY, meta_theme_info->cursor_size);
    }
#else
  old_key = g_settings_get_string (mouse_settings, CURSOR_FONT_KEY);
  if (compare (old_key, meta_theme_info->cursor_theme_name))
    {
      g_settings_set_string (mouse_settings, CURSOR_FONT_KEY, meta_theme_info->cursor_theme_name);
    }
#endif

  g_free (old_key);
  g_object_unref (interface_settings);
  g_object_unref (marco_settings);
  g_object_unref (mouse_settings);
  if (notification_settings != NULL)
    g_object_unref (notification_settings);
}
void
mate_meta_theme_set (MateThemeMetaInfo *meta_theme_info)
{
  GSettings *interface_settings;
  GSettings *marco_settings;
  GSettings *mouse_settings;
  GSettings *notification_settings = NULL;
  gchar *old_key;
  gint old_key_int;

  interface_settings = g_settings_new (INTERFACE_SCHEMA);
  marco_settings = g_settings_new (MARCO_SCHEMA);
  mouse_settings = g_settings_new (MOUSE_SCHEMA);
  
  if (mate_gsettings_schema_exists (NOTIFICATION_SCHEMA))
    {
      notification_settings = g_settings_new (NOTIFICATION_SCHEMA);
    }

  /* Set the gtk+ key */
  old_key = g_settings_get_string (interface_settings, GTK_THEME_KEY);
  if (compare (old_key, meta_theme_info->gtk_theme_name))
    {
      g_settings_set_string (interface_settings, GTK_THEME_KEY, meta_theme_info->gtk_theme_name);
    }
  g_free (old_key);

  /* Set the color scheme key */
  old_key = g_settings_get_string (interface_settings, COLOR_SCHEME_KEY);
  if (compare (old_key, meta_theme_info->gtk_color_scheme))
    {
      /* only save the color scheme if it differs from the default
         scheme for the selected gtk theme */
      gchar *newval, *gtkcols;

      newval = meta_theme_info->gtk_color_scheme;
      gtkcols = gtkrc_get_color_scheme_for_theme (meta_theme_info->gtk_theme_name);

      if (newval == NULL || !strcmp (newval, "") ||
          mate_theme_color_scheme_equal (newval, gtkcols))
        {
          g_settings_reset (interface_settings, COLOR_SCHEME_KEY);
        }
      else
        {
          g_settings_set_string (interface_settings, COLOR_SCHEME_KEY, newval);
        }
      g_free (gtkcols);
    }
  g_free (old_key);

  /* Set the wm key */
  g_settings_set_string (marco_settings, MARCO_THEME_KEY, meta_theme_info->marco_theme_name);

  /* set the icon theme */
  old_key = g_settings_get_string (interface_settings, ICON_THEME_KEY);
  if (compare (old_key, meta_theme_info->icon_theme_name))
    {
      g_settings_set_string (interface_settings, ICON_THEME_KEY, meta_theme_info->icon_theme_name);
    }
  g_free (old_key);

  /* set the notification theme */
  if (notification_settings != NULL)
    {
    if (meta_theme_info->notification_theme_name != NULL)
      {
        old_key = g_settings_get_string (notification_settings, NOTIFICATION_THEME_KEY);
        if (compare (old_key, meta_theme_info->notification_theme_name))
          {
            g_settings_set_string (notification_settings, NOTIFICATION_THEME_KEY, meta_theme_info->notification_theme_name);
          }
        g_free (old_key);
      }
    }

  /* Set the cursor theme key */
#ifdef HAVE_XCURSOR
  old_key = g_settings_get_string (mouse_settings, CURSOR_THEME_KEY);
  if (compare (old_key, meta_theme_info->cursor_theme_name))
    {
      g_settings_set_string (mouse_settings, CURSOR_THEME_KEY, meta_theme_info->cursor_theme_name);
    }

  old_key_int = g_settings_get_int (mouse_settings, CURSOR_SIZE_KEY);
  if (old_key_int != meta_theme_info->cursor_size)
    {
      g_settings_set_int (mouse_settings, CURSOR_SIZE_KEY, meta_theme_info->cursor_size);
    }
#else
  old_key = g_settings_get_string (mouse_settings, CURSOR_FONT_KEY);
  if (compare (old_key, meta_theme_info->cursor_theme_name))
    {
      g_settings_set_string (mouse_settings, CURSOR_FONT_KEY, meta_theme_info->cursor_theme_name);
    }
#endif

  g_free (old_key);
  g_object_unref (interface_settings);
  g_object_unref (marco_settings);
  g_object_unref (mouse_settings);
  if (notification_settings != NULL)
    g_object_unref (notification_settings);
}