static MateThemeMetaInfo *
theme_load_from_gsettings (AppearanceData *data)
{
  MateThemeMetaInfo *theme;
  gchar *scheme;

  theme = mate_theme_meta_info_new ();

  theme->gtk_theme_name = g_settings_get_string (data->interface_settings, GTK_THEME_KEY);
  if (theme->gtk_theme_name == NULL)
    theme->gtk_theme_name = g_strdup ("Menta");

  scheme = g_settings_get_string (data->interface_settings, COLOR_SCHEME_KEY);
  if (scheme == NULL || !strcmp (scheme, "")) {
    g_free (scheme);
    scheme = gtkrc_get_color_scheme_for_theme (theme->gtk_theme_name);
  }
  theme->gtk_color_scheme = scheme;

  theme->marco_theme_name = g_settings_get_string (data->marco_settings, MARCO_THEME_KEY);
  if (theme->marco_theme_name == NULL)
    theme->marco_theme_name = g_strdup ("Menta");

  theme->icon_theme_name = g_settings_get_string (data->interface_settings, ICON_THEME_KEY);
  if (theme->icon_theme_name == NULL)
    theme->icon_theme_name = g_strdup ("menta");

  if (mate_gsettings_schema_exists (NOTIFICATION_SCHEMA)) {
    GSettings *notification_settings;
    notification_settings = g_settings_new (NOTIFICATION_SCHEMA);
    theme->notification_theme_name = g_settings_get_string (notification_settings, NOTIFICATION_THEME_KEY);
    g_object_unref (notification_settings);
  }
  else
    theme->notification_theme_name = NULL;

  theme->cursor_theme_name = g_settings_get_string (data->mouse_settings, CURSOR_THEME_KEY);
  theme->cursor_size = g_settings_get_int (data->mouse_settings, CURSOR_SIZE_KEY);

  if (theme->cursor_theme_name == NULL)
    theme->cursor_theme_name = g_strdup ("default");

  theme->application_font = g_settings_get_string (data->interface_settings, GTK_FONT_KEY);

  return theme;
}
static MateThemeMetaInfo *
theme_load_from_mateconf (MateConfClient *client)
{
  MateThemeMetaInfo *theme;
  gchar *scheme;

  theme = mate_theme_meta_info_new ();

  theme->gtk_theme_name = mateconf_client_get_string (client, GTK_THEME_KEY, NULL);
  if (theme->gtk_theme_name == NULL)
    theme->gtk_theme_name = g_strdup ("Clearlooks");

  scheme = mateconf_client_get_string (client, COLOR_SCHEME_KEY, NULL);
  if (scheme == NULL || !strcmp (scheme, "")) {
    g_free (scheme);
    scheme = gtkrc_get_color_scheme_for_theme (theme->gtk_theme_name);
  }
  theme->gtk_color_scheme = scheme;

  theme->marco_theme_name = mateconf_client_get_string (client, MARCO_THEME_KEY, NULL);
  if (theme->marco_theme_name == NULL)
    theme->marco_theme_name = g_strdup ("Clearlooks");

  theme->icon_theme_name = mateconf_client_get_string (client, ICON_THEME_KEY, NULL);
  if (theme->icon_theme_name == NULL)
    theme->icon_theme_name = g_strdup ("mate");

  theme->notification_theme_name = mateconf_client_get_string (client, NOTIFICATION_THEME_KEY, NULL);

  theme->cursor_theme_name = mateconf_client_get_string (client, CURSOR_THEME_KEY, NULL);
#ifdef HAVE_XCURSOR
  theme->cursor_size = mateconf_client_get_int (client, CURSOR_SIZE_KEY, NULL);
#endif
  if (theme->cursor_theme_name == NULL)
    theme->cursor_theme_name = g_strdup ("default");

  theme->application_font = mateconf_client_get_string (client, APPLICATION_FONT_KEY, NULL);

  return theme;
}
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);
}
static MateThemeMetaInfo *
theme_load_from_gsettings (AppearanceData *data)
{
  MateThemeMetaInfo *theme;
  gchar *scheme;
  const char * const *schemas;
  gboolean schema_exists;
  gint i;

  theme = mate_theme_meta_info_new ();

  theme->gtk_theme_name = g_settings_get_string (data->interface_settings, GTK_THEME_KEY);
  if (theme->gtk_theme_name == NULL)
    theme->gtk_theme_name = g_strdup ("Clearlooks");

  scheme = g_settings_get_string (data->interface_settings, COLOR_SCHEME_KEY);
  if (scheme == NULL || !strcmp (scheme, "")) {
    g_free (scheme);
    scheme = gtkrc_get_color_scheme_for_theme (theme->gtk_theme_name);
  }
  theme->gtk_color_scheme = scheme;

  theme->marco_theme_name = g_settings_get_string (data->marco_settings, MARCO_THEME_KEY);
  if (theme->marco_theme_name == NULL)
    theme->marco_theme_name = g_strdup ("Clearlooks");

  theme->icon_theme_name = g_settings_get_string (data->interface_settings, ICON_THEME_KEY);
  if (theme->icon_theme_name == NULL)
    theme->icon_theme_name = g_strdup ("mate");

  /* 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) {
    GSettings *notification_settings;
    notification_settings = g_settings_new (NOTIFICATION_SCHEMA);
    theme->notification_theme_name = g_settings_get_string (notification_settings, NOTIFICATION_THEME_KEY);
    g_object_unref (notification_settings);
  }
  else
    theme->notification_theme_name = NULL;

  theme->cursor_theme_name = g_settings_get_string (data->mouse_settings, CURSOR_THEME_KEY);
#ifdef HAVE_XCURSOR
  theme->cursor_size = g_settings_get_int (data->mouse_settings, CURSOR_SIZE_KEY);
#endif
  if (theme->cursor_theme_name == NULL)
    theme->cursor_theme_name = g_strdup ("default");

  theme->application_font = g_settings_get_string (data->interface_settings, GTK_FONT_KEY);

  return theme;
}